Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 877cc904

Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt

  • ID 877cc9047c7f13462c4697d10f4e3b34e1f2fbbe
  • Vorgänger ee8e3637
  • Nachfolger 8b529ad7

DispositionManager: Lieferschein beim Erstellen nicht automatisch speichern

Unterschiede anzeigen:

SL/Controller/DispositionManager.pm
182 182
  my ($self) = @_;
183 183
  my @error_report;
184 184

  
185
  my $basket_items_ids = $::form->{ids};
186
  my $vendor_items_ids = $::form->{vendor_part_ids};
185
  my $basket_item_ids = $::form->{ids};
186
  my $vendor_item_ids = $::form->{vendor_part_ids};
187 187

  
188
  unless (($basket_items_ids && scalar @{ $basket_items_ids})
189
      || ( $vendor_items_ids && scalar @{ $vendor_items_ids}))
188
  unless (($basket_item_ids && scalar @{ $basket_item_ids})
189
      || ( $vendor_item_ids && scalar @{ $vendor_item_ids}))
190 190
    {
191 191
    $self->js->flash('error', t8('There are no items selected'));
192 192
    return $self->js->render();
193 193
  }
194
  my $v_id =  $::form->{vendor_ids}->[0] ;
195

  
196
  my ($vendor, $employee);
197
  $vendor   = SL::DB::Manager::Vendor->find_by(id => $v_id);
198
  $employee = SL::DB::Manager::Employee->current;
199

  
200

  
201
  my @orderitem_maps = (); # part, qty, orderer_id
202
  if ($basket_items_ids && scalar @{ $basket_items_ids}) {
203
    my $basket_items = SL::DB::Manager::PurchaseBasketItem->get_all(
204
      query => [ id => $basket_items_ids ],
205
      with_objects => ['part'],
206
    );
207
    push @orderitem_maps, map {{
208
        part       => $_->part,
209
        qty        => $_->qty,
210
        orderer_id => $_->orderer_id,
211
      }} @{$basket_items};
212
  }
213
  if ($vendor_items_ids && scalar @{ $vendor_items_ids}) {
214
    my $vendor_items = SL::DB::Manager::Part->get_all(
215
      query => [ id => $vendor_items_ids ] );
216
    push @orderitem_maps, map {{
217
        part       => $_,
218
        qty        => $_->order_qty || 1,
219
        orderer_id => $employee->id,
220
      }} @{$vendor_items};
221
  }
222 194

  
223
  my $order = SL::DB::Order->new(
224
    vendor_id               => $vendor->id,
225
    employee_id             => $employee->id,
226
    intnotes                => $vendor->notes,
227
    salesman_id             => $employee->id,
228
    payment_id              => $vendor->payment_id,
229
    delivery_term_id        => $vendor->delivery_term_id,
230
    taxzone_id              => $vendor->taxzone_id,
231
    currency_id             => $vendor->currency_id,
232
    transdate               => DateTime->today_local
233
  );
234

  
235
  my @order_items;
236
  my $i = 0;
237
  foreach my $orderitem_map (@orderitem_maps) {
238
    $i++;
239
    my $part = $orderitem_map->{part};
240
    my $qty = $orderitem_map->{qty};
241
    my $orderer_id = $orderitem_map->{orderer_id};
242

  
243
    my $order_item = SL::DB::OrderItem->new(
244
      part                => $part,
245
      qty                 => $qty,
246
      unit                => $part->unit,
247
      description         => $part->description,
248
      price_factor_id     => $part->price_factor_id,
249
      price_factor        =>
250
        $part->price_factor_id ? $part->price_factor->factor
251
                               : '',
252
      orderer_id          => $orderer_id,
253
      position            => $i,
254
    );
255

  
256
    my $price_source  = SL::PriceSource->new(
257
      record_item => $order_item, record => $order);
258
    $order_item->sellprice(
259
      $price_source->best_price ? $price_source->best_price->price
260
                                : 0);
261
    $order_item->active_price_source(
262
      $price_source->best_price ? $price_source->best_price->source
263
                                : '');
264
    push @order_items, $order_item;
195
  my $vendor_id =  $::form->{vendor_ids}->[0];
196

  
197
  # check for same vendor
198
  my %basket_id_vendor_id_map =
199
    map {$::form->{basket_ids}->[$_] => $::form->{vendor_ids}->[$_]}
200
    (0..$#{$::form->{vendor_ids}});
201
  my @different_vendor_ids =
202
    grep { $basket_id_vendor_id_map{$_} ne $vendor_id }
203
    @{$basket_item_ids};
204
  if (scalar @different_vendor_ids) {
205
    $self->js->flash('error', t8('There are mulitple vendors selected'));
206
    return $self->js->render();
265 207
  }
266 208

  
267
  $order->assign_attributes(orderitems => \@order_items);
268

  
269
  $order->db->with_transaction( sub {
270
    $order->calculate_prices_and_taxes;
271
    $order->save;
272

  
273
    my $snumbers = "ordernumber_" . $order->ordnumber;
274
    SL::DB::History->new(
275
                      trans_id    => $order->id,
276
                      snumbers    => $snumbers,
277
                      employee_id => SL::DB::Manager::Employee->current->id,
278
                      addition    => 'SAVED',
279
                      what_done   => 'PurchaseBasket->Order',
280
                    )->save();
281
    foreach my $item(@{ $order->orderitems }){
282
      $item->parse_custom_variable_values->save;
283
      $item->{custom_variables} = \@{ $item->cvars_by_config };
284
      $item->save;
285
    }
286
    if ($basket_items_ids && scalar @{ $basket_items_ids}) {
287
      SL::DB::Manager::PurchaseBasketItem->delete_all(
288
        where => [ id => $basket_items_ids]);
289
    }
290
    return 1;
291
  }) || die "error: " . $order->db->error;
292

  
293 209
  $self->redirect_to(
294 210
    controller => 'Order',
295
    action     => 'edit',
211
    action     => 'add_from_purchase_basket',
296 212
    type       => 'purchase_order',
297
    vc         => 'vendor',
298
    id         => $order->id,
213
    basket_item_ids => $basket_item_ids,
214
    vendor_item_ids => $vendor_item_ids,
215
    vendor_id       => $vendor_id,
299 216
  );
300 217
}
301 218

  
......
417 334
      ],
418 335
      action => [
419 336
        t8('Action'),
420
        call    => [ 'kivi.DispositionManager.create_order' ],
337
        call    => [ 'kivi.DispositionManager.create_purchase_order' ],
421 338
        tooltip => t8('Create purchase order'),
422 339
      ],
423 340
      action => [
SL/Controller/Order.pm
67 67

  
68 68
__PACKAGE__->run_before('check_auth_for_edit',
69 69
                        except => [ qw(edit show_customer_vendor_details_dialog price_popup load_second_rows close_quotations) ]);
70
__PACKAGE__->run_before('get_basket_info_from_from');
70 71

  
71 72
#
72 73
# actions
......
126 127
  );
127 128
}
128 129

  
130
sub action_add_from_purchase_basket {
131
  my ($self) = @_;
132

  
133
  my $basket_item_ids = $::form->{basket_item_ids};
134
  my $vendor_item_ids = $::form->{vendor_item_ids};
135
  my $vendor_id       = $::form->{vendor_id};
136

  
137

  
138
  unless (scalar @{ $basket_item_ids} || scalar @{ $vendor_item_ids}) {
139
    $self->js->flash('error', t8('There are no items selected'));
140
    return $self->js->render();
141
  }
142

  
143
  my $order = SL::DB::Order->create_from_purchase_basket(
144
    $basket_item_ids, $vendor_item_ids, $vendor_id
145
  );
146

  
147
  $self->order($order);
148

  
149
  $self->action_add();
150
}
151

  
129 152
# edit an existing order
130 153
sub action_edit {
131 154
  my ($self) = @_;
......
1700 1723
  }
1701 1724
  $self->js->val('[name="converted_from_orderitems_ids[+]"]', '');
1702 1725
  $self->js->val('[name="converted_from_reclamation_items_ids[+]"]', '');
1726
  $self->js->val('[name="basket_item_ids[+]"]', '');
1703 1727
}
1704 1728

  
1705 1729
#
......
2062 2086
  return $item;
2063 2087
}
2064 2088

  
2089
sub get_basket_info_from_from {
2090
  my ($self) = @_;
2091

  
2092
  my $order = $self->order;
2093
  my $basket_item_ids = $::form->{basket_item_ids};
2094
  if (scalar @{ $basket_item_ids || [] }) {
2095
    for my $idx (0 .. $#{ $order->items_sorted }) {
2096
      my $order_item = $order->items_sorted->[$idx];
2097
      $order_item->{basket_item_id} = $basket_item_ids->[$idx];
2098
    }
2099
  }
2100
}
2101

  
2065 2102
sub setup_order_from_cv {
2066 2103
  my ($order) = @_;
2067 2104

  
SL/DB/Order.pm
9 9
use List::MoreUtils qw(any);
10 10

  
11 11
use SL::DBUtils ();
12
use SL::DB::PurchaseBasketItem;
12 13
use SL::DB::MetaSetup::Order;
13 14
use SL::DB::Manager::Order;
14 15
use SL::DB::Helper::Attr;
......
77 78
__PACKAGE__->before_save('_before_save_create_new_project');
78 79
__PACKAGE__->before_save('_before_save_remove_empty_custom_shipto');
79 80
__PACKAGE__->before_save('_before_save_set_custom_shipto_module');
81
__PACKAGE__->before_save('_before_save_delete_from_purchase_basket');
80 82

  
81 83
# hooks
82 84

  
......
134 136
  return 1;
135 137
}
136 138

  
139
sub _before_save_delete_from_purchase_basket {
140
  my ($self) = @_;
141

  
142
  my @basket_item_ids = grep { $_ ne ''} map { $_->{basket_item_id} } $self->orderitems;
143

  
144
  if (scalar @basket_item_ids) {
145
    SL::DB::Manager::PurchaseBasketItem->delete_all(
146
      where => [ id => \@basket_item_ids]
147
    );
148
  }
149

  
150
  return 1;
151
}
152

  
137 153
# methods
138 154

  
139 155
sub items { goto &orderitems; }
......
336 352
  return $cloned;
337 353
}
338 354

  
355
sub create_from_purchase_basket {
356
  my ($class, $basket_item_ids, $vendor_item_ids, $vendor_id) = @_;
357

  
358
  my ($vendor, $employee);
359
  $vendor   = SL::DB::Manager::Vendor->find_by(id => $vendor_id);
360
  $employee = SL::DB::Manager::Employee->current;
361

  
362
  my @orderitem_maps = (); # part, qty, orderer_id
363
  if ($basket_item_ids && scalar @{ $basket_item_ids}) {
364
    my $basket_items = SL::DB::Manager::PurchaseBasketItem->get_all(
365
      query => [ id => $basket_item_ids ],
366
      with_objects => ['part'],
367
    );
368
    push @orderitem_maps, map {{
369
        basket_item_id => $_->id,
370
        part       => $_->part,
371
        qty        => $_->qty,
372
        orderer_id => $_->orderer_id,
373
      }} @{$basket_items};
374
  }
375
  if ($vendor_item_ids && scalar @{ $vendor_item_ids}) {
376
    my $vendor_items = SL::DB::Manager::Part->get_all(
377
      query => [ id => $vendor_item_ids ] );
378
    push @orderitem_maps, map {{
379
        basket_item_id => undef,
380
        part       => $_,
381
        qty        => $_->order_qty || 1,
382
        orderer_id => $employee->id,
383
      }} @{$vendor_items};
384
  }
385

  
386
  my $order = $class->new(
387
    vendor_id               => $vendor->id,
388
    employee_id             => $employee->id,
389
    intnotes                => $vendor->notes,
390
    salesman_id             => $employee->id,
391
    payment_id              => $vendor->payment_id,
392
    delivery_term_id        => $vendor->delivery_term_id,
393
    taxzone_id              => $vendor->taxzone_id,
394
    currency_id             => $vendor->currency_id,
395
    transdate               => DateTime->today_local
396
  );
397

  
398
  my @order_items;
399
  my $i = 0;
400
  foreach my $orderitem_map (@orderitem_maps) {
401
    $i++;
402
    my $part = $orderitem_map->{part};
403
    my $qty = $orderitem_map->{qty};
404
    my $orderer_id = $orderitem_map->{orderer_id};
405

  
406
    my $order_item = SL::DB::OrderItem->new(
407
      part                => $part,
408
      qty                 => $qty,
409
      unit                => $part->unit,
410
      description         => $part->description,
411
      price_factor_id     => $part->price_factor_id,
412
      price_factor        =>
413
        $part->price_factor_id ? $part->price_factor->factor
414
                               : '',
415
      orderer_id          => $orderer_id,
416
      position            => $i,
417
    );
418
    $order_item->{basket_item_id} = $orderitem_map->{basket_item_id};
419

  
420
    my $price_source  = SL::PriceSource->new(
421
      record_item => $order_item, record => $order);
422
    $order_item->sellprice(
423
      $price_source->best_price ? $price_source->best_price->price
424
                                : 0);
425
    $order_item->active_price_source(
426
      $price_source->best_price ? $price_source->best_price->source
427
                                : '');
428
    push @order_items, $order_item;
429
  }
430

  
431
  $order->assign_attributes(orderitems => \@order_items);
432

  
433
  $order->calculate_prices_and_taxes;
434

  
435
  foreach my $item(@{ $order->orderitems }){
436
    $item->parse_custom_variable_values;
437
    $item->{custom_variables} = \@{ $item->cvars_by_config };
438
  }
439

  
440
  return $order;
441
}
442

  
339 443
sub new_from {
340 444
  my ($class, $source, %params) = @_;
341 445

  
js/kivi.DispositionManager.js
19 19
    $('#vendor_parts').load(url);
20 20
  }
21 21

  
22
  ns.create_order = function() {
22
  ns.create_purchase_order = function() {
23 23
    var data = $('#purchasebasket').serializeArray();
24 24
    data.push({ name: 'action', value: 'DispositionManager/transfer_to_purchase_order' });
25 25
    $.post("controller.pl", data, kivi.eval_json_result);
templates/design40_webpages/disposition_manager/show_purchase_basket.html
45 45
    [% FOREACH basket_item = BASKET_ITEMS %]
46 46

  
47 47
      <tr>
48
        <td>[% L.checkbox_tag('ids[+]', "data-checkall"=1, checked = '1',  value=basket_item.id) %] [% HTML.escape(basket_item.part.partsgroup.partsgroup) %]
48
        <td>
49
          [% L.checkbox_tag('ids[+]', "data-checkall"=1, checked = '1',  value=basket_item.id) %]
50
          [% L.hidden_tag('basket_ids[+]', basket_item.id) %]
49 51
        </td>
50 52
        <td> [% basket_item.part.presenter.part(target = '_blank') %]
51 53
          [% P.button_tag(
templates/design40_webpages/order/tabs/_row.html
19 19
      [% L.hidden_tag("converted_from_reclamation_items_ids[+]", ITEM.converted_from_reclamation_items_id) %]
20 20
      [% L.hidden_tag("order.orderitems[+].id", ITEM.id, id='item_' _ ID) %]
21 21
      [% L.hidden_tag("order.orderitems[].parts_id", ITEM.parts_id) %]
22
      [% L.hidden_tag("basket_item_ids[+]", ITEM.basket_item_id) %]
22 23
    </td>
23 24
    <td class="center"><div name="position">[% HTML.escape(ITEM.position) %]</div></td>
24 25
    <td><img src="image/updown.png" alt="[% LxERP.t8('reorder item') %]" class="dragdrop"></td>
templates/webpages/disposition_manager/show_purchase_basket.html
33 33
    [% FOREACH basket_item = BASKET_ITEMS %]
34 34

  
35 35
      <tr class="listrow">
36
        <td>[% L.checkbox_tag('ids[+]', "data-checkall"=1, checked = '1',  value=basket_item.id) %] [% HTML.escape(basket_item.part.partsgroup.partsgroup) %]
36
        <td>
37
          [% L.checkbox_tag('ids[+]', "data-checkall"=1, checked = '1',  value=basket_item.id) %]
38
          [% L.hidden_tag('basket_ids[+]', basket_item.id) %]
37 39
        </td>
38 40
        <td> [% basket_item.part.presenter.part(target = '_blank') %]
39 41
          [% P.button_tag(
templates/webpages/order/tabs/_row.html
19 19
      [% L.hidden_tag("converted_from_reclamation_items_ids[+]", ITEM.converted_from_reclamation_items_id) %]
20 20
      [% L.hidden_tag("order.orderitems[+].id", ITEM.id, id='item_' _ ID) %]
21 21
      [% L.hidden_tag("order.orderitems[].parts_id", ITEM.parts_id) %]
22
      [% L.hidden_tag("purchase_basket_item_ids[+]", ITEM.purchase_basket_item_ids) %]
22 23
    </td>
23 24
    <td>
24 25
      <div name="position" class="numeric">

Auch abrufbar als: Unified diff