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) 1999-2003
 
  11 #  Author: Dieter Simader
 
  12 #   Email: dsimader@sql-ledger.org
 
  13 #     Web: http://www.sql-ledger.org
 
  17 # This program is free software; you can redistribute it and/or modify
 
  18 # it under the terms of the GNU General Public License as published by
 
  19 # the Free Software Foundation; either version 2 of the License, or
 
  20 # (at your option) any later version.
 
  22 # This program is distributed in the hope that it will be useful,
 
  23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  25 # GNU General Public License for more details.
 
  26 # You should have received a copy of the GNU General Public License
 
  27 # along with this program; if not, write to the Free Software
 
  28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  29 #======================================================================
 
  31 # Delivery Order entry module
 
  32 #======================================================================
 
  36 use List::Util qw(max);
 
  44   $main::lxdebug->enter_sub();
 
  48   my $myconfig = \%main::myconfig;
 
  49   my $form     = $main::form;
 
  52   my $dbh = $form->get_standard_dbh($myconfig);
 
  54   my (@where, @values, $where);
 
  56   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
 
  59     qq|SELECT dord.id, dord.donumber, dord.ordnumber, dord.transdate,
 
  60          ct.name, dord.${vc}_id, dord.globalproject_id,
 
  61          dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
 
  62          dord.transaction_description,
 
  63          pr.projectnumber AS globalprojectnumber,
 
  67        FROM delivery_orders dord
 
  68        LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
 
  69        LEFT JOIN employee e ON (dord.employee_id = e.id)
 
  70        LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
 
  71        LEFT JOIN project pr ON (dord.globalproject_id = pr.id)
 
  72        LEFT JOIN oe ON ((dord.ordnumber = oe.ordnumber) AND NOT COALESCE(oe.quotation, FALSE))|;
 
  74   push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
 
  76   my $department_id = (split /--/, $form->{department})[1];
 
  78     push @where,  qq|dord.department_id = ?|;
 
  79     push @values, conv_i($department_id);
 
  82   if ($form->{project_id}) {
 
  84       qq|(dord.globalproject_id = ?) OR EXISTS
 
  85           (SELECT * FROM delivery_order_items doi
 
  86            WHERE (doi.project_id = ?) AND (oi.delivery_order_id = dord.id))|;
 
  87     push @values, conv_i($form->{project_id}), conv_i($form->{project_id});
 
  90   if ($form->{"${vc}_id"}) {
 
  91     push @where,  qq|dord.${vc}_id = ?|;
 
  92     push @values, $form->{"${vc}_id"};
 
  94   } elsif ($form->{$vc}) {
 
  95     push @where,  qq|ct.name ILIKE ?|;
 
  96     push @values, '%' . $form->{$vc} . '%';
 
  99   foreach my $item (qw(employee_id salesman_id)) {
 
 100     next unless ($form->{$item});
 
 101     push @where, "dord.$item = ?";
 
 102     push @values, conv_i($form->{$item});
 
 105   foreach my $item (qw(donumber ordnumber cusordnumber transaction_description)) {
 
 106     next unless ($form->{$item});
 
 107     push @where,  qq|dord.$item ILIKE ?|;
 
 108     push @values, '%' . $form->{$item} . '%';
 
 111   if (!($form->{open} && $form->{closed})) {
 
 112     push @where, ($form->{open} ? "NOT " : "") . "COALESCE(dord.closed, FALSE)";
 
 115   if (($form->{notdelivered} || $form->{delivered}) &&
 
 116       ($form->{notdelivered} ne $form->{delivered})) {
 
 117     push @where, ($form->{delivered} ? "" : "NOT ") . "COALESCE(dord.delivered, FALSE)";
 
 120   if($form->{transdatefrom}) {
 
 121     push @where,  qq|dord.transdate >= ?|;
 
 122     push @values, conv_date($form->{transdatefrom});
 
 125   if($form->{transdateto}) {
 
 126     push @where,  qq|dord.transdate <= ?|;
 
 127     push @values, conv_date($form->{transdateto});
 
 131     $query .= " WHERE " . join(" AND ", map { "($_)" } @where);
 
 134   my %allowed_sort_columns = (
 
 135     "transdate"               => "dord.transdate",
 
 137     "donumber"                => "dord.donumber",
 
 138     "ordnumber"               => "dord.ordnumber",
 
 140     "employee"                => "e.name",
 
 141     "salesman"                => "sm.name",
 
 142     "shipvia"                 => "dord.shipvia",
 
 143     "transaction_description" => "dord.transaction_description"
 
 146   my $sortoder = "dord.id";
 
 147   if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
 
 148     $sortorder = $allowed_sort_columns{$form->{sort}};
 
 151   $query .= qq| ORDER by | . $sortorder;
 
 153   $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
 
 155   $main::lxdebug->dump(0, "DO", $form->{DO});
 
 157   $main::lxdebug->leave_sub();
 
 161   $main::lxdebug->enter_sub();
 
 165   my $myconfig = \%main::myconfig;
 
 166   my $form     = $main::form;
 
 168   # connect to database, turn off autocommit
 
 169   my $dbh = $form->get_standard_dbh($myconfig);
 
 171   my ($query, @values, $sth, $null);
 
 173   my $all_units = AM->retrieve_units($myconfig, $form);
 
 174   $form->{all_units} = $all_units;
 
 176   $form->{donumber}    = $form->update_defaults($myconfig, $form->{type} eq 'sales_delivery_order' ? 'sdonumber' : 'pdonumber', $dbh) unless $form->{donumber};
 
 177   $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
 
 178   $form->get_employee($dbh) unless ($form->{employee_id});
 
 180   my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
 
 184     $query = qq|DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = ?)|;
 
 185     do_query($form, $dbh, $query, conv_i($form->{id}));
 
 187     $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
 
 188     do_query($form, $dbh, $query, conv_i($form->{id}));
 
 190     $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
 
 191     do_query($form, $dbh, $query, conv_i($form->{id}));
 
 195     $query = qq|SELECT nextval('id')|;
 
 196     ($form->{id}) = selectrow_query($form, $dbh, $query);
 
 198     $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id) VALUES (?, '', ?)|;
 
 199     do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}));
 
 205   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
 
 206   my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
 
 209   my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
 
 210   my @part_ids    = keys %part_id_map;
 
 214     $query         = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
 
 215     %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
 
 218   my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
 
 219   my $h_item_id = prepare_query($form, $dbh, $q_item_id);
 
 222     qq|INSERT INTO delivery_order_items (
 
 223          id, delivery_order_id, parts_id, description, longdescription, qty, base_qty,
 
 224          sellprice, discount, unit, reqdate, project_id, serialnumber,
 
 225          ordnumber, transdate, cusordnumber,
 
 226          lastcost, price_factor_id, price_factor, marge_price_factor)
 
 227        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
 
 228          (SELECT factor FROM price_factors WHERE id = ?), ?)|;
 
 229   my $h_item = prepare_query($form, $dbh, $q_item);
 
 232     qq|INSERT INTO delivery_order_items_stock (delivery_order_item_id, qty, unit, warehouse_id, bin_id, chargenumber)
 
 233        VALUES (?, ?, ?, ?, ?, ?)|;
 
 234   my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
 
 236   my $in_out       = $form->{type} =~ /^sales/ ? 'out' : 'in';
 
 238   for my $i (1 .. $form->{rowcount}) {
 
 239     next if (!$form->{"id_$i"});
 
 241     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
 243     my $item_unit = $part_unit_map{$form->{"id_$i"}};
 
 246     if (defined($all_units->{$item_unit}->{factor}) && (($all_units->{$item_unit}->{factor} * 1) != 0)) {
 
 247       $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
 
 249     my $baseqty = $form->{"qty_$i"} * $basefactor;
 
 251     $form->{"lastcost_$i"} *= 1;
 
 253     # set values to 0 if nothing entered
 
 254     $form->{"discount_$i"}  = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
 
 255     $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
 
 257     $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
 
 258     $linetotal    = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
 
 260     $reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
 
 262     do_statement($form, $h_item_id, $q_item_id);
 
 263     my ($item_id) = $h_item_id->fetchrow_array();
 
 265     # save detail record in delivery_order_items table
 
 266     @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
 
 267                $form->{"description_$i"}, $form->{"longdescription_$i"},
 
 268                $form->{"qty_$i"}, $baseqty,
 
 269                $form->{"sellprice_$i"}, $form->{"discount_$i"},
 
 270                $form->{"unit_$i"}, conv_date($reqdate), conv_i($form->{"project_id_$i"}),
 
 271                $form->{"serialnumber_$i"},
 
 272                $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
 
 273                $form->{"cusordnumber_$i"},
 
 274                $form->{"lastcost_$i"},
 
 275                conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
 
 276                conv_i($form->{"marge_price_factor_$i"}));
 
 277     do_statement($form, $h_item, $q_item, @values);
 
 279     my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
 
 281     foreach my $sinfo (@{ $stock_info }) {
 
 282       @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
 
 283                  conv_i($sinfo->{bin_id}), $sinfo->{chargenumber});
 
 284       do_statement($form, $h_item_stock, $q_item_stock, @values);
 
 288   $h_item_id->finish();
 
 290   $h_item_stock->finish();
 
 292   ($null, $form->{department_id}) = split(/--/, $form->{department});
 
 296     qq|UPDATE delivery_orders SET
 
 297          donumber = ?, ordnumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
 
 298          customer_id = ?, reqdate = ?,
 
 299          shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
 
 300          delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
 
 301          globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
 
 305   @values = ($form->{donumber}, $form->{ordnumber},
 
 306              $form->{cusordnumber}, conv_date($form->{transdate}),
 
 307              conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
 
 308              conv_date($reqdate), $form->{shippingpoint}, $form->{shipvia},
 
 309              $form->{notes}, $form->{intnotes},
 
 310              $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
 
 311              conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
 
 312              conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
 
 313              conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
 
 314              $form->{transaction_description},
 
 315              $form->{type} =~ /^sales/ ? 't' : 'f',
 
 316              conv_i($form->{id}));
 
 317   do_query($form, $dbh, $query, @values);
 
 320   $form->{name} = $form->{ $form->{vc} };
 
 321   $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
 
 323   if (!$form->{shipto_id}) {
 
 324     $form->add_shipto($dbh, $form->{id}, "DO");
 
 327   # save printed, emailed, queued
 
 328   $form->save_status($dbh);
 
 330   my $rc = $dbh->commit();
 
 332   $form->{saved_donumber} = $form->{donumber};
 
 334   Common::webdav_folder($form) if ($main::webdav);
 
 336   $main::lxdebug->leave_sub();
 
 342   $main::lxdebug->enter_sub();
 
 346   my $myconfig = \%main::myconfig;
 
 347   my $form     = $main::form;
 
 349   return $main::lxdebug->leave_sub() unless ($form->{id});
 
 351   my $dbh = $form->get_standard_dbh($myconfig);
 
 352   do_query($form, $dbh, qq|UPDATE do SET closed = TRUE where id = ?|, conv_i($form->{id}));
 
 355   $main::lxdebug->leave_sub();
 
 359   $main::lxdebug->enter_sub();
 
 363   my $myconfig = \%main::myconfig;
 
 364   my $form     = $main::form;
 
 365   my $spool    = $main::spool;
 
 367   # connect to database
 
 368   my $dbh = $form->get_standard_dbh($myconfig);
 
 371   my $query = qq|SELECT s.spoolfile FROM status s WHERE s.trans_id = ?|;
 
 372   my $sth   = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
 
 377   while (($spoolfile) = $sth->fetchrow_array) {
 
 378     push @spoolfiles, $spoolfile;
 
 383   @values = (conv_i($form->{id}));
 
 385   # delete status entries
 
 386   $query = qq|DELETE FROM status
 
 388   do_query($form, $dbh, $query, @values);
 
 390   # delete individual entries
 
 391   $query = qq|DELETE FROM delivery_order_items_stock
 
 392               WHERE delivery_order_item_id IN (
 
 393                 SELECT id FROM delivery_order_items
 
 394                 WHERE delivery_order_id = ?
 
 396   do_query($form, $dbh, $query, @values);
 
 398   # delete individual entries
 
 399   $query = qq|DELETE FROM delivery_order_items
 
 400               WHERE delivery_order_id = ?|;
 
 401   do_query($form, $dbh, $query, @values);
 
 404   $query = qq|DELETE FROM delivery_orders
 
 406   do_query($form, $dbh, $query, @values);
 
 408   $query = qq|DELETE FROM shipto
 
 409               WHERE trans_id = ? AND module = 'DO'|;
 
 410   do_query($form, $dbh, $query, @values);
 
 412   my $rc = $dbh->commit();
 
 415     foreach $spoolfile (@spoolfiles) {
 
 416       unlink "$spool/$spoolfile" if $spoolfile;
 
 420   $main::lxdebug->leave_sub();
 
 426   $main::lxdebug->enter_sub();
 
 430   my $myconfig = \%main::myconfig;
 
 431   my $form     = $main::form;
 
 433   # connect to database
 
 434   my $dbh = $form->get_standard_dbh($myconfig);
 
 436   my ($query, $query_add, @values, $sth, $ref);
 
 439     $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate, current_date AS reqdate|);
 
 440     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 443   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
 
 447     # retrieve order for single id
 
 448     # NOTE: this query is intended to fetch all information only ONCE.
 
 449     # so if any of these infos is important (or even different) for any item,
 
 450     # it will be killed out and then has to be fetched from the item scope query further down
 
 452       qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.transdate, dord.reqdate,
 
 453            dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
 
 454            e.name AS employee, dord.employee_id, dord.salesman_id,
 
 455            dord.${vc}_id, cv.name AS ${vc},
 
 456            dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
 
 457            d.description AS department, dord.language_id,
 
 459            dord.globalproject_id, dord.delivered, dord.transaction_description
 
 460          FROM delivery_orders dord
 
 461          JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
 
 462          LEFT JOIN employee e ON (dord.employee_id = e.id)
 
 463          LEFT JOIN department d ON (dord.department_id = d.id)
 
 465     $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
 
 467     $ref = $sth->fetchrow_hashref(NAME_lc);
 
 470     map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
 
 472     $form->{saved_donumber} = $form->{donumber};
 
 474     # if not given, fill transdate with current_date
 
 475     $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
 
 477     $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'DO'|;
 
 478     $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
 
 480     $ref = $sth->fetchrow_hashref(NAME_lc);
 
 482     map { $form->{$_} = $ref->{$_} } keys %$ref;
 
 485     # get printed, emailed and queued
 
 486     $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
 
 487     $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
 
 489     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 490       $form->{printed} .= "$ref->{formname} " if $ref->{printed};
 
 491       $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
 
 492       $form->{queued}  .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
 
 495     map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
 
 497     my %oid = ('Pg'     => 'oid',
 
 498                'Oracle' => 'rowid');
 
 500     my $transdate = $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
 
 502     # retrieve individual items
 
 503     # this query looks up all information about the items
 
 504     # stuff different from the whole will not be overwritten, but saved with a suffix.
 
 506       qq|SELECT doi.id AS delivery_order_items_id,
 
 507            p.partnumber, p.assembly, doi.description, doi.qty,
 
 508            doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.bin, p.notes AS partnotes,
 
 509            doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
 
 510            doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
 
 511            doi.price_factor_id, doi.price_factor, doi.marge_price_factor,
 
 514          FROM delivery_order_items doi
 
 515          JOIN parts p ON (doi.parts_id = p.id)
 
 516          JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
 
 517          LEFT JOIN project pr ON (doi.project_id = pr.id)
 
 518          LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
 519          WHERE doi.delivery_order_id = ?
 
 520          ORDER BY doi.$oid{$myconfig->{dbdriver}}|;
 
 522     $form->{form_details} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
 
 524     my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
 
 527       qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber
 
 528            FROM delivery_order_items_stock
 
 529            WHERE delivery_order_item_id = ?|;
 
 530     my $sth = prepare_query($form, $dbh, $query);
 
 532     foreach my $doi (@{ $form->{form_details} }) {
 
 533       do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
 
 535       while (my $ref = $sth->fetchrow_hashref()) {
 
 536         push @{ $requests }, $ref;
 
 539       $doi->{"stock_${in_out}"} = YAML::Dump($requests);
 
 546     $form->lastname_used($dbh, $myconfig, $form->{vc}) unless $form->{"$form->{vc}_id"};
 
 550   Common::webdav_folder($form) if ($main::webdav);
 
 552   $main::lxdebug->leave_sub();
 
 556   $main::lxdebug->enter_sub();
 
 560   my $myconfig = \%main::myconfig;
 
 561   my $form     = $main::form;
 
 563   # connect to database
 
 564   my $dbh = $form->get_standard_dbh($myconfig);
 
 574   my %oid = ('Pg'     => 'oid',
 
 575              'Oracle' => 'rowid');
 
 577   my (@project_ids, %projectnumbers);
 
 579   push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
 
 581   # sort items by partsgroup
 
 582   for $i (1 .. $form->{rowcount}) {
 
 584     if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
 
 585       $partsgroup = $form->{"partsgroup_$i"};
 
 587     push @partsgroup, [$i, $partsgroup];
 
 588     push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
 
 592     $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
 
 593       join(", ", map("?", @project_ids)) . ")";
 
 594     $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
 
 595     while (my $ref = $sth->fetchrow_hashref()) {
 
 596       $projectnumbers{$ref->{id}} = $ref->{projectnumber};
 
 601   $form->{"globalprojectnumber"} =
 
 602     $projectnumbers{$form->{"globalproject_id"}};
 
 604   my $q_pg     = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
 
 606                     JOIN parts p ON (a.parts_id = p.id)
 
 607                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
 
 609                       AND a.id = ? $sortorder|;
 
 610   my $h_pg     = prepare_query($form, $dbh, $q_pg);
 
 612   my $q_bin_wh = qq|SELECT (SELECT description FROM bin       WHERE id = ?) AS bin,
 
 613                            (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
 
 614   my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
 
 616   my $in_out   = $form->{type} =~ /^sales/ ? 'out' : 'in';
 
 621     qw(runningnumber number description longdescription qty unit
 
 622        partnotes serialnumber reqdate projectnumber
 
 623        si_runningnumber si_number si_description
 
 624        si_warehouse si_bin si_chargenumber si_qty si_unit);
 
 627   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
 
 630     next if (!$form->{"id_$i"});
 
 634     if ($item->[1] ne $sameitem) {
 
 635       push(@{ $form->{description} }, qq|$item->[1]|);
 
 636       $sameitem = $item->[1];
 
 638       map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
 
 641     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
 
 643     # add number, description and qty to $form->{number}, ....
 
 645     my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
 
 647     push @{ $form->{runningnumber} },   $position;
 
 648     push @{ $form->{number} },          $form->{"partnumber_$i"};
 
 649     push @{ $form->{description} },     $form->{"description_$i"};
 
 650     push @{ $form->{longdescription} }, $form->{"longdescription_$i"};
 
 651     push @{ $form->{qty} },             $form->format_amount($myconfig, $form->{"qty_$i"});
 
 652     push @{ $form->{unit} },            $form->{"unit_$i"};
 
 653     push @{ $form->{partnotes} },       $form->{"partnotes_$i"};
 
 654     push @{ $form->{serialnumber} },    $form->{"serialnumber_$i"};
 
 655     push @{ $form->{reqdate} },         $form->{"reqdate_$i"};
 
 656     push @{ $form->{projectnumber} },   $projectnumbers{$form->{"project_id_$i"}};
 
 658     if ($form->{"assembly_$i"}) {
 
 661       # get parts and push them onto the stack
 
 663       if ($form->{groupitems}) {
 
 665           qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
 
 667         $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
 
 670       do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
 
 672       while (my $ref = $h_pg->fetchrow_hashref(NAME_lc)) {
 
 673         if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
 
 674           map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
 
 675           $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
 
 676           push(@{ $form->{description} }, $sameitem);
 
 679         push(@{ $form->{description} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq|, $ref->{partnumber}, $ref->{description}|);
 
 681         map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
 
 685     if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
 
 686       my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
 
 688       foreach my $si (@{ $stock_info }) {
 
 691         do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
 
 692         my $bin_wh = $h_bin_wh->fetchrow_hashref();
 
 694         push @{ $form->{si_runningnumber} }, $num_si;
 
 695         push @{ $form->{si_number} },        $form->{"partnumber_$i"};
 
 696         push @{ $form->{si_description} },   $form->{"description_$i"};
 
 697         push @{ $form->{si_warehouse} },     $bin_wh->{warehouse};
 
 698         push @{ $form->{si_bin} },           $bin_wh->{bin};
 
 699         push @{ $form->{si_chargenumber} },  $si->{chargenumber};
 
 700         push @{ $form->{si_qty} },           $form->format_amount($myconfig, $si->{qty} * 1);
 
 701         push @{ $form->{si_unit} },          $si->{unit};
 
 709   $form->{username} = $myconfig->{name};
 
 711   $main::lxdebug->leave_sub();
 
 714 sub project_description {
 
 715   $main::lxdebug->enter_sub();
 
 717   my ($self, $dbh, $id) = @_;
 
 719   my $query = qq|SELECT description FROM project WHERE id = ?|;
 
 720   my ($value) = selectrow_query($form, $dbh, $query, $id);
 
 722   $main::lxdebug->leave_sub();
 
 727 sub unpack_stock_information {
 
 728   $main::lxdebug->enter_sub();
 
 733   Common::check_params_x(\%params, qw(packed));
 
 737   eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
 
 739   $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
 
 741   foreach my $entry (@{ $unpacked }) {
 
 742     next if ('HASH' eq ref $entry);
 
 747   $main::lxdebug->leave_sub();
 
 752 sub get_item_availability {
 
 753   $main::lxdebug->enter_sub();
 
 758   Common::check_params(\%params, qw(parts_id));
 
 760   my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
 
 761   my $form      = $main::form;
 
 764     qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, SUM(qty) AS qty, i.parts_id,
 
 765          w.description AS warehousedescription,
 
 766          b.description AS bindescription
 
 768        LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
 
 769        LEFT JOIN bin b       ON (i.bin_id       = b.id)
 
 770        WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
 
 772        GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.parts_id, w.description, b.description
 
 773        ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber)|;
 
 775   my $contents = selectall_hashref_query($form, $form->get_standard_dbh($myconfig), $query, @parts_ids);
 
 777   $main::lxdebug->leave_sub();
 
 779   return @{ $contents };
 
 783 sub check_stock_availability {
 
 784   $main::lxdebug->enter_sub();
 
 789   Common::check_params(\%params, qw(requests parts_id));
 
 791   my $myconfig    = \%main::myconfig;
 
 792   my $form        =  $main::form;
 
 794   my $dbh         = $form->get_standard_dbh($myconfig);
 
 796   my $units       = AM->retrieve_units($myconfig, $form, "dimension");
 
 798   my ($partunit)  = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
 
 799   my $unit_factor = $units->{$partunit}->{factor} || 1;
 
 801   my @contents    = $self->get_item_availability(%params);
 
 805   foreach my $sinfo (@{ $params{requests} }) {
 
 808     foreach my $row (@contents) {
 
 809       next if (($row->{bin_id}       != $sinfo->{bin_id}) ||
 
 810                ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
 
 811                ($row->{chargenumber} ne $sinfo->{chargenumber}));
 
 815       my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
 
 817       if ($base_qty > $row->{qty}) {
 
 819         push @errors, $sinfo;
 
 825     push @errors, $sinfo if (!$found);
 
 828   $main::lxdebug->leave_sub();
 
 833 sub transfer_in_out {
 
 834   $main::lxdebug->enter_sub();
 
 839   Common::check_params(\%params, qw(direction requests));
 
 841   if (!@{ $params{requests} }) {
 
 842     $main::lxdebug->leave_sub();
 
 846   my $myconfig = \%main::myconfig;
 
 847   my $form     = $main::form;
 
 849   my $prefix   = $params{direction} eq 'in' ? 'dst' : 'src';
 
 853   foreach my $request (@{ $params{requests} }) {
 
 855       'parts_id'               => $request->{parts_id},
 
 856       "${prefix}_warehouse_id" => $request->{warehouse_id},
 
 857       "${prefix}_bin_id"       => $request->{bin_id},
 
 858       'chargenumber'           => $request->{chargenumber},
 
 859       'qty'                    => $request->{qty},
 
 860       'unit'                   => $request->{unit},
 
 861       'oe_id'                  => $form->{id},
 
 862       'shippingdate'           => 'current_date',
 
 863       'transfer_type'          => $params{direction} eq 'in' ? 'stock' : 'shipped',
 
 867   WH->transfer(@transfers);
 
 869   $main::lxdebug->leave_sub();