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};
 
 161   $payment_id  = $form->{payment_id}  if ($form->{payment_id});
 
 162   $language_id = $form->{language_id} if ($form->{language_id});
 
 163   $taxzone_id  = $form->{taxzone_id}  if ($form->{taxzone_id});
 
 164   $salesman_id = $form->{salesman_id} if ($editing);
 
 167   $taxincluded    = $form->{taxincluded};
 
 168   $form->{shipto} = 1 if $form->{id};
 
 170   if ($form->{"all_$form->{vc}"}) {
 
 171     unless ($form->{"$form->{vc}_id"}) {
 
 172       $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
 
 176   $cp_id    = $form->{cp_id};
 
 177   $intnotes = $form->{intnotes};
 
 179   $form->{cp_id} = $cp_id;
 
 181   $form->{payment_id}  = $payment_id  if ($payment_id);
 
 182   $form->{language_id} = $language_id if ($language_id);
 
 183   $form->{taxzone_id}  = $taxzone_id  if ($taxzone_id);
 
 184   $form->{intnotes}    = $intnotes    if ($intnotes);
 
 186   ($form->{ $form->{vc} })  = split /--/, $form->{ $form->{vc} };
 
 187   $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
 
 189   $form->{taxincluded} = $taxincluded if ($form->{id});
 
 191   $form->{employee} = "$form->{employee}--$form->{employee_id}";
 
 193   $form->{salesman_id} = $salesman_id if ($editing);
 
 195   $lxdebug->leave_sub();
 
 199   $lxdebug->enter_sub();
 
 203   $form->{formname} = $form->{type} unless $form->{formname};
 
 206   foreach $ref (@{ $form->{form_details} }) {
 
 207     $form->{rowcount} = ++$i;
 
 209     map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
 
 211   for my $i (1 .. $form->{rowcount}) {
 
 213       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
 
 215       $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
 
 217     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
 219     $decimalplaces = ($dec > 2) ? $dec : 2;
 
 221     # copy reqdate from deliverydate for invoice -> order conversion
 
 222     $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
 
 224     $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
 
 226     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
 
 227     $dec_qty = length $dec_qty;
 
 228     $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
 230     map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
 
 233   $lxdebug->leave_sub();
 
 237   $lxdebug->enter_sub();
 
 241   $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
 
 242   $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
 
 244   # use JavaScript Calendar or not
 
 245   $form->{jsscript} = 1;
 
 248   $jsscript = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
 
 250   my @old_project_ids = ($form->{"globalproject_id"});
 
 251   map({ push(@old_project_ids, $form->{"project_id_$_"})
 
 252           if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
 
 254   my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
 
 255   $form->get_lists("contacts"       => "ALL_CONTACTS",
 
 256                    "shipto"         => "ALL_SHIPTO",
 
 258                      "key"          => "ALL_PROJECTS",
 
 260                      "old_id"       => \@old_project_ids
 
 262                    "employees"      => "ALL_EMPLOYEES",
 
 263                    "salesmen"       => "ALL_SALESMEN",
 
 265                    "price_factors"  => "ALL_PRICE_FACTORS",
 
 266                    "departments"    => "ALL_DEPARTMENTS",
 
 267                    "business_types" => "ALL_BUSINESS_TYPES",
 
 270   map { $_->{value} = "$_->{description}--$_->{id}" } @{ $form->{ALL_DEPARTMENTS} };
 
 271   map { $_->{value} = "$_->{name}--$_->{id}"        } @{ $form->{ALL_VC} };
 
 273   $form->{SHOW_VC_DROP_DOWN} =  $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
 
 275   $form->{oldvcname}         =  $form->{"old$form->{vc}"};
 
 276   $form->{oldvcname}         =~ s/--.*//;
 
 278   $form->{onload} = "";
 
 279   if ($form->{resubmit}) {
 
 280     if ($form->{format} eq "html") {
 
 281       $form->{onload} = "window.open('about:blank','Beleg'); document.do.target = 'Beleg';";
 
 283     $form->{onload} .= "document.do.submit();"
 
 287   print $form->parse_html_template('do/form_header');
 
 289   $lxdebug->leave_sub();
 
 293   $lxdebug->enter_sub();
 
 297   $form->{PRINT_OPTIONS} = print_options('inline' => 1);
 
 299   print $form->parse_html_template('do/form_footer');
 
 301   $lxdebug->leave_sub();
 
 304 sub update_delivery_order {
 
 305   $lxdebug->enter_sub();
 
 309   set_headings($form->{"id"} ? "edit" : "add");
 
 313   $payment_id = $form->{payment_id} if $form->{payment_id};
 
 315   check_name($form->{vc});
 
 317   $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
 
 320   $i = $form->{rowcount};
 
 322   if (   ($form->{"partnumber_$i"} eq "")
 
 323       && ($form->{"description_$i"} eq "")
 
 324       && ($form->{"partsgroup_$i"}  eq "")) {
 
 330     if ($form->{type} eq 'purchase_delivery_order') {
 
 331       IR->retrieve_item(\%myconfig, $form);
 
 333       IS->retrieve_item(\%myconfig, $form);
 
 336     my $rows = scalar @{ $form->{item_list} };
 
 339       $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
 
 348         map { $form->{item_list}[$i]{$_} =~ s/\"/"/g }    qw(partnumber description unit);
 
 349         map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
 
 351         $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
 
 352         $form->{"sellprice_$i"}          = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
 
 353         $form->{"qty_$i"}                = $form->format_amount(\%myconfig, $form->{"qty_$i"});
 
 360       # ok, so this is a new part
 
 361       # ask if it is a part or service item
 
 363       if (   $form->{"partsgroup_$i"}
 
 364           && ($form->{"partsnumber_$i"} eq "")
 
 365           && ($form->{"description_$i"} eq "")) {
 
 367         $form->{"discount_$i"} = "";
 
 371         $form->{"id_$i"}   = 0;
 
 377   $lxdebug->leave_sub();
 
 381   $lxdebug->enter_sub();
 
 385   $form->{vc} = $form->{type} eq 'purchase_order' ? 'vendor' : 'customer';
 
 387   $form->get_lists("projects"     => { "key" => "ALL_PROJECTS",
 
 389                    "employees"    => "ALL_EMPLOYEES",
 
 390                    "salesmen"     => "ALL_SALESMEN",
 
 391                    "$form->{vc}s" => "ALL_VC");
 
 393   $form->{SHOW_VC_DROP_DOWN} =  $myconfig{vclimit} > scalar @{ $form->{ALL_VC} };
 
 394   $form->{jsscript}          = 1;
 
 395   $form->{title}             = $locale->text('Delivery Orders');
 
 399   print $form->parse_html_template('do/search');
 
 401   $lxdebug->leave_sub();
 
 405   $lxdebug->enter_sub();
 
 409   ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
 
 411   $form->{sort} ||= 'transdate';
 
 415   $form->{rowcount} = scalar @{ $form->{DO} };
 
 422     shipvia                 globalprojectnumber
 
 423     transaction_description
 
 427   $form->{l_open}      = $form->{l_closed} = "Y" if ($form->{open}      && $form->{closed});
 
 428   $form->{l_delivered} = "Y"                     if ($form->{delivered} && $form->{notdelivered});
 
 430   $form->{title}       = $locale->text('Delivery Orders');
 
 432   my $attachment_basename = $form->{vc} eq 'vendor' ? $locale->text('purchase_delivery_order_list') : $locale->text('sales_delivery_order_list');
 
 434   my $report = SL::ReportGenerator->new(\%myconfig, $form);
 
 436   my @hidden_variables = map { "l_${_}" } @columns;
 
 437   push @hidden_variables, $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered donumber ordnumber
 
 438                                           transaction_description transdatefrom transdateto type vc employee_id salesman_id);
 
 440   my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
 
 443     'ids'                     => { 'text' => '', },
 
 444     'transdate'               => { 'text' => $locale->text('Date'), },
 
 445     'id'                      => { 'text' => $locale->text('ID'), },
 
 446     'donumber'                => { 'text' => $locale->text('Delivery Order'), },
 
 447     'ordnumber'               => { 'text' => $locale->text('Order'), },
 
 448     'name'                    => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
 
 449     'employee'                => { 'text' => $locale->text('Salesperson'), },
 
 450     'shipvia'                 => { 'text' => $locale->text('Ship via'), },
 
 451     'globalprojectnumber'     => { 'text' => $locale->text('Project Number'), },
 
 452     'transaction_description' => { 'text' => $locale->text('Transaction description'), },
 
 453     'open'                    => { 'text' => $locale->text('Open'), },
 
 454     'delivered'               => { 'text' => $locale->text('Delivered'), },
 
 457   foreach my $name (qw(id transdate donumber ordnumber name employee shipvia)) {
 
 458     $column_defs{$name}->{link} = $href . "&sort=$name";
 
 461   $form->{"l_type"} = "Y";
 
 462   map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
 
 463   $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
 
 465   $report->set_columns(%column_defs);
 
 466   $report->set_column_order(@columns);
 
 468   $report->set_export_options('orders', @hidden_variables);
 
 470   $report->set_sort_indicator($form->{sort}, 1);
 
 473   if ($form->{customer}) {
 
 474     push @options, $locale->text('Customer') . " : $form->{customer}";
 
 476   if ($form->{vendor}) {
 
 477     push @options, $locale->text('Vendor') . " : $form->{vendor}";
 
 479   if ($form->{department}) {
 
 480     ($department) = split /--/, $form->{department};
 
 481     push @options, $locale->text('Department') . " : $department";
 
 483   if ($form->{donumber}) {
 
 484     push @options, $locale->text('Delivery Order Number') . " : $form->{donumber}";
 
 486   if ($form->{ordnumber}) {
 
 487     push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
 
 489   if ($form->{transaction_description}) {
 
 490     push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
 
 492   if ($form->{transdatefrom}) {
 
 493     push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
 
 495   if ($form->{transdateto}) {
 
 496     push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
 
 499     push @options, $locale->text('Open');
 
 501   if ($form->{closed}) {
 
 502     push @options, $locale->text('Closed');
 
 504   if ($form->{delivered}) {
 
 505     push @options, $locale->text('Delivered');
 
 507   if ($form->{notdelivered}) {
 
 508     push @options, $locale->text('Not delivered');
 
 511   $report->set_options('top_info_text'       => join("\n", @options),
 
 512                        'output_format'       => 'HTML',
 
 513                        'title'               => $form->{title},
 
 514                        'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
 
 516   $report->set_options_from_form();
 
 518   # add sort and escape callback, this one we use for the add sub
 
 519   $form->{callback} = $href .= "&sort=$form->{sort}";
 
 521   # escape callback for href
 
 522   $callback = $form->escape($href);
 
 524   my $edit_url       = build_std_url('action=edit', 'type', 'vc');
 
 525   my $edit_order_url = build_std_url('script=oe.pl', 'type=' . ($form->{type} eq 'sales_delivery_order' ? 'sales_order' : 'purchase_order'), 'action=edit');
 
 527   foreach $dord (@{ $form->{DO} }) {
 
 528     $dord->{open}      = $dord->{closed}    ? $locale->text('No')  : $locale->text('Yes');
 
 529     $dord->{delivered} = $dord->{delivered} ? $locale->text('Yes') : $locale->text('No');
 
 531     my $row = { map { $_ => { 'data' => $dord->{$_} } } @columns };
 
 533     $row->{donumber}->{link}  = $edit_url       . "&id=" . E($dord->{id})      . "&callback=${callback}";
 
 534     $row->{ordnumber}->{link} = $edit_order_url . "&id=" . E($dord->{oe_id})   . "&callback=${callback}";
 
 536     $report->add_data($row);
 
 539   $report->generate_with_headers();
 
 541   $lxdebug->leave_sub();
 
 545   $lxdebug->enter_sub();
 
 549   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 551   $form->isblank("transdate", $locale->text('Delivery Order Date missing!'));
 
 553   $form->{donumber} =~ s/^\s*//g;
 
 554   $form->{donumber} =~ s/\s*$//g;
 
 556   $msg = ucfirst $form->{vc};
 
 557   $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
 
 559   # $locale->text('Customer missing!');
 
 560   # $locale->text('Vendor missing!');
 
 564   # if the name changed get new values
 
 565   if (check_name($form->{vc})) {
 
 570   $form->{id} = 0 if $form->{saveasnew};
 
 575   if(!exists $form->{addition}) {
 
 576     $form->{snumbers} = qq|donumber_| . $form->{donumber};
 
 577         $form->{addition} = "SAVED";
 
 578         $form->save_history($form->dbconnect(\%myconfig));
 
 580   # /saving the history
 
 582   $form->{simple_save} = 1;
 
 583   if(!$form->{print_and_save}) {
 
 584     set_headings("edit");
 
 588   $lxdebug->leave_sub();
 
 592   $lxdebug->enter_sub();
 
 596   map { delete $form->{$_} } qw(action header login password);
 
 597   my @variables = map { { 'key' => $_, 'value' => $form->{$_} } } grep { '' eq ref $form->{$_} } keys %{ $form };
 
 599   $form->{title} = $locale->text('Delete delivery order');
 
 602   print $form->parse_html_template('do/delete', { 'VARIABLES' => \@variables });
 
 604   $lxdebug->leave_sub();
 
 607 sub delete_delivery_order {
 
 608   $lxdebug->enter_sub();
 
 614     if(!exists $form->{addition}) {
 
 615       $form->{snumbers} = qq|donumber_| . $form->{donumber};
 
 616           $form->{addition} = "DELETED";
 
 617           $form->save_history($form->dbconnect(\%myconfig));
 
 619     # /saving the history
 
 621     $form->info($locale->text('Delivery Order deleted!'));
 
 625   $form->error($locale->text('Cannot delete delivery order!'));
 
 627   $lxdebug->leave_sub();
 
 631   $lxdebug->enter_sub();
 
 634   $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
 
 636   $form->{deliverydate} = $form->{transdate};
 
 637   $form->{transdate}    = $form->{invdate} = $form->current_date(\%myconfig);
 
 638   $form->{duedate}      = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
 
 644   $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
 
 646   if ($form->{type} eq 'purchase_delivery_order') {
 
 647     $form->{title}  = $locale->text('Add Vendor Invoice');
 
 648     $form->{script} = 'ir.pl';
 
 652     $form->{title}  = $locale->text('Add Sales Invoice');
 
 653     $form->{script} = 'is.pl';
 
 658   for $i (1 .. $form->{rowcount}) {
 
 659     map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"} } qw(ship qty sellprice listprice basefactor);
 
 662   $form->{type} = "invoice";
 
 665   $locale = new Locale "$myconfig{countrycode}", "$script";
 
 667   require "bin/mozilla/$form->{script}";
 
 669   my $currency = $form->{currency};
 
 672   $form->{currency}     = $currency;
 
 673   $form->{exchangerate} = "";
 
 674   $form->{forex}        = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell));
 
 675   $form->{exchangerate} = $exchangerate if ($form->{forex});
 
 680   for $i (1 .. $form->{rowcount}) {
 
 681     $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
 
 683     ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
 
 685     $decimalplaces = ($dec > 2) ? $dec : 2;
 
 687     # copy delivery date from reqdate for order -> invoice conversion
 
 688     $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
 
 689       unless $form->{"deliverydate_$i"};
 
 691     $form->{"sellprice_$i"} =
 
 692       $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
 
 695     (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
 
 696     $dec_qty = length $dec_qty;
 
 698       $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
 
 704   $lxdebug->leave_sub();
 
 708   $lxdebug->enter_sub();
 
 712   $form->{saveasnew} = 1;
 
 714   map { delete $form->{$_} } qw(printed emailed queued);
 
 716   # Let Lx-Office assign a new order number if the user hasn't changed the
 
 717   # previous one. If it has been changed manually then use it as-is.
 
 718   $form->{donumber} =~ s/^\s*//g;
 
 719   $form->{donumber} =~ s/\s*$//g;
 
 720   if ($form->{saved_donumber} && ($form->{saved_donumber} eq $form->{donumber})) {
 
 721     delete($form->{donumber});
 
 726   $lxdebug->leave_sub();
 
 730   $lxdebug->enter_sub();
 
 734   $form->{print_and_save} = 1;
 
 738   my $saved_form = save_form();
 
 742   restore_form($saved_form, 0, qw(id ordnumber quonumber));
 
 746   $lxdebug->leave_sub();
 
 749 sub calculate_stock_in_out {
 
 750   $lxdebug->enter_sub();
 
 754   if (!$form->{"id_${i}"}) {
 
 755     $lxdebug->leave_sub();
 
 759   my $in_out   = $form->{type} =~ /^sales/ ? 'out' : 'in';
 
 760   my $sinfo    = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_${i}"});
 
 762   my $sum      = AM->sum_with_unit(map { $_->{qty}, $_->{unit} } @{ $sinfo });
 
 764   my $content  = $form->format_amount_units('amount'      => $sum * 1,
 
 765                                             'part_unit'   => $form->{"partunit_$i"},
 
 766                                             'amount_unit' => $units->{$form->{"partunit_$i"}}->{base_unit},
 
 767                                             'conv_units'  => 'convertible_not_smaller',
 
 769   $content    .= qq| <input type="button" onclick="open_stock_in_out_window('${in_out}', $i);" value="?">|;
 
 771   $lxdebug->leave_sub();
 
 776 sub get_basic_bin_wh_info {
 
 777   $lxdebug->enter_sub();
 
 779   my $stock_info = shift;
 
 781   foreach my $sinfo (@{ $stock_info }) {
 
 782     next unless ($sinfo->{bin_id});
 
 784     my $bin_info = WH->get_basic_bin_info('id' => $sinfo->{bin_id});
 
 785     map { $sinfo->{"${_}_description"} = $sinfo->{"${_}description"} = $bin_info->{"${_}_description"} } qw(bin warehouse);
 
 788   $lxdebug->leave_sub();
 
 791 sub stock_in_out_form {
 
 792   $lxdebug->enter_sub();
 
 794   if ($form->{in_out} eq 'out') {
 
 800   $lxdebug->leave_sub();
 
 803 sub redo_stock_info {
 
 804   $lxdebug->enter_sub();
 
 808   my @non_empty = grep { $_->{qty} } @{ $params{stock_info} };
 
 810   if ($params{add_empty_row}) {
 
 812       'warehouse_id' => scalar(@non_empty) ? $non_empty[-1]->{warehouse_id} : undef,
 
 813       'bin_id'       => scalar(@non_empty) ? $non_empty[-1]->{bin_id}       : undef,
 
 817   @{ $params{stock_info} } = @non_empty;
 
 819   $lxdebug->leave_sub();
 
 822 sub update_stock_in {
 
 823   $lxdebug->enter_sub();
 
 827   foreach my $i (1..$form->{rowcount}) {
 
 828     push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber qty unit) };
 
 830   $main::lxdebug->dump(0, "si", $stock_info);
 
 832   display_stock_in_form($stock_info);
 
 834   $lxdebug->leave_sub();
 
 838   $lxdebug->enter_sub();
 
 840   my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
 
 842   display_stock_in_form($stock_info);
 
 844   $lxdebug->leave_sub();
 
 847 sub display_stock_in_form {
 
 848   $lxdebug->enter_sub();
 
 850   my $stock_info = shift;
 
 852   $form->{title} = $locale->text('Stock');
 
 854   my $part_info  = IC->get_basic_part_info('id' => $form->{parts_id});
 
 856   my $units      = AM->retrieve_units(\%myconfig, $form, "dimension");
 
 857   my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
 
 859   my $access     = $auth->check_right($form->{login}, 'all_warehouses') ? undef : $form->{login};
 
 861   $form->get_lists('warehouses' => { 'key'    => 'WAREHOUSES',
 
 863                                      'access' => $access, });
 
 865   redo_stock_info('stock_info' => $stock_info, 'add_empty_row' => !$form->{closed});
 
 867   get_basic_bin_wh_info($stock_info);
 
 870   print $form->parse_html_template('do/stock_in_form', { 'UNITS'      => $units_data,
 
 871                                                          'STOCK_INFO' => $stock_info,
 
 872                                                          'PART_INFO'  => $part_info, });
 
 874   $lxdebug->leave_sub();
 
 878   $lxdebug->enter_sub();
 
 882   foreach my $i (1..$form->{rowcount}) {
 
 883     $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
 
 885     next if ($form->{"qty_$i"} <= 0);
 
 887     push @{ $stock_info }, { map { $_ => $form->{"${_}_${i}"} } qw(warehouse_id bin_id chargenumber qty unit) };
 
 890   $form->{stock} = YAML::Dump($stock_info);
 
 893   print $form->parse_html_template('do/set_stock_in_out');
 
 895   $lxdebug->leave_sub();
 
 899   $lxdebug->enter_sub();
 
 901   $form->{title} = $locale->text('Release From Stock');
 
 903   my $part_info  = IC->get_basic_part_info('id' => $form->{parts_id});
 
 905   my $units      = AM->retrieve_units(\%myconfig, $form, "dimension");
 
 906   my $units_data = AM->unit_select_data($units, undef, undef, $part_info->{unit});
 
 908   my @contents   = DO->get_item_availability('parts_id' => $form->{parts_id});
 
 910   my $stock_info = DO->unpack_stock_information('packed' => $form->{stock});
 
 912   if (!$form->{closed}) {
 
 913     foreach my $row (@contents) {
 
 914       $row->{available_qty} = $form->format_amount_units('amount'      => $row->{qty} * 1,
 
 915                                                          'part_unit'   => $part_info->{unit},
 
 916                                                          'conv_units'  => 'convertible_not_smaller',
 
 919       foreach my $sinfo (@{ $stock_info }) {
 
 920         next if (($row->{bin_id}       != $sinfo->{bin_id}) ||
 
 921                  ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
 
 922                  ($row->{chargenumber} ne $sinfo->{chargenumber}));
 
 924         map { $row->{"stock_$_"} = $sinfo->{$_} } qw(qty unit error);
 
 929     get_basic_bin_wh_info($stock_info);
 
 931     foreach my $sinfo (@{ $stock_info }) {
 
 932       map { $sinfo->{"stock_$_"} = $sinfo->{$_} } qw(qty unit);
 
 937   print $form->parse_html_template('do/stock_out_form', { 'UNITS'      => $units_data,
 
 938                                                           'WHCONTENTS' => $form->{closed} ? $stock_info : \@contents,
 
 939                                                           'PART_INFO'  => $part_info, });
 
 941   $lxdebug->leave_sub();
 
 945   $lxdebug->enter_sub();
 
 949   foreach my $i (1 .. $form->{rowcount}) {
 
 950     $form->{"qty_$i"} = $form->parse_amount(\%myconfig, $form->{"qty_$i"});
 
 952     next if ($form->{"qty_$i"} <= 0);
 
 954     push @{ $stock_info }, {
 
 955       'warehouse_id' => $form->{"warehouse_id_$i"},
 
 956       'bin_id'       => $form->{"bin_id_$i"},
 
 957       'chargenumber' => $form->{"chargenumber_$i"},
 
 958       'qty'          => $form->{"qty_$i"},
 
 959       'unit'         => $form->{"unit_$i"},
 
 964   my @errors     = DO->check_stock_availability('requests' => $stock_info,
 
 965                                                 'parts_id' => $form->{parts_id});
 
 967   $form->{stock} = YAML::Dump($stock_info);
 
 970     $form->{ERRORS} = [];
 
 971     map { push @{ $form->{ERRORS} }, $locale->text('Error in row #1: The quantity you entered is bigger than the stocked quantity.', $_->{row}); } @errors;
 
 976     print $form->parse_html_template('do/set_stock_in_out');
 
 979   $lxdebug->leave_sub();
 
 982 sub transfer_in_and_close {
 
 983   $lxdebug->enter_sub();
 
 985   my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_in_${_}"} } (1 .. $form->{rowcount});
 
 989     my $units         = AM->retrieve_units(\%myconfig, $form, "dimension");
 
 990     my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
 
 993     $form->{ERRORS}   = [];
 
 995     foreach my $i (1 .. $form->{rowcount}) {
 
 996       next unless ($form->{"id_$i"} && $form->{"stock_in_$i"});
 
 998       my $row_sum_base_qty = 0;
 
 999       my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
 
1001       foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_in_$i"}) }) {
 
1002         $request->{parts_id}  = $form->{"id_$i"};
 
1003         $row_sum_base_qty    += $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
 
1005         push @all_requests, $request;
 
1008       next if (0 == $row_sum_base_qty);
 
1010       my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
 
1012       if ($do_base_qty != $row_sum_base_qty) {
 
1013         push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no stock at all or the full quantity of #2 #3.',
 
1014                                                  $i, $form->{"qty_$i"}, $form->{"unit_$i"});
 
1018     if (@{ $form->{ERRORS} }) {
 
1019       push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been closed. The warehouse contents have not changed.');
 
1022       $lxdebug->leave_sub();
 
1028   DO->transfer_in_out('direction' => 'in',
 
1029                       'requests'  => \@all_requests);
 
1031   $form->{closed}    = 1;
 
1032   $form->{delivered} = 1;
 
1036   $lxdebug->leave_sub();
 
1039 sub transfer_out_and_close {
 
1040   $lxdebug->enter_sub();
 
1042   my @part_ids = map { $form->{"id_${_}"} } grep { $form->{"id_${_}"} && $form->{"stock_out_${_}"} } (1 .. $form->{rowcount});
 
1046     my $units         = AM->retrieve_units(\%myconfig, $form, "dimension");
 
1047     my %part_info_map = IC->get_basic_part_info('id' => \@part_ids);
 
1050     $form->{ERRORS}   = [];
 
1052     foreach my $i (1 .. $form->{rowcount}) {
 
1053       next unless ($form->{"id_$i"} && $form->{"stock_out_$i"});
 
1055       my $row_sum_base_qty = 0;
 
1056       my $base_unit_factor = $units->{ $part_info_map{$form->{"id_$i"}}->{unit} }->{factor} || 1;
 
1058       foreach my $request (@{ DO->unpack_stock_information('packed' => $form->{"stock_out_$i"}) }) {
 
1059         $request->{parts_id} = $form->{"id_$i"};
 
1060         $request->{base_qty} = $request->{qty} * $units->{$request->{unit}}->{factor} / $base_unit_factor;
 
1062         my $map_key          = join '--', ($form->{"id_$i"}, @{$request}{qw(warehouse_id bin_id chargenumber)});
 
1064         $request_map{$map_key}                 ||= $request;
 
1065         $request_map{$map_key}->{sum_base_qty} ||= 0;
 
1066         $request_map{$map_key}->{sum_base_qty}  += $request->{base_qty};
 
1067         $row_sum_base_qty                       += $request->{base_qty};
 
1069         push @all_requests, $request;
 
1072       next if (0 == $row_sum_base_qty);
 
1074       my $do_base_qty = $form->parse_amount(\%myconfig, $form->{"qty_$i"}) * $units->{$form->{"unit_$i"}}->{factor} / $base_unit_factor;
 
1076       if ($do_base_qty != $row_sum_base_qty) {
 
1077         push @{ $form->{ERRORS} }, $locale->text('Error in position #1: You must either assign no transfer at all or the full quantity of #2 #3.',
 
1078                                                  $i, $form->{"qty_$i"}, $form->{"unit_$i"});
 
1083       my @bin_ids      = map { $_->{bin_id} } values %request_map;
 
1084       my %bin_info_map = WH->get_basic_bin_info('id' => \@bin_ids);
 
1085       my @contents     = DO->get_item_availability('parts_id' => \@part_ids);
 
1087       foreach my $inv (@contents) {
 
1088         my $map_key = join '--', @{$inv}{qw(parts_id warehouse_id bin_id chargenumber)};
 
1090         next unless ($request_map{$map_key});
 
1092         my $request    = $request_map{$map_key};
 
1093         $request->{ok} = $request->{sum_base_qty} <= $inv->{qty};
 
1096       foreach $request (values %request_map) {
 
1097         next if ($request->{ok});
 
1099         my $pinfo = $part_info_map{$request->{parts_id}};
 
1100         my $binfo = $bin_info_map{$request->{bin_id}};
 
1102         push @{ $form->{ERRORS} }, $locale->text("There is not enough available of '#1' at warehouse '#2', bin '#3', #4, for the transfer of #5.",
 
1103                                                  $pinfo->{description}, $binfo->{warehouse_description}, $binfo->{bin_description},
 
1104                                                  $request->{chargenumber} ? $locale->text('chargenumber #1', $request->{chargenumber}) : $locale->text('no chargenumber'),
 
1105                                                  $form->format_amount_units('amount'      => $request->{sum_base_qty},
 
1106                                                                             'part_unit'   => $pinfo->{unit},
 
1107                                                                             'conv_units'  => 'convertible_not_smaller'));
 
1111     if (@{ $form->{ERRORS} }) {
 
1112       push @{ $form->{ERRORS} }, $locale->text('The delivery order has not been closed. The warehouse contents have not changed.');
 
1115       $lxdebug->leave_sub();
 
1121   DO->transfer_in_out('direction' => 'out',
 
1122                       'requests'  => \@all_requests);
 
1124   $form->{closed}    = 1;
 
1125   $form->{delivered} = 1;
 
1129   $lxdebug->leave_sub();
 
1133   call_sub($form->{yes_nextsub});
 
1137   call_sub($form->{no_nextsub});
 
1141   call_sub($form->{update_nextsub} || $form->{nextsub} || 'update_delivery_order');