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);
40 use SL::ReportGenerator;
43 require "bin/mozilla/arap.pl";
44 require "bin/mozilla/common.pl";
45 require "bin/mozilla/invoice_io.pl";
46 require "bin/mozilla/io.pl";
47 require "bin/mozilla/reportgenerator.pl";
58 $main::auth->assert($main::form->{type} . '_edit');
62 $main::lxdebug->enter_sub();
68 my $form = $main::form;
69 my $locale = $main::locale;
71 if ($form->{type} eq 'purchase_delivery_order') {
72 $form->{vc} = 'vendor';
73 $form->{title} = $action eq "edit" ? $locale->text('Edit Purchase Delivery Order') : $locale->text('Add Purchase Delivery Order');
75 $form->{vc} = 'customer';
76 $form->{title} = $action eq "edit" ? $locale->text('Edit Sales Delivery Order') : $locale->text('Add Sales Delivery Order');
79 $form->{heading} = $locale->text('Delivery Order');
81 $main::lxdebug->leave_sub();
85 $main::lxdebug->enter_sub();
89 my $form = $main::form;
93 $form->{callback} = build_std_url('action=add', 'type', 'vc') unless ($form->{callback});
99 $main::lxdebug->leave_sub();
103 $main::lxdebug->enter_sub();
107 my $form = $main::form;
109 # show history button
110 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
111 #/show hhistory button
113 $form->{simple_save} = 0;
115 set_headings("edit");
117 # editing without stuff to edit? try adding it first
118 if ($form->{rowcount} && !$form->{print_and_save}) {
119 # map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
123 undef $form->{rowcount};
125 $main::lxdebug->leave_sub();
128 } elsif (!$form->{id}) {
130 $main::lxdebug->leave_sub();
134 my ($language_id, $printer_id);
135 if ($form->{print_and_save}) {
136 $form->{action} = "dispatcher";
137 $form->{action_print} = "1";
138 $form->{resubmit} = 1;
139 $language_id = $form->{language_id};
140 $printer_id = $form->{printer_id};
143 set_headings("edit");
148 if ($form->{print_and_save}) {
149 $form->{language_id} = $language_id;
150 $form->{printer_id} = $printer_id;
155 $main::lxdebug->leave_sub();
159 $main::lxdebug->enter_sub();
163 my $form = $main::form;
164 my %myconfig = %main::myconfig;
166 # get customer/vendor
167 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
169 # retrieve order/quotation
170 $form->{webdav} = $main::webdav;
171 $form->{jsscript} = 1;
173 my $editing = $form->{id};
175 DO->retrieve('vc' => $form->{vc},
176 'ids' => $form->{id});
178 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
179 $form->{shipto} = 1 if $form->{id};
181 # get customer / vendor
182 if ($form->{vc} eq 'vendor') {
183 IR->get_vendor(\%myconfig, \%$form);
185 IS->get_customer(\%myconfig, \%$form);
188 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
189 $form->restore_vars(qw(taxincluded)) if $form->{id};
190 $form->restore_vars(qw(salesman_id)) if $editing;
192 if ($form->{"all_$form->{vc}"}) {
193 unless ($form->{"$form->{vc}_id"}) {
194 $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
198 ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
199 $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
201 $form->{employee} = "$form->{employee}--$form->{employee_id}";
203 $main::lxdebug->leave_sub();
207 $main::lxdebug->enter_sub();
211 my $form = $main::form;
212 my %myconfig = %main::myconfig;
214 $form->{formname} = $form->{type} unless $form->{formname};
217 foreach my $ref (@{ $form->{form_details} }) {
218 $form->{rowcount} = ++$i;
220 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
222 for my $i (1 .. $form->{rowcount}) {
224 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
226 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
228 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
230 my $decimalplaces = ($dec > 2) ? $dec : 2;
232 # copy reqdate from deliverydate for invoice -> order conversion
233 $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
235 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
237 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
238 $dec_qty = length $dec_qty;
239 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
242 $main::lxdebug->leave_sub();
246 $main::lxdebug->enter_sub();
250 my $form = $main::form;
251 my %myconfig = %main::myconfig;
253 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
254 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
256 # use JavaScript Calendar or not
257 $form->{jsscript} = 1;
260 my $jsscript = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
262 my @old_project_ids = ($form->{"globalproject_id"});
263 map({ push(@old_project_ids, $form->{"project_id_$_"})
264 if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
266 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
267 $form->get_lists("contacts" => "ALL_CONTACTS",
268 "shipto" => "ALL_SHIPTO",
270 "key" => "ALL_PROJECTS",
272 "old_id" => \@old_project_ids
274 "employees" => "ALL_EMPLOYEES",
275 "salesmen" => "ALL_SALESMEN",
277 "price_factors" => "ALL_PRICE_FACTORS",
278 "departments" => "ALL_DEPARTMENTS",
279 "business_types" => "ALL_BUSINESS_TYPES",
282 map { $_->{value} = "$_->{description}--$_->{id}" } @{ $form->{ALL_DEPARTMENTS} };
283 map { $_->{value} = "$_->{name}--$_->{id}" } @{ $form->{ALL_VC} };
285 $form->{SHOW_VC_DROP_DOWN} = $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
287 $form->{oldvcname} = $form->{"old$form->{vc}"};
288 $form->{oldvcname} =~ s/--.*//;
290 $form->{onload} = "";
291 if ($form->{resubmit}) {
292 if ($form->{format} eq "html") {
293 $form->{onload} = "window.open('about:blank','Beleg'); document.do.target = 'Beleg';";
295 # emulate click for resubmitting actions
296 $form->{onload} .= "document.do.${_}.click(); " for grep { /^action_/ } keys %$form;
297 $form->{onload} .= "document.do.submit();"
301 # Fix für Bug 1082 Erwartet wird: 'abteilungsNAME--abteilungsID'
302 $form->{department} .= '--' . $form->{department_id};
304 print $form->parse_html_template('do/form_header');
306 $main::lxdebug->leave_sub();
310 $main::lxdebug->enter_sub();
314 my $form = $main::form;
316 $form->{PRINT_OPTIONS} = print_options('inline' => 1);
318 print $form->parse_html_template('do/form_footer');
320 $main::lxdebug->leave_sub();
323 sub update_delivery_order {
324 $main::lxdebug->enter_sub();
328 my $form = $main::form;
329 my %myconfig = %main::myconfig;
331 set_headings($form->{"id"} ? "edit" : "add");
336 $payment_id = $form->{payment_id} if $form->{payment_id};
338 check_name($form->{vc});
340 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
343 my $i = $form->{rowcount};
345 if ( ($form->{"partnumber_$i"} eq "")
346 && ($form->{"description_$i"} eq "")
347 && ($form->{"partsgroup_$i"} eq "")) {
353 if ($form->{type} eq 'purchase_delivery_order') {
354 IR->retrieve_item(\%myconfig, $form);
356 IS->retrieve_item(\%myconfig, $form);
359 my $rows = scalar @{ $form->{item_list} };
362 $form->{"qty_$i"} = 1 unless $form->parse_amount(\%myconfig, $form->{"qty_$i"});
371 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
373 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
374 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
375 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
382 # ok, so this is a new part
383 # ask if it is a part or service item
385 if ( $form->{"partsgroup_$i"}
386 && ($form->{"partsnumber_$i"} eq "")
387 && ($form->{"description_$i"} eq "")) {
389 $form->{"discount_$i"} = "";
393 $form->{"id_$i"} = 0;
399 $main::lxdebug->leave_sub();
403 $main::lxdebug->enter_sub();
407 my $form = $main::form;
408 my %myconfig = %main::myconfig;
409 my $locale = $main::locale;
411 $form->{vc} = $form->{type} eq 'purchase_delivery_order' ? 'vendor' : 'customer';
413 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
415 "employees" => "ALL_EMPLOYEES",
416 "salesmen" => "ALL_SALESMEN",
417 "$form->{vc}s" => "ALL_VC");
419 $form->{SHOW_VC_DROP_DOWN} = $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
420 $form->{jsscript} = 1;
421 $form->{title} = $locale->text('Delivery Orders');
425 print $form->parse_html_template('do/search');
427 $main::lxdebug->leave_sub();
431 $main::lxdebug->enter_sub();
435 my $form = $main::form;
436 my %myconfig = %main::myconfig;
437 my $locale = $main::locale;
438 my $cgi = $main::cgi;
440 ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) = split(/--/, $form->{ $form->{vc} });
442 report_generator_set_default_sort('transdate', 1);
446 $form->{rowcount} = scalar @{ $form->{DO} };
453 shipvia globalprojectnumber
454 transaction_description
458 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
459 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
461 $form->{title} = $locale->text('Delivery Orders');
463 my $attachment_basename = $form->{vc} eq 'vendor' ? $locale->text('purchase_delivery_order_list') : $locale->text('sales_delivery_order_list');
465 my $report = SL::ReportGenerator->new(\%myconfig, $form);
467 my @hidden_variables = map { "l_${_}" } @columns;
468 push @hidden_variables, $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered donumber ordnumber
469 transaction_description transdatefrom transdateto type vc employee_id salesman_id project_id);
471 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
474 'ids' => { 'text' => '', },
475 'transdate' => { 'text' => $locale->text('Date'), },
476 'id' => { 'text' => $locale->text('ID'), },
477 'donumber' => { 'text' => $locale->text('Delivery Order'), },
478 'ordnumber' => { 'text' => $locale->text('Order'), },
479 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
480 'employee' => { 'text' => $locale->text('Employee'), },
481 'shipvia' => { 'text' => $locale->text('Ship via'), },
482 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
483 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
484 'open' => { 'text' => $locale->text('Open'), },
485 'delivered' => { 'text' => $locale->text('Delivered'), },
488 foreach my $name (qw(id transdate donumber ordnumber name employee shipvia transaction_description)) {
489 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
490 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
493 $form->{"l_type"} = "Y";
494 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
496 $column_defs{ids}->{visible} = 'HTML';
498 $report->set_columns(%column_defs);
499 $report->set_column_order(@columns);
501 $report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
503 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
506 if ($form->{customer}) {
507 push @options, $locale->text('Customer') . " : $form->{customer}";
509 if ($form->{vendor}) {
510 push @options, $locale->text('Vendor') . " : $form->{vendor}";
512 if ($form->{department}) {
513 my ($department) = split /--/, $form->{department};
514 push @options, $locale->text('Department') . " : $department";
516 if ($form->{donumber}) {
517 push @options, $locale->text('Delivery Order Number') . " : $form->{donumber}";
519 if ($form->{ordnumber}) {
520 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
522 if ($form->{transaction_description}) {
523 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
525 if ($form->{transdatefrom}) {
526 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
528 if ($form->{transdateto}) {
529 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
532 push @options, $locale->text('Open');
534 if ($form->{closed}) {
535 push @options, $locale->text('Closed');
537 if ($form->{delivered}) {
538 push @options, $locale->text('Delivered');
540 if ($form->{notdelivered}) {
541 push @options, $locale->text('Not delivered');
544 $report->set_options('top_info_text' => join("\n", @options),
545 'raw_top_info_text' => $form->parse_html_template('do/orders_top'),
546 'raw_bottom_info_text' => $form->parse_html_template('do/orders_bottom'),
547 'output_format' => 'HTML',
548 'title' => $form->{title},
549 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
551 $report->set_options_from_form();
553 # add sort and escape callback, this one we use for the add sub
554 $form->{callback} = $href .= "&sort=$form->{sort}";
556 # escape callback for href
557 my $callback = $form->escape($href);
559 my $edit_url = build_std_url('action=edit', 'type', 'vc');
560 my $edit_order_url = build_std_url('script=oe.pl', 'type=' . ($form->{type} eq 'sales_delivery_order' ? 'sales_order' : 'purchase_order'), 'action=edit');
564 foreach my $dord (@{ $form->{DO} }) {
565 $dord->{open} = $dord->{closed} ? $locale->text('No') : $locale->text('Yes');
566 $dord->{delivered} = $dord->{delivered} ? $locale->text('Yes') : $locale->text('No');
568 my $row = { map { $_ => { 'data' => $dord->{$_} } } @columns };
571 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $dord->{id})
572 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
573 'valign' => 'center',
577 $row->{donumber}->{link} = $edit_url . "&id=" . E($dord->{id}) . "&callback=${callback}";
578 $row->{ordnumber}->{link} = $edit_order_url . "&id=" . E($dord->{oe_id}) . "&callback=${callback}";
580 $report->add_data($row);
585 $report->generate_with_headers();
587 $main::lxdebug->leave_sub();
591 $main::lxdebug->enter_sub();
595 my $form = $main::form;
596 my %myconfig = %main::myconfig;
597 my $locale = $main::locale;
599 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
601 $form->isblank("transdate", $locale->text('Delivery Order Date missing!'));
603 $form->{donumber} =~ s/^\s*//g;
604 $form->{donumber} =~ s/\s*$//g;
606 my $msg = ucfirst $form->{vc};
607 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
609 # $locale->text('Customer missing!');
610 # $locale->text('Vendor missing!');
612 remove_emptied_rows();
615 # if the name changed get new values
616 if (check_name($form->{vc})) {
621 $form->{id} = 0 if $form->{saveasnew};
622 # best case fix für bug 1079. Einkaufsrabatt wird nicht richtig
623 # aus Lieferantenauftrag -> Lieferschein -> Rechnung übernommen
624 # Tritt nur auf, wenn man direkt über Lieferschein -> speichern ->
625 # Workflow Rechnung geht (beim Aufruf über edit() i.O.)
626 # Gut. DO-save() speichert den Discount im DB-Format 0.12 für
627 # 12%, die Konvertierung wird leider in $form gemacht und daher
628 # wird die Maske mit dem falschen Rabatt wieder aufgebaut.
629 # Wie immer: backup_vars verwenden um nichts anderes kaputt zu
630 # machen. jan 03.03.2010
631 for my $i (1 .. $form->{rowcount}) {
632 $form->{"backup_discount_$i"} = $form->{"discount_$i"};
635 for my $i (1 .. $form->{rowcount}) {
636 $form->{"discount_$i"} = $form->{"backup_discount_$i"};
637 delete $form->{"backup_discount_$i"};
640 if(!exists $form->{addition}) {
641 $form->{snumbers} = qq|donumber_| . $form->{donumber};
642 $form->{addition} = "SAVED";
643 $form->save_history($form->dbconnect(\%myconfig));
645 # /saving the history
647 $form->{simple_save} = 1;
648 if(!$form->{print_and_save}) {
649 set_headings("edit");
653 $main::lxdebug->leave_sub();
657 $main::lxdebug->enter_sub();
661 my $form = $main::form;
662 my $locale = $main::locale;
664 map { delete $form->{$_} } qw(action header login password);
665 my @variables = map { { 'key' => $_, 'value' => $form->{$_} } } grep { '' eq ref $form->{$_} } keys %{ $form };
667 $form->{title} = $locale->text('Delete delivery order');
670 print $form->parse_html_template('do/delete', { 'VARIABLES' => \@variables });
672 $main::lxdebug->leave_sub();
675 sub delete_delivery_order {
676 $main::lxdebug->enter_sub();
680 my $form = $main::form;
681 my %myconfig = %main::myconfig;
682 my $locale = $main::locale;
686 if(!exists $form->{addition}) {
687 $form->{snumbers} = qq|donumber_| . $form->{donumber};
688 $form->{addition} = "DELETED";
689 $form->save_history($form->dbconnect(\%myconfig));
691 # /saving the history
693 $form->info($locale->text('Delivery Order deleted!'));
697 $form->error($locale->text('Cannot delete delivery order!'));
699 $main::lxdebug->leave_sub();
703 $main::lxdebug->enter_sub();
705 my $form = $main::form;
706 my %myconfig = %main::myconfig;
707 my $locale = $main::locale;
710 $main::auth->assert($form->{type} eq 'purchase_delivery_order' ? 'vendor_invoice_edit' : 'invoice_edit');
712 $form->{convert_from_do_ids} = $form->{id};
713 $form->{deliverydate} = $form->{transdate};
714 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
715 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
716 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
720 delete @{$form}{qw(id closed delivered)};
722 my ($script, $buysell);
723 if ($form->{type} eq 'purchase_delivery_order') {
724 $form->{title} = $locale->text('Add Vendor Invoice');
725 $form->{script} = 'ir.pl';
730 $form->{title} = $locale->text('Add Sales Invoice');
731 $form->{script} = 'is.pl';
736 for my $i (1 .. $form->{rowcount}) {
737 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"} } qw(ship qty sellprice listprice basefactor);
740 $form->{type} = "invoice";
743 $main::locale = new Locale "$myconfig{countrycode}", "$script";
744 $locale = $main::locale;
746 require "bin/mozilla/$form->{script}";
748 my $currency = $form->{currency};
751 $form->{currency} = $currency;
752 $form->{exchangerate} = "";
753 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell);
754 $form->{exchangerate} = $form->{forex} if ($form->{forex});
759 for my $i (1 .. $form->{rowcount}) {
760 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
762 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
764 my $decimalplaces = ($dec > 2) ? $dec : 2;
766 # copy delivery date from reqdate for order -> invoice conversion
767 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
768 unless $form->{"deliverydate_$i"};
770 $form->{"sellprice_$i"} =
771 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
774 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
775 $dec_qty = length $dec_qty;
777 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
783 $main::lxdebug->leave_sub();
787 $main::lxdebug->enter_sub();
789 my $form = $main::form;
790 my %myconfig = %main::myconfig;
791 my $locale = $main::locale;
794 $main::auth->assert($form->{type} eq 'sales_delivery_order' ? 'invoice_edit' : 'vendor_invoice_edit');
796 my @do_ids = map { $form->{"trans_id_$_"} } grep { $form->{"multi_id_$_"} } (1..$form->{rowcount});
798 if (!scalar @do_ids) {
799 $form->show_generic_error($locale->text('You have not selected any delivery order.'), 'back_button' => 1);
802 map { delete $form->{$_} } grep { m/^(?:trans|multi)_id_\d+/ } keys %{ $form };
804 if (!DO->retrieve('vc' => $form->{vc}, 'ids' => \@do_ids)) {
805 $form->show_generic_error($form->{vc} eq 'customer' ?
806 $locale->text('You cannot create an invoice for delivery orders for different customers.') :
807 $locale->text('You cannot create an invoice for delivery orders from different vendors.'),
811 my $source_type = $form->{type};
812 $form->{convert_from_do_ids} = join ' ', @do_ids;
813 # bei der auswahl von mehreren Lieferscheinen fuer eine Rechnung, die einfach in donumber_array
814 # zwischenspeichern (DO.pm) und als ' '-separierte Liste wieder zurueckschreiben
815 # Hinweis: delete gibt den wert zurueck und loescht danach das element (nett und einfach)
816 # $shell: perldoc perlunc; /delete EXPR
817 $form->{donumber} = delete $form->{donumber_array};
818 $form->{deliverydate} = $form->{transdate};
819 $form->{transdate} = $form->current_date(\%myconfig);
820 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
821 $form->{type} = "invoice";
823 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
825 my ($script, $buysell);
826 if ($source_type eq 'purchase_delivery_order') {
827 $form->{title} = $locale->text('Add Vendor Invoice');
828 $form->{script} = 'ir.pl';
833 $form->{title} = $locale->text('Add Sales Invoice');
834 $form->{script} = 'is.pl';
839 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
841 $form->{rowcount} = 0;
842 foreach my $ref (@{ $form->{form_details} }) {
844 $ref->{reqdate} ||= $ref->{dord_transdate}; # copy transdates into each invoice row
845 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{ $ref };
846 map { $form->{"${_}_$form->{rowcount}"} = $form->format_amount(\%myconfig, $ref->{$_}) } qw(qty sellprice discount lastcost);
847 $form->{"discount_$form->{rowcount}"} = $form->{"discount_$form->{rowcount}"} * 100; #s.a. Bug 1151
848 # Anm.: Eine Änderung des discounts in der SL/DO.pm->retrieve (select (doi.discount * 100) as discount) ergibt in psql einen
849 # Wert von 10.0000001490116. Ferner ist der Rabatt in der Rechnung dann bei 1.0 (?). Deswegen lasse ich das hier. jb 10.10.09
851 delete $form->{form_details};
853 $locale = new Locale "$myconfig{countrycode}", "$script";
855 require "bin/mozilla/$form->{script}";
861 $main::lxdebug->leave_sub();
865 $main::lxdebug->enter_sub();
869 my $form = $main::form;
871 $form->{saveasnew} = 1;
873 $form->{delivered} = 0;
874 map { delete $form->{$_} } qw(printed emailed queued);
875 delete @{ $form }{ grep { m/^stock_(?:in|out)_\d+/ } keys %{ $form } };
877 # Let Lx-Office assign a new order number if the user hasn't changed the
878 # previous one. If it has been changed manually then use it as-is.
879 $form->{donumber} =~ s/^\s*//g;
880 $form->{donumber} =~ s/\s*$//g;
881 if ($form->{saved_donumber} && ($form->{saved_donumber} eq $form->{donumber})) {
882 delete($form->{donumber});
887 $main::lxdebug->leave_sub();
891 $main::lxdebug->enter_sub();
895 my $form = $main::form;
897 $form->{print_and_save} = 1;
901 my $saved_form = save_form();
905 restore_form($saved_form, 0, qw(id ordnumber quonumber));
909 $main::lxdebug->leave_sub();
912 sub calculate_stock_in_out {
913 $main::lxdebug->enter_sub();
915 my $form = $main::form;
919 if (!$form->{"id_${i}"}) {
920 $main::lxdebug->leave_sub();
924 my $all_units = AM->retrieve_all_units();
926 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
927 my $sinfo = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_${i}"});
929 my $sum = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $sinfo });
931 my $content = $form->format_amount_units('amount' => $sum * 1,
932 'part_unit' => $form->{"partunit_$i"},
933 'amount_unit' => $all_units->{$form->{"partunit_$i"}}->{base_unit},
934 'conv_units' => 'convertible_not_smaller',
936 $content .= qq| <input type="button" onclick="open_stock_in_out_window('${in_out}', $i);" value="?">|;
938 $main::lxdebug->leave_sub();
943 sub get_basic_bin_wh_info {
944 $main::lxdebug->enter_sub();
946 my $stock_info = shift;
948 my $form = $main::form;
950 foreach my $sinfo (@{ $stock_info }) {
951 next unless ($sinfo->{bin_id});
953 my $bin_info = WH->get_basic_bin_info('id' => $sinfo->{bin_id});
954 map { $sinfo->{"${_}_description"} = $sinfo->{"${_}description"} = $bin_info->{"${_}_description"} } qw(bin warehouse);
957 $main::lxdebug->leave_sub();
960 sub stock_in_out_form {
961 $main::lxdebug->enter_sub();
963 my $form = $main::form;
965 if ($form->{in_out} eq 'out') {
971 $main::lxdebug->leave_sub();
974 sub redo_stock_info {
975 $main::lxdebug->enter_sub();
979 my $form = $main::form;
981 my @non_empty = grep { $_->{qty} } @{ $params{stock_info} };
983 if ($params{add_empty_row}) {
985 'warehouse_id' => scalar(@non_empty) ? $non_empty[-1]->{warehouse_id} : undef,
986 'bin_id' => scalar(@non_empty) ? $non_empty[-1]->{bin_id} : undef,
990 @{ $params{stock_info} } = @non_empty;
992 $main::lxdebug->leave_sub();
995 sub update_stock_in {
996 $main::lxdebug->enter_sub();
998 my $form = $main::form;
999 my %myconfig = %main::myconfig;
1001 my $stock_info = [];
1003 foreach my $i (1..$form->{rowcount}) {
1004 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1005 push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber qty unit) };
1008 display_stock_in_form($stock_info);
1010 $main::lxdebug->leave_sub();
1014 $main::lxdebug->enter_sub();
1016 my $form = $main::form;
1018 my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
1020 display_stock_in_form($stock_info);
1022 $main::lxdebug->leave_sub();
1025 sub display_stock_in_form {
1026 $main::lxdebug->enter_sub();
1028 my $stock_info = shift;
1030 my $form = $main::form;
1031 my %myconfig = %main::myconfig;
1032 my $locale = $main::locale;
1034 $form->{title} = $locale->text('Stock');
1036 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
1038 my $units = AM->retrieve_units(\%myconfig, $form);
1039 my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
1041 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
1042 'bins' => 'BINS' });
1044 redo_stock_info('stock_info' => $stock_info, 'add_empty_row' => !$form->{delivered});
1046 get_basic_bin_wh_info($stock_info);
1049 print $form->parse_html_template('do/stock_in_form', { 'UNITS' => $units_data,
1050 'STOCK_INFO' => $stock_info,
1051 'PART_INFO' => $part_info, });
1053 $main::lxdebug->leave_sub();
1057 $main::lxdebug->enter_sub();
1059 my $form = $main::form;
1060 my %myconfig = %main::myconfig;
1062 my $stock_info = [];
1064 foreach my $i (1..$form->{rowcount}) {
1065 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1067 next if ($form->{"qty_$i"} <= 0);
1069 push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber qty unit) };
1072 $form->{stock} = YAML::Dump($stock_info);
1075 print $form->parse_html_template('do/set_stock_in_out');
1077 $main::lxdebug->leave_sub();
1080 sub stock_out_form {
1081 $main::lxdebug->enter_sub();
1083 my $form = $main::form;
1084 my %myconfig = %main::myconfig;
1085 my $locale = $main::locale;
1087 $form->{title} = $locale->text('Release From Stock');
1089 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
1091 my $units = AM->retrieve_units(\%myconfig, $form);
1092 my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
1094 my @contents = DO->get_item_availability('parts_id' => $form->{parts_id});
1096 my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
1098 if (!$form->{delivered}) {
1099 foreach my $row (@contents) {
1100 $row->{available_qty} = $form->format_amount_units('amount' => $row->{qty} * 1,
1101 'part_unit' => $part_info->{unit},
1102 'conv_units' => 'convertible_not_smaller',
1105 foreach my $sinfo (@{ $stock_info }) {
1106 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
1107 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1108 ($row->{chargenumber} ne $sinfo->{chargenumber}));
1110 map { $row->{"stock_$_"} = $sinfo->{$_} } qw(qty unit error);
1115 get_basic_bin_wh_info($stock_info);
1117 foreach my $sinfo (@{ $stock_info }) {
1118 map { $sinfo->{"stock_$_"} = $sinfo->{$_} } qw(qty unit);
1123 print $form->parse_html_template('do/stock_out_form', { 'UNITS' => $units_data,
1124 'WHCONTENTS' => $form->{delivered} ? $stock_info : \@contents,
1125 'PART_INFO' => $part_info, });
1127 $main::lxdebug->leave_sub();
1131 $main::lxdebug->enter_sub();
1133 my $form = $main::form;
1134 my %myconfig = %main::myconfig;
1135 my $locale = $main::locale;
1137 my $stock_info = [];
1139 foreach my $i (1 .. $form->{rowcount}) {
1140 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1142 next if ($form->{"qty_$i"} <= 0);
1144 push @{ $stock_info }, {
1145 'warehouse_id' => $form->{"warehouse_id_$i"},
1146 'bin_id' => $form->{"bin_id_$i"},
1147 'chargenumber' => $form->{"chargenumber_$i"},
1148 'qty' => $form->{"qty_$i"},
1149 'unit' => $form->{"unit_$i"},
1154 my @errors = DO->check_stock_availability('requests' => $stock_info,
1155 'parts_id' => $form->{parts_id});
1157 $form->{stock} = YAML::Dump($stock_info);
1160 $form->{ERRORS} = [];
1161 map { push @{ $form->{ERRORS} }, $locale->text('Error in row #1: The quantity you entered is bigger than the stocked quantity.', $_->{row}); } @errors;
1162 stock_in_out_form();
1166 print $form->parse_html_template('do/set_stock_in_out');
1169 $main::lxdebug->leave_sub();
1173 $main::lxdebug->enter_sub();
1175 my $form = $main::form;
1176 my %myconfig = %main::myconfig;
1177 my $locale = $main::locale;
1179 if (DO->is_marked_as_delivered('id' => $form->{id})) {
1180 $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred in.'), 'back_button' => 1);
1183 my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_in_${_}"} } (1 .. $form->{rowcount});
1187 my $units = AM->retrieve_units(\%myconfig, $form);
1188 my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1191 $form->{ERRORS} = [];
1193 foreach my $i (1 .. $form->{rowcount}) {
1194 next unless ($form->{"id_$i"} && $form->{"stock_in_$i"});
1196 my $row_sum_base_qty = 0;
1197 my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1199 foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_in_$i"}) }) {
1200 $request->{parts_id} = $form->{"id_$i"};
1201 $row_sum_base_qty += $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1203 push @all_requests, $request;
1206 next if (0 == $row_sum_base_qty);
1208 my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1210 # if ($do_base_qty != $row_sum_base_qty) {
1211 # push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no stock at all or the full quantity of #2 #3.',
1212 # $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1216 if (@{ $form->{ERRORS} }) {
1217 push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1220 $main::lxdebug->leave_sub();
1226 DO->transfer_in_out('direction' => 'in',
1227 'requests' => \@all_requests);
1229 $form->{delivered} = 1;
1233 $main::lxdebug->leave_sub();
1237 $main::lxdebug->enter_sub();
1239 my $form = $main::form;
1240 my %myconfig = %main::myconfig;
1241 my $locale = $main::locale;
1243 if (DO->is_marked_as_delivered('id' => $form->{id})) {
1244 $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred out.'), 'back_button' => 1);
1247 my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_out_${_}"} } (1 .. $form->{rowcount});
1251 my $units = AM->retrieve_units(\%myconfig, $form);
1252 my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1255 $form->{ERRORS} = [];
1257 foreach my $i (1 .. $form->{rowcount}) {
1258 next unless ($form->{"id_$i"} && $form->{"stock_out_$i"});
1260 my $row_sum_base_qty = 0;
1261 my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1263 foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_out_$i"}) }) {
1264 $request->{parts_id} = $form->{"id_$i"};
1265 $request->{base_qty} = $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1267 my $map_key = join '--', ($form->{"id_$i"}, @{$request}{qw(warehouse_id bin_id chargenumber)});
1269 $request_map{$map_key} ||= $request;
1270 $request_map{$map_key}->{sum_base_qty} ||= 0;
1271 $request_map{$map_key}->{sum_base_qty} += $request->{base_qty};
1272 $row_sum_base_qty += $request->{base_qty};
1274 push @all_requests, $request;
1277 next if (0 == $row_sum_base_qty);
1279 my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1281 # if ($do_base_qty != $row_sum_base_qty) {
1282 # push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no transfer at all or the full quantity of #2 #3.',
1283 # $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1288 my @bin_ids = map { $_->{bin_id} } values %request_map;
1289 my %bin_info_map = WH->get_basic_bin_info('id' => \@bin_ids);
1290 my @contents = DO->get_item_availability('parts_id' => \@part_ids);
1292 foreach my $inv (@contents) {
1293 my $map_key = join '--', @{$inv}{qw(parts_id warehouse_id bin_id chargenumber)};
1295 next unless ($request_map{$map_key});
1297 my $request = $request_map{$map_key};
1298 $request->{ok} = $request->{sum_base_qty} <= $inv->{qty};
1301 foreach my $request (values %request_map) {
1302 next if ($request->{ok});
1304 my $pinfo = $part_info_map{$request->{parts_id}};
1305 my $binfo = $bin_info_map{$request->{bin_id}};
1307 push @{ $form->{ERRORS} }, $locale->text("There is not enough available of '#1' at warehouse '#2', bin '#3', #4, for the transfer of #5.",
1308 $pinfo->{description}, $binfo->{warehouse_description}, $binfo->{bin_description},
1309 $request->{chargenumber} ? $locale->text('chargenumber #1', $request->{chargenumber}) : $locale->text('no chargenumber'),
1310 $form->format_amount_units('amount' => $request->{sum_base_qty},
1311 'part_unit' => $pinfo->{unit},
1312 'conv_units' => 'convertible_not_smaller'));
1316 if (@{ $form->{ERRORS} }) {
1317 push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1320 $main::lxdebug->leave_sub();
1326 DO->transfer_in_out('direction' => 'out',
1327 'requests' => \@all_requests);
1329 $form->{delivered} = 1;
1333 $main::lxdebug->leave_sub();
1337 $main::lxdebug->enter_sub();
1339 my $form = $main::form;
1341 DO->close_orders('ids' => [ $form->{id} ]);
1343 $form->{closed} = 1;
1347 $main::lxdebug->leave_sub();
1352 call_sub($main::form->{yes_nextsub});
1356 call_sub($main::form->{no_nextsub});
1360 call_sub($main::form->{update_nextsub} || $main::form->{nextsub} || 'update_delivery_order');
1364 my $form = $main::form;
1365 my $locale = $main::locale;
1367 foreach my $action (qw(update ship_to print e_mail save transfer_out transfer_in mark_closed save_as_new invoice delete)) {
1368 if ($form->{"action_${action}"}) {
1374 $form->error($locale->text('No action defined.'));