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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
31 #======================================================================
33 use List::Util qw(max sum);
34 use POSIX qw(strftime);
37 use SL::DB::DeliveryOrder;
41 use SL::ReportGenerator;
44 require "bin/mozilla/arap.pl";
45 require "bin/mozilla/common.pl";
46 require "bin/mozilla/invoice_io.pl";
47 require "bin/mozilla/io.pl";
48 require "bin/mozilla/reportgenerator.pl";
57 $main::auth->assert($main::form->{type} . '_edit');
61 $main::lxdebug->enter_sub();
67 my $form = $main::form;
68 my $locale = $main::locale;
70 if ($form->{type} eq 'purchase_delivery_order') {
71 $form->{vc} = 'vendor';
72 $form->{title} = $action eq "edit" ? $locale->text('Edit Purchase Delivery Order') : $locale->text('Add Purchase Delivery Order');
74 $form->{vc} = 'customer';
75 $form->{title} = $action eq "edit" ? $locale->text('Edit Sales Delivery Order') : $locale->text('Add Sales Delivery Order');
78 $form->{heading} = $locale->text('Delivery Order');
80 $main::lxdebug->leave_sub();
84 $main::lxdebug->enter_sub();
88 my $form = $main::form;
92 $form->{callback} = build_std_url('action=add', 'type', 'vc') unless ($form->{callback});
98 $main::lxdebug->leave_sub();
102 $main::lxdebug->enter_sub();
106 my $form = $main::form;
108 # show history button
109 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
110 #/show hhistory button
112 $form->{simple_save} = 0;
114 set_headings("edit");
116 # editing without stuff to edit? try adding it first
117 if ($form->{rowcount} && !$form->{print_and_save}) {
118 # map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
122 undef $form->{rowcount};
124 $main::lxdebug->leave_sub();
127 } elsif (!$form->{id}) {
129 $main::lxdebug->leave_sub();
133 my ($language_id, $printer_id);
134 if ($form->{print_and_save}) {
135 $form->{action} = "dispatcher";
136 $form->{action_print} = "1";
137 $form->{resubmit} = 1;
138 $language_id = $form->{language_id};
139 $printer_id = $form->{printer_id};
142 set_headings("edit");
147 if ($form->{print_and_save}) {
148 $form->{language_id} = $language_id;
149 $form->{printer_id} = $printer_id;
154 $main::lxdebug->leave_sub();
158 $main::lxdebug->enter_sub();
162 my $form = $main::form;
163 my %myconfig = %main::myconfig;
165 # get customer/vendor
166 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
168 # retrieve order/quotation
169 $form->{webdav} = $::lx_office_conf{features}->{webdav};
170 $form->{jsscript} = 1;
172 my $editing = $form->{id};
174 DO->retrieve('vc' => $form->{vc},
175 'ids' => $form->{id});
177 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
178 $form->{shipto} = 1 if $form->{id};
180 # get customer / vendor
181 if ($form->{vc} eq 'vendor') {
182 IR->get_vendor(\%myconfig, \%$form);
183 $form->{discount} = $form->{vendor_discount};
185 IS->get_customer(\%myconfig, \%$form);
186 $form->{discount} = $form->{customer_discount};
189 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
190 $form->restore_vars(qw(taxincluded)) if $form->{id};
191 $form->restore_vars(qw(salesman_id)) if $editing;
193 if ($form->{"all_$form->{vc}"}) {
194 unless ($form->{"$form->{vc}_id"}) {
195 $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
199 ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
200 $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
202 $form->{employee} = "$form->{employee}--$form->{employee_id}";
204 $main::lxdebug->leave_sub();
208 $main::lxdebug->enter_sub();
212 my $form = $main::form;
213 my %myconfig = %main::myconfig;
215 $form->{formname} = $form->{type} unless $form->{formname};
218 foreach my $ref (@{ $form->{form_details} }) {
219 $form->{rowcount} = ++$i;
221 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
223 for my $i (1 .. $form->{rowcount}) {
225 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
227 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
229 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
231 my $decimalplaces = ($dec > 2) ? $dec : 2;
233 # copy reqdate from deliverydate for invoice -> order conversion
234 $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
236 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
237 $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, $decimalplaces);
239 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
240 $dec_qty = length $dec_qty;
241 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
244 $main::lxdebug->leave_sub();
248 $main::lxdebug->enter_sub();
252 my $form = $main::form;
253 my %myconfig = %main::myconfig;
255 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
256 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
258 # use JavaScript Calendar or not
259 $form->{jsscript} = 1;
262 my $jsscript = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
264 my @old_project_ids = ($form->{"globalproject_id"});
265 map({ push(@old_project_ids, $form->{"project_id_$_"})
266 if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
268 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
269 $form->get_lists("contacts" => "ALL_CONTACTS",
270 "shipto" => "ALL_SHIPTO",
272 "key" => "ALL_PROJECTS",
274 "old_id" => \@old_project_ids
276 "employees" => "ALL_EMPLOYEES",
277 "salesmen" => "ALL_SALESMEN",
279 "price_factors" => "ALL_PRICE_FACTORS",
280 "departments" => "ALL_DEPARTMENTS",
281 "business_types" => "ALL_BUSINESS_TYPES",
284 map { $_->{value} = "$_->{description}--$_->{id}" } @{ $form->{ALL_DEPARTMENTS} };
285 map { $_->{value} = "$_->{name}--$_->{id}" } @{ $form->{ALL_VC} };
287 $form->{SHOW_VC_DROP_DOWN} = $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
289 $form->{oldvcname} = $form->{"old$form->{vc}"};
290 $form->{oldvcname} =~ s/--.*//;
292 $form->{onload} = "";
293 if ($form->{resubmit}) {
294 if ($form->{format} eq "html") {
295 $form->{onload} = "window.open('about:blank','Beleg'); document.do.target = 'Beleg';";
297 # emulate click for resubmitting actions
298 $form->{onload} .= "document.do.${_}.click(); " for grep { /^action_/ } keys %$form;
299 $form->{onload} .= "document.do.submit();"
303 # Fix für Bug 1082 Erwartet wird: 'abteilungsNAME--abteilungsID'
304 $form->{department} .= '--' . $form->{department_id};
306 print $form->parse_html_template('do/form_header');
308 $main::lxdebug->leave_sub();
312 $main::lxdebug->enter_sub();
316 my $form = $main::form;
318 $form->{PRINT_OPTIONS} = print_options('inline' => 1);
320 print $form->parse_html_template('do/form_footer');
322 $main::lxdebug->leave_sub();
325 sub update_delivery_order {
326 $main::lxdebug->enter_sub();
330 my $form = $main::form;
331 my %myconfig = %main::myconfig;
333 set_headings($form->{"id"} ? "edit" : "add");
338 $payment_id = $form->{payment_id} if $form->{payment_id};
340 check_name($form->{vc});
341 $form->{discount} = $form->{"$form->{vc}_discount"} if defined $form->{"$form->{vc}_discount"};
342 # Problem: Wenn man ohne Erneuern einen Kunden/Lieferanten
343 # wechselt, wird der entsprechende Kunden/ Lieferantenrabatt
344 # nicht übernommen. Grundproblem: In Commit 82574e78
345 # hab ich aus discount customer_discount und vendor_discount
346 # gemacht und entsprechend an den Oberflächen richtig hin-
347 # geschoben. Die damals bessere Lösung wäre gewesen:
348 # In den Templates nur die hidden für form-discount wieder ein-
349 # setzen dann wäre die Verrenkung jetzt nicht notwendig.
350 # TODO: Ggf. Bugfix 1284, 1575 und 817 wieder zusammenführen
351 # Testfälle: Kunden mit Rabatt 0 -> Rabatt 20 i.O.
352 # Kunde mit Rabatt 20 -> Rabatt 0 i.O.
353 # Kunde mit Rabatt 20 -> Rabatt 5,5 i.O.
354 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
357 my $i = $form->{rowcount};
359 if ( ($form->{"partnumber_$i"} eq "")
360 && ($form->{"description_$i"} eq "")
361 && ($form->{"partsgroup_$i"} eq "")) {
367 if ($form->{type} eq 'purchase_delivery_order') {
368 IR->retrieve_item(\%myconfig, $form);
370 IS->retrieve_item(\%myconfig, $form);
373 my $rows = scalar @{ $form->{item_list} };
376 $form->{"qty_$i"} = 1 unless $form->parse_amount(\%myconfig, $form->{"qty_$i"});
385 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
387 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
388 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
389 $form->{"lastcost_$i"} = $form->format_amount(\%myconfig, $form->{"lastcost_$i"});
390 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
397 # ok, so this is a new part
398 # ask if it is a part or service item
400 if ( $form->{"partsgroup_$i"}
401 && ($form->{"partsnumber_$i"} eq "")
402 && ($form->{"description_$i"} eq "")) {
404 $form->{"discount_$i"} = "";
405 $form->{"not_discountable_$i"} = "";
409 $form->{"id_$i"} = 0;
415 $main::lxdebug->leave_sub();
419 $main::lxdebug->enter_sub();
423 my $form = $main::form;
424 my %myconfig = %main::myconfig;
425 my $locale = $main::locale;
427 $form->{vc} = $form->{type} eq 'purchase_delivery_order' ? 'vendor' : 'customer';
429 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
431 "employees" => "ALL_EMPLOYEES",
432 "salesmen" => "ALL_SALESMEN",
433 "$form->{vc}s" => "ALL_VC");
435 $form->{SHOW_VC_DROP_DOWN} = $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
436 $form->{jsscript} = 1;
437 $form->{title} = $locale->text('Delivery Orders');
441 print $form->parse_html_template('do/search');
443 $main::lxdebug->leave_sub();
447 $main::lxdebug->enter_sub();
451 my $form = $main::form;
452 my %myconfig = %main::myconfig;
453 my $locale = $main::locale;
454 my $cgi = $main::cgi;
456 ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) = split(/--/, $form->{ $form->{vc} });
458 report_generator_set_default_sort('transdate', 1);
462 $form->{rowcount} = scalar @{ $form->{DO} };
469 shipvia globalprojectnumber
470 transaction_description
474 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
475 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
477 $form->{title} = $locale->text('Delivery Orders');
479 my $attachment_basename = $form->{vc} eq 'vendor' ? $locale->text('purchase_delivery_order_list') : $locale->text('sales_delivery_order_list');
481 my $report = SL::ReportGenerator->new(\%myconfig, $form);
483 my @hidden_variables = map { "l_${_}" } @columns;
484 push @hidden_variables, $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered donumber ordnumber
485 transaction_description transdatefrom transdateto type vc employee_id salesman_id project_id);
487 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
490 'ids' => { 'text' => '', },
491 'transdate' => { 'text' => $locale->text('Date'), },
492 'id' => { 'text' => $locale->text('ID'), },
493 'donumber' => { 'text' => $locale->text('Delivery Order'), },
494 'ordnumber' => { 'text' => $locale->text('Order'), },
495 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
496 'employee' => { 'text' => $locale->text('Employee'), },
497 'shipvia' => { 'text' => $locale->text('Ship via'), },
498 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
499 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
500 'open' => { 'text' => $locale->text('Open'), },
501 'delivered' => { 'text' => $locale->text('Delivered'), },
504 foreach my $name (qw(id transdate donumber ordnumber name employee shipvia transaction_description)) {
505 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
506 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
509 $form->{"l_type"} = "Y";
510 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
512 $column_defs{ids}->{visible} = 'HTML';
514 $report->set_columns(%column_defs);
515 $report->set_column_order(@columns);
517 $report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
519 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
522 if ($form->{customer}) {
523 push @options, $locale->text('Customer') . " : $form->{customer}";
525 if ($form->{vendor}) {
526 push @options, $locale->text('Vendor') . " : $form->{vendor}";
528 if ($form->{department}) {
529 my ($department) = split /--/, $form->{department};
530 push @options, $locale->text('Department') . " : $department";
532 if ($form->{donumber}) {
533 push @options, $locale->text('Delivery Order Number') . " : $form->{donumber}";
535 if ($form->{ordnumber}) {
536 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
538 if ($form->{transaction_description}) {
539 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
541 if ($form->{transdatefrom}) {
542 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
544 if ($form->{transdateto}) {
545 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
548 push @options, $locale->text('Open');
550 if ($form->{closed}) {
551 push @options, $locale->text('Closed');
553 if ($form->{delivered}) {
554 push @options, $locale->text('Delivered');
556 if ($form->{notdelivered}) {
557 push @options, $locale->text('Not delivered');
560 $report->set_options('top_info_text' => join("\n", @options),
561 'raw_top_info_text' => $form->parse_html_template('do/orders_top'),
562 'raw_bottom_info_text' => $form->parse_html_template('do/orders_bottom'),
563 'output_format' => 'HTML',
564 'title' => $form->{title},
565 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
567 $report->set_options_from_form();
568 $locale->set_numberformat_wo_thousands_separator(\%myconfig) if lc($report->{options}->{output_format}) eq 'csv';
570 # add sort and escape callback, this one we use for the add sub
571 $form->{callback} = $href .= "&sort=$form->{sort}";
573 # escape callback for href
574 my $callback = $form->escape($href);
576 my $edit_url = build_std_url('action=edit', 'type', 'vc');
577 my $edit_order_url = build_std_url('script=oe.pl', 'type=' . ($form->{type} eq 'sales_delivery_order' ? 'sales_order' : 'purchase_order'), 'action=edit');
581 foreach my $dord (@{ $form->{DO} }) {
582 $dord->{open} = $dord->{closed} ? $locale->text('No') : $locale->text('Yes');
583 $dord->{delivered} = $dord->{delivered} ? $locale->text('Yes') : $locale->text('No');
585 my $row = { map { $_ => { 'data' => $dord->{$_} } } @columns };
588 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $dord->{id})
589 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
590 'valign' => 'center',
594 $row->{donumber}->{link} = $edit_url . "&id=" . E($dord->{id}) . "&callback=${callback}";
595 $row->{ordnumber}->{link} = $edit_order_url . "&id=" . E($dord->{oe_id}) . "&callback=${callback}";
597 $report->add_data($row);
602 $report->generate_with_headers();
604 $main::lxdebug->leave_sub();
608 $main::lxdebug->enter_sub();
614 my $form = $main::form;
615 my %myconfig = %main::myconfig;
616 my $locale = $main::locale;
618 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
620 $form->isblank("transdate", $locale->text('Delivery Order Date missing!'));
622 $form->{donumber} =~ s/^\s*//g;
623 $form->{donumber} =~ s/\s*$//g;
625 my $msg = ucfirst $form->{vc};
626 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
628 # $locale->text('Customer missing!');
629 # $locale->text('Vendor missing!');
631 remove_emptied_rows();
634 # if the name changed get new values
635 if (check_name($form->{vc})) {
640 $form->{id} = 0 if $form->{saveasnew};
641 # best case fix für bug 1079. Einkaufsrabatt wird nicht richtig
642 # aus Lieferantenauftrag -> Lieferschein -> Rechnung übernommen
643 # Tritt nur auf, wenn man direkt über Lieferschein -> speichern ->
644 # Workflow Rechnung geht (beim Aufruf über edit() i.O.)
645 # Gut. DO-save() speichert den Discount im DB-Format 0.12 für
646 # 12%, die Konvertierung wird leider in $form gemacht und daher
647 # wird die Maske mit dem falschen Rabatt wieder aufgebaut.
648 # Wie immer: backup_vars verwenden um nichts anderes kaputt zu
649 # machen. jan 03.03.2010
650 # nicht mehr notwendig da für bug 1284 der backend aufruf entsprechend
654 if(!exists $form->{addition}) {
655 $form->{snumbers} = qq|donumber_| . $form->{donumber};
656 $form->{addition} = "SAVED";
659 # /saving the history
661 $form->{simple_save} = 1;
662 if (!$params{no_redirect} && !$form->{print_and_save}) {
663 set_headings("edit");
667 $main::lxdebug->leave_sub();
671 $main::lxdebug->enter_sub();
675 my $form = $main::form;
676 my $locale = $main::locale;
678 map { delete $form->{$_} } qw(action header login password);
679 my @variables = map { { 'key' => $_, 'value' => $form->{$_} } } grep { '' eq ref $form->{$_} } keys %{ $form };
681 $form->{title} = $locale->text('Delete delivery order');
684 print $form->parse_html_template('do/delete', { 'VARIABLES' => \@variables });
686 $main::lxdebug->leave_sub();
689 sub delete_delivery_order {
690 $main::lxdebug->enter_sub();
694 my $form = $main::form;
695 my %myconfig = %main::myconfig;
696 my $locale = $main::locale;
700 if(!exists $form->{addition}) {
701 $form->{snumbers} = qq|donumber_| . $form->{donumber};
702 $form->{addition} = "DELETED";
705 # /saving the history
707 $form->info($locale->text('Delivery Order deleted!'));
711 $form->error($locale->text('Cannot delete delivery order!'));
713 $main::lxdebug->leave_sub();
717 $main::lxdebug->enter_sub();
719 my $form = $main::form;
720 my %myconfig = %main::myconfig;
721 my $locale = $main::locale;
724 $main::auth->assert($form->{type} eq 'purchase_delivery_order' ? 'vendor_invoice_edit' : 'invoice_edit');
726 $form->{convert_from_do_ids} = $form->{id};
727 $form->{deliverydate} = $form->{transdate};
728 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
729 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
730 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
734 delete @{$form}{qw(id closed delivered)};
736 my ($script, $buysell);
737 if ($form->{type} eq 'purchase_delivery_order') {
738 $form->{title} = $locale->text('Add Vendor Invoice');
739 $form->{script} = 'ir.pl';
744 $form->{title} = $locale->text('Add Sales Invoice');
745 $form->{script} = 'is.pl';
750 for my $i (1 .. $form->{rowcount}) {
752 unless ($form->{"ordnumber"}) {
753 if ($form->{discount}) { # Falls wir einen Lieferanten-/Kundenrabatt haben
754 # und rabattfähig sind, dann
755 unless ($form->{"not_discountable_$i"}) {
756 $form->{"discount_$i"} = $form->{discount}*100; # ... nehmen wir diesen Rabatt
760 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"} } qw(ship qty sellprice listprice lastcost basefactor);
763 $form->{type} = "invoice";
766 $main::locale = new Locale "$myconfig{countrycode}", "$script";
767 $locale = $main::locale;
769 require "bin/mozilla/$form->{script}";
771 my $currency = $form->{currency};
774 if ($form->{ordnumber}) {
775 require SL::DB::Order;
776 if (my $order = SL::DB::Manager::Order->find_by(ordnumber => $form->{ordnumber})) {
778 $form->{orddate} = $order->transdate_as_date;
779 $form->{$_} = $order->$_ for qw(payment_id salesman_id taxzone_id quonumber);
783 $form->{currency} = $currency;
784 $form->{exchangerate} = "";
785 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell);
786 $form->{exchangerate} = $form->{forex} if ($form->{forex});
791 for my $i (1 .. $form->{rowcount}) {
792 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
794 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
796 my $decimalplaces = ($dec > 2) ? $dec : 2;
798 # copy delivery date from reqdate for order -> invoice conversion
799 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
800 unless $form->{"deliverydate_$i"};
803 $form->{"sellprice_$i"} =
804 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
807 $form->{"lastcost_$i"} =
808 $form->format_amount(\%myconfig, $form->{"lastcost_$i"},
811 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
812 $dec_qty = length $dec_qty;
814 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
820 $main::lxdebug->leave_sub();
824 $main::lxdebug->enter_sub();
826 my $form = $main::form;
827 my %myconfig = %main::myconfig;
828 my $locale = $main::locale;
831 $main::auth->assert($form->{type} eq 'sales_delivery_order' ? 'invoice_edit' : 'vendor_invoice_edit');
833 my @do_ids = map { $form->{"trans_id_$_"} } grep { $form->{"multi_id_$_"} } (1..$form->{rowcount});
835 if (!scalar @do_ids) {
836 $form->show_generic_error($locale->text('You have not selected any delivery order.'), 'back_button' => 1);
839 map { delete $form->{$_} } grep { m/^(?:trans|multi)_id_\d+/ } keys %{ $form };
841 if (!DO->retrieve('vc' => $form->{vc}, 'ids' => \@do_ids)) {
842 $form->show_generic_error($form->{vc} eq 'customer' ?
843 $locale->text('You cannot create an invoice for delivery orders for different customers.') :
844 $locale->text('You cannot create an invoice for delivery orders from different vendors.'),
848 my $source_type = $form->{type};
849 $form->{convert_from_do_ids} = join ' ', @do_ids;
850 # bei der auswahl von mehreren Lieferscheinen fuer eine Rechnung, die einfach in donumber_array
851 # zwischenspeichern (DO.pm) und als ' '-separierte Liste wieder zurueckschreiben
852 # Hinweis: delete gibt den wert zurueck und loescht danach das element (nett und einfach)
853 # $shell: perldoc perlunc; /delete EXPR
854 $form->{donumber} = delete $form->{donumber_array};
855 $form->{deliverydate} = $form->{transdate};
856 $form->{transdate} = $form->current_date(\%myconfig);
857 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
858 $form->{type} = "invoice";
860 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
862 my ($script, $buysell);
863 if ($source_type eq 'purchase_delivery_order') {
864 $form->{title} = $locale->text('Add Vendor Invoice');
865 $form->{script} = 'ir.pl';
870 $form->{title} = $locale->text('Add Sales Invoice');
871 $form->{script} = 'is.pl';
876 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
878 # get vendor or customer discount
880 my $saved_form = save_form();
881 if ($form->{vc} eq 'vendor') {
882 IR->get_vendor(\%myconfig, \%$form);
883 $vc_discount = $form->{vendor_discount};
885 IS->get_customer(\%myconfig, \%$form);
886 $vc_discount = $form->parse_amount(\%myconfig, $form->{customer_discount});
888 restore_form($saved_form);
890 $form->{rowcount} = 0;
891 foreach my $ref (@{ $form->{form_details} }) {
893 $ref->{reqdate} ||= $ref->{dord_transdate}; # copy transdates into each invoice row
894 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{ $ref };
895 map { $form->{"${_}_$form->{rowcount}"} = $form->format_amount(\%myconfig, $ref->{$_}) } qw(qty sellprice lastcost);
897 if ($vc_discount){ # falls wir einen Lieferanten/Kundenrabatt haben
898 # und keinen anderen discount wert an $i ...
899 $form->{"discount_$form->{rowcount}"} ||= $vc_discount; # ... nehmen wir diesen Rabatt
902 $form->{"discount_$form->{rowcount}"} = $form->{"discount_$form->{rowcount}"} * 100; #s.a. Bug 1151
903 # Anm.: Eine Änderung des discounts in der SL/DO.pm->retrieve (select (doi.discount * 100) as discount) ergibt in psql einen
904 # Wert von 10.0000001490116. Ferner ist der Rabatt in der Rechnung dann bei 1.0 (?). Deswegen lasse ich das hier. jb 10.10.09
906 $form->{"discount_$form->{rowcount}"} = $form->format_amount(\%myconfig, $form->{"discount_$form->{rowcount}"});
908 delete $form->{form_details};
910 $locale = new Locale "$myconfig{countrycode}", "$script";
912 require "bin/mozilla/$form->{script}";
918 $main::lxdebug->leave_sub();
922 $main::lxdebug->enter_sub();
926 my $form = $main::form;
928 $form->{saveasnew} = 1;
930 $form->{delivered} = 0;
931 map { delete $form->{$_} } qw(printed emailed queued);
932 delete @{ $form }{ grep { m/^stock_(?:in|out)_\d+/ } keys %{ $form } };
934 # Let Lx-Office assign a new order number if the user hasn't changed the
935 # previous one. If it has been changed manually then use it as-is.
936 $form->{donumber} =~ s/^\s*//g;
937 $form->{donumber} =~ s/\s*$//g;
938 if ($form->{saved_donumber} && ($form->{saved_donumber} eq $form->{donumber})) {
939 delete($form->{donumber});
944 $main::lxdebug->leave_sub();
948 $main::lxdebug->enter_sub();
952 my $form = $main::form;
954 $form->{print_and_save} = 1;
956 my $saved_form = save_form();
960 restore_form($saved_form, 0, qw(id ordnumber quonumber));
964 $main::lxdebug->leave_sub();
967 sub calculate_stock_in_out {
968 $main::lxdebug->enter_sub();
970 my $form = $main::form;
974 if (!$form->{"id_${i}"}) {
975 $main::lxdebug->leave_sub();
979 my $all_units = AM->retrieve_all_units();
981 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
982 my $sinfo = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_${i}"});
984 my $sum = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $sinfo });
986 my $content = $form->format_amount_units('amount' => $sum * 1,
987 'part_unit' => $form->{"partunit_$i"},
988 'amount_unit' => $all_units->{$form->{"partunit_$i"}}->{base_unit},
989 'conv_units' => 'convertible_not_smaller',
991 $content = qq|<span id="stock_in_out_qty_display_${i}">${content}</span> <input type="button" onclick="open_stock_in_out_window('${in_out}', $i);" value="?">|;
993 $main::lxdebug->leave_sub();
998 sub get_basic_bin_wh_info {
999 $main::lxdebug->enter_sub();
1001 my $stock_info = shift;
1003 my $form = $main::form;
1005 foreach my $sinfo (@{ $stock_info }) {
1006 next unless ($sinfo->{bin_id});
1008 my $bin_info = WH->get_basic_bin_info('id' => $sinfo->{bin_id});
1009 map { $sinfo->{"${_}_description"} = $sinfo->{"${_}description"} = $bin_info->{"${_}_description"} } qw(bin warehouse);
1012 $main::lxdebug->leave_sub();
1015 sub stock_in_out_form {
1016 $main::lxdebug->enter_sub();
1018 my $form = $main::form;
1020 if ($form->{in_out} eq 'out') {
1026 $main::lxdebug->leave_sub();
1029 sub redo_stock_info {
1030 $main::lxdebug->enter_sub();
1034 my $form = $main::form;
1036 my @non_empty = grep { $_->{qty} } @{ $params{stock_info} };
1038 if ($params{add_empty_row}) {
1040 'warehouse_id' => scalar(@non_empty) ? $non_empty[-1]->{warehouse_id} : undef,
1041 'bin_id' => scalar(@non_empty) ? $non_empty[-1]->{bin_id} : undef,
1045 @{ $params{stock_info} } = @non_empty;
1047 $main::lxdebug->leave_sub();
1050 sub update_stock_in {
1051 $main::lxdebug->enter_sub();
1053 my $form = $main::form;
1054 my %myconfig = %main::myconfig;
1056 my $stock_info = [];
1058 foreach my $i (1..$form->{rowcount}) {
1059 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1060 push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber bestbefore qty unit) };
1063 display_stock_in_form($stock_info);
1065 $main::lxdebug->leave_sub();
1069 $main::lxdebug->enter_sub();
1071 my $form = $main::form;
1073 my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
1075 display_stock_in_form($stock_info);
1077 $main::lxdebug->leave_sub();
1080 sub display_stock_in_form {
1081 $main::lxdebug->enter_sub();
1083 my $stock_info = shift;
1085 my $form = $main::form;
1086 my %myconfig = %main::myconfig;
1087 my $locale = $main::locale;
1089 $form->{jsscript} = 1;
1091 $form->{title} = $locale->text('Stock');
1093 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
1095 my $units = AM->retrieve_units(\%myconfig, $form);
1096 # der zweite Parameter von unit_select_data gibt den default-Namen (selected) vor
1097 my $units_data = AM->unit_select_data($units, $form->{do_unit}, undef, $part_info->{unit});
1099 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
1100 'bins' => 'BINS' });
1102 redo_stock_info('stock_info' => $stock_info, 'add_empty_row' => !$form->{delivered});
1104 get_basic_bin_wh_info($stock_info);
1107 print $form->parse_html_template('do/stock_in_form', { 'UNITS' => $units_data,
1108 'STOCK_INFO' => $stock_info,
1109 'PART_INFO' => $part_info, });
1111 $main::lxdebug->leave_sub();
1114 sub _stock_in_out_set_qty_display {
1115 my $stock_info = shift;
1117 my $all_units = AM->retrieve_all_units();
1118 my $sum = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $stock_info });
1119 $form->{qty_display} = $form->format_amount_units(amount => $sum * 1,
1120 part_unit => $form->{partunit},
1121 amount_unit => $all_units->{ $form->{partunit} }->{base_unit},
1122 conv_units => 'convertible_not_smaller',
1127 $main::lxdebug->enter_sub();
1129 my $form = $main::form;
1130 my %myconfig = %main::myconfig;
1132 my $stock_info = [];
1134 foreach my $i (1..$form->{rowcount}) {
1135 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1137 next if ($form->{"qty_$i"} <= 0);
1139 push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber bestbefore qty unit) };
1142 $form->{stock} = YAML::Dump($stock_info);
1144 _stock_in_out_set_qty_display($stock_info);
1147 print $form->parse_html_template('do/set_stock_in_out');
1149 $main::lxdebug->leave_sub();
1152 sub stock_out_form {
1153 $main::lxdebug->enter_sub();
1155 my $form = $main::form;
1156 my %myconfig = %main::myconfig;
1157 my $locale = $main::locale;
1159 $form->{title} = $locale->text('Release From Stock');
1161 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
1163 my $units = AM->retrieve_units(\%myconfig, $form);
1164 my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
1166 my @contents = DO->get_item_availability('parts_id' => $form->{parts_id});
1168 my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
1170 if (!$form->{delivered}) {
1171 foreach my $row (@contents) {
1172 $row->{available_qty} = $form->format_amount_units('amount' => $row->{qty} * 1,
1173 'part_unit' => $part_info->{unit},
1174 'conv_units' => 'convertible_not_smaller',
1177 foreach my $sinfo (@{ $stock_info }) {
1178 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
1179 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1180 ($row->{chargenumber} ne $sinfo->{chargenumber}) ||
1181 ($row->{bestbefore} ne $sinfo->{bestbefore}));
1183 map { $row->{"stock_$_"} = $sinfo->{$_} } qw(qty unit error);
1188 get_basic_bin_wh_info($stock_info);
1190 foreach my $sinfo (@{ $stock_info }) {
1191 map { $sinfo->{"stock_$_"} = $sinfo->{$_} } qw(qty unit);
1196 print $form->parse_html_template('do/stock_out_form', { 'UNITS' => $units_data,
1197 'WHCONTENTS' => $form->{delivered} ? $stock_info : \@contents,
1198 'PART_INFO' => $part_info, });
1200 $main::lxdebug->leave_sub();
1204 $main::lxdebug->enter_sub();
1206 my $form = $main::form;
1207 my %myconfig = %main::myconfig;
1208 my $locale = $main::locale;
1210 my $stock_info = [];
1212 foreach my $i (1 .. $form->{rowcount}) {
1213 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1215 next if ($form->{"qty_$i"} <= 0);
1217 push @{ $stock_info }, {
1218 'warehouse_id' => $form->{"warehouse_id_$i"},
1219 'bin_id' => $form->{"bin_id_$i"},
1220 'chargenumber' => $form->{"chargenumber_$i"},
1221 'bestbefore' => $form->{"bestbefore_$i"},
1222 'qty' => $form->{"qty_$i"},
1223 'unit' => $form->{"unit_$i"},
1228 my @errors = DO->check_stock_availability('requests' => $stock_info,
1229 'parts_id' => $form->{parts_id});
1231 $form->{stock} = YAML::Dump($stock_info);
1234 $form->{ERRORS} = [];
1235 map { push @{ $form->{ERRORS} }, $locale->text('Error in row #1: The quantity you entered is bigger than the stocked quantity.', $_->{row}); } @errors;
1236 stock_in_out_form();
1239 _stock_in_out_set_qty_display($stock_info);
1242 print $form->parse_html_template('do/set_stock_in_out');
1245 $main::lxdebug->leave_sub();
1249 $main::lxdebug->enter_sub();
1251 my $form = $main::form;
1252 my %myconfig = %main::myconfig;
1253 my $locale = $main::locale;
1255 if ($form->{id} && DO->is_marked_as_delivered(id => $form->{id})) {
1256 $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred in.'), 'back_button' => 1);
1259 save(no_redirect => 1);
1261 my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_in_${_}"} } (1 .. $form->{rowcount});
1265 my $units = AM->retrieve_units(\%myconfig, $form);
1266 my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1269 $form->{ERRORS} = [];
1271 foreach my $i (1 .. $form->{rowcount}) {
1272 next unless ($form->{"id_$i"} && $form->{"stock_in_$i"});
1274 my $row_sum_base_qty = 0;
1275 my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1277 foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_in_$i"}) }) {
1278 $request->{parts_id} = $form->{"id_$i"};
1279 $row_sum_base_qty += $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1281 push @all_requests, $request;
1284 next if (0 == $row_sum_base_qty);
1286 my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1288 # if ($do_base_qty != $row_sum_base_qty) {
1289 # push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no stock at all or the full quantity of #2 #3.',
1290 # $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1294 if (@{ $form->{ERRORS} }) {
1295 push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1297 set_headings('edit');
1299 $main::lxdebug->leave_sub();
1305 DO->transfer_in_out('direction' => 'in',
1306 'requests' => \@all_requests);
1308 SL::DB::DeliveryOrder->new(id => $form->{id})->load->update_attributes(delivered => 1);
1310 $form->{callback} = 'do.pl?action=edit&type=purchase_delivery_order&id=' . $form->escape($form->{id});
1313 $main::lxdebug->leave_sub();
1317 $main::lxdebug->enter_sub();
1319 my $form = $main::form;
1320 my %myconfig = %main::myconfig;
1321 my $locale = $main::locale;
1323 if ($form->{id} && DO->is_marked_as_delivered(id => $form->{id})) {
1324 $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred out.'), 'back_button' => 1);
1327 save(no_redirect => 1);
1329 my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_out_${_}"} } (1 .. $form->{rowcount});
1333 my $units = AM->retrieve_units(\%myconfig, $form);
1334 my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1337 $form->{ERRORS} = [];
1339 foreach my $i (1 .. $form->{rowcount}) {
1340 next unless ($form->{"id_$i"} && $form->{"stock_out_$i"});
1342 my $row_sum_base_qty = 0;
1343 my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1345 foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_out_$i"}) }) {
1346 $request->{parts_id} = $form->{"id_$i"};
1347 $request->{base_qty} = $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1348 $request->{project_id} = $form->{"project_id_$i"} ? $form->{"project_id_$i"} : $form->{globalproject_id};
1350 my $map_key = join '--', ($form->{"id_$i"}, @{$request}{qw(warehouse_id bin_id chargenumber bestbefore)});
1352 $request_map{$map_key} ||= $request;
1353 $request_map{$map_key}->{sum_base_qty} ||= 0;
1354 $request_map{$map_key}->{sum_base_qty} += $request->{base_qty};
1355 $row_sum_base_qty += $request->{base_qty};
1357 push @all_requests, $request;
1360 next if (0 == $row_sum_base_qty);
1362 my $do_base_qty = $form->{"qty_$i"} * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1364 # if ($do_base_qty != $row_sum_base_qty) {
1365 # push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no transfer at all or the full quantity of #2 #3.',
1366 # $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1371 my @bin_ids = map { $_->{bin_id} } values %request_map;
1372 my %bin_info_map = WH->get_basic_bin_info('id' => \@bin_ids);
1373 my @contents = DO->get_item_availability('parts_id' => \@part_ids);
1375 foreach my $inv (@contents) {
1376 my $map_key = join '--', @{$inv}{qw(parts_id warehouse_id bin_id chargenumber bestbefore)};
1378 next unless ($request_map{$map_key});
1380 my $request = $request_map{$map_key};
1381 $request->{ok} = $request->{sum_base_qty} <= $inv->{qty};
1384 foreach my $request (values %request_map) {
1385 next if ($request->{ok});
1387 my $pinfo = $part_info_map{$request->{parts_id}};
1388 my $binfo = $bin_info_map{$request->{bin_id}};
1390 if ($::lx_office_conf{features}->{show_best_before}) {
1391 push @{ $form->{ERRORS} }, $locale->text("There is not enough available of '#1' at warehouse '#2', bin '#3', #4, #5, for the transfer of #6.",
1392 $pinfo->{description},
1393 $binfo->{warehouse_description},
1394 $binfo->{bin_description},
1395 $request->{chargenumber} ? $locale->text('chargenumber #1', $request->{chargenumber}) : $locale->text('no chargenumber'),
1396 $request->{bestbefore} ? $locale->text('bestbefore #1', $request->{bestbefore}) : $locale->text('no bestbefore'),
1397 $form->format_amount_units('amount' => $request->{sum_base_qty},
1398 'part_unit' => $pinfo->{unit},
1399 'conv_units' => 'convertible_not_smaller'));
1401 push @{ $form->{ERRORS} }, $locale->text("There is not enough available of '#1' at warehouse '#2', bin '#3', #4, for the transfer of #5.",
1402 $pinfo->{description},
1403 $binfo->{warehouse_description},
1404 $binfo->{bin_description},
1405 $request->{chargenumber} ? $locale->text('chargenumber #1', $request->{chargenumber}) : $locale->text('no chargenumber'),
1406 $form->format_amount_units('amount' => $request->{sum_base_qty},
1407 'part_unit' => $pinfo->{unit},
1408 'conv_units' => 'convertible_not_smaller'));
1413 if (@{ $form->{ERRORS} }) {
1414 push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1416 set_headings('edit');
1418 $main::lxdebug->leave_sub();
1423 DO->transfer_in_out('direction' => 'out',
1424 'requests' => \@all_requests);
1426 SL::DB::DeliveryOrder->new(id => $form->{id})->load->update_attributes(delivered => 1);
1428 $form->{callback} = 'do.pl?action=edit&type=sales_delivery_order&id=' . $form->escape($form->{id});
1431 $main::lxdebug->leave_sub();
1435 $main::lxdebug->enter_sub();
1437 my $form = $main::form;
1439 DO->close_orders('ids' => [ $form->{id} ]);
1441 $form->{closed} = 1;
1445 $main::lxdebug->leave_sub();
1450 call_sub($main::form->{yes_nextsub});
1454 call_sub($main::form->{no_nextsub});
1458 call_sub($main::form->{update_nextsub} || $main::form->{nextsub} || 'update_delivery_order');
1462 my $form = $main::form;
1463 my $locale = $main::locale;
1465 foreach my $action (qw(update ship_to print e_mail save transfer_out transfer_in mark_closed save_as_new invoice delete)) {
1466 if ($form->{"action_${action}"}) {
1472 $form->error($locale->text('No action defined.'));