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 #======================================================================
33 #======================================================================
38 $main::lxdebug->enter_sub();
40 my ($self, $myconfig, $form) = @_;
43 my $dbh = $form->dbconnect($myconfig);
46 my $ordnumber = 'ordnumber';
48 my ($null, $department_id) = split /--/, $form->{department};
50 my $department = " AND o.department_id = $department_id" if $department_id;
52 my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
54 if ($form->{type} =~ /_quotation$/) {
56 $ordnumber = 'quonumber';
59 my $number = $form->like(lc $form->{$ordnumber});
60 my $name = $form->like(lc $form->{ $form->{vc} });
62 my $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
63 o.amount, ct.name, o.netamount, o.$form->{vc}_id,
64 ex.$rate AS exchangerate,
65 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
68 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
69 LEFT JOIN employee e ON (o.employee_id = e.id)
70 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
71 AND ex.transdate = o.transdate)
72 WHERE o.quotation = '$quotation'
75 # build query if type eq (ship|receive)_order
76 if ($form->{type} =~ /(ship|receive)_order/) {
77 my ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
79 $query = qq|SELECT DISTINCT ON (o.id) o.id, o.ordnumber, o.transdate,
80 o.reqdate, o.amount, ct.name, o.netamount, o.$form->{vc}_id,
81 ex.$rate AS exchangerate,
82 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
85 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
86 JOIN orderitems oi ON (oi.trans_id = o.id)
87 JOIN parts p ON (p.id = oi.parts_id)|;
89 if ($warehouse_id && $form->{type} eq 'ship_order') {
91 JOIN inventory i ON (oi.parts_id = i.parts_id)
96 LEFT JOIN employee e ON (o.employee_id = e.id)
97 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
98 AND ex.transdate = o.transdate)
99 WHERE o.quotation = '0'
100 AND (p.inventory_accno_id > 0 OR p.assembly = '1')
101 AND oi.qty <> oi.ship
104 if ($warehouse_id && $form->{type} eq 'ship_order') {
106 AND i.warehouse_id = $warehouse_id
107 AND i.qty >= (oi.qty - oi.ship)
113 if ($form->{"$form->{vc}_id"}) {
114 $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
116 if ($form->{ $form->{vc} }) {
117 $query .= " AND lower(ct.name) LIKE '$name'";
120 if (!$form->{open} && !$form->{closed}) {
121 $query .= " AND o.id = 0";
122 } elsif (!($form->{open} && $form->{closed})) {
123 $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
126 my $sortorder = join ', ',
127 ("o.id", $form->sort_columns(transdate, $ordnumber, name));
128 $sortorder = $form->{sort} unless $sortorder;
130 $query .= " AND lower($ordnumber) LIKE '$number'" if $form->{$ordnumber};
131 $query .= " AND o.transdate >= '$form->{transdatefrom}'"
132 if $form->{transdatefrom};
133 $query .= " AND o.transdate <= '$form->{transdateto}'"
134 if $form->{transdateto};
135 $query .= " ORDER by $sortorder";
137 my $sth = $dbh->prepare($query);
138 $sth->execute || $form->dberror($query);
141 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
142 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
143 push @{ $form->{OE} }, $ref if $ref->{id} != $id{ $ref->{id} };
144 $id{ $ref->{id} } = $ref->{id};
150 $main::lxdebug->leave_sub();
154 $main::lxdebug->enter_sub();
156 my ($self, $myconfig, $form) = @_;
158 # connect to database, turn off autocommit
159 my $dbh = $form->dbconnect_noauto($myconfig);
161 my ($query, $sth, $null);
162 my $exchangerate = 0;
164 ($null, $form->{employee_id}) = split /--/, $form->{employee};
165 unless ($form->{employee_id}) {
166 $form->get_employee($dbh);
169 ($null, $form->{contact_id}) = split /--/, $form->{contact};
170 $form->{contact_id} *= 1;
172 my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
176 &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
178 $query = qq|DELETE FROM orderitems
179 WHERE trans_id = $form->{id}|;
180 $dbh->do($query) || $form->dberror($query);
182 $query = qq|DELETE FROM shipto
183 WHERE trans_id = $form->{id}|;
184 $dbh->do($query) || $form->dberror($query);
188 my $uid = rand() . time;
190 $uid .= $form->{login};
192 $uid = substr($uid, 2, 75);
194 $query = qq|INSERT INTO oe (ordnumber, employee_id)
195 VALUES ('$uid', $form->{employee_id})|;
196 $dbh->do($query) || $form->dberror($query);
198 $query = qq|SELECT o.id FROM oe o
199 WHERE o.ordnumber = '$uid'|;
200 $sth = $dbh->prepare($query);
201 $sth->execute || $form->dberror($query);
203 ($form->{id}) = $sth->fetchrow_array;
207 map { $form->{$_} =~ s/\'/\'\'/g }
208 qw(ordnumber quonumber shippingpoint shipvia notes intnotes message);
223 for my $i (1 .. $form->{rowcount}) {
226 $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"})
229 if ($form->{"qty_$i"}) {
231 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
232 qw(partnumber description unit);
234 # set values to 0 if nothing entered
235 $form->{"discount_$i"} =
236 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
238 $form->{"sellprice_$i"} =
239 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
240 $fxsellprice = $form->{"sellprice_$i"};
242 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
244 my $decimalplaces = ($dec > 2) ? $dec : 2;
247 $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
249 $form->{"sellprice_$i"} =
250 $form->round_amount($form->{"sellprice_$i"} - $discount,
253 $form->{"inventory_accno_$i"} *= 1;
254 $form->{"expense_accno_$i"} *= 1;
257 $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;
269 # we are not keeping a natural price, do not round
270 $form->{"sellprice_$i"} =
271 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
273 $taxamount = $linetotal * $taxrate;
274 $taxbase = $linetotal;
277 if ($form->round_amount($taxrate, 7) == 0) {
278 if ($form->{taxincluded}) {
279 foreach $item (@taxaccounts) {
281 $form->round_amount($linetotal * $form->{"${item}_rate"} /
282 (1 + abs($form->{"${item}_rate"})),
285 $taxaccounts{$item} += $taxamount;
286 $taxdiff += $taxamount;
288 $taxbase{$item} += $taxbase;
290 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
292 foreach $item (@taxaccounts) {
293 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
294 $taxbase{$item} += $taxbase;
298 foreach $item (@taxaccounts) {
299 $taxaccounts{$item} +=
300 $taxamount * $form->{"${item}_rate"} / $taxrate;
301 $taxbase{$item} += $taxbase;
305 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
307 $project_id = 'NULL';
308 if ($form->{"projectnumber_$i"}) {
309 $project_id = $form->{"projectnumber_$i"};
312 ($form->{"reqdate_$i"}) ? qq|'$form->{"reqdate_$i"}'| : "NULL";
314 # get pricegroup_id and save ist
315 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_drag_$i"};
317 # save detail record in orderitems table
318 $query = qq|INSERT INTO orderitems (|;
319 $query .= "id, " if $form->{"orderitems_id_$i"};
320 $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
321 unit, reqdate, project_id, serialnumber, ship, pricegroup_id)
323 $query .= qq|$form->{"orderitems_id_$i"},|
324 if $form->{"orderitems_id_$i"};
325 $query .= qq|$form->{id}, $form->{"id_$i"},
326 '$form->{"description_$i"}', $form->{"qty_$i"},
327 $fxsellprice, $form->{"discount_$i"},
328 '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
329 '$form->{"serialnumber_$i"}', $form->{"ship_$i"},
331 $dbh->do($query) || $form->dberror($query);
333 $form->{"sellprice_$i"} = $fxsellprice;
334 $form->{"discount_$i"} *= 100;
338 # set values which could be empty
339 map { $form->{$_} *= 1 }
340 qw(vendor_id customer_id taxincluded closed quotation);
342 $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
346 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
348 $amount = $form->round_amount($netamount + $tax, 2);
349 $netamount = $form->round_amount($netamount, 2);
351 if ($form->{currency} eq $form->{defaultcurrency}) {
352 $form->{exchangerate} = 1;
355 $form->check_exchangerate($myconfig,
358 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
361 $form->{exchangerate} =
364 : $form->parse_amount($myconfig, $form->{exchangerate});
368 # fill in subject if there is none
369 if ($form->{type} =~ /_order$/) {
371 $form->{subject} = qq|$form->{label} $form->{ordnumber}|
372 unless $form->{subject};
375 $form->{subject} = qq|$form->{label} $form->{quonumber}|
376 unless $form->{subject};
379 # if there is a message stuff it into the intnotes
380 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
381 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
382 my $now = scalar localtime;
383 $form->{intnotes} .= qq|\r
384 \r| if $form->{intnotes};
386 $form->{intnotes} .= qq|[email]\r
389 $cc${bcc}Subject: $form->{subject}\r
391 Message: $form->{message}\r| if $form->{message};
393 ($null, $form->{department_id}) = split(/--/, $form->{department});
394 $form->{department_id} *= 1;
397 $query = qq|UPDATE oe set
398 ordnumber = '$form->{ordnumber}',
399 quonumber = '$form->{quonumber}',
400 cusordnumber = '$form->{cusordnumber}',
401 transdate = '$form->{transdate}',
402 vendor_id = $form->{vendor_id},
403 customer_id = $form->{customer_id},
405 netamount = $netamount,
407 taxincluded = '$form->{taxincluded}',
408 shippingpoint = '$form->{shippingpoint}',
409 shipvia = '$form->{shipvia}',
410 notes = '$form->{notes}',
411 intnotes = '$form->{intnotes}',
412 curr = '$form->{currency}',
413 closed = '$form->{closed}',
414 quotation = '$quotation',
415 department_id = $form->{department_id},
416 employee_id = $form->{employee_id},
417 cp_id = $form->{contact_id}
418 WHERE id = $form->{id}|;
419 $dbh->do($query) || $form->dberror($query);
421 $form->{ordtotal} = $amount;
423 if ($form->{webdav}) {
424 &webdav_folder($myconfig, $form);
428 $form->{name} = $form->{ $form->{vc} };
429 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
430 $form->add_shipto($dbh, $form->{id});
432 # save printed, emailed, queued
433 $form->save_status($dbh);
435 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
436 if ($form->{vc} eq 'customer') {
437 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
438 $form->{exchangerate}, 0);
440 if ($form->{vc} eq 'vendor') {
441 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
442 0, $form->{exchangerate});
446 if ($form->{type} =~ /_order$/) {
449 &adj_onhand($dbh, $form, $ml * -1);
450 &adj_inventory($dbh, $myconfig, $form);
453 my $rc = $dbh->commit;
456 $main::lxdebug->leave_sub();
462 $main::lxdebug->enter_sub();
464 my ($self, $myconfig, $form, $spool) = @_;
466 # connect to database
467 my $dbh = $form->dbconnect_noauto($myconfig);
470 my $query = qq|SELECT s.spoolfile FROM status s
471 WHERE s.trans_id = $form->{id}|;
472 $sth = $dbh->prepare($query);
473 $sth->execute || $self->dberror($query);
478 while (($spoolfile) = $sth->fetchrow_array) {
479 push @spoolfiles, $spoolfile;
483 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
484 WHERE o.trans_id = $form->{id}|;
485 $sth = $dbh->prepare($query);
486 $sth->execute || $self->dberror($query);
488 while (my ($id, $ship) = $sth->fetchrow_array) {
489 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
494 $query = qq|DELETE FROM inventory
495 WHERE oe_id = $form->{id}|;
496 $dbh->do($query) || $form->dberror($query);
498 # delete status entries
499 $query = qq|DELETE FROM status
500 WHERE trans_id = $form->{id}|;
501 $dbh->do($query) || $form->dberror($query);
504 $query = qq|DELETE FROM oe
505 WHERE id = $form->{id}|;
506 $dbh->do($query) || $form->dberror($query);
508 # delete individual entries
509 $query = qq|DELETE FROM orderitems
510 WHERE trans_id = $form->{id}|;
511 $dbh->do($query) || $form->dberror($query);
513 $query = qq|DELETE FROM shipto
514 WHERE trans_id = $form->{id}|;
515 $dbh->do($query) || $form->dberror($query);
517 my $rc = $dbh->commit;
521 foreach $spoolfile (@spoolfiles) {
522 unlink "$spool/$spoolfile" if $spoolfile;
526 $main::lxdebug->leave_sub();
532 $main::lxdebug->enter_sub();
534 my ($self, $myconfig, $form) = @_;
536 # connect to database
537 my $dbh = $form->dbconnect_noauto($myconfig);
543 # get default accounts and last order number
544 $query = qq|SELECT (SELECT c.accno FROM chart c
545 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
546 (SELECT c.accno FROM chart c
547 WHERE d.income_accno_id = c.id) AS income_accno,
548 (SELECT c.accno FROM chart c
549 WHERE d.expense_accno_id = c.id) AS expense_accno,
550 (SELECT c.accno FROM chart c
551 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
552 (SELECT c.accno FROM chart c
553 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
557 $query = qq|SELECT (SELECT c.accno FROM chart c
558 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
559 (SELECT c.accno FROM chart c
560 WHERE d.income_accno_id = c.id) AS income_accno,
561 (SELECT c.accno FROM chart c
562 WHERE d.expense_accno_id = c.id) AS expense_accno,
563 (SELECT c.accno FROM chart c
564 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
565 (SELECT c.accno FROM chart c
566 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
567 d.curr AS currencies,
568 current_date AS transdate, current_date AS reqdate
571 my $sth = $dbh->prepare($query);
572 $sth->execute || $form->dberror($query);
574 my $ref = $sth->fetchrow_hashref(NAME_lc);
575 map { $form->{$_} = $ref->{$_} } keys %$ref;
578 ($form->{currency}) = split /:/, $form->{currencies};
583 $query = qq|SELECT o.cp_id,o.ordnumber, o.transdate, o.reqdate,
584 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
585 o.curr AS currency, e.name AS employee, o.employee_id,
586 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
587 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
588 d.description AS department
590 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
591 LEFT JOIN employee e ON (o.employee_id = e.id)
592 LEFT JOIN department d ON (o.department_id = d.id)
593 WHERE o.id = $form->{id}|;
594 $sth = $dbh->prepare($query);
595 $sth->execute || $form->dberror($query);
597 $ref = $sth->fetchrow_hashref(NAME_lc);
598 map { $form->{$_} = $ref->{$_} } keys %$ref;
601 $query = qq|SELECT s.* FROM shipto s
602 WHERE s.trans_id = $form->{id}|;
603 $sth = $dbh->prepare($query);
604 $sth->execute || $form->dberror($query);
606 $ref = $sth->fetchrow_hashref(NAME_lc);
607 map { $form->{$_} = $ref->{$_} } keys %$ref;
610 # get printed, emailed and queued
611 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
613 WHERE s.trans_id = $form->{id}|;
614 $sth = $dbh->prepare($query);
615 $sth->execute || $form->dberror($query);
617 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
618 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
619 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
620 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
621 if $ref->{spoolfile};
624 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
626 my %oid = ('Pg' => 'oid',
627 'Oracle' => 'rowid');
629 # retrieve individual items
630 $query = qq|SELECT o.id AS orderitems_id,
631 c1.accno AS inventory_accno,
632 c2.accno AS income_accno,
633 c3.accno AS expense_accno,
634 p.partnumber, p.assembly, o.description, o.qty,
635 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
636 o.reqdate, o.project_id, o.serialnumber, o.ship,
638 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
640 JOIN parts p ON (o.parts_id = p.id)
641 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
642 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
643 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
644 LEFT JOIN project pr ON (o.project_id = pr.id)
645 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
646 WHERE o.trans_id = $form->{id}
647 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
648 $sth = $dbh->prepare($query);
649 $sth->execute || $form->dberror($query);
651 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
653 #set expense_accno=inventory_accno if they are different => bilanz
655 ($ref->{expense_accno} != $ref->{inventory_accno})
656 ? $ref->{inventory_accno}
657 : $ref->{expense_accno};
659 # get tax rates and description
661 ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
662 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
664 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
666 $stw = $dbh->prepare($query);
667 $stw->execute || $form->dberror($query);
668 $ref->{taxaccounts} = "";
669 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
671 # if ($customertax{$ref->{accno}}) {
672 $ref->{taxaccounts} .= "$ptr->{accno} ";
673 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
674 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
675 $form->{"$ptr->{accno}_description"} = $ptr->{description};
676 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
677 $form->{taxaccounts} .= "$ptr->{accno} ";
682 chop $ref->{taxaccounts};
683 push @{ $form->{form_details} }, $ref;
691 $form->lastname_used($dbh, $myconfig, $form->{vc})
692 unless $form->{"$form->{vc}_id"};
696 $form->{exchangerate} =
697 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
698 ($form->{vc} eq 'customer') ? "buy" : "sell");
700 if ($form->{webdav}) {
701 &webdav_folder($myconfig, $form);
704 my $rc = $dbh->commit;
707 $main::lxdebug->leave_sub();
713 $main::lxdebug->enter_sub();
715 my ($self, $myconfig, $form) = @_;
717 # connect to database
718 my $dbh = $form->dbconnect($myconfig);
726 my %oid = ('Pg' => 'oid',
727 'Oracle' => 'rowid');
729 # sort items by partsgroup
730 for $i (1 .. $form->{rowcount}) {
732 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
733 $form->format_string("partsgroup_$i");
734 $partsgroup = $form->{"partsgroup_$i"};
736 push @partsgroup, [$i, $partsgroup];
739 # if there is a warehouse limit picking
740 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
742 # run query to check for inventory
743 $query = qq|SELECT sum(i.qty) AS qty
746 AND i.warehouse_id = ?|;
747 $sth = $dbh->prepare($query) || $form->dberror($query);
749 for $i (1 .. $form->{rowcount}) {
750 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
752 ($qty) = $sth->fetchrow_array;
755 $form->{"qty_$i"} = 0 if $qty == 0;
757 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
758 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
764 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
767 if ($item->[1] ne $sameitem) {
768 push(@{ $form->{description} }, qq|$item->[1]|);
769 $sameitem = $item->[1];
771 map { push(@{ $form->{$_} }, "") }
772 qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
775 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
777 if ($form->{"qty_$i"} != 0) {
779 # add number, description and qty to $form->{number}, ....
780 push(@{ $form->{runningnumber} }, $i);
781 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
782 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
783 push(@{ $form->{qty} },
784 $form->format_amount($myconfig, $form->{"qty_$i"}));
785 push(@{ $form->{ship} },
786 $form->format_amount($myconfig, $form->{"ship_$i"}));
787 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
788 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
789 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
790 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
791 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
793 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
795 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
797 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
798 my ($dec) = ($sellprice =~ /\.(\d+)/);
800 my $decimalplaces = ($dec > 2) ? $dec : 2;
804 $sellprice * $form->parse_amount($myconfig,
805 $form->{"discount_$i"}) / 100,
808 # keep a netprice as well, (sellprice - discount)
809 $form->{"netprice_$i"} = $sellprice - $discount;
812 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
814 push(@{ $form->{netprice} },
815 ($form->{"netprice_$i"} != 0)
816 ? $form->format_amount(
817 $myconfig, $form->{"netprice_$i"},
824 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
826 $linetotal = ($linetotal != 0) ? $linetotal : " ";
828 push(@{ $form->{discount} }, $discount);
830 $form->{ordtotal} += $linetotal;
832 push(@{ $form->{linetotal} },
833 $form->format_amount($myconfig, $linetotal, 2));
835 my ($taxamount, $taxbase);
838 map { $taxrate += $form->{"${_}_rate"} } split / /,
839 $form->{"taxaccounts_$i"};
841 if ($form->{taxincluded}) {
844 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
845 $taxbase = $linetotal / (1 + $taxrate);
847 $taxamount = $linetotal * $taxrate;
848 $taxbase = $linetotal;
851 if ($taxamount != 0) {
852 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
853 $taxaccounts{$item} +=
854 $taxamount * $form->{"${item}_rate"} / $taxrate;
855 $taxbase{$item} += $taxbase;
859 if ($form->{"assembly_$i"}) {
862 # get parts and push them onto the stack
864 if ($form->{groupitems}) {
866 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
868 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
871 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
874 JOIN parts p ON (a.parts_id = p.id)
875 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
877 AND a.id = '$form->{"id_$i"}'
879 $sth = $dbh->prepare($query);
880 $sth->execute || $form->dberror($query);
882 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
883 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
884 map { push(@{ $form->{$_} }, "") }
885 qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
886 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
887 push(@{ $form->{description} }, $sameitem);
890 push(@{ $form->{description} },
891 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
893 . qq|, $ref->{partnumber}, $ref->{description}|);
895 map { push(@{ $form->{$_} }, "") }
896 qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
906 foreach $item (sort keys %taxaccounts) {
907 if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
908 push(@{ $form->{taxbase} },
909 $form->format_amount($myconfig, $taxbase{$item}, 2));
911 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
913 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
914 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
915 push(@{ $form->{taxrate} },
916 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
917 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
921 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
923 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
926 $form->{quototal} = $form->{ordtotal} =
927 $form->format_amount($myconfig, $form->{ordtotal}, 2);
930 map { $form->{$_} = $myconfig->{$_} }
931 (qw(company address tel fax signature businessnumber));
932 $form->{username} = $myconfig->{name};
936 $main::lxdebug->leave_sub();
939 sub project_description {
940 $main::lxdebug->enter_sub();
942 my ($self, $dbh, $id) = @_;
944 my $query = qq|SELECT p.description
947 my $sth = $dbh->prepare($query);
948 $sth->execute || $form->dberror($query);
950 ($_) = $sth->fetchrow_array;
954 $main::lxdebug->leave_sub();
960 $main::lxdebug->enter_sub();
962 my ($self, $myconfig, $form) = @_;
964 my $dbh = $form->dbconnect($myconfig);
967 my $query = qq|SELECT id, description
970 my $sth = $dbh->prepare($query);
971 $sth->execute || $form->dberror($query);
973 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
974 push @{ $form->{all_warehouses} }, $ref;
980 $main::lxdebug->leave_sub();
984 $main::lxdebug->enter_sub();
986 my ($self, $myconfig, $form) = @_;
988 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
992 ($null, $employee_id) = split /--/, $form->{employee};
994 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
996 my $dbh = $form->dbconnect_noauto($myconfig);
1002 $query = qq|SELECT o.serialnumber, o.ship
1004 WHERE o.trans_id = ?
1007 $sth = $dbh->prepare($query) || $form->dberror($query);
1009 $query = qq|SELECT sum(i.qty)
1011 WHERE i.parts_id = ?
1012 AND i.warehouse_id = ?|;
1013 $wth = $dbh->prepare($query) || $form->dberror($query);
1015 for my $i (1 .. $form->{rowcount} - 1) {
1018 (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
1020 : $form->{"ship_$i"};
1022 if ($warehouse_id && $form->{type} eq 'ship_order') {
1024 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1026 ($qty) = $wth->fetchrow_array;
1037 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1038 qty, oe_id, orderitems_id, shippingdate, employee_id)
1039 VALUES ($form->{"id_$i"}, $warehouse_id,
1040 $ship, $form->{"id"},
1041 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1043 $dbh->do($query) || $form->dberror($query);
1045 # add serialnumber, ship to orderitems
1046 $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
1048 ($serialnumber, $ship) = $sth->fetchrow_array;
1051 $serialnumber .= " " if $serialnumber;
1052 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1053 $ship += $form->{"ship_$i"};
1055 $query = qq|UPDATE orderitems SET
1056 serialnumber = '$serialnumber',
1058 WHERE trans_id = $form->{id}
1059 AND id = $form->{"orderitems_id_$i"}|;
1060 $dbh->do($query) || $form->dberror($query);
1062 # update order with ship via
1063 $query = qq|UPDATE oe SET
1064 shippingpoint = '$form->{shippingpoint}',
1065 shipvia = '$form->{shipvia}'
1066 WHERE id = $form->{id}|;
1067 $dbh->do($query) || $form->dberror($query);
1069 # update onhand for parts
1070 $form->update_balance($dbh, "parts", "onhand",
1071 qq|id = $form->{"id_$i"}|,
1072 $form->{"ship_$i"} * $ml);
1077 my $rc = $dbh->commit;
1080 $main::lxdebug->leave_sub();
1086 $main::lxdebug->enter_sub();
1088 my ($dbh, $form, $ml) = @_;
1090 my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
1092 JOIN parts p ON (p.id = oi.parts_id)
1093 WHERE oi.trans_id = $form->{id}|;
1094 my $sth = $dbh->prepare($query);
1095 $sth->execute || $form->dberror($query);
1097 $query = qq|SELECT sum(p.inventory_accno_id)
1099 JOIN assembly a ON (a.parts_id = p.id)
1101 my $ath = $dbh->prepare($query) || $form->dberror($query);
1105 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1107 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1109 # do not update if assembly consists of all services
1110 if ($ref->{assembly}) {
1111 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1113 ($ispa) = $sth->fetchrow_array;
1120 # adjust onhand in parts table
1121 $form->update_balance($dbh, "parts", "onhand",
1122 qq|id = $ref->{parts_id}|,
1123 $ref->{ship} * $ml);
1129 $main::lxdebug->leave_sub();
1133 $main::lxdebug->enter_sub();
1135 my ($dbh, $myconfig, $form) = @_;
1137 my %oid = ('Pg' => 'oid',
1138 'Oracle' => 'rowid');
1140 # increase/reduce qty in inventory table
1141 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1143 WHERE oi.trans_id = $form->{id}|;
1144 my $sth = $dbh->prepare($query);
1145 $sth->execute || $form->dberror($query);
1147 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1148 (SELECT SUM(qty) FROM inventory
1149 WHERE oe_id = $form->{id}
1150 AND orderitems_id = ?) AS total
1152 WHERE oe_id = $form->{id}
1153 AND orderitems_id = ?|;
1154 my $ith = $dbh->prepare($query) || $form->dberror($query);
1157 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1159 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1161 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1163 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1165 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1166 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1168 $form->update_balance($dbh, "inventory", "qty",
1169 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1178 # delete inventory entries if qty = 0
1179 $query = qq|DELETE FROM inventory
1180 WHERE oe_id = $form->{id}
1182 $dbh->do($query) || $form->dberror($query);
1184 $main::lxdebug->leave_sub();
1188 $main::lxdebug->enter_sub();
1190 my ($self, $myconfig, $form) = @_;
1192 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1195 my $dbh = $form->dbconnect($myconfig);
1197 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1200 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1201 WHERE p.onhand > 0|;
1203 if ($form->{partnumber}) {
1204 $var = $form->like(lc $form->{partnumber});
1206 AND lower(p.partnumber) LIKE '$var'";
1208 if ($form->{description}) {
1209 $var = $form->like(lc $form->{description});
1211 AND lower(p.description) LIKE '$var'";
1213 if ($form->{partsgroup}) {
1214 $var = $form->like(lc $form->{partsgroup});
1216 AND lower(pg.partsgroup) LIKE '$var'";
1219 $sth = $dbh->prepare($query);
1220 $sth->execute || $form->dberror($query);
1222 $query = qq|SELECT sum(i.qty), w.description, w.id
1224 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1225 WHERE i.parts_id = ?
1226 AND NOT i.warehouse_id = $warehouse_id
1227 GROUP BY w.description, w.id|;
1228 $wth = $dbh->prepare($query) || $form->dberror($query);
1230 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1232 $wth->execute($ref->{id}) || $form->dberror;
1234 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1235 push @{ $form->{all_inventory} },
1236 { 'id' => $ref->{id},
1237 'partnumber' => $ref->{partnumber},
1238 'description' => $ref->{description},
1239 'partsgroup' => $ref->{partsgroup},
1241 'warehouse_id' => $warehouse_id,
1242 'warehouse' => $warehouse }
1252 @{ $form->{all_inventory} } =
1253 sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
1254 @{ $form->{all_inventory} };
1256 $main::lxdebug->leave_sub();
1258 return @{ $form->{all_inventory} };
1262 $main::lxdebug->enter_sub();
1264 my ($self, $myconfig, $form) = @_;
1266 my $dbh = $form->dbconnect_noauto($myconfig);
1268 my $query = qq|INSERT INTO inventory
1269 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1270 VALUES (?, ?, ?, ?, ?)|;
1271 $sth = $dbh->prepare($query) || $form->dberror($query);
1273 $form->get_employee($dbh);
1278 $shippingdate = "$a[5]-$a[4]-$a[3]";
1280 for my $i (1 .. $form->{rowcount}) {
1281 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1283 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1288 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
1289 $shippingdate, $form->{employee_id})
1295 $sth->execute($form->{"warehouse_id_$i"},
1296 $form->{"id_$i"}, $qty * -1, $shippingdate,
1297 $form->{employee_id})
1304 my $rc = $dbh->commit;
1307 $main::lxdebug->leave_sub();
1313 $main::lxdebug->enter_sub();
1315 my ($myconfig, $form) = @_;
1318 $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
1319 if ($form->{type} eq "sales_quotation");
1320 $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
1321 if ($form->{type} eq "sales_order");
1322 $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
1323 if ($form->{type} eq "request_quotation");
1324 $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
1325 if ($form->{type} eq "purchase_order");
1329 mkdir($path, 0770) or die "can't make directory $!\n";
1333 foreach $file (@files) {
1334 $file =~ /\/([^\/]*)$/;
1336 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1338 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1339 $form->{WEBDAV}{$fname} = $link;
1344 $main::lxdebug->leave_sub();