Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 58b77960

Von Tamino Steinert vor mehr als 1 Jahr hinzugefügt

  • ID 58b77960b4fe85dcf056e83633abb5f6329775ae
  • Vorgänger 9b6e957b
  • Nachfolger aba3c525

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
......
128 129
  );
129 130
}
130 131

  
132
sub action_add_from_purchase_basket {
133
  my ($self) = @_;
134

  
135
  my $basket_item_ids = $::form->{basket_item_ids};
136
  my $vendor_item_ids = $::form->{vendor_item_ids};
137
  my $vendor_id       = $::form->{vendor_id};
138

  
139

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

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

  
149
  $self->order($order);
150

  
151
  $self->action_add();
152
}
153

  
131 154
# edit an existing order
132 155
sub action_edit {
133 156
  my ($self) = @_;
......
1708 1731
  }
1709 1732
  $self->js->val('[name="converted_from_orderitems_ids[+]"]', '');
1710 1733
  $self->js->val('[name="converted_from_reclamation_items_ids[+]"]', '');
1734
  $self->js->val('[name="basket_item_ids[+]"]', '');
1711 1735
}
1712 1736

  
1713 1737
#
......
2075 2099
  return $item;
2076 2100
}
2077 2101

  
2102
sub get_basket_info_from_from {
2103
  my ($self) = @_;
2104

  
2105
  my $order = $self->order;
2106
  my $basket_item_ids = $::form->{basket_item_ids};
2107
  if (scalar @{ $basket_item_ids || [] }) {
2108
    for my $idx (0 .. $#{ $order->items_sorted }) {
2109
      my $order_item = $order->items_sorted->[$idx];
2110
      $order_item->{basket_item_id} = $basket_item_ids->[$idx];
2111
    }
2112
  }
2113
}
2114

  
2078 2115
sub setup_order_from_cv {
2079 2116
  my ($order) = @_;
2080 2117

  
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; }
......
338 354
  return $cloned;
339 355
}
340 356

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

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

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

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

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

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

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

  
433
  $order->assign_attributes(orderitems => \@order_items);
434

  
435
  $order->calculate_prices_and_taxes;
436

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

  
442
  return $order;
443
}
444

  
341 445
sub new_from {
342 446
  my ($class, $source, %params) = @_;
343 447

  
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