1 #=====================================================================
2 # SQL-Ledger Accounting
5 # Author: Dieter Simader
6 # Email: dsimader@sql-ledger.org
7 # Web: http://www.sql-ledger.org
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
28 #======================================================================
34 my ($self, $myconfig, $form) = @_;
37 my $dbh = $form->dbconnect($myconfig);
40 my $ordnumber = 'ordnumber';
42 my ($null, $department_id) = split /--/, $form->{department};
44 my $department = " AND o.department_id = $department_id" if $department_id;
46 my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
48 ($form->{transdatefrom}, $form->{transdateto}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
50 if ($form->{type} =~ /_quotation$/) {
52 $ordnumber = 'quonumber';
55 my $number = $form->like(lc $form->{$ordnumber});
56 my $name = $form->like(lc $form->{$form->{vc}});
58 my $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
59 o.amount, ct.name, o.netamount, o.$form->{vc}_id,
60 ex.$rate AS exchangerate,
61 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
62 e.name AS employee, m.name AS manager, o.curr
64 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
65 LEFT JOIN employee e ON (o.employee_id = e.id)
66 LEFT JOIN employee m ON (e.managerid = m.id)
67 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
68 AND ex.transdate = o.transdate)
69 WHERE o.quotation = '$quotation'
72 my %ordinal = ( 'id' => 1,
83 my @a = (transdate, $ordnumber, name);
84 push @a, "employee" if $form->{l_employee};
85 if ($form->{type} !~ /(ship|receive)_order/) {
86 push @a, "manager" if $form->{l_manager};
88 my $sortorder = $form->sort_order(\@a, \%ordinal);
91 # build query if type eq (ship|receive)_order
92 if ($form->{type} =~ /(ship|receive)_order/) {
94 my ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
96 $query = qq|SELECT DISTINCT o.id, o.ordnumber, o.transdate,
97 o.reqdate, o.amount, ct.name, o.netamount, o.$form->{vc}_id,
98 ex.$rate AS exchangerate,
99 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
100 e.name AS employee, o.curr
102 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
103 JOIN orderitems oi ON (oi.trans_id = o.id)
104 JOIN parts p ON (p.id = oi.parts_id)|;
106 if ($warehouse_id && $form->{type} eq 'ship_order') {
108 JOIN inventory i ON (oi.parts_id = i.parts_id)
113 LEFT JOIN employee e ON (o.employee_id = e.id)
114 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
115 AND ex.transdate = o.transdate)
116 WHERE o.quotation = '0'
117 AND (p.inventory_accno_id > 0 OR p.assembly = '1')
118 AND oi.qty != oi.ship
121 if ($warehouse_id && $form->{type} eq 'ship_order') {
123 AND i.warehouse_id = $warehouse_id
124 AND i.qty >= (oi.qty - oi.ship)
130 if ($form->{"$form->{vc}_id"}) {
131 $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
133 if ($form->{$form->{vc}}) {
134 $query .= " AND lower(ct.name) LIKE '$name'";
137 if (!$form->{open} && !$form->{closed}) {
138 $query .= " AND o.id = 0";
139 } elsif (!($form->{open} && $form->{closed})) {
140 $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
143 if ($form->{$ordnumber}) {
144 $query .= " AND lower($ordnumber) LIKE '$number'";
146 if ($form->{shipvia}) {
147 $var = $form->like(lc $form->{shipvia});
148 $query .= " AND lower(o.shipvia) LIKE '$var'";
150 if ($form->{transdatefrom}) {
151 $query .= " AND o.transdate >= '$form->{transdatefrom}'";
153 if ($form->{transdateto}) {
154 $query .= " AND o.transdate <= '$form->{transdateto}'";
157 $query .= " ORDER by $sortorder";
159 my $sth = $dbh->prepare($query);
160 $sth->execute || $form->dberror($query);
163 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
164 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
165 push @{ $form->{OE} }, $ref if $ref->{id} != $id{$ref->{id}};
166 $id{$ref->{id}} = $ref->{id};
176 my ($self, $myconfig, $form) = @_;
178 # connect to database, turn off autocommit
179 my $dbh = $form->dbconnect_noauto($myconfig);
184 my $exchangerate = 0;
186 ($null, $form->{employee_id}) = split /--/, $form->{employee};
187 unless ($form->{employee_id}) {
188 ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
189 $form->{employee} = "$form->{employee}--$form->{employee_id}";
192 my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
196 &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
198 $query = qq|DELETE FROM orderitems
199 WHERE trans_id = $form->{id}|;
200 $dbh->do($query) || $form->dberror($query);
202 $query = qq|DELETE FROM shipto
203 WHERE trans_id = $form->{id}|;
204 $dbh->do($query) || $form->dberror($query);
208 $uid .= $form->{login};
210 $query = qq|INSERT INTO oe (ordnumber, employee_id)
211 VALUES ('$uid', $form->{employee_id})|;
212 $dbh->do($query) || $form->dberror($query);
214 $query = qq|SELECT id FROM oe
215 WHERE ordnumber = '$uid'|;
216 $sth = $dbh->prepare($query);
217 $sth->execute || $form->dberror($query);
219 ($form->{id}) = $sth->fetchrow_array;
236 for my $i (1 .. $form->{rowcount}) {
238 map { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(qty ship);
240 $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
241 $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
243 if ($form->{"qty_$i"}) {
245 $fxsellprice = $form->{"sellprice_$i"};
247 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
249 my $decimalplaces = ($dec > 2) ? $dec : 2;
251 $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"}, $decimalplaces);
252 $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
254 $form->{"inventory_accno_$i"} *= 1;
255 $form->{"expense_accno_$i"} *= 1;
257 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
259 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
263 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
265 if ($form->{taxincluded}) {
266 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
267 $taxbase = $linetotal - $taxamount;
268 # we are not keeping a natural price, do not round
269 $form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
271 $taxamount = $linetotal * $taxrate;
272 $taxbase = $linetotal;
275 if (@taxaccounts && $form->round_amount($taxamount, 2) == 0) {
276 if ($form->{taxincluded}) {
277 foreach $item (@taxaccounts) {
278 $taxamount = $form->round_amount($linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})), 2);
280 $taxaccounts{$item} += $taxamount;
281 $taxdiff += $taxamount;
283 $taxbase{$item} += $taxbase;
285 $taxaccounts{$taxaccounts[0]} += $taxdiff;
287 foreach $item (@taxaccounts) {
288 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
289 $taxbase{$item} += $taxbase;
293 foreach $item (@taxaccounts) {
294 $taxaccounts{$item} += $taxamount * $form->{"${item}_rate"} / $taxrate;
295 $taxbase{$item} += $taxbase;
300 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
302 $project_id = 'NULL';
303 if ($form->{"projectnumber_$i"}) {
304 ($null, $project_id) = split /--/, $form->{"projectnumber_$i"};
308 # save detail record in orderitems table
309 $query = qq|INSERT INTO orderitems (|;
310 $query .= "id, " if $form->{"orderitems_id_$i"};
311 $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
312 unit, reqdate, project_id, serialnumber, ship)
314 $query .= qq|$form->{"orderitems_id_$i"},| if $form->{"orderitems_id_$i"};
315 $query .= qq|$form->{id}, $form->{"id_$i"}, |
316 .$dbh->quote($form->{"description_$i"}).qq|,
317 $form->{"qty_$i"}, $fxsellprice, $form->{"discount_$i"}, |
318 .$dbh->quote($form->{"unit_$i"}).qq|, |
319 .$form->dbquote($form->{"reqdate_$i"}, SQL_DATE).qq|,
321 .$dbh->quote($form->{"serialnumber_$i"}).qq|,
322 $form->{"ship_$i"})|;
323 $dbh->do($query) || $form->dberror($query);
325 $form->{"sellprice_$i"} = $fxsellprice;
326 $form->{"discount_$i"} *= 100;
331 # set values which could be empty
332 map { $form->{$_} *= 1 } qw(vendor_id customer_id taxincluded closed quotation);
336 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
338 $amount = $form->round_amount($netamount + $tax, 2);
339 $netamount = $form->round_amount($netamount, 2);
341 if ($form->{currency} eq $form->{defaultcurrency}) {
342 $form->{exchangerate} = 1;
344 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, ($form->{vc} eq 'customer') ? 'buy' : 'sell');
347 $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
349 my $quotation = ($form->{type} =~ /_order$/) ? '0' : '1';
351 ($null, $form->{department_id}) = split(/--/, $form->{department});
352 $form->{department_id} *= 1;
355 $query = qq|UPDATE oe set
356 ordnumber = |.$dbh->quote($form->{ordnumber}).qq|,
357 quonumber = |.$dbh->quote($form->{quonumber}).qq|,
358 transdate = '$form->{transdate}',
359 vendor_id = $form->{vendor_id},
360 customer_id = $form->{customer_id},
362 netamount = $netamount,
363 reqdate = |.$form->dbquote($form->{reqdate}, SQL_DATE).qq|,
364 taxincluded = '$form->{taxincluded}',
365 shippingpoint = |.$dbh->quote($form->{shippingpoint}).qq|,
366 shipvia = |.$dbh->quote($form->{shipvia}).qq|,
367 notes = |.$dbh->quote($form->{notes}).qq|,
368 intnotes = |.$dbh->quote($form->{intnotes}).qq|,
369 curr = '$form->{currency}',
370 closed = '$form->{closed}',
371 quotation = '$quotation',
372 department_id = $form->{department_id},
373 employee_id = $form->{employee_id},
374 language_code = '$form->{language_code}'
375 WHERE id = $form->{id}|;
376 $dbh->do($query) || $form->dberror($query);
378 $form->{ordtotal} = $amount;
381 $form->{name} = $form->{$form->{vc}};
382 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
383 $form->add_shipto($dbh, $form->{id});
385 # save printed, emailed, queued
386 $form->save_status($dbh);
388 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
389 if ($form->{vc} eq 'customer') {
390 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0);
392 if ($form->{vc} eq 'vendor') {
393 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0, $form->{exchangerate});
398 if ($form->{type} =~ /_order$/) {
400 &adj_onhand($dbh, $form, $ml * -1);
401 &adj_inventory($dbh, $myconfig, $form);
404 my %audittrail = ( tablename => 'oe',
405 reference => ($form->{type} =~ /_order$/) ? $form->{ordnumber} : $form->{quonumber},
406 formname => $form->{type},
410 $form->audittrail($dbh, "", \%audittrail);
412 my $rc = $dbh->commit;
422 my ($self, $myconfig, $form, $spool) = @_;
424 # connect to database
425 my $dbh = $form->dbconnect_noauto($myconfig);
428 my $query = qq|SELECT spoolfile FROM status
429 WHERE trans_id = $form->{id}
430 AND spoolfile IS NOT NULL|;
431 $sth = $dbh->prepare($query);
432 $sth->execute || $form->dberror($query);
437 while (($spoolfile) = $sth->fetchrow_array) {
438 push @spoolfiles, $spoolfile;
443 $query = qq|SELECT o.parts_id, o.ship, p.inventory_accno_id
445 JOIN parts p ON (p.id = o.parts_id)
446 WHERE trans_id = $form->{id}|;
447 $sth = $dbh->prepare($query);
448 $sth->execute || $form->dberror($query);
450 if ($form->{type} =~ /_order$/) {
451 $ml = ($form->{type} eq 'purchase_order') ? -1 : 1;
452 while (my ($id, $ship, $inv) = $sth->fetchrow_array) {
453 $form->update_balance($dbh,
457 $ship * $ml) if $inv;
463 $query = qq|DELETE FROM inventory
464 WHERE oe_id = $form->{id}|;
465 $dbh->do($query) || $form->dberror($query);
467 # delete status entries
468 $query = qq|DELETE FROM status
469 WHERE trans_id = $form->{id}|;
470 $dbh->do($query) || $form->dberror($query);
473 $query = qq|DELETE FROM oe
474 WHERE id = $form->{id}|;
475 $dbh->do($query) || $form->dberror($query);
477 # delete individual entries
478 $query = qq|DELETE FROM orderitems
479 WHERE trans_id = $form->{id}|;
480 $dbh->do($query) || $form->dberror($query);
482 $query = qq|DELETE FROM shipto
483 WHERE trans_id = $form->{id}|;
484 $dbh->do($query) || $form->dberror($query);
486 my %audittrail = ( tablename => 'oe',
487 reference => ($form->{type} =~ /_order$/) ? $form->{ordnumber} : $form->{quonumber},
488 formname => $form->{type},
492 $form->audittrail($dbh, "", \%audittrail);
494 my $rc = $dbh->commit;
498 foreach $spoolfile (@spoolfiles) {
499 unlink "$spool/$spoolfile" if $spoolfile;
510 my ($self, $myconfig, $form) = @_;
512 # connect to database
513 my $dbh = $form->dbconnect($myconfig);
519 # get default accounts and last order number
520 $query = qq|SELECT (SELECT c.accno FROM chart c
521 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
522 (SELECT c.accno FROM chart c
523 WHERE d.income_accno_id = c.id) AS income_accno,
524 (SELECT c.accno FROM chart c
525 WHERE d.expense_accno_id = c.id) AS expense_accno,
526 (SELECT c.accno FROM chart c
527 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
528 (SELECT c.accno FROM chart c
529 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
533 $query = qq|SELECT (SELECT c.accno FROM chart c
534 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
535 (SELECT c.accno FROM chart c
536 WHERE d.income_accno_id = c.id) AS income_accno,
537 (SELECT c.accno FROM chart c
538 WHERE d.expense_accno_id = c.id) AS expense_accno,
539 (SELECT c.accno FROM chart c
540 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
541 (SELECT c.accno FROM chart c
542 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
543 d.curr AS currencies,
544 current_date AS transdate
547 my $sth = $dbh->prepare($query);
548 $sth->execute || $form->dberror($query);
550 my $ref = $sth->fetchrow_hashref(NAME_lc);
551 map { $form->{$_} = $ref->{$_} } keys %$ref;
558 $query = qq|SELECT o.ordnumber, o.transdate, o.reqdate,
559 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
560 o.curr AS currency, e.name AS employee, o.employee_id,
561 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
562 o.closed, o.reqdate, o.quonumber, o.department_id,
563 d.description AS department, o.language_code
565 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
566 LEFT JOIN employee e ON (o.employee_id = e.id)
567 LEFT JOIN department d ON (o.department_id = d.id)
568 WHERE o.id = $form->{id}|;
569 $sth = $dbh->prepare($query);
570 $sth->execute || $form->dberror($query);
572 $ref = $sth->fetchrow_hashref(NAME_lc);
573 map { $form->{$_} = $ref->{$_} } keys %$ref;
577 $query = qq|SELECT * FROM shipto
578 WHERE trans_id = $form->{id}|;
579 $sth = $dbh->prepare($query);
580 $sth->execute || $form->dberror($query);
582 $ref = $sth->fetchrow_hashref(NAME_lc);
583 map { $form->{$_} = $ref->{$_} } keys %$ref;
586 # get printed, emailed and queued
587 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
589 WHERE s.trans_id = $form->{id}|;
590 $sth = $dbh->prepare($query);
591 $sth->execute || $form->dberror($query);
593 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
594 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
595 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
596 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
599 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
602 my %oid = ( 'Pg' => 'oid',
608 # retrieve individual items
609 $query = qq|SELECT o.id AS orderitems_id,
610 c1.accno AS inventory_accno,
611 c2.accno AS income_accno,
612 c3.accno AS expense_accno,
613 p.partnumber, p.assembly, o.description, o.qty,
614 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin,
615 o.reqdate, o.project_id, o.serialnumber, o.ship,
617 pg.partsgroup, p.partsgroup_id, p.partnumber AS sku,
618 p.listprice, p.lastcost, p.weight,
619 t.description AS partsgrouptranslation
621 JOIN parts p ON (o.parts_id = p.id)
622 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
623 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
624 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
625 LEFT JOIN project pr ON (o.project_id = pr.id)
626 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
627 LEFT JOIN translation t ON (t.trans_id = p.partsgroup_id AND t.language_code = '$form->{language_code}')
628 WHERE o.trans_id = $form->{id}
629 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
630 $sth = $dbh->prepare($query);
631 $sth->execute || $form->dberror($query);
633 # foreign exchange rates
634 &exchangerate_defaults($dbh, $form);
636 # query for price matrix
637 my $pmh = &price_matrix_query($dbh, $form);
640 $query = qq|SELECT c.accno
642 JOIN partstax pt ON (pt.chart_id = c.id)
643 WHERE pt.parts_id = ?|;
644 my $tth = $dbh->prepare($query) || $form->dberror($query);
651 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
653 ($decimalplaces) = ($ref->{sellprice} =~ /\.(\d+)/);
654 $decimalplaces = length $decimalplaces;
655 $decimalplaces = 2 unless $decimalplaces;
657 $tth->execute($ref->{id});
658 $ref->{taxaccounts} = "";
661 while ($ptref = $tth->fetchrow_hashref(NAME_lc)) {
662 $ref->{taxaccounts} .= "$ptref->{accno} ";
663 $taxrate += $form->{"$ptref->{accno}_rate"};
666 chop $ref->{taxaccounts};
669 $sellprice = $ref->{sellprice};
670 $listprice = $ref->{listprice};
672 # multiply by exchangerate
673 $ref->{sellprice} = $form->round_amount($ref->{sellprice} * $form->{$form->{currency}}, $decimalplaces);
674 $ref->{listprice} = $form->round_amount($ref->{listprice} * $form->{$form->{currency}}, $decimalplaces);
676 # partnumber and price matrix
677 &price_matrix($pmh, $ref, $form->{transdate}, $decimalplaces, $form, $myconfig, 1);
679 $ref->{sellprice} = $sellprice;
680 $ref->{listprice} = $listprice;
682 $ref->{partsgroup} = $ref->{partsgrouptranslation} if $ref->{partsgrouptranslation};
684 push @{ $form->{form_details} }, $ref;
692 $form->lastname_used($dbh, $myconfig, $form->{vc}) unless $form->{"$form->{vc}_id"};
693 delete $form->{notes};
702 sub price_matrix_query {
703 my ($dbh, $form) = @_;
708 if ($form->{customer_id}) {
709 $query = qq|SELECT p.*, g.pricegroup
711 LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
713 AND p.customer_id = $form->{customer_id}
717 SELECT p.*, g.pricegroup
719 LEFT JOIN pricegroup g ON (g.id = p.pricegroup_id)
720 JOIN customer c ON (c.pricegroup_id = g.id)
722 AND c.id = $form->{customer_id}
726 SELECT p.*, '' AS pricegroup
728 WHERE p.customer_id = 0
729 AND p.pricegroup_id = 0
732 ORDER BY customer_id DESC, pricegroup_id DESC, pricebreak
734 $sth = $dbh->prepare($query) || $form->dberror($query);
737 if ($form->{vendor_id}) {
738 # price matrix and vendor's partnumber
739 $query = qq|SELECT partnumber
742 AND vendor_id = $form->{vendor_id}|;
743 $sth = $dbh->prepare($query) || $form->dberror($query);
752 my ($pmh, $ref, $transdate, $decimalplaces, $form, $myconfig, $init) = @_;
754 $ref->{pricematrix} = "";
755 my $customerprice = 0;
760 # depends if this is a customer or vendor
761 if ($form->{customer_id}) {
762 $pmh->execute($ref->{id}, $ref->{id}, $ref->{id});
764 while ($mref = $pmh->fetchrow_hashref(NAME_lc)) {
767 if ($mref->{validfrom}) {
768 next if $transdate < $form->datetonum($mref->{validfrom}, $myconfig);
770 if ($mref->{validto}) {
771 next if $transdate > $form->datetonum($mref->{validto}, $myconfig);
775 $sellprice = $form->round_amount($mref->{sellprice} * $form->{$mref->{curr}}, $decimalplaces);
777 if ($mref->{customer_id}) {
778 $ref->{sellprice} = $sellprice unless $mref->{pricebreak};
779 $ref->{pricematrix} .= "$mref->{pricebreak}:$sellprice ";
783 if ($mref->{pricegroup_id}) {
784 if (! $customerprice) {
785 $ref->{sellprice} = $sellprice unless $mref->{pricebreak};
786 $ref->{pricematrix} .= "$mref->{pricebreak}:$sellprice ";
791 if (! $customerprice && ! $pricegroup) {
792 $ref->{sellprice} = $sellprice unless $mref->{pricebreak};
793 $ref->{pricematrix} .= "$mref->{pricebreak}:$sellprice ";
799 if ($ref->{pricematrix} !~ /^0:/) {
801 $sellprice = $form->round_amount($ref->{sellprice}, $decimalplaces);
803 $sellprice = $form->round_amount($ref->{sellprice} * (1 - $form->{tradediscount}), $decimalplaces);
805 $ref->{pricematrix} = "0:$sellprice ".$ref->{pricematrix};
807 chop $ref->{pricematrix};
812 if ($form->{vendor_id}) {
813 $pmh->execute($ref->{id});
815 $mref = $pmh->fetchrow_hashref(NAME_lc);
817 if ($mref->{partnumber}) {
818 $ref->{partnumber} = $mref->{partnumber};
821 if ($mref->{lastcost}) {
823 $ref->{sellprice} = $form->round_amount($mref->{lastcost} * $form->{$mref->{curr}}, $decimalplaces);
827 $ref->{sellprice} *= 1;
829 # add 0:price to matrix
830 $ref->{pricematrix} = "0:$ref->{sellprice}";
837 sub exchangerate_defaults {
838 my ($dbh, $form) = @_;
841 my $buysell = ($form->{vc} eq "customer") ? "buy" : "sell";
843 # get default currencies
844 my $query = qq|SELECT substr(curr,1,3), curr FROM defaults|;
845 my $eth = $dbh->prepare($query) || $form->dberror($query);
847 ($form->{defaultcurrency}, $form->{currencies}) = $eth->fetchrow_array;
850 $query = qq|SELECT $buysell
854 my $eth1 = $dbh->prepare($query) || $form->dberror($query);
855 $query = qq~SELECT max(transdate || ' ' || $buysell || ' ' || curr)
858 my $eth2 = $dbh->prepare($query) || $form->dberror($query);
860 # get exchange rates for transdate or max
861 foreach $var (split /:/, substr($form->{currencies},4)) {
862 $eth1->execute($var, $form->{transdate});
863 ($form->{$var}) = $eth1->fetchrow_array;
864 if (! $form->{$var} ) {
865 $eth2->execute($var);
867 ($form->{$var}) = $eth2->fetchrow_array;
868 ($null, $form->{$var}) = split / /, $form->{$var};
869 $form->{$var} = 1 unless $form->{$var};
875 $form->{$form->{defaultcurrency}} = 1;
881 my ($self, $myconfig, $form) = @_;
883 # connect to database
884 my $dbh = $form->dbconnect($myconfig);
892 my $projectnumber_id;
896 my %oid = ( 'Pg' => 'oid',
902 # sort items by project and partsgroup
903 for $i (1 .. $form->{rowcount}) {
906 $projectnumber_id = 0;
907 if ($form->{"projectnumber_$i"} && $form->{groupprojectnumber}) {
908 ($projectnumber, $projectnumber_id) = split /--/, $form->{"projectnumber_$i"};
910 if ($form->{"partsgroup_$i"} && $form->{grouppartsgroup}) {
911 ($partsgroup) = split /--/, $form->{"partsgroup_$i"};
913 push @sortlist, [ $i, "$projectnumber$partsgroup", $projectnumber, $projectnumber_id, $partsgroup ];
915 # sort the whole thing by project and group
916 @sortlist = sort { $a->[1] cmp $b->[1] } @sortlist;
920 # if there is a warehouse limit picking
921 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
922 # run query to check for inventory
923 $query = qq|SELECT sum(qty) AS qty
926 AND warehouse_id = ?|;
927 $sth = $dbh->prepare($query) || $form->dberror($query);
929 for $i (1 .. $form->{rowcount}) {
930 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
932 ($qty) = $sth->fetchrow_array;
935 $form->{"qty_$i"} = 0 if $qty == 0;
937 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
938 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
950 $query = qq|SELECT p.description, t.description
952 LEFT JOIN translation t ON (t.trans_id = p.id AND t.language_code = '$form->{language_code}')
954 my $prh = $dbh->prepare($query) || $form->dberror($query);
956 my $runningnumber = 1;
959 my $k = scalar @sortlist;
962 foreach $item (@sortlist) {
966 if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
967 if ($item->[1] ne $sameitem) {
970 if ($form->{groupprojectnumber} && $item->[2]) {
971 # get project description
972 $prh->execute($item->[3]) || $form->dberror($query);
974 ($projectnumber, $translation) = $prh->fetchrow_array;
977 $projectnumber = ($translation) ? "$item->[2], $translation" : "$item->[2], $projectnumber";
980 if ($form->{grouppartsgroup} && $item->[4]) {
981 $projectnumber .= " / " if $projectnumber;
982 $projectnumber .= $item->[4];
985 $form->{projectnumber} = $projectnumber;
986 $form->format_string(projectnumber);
988 push(@{ $form->{description} }, qq|$form->{projectnumber}|);
989 $sameitem = $item->[1];
991 map { push(@{ $form->{$_} }, "") } qw(runningnumber number sku qty ship unit bin serialnumber reqdate projectnumber sellprice listprice netprice discount discountrate linetotal weight);
995 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
997 if ($form->{"qty_$i"} != 0) {
999 $form->{totalqty} += $form->{"qty_$i"};
1000 $form->{totalship} += $form->{"ship_$i"};
1001 $form->{totalweight} += ($form->{"weight_$i"} * $form->{"qty_$i"});
1003 # add number, description and qty to $form->{number}, ....
1004 push(@{ $form->{runningnumber} }, $runningnumber++);
1005 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
1006 push(@{ $form->{sku} }, qq|$form->{"sku_$i"}|);
1007 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
1008 push(@{ $form->{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"}));
1009 push(@{ $form->{ship} }, $form->format_amount($myconfig, $form->{"ship_$i"}));
1010 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
1011 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
1012 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
1013 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
1014 push(@{ $form->{projectnumber} }, qq|$form->{"projectnumber_$i"}|);
1016 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
1018 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
1020 push(@{ $form->{weight} }, $form->{"weight_$i"});
1022 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
1023 my ($dec) = ($sellprice =~ /\.(\d+)/);
1025 my $decimalplaces = ($dec > 2) ? $dec : 2;
1027 my $discount = $form->round_amount($sellprice * $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100, $decimalplaces);
1029 # keep a netprice as well, (sellprice - discount)
1030 $form->{"netprice_$i"} = $sellprice - $discount;
1032 my $linetotal = $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
1034 push(@{ $form->{netprice} }, ($form->{"netprice_$i"} != 0) ? $form->format_amount($myconfig, $form->{"netprice_$i"}, $decimalplaces) : " ");
1036 $discount = ($discount != 0) ? $form->format_amount($myconfig, $discount * -1, $decimalplaces) : " ";
1037 $linetotal = ($linetotal != 0) ? $linetotal : " ";
1039 push(@{ $form->{discount} }, $discount);
1040 push(@{ $form->{discountrate} }, $form->format_amount($myconfig, $form->{"discount_$i"}));
1042 $form->{ordtotal} += $linetotal;
1044 # this is for the subtotals for grouping
1045 $subtotal += $linetotal;
1047 push(@{ $form->{linetotal} }, $form->format_amount($myconfig, $linetotal, 2));
1051 map { $taxrate += $form->{"${_}_rate"} } split / /, $form->{"taxaccounts_$i"};
1053 if ($form->{taxincluded}) {
1055 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
1056 $taxbase = $linetotal / (1 + $taxrate);
1058 $taxamount = $linetotal * $taxrate;
1059 $taxbase = $linetotal;
1063 if ($form->round_amount($taxamount, 2) != 0) {
1064 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
1065 $taxaccounts{$item} += $taxamount * $form->{"${item}_rate"} / $taxrate;
1066 $taxbase{$item} += $taxbase;
1070 if ($form->{"assembly_$i"}) {
1071 $form->{stagger} = -1;
1072 &assembly_details($dbh, $form, $form->{"id_$i"}, $oid{$myconfig->{dbdriver}}, $form->{"qty_$i"});
1078 if ($form->{groupprojectnumber} || $form->{grouppartsgroup}) {
1082 if ($sortlist[$j]->[1] ne $sameitem) {
1084 map { push(@{ $form->{$_} }, "") } qw(runningnumber number sku qty ship unit bin serialnumber reqdate projectnumber sellprice listprice netprice discount discountrate weight);
1086 push(@{ $form->{description} }, $form->{groupsubtotaldescription});
1088 if (exists $form->{groupsubtotaldescription}) {
1089 push(@{ $form->{linetotal} }, $form->format_amount($myconfig, $subtotal, 2));
1091 push(@{ $form->{linetotal} }, "");
1100 if (exists $form->{groupsubtotaldescription}) {
1102 map { push(@{ $form->{$_} }, "") } qw(runningnumber number sku qty ship unit bin serialnumber reqdate projectnumber sellprice listprice netprice discount discountrate weight);
1104 push(@{ $form->{description} }, $form->{groupsubtotaldescription});
1105 push(@{ $form->{linetotal} }, $form->format_amount($myconfig, $subtotal, 2));
1114 foreach $item (sort keys %taxaccounts) {
1115 if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
1116 push(@{ $form->{taxbase} }, $form->format_amount($myconfig, $taxbase{$item}, 2));
1118 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
1120 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
1121 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
1122 push(@{ $form->{taxrate} }, $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
1123 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
1127 map { $form->{$_} = $form->format_amount($myconfig, $form->{$_}) } qw(totalqty totalship totalweight);
1128 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1129 $form->{ordtotal} = ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
1133 if ($form->{language_code}) {
1134 $c = new CP $form->{language_code};
1136 $c = new CP $myconfig->{countrycode};
1140 ($whole, $form->{decimal}) = split /\./, $form->{ordtotal};
1141 $form->{decimal} .= "00";
1142 $form->{decimal} = substr($form->{decimal}, 0, 2);
1143 $form->{text_amount} = $c->num2text($whole);
1146 $form->{quototal} = $form->{ordtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1153 sub assembly_details {
1154 my ($dbh, $form, $id, $oid, $qty) = @_;
1160 if ($form->{format} eq 'html') {
1161 $spacer = " " x (3 * ($form->{stagger} - 1)) if $form->{stagger} > 1;
1163 if ($form->{format} =~ /(postscript|pdf)/) {
1164 if ($form->{stagger} > 1) {
1165 $spacer = ($form->{stagger} - 1) * 3;
1166 $spacer = '\rule{'.$spacer.'mm}{0mm}';
1170 # get parts and push them onto the stack
1173 if ($form->{grouppartsgroup}) {
1174 $sortorder = qq|ORDER BY pg.partsgroup, a.$oid|;
1176 $sortorder = qq|ORDER BY a.$oid|;
1179 my $where = ($form->{formname} eq 'work_order') ? "1 = 1" : "a.bom = '1'";
1181 my $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
1182 pg.partsgroup, p.partnumber AS sku, p.assembly, p.id, p.bin
1184 JOIN parts p ON (a.parts_id = p.id)
1185 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1189 my $sth = $dbh->prepare($query);
1190 $sth->execute || $form->dberror($query);
1192 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1194 if ($form->{grouppartsgroup} && $ref->{partsgroup} ne $sm) {
1195 map { push(@{ $form->{$_} }, "") } qw(number sku unit qty runningnumber ship bin serialnumber reqdate projectnumber sellprice listprice netprice discount discountrate linetotal);
1196 $sm = ($ref->{partsgroup}) ? $ref->{partsgroup} : "";
1197 push(@{ $form->{description} }, "$spacer$sm");
1200 if ($form->{stagger}) {
1201 push(@{ $form->{description} }, qq|$spacer$ref->{sku}, $ref->{description}|);
1202 map { push(@{ $form->{$_} }, "") } qw(number sku runningnumber ship serialnumber reqdate projectnumber sellprice listprice netprice discount discountrate linetotal);
1204 push(@{ $form->{description} }, qq|$ref->{description}|);
1205 push(@{ $form->{sku} }, $ref->{partnumber});
1206 push(@{ $form->{number} }, $ref->{partnumber});
1208 map { push(@{ $form->{$_} }, "") } qw(runningnumber ship serialnumber reqdate projectnumber sellprice listprice netprice discount discountrate linetotal);
1211 push(@{ $form->{qty} }, $form->format_amount($myconfig, $ref->{qty} * $qty));
1212 map { push(@{ $form->{$_} }, $ref->{$_}) } qw(unit bin);
1215 if ($ref->{assembly} && $form->{formname} eq 'work_order') {
1216 &assembly_details($dbh, $form, $ref->{id}, $oid, $ref->{qty} * $qty);
1227 sub project_description {
1228 my ($self, $dbh, $id) = @_;
1230 my $query = qq|SELECT description
1233 ($_) = $dbh->selectrow_array;
1240 sub get_warehouses {
1241 my ($self, $myconfig, $form) = @_;
1243 my $dbh = $form->dbconnect($myconfig);
1245 my $query = qq|SELECT id, description
1249 my $sth = $dbh->prepare($query);
1250 $sth->execute || $form->dberror($query);
1252 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1253 push @{ $form->{all_warehouses} }, $ref;
1262 sub save_inventory {
1263 my ($self, $myconfig, $form) = @_;
1265 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1268 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
1270 my $dbh = $form->dbconnect_noauto($myconfig);
1277 ($null, $employee_id) = split /--/, $form->{employee};
1278 ($null, $employee_id) = $form->get_employee($dbh) if ! $employee_id;
1280 $query = qq|SELECT serialnumber, ship
1285 $sth = $dbh->prepare($query) || $form->dberror($query);
1287 $query = qq|SELECT sum(qty)
1290 AND warehouse_id = ?|;
1291 $wth = $dbh->prepare($query) || $form->dberror($query);
1294 for my $i (1 .. $form->{rowcount}) {
1296 $ship = (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"})) ? $form->{"qty_$i"} : $form->{"ship_$i"};
1298 if ($warehouse_id && $form->{type} eq 'ship_order') {
1300 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1302 ($qty) = $wth->fetchrow_array;
1314 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1315 qty, oe_id, orderitems_id, shippingdate, employee_id)
1316 VALUES ($form->{"id_$i"}, $warehouse_id,
1317 $ship, $form->{"id"},
1318 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1320 $dbh->do($query) || $form->dberror($query);
1322 # add serialnumber, ship to orderitems
1323 $sth->execute($form->{id}, $form->{"orderitems_id_$i"}) || $form->dberror;
1324 ($serialnumber, $ship) = $sth->fetchrow_array;
1327 $serialnumber .= " " if $serialnumber;
1328 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1329 $ship += $form->{"ship_$i"};
1331 $query = qq|UPDATE orderitems SET
1332 serialnumber = '$serialnumber',
1334 reqdate = '$form->{shippingdate}'
1335 WHERE trans_id = $form->{id}
1336 AND id = $form->{"orderitems_id_$i"}|;
1337 $dbh->do($query) || $form->dberror($query);
1340 # update order with ship via
1341 $query = qq|UPDATE oe SET
1342 shippingpoint = '$form->{shippingpoint}',
1343 shipvia = '$form->{shipvia}'
1344 WHERE id = $form->{id}|;
1345 $dbh->do($query) || $form->dberror($query);
1348 # update onhand for parts
1349 $form->update_balance($dbh,
1352 qq|id = $form->{"id_$i"}|,
1353 $form->{"ship_$i"} * $ml);
1358 my $rc = $dbh->commit;
1367 my ($dbh, $form, $ml) = @_;
1369 my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
1371 JOIN parts p ON (p.id = oi.parts_id)
1372 WHERE oi.trans_id = $form->{id}|;
1373 my $sth = $dbh->prepare($query);
1374 $sth->execute || $form->dberror($query);
1376 $query = qq|SELECT sum(p.inventory_accno_id)
1378 JOIN assembly a ON (a.parts_id = p.id)
1380 my $ath = $dbh->prepare($query) || $form->dberror($query);
1385 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1387 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1389 # do not update if assembly consists of all services
1390 if ($ref->{assembly}) {
1391 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1393 ($ispa) = $ath->fetchrow_array;
1400 # adjust onhand in parts table
1401 $form->update_balance($dbh,
1404 qq|id = $ref->{parts_id}|,
1405 $ref->{ship} * $ml);
1415 my ($dbh, $myconfig, $form) = @_;
1417 my %oid = ( 'Pg' => 'oid',
1419 'Oracle' => 'rowid',
1423 # increase/reduce qty in inventory table
1424 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1426 WHERE oi.trans_id = $form->{id}|;
1427 my $sth = $dbh->prepare($query);
1428 $sth->execute || $form->dberror($query);
1430 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1431 (SELECT SUM(qty) FROM inventory
1432 WHERE oe_id = $form->{id}
1433 AND orderitems_id = ?) AS total
1435 WHERE oe_id = $form->{id}
1436 AND orderitems_id = ?|;
1437 my $ith = $dbh->prepare($query) || $form->dberror($query);
1440 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1442 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1444 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1446 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1448 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1449 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1451 $form->update_balance($dbh,
1454 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1463 # delete inventory entries if qty = 0
1464 $query = qq|DELETE FROM inventory
1465 WHERE oe_id = $form->{id}
1467 $dbh->do($query) || $form->dberror($query);
1473 my ($self, $myconfig, $form) = @_;
1475 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1478 my $dbh = $form->dbconnect($myconfig);
1480 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1483 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1484 WHERE p.onhand > 0|;
1486 if ($form->{partnumber}) {
1487 $var = $form->like(lc $form->{partnumber});
1489 AND lower(p.partnumber) LIKE '$var'";
1491 if ($form->{description}) {
1492 $var = $form->like(lc $form->{description});
1494 AND lower(p.description) LIKE '$var'";
1496 if ($form->{partsgroup}) {
1497 $var = $form->like(lc $form->{partsgroup});
1499 AND lower(pg.partsgroup) LIKE '$var'";
1502 $sth = $dbh->prepare($query);
1503 $sth->execute || $form->dberror($query);
1506 $query = qq|SELECT sum(i.qty), w.description, w.id
1508 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1509 WHERE i.parts_id = ?
1510 AND i.warehouse_id != $warehouse_id
1511 GROUP BY w.description, w.id|;
1512 $wth = $dbh->prepare($query) || $form->dberror($query);
1514 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1516 $wth->execute($ref->{id}) || $form->dberror;
1518 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1519 push @{ $form->{all_inventory} }, {'id' => $ref->{id},
1520 'partnumber' => $ref->{partnumber},
1521 'description' => $ref->{description},
1522 'partsgroup' => $ref->{partsgroup},
1524 'warehouse_id' => $warehouse_id,
1525 'warehouse' => $warehouse} if $qty > 0;
1534 @{ $form->{all_inventory} } = sort { $a->{$form->{sort}} cmp $b->{$form->{sort}} } @{ $form->{all_inventory} };
1540 my ($self, $myconfig, $form) = @_;
1542 my $dbh = $form->dbconnect_noauto($myconfig);
1544 my $query = qq|INSERT INTO inventory
1545 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1546 VALUES (?, ?, ?, ?, ?)|;
1547 $sth = $dbh->prepare($query) || $form->dberror($query);
1549 ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
1551 my @a = localtime; $a[5] += 1900; $a[4]++;
1552 $shippingdate = "$a[5]-$a[4]-$a[3]";
1554 for my $i (1 .. $form->{rowcount}) {
1555 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1557 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1561 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty, $shippingdate, $form->{employee_id}) || $form->dberror;
1566 $sth->execute($form->{"warehouse_id_$i"}, $form->{"id_$i"}, $qty * -1, $shippingdate, $form->{employee_id}) || $form->dberror;
1572 my $rc = $dbh->commit;