1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger, Accounting
9 # Copyright (c) 1998-2003
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
29 #======================================================================
32 #======================================================================
35 use List::MoreUtils qw(uniq);
36 use List::Util qw(max sum);
37 use POSIX qw(strftime);
39 use SL::DB::DeliveryOrder;
43 use SL::MoreCommon qw(ary_diff restore_form save_form);
44 use SL::ReportGenerator;
47 use Sort::Naturally ();
48 require "bin/mozilla/common.pl";
49 require "bin/mozilla/io.pl";
50 require "bin/mozilla/reportgenerator.pl";
59 $main::auth->assert($main::form->{type} . '_edit');
63 $main::lxdebug->enter_sub();
69 my $form = $main::form;
70 my $locale = $main::locale;
72 if ($form->{type} eq 'purchase_delivery_order') {
73 $form->{vc} = 'vendor';
74 $form->{title} = $action eq "edit" ? $locale->text('Edit Purchase Delivery Order') : $locale->text('Add Purchase Delivery Order');
76 $form->{vc} = 'customer';
77 $form->{title} = $action eq "edit" ? $locale->text('Edit Sales Delivery Order') : $locale->text('Add Sales Delivery Order');
80 $form->{heading} = $locale->text('Delivery Order');
82 $main::lxdebug->leave_sub();
86 $main::lxdebug->enter_sub();
90 if (($::form->{type} =~ /purchase/) && !$::instance_conf->get_allow_new_purchase_invoice) {
91 $::form->show_generic_error($::locale->text("You do not have the permissions to access this function."));
94 my $form = $main::form;
98 $form->{show_details} = $::myconfig{show_form_details};
99 $form->{callback} = build_std_url('action=add', 'type', 'vc') unless ($form->{callback});
105 $main::lxdebug->leave_sub();
109 $main::lxdebug->enter_sub();
113 my $form = $main::form;
115 $form->{show_details} = $::myconfig{show_form_details};
117 # show history button
118 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
119 #/show hhistory button
121 $form->{simple_save} = 0;
123 set_headings("edit");
125 # editing without stuff to edit? try adding it first
126 if ($form->{rowcount} && !$form->{print_and_save}) {
127 # map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
131 undef $form->{rowcount};
133 $main::lxdebug->leave_sub();
136 } elsif (!$form->{id}) {
138 $main::lxdebug->leave_sub();
142 my ($language_id, $printer_id);
143 if ($form->{print_and_save}) {
144 $form->{action} = "dispatcher";
145 $form->{action_print} = "1";
146 $form->{resubmit} = 1;
147 $language_id = $form->{language_id};
148 $printer_id = $form->{printer_id};
151 set_headings("edit");
156 if ($form->{print_and_save}) {
157 $form->{language_id} = $language_id;
158 $form->{printer_id} = $printer_id;
163 $main::lxdebug->leave_sub();
167 $main::lxdebug->enter_sub();
171 my $form = $main::form;
172 my %myconfig = %main::myconfig;
174 # retrieve order/quotation
175 my $editing = $form->{id};
177 DO->retrieve('vc' => $form->{vc},
178 'ids' => $form->{id});
180 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes delivery_term_id currency));
182 # get customer / vendor
183 if ($form->{vc} eq 'vendor') {
184 IR->get_vendor(\%myconfig, \%$form);
185 $form->{discount} = $form->{vendor_discount};
187 IS->get_customer(\%myconfig, \%$form);
188 $form->{discount} = $form->{customer_discount};
191 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id delivery_term_id));
192 $form->restore_vars(qw(currency)) if ($form->{id} || $form->{convert_from_oe_ids});
193 $form->restore_vars(qw(taxincluded)) if $form->{id};
194 $form->restore_vars(qw(salesman_id)) if $editing;
196 $main::lxdebug->leave_sub();
200 $main::lxdebug->enter_sub();
204 my $form = $main::form;
205 my %myconfig = %main::myconfig;
207 $form->{formname} = $form->{type} unless $form->{formname};
210 foreach my $ref (@{ $form->{form_details} }) {
211 $form->{rowcount} = ++$i;
213 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
215 for my $i (1 .. $form->{rowcount}) {
217 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
219 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
221 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
223 my $decimalplaces = ($dec > 2) ? $dec : 2;
225 # copy reqdate from deliverydate for invoice -> order conversion
226 $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
228 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
229 $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
231 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
232 $dec_qty = length $dec_qty;
233 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
236 $main::lxdebug->leave_sub();
239 sub setup_do_action_bar {
240 my @transfer_qty = qw(kivi.SalesPurchase.delivery_order_check_transfer_qty);
241 my @req_trans_desc = qw(kivi.SalesPurchase.check_transaction_description) x!!$::instance_conf->get_require_transaction_description_ps;
242 my $is_customer = $::form->{vc} eq 'customer';
244 my $undo_date = DateTime->today->subtract(days => $::instance_conf->get_undo_transfer_interval);
245 my $insertdate = DateTime->from_kivitendo($::form->{insertdate});
246 my $undo_transfer = 0;
247 if (ref $undo_date eq 'DateTime' && ref $insertdate eq 'DateTime') {
248 # DateTime->compare it returns 1 if $dt1 > $dt2
249 $undo_transfer = DateTime->compare($insertdate, $undo_date) == 1 ? 1 : 0;
251 for my $bar ($::request->layout->get('actionbar')) {
255 submit => [ '#form', { action => "update" } ],
256 id => 'update_button',
257 accesskey => 'enter',
263 submit => [ '#form', { action => "save" } ],
264 checks => [ 'kivi.validate_form' ],
265 disabled => $::form->{delivered} ? t8('This record has already been delivered.') : undef,
269 submit => [ '#form', { action => "save_as_new" } ],
270 checks => [ 'kivi.validate_form' ],
271 disabled => !$::form->{id},
274 t8('Mark as closed'),
275 submit => [ '#form', { action => "mark_closed" } ],
276 checks => [ 'kivi.validate_form' ],
277 confirm => t8('This will remove the delivery order from showing as open even if contents are not delivered. Proceed?'),
278 disabled => !$::form->{id} ? t8('This record has not been saved yet.')
279 : $::form->{closed} ? t8('This record has already been closed.')
282 ], # end of combobox "Save"
286 submit => [ '#form', { action => "delete" } ],
287 confirm => t8('Do you really want to delete this object?'),
288 disabled => !$::form->{id} ? t8('This record has not been saved yet.')
289 : $::form->{delivered} ? t8('This record has already been delivered.')
290 : ($::form->{vc} eq 'customer' && !$::instance_conf->get_sales_delivery_order_show_delete) ? t8('Deleting this type of record has been disabled in the configuration.')
291 : ($::form->{vc} eq 'vendor' && !$::instance_conf->get_purchase_delivery_order_show_delete) ? t8('Deleting this type of record has been disabled in the configuration.')
298 submit => [ '#form', { action => "transfer_out" } ],
299 checks => [ 'kivi.validate_form', @transfer_qty ],
300 disabled => $::form->{delivered} ? t8('This record has already been delivered.') : undef,
301 only_if => $is_customer,
304 t8('Transfer out via default'),
305 submit => [ '#form', { action => "transfer_out_default" } ],
306 checks => [ 'kivi.validate_form' ],
307 disabled => $::form->{delivered} ? t8('This record has already been delivered.') : undef,
308 only_if => $is_customer && $::instance_conf->get_transfer_default,
312 submit => [ '#form', { action => "transfer_in" } ],
313 checks => [ 'kivi.validate_form', @transfer_qty ],
314 disabled => $::form->{delivered} ? t8('This record has already been delivered.') : undef,
315 only_if => !$is_customer,
318 t8('Transfer in via default'),
319 submit => [ '#form', { action => "transfer_in_default" } ],
320 checks => [ 'kivi.validate_form' ],
321 disabled => $::form->{delivered} ? t8('This record has already been delivered.') : undef,
322 only_if => !$is_customer && $::instance_conf->get_transfer_default,
326 submit => [ '#form', { action => "delete_transfers" } ],
327 checks => [ 'kivi.validate_form' ],
328 only_if => $::form->{delivered},
329 disabled => !$undo_transfer ? t8('Transfer date exceeds the maximum allowed interval.') : undef,
331 ], # end of combobox "Transfer out"
338 submit => [ '#form', { action => "invoice" } ],
339 disabled => !$::form->{id} ? t8('This record has not been saved yet.') : undef,
340 confirm => $::form->{delivered} ? undef
341 : ($::form->{vc} eq 'customer' && $::instance_conf->get_sales_delivery_order_check_stocked) ? t8('This record has not been stocked out. Proceed?')
342 : ($::form->{vc} eq 'vendor' && $::instance_conf->get_purchase_delivery_order_check_stocked) ? t8('This record has not been stocked in. Proceed?')
347 action => [ t8('Export') ],
350 call => [ 'kivi.SalesPurchase.show_print_dialog' ],
351 checks => [ 'kivi.validate_form' ],
355 call => [ 'kivi.SalesPurchase.show_email_dialog' ],
356 checks => [ 'kivi.validate_form' ],
357 disabled => !$::form->{id} ? t8('This record has not been saved yet.') : undef,
359 ], # end of combobox "Export"
362 action => [ t8('more') ],
365 call => [ 'set_history_window', $::form->{id} * 1, 'id' ],
366 disabled => !$::form->{id} ? t8('This record has not been saved yet.') : undef,
370 call => [ 'follow_up_window' ],
371 disabled => !$::form->{id} ? t8('This record has not been saved yet.') : undef,
373 ], # end if combobox "more"
376 $::request->layout->add_javascripts('kivi.Validator.js');
379 sub setup_do_search_action_bar {
382 for my $bar ($::request->layout->get('actionbar')) {
386 submit => [ '#form' ],
387 accesskey => 'enter',
388 checks => [ 'kivi.validate_form' ],
392 $::request->layout->add_javascripts('kivi.Validator.js');
395 sub setup_do_orders_action_bar {
398 for my $bar ($::request->layout->get('actionbar')) {
402 submit => [ '#form', { action => 'invoice_multi' } ],
403 checks => [ [ 'kivi.check_if_entries_selected', '#form tbody input[type=checkbox]' ] ],
404 accesskey => 'enter',
408 call => [ 'kivi.SalesPurchase.show_print_dialog', 'js:kivi.MassDeliveryOrderPrint.submitMultiOrders' ],
409 checks => [ [ 'kivi.check_if_entries_selected', '#form tbody input[type=checkbox]' ] ],
416 $main::lxdebug->enter_sub();
420 my $form = $main::form;
421 my %myconfig = %main::myconfig;
423 my $class = "SL::DB::" . ($form->{vc} eq 'customer' ? 'Customer' : 'Vendor');
424 $form->{VC_OBJ} = $class->load_cached($form->{ $form->{vc} . '_id' });
426 $form->{CONTACT_OBJ} = $form->{cp_id} ? SL::DB::Contact->load_cached($form->{cp_id}) : undef;
427 my $current_employee = SL::DB::Manager::Employee->current;
428 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
429 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
430 $form->{employee_id} ||= $current_employee->id;
431 $form->{salesman_id} ||= $current_employee->id;
433 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
434 $form->get_lists("price_factors" => "ALL_PRICE_FACTORS",
435 "business_types" => "ALL_BUSINESS_TYPES",
437 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
438 $form->{ALL_LANGUAGES} = SL::DB::Manager::Language->get_all_sorted;
441 my @old_project_ids = uniq grep { $_ } map { $_ * 1 } ($form->{"globalproject_id"}, map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
442 my @old_ids_cond = @old_project_ids ? (id => \@old_project_ids) : ();
444 if (($vc eq 'customers') && $::instance_conf->get_customer_projects_only_in_sales) {
447 customer_id => $::form->{customer_id},
448 billable_customer_id => $::form->{customer_id},
453 and => [ active => 1, @customer_cond ],
457 $::form->{ALL_PROJECTS} = SL::DB::Manager::Project->get_all_sorted(query => \@conditions);
458 $::form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{employee_id}, deleted => 0 ] ]);
459 $::form->{ALL_SALESMEN} = SL::DB::Manager::Employee->get_all_sorted(query => [ or => [ id => $::form->{salesman_id}, deleted => 0 ] ]);
460 $::form->{ALL_SHIPTO} = SL::DB::Manager::Shipto->get_all_sorted(query => [
461 or => [ trans_id => $::form->{"$::form->{vc}_id"} * 1, and => [ shipto_id => $::form->{shipto_id} * 1, trans_id => undef ] ]
463 $::form->{ALL_CONTACTS} = SL::DB::Manager::Contact->get_all_sorted(query => [
465 cp_cv_id => $::form->{"$::form->{vc}_id"} * 1,
468 cp_id => $::form->{cp_id} * 1
473 my $dispatch_to_popup = '';
474 if ($form->{resubmit} && ($form->{format} eq "html")) {
475 $dispatch_to_popup = "window.open('about:blank','Beleg'); document.do.target = 'Beleg';";
476 $dispatch_to_popup .= "document.do.submit();";
477 } elsif ($form->{resubmit} && $form->{action_print}) {
478 # emulate click for resubmitting actions
479 $dispatch_to_popup = "kivi.SalesPurchase.show_print_dialog(); kivi.SalesPurchase.print_record();";
481 $::request->{layout}->add_javascripts_inline("\$(function(){$dispatch_to_popup});");
484 $form->{follow_up_trans_info} = $form->{donumber} .'('. $form->{VC_OBJ}->name .')' if $form->{VC_OBJ};
486 $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.File kivi.MassDeliveryOrderPrint kivi.SalesPurchase kivi.Part kivi.CustomerVendor kivi.Validator ckeditor/ckeditor ckeditor/adapters/jquery kivi.io));
488 setup_do_action_bar();
491 # Fix für Bug 1082 Erwartet wird: 'abteilungsNAME--abteilungsID'
492 # und Erweiterung für Bug 1760:
493 # Das war leider nur ein Teil-Fix, da das Verhalten den 'Erneuern'-Knopf
494 # nicht überlebt. Konsequent jetzt auf L umgestellt
495 # $ perldoc SL::Template::Plugin::L
496 # Daher entsprechend nur die Anpassung in form_header
497 # und in DO.pm gemacht. 4 Testfälle:
498 # department_id speichern | i.O.
499 # department_id lesen | i.O.
500 # department leer überlebt erneuern | i.O.
501 # department nicht leer überlebt erneuern | i.O.
502 # $main::lxdebug->message(0, 'ABTEILUNGS ID in form?' . $form->{department_id});
503 print $form->parse_html_template('do/form_header');
505 $main::lxdebug->leave_sub();
509 $main::lxdebug->enter_sub();
513 my $form = $main::form;
515 $form->{PRINT_OPTIONS} = setup_sales_purchase_print_options();
516 $form->{ALL_DELIVERY_TERMS} = SL::DB::Manager::DeliveryTerm->get_all_sorted();
518 my $shipto_cvars = SL::DB::Shipto->new->cvars_by_config;
519 foreach my $var (@{ $shipto_cvars }) {
520 my $name = "shiptocvar_" . $var->config->name;
521 $var->value($form->{$name}) if exists $form->{$name};
524 print $form->parse_html_template('do/form_footer',
525 {transfer_default => ($::instance_conf->get_transfer_default),
526 shipto_cvars => $shipto_cvars});
528 $main::lxdebug->leave_sub();
531 sub update_delivery_order {
532 $main::lxdebug->enter_sub();
536 my $form = $main::form;
537 my %myconfig = %main::myconfig;
539 set_headings($form->{"id"} ? "edit" : "add");
541 $form->{insertdate} = SL::DB::DeliveryOrder->new(id => $form->{id})->load->itime_as_date if $form->{id};
546 $payment_id = $form->{payment_id} if $form->{payment_id};
548 my $vc = $form->{vc};
549 if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
550 $::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};
552 IS->get_customer(\%myconfig, $form) if $vc eq 'customer';
553 IR->get_vendor(\%myconfig, $form) if $vc eq 'vendor';
556 $form->{discount} = $form->{"$form->{vc}_discount"} if defined $form->{"$form->{vc}_discount"};
557 # Problem: Wenn man ohne Erneuern einen Kunden/Lieferanten
558 # wechselt, wird der entsprechende Kunden/ Lieferantenrabatt
559 # nicht übernommen. Grundproblem: In Commit 82574e78
560 # hab ich aus discount customer_discount und vendor_discount
561 # gemacht und entsprechend an den Oberflächen richtig hin-
562 # geschoben. Die damals bessere Lösung wäre gewesen:
563 # In den Templates nur die hidden für form-discount wieder ein-
564 # setzen dann wäre die Verrenkung jetzt nicht notwendig.
565 # TODO: Ggf. Bugfix 1284, 1575 und 817 wieder zusammenführen
566 # Testfälle: Kunden mit Rabatt 0 -> Rabatt 20 i.O.
567 # Kunde mit Rabatt 20 -> Rabatt 0 i.O.
568 # Kunde mit Rabatt 20 -> Rabatt 5,5 i.O.
569 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
571 my $i = $form->{rowcount};
573 if ( ($form->{"partnumber_$i"} eq "")
574 && ($form->{"description_$i"} eq "")
575 && ($form->{"partsgroup_$i"} eq "")) {
582 if ($form->{type} eq 'purchase_delivery_order') {
583 IR->retrieve_item(\%myconfig, $form);
586 IS->retrieve_item(\%myconfig, $form);
590 my $rows = scalar @{ $form->{item_list} };
593 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
594 if( !$form->{"qty_$i"} ) {
595 $form->{"qty_$i"} = 1;
600 select_item(mode => $mode, pre_entered_qty => $form->{"qty_$i"});
601 $::dispatcher->end_request;
605 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
607 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
609 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
612 $form->{"sellprice_$i"} = $sellprice;
614 my $record = _make_record();
615 my $price_source = SL::PriceSource->new(record_item => $record->items->[$i-1], record => $record);
616 my $best_price = $price_source->best_price;
617 my $best_discount = $price_source->best_discount;
620 $::form->{"sellprice_$i"} = $best_price->price;
621 $::form->{"active_price_source_$i"} = $best_price->source;
623 if ($best_discount) {
624 $::form->{"discount_$i"} = $best_discount->discount;
625 $::form->{"active_discount_source_$i"} = $best_discount->source;
629 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
630 $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"});
631 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
632 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100.0);
639 # ok, so this is a new part
640 # ask if it is a part or service item
642 if ( $form->{"partsgroup_$i"}
643 && ($form->{"partsnumber_$i"} eq "")
644 && ($form->{"description_$i"} eq "")) {
646 $form->{"discount_$i"} = "";
647 $form->{"not_discountable_$i"} = "";
651 $form->{"id_$i"} = 0;
657 $main::lxdebug->leave_sub();
661 $main::lxdebug->enter_sub();
665 my $form = $main::form;
666 my %myconfig = %main::myconfig;
667 my $locale = $main::locale;
669 $form->{vc} = $form->{type} eq 'purchase_delivery_order' ? 'vendor' : 'customer';
671 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
673 "business_types" => "ALL_BUSINESS_TYPES");
674 $form->{ALL_EMPLOYEES} = SL::DB::Manager::Employee->get_all_sorted(query => [ deleted => 0 ]);
675 $form->{ALL_DEPARTMENTS} = SL::DB::Manager::Department->get_all_sorted;
676 $form->{title} = $locale->text('Delivery Orders');
678 setup_do_search_action_bar();
682 print $form->parse_html_template('do/search');
684 $main::lxdebug->leave_sub();
688 $main::lxdebug->enter_sub();
692 my $form = $main::form;
693 my %myconfig = %main::myconfig;
694 my $locale = $main::locale;
695 my $cgi = $::request->{cgi};
697 $::request->{layout}->use_javascript(map { "${_}.js" } qw(kivi.MassDeliveryOrderPrint kivi.SalesPurchase));
698 ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) = split(/--/, $form->{ $form->{vc} });
700 report_generator_set_default_sort('transdate', 1);
704 $form->{rowcount} = scalar @{ $form->{DO} };
707 ids transdate reqdate
709 ordnumber customernumber cusordnumber
710 name employee salesman
711 shipvia globalprojectnumber
712 transaction_description department
717 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
718 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
720 $form->{title} = $locale->text('Delivery Orders');
722 my $attachment_basename = $form->{vc} eq 'vendor' ? $locale->text('purchase_delivery_order_list') : $locale->text('sales_delivery_order_list');
724 my $report = SL::ReportGenerator->new(\%myconfig, $form);
726 my @hidden_variables = map { "l_${_}" } @columns;
727 push @hidden_variables, $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered donumber ordnumber serialnumber cusordnumber
728 transaction_description transdatefrom transdateto reqdatefrom reqdateto
729 type vc employee_id salesman_id project_id parts_partnumber parts_description
730 insertdatefrom insertdateto business_id all department_id);
732 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
735 'ids' => { raw_header_data => SL::Presenter::Tag::checkbox_tag("", id => "multi_all", checkall => "[data-checkall=1]"), align => 'center' },
736 'transdate' => { 'text' => $locale->text('Delivery Order Date'), },
737 'reqdate' => { 'text' => $locale->text('Reqdate'), },
738 'id' => { 'text' => $locale->text('ID'), },
739 'donumber' => { 'text' => $locale->text('Delivery Order'), },
740 'ordnumber' => { 'text' => $locale->text('Order'), },
741 'customernumber' => { 'text' => $locale->text('Customer Number'), },
742 'cusordnumber' => { 'text' => $locale->text('Customer Order Number'), },
743 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
744 'employee' => { 'text' => $locale->text('Employee'), },
745 'salesman' => { 'text' => $locale->text('Salesman'), },
746 'shipvia' => { 'text' => $locale->text('Ship via'), },
747 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
748 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
749 'open' => { 'text' => $locale->text('Open'), },
750 'delivered' => { 'text' => $locale->text('Delivered'), },
751 'department' => { 'text' => $locale->text('Department'), },
752 'insertdate' => { 'text' => $locale->text('Insert Date'), },
755 foreach my $name (qw(id transdate reqdate donumber ordnumber name employee salesman shipvia transaction_description department insertdate)) {
756 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
757 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
760 $form->{"l_type"} = "Y";
761 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
763 $column_defs{ids}->{visible} = 'HTML';
765 $report->set_columns(%column_defs);
766 $report->set_column_order(@columns);
768 $report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
770 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
773 if ($form->{customer}) {
774 push @options, $locale->text('Customer') . " : $form->{customer}";
776 if ($form->{vendor}) {
777 push @options, $locale->text('Vendor') . " : $form->{vendor}";
779 if ($form->{cp_name}) {
780 push @options, $locale->text('Contact Person') . " : $form->{cp_name}";
782 if ($form->{department_id}) {
783 push @options, $locale->text('Department') . " : " . SL::DB::Department->new(id => $form->{department_id})->load->description;
785 if ($form->{donumber}) {
786 push @options, $locale->text('Delivery Order Number') . " : $form->{donumber}";
788 if ($form->{ordnumber}) {
789 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
791 push @options, $locale->text('Serial Number') . " : $form->{serialnumber}" if $form->{serialnumber};
792 if ($form->{business_id}) {
793 my $vc_type_label = $form->{vc} eq 'customer' ? $locale->text('Customer type') : $locale->text('Vendor type');
794 push @options, $vc_type_label . " : " . SL::DB::Business->new(id => $form->{business_id})->load->description;
796 if ($form->{transaction_description}) {
797 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
799 if ($form->{parts_description}) {
800 push @options, $locale->text('Part Description') . " : $form->{parts_description}";
802 if ($form->{parts_partnumber}) {
803 push @options, $locale->text('Part Number') . " : $form->{parts_partnumber}";
805 if ( $form->{transdatefrom} or $form->{transdateto} ) {
806 push @options, $locale->text('Delivery Order Date');
807 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if $form->{transdatefrom};
808 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if $form->{transdateto};
810 if ( $form->{reqdatefrom} or $form->{reqdateto} ) {
811 push @options, $locale->text('Reqdate');
812 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{reqdatefrom}, 1) if $form->{reqdatefrom};
813 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{reqdateto}, 1) if $form->{reqdateto};
815 if ( $form->{insertdatefrom} or $form->{insertdateto} ) {
816 push @options, $locale->text('Insert Date');
817 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{insertdatefrom}, 1) if $form->{insertdatefrom};
818 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{insertdateto}, 1) if $form->{insertdateto};
821 push @options, $locale->text('Open');
823 if ($form->{closed}) {
824 push @options, $locale->text('Closed');
826 if ($form->{delivered}) {
827 push @options, $locale->text('Delivered');
829 if ($form->{notdelivered}) {
830 push @options, $locale->text('Not delivered');
832 push @options, $locale->text('Quick Search') . " : $form->{all}" if $form->{all};
834 my $pr = SL::DB::Manager::Printer->find_by(
835 printer_description => $::locale->text("sales_delivery_order_printer"));
837 $form->{printer_id} = $pr->id;
840 my $print_options = SL::Helper::PrintOptions->get_print_options(
842 hide_language_id => 1,
848 $report->set_options('top_info_text' => join("\n", @options),
849 'raw_top_info_text' => $form->parse_html_template('do/orders_top'),
850 'raw_bottom_info_text' => $form->parse_html_template('do/orders_bottom', { print_options => $print_options }),
851 'output_format' => 'HTML',
852 'title' => $form->{title},
853 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
855 $report->set_options_from_form();
856 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
858 # add sort and escape callback, this one we use for the add sub
859 $form->{callback} = $href .= "&sort=$form->{sort}";
861 # escape callback for href
862 my $callback = $form->escape($href);
864 my $edit_url = build_std_url('action=edit', 'type', 'vc');
865 my $edit_order_url = ($::instance_conf->get_feature_experimental_order)
866 ? build_std_url('script=controller.pl', 'action=Order/edit', 'type=' . ($form->{type} eq 'sales_delivery_order' ? 'sales_order' : 'purchase_order'))
867 : build_std_url('script=oe.pl', 'action=edit', 'type=' . ($form->{type} eq 'sales_delivery_order' ? 'sales_order' : 'purchase_order'));
871 foreach my $dord (@{ $form->{DO} }) {
872 $dord->{open} = $dord->{closed} ? $locale->text('No') : $locale->text('Yes');
873 $dord->{delivered} = $dord->{delivered} ? $locale->text('Yes') : $locale->text('No');
875 my $row = { map { $_ => { 'data' => $dord->{$_} } } @columns };
877 my $ord_id = $dord->{id};
879 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $ord_id)
880 . $cgi->checkbox('-name' => "multi_id_${idx}",' id' => "multi_id_id_".$ord_id, '-value' => 1, 'data-checkall' => 1, '-label' => ''),
881 'valign' => 'center',
885 $row->{donumber}->{link} = $edit_url . "&id=" . E($dord->{id}) . "&callback=${callback}";
886 $row->{ordnumber}->{link} = $edit_order_url . "&id=" . E($dord->{oe_id}) . "&callback=${callback}" if $dord->{oe_id};
887 $report->add_data($row);
892 setup_do_orders_action_bar();
894 $report->generate_with_headers();
896 $main::lxdebug->leave_sub();
900 $main::lxdebug->enter_sub();
906 my $form = $main::form;
907 my %myconfig = %main::myconfig;
908 my $locale = $main::locale;
910 $form->mtime_ischanged('delivery_orders');
912 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
914 $form->isblank("transdate", $locale->text('Delivery Order Date missing!'));
916 $form->{donumber} =~ s/^\s*//g;
917 $form->{donumber} =~ s/\s*$//g;
919 my $msg = ucfirst $form->{vc};
920 $form->isblank($form->{vc} . "_id", $locale->text($msg . " missing!"));
922 # $locale->text('Customer missing!');
923 # $locale->text('Vendor missing!');
925 remove_emptied_rows();
928 # if the name changed get new values
929 my $vc = $form->{vc};
930 if (($form->{"previous_${vc}_id"} || $form->{"${vc}_id"}) != $form->{"${vc}_id"}) {
931 $::form->{salesman_id} = SL::DB::Manager::Employee->current->id if exists $::form->{salesman_id};
933 IS->get_customer(\%myconfig, $form) if $vc eq 'customer';
934 IR->get_vendor(\%myconfig, $form) if $vc eq 'vendor';
937 $::dispatcher->end_request;
940 $form->{id} = 0 if $form->{saveasnew};
944 if(!exists $form->{addition}) {
945 $form->{snumbers} = qq|donumber_| . $form->{donumber};
946 $form->{addition} = "SAVED";
949 # /saving the history
951 $form->{simple_save} = 1;
952 if (!$params{no_redirect} && !$form->{print_and_save}) {
953 delete @{$form}{ary_diff([keys %{ $form }], [qw(login id script type cursor_fokus)])};
955 $::dispatcher->end_request;
957 $main::lxdebug->leave_sub();
961 $main::lxdebug->enter_sub();
965 my $form = $main::form;
966 my %myconfig = %main::myconfig;
967 my $locale = $main::locale;
969 if ($ret = DO->delete()) {
971 if(!exists $form->{addition}) {
972 $form->{snumbers} = qq|donumber_| . $form->{donumber};
973 $form->{addition} = "DELETED";
976 # /saving the history
978 $form->info($locale->text('Delivery Order deleted!'));
979 $::dispatcher->end_request;
982 $form->error($locale->text('Cannot delete delivery order!') . $ret);
984 $main::lxdebug->leave_sub();
986 sub delete_transfers {
987 $main::lxdebug->enter_sub();
991 my $form = $main::form;
992 my %myconfig = %main::myconfig;
993 my $locale = $main::locale;
996 die "Invalid form type" unless $form->{type} =~ m/^(sales|purchase)_delivery_order$/;
998 if ($ret = DO->delete_transfers()) {
1000 if(!exists $form->{addition}) {
1001 $form->{snumbers} = qq|donumber_| . $form->{donumber};
1002 $form->{addition} = "UNDO TRANSFER";
1003 $form->save_history;
1005 # /saving the history
1007 flash_later('info', $locale->text("Transfer undone."));
1009 $form->{callback} = 'do.pl?action=edit&type=' . $form->{type} . '&id=' . $form->escape($form->{id});
1013 $form->error($locale->text('Cannot undo delivery order transfer!') . $ret);
1015 $main::lxdebug->leave_sub();
1019 $main::lxdebug->enter_sub();
1021 my $form = $main::form;
1022 my %myconfig = %main::myconfig;
1023 my $locale = $main::locale;
1026 $form->mtime_ischanged('delivery_orders');
1028 $main::auth->assert($form->{type} eq 'purchase_delivery_order' ? 'vendor_invoice_edit' : 'invoice_edit');
1030 $form->{convert_from_do_ids} = $form->{id};
1031 # if we have a reqdate (Liefertermin), this is definetely the preferred
1032 # deliverydate for invoices
1033 $form->{deliverydate} = $form->{reqdate} || $form->{transdate};
1034 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1035 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1036 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1038 $form->{rowcount}--;
1040 delete @{$form}{qw(id closed delivered)};
1042 my ($script, $buysell);
1043 if ($form->{type} eq 'purchase_delivery_order') {
1044 $form->{title} = $locale->text('Add Vendor Invoice');
1045 $form->{script} = 'ir.pl';
1050 $form->{title} = $locale->text('Add Sales Invoice');
1051 $form->{script} = 'is.pl';
1056 for my $i (1 .. $form->{rowcount}) {
1057 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"} } qw(ship qty sellprice lastcost basefactor discount);
1059 # adds a customer/vendor discount, unless we have a workflow case
1060 # CAVEAT: has to be done, after the above parse_amount
1061 unless ($form->{"ordnumber"}) {
1062 if ($form->{discount}) { # Falls wir einen Lieferanten-/Kundenrabatt haben
1063 # und rabattfähig sind, dann
1064 unless ($form->{"not_discountable_$i"}) {
1065 $form->{"discount_$i"} = $form->{discount}*100; # ... nehmen wir diesen Rabatt
1069 $form->{"donumber_$i"} = $form->{donumber};
1070 $form->{"converted_from_delivery_order_items_id_$i"} = delete $form->{"delivery_order_items_id_$i"};
1073 $form->{type} = "invoice";
1076 $main::locale = Locale->new("$myconfig{countrycode}", "$script");
1077 $locale = $main::locale;
1079 require "bin/mozilla/$form->{script}";
1081 my $currency = $form->{currency};
1084 if ($form->{ordnumber}) {
1085 require SL::DB::Order;
1086 my $vc_id = $form->{type} =~ /^sales/ ? 'customer_id' : 'vendor_id';
1087 if (my $order = SL::DB::Manager::Order->find_by(ordnumber => $form->{ordnumber}, $vc_id => $form->{"$vc_id"})) {
1089 $form->{orddate} = $order->transdate_as_date;
1090 $form->{$_} = $order->$_ for qw(payment_id salesman_id taxzone_id quonumber taxincluded);
1091 $form->{taxincluded_changed_by_user} = 1;
1095 $form->{currency} = $currency;
1096 $form->{exchangerate} = "";
1097 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1098 $form->{exchangerate} = $form->{forex} if ($form->{forex});
1103 for my $i (1 .. $form->{rowcount}) {
1104 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
1106 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1108 my $decimalplaces = ($dec > 2) ? $dec : 2;
1110 # copy delivery date from reqdate for order -> invoice conversion
1111 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1112 unless $form->{"deliverydate_$i"};
1115 $form->{"sellprice_$i"} =
1116 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1119 $form->{"lastcost_$i"} =
1120 $form->format_amount(\%myconfig, $form->{"lastcost_$i"},
1123 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1124 $dec_qty = length $dec_qty;
1126 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1132 $main::lxdebug->leave_sub();
1136 $main::lxdebug->enter_sub();
1138 my $form = $main::form;
1139 my %myconfig = %main::myconfig;
1140 my $locale = $main::locale;
1143 $main::auth->assert($form->{type} eq 'sales_delivery_order' ? 'invoice_edit' : 'vendor_invoice_edit');
1145 my @do_ids = map { $form->{"trans_id_$_"} } grep { $form->{"multi_id_$_"} } (1..$form->{rowcount});
1147 if (!scalar @do_ids) {
1148 $form->show_generic_error($locale->text('You have not selected any delivery order.'));
1151 map { delete $form->{$_} } grep { m/^(?:trans|multi)_id_\d+/ } keys %{ $form };
1153 if (!DO->retrieve('vc' => $form->{vc}, 'ids' => \@do_ids)) {
1154 $form->show_generic_error($form->{vc} eq 'customer' ?
1155 $locale->text('You cannot create an invoice for delivery orders for different customers.') :
1156 $locale->text('You cannot create an invoice for delivery orders from different vendors.'),
1157 'back_button' => 1);
1160 my $source_type = $form->{type};
1161 $form->{convert_from_do_ids} = join ' ', @do_ids;
1162 # bei der auswahl von mehreren Lieferscheinen fuer eine Rechnung, die einfach in donumber_array
1163 # zwischenspeichern (DO.pm) und als ' '-separierte Liste wieder zurueckschreiben
1164 # Hinweis: delete gibt den wert zurueck und loescht danach das element (nett und einfach)
1165 # $shell: perldoc perlunc; /delete EXPR
1166 $form->{donumber} = delete $form->{donumber_array};
1167 $form->{ordnumber} = delete $form->{ordnumber_array};
1168 $form->{cusordnumber} = delete $form->{cusordnumber_array};
1169 $form->{deliverydate} = $form->{transdate};
1170 $form->{transdate} = $form->current_date(\%myconfig);
1171 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1172 $form->{type} = "invoice";
1173 $form->{closed} = 0;
1174 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1176 my ($script, $buysell);
1177 if ($source_type eq 'purchase_delivery_order') {
1178 $form->{title} = $locale->text('Add Vendor Invoice');
1179 $form->{script} = 'ir.pl';
1184 $form->{title} = $locale->text('Add Sales Invoice');
1185 $form->{script} = 'is.pl';
1190 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1192 # get vendor or customer discount
1194 my $saved_form = save_form();
1195 if ($form->{vc} eq 'vendor') {
1196 IR->get_vendor(\%myconfig, \%$form);
1197 $vc_discount = $form->{vendor_discount};
1199 IS->get_customer(\%myconfig, \%$form);
1200 $vc_discount = $form->{customer_discount};
1202 # use payment terms from customer or vendor
1203 restore_form($saved_form,0,qw(payment_id));
1205 $form->{rowcount} = 0;
1206 foreach my $ref (@{ $form->{form_details} }) {
1207 $form->{rowcount}++;
1208 $ref->{reqdate} ||= $ref->{dord_transdate}; # copy transdates into each invoice row
1209 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{ $ref };
1210 map { $form->{"${_}_$form->{rowcount}"} = $form->format_amount(\%myconfig, $ref->{$_}) } qw(qty sellprice lastcost);
1211 $form->{"converted_from_delivery_order_items_id_$form->{rowcount}"} = delete $form->{"delivery_order_items_id_$form->{rowcount}"};
1213 if ($vc_discount){ # falls wir einen Lieferanten/Kundenrabatt haben
1214 # und keinen anderen discount wert an $i ...
1215 $form->{"discount_$form->{rowcount}"} ||= $vc_discount; # ... nehmen wir diesen Rabatt
1218 $form->{"discount_$form->{rowcount}"} = $form->{"discount_$form->{rowcount}"} * 100; #s.a. Bug 1151
1219 # Anm.: Eine Änderung des discounts in der SL/DO.pm->retrieve (select (doi.discount * 100) as discount) ergibt in psql einen
1220 # Wert von 10.0000001490116. Ferner ist der Rabatt in der Rechnung dann bei 1.0 (?). Deswegen lasse ich das hier. jb 10.10.09
1222 $form->{"discount_$form->{rowcount}"} = $form->format_amount(\%myconfig, $form->{"discount_$form->{rowcount}"});
1224 delete $form->{form_details};
1226 $locale = Locale->new("$myconfig{countrycode}", "$script");
1228 require "bin/mozilla/$form->{script}";
1235 $main::lxdebug->leave_sub();
1239 $main::lxdebug->enter_sub();
1243 my $form = $main::form;
1245 $form->{saveasnew} = 1;
1246 $form->{closed} = 0;
1247 $form->{delivered} = 0;
1248 map { delete $form->{$_} } qw(printed emailed queued);
1249 delete @{ $form }{ grep { m/^stock_(?:in|out)_\d+/ } keys %{ $form } };
1250 $form->{"converted_from_delivery_order_items_id_$_"} = delete $form->{"delivery_order_items_id_$_"} for 1 .. $form->{"rowcount"};
1251 # Let kivitendo assign a new order number if the user hasn't changed the
1252 # previous one. If it has been changed manually then use it as-is.
1253 $form->{donumber} =~ s/^\s*//g;
1254 $form->{donumber} =~ s/\s*$//g;
1255 if ($form->{saved_donumber} && ($form->{saved_donumber} eq $form->{donumber})) {
1256 delete($form->{donumber});
1261 $main::lxdebug->leave_sub();
1264 sub calculate_stock_in_out {
1265 $main::lxdebug->enter_sub();
1267 my $form = $main::form;
1271 if (!$form->{"id_${i}"}) {
1272 $main::lxdebug->leave_sub();
1276 my $all_units = AM->retrieve_all_units();
1278 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
1279 my $sinfo = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_${i}"});
1281 my $do_qty = AM->sum_with_unit($::form->{"qty_$i"}, $::form->{"unit_$i"});
1282 my $sum = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $sinfo });
1283 my $matches = $do_qty == $sum;
1285 my $content = $form->format_amount_units('amount' => $sum * 1,
1286 'part_unit' => $form->{"partunit_$i"},
1287 'amount_unit' => $all_units->{$form->{"partunit_$i"}}->{base_unit},
1288 'conv_units' => 'convertible_not_smaller',
1290 $content = qq|<span id="stock_in_out_qty_display_${i}">${content}</span><input type=hidden id='stock_in_out_qty_matches_$i' value='$matches'> <input type="button" onclick="open_stock_in_out_window('${in_out}', $i);" value="?">|;
1292 $main::lxdebug->leave_sub();
1297 sub get_basic_bin_wh_info {
1298 $main::lxdebug->enter_sub();
1300 my $stock_info = shift;
1302 my $form = $main::form;
1304 foreach my $sinfo (@{ $stock_info }) {
1305 next unless ($sinfo->{bin_id});
1307 my $bin_info = WH->get_basic_bin_info('id' => $sinfo->{bin_id});
1308 map { $sinfo->{"${_}_description"} = $sinfo->{"${_}description"} = $bin_info->{"${_}_description"} } qw(bin warehouse);
1311 $main::lxdebug->leave_sub();
1314 sub stock_in_out_form {
1315 $main::lxdebug->enter_sub();
1317 my $form = $main::form;
1319 if ($form->{in_out} eq 'out') {
1325 $main::lxdebug->leave_sub();
1328 sub redo_stock_info {
1329 $main::lxdebug->enter_sub();
1333 my $form = $main::form;
1335 my @non_empty = grep { $_->{qty} } @{ $params{stock_info} };
1337 if ($params{add_empty_row}) {
1339 'warehouse_id' => scalar(@non_empty) ? $non_empty[-1]->{warehouse_id} : undef,
1340 'bin_id' => scalar(@non_empty) ? $non_empty[-1]->{bin_id} : undef,
1344 @{ $params{stock_info} } = @non_empty;
1346 $main::lxdebug->leave_sub();
1349 sub update_stock_in {
1350 $main::lxdebug->enter_sub();
1352 my $form = $main::form;
1353 my %myconfig = %main::myconfig;
1355 my $stock_info = [];
1357 foreach my $i (1..$form->{rowcount}) {
1358 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1359 push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber
1360 bestbefore qty unit delivery_order_items_stock_id) };
1363 display_stock_in_form($stock_info);
1365 $main::lxdebug->leave_sub();
1369 $main::lxdebug->enter_sub();
1371 my $form = $main::form;
1373 my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
1375 display_stock_in_form($stock_info);
1377 $main::lxdebug->leave_sub();
1380 sub display_stock_in_form {
1381 $main::lxdebug->enter_sub();
1383 my $stock_info = shift;
1385 my $form = $main::form;
1386 my %myconfig = %main::myconfig;
1387 my $locale = $main::locale;
1389 $form->{title} = $locale->text('Stock');
1391 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
1393 # Standardlagerplatz für Standard-Auslagern verwenden, falls keiner für die Ware explizit definiert wurde
1394 if ($::instance_conf->get_transfer_default_use_master_default_bin) {
1395 $part_info->{warehouse_id} ||= $::instance_conf->get_warehouse_id;
1396 $part_info->{bin_id} ||= $::instance_conf->get_bin_id;
1399 my $units = AM->retrieve_units(\%myconfig, $form);
1400 # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
1401 my $units_data = AM->unit_select_data($units, $form->{do_unit}, undef, $part_info->{unit});
1403 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
1404 'bins' => 'BINS' });
1406 redo_stock_info('stock_info' => $stock_info, 'add_empty_row' => !$form->{delivered});
1408 get_basic_bin_wh_info($stock_info);
1410 $form->header(no_layout => 1);
1411 print $form->parse_html_template('do/stock_in_form', { 'UNITS' => $units_data,
1412 'STOCK_INFO' => $stock_info,
1413 'PART_INFO' => $part_info, });
1415 $main::lxdebug->leave_sub();
1418 sub _stock_in_out_set_qty_display {
1419 my $stock_info = shift;
1421 my $all_units = AM->retrieve_all_units();
1422 my $sum = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $stock_info });
1423 $form->{qty_display} = $form->format_amount_units(amount => $sum * 1,
1424 part_unit => $form->{partunit},
1425 amount_unit => $all_units->{ $form->{partunit} }->{base_unit},
1426 conv_units => 'convertible_not_smaller',
1431 $main::lxdebug->enter_sub();
1433 my $form = $main::form;
1434 my %myconfig = %main::myconfig;
1436 my $stock_info = [];
1438 foreach my $i (1..$form->{rowcount}) {
1439 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1441 next if ($form->{"qty_$i"} <= 0);
1443 push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(delivery_order_items_stock_id warehouse_id bin_id chargenumber bestbefore qty unit) };
1446 $form->{stock} = SL::YAML::Dump($stock_info);
1448 _stock_in_out_set_qty_display($stock_info);
1450 my $do_qty = AM->sum_with_unit($::form->parse_amount(\%::myconfig, $::form->{do_qty}), $::form->{do_unit});
1451 my $transfer_qty = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $stock_info });
1454 print $form->parse_html_template('do/set_stock_in_out', {
1455 qty_matches => $do_qty == $transfer_qty,
1458 $main::lxdebug->leave_sub();
1461 sub stock_out_form {
1462 $main::lxdebug->enter_sub();
1464 my $form = $main::form;
1465 my %myconfig = %main::myconfig;
1466 my $locale = $main::locale;
1468 $form->{title} = $locale->text('Release From Stock');
1470 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
1472 my $units = AM->retrieve_units(\%myconfig, $form);
1473 my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
1475 my @contents = DO->get_item_availability('parts_id' => $form->{parts_id});
1477 my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
1479 if (!$form->{delivered}) {
1480 foreach my $row (@contents) {
1481 $row->{available_qty} = $form->format_amount_units('amount' => $row->{qty} * 1,
1482 'part_unit' => $part_info->{unit},
1483 'conv_units' => 'convertible_not_smaller',
1486 foreach my $sinfo (@{ $stock_info }) {
1487 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
1488 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1489 ($row->{chargenumber} ne $sinfo->{chargenumber}) ||
1490 ($row->{bestbefore} ne $sinfo->{bestbefore}));
1492 map { $row->{"stock_$_"} = $sinfo->{$_} } qw(qty unit error delivery_order_items_stock_id);
1497 get_basic_bin_wh_info($stock_info);
1499 foreach my $sinfo (@{ $stock_info }) {
1500 map { $sinfo->{"stock_$_"} = $sinfo->{$_} } qw(qty unit);
1504 $form->header(no_layout => 1);
1505 print $form->parse_html_template('do/stock_out_form', { 'UNITS' => $units_data,
1506 'WHCONTENTS' => $form->{delivered} ? $stock_info : \@contents,
1507 'PART_INFO' => $part_info, });
1509 $main::lxdebug->leave_sub();
1513 $main::lxdebug->enter_sub();
1515 my $form = $main::form;
1516 my %myconfig = %main::myconfig;
1517 my $locale = $main::locale;
1519 my $stock_info = [];
1521 foreach my $i (1 .. $form->{rowcount}) {
1522 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1524 next if ($form->{"qty_$i"} <= 0);
1526 push @{ $stock_info }, {
1527 'warehouse_id' => $form->{"warehouse_id_$i"},
1528 'bin_id' => $form->{"bin_id_$i"},
1529 'chargenumber' => $form->{"chargenumber_$i"},
1530 'bestbefore' => $form->{"bestbefore_$i"},
1531 'qty' => $form->{"qty_$i"},
1532 'unit' => $form->{"unit_$i"},
1534 'delivery_order_items_stock_id' => $form->{"delivery_order_items_stock_id_$i"},
1538 my @errors = DO->check_stock_availability('requests' => $stock_info,
1539 'parts_id' => $form->{parts_id});
1541 $form->{stock} = SL::YAML::Dump($stock_info);
1544 $form->{ERRORS} = [];
1545 map { push @{ $form->{ERRORS} }, $locale->text('Error in row #1: The quantity you entered is bigger than the stocked quantity.', $_->{row}); } @errors;
1546 stock_in_out_form();
1549 _stock_in_out_set_qty_display($stock_info);
1551 my $do_qty = AM->sum_with_unit($::form->parse_amount(\%::myconfig, $::form->{do_qty}), $::form->{do_unit});
1552 my $transfer_qty = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $stock_info });
1555 print $form->parse_html_template('do/set_stock_in_out', {
1556 qty_matches => $do_qty == $transfer_qty,
1560 $main::lxdebug->leave_sub();
1564 $main::lxdebug->enter_sub();
1566 my $form = $main::form;
1567 my %myconfig = %main::myconfig;
1568 my $locale = $main::locale;
1570 if ($form->{id} && DO->is_marked_as_delivered(id => $form->{id})) {
1571 $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred in.'));
1574 save(no_redirect => 1);
1576 my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_in_${_}"} } (1 .. $form->{rowcount});
1580 my $units = AM->retrieve_units(\%myconfig, $form);
1581 my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1584 $form->{ERRORS} = [];
1586 foreach my $i (1 .. $form->{rowcount}) {
1587 next unless ($form->{"id_$i"} && $form->{"stock_in_$i"});
1589 my $row_sum_base_qty = 0;
1590 my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1592 foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_in_$i"}) }) {
1593 $request->{parts_id} = $form->{"id_$i"};
1594 $row_sum_base_qty += $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1596 $request->{project_id} = $form->{"project_id_$i"} || $form->{globalproject_id};
1598 push @all_requests, $request;
1601 next if (0 == $row_sum_base_qty);
1603 my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1605 # if ($do_base_qty != $row_sum_base_qty) {
1606 # push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no stock at all or the full quantity of #2 #3.',
1607 # $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1611 if (@{ $form->{ERRORS} }) {
1612 push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1614 set_headings('edit');
1616 $main::lxdebug->leave_sub();
1618 $::dispatcher->end_request;
1622 DO->transfer_in_out('direction' => 'in',
1623 'requests' => \@all_requests);
1625 SL::DB::DeliveryOrder->new(id => $form->{id})->load->update_attributes(delivered => 1);
1627 flash_later('info', $locale->text("Transfer successful"));
1628 $form->{callback} = 'do.pl?action=edit&type=purchase_delivery_order&id=' . $form->escape($form->{id});
1631 $main::lxdebug->leave_sub();
1635 $main::lxdebug->enter_sub();
1637 my $form = $main::form;
1638 my %myconfig = %main::myconfig;
1639 my $locale = $main::locale;
1641 if ($form->{id} && DO->is_marked_as_delivered(id => $form->{id})) {
1642 $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred out.'));
1645 save(no_redirect => 1);
1647 my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_out_${_}"} } (1 .. $form->{rowcount});
1651 my $units = AM->retrieve_units(\%myconfig, $form);
1652 my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1655 $form->{ERRORS} = [];
1657 foreach my $i (1 .. $form->{rowcount}) {
1658 next unless ($form->{"id_$i"} && $form->{"stock_out_$i"});
1660 my $row_sum_base_qty = 0;
1661 my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1663 foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_out_$i"}) }) {
1664 $request->{parts_id} = $form->{"id_$i"};
1665 $request->{base_qty} = $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1666 $request->{project_id} = $form->{"project_id_$i"} ? $form->{"project_id_$i"} : $form->{globalproject_id};
1668 my $map_key = join '--', ($form->{"id_$i"}, @{$request}{qw(warehouse_id bin_id chargenumber bestbefore)});
1670 $request_map{$map_key} ||= $request;
1671 $request_map{$map_key}->{sum_base_qty} ||= 0;
1672 $request_map{$map_key}->{sum_base_qty} += $request->{base_qty};
1673 $row_sum_base_qty += $request->{base_qty};
1675 push @all_requests, $request;
1678 next if (0 == $row_sum_base_qty);
1680 my $do_base_qty = $form->{"qty_$i"} * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1682 # if ($do_base_qty != $row_sum_base_qty) {
1683 # push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no transfer at all or the full quantity of #2 #3.',
1684 # $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1689 my @bin_ids = map { $_->{bin_id} } values %request_map;
1690 my %bin_info_map = WH->get_basic_bin_info('id' => \@bin_ids);
1691 my @contents = DO->get_item_availability('parts_id' => \@part_ids);
1693 foreach my $inv (@contents) {
1694 my $map_key = join '--', @{$inv}{qw(parts_id warehouse_id bin_id chargenumber bestbefore)};
1696 next unless ($request_map{$map_key});
1698 my $request = $request_map{$map_key};
1699 $request->{ok} = $request->{sum_base_qty} <= $inv->{qty};
1702 foreach my $request (values %request_map) {
1703 next if ($request->{ok});
1705 my $pinfo = $part_info_map{$request->{parts_id}};
1706 my $binfo = $bin_info_map{$request->{bin_id}};
1708 if ($::instance_conf->get_show_bestbefore) {
1709 push @{ $form->{ERRORS} }, $locale->text("There is not enough available of '#1' at warehouse '#2', bin '#3', #4, #5, for the transfer of #6.",
1710 $pinfo->{description},
1711 $binfo->{warehouse_description},
1712 $binfo->{bin_description},
1713 $request->{chargenumber} ? $locale->text('chargenumber #1', $request->{chargenumber}) : $locale->text('no chargenumber'),
1714 $request->{bestbefore} ? $locale->text('bestbefore #1', $request->{bestbefore}) : $locale->text('no bestbefore'),
1715 $form->format_amount_units('amount' => $request->{sum_base_qty},
1716 'part_unit' => $pinfo->{unit},
1717 'conv_units' => 'convertible_not_smaller'));
1719 push @{ $form->{ERRORS} }, $locale->text("There is not enough available of '#1' at warehouse '#2', bin '#3', #4, for the transfer of #5.",
1720 $pinfo->{description},
1721 $binfo->{warehouse_description},
1722 $binfo->{bin_description},
1723 $request->{chargenumber} ? $locale->text('chargenumber #1', $request->{chargenumber}) : $locale->text('no chargenumber'),
1724 $form->format_amount_units('amount' => $request->{sum_base_qty},
1725 'part_unit' => $pinfo->{unit},
1726 'conv_units' => 'convertible_not_smaller'));
1731 if (@{ $form->{ERRORS} }) {
1732 push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1734 set_headings('edit');
1736 $main::lxdebug->leave_sub();
1738 $::dispatcher->end_request;
1741 DO->transfer_in_out('direction' => 'out',
1742 'requests' => \@all_requests);
1744 SL::DB::DeliveryOrder->new(id => $form->{id})->load->update_attributes(delivered => 1);
1746 flash_later('info', $locale->text("Transfer successful"));
1747 $form->{callback} = 'do.pl?action=edit&type=sales_delivery_order&id=' . $form->escape($form->{id});
1750 $main::lxdebug->leave_sub();
1754 $main::lxdebug->enter_sub();
1756 my $form = $main::form;
1758 DO->close_orders('ids' => [ $form->{id} ]);
1760 $form->{closed} = 1;
1764 $main::lxdebug->leave_sub();
1768 $::lxdebug->enter_sub;
1770 $::auth->assert('purchase_delivery_order_edit | sales_delivery_order_edit');
1773 retrieve_partunits();
1775 my $new_rowcount = $::form->{"rowcount"} * 1 + 1;
1776 $::form->{"project_id_${new_rowcount}"} = $::form->{"globalproject_id"};
1778 $::form->language_payment(\%::myconfig);
1780 Common::webdav_folder($::form);
1783 display_row(++$::form->{rowcount});
1786 $::lxdebug->leave_sub;
1790 call_sub($main::form->{yes_nextsub});
1794 call_sub($main::form->{no_nextsub});
1798 call_sub($main::form->{update_nextsub} || $main::form->{nextsub} || 'update_delivery_order');
1802 my $form = $main::form;
1803 my $locale = $main::locale;
1805 foreach my $action (qw(update print save transfer_out transfer_out_default sort
1806 transfer_in transfer_in_default mark_closed save_as_new invoice delete)) {
1807 if ($form->{"action_${action}"}) {
1813 $form->error($locale->text('No action defined.'));
1816 sub transfer_out_default {
1817 $main::lxdebug->enter_sub();
1819 my $form = $main::form;
1821 transfer_in_out_default('direction' => 'out');
1823 $main::lxdebug->leave_sub();
1826 sub transfer_in_default {
1827 $main::lxdebug->enter_sub();
1829 my $form = $main::form;
1831 transfer_in_out_default('direction' => 'in');
1833 $main::lxdebug->leave_sub();
1836 # Falls das Standardlagerverfahren aktiv ist, wird
1837 # geprüft, ob alle Standardlagerplätze für die Auslager-
1838 # artikel vorhanden sind UND ob die Warenmenge ausreicht zum
1839 # Auslagern. Falls nicht wird entsprechend eine Fehlermeldung
1840 # generiert. Offen Chargennummer / bestbefore wird nicht berücksichtigt
1841 sub transfer_in_out_default {
1842 $main::lxdebug->enter_sub();
1844 my $form = $main::form;
1845 my %myconfig = %main::myconfig;
1846 my $locale = $main::locale;
1849 my (%missing_default_bins, %qty_parts, @all_requests, %part_info_map, $default_warehouse_id, $default_bin_id);
1851 Common::check_params(\%params, qw(direction));
1853 # entsprechende defaults holen, falls standardlagerplatz verwendet werden soll
1854 if ($::instance_conf->get_transfer_default_use_master_default_bin) {
1855 $default_warehouse_id = $::instance_conf->get_warehouse_id;
1856 $default_bin_id = $::instance_conf->get_bin_id;
1860 my @part_ids = map { $form->{"id_${_}"} } (1 .. $form->{rowcount});
1862 my $units = AM->retrieve_units(\%myconfig, $form);
1863 %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1864 foreach my $i (1 .. $form->{rowcount}) {
1865 next unless ($form->{"id_$i"});
1866 my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1867 my $qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1869 $form->show_generic_error($locale->text("Cannot transfer negative entries." )) if ($qty < 0);
1870 # if we do not want to transfer services and this part is a service, set qty to zero
1871 # ... and do not create a hash entry in %qty_parts below (will skip check for bins for the transfer == out case)
1872 # ... and push only a empty (undef) element to @all_requests (will skip check for bin_id and warehouse_id and will not alter the row)
1874 $qty = 0 if (!$::instance_conf->get_transfer_default_services && $part_info_map{$form->{"id_$i"}}->{part_type} eq 'service');
1875 $qty_parts{$form->{"id_$i"}} += $qty;
1877 delete $qty_parts{$form->{"id_$i"}} unless $qty_parts{$form->{"id_$i"}};
1878 undef $form->{"stock_in_$i"};
1881 $part_info_map{$form->{"id_$i"}}{bin_id} ||= $default_bin_id;
1882 $part_info_map{$form->{"id_$i"}}{warehouse_id} ||= $default_warehouse_id;
1884 push @all_requests, ($qty == 0) ? { } : {
1885 'chargenumber' => '', #?? die müsste entsprechend geholt werden
1886 #'bestbefore' => undef, # TODO wird nicht berücksichtigt
1887 'bin_id' => $part_info_map{$form->{"id_$i"}}{bin_id},
1889 'parts_id' => $form->{"id_$i"},
1890 'comment' => $locale->text("Default transfer delivery order"),
1891 'unit' => $part_info_map{$form->{"id_$i"}}{unit},
1892 'warehouse_id' => $part_info_map{$form->{"id_$i"}}{warehouse_id},
1893 'oe_id' => $form->{id},
1894 'project_id' => $form->{"project_id_$i"} ? $form->{"project_id_$i"} : $form->{globalproject_id}
1898 # jetzt wird erst überprüft, ob die Stückzahl entsprechend stimmt.
1899 # check if bin (transfer in and transfer out and qty (transfer out) is correct
1900 foreach my $key (keys %qty_parts) {
1902 $missing_default_bins{$key}{missing_bin} = 1 unless ($part_info_map{$key}{bin_id});
1903 next unless ($part_info_map{$key}{bin_id}); # abbruch
1905 if ($params{direction} eq 'out') { # wird nur für ausgehende Mengen benötigt
1906 my ($max_qty, $error) = WH->get_max_qty_parts_bin(parts_id => $key, bin_id => $part_info_map{$key}{bin_id});
1908 # wir können nicht entscheiden, welche charge oder mhd (bestbefore) ausgewählt sein soll
1909 # deshalb rückmeldung nach oben geben, manuell auszulagern
1910 # TODO Bei nur einem Treffer mit Charge oder bestbefore wäre das noch möglich
1911 $missing_default_bins{$key}{chargenumber} = 1;
1913 if ($max_qty < $qty_parts{$key}){
1914 $missing_default_bins{$key}{missing_qty} = $max_qty - $qty_parts{$key};
1920 # Abfrage für Fehlerbehandlung (nur bei direction == out)
1921 if (scalar (keys %missing_default_bins)) {
1923 foreach my $fehler (keys %missing_default_bins) {
1925 my $ware = WH->get_part_description(parts_id => $fehler);
1926 if ($missing_default_bins{$fehler}{missing_bin}){
1927 $fehlertext .= "Kein Standardlagerplatz definiert bei $ware <br>";
1929 if ($missing_default_bins{$fehler}{missing_qty}) { # missing_qty
1930 $fehlertext .= "Es fehlen " . $missing_default_bins{$fehler}{missing_qty}*-1 .
1931 " von $ware auf dem Standard-Lagerplatz " . $part_info_map{$fehler}{bin} . " zum Auslagern<br>";
1933 if ($missing_default_bins{$fehler}{chargenumber}){
1934 $fehlertext .= "Die Ware hat eine Chargennummer oder eine Mindesthaltbarkeit definiert.
1935 Hier kann man nicht automatisch entscheiden.
1936 Bitte diesen Lieferschein manuell auslagern.
1939 # auslagern soll immer gehen, auch wenn nicht genügend auf lager ist.
1940 # der lagerplatz ist hier extra konfigurierbar, bspw. Lager-Korrektur mit
1941 # Lagerplatz Lagerplatz-Korrektur
1942 my $default_warehouse_id_ignore_onhand = $::instance_conf->get_warehouse_id_ignore_onhand;
1943 my $default_bin_id_ignore_onhand = $::instance_conf->get_bin_id_ignore_onhand;
1944 if ($::instance_conf->get_transfer_default_ignore_onhand && $default_bin_id_ignore_onhand) {
1945 # entsprechende defaults holen
1946 # falls chargenumber, bestbefore oder anzahl nicht stimmt, auf automatischen
1947 # lagerplatz wegbuchen!
1948 foreach (@all_requests) {
1949 if ($_->{parts_id} eq $fehler){
1950 $_->{bin_id} = $default_bin_id_ignore_onhand;
1951 $_->{warehouse_id} = $default_warehouse_id_ignore_onhand;
1955 #$main::lxdebug->message(0, 'Fehlertext: ' . $fehlertext);
1956 $form->show_generic_error($locale->text("Cannot transfer. <br> Reason:<br>#1", $fehlertext ));
1962 # hier der eigentliche fallunterschied für in oder out
1963 my $prefix = $params{direction} eq 'in' ? 'in' : 'out';
1965 # dieser array_ref ist für DO->save da:
1966 # einmal die all_requests in YAML verwandeln, damit delivery_order_items_stock
1967 # gefüllt werden kann.
1968 # could be dumped to the form in the first loop,
1969 # but maybe bin_id and warehouse_id has changed to the "korrekturlager" with
1970 # allowed negative qty ($::instance_conf->get_warehouse_id_ignore_onhand) ...
1972 foreach (@all_requests){
1974 next unless scalar(%{ $_ });
1975 $form->{"stock_${prefix}_$i"} = SL::YAML::Dump([$_]);
1978 save(no_redirect => 1); # Wir können auslagern, deshalb beleg speichern
1979 # und in delivery_order_items_stock speichern
1981 # ... and fill back the persistent dois_id for inventory fk
1982 undef (@all_requests);
1983 foreach my $i (1 .. $form->{rowcount}) {
1984 next unless ($form->{"id_$i"} && $form->{"stock_${prefix}_$i"});
1985 push @all_requests, @{ DO->unpack_stock_information('packed' => $form->{"stock_${prefix}_$i"}) };
1987 DO->transfer_in_out('direction' => $prefix,
1988 'requests' => \@all_requests);
1990 SL::DB::DeliveryOrder->new(id => $form->{id})->load->update_attributes(delivered => 1);
1992 $form->{callback} = 'do.pl?action=edit&type=sales_delivery_order&id=' . $form->escape($form->{id}) if $params{direction} eq 'out';
1993 $form->{callback} = 'do.pl?action=edit&type=purchase_delivery_order&id=' . $form->escape($form->{id}) if $params{direction} eq 'in';
1999 $main::lxdebug->enter_sub();
2003 my $form = $main::form;
2006 save(no_redirect => 1); # has to be done, at least for newly added positions
2008 # hashify partnumbers, positions. key is delivery_order_items_id
2009 for my $i (1 .. ($form->{rowcount}) ) {
2010 $temp_hash{$form->{"delivery_order_items_id_$i"}} = { runningnumber => $form->{"runningnumber_$i"}, partnumber => $form->{"partnumber_$i"} };
2011 if ($form->{id} && $form->{"discount_$i"}) {
2012 # prepare_order assumes a db value if there is a form->id and multiplies *100
2013 # We hope for new controller code (no more format_amount/parse_amount distinction)
2014 $form->{"discount_$i"} /=100;
2017 # naturally sort partnumbers and get a sorted array of doi_ids
2018 my @sorted_doi_ids = sort { Sort::Naturally::ncmp($temp_hash{$a}->{"partnumber"}, $temp_hash{$b}->{"partnumber"}) } keys %temp_hash;
2023 for (@sorted_doi_ids) {
2024 $form->{"runningnumber_$temp_hash{$_}->{runningnumber}"} = $new_number;
2027 # all parse_amounts changes are in form (i.e. , to .) therefore we need
2028 # another format_amount to change it back, for the next save ;-(
2029 # works great except for row discounts (see above comment)
2033 $main::lxdebug->leave_sub();
2045 do.pl - Script for all calls to delivery order
2053 Sorts all position with Natural Sort. Can be activated in form_footer.html like this
2054 C<E<lt>input class="submit" type="submit" name="action_sort" id="sort_button" value="[% 'Sort and Save' | $T8 %]"E<gt>>
2060 Sort and Save can be implemented as an optional button if configuration ca be set by client config.
2061 Example coding for database scripts and templates in (git show af2f24b8), check also
2062 autogeneration for rose (scripts/rose_auto_create_model.pl --h)