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 => [

Auch abrufbar als: Unified diff