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";
54 $auth->assert($form->{type} . '_edit');
58 $lxdebug->enter_sub();
64 if ($form->{type} eq 'purchase_delivery_order') {
65 $form->{vc} = 'vendor';
66 $form->{title} = $action eq "edit" ? $locale->text('Edit Purchase Delivery Order') : $locale->text('Add Purchase Delivery Order');
68 $form->{vc} = 'customer';
69 $form->{title} = $action eq "edit" ? $locale->text('Edit Sales Delivery Order') : $locale->text('Add Sales Delivery Order');
72 $form->{heading} = $locale->text('Delivery Order');
74 $lxdebug->leave_sub();
78 $lxdebug->enter_sub();
84 $form->{callback} = build_std_url('action=add', 'type', 'vc') unless ($form->{callback});
90 $lxdebug->leave_sub();
94 $lxdebug->enter_sub();
99 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
100 #/show hhistory button
102 $form->{simple_save} = 0;
104 set_headings("edit");
106 # editing without stuff to edit? try adding it first
107 if ($form->{rowcount} && !$form->{print_and_save}) {
108 # map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
112 undef $form->{rowcount};
114 $lxdebug->leave_sub();
117 } elsif (!$form->{id}) {
119 $lxdebug->leave_sub();
123 if ($form->{print_and_save}) {
124 $form->{action} = "print";
125 $form->{resubmit} = 1;
126 $language_id = $form->{language_id};
127 $printer_id = $form->{printer_id};
130 set_headings("edit");
135 if ($form->{print_and_save}) {
136 $form->{language_id} = $language_id;
137 $form->{printer_id} = $printer_id;
142 $lxdebug->leave_sub();
146 $lxdebug->enter_sub();
150 # get customer/vendor
151 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
153 # retrieve order/quotation
154 $form->{webdav} = $webdav;
155 $form->{jsscript} = 1;
157 my $editing = $form->{id};
159 DO->retrieve('vc' => $form->{vc},
160 'ids' => $form->{id});
162 $payment_id = $form->{payment_id} if ($form->{payment_id});
163 $language_id = $form->{language_id} if ($form->{language_id});
164 $taxzone_id = $form->{taxzone_id} if ($form->{taxzone_id});
165 $salesman_id = $form->{salesman_id} if ($editing);
168 $taxincluded = $form->{taxincluded};
169 $form->{shipto} = 1 if $form->{id};
171 if ($form->{"all_$form->{vc}"}) {
172 unless ($form->{"$form->{vc}_id"}) {
173 $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
177 $cp_id = $form->{cp_id};
178 $intnotes = $form->{intnotes};
180 $form->{cp_id} = $cp_id;
182 $form->{payment_id} = $payment_id if ($payment_id);
183 $form->{language_id} = $language_id if ($language_id);
184 $form->{taxzone_id} = $taxzone_id if ($taxzone_id);
185 $form->{intnotes} = $intnotes if ($intnotes);
187 ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
188 $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
190 $form->{taxincluded} = $taxincluded if ($form->{id});
192 $form->{employee} = "$form->{employee}--$form->{employee_id}";
194 $form->{salesman_id} = $salesman_id if ($editing);
196 $lxdebug->leave_sub();
200 $lxdebug->enter_sub();
204 $form->{formname} = $form->{type} unless $form->{formname};
207 foreach $ref (@{ $form->{form_details} }) {
208 $form->{rowcount} = ++$i;
210 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
212 for my $i (1 .. $form->{rowcount}) {
214 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
216 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
218 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
220 $decimalplaces = ($dec > 2) ? $dec : 2;
222 # copy reqdate from deliverydate for invoice -> order conversion
223 $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
225 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
227 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
228 $dec_qty = length $dec_qty;
229 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
231 map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
234 $lxdebug->leave_sub();
238 $lxdebug->enter_sub();
242 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
243 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
245 # use JavaScript Calendar or not
246 $form->{jsscript} = 1;
249 $jsscript = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
251 my @old_project_ids = ($form->{"globalproject_id"});
252 map({ push(@old_project_ids, $form->{"project_id_$_"})
253 if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
255 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
256 $form->get_lists("contacts" => "ALL_CONTACTS",
257 "shipto" => "ALL_SHIPTO",
259 "key" => "ALL_PROJECTS",
261 "old_id" => \@old_project_ids
263 "employees" => "ALL_EMPLOYEES",
264 "salesmen" => "ALL_SALESMEN",
266 "price_factors" => "ALL_PRICE_FACTORS",
267 "departments" => "ALL_DEPARTMENTS",
268 "business_types" => "ALL_BUSINESS_TYPES",
271 map { $_->{value} = "$_->{description}--$_->{id}" } @{ $form->{ALL_DEPARTMENTS} };
272 map { $_->{value} = "$_->{name}--$_->{id}" } @{ $form->{ALL_VC} };
274 $form->{SHOW_VC_DROP_DOWN} = $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
276 $form->{oldvcname} = $form->{"old$form->{vc}"};
277 $form->{oldvcname} =~ s/--.*//;
279 $form->{onload} = "";
280 if ($form->{resubmit}) {
281 if ($form->{format} eq "html") {
282 $form->{onload} = "window.open('about:blank','Beleg'); document.do.target = 'Beleg';";
284 $form->{onload} .= "document.do.submit();"
288 print $form->parse_html_template('do/form_header');
290 $lxdebug->leave_sub();
294 $lxdebug->enter_sub();
298 $form->{PRINT_OPTIONS} = print_options('inline' => 1);
300 print $form->parse_html_template('do/form_footer');
302 $lxdebug->leave_sub();
305 sub update_delivery_order {
306 $lxdebug->enter_sub();
310 set_headings($form->{"id"} ? "edit" : "add");
314 $payment_id = $form->{payment_id} if $form->{payment_id};
316 check_name($form->{vc});
318 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
321 $i = $form->{rowcount};
323 if ( ($form->{"partnumber_$i"} eq "")
324 && ($form->{"description_$i"} eq "")
325 && ($form->{"partsgroup_$i"} eq "")) {
331 if ($form->{type} eq 'purchase_delivery_order') {
332 IR->retrieve_item(\%myconfig, $form);
334 IS->retrieve_item(\%myconfig, $form);
337 my $rows = scalar @{ $form->{item_list} };
340 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
349 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
350 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
352 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
353 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
354 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
361 # ok, so this is a new part
362 # ask if it is a part or service item
364 if ( $form->{"partsgroup_$i"}
365 && ($form->{"partsnumber_$i"} eq "")
366 && ($form->{"description_$i"} eq "")) {
368 $form->{"discount_$i"} = "";
372 $form->{"id_$i"} = 0;
378 $lxdebug->leave_sub();
382 $lxdebug->enter_sub();
386 $form->{vc} = $form->{type} eq 'purchase_delivery_order' ? 'vendor' : 'customer';
388 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
390 "employees" => "ALL_EMPLOYEES",
391 "salesmen" => "ALL_SALESMEN",
392 "$form->{vc}s" => "ALL_VC");
394 $form->{SHOW_VC_DROP_DOWN} = $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
395 $form->{jsscript} = 1;
396 $form->{title} = $locale->text('Delivery Orders');
400 print $form->parse_html_template('do/search');
402 $lxdebug->leave_sub();
406 $lxdebug->enter_sub();
410 ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
412 $form->{sort} ||= 'transdate';
416 $form->{rowcount} = scalar @{ $form->{DO} };
423 shipvia globalprojectnumber
424 transaction_description
428 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
429 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
431 $form->{title} = $locale->text('Delivery Orders');
433 my $attachment_basename = $form->{vc} eq 'vendor' ? $locale->text('purchase_delivery_order_list') : $locale->text('sales_delivery_order_list');
435 my $report = SL::ReportGenerator->new(\%myconfig, $form);
437 my @hidden_variables = map { "l_${_}" } @columns;
438 push @hidden_variables, $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered donumber ordnumber
439 transaction_description transdatefrom transdateto type vc employee_id salesman_id);
441 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
444 'ids' => { 'text' => '', },
445 'transdate' => { 'text' => $locale->text('Date'), },
446 'id' => { 'text' => $locale->text('ID'), },
447 'donumber' => { 'text' => $locale->text('Delivery Order'), },
448 'ordnumber' => { 'text' => $locale->text('Order'), },
449 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
450 'employee' => { 'text' => $locale->text('Salesperson'), },
451 'shipvia' => { 'text' => $locale->text('Ship via'), },
452 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
453 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
454 'open' => { 'text' => $locale->text('Open'), },
455 'delivered' => { 'text' => $locale->text('Delivered'), },
458 foreach my $name (qw(id transdate donumber ordnumber name employee shipvia)) {
459 $column_defs{$name}->{link} = $href . "&sort=$name";
462 $form->{"l_type"} = "Y";
463 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
465 $column_defs{ids}->{visible} = 'HTML';
467 $report->set_columns(%column_defs);
468 $report->set_column_order(@columns);
470 $report->set_export_options('orders', @hidden_variables);
472 $report->set_sort_indicator($form->{sort}, 1);
475 if ($form->{customer}) {
476 push @options, $locale->text('Customer') . " : $form->{customer}";
478 if ($form->{vendor}) {
479 push @options, $locale->text('Vendor') . " : $form->{vendor}";
481 if ($form->{department}) {
482 ($department) = split /--/, $form->{department};
483 push @options, $locale->text('Department') . " : $department";
485 if ($form->{donumber}) {
486 push @options, $locale->text('Delivery Order Number') . " : $form->{donumber}";
488 if ($form->{ordnumber}) {
489 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
491 if ($form->{transaction_description}) {
492 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
494 if ($form->{transdatefrom}) {
495 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
497 if ($form->{transdateto}) {
498 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
501 push @options, $locale->text('Open');
503 if ($form->{closed}) {
504 push @options, $locale->text('Closed');
506 if ($form->{delivered}) {
507 push @options, $locale->text('Delivered');
509 if ($form->{notdelivered}) {
510 push @options, $locale->text('Not delivered');
513 $report->set_options('top_info_text' => join("\n", @options),
514 'raw_top_info_text' => $form->parse_html_template('do/orders_top'),
515 'raw_bottom_info_text' => $form->parse_html_template('do/orders_bottom'),
516 'output_format' => 'HTML',
517 'title' => $form->{title},
518 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
520 $report->set_options_from_form();
522 # add sort and escape callback, this one we use for the add sub
523 $form->{callback} = $href .= "&sort=$form->{sort}";
525 # escape callback for href
526 $callback = $form->escape($href);
528 my $edit_url = build_std_url('action=edit', 'type', 'vc');
529 my $edit_order_url = build_std_url('script=oe.pl', 'type=' . ($form->{type} eq 'sales_delivery_order' ? 'sales_order' : 'purchase_order'), 'action=edit');
533 foreach $dord (@{ $form->{DO} }) {
534 $dord->{open} = $dord->{closed} ? $locale->text('No') : $locale->text('Yes');
535 $dord->{delivered} = $dord->{delivered} ? $locale->text('Yes') : $locale->text('No');
537 my $row = { map { $_ => { 'data' => $dord->{$_} } } @columns };
540 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $dord->{id})
541 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
542 'valign' => 'center',
546 $row->{donumber}->{link} = $edit_url . "&id=" . E($dord->{id}) . "&callback=${callback}";
547 $row->{ordnumber}->{link} = $edit_order_url . "&id=" . E($dord->{oe_id}) . "&callback=${callback}";
549 $report->add_data($row);
554 $report->generate_with_headers();
556 $lxdebug->leave_sub();
560 $lxdebug->enter_sub();
564 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
566 $form->isblank("transdate", $locale->text('Delivery Order Date missing!'));
568 $form->{donumber} =~ s/^\s*//g;
569 $form->{donumber} =~ s/\s*$//g;
571 $msg = ucfirst $form->{vc};
572 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
574 # $locale->text('Customer missing!');
575 # $locale->text('Vendor missing!');
579 # if the name changed get new values
580 if (check_name($form->{vc})) {
585 $form->{id} = 0 if $form->{saveasnew};
590 if(!exists $form->{addition}) {
591 $form->{snumbers} = qq|donumber_| . $form->{donumber};
592 $form->{addition} = "SAVED";
593 $form->save_history($form->dbconnect(\%myconfig));
595 # /saving the history
597 $form->{simple_save} = 1;
598 if(!$form->{print_and_save}) {
599 set_headings("edit");
603 $lxdebug->leave_sub();
607 $lxdebug->enter_sub();
611 map { delete $form->{$_} } qw(action header login password);
612 my @variables = map { { 'key' => $_, 'value' => $form->{$_} } } grep { '' eq ref $form->{$_} } keys %{ $form };
614 $form->{title} = $locale->text('Delete delivery order');
617 print $form->parse_html_template('do/delete', { 'VARIABLES' => \@variables });
619 $lxdebug->leave_sub();
622 sub delete_delivery_order {
623 $lxdebug->enter_sub();
629 if(!exists $form->{addition}) {
630 $form->{snumbers} = qq|donumber_| . $form->{donumber};
631 $form->{addition} = "DELETED";
632 $form->save_history($form->dbconnect(\%myconfig));
634 # /saving the history
636 $form->info($locale->text('Delivery Order deleted!'));
640 $form->error($locale->text('Cannot delete delivery order!'));
642 $lxdebug->leave_sub();
646 $lxdebug->enter_sub();
649 $auth->assert($form->{type} eq 'purchase_delivery_order' ? 'vendor_invoice_edit' : 'invoice_edit');
651 $form->{close_do_ids} = $form->{id};
653 $form->{deliverydate} = $form->{transdate};
654 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
655 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
661 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
663 if ($form->{type} eq 'purchase_delivery_order') {
664 $form->{title} = $locale->text('Add Vendor Invoice');
665 $form->{script} = 'ir.pl';
669 $form->{title} = $locale->text('Add Sales Invoice');
670 $form->{script} = 'is.pl';
675 for $i (1 .. $form->{rowcount}) {
676 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"} } qw(ship qty sellprice listprice basefactor);
679 $form->{type} = "invoice";
682 $locale = new Locale "$myconfig{countrycode}", "$script";
684 require "bin/mozilla/$form->{script}";
686 my $currency = $form->{currency};
689 $form->{currency} = $currency;
690 $form->{exchangerate} = "";
691 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell);
692 $form->{exchangerate} = $form->{forex} if ($form->{forex});
697 for $i (1 .. $form->{rowcount}) {
698 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
700 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
702 $decimalplaces = ($dec > 2) ? $dec : 2;
704 # copy delivery date from reqdate for order -> invoice conversion
705 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
706 unless $form->{"deliverydate_$i"};
708 $form->{"sellprice_$i"} =
709 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
712 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
713 $dec_qty = length $dec_qty;
715 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
721 $lxdebug->leave_sub();
725 $lxdebug->enter_sub();
728 $auth->assert($form->{type} eq 'sales_delivery_order' ? 'invoice_edit' : 'vendor_invoice_edit');
730 my @do_ids = map { $form->{"trans_id_$_"} } grep { $form->{"multi_id_$_"} } (1..$form->{rowcount});
732 if (!scalar @do_ids) {
733 $form->show_generic_error($locale->text('You have not selected any delivery order.'), 'back_button' => 1);
736 map { delete $form->{$_} } grep { m/^(?:trans|multi)_id_\d+/ } keys %{ $form };
738 if (!DO->retrieve('vc' => $form->{vc}, 'ids' => \@do_ids)) {
739 $form->show_generic_error($form->{vc} eq 'customer' ?
740 $locale->text('You cannot create an invoice for delivery orders for different customers.') :
741 $locale->text('You cannot create an invoice for delivery orders from different vendors.'),
745 $form->{close_do_ids} = join ' ', @do_ids;
746 $form->{deliverydate} = $form->{transdate};
747 $form->{transdate} = $form->current_date(\%myconfig);
748 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
749 $form->{type} = "invoice";
751 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
754 if ($form->{type} eq 'purchase_delivery_order') {
755 $form->{title} = $locale->text('Add Vendor Invoice');
756 $form->{script} = 'ir.pl';
761 $form->{title} = $locale->text('Add Sales Invoice');
762 $form->{script} = 'is.pl';
767 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
769 $form->{rowcount} = 0;
770 foreach my $ref (@{ $form->{form_details} }) {
772 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{ $ref };
773 map { $form->{"${_}_$form->{rowcount}"} = $form->format_amount(\%myconfig, $ref->{$_}) } qw(qty sellprice discount lastcost);
775 delete $form->{form_details};
777 $locale = new Locale "$myconfig{countrycode}", "$script";
779 require "bin/mozilla/$form->{script}";
785 $lxdebug->leave_sub();
789 $lxdebug->enter_sub();
793 $form->{saveasnew} = 1;
795 $form->{delivered} = 0;
796 map { delete $form->{$_} } qw(printed emailed queued);
798 # Let Lx-Office assign a new order number if the user hasn't changed the
799 # previous one. If it has been changed manually then use it as-is.
800 $form->{donumber} =~ s/^\s*//g;
801 $form->{donumber} =~ s/\s*$//g;
802 if ($form->{saved_donumber} && ($form->{saved_donumber} eq $form->{donumber})) {
803 delete($form->{donumber});
808 $lxdebug->leave_sub();
812 $lxdebug->enter_sub();
816 $form->{print_and_save} = 1;
820 my $saved_form = save_form();
824 restore_form($saved_form, 0, qw(id ordnumber quonumber));
828 $lxdebug->leave_sub();
831 sub calculate_stock_in_out {
832 $lxdebug->enter_sub();
836 if (!$form->{"id_${i}"}) {
837 $lxdebug->leave_sub();
841 AM->retrieve_all_units();
843 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
844 my $sinfo = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_${i}"});
846 my $sum = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $sinfo });
848 my $content = $form->format_amount_units('amount' => $sum * 1,
849 'part_unit' => $form->{"partunit_$i"},
850 'amount_unit' => $all_units->{$form->{"partunit_$i"}}->{base_unit},
851 'conv_units' => 'convertible_not_smaller',
853 $content .= qq| <input type="button" onclick="open_stock_in_out_window('${in_out}', $i);" value="?">|;
855 $lxdebug->leave_sub();
860 sub get_basic_bin_wh_info {
861 $lxdebug->enter_sub();
863 my $stock_info = shift;
865 foreach my $sinfo (@{ $stock_info }) {
866 next unless ($sinfo->{bin_id});
868 my $bin_info = WH->get_basic_bin_info('id' => $sinfo->{bin_id});
869 map { $sinfo->{"${_}_description"} = $sinfo->{"${_}description"} = $bin_info->{"${_}_description"} } qw(bin warehouse);
872 $lxdebug->leave_sub();
875 sub stock_in_out_form {
876 $lxdebug->enter_sub();
878 if ($form->{in_out} eq 'out') {
884 $lxdebug->leave_sub();
887 sub redo_stock_info {
888 $lxdebug->enter_sub();
892 my @non_empty = grep { $_->{qty} } @{ $params{stock_info} };
894 if ($params{add_empty_row}) {
896 'warehouse_id' => scalar(@non_empty) ? $non_empty[-1]->{warehouse_id} : undef,
897 'bin_id' => scalar(@non_empty) ? $non_empty[-1]->{bin_id} : undef,
901 @{ $params{stock_info} } = @non_empty;
903 $lxdebug->leave_sub();
906 sub update_stock_in {
907 $lxdebug->enter_sub();
911 foreach my $i (1..$form->{rowcount}) {
912 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
913 push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber qty unit) };
916 display_stock_in_form($stock_info);
918 $lxdebug->leave_sub();
922 $lxdebug->enter_sub();
924 my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
926 display_stock_in_form($stock_info);
928 $lxdebug->leave_sub();
931 sub display_stock_in_form {
932 $lxdebug->enter_sub();
934 my $stock_info = shift;
936 $form->{title} = $locale->text('Stock');
938 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
940 my $units = AM->retrieve_units(\%myconfig, $form);
941 my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
943 $form->get_lists('warehouses' => { 'key' => 'WAREHOUSES',
946 redo_stock_info('stock_info' => $stock_info, 'add_empty_row' => !$form->{delivered});
948 get_basic_bin_wh_info($stock_info);
951 print $form->parse_html_template('do/stock_in_form', { 'UNITS' => $units_data,
952 'STOCK_INFO' => $stock_info,
953 'PART_INFO' => $part_info, });
955 $lxdebug->leave_sub();
959 $lxdebug->enter_sub();
963 foreach my $i (1..$form->{rowcount}) {
964 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
966 next if ($form->{"qty_$i"} <= 0);
968 push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber qty unit) };
971 $form->{stock} = YAML::Dump($stock_info);
974 print $form->parse_html_template('do/set_stock_in_out');
976 $lxdebug->leave_sub();
980 $lxdebug->enter_sub();
982 $form->{title} = $locale->text('Release From Stock');
984 my $part_info = IC->get_basic_part_info('id' => $form->{parts_id});
986 my $units = AM->retrieve_units(\%myconfig, $form);
987 my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
989 my @contents = DO->get_item_availability('parts_id' => $form->{parts_id});
991 my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
993 if (!$form->{delivered}) {
994 foreach my $row (@contents) {
995 $row->{available_qty} = $form->format_amount_units('amount' => $row->{qty} * 1,
996 'part_unit' => $part_info->{unit},
997 'conv_units' => 'convertible_not_smaller',
1000 foreach my $sinfo (@{ $stock_info }) {
1001 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
1002 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1003 ($row->{chargenumber} ne $sinfo->{chargenumber}));
1005 map { $row->{"stock_$_"} = $sinfo->{$_} } qw(qty unit error);
1010 get_basic_bin_wh_info($stock_info);
1012 foreach my $sinfo (@{ $stock_info }) {
1013 map { $sinfo->{"stock_$_"} = $sinfo->{$_} } qw(qty unit);
1018 print $form->parse_html_template('do/stock_out_form', { 'UNITS' => $units_data,
1019 'WHCONTENTS' => $form->{delivered} ? $stock_info : \@contents,
1020 'PART_INFO' => $part_info, });
1022 $lxdebug->leave_sub();
1026 $lxdebug->enter_sub();
1028 my $stock_info = [];
1030 foreach my $i (1 .. $form->{rowcount}) {
1031 $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
1033 next if ($form->{"qty_$i"} <= 0);
1035 push @{ $stock_info }, {
1036 'warehouse_id' => $form->{"warehouse_id_$i"},
1037 'bin_id' => $form->{"bin_id_$i"},
1038 'chargenumber' => $form->{"chargenumber_$i"},
1039 'qty' => $form->{"qty_$i"},
1040 'unit' => $form->{"unit_$i"},
1045 my @errors = DO->check_stock_availability('requests' => $stock_info,
1046 'parts_id' => $form->{parts_id});
1048 $form->{stock} = YAML::Dump($stock_info);
1051 $form->{ERRORS} = [];
1052 map { push @{ $form->{ERRORS} }, $locale->text('Error in row #1: The quantity you entered is bigger than the stocked quantity.', $_->{row}); } @errors;
1053 stock_in_out_form();
1057 print $form->parse_html_template('do/set_stock_in_out');
1060 $lxdebug->leave_sub();
1064 $lxdebug->enter_sub();
1066 if (DO->is_marked_as_delivered('id' => $form->{id})) {
1067 $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred in.'), 'back_button' => 1);
1070 my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_in_${_}"} } (1 .. $form->{rowcount});
1074 my $units = AM->retrieve_units(\%myconfig, $form);
1075 my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1078 $form->{ERRORS} = [];
1080 foreach my $i (1 .. $form->{rowcount}) {
1081 next unless ($form->{"id_$i"} && $form->{"stock_in_$i"});
1083 my $row_sum_base_qty = 0;
1084 my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1086 foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_in_$i"}) }) {
1087 $request->{parts_id} = $form->{"id_$i"};
1088 $row_sum_base_qty += $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1090 push @all_requests, $request;
1093 next if (0 == $row_sum_base_qty);
1095 my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1097 if ($do_base_qty != $row_sum_base_qty) {
1098 push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no stock at all or the full quantity of #2 #3.',
1099 $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1103 if (@{ $form->{ERRORS} }) {
1104 push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1107 $lxdebug->leave_sub();
1113 DO->transfer_in_out('direction' => 'in',
1114 'requests' => \@all_requests);
1116 $form->{delivered} = 1;
1120 $lxdebug->leave_sub();
1124 $lxdebug->enter_sub();
1126 if (DO->is_marked_as_delivered('id' => $form->{id})) {
1127 $form->show_generic_error($locale->text('The parts for this delivery order have already been transferred out.'), 'back_button' => 1);
1130 my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_out_${_}"} } (1 .. $form->{rowcount});
1134 my $units = AM->retrieve_units(\%myconfig, $form);
1135 my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
1138 $form->{ERRORS} = [];
1140 foreach my $i (1 .. $form->{rowcount}) {
1141 next unless ($form->{"id_$i"} && $form->{"stock_out_$i"});
1143 my $row_sum_base_qty = 0;
1144 my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
1146 foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_out_$i"}) }) {
1147 $request->{parts_id} = $form->{"id_$i"};
1148 $request->{base_qty} = $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
1150 my $map_key = join '--', ($form->{"id_$i"}, @{$request}{qw(warehouse_id bin_id chargenumber)});
1152 $request_map{$map_key} ||= $request;
1153 $request_map{$map_key}->{sum_base_qty} ||= 0;
1154 $request_map{$map_key}->{sum_base_qty} += $request->{base_qty};
1155 $row_sum_base_qty += $request->{base_qty};
1157 push @all_requests, $request;
1160 next if (0 == $row_sum_base_qty);
1162 my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
1164 if ($do_base_qty != $row_sum_base_qty) {
1165 push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no transfer at all or the full quantity of #2 #3.',
1166 $i, $form->{"qty_$i"}, $form->{"unit_$i"});
1171 my @bin_ids = map { $_->{bin_id} } values %request_map;
1172 my %bin_info_map = WH->get_basic_bin_info('id' => \@bin_ids);
1173 my @contents = DO->get_item_availability('parts_id' => \@part_ids);
1175 foreach my $inv (@contents) {
1176 my $map_key = join '--', @{$inv}{qw(parts_id warehouse_id bin_id chargenumber)};
1178 next unless ($request_map{$map_key});
1180 my $request = $request_map{$map_key};
1181 $request->{ok} = $request->{sum_base_qty} <= $inv->{qty};
1184 foreach $request (values %request_map) {
1185 next if ($request->{ok});
1187 my $pinfo = $part_info_map{$request->{parts_id}};
1188 my $binfo = $bin_info_map{$request->{bin_id}};
1190 push @{ $form->{ERRORS} }, $locale->text("There is not enough available of '#1' at warehouse '#2', bin '#3', #4, for the transfer of #5.",
1191 $pinfo->{description}, $binfo->{warehouse_description}, $binfo->{bin_description},
1192 $request->{chargenumber} ? $locale->text('chargenumber #1', $request->{chargenumber}) : $locale->text('no chargenumber'),
1193 $form->format_amount_units('amount' => $request->{sum_base_qty},
1194 'part_unit' => $pinfo->{unit},
1195 'conv_units' => 'convertible_not_smaller'));
1199 if (@{ $form->{ERRORS} }) {
1200 push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been marked as delivered. The warehouse contents have not changed.');
1203 $lxdebug->leave_sub();
1209 DO->transfer_in_out('direction' => 'out',
1210 'requests' => \@all_requests);
1212 $form->{delivered} = 1;
1216 $lxdebug->leave_sub();
1220 $lxdebug->enter_sub();
1222 DO->close_orders('ids' => [ $form->{id} ]);
1224 $form->{closed} = 1;
1228 $lxdebug->leave_sub();
1233 call_sub($form->{yes_nextsub});
1237 call_sub($form->{no_nextsub});
1241 call_sub($form->{update_nextsub} || $form->{nextsub} || 'update_delivery_order');