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);
189 my $uid = rand() . time;
191 $uid .= $form->{login};
193 $uid = substr($uid, 2, 75);
195 $query = qq|INSERT INTO oe (ordnumber, employee_id)
196 VALUES ('$uid', $form->{employee_id})|;
197 $dbh->do($query) || $form->dberror($query);
199 $query = qq|SELECT o.id FROM oe o
200 WHERE o.ordnumber = '$uid'|;
201 $sth = $dbh->prepare($query);
202 $sth->execute || $form->dberror($query);
205 ($form->{id}) = $sth->fetchrow_array;
209 map { $form->{$_} =~ s/\'/\'\'/g }
210 qw(ordnumber quonumber shippingpoint shipvia notes intnotes message);
225 for my $i (1 .. $form->{rowcount}) {
228 $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"})
231 $form->{"orderitems_id_$i"} = "";
234 if ($form->{"qty_$i"}) {
236 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
237 qw(partnumber description unit);
239 # set values to 0 if nothing entered
240 $form->{"discount_$i"} =
241 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
243 $form->{"sellprice_$i"} =
244 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
245 $fxsellprice = $form->{"sellprice_$i"};
247 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
249 my $decimalplaces = ($dec > 2) ? $dec : 2;
252 $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
254 $form->{"sellprice_$i"} =
255 $form->round_amount($form->{"sellprice_$i"} - $discount,
258 $form->{"inventory_accno_$i"} *= 1;
259 $form->{"expense_accno_$i"} *= 1;
262 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
264 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
268 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
270 if ($form->{taxincluded}) {
271 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
272 $taxbase = $linetotal - $taxamount;
274 # we are not keeping a natural price, do not round
275 $form->{"sellprice_$i"} =
276 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
278 $taxamount = $linetotal * $taxrate;
279 $taxbase = $linetotal;
282 if ($form->round_amount($taxrate, 7) == 0) {
283 if ($form->{taxincluded}) {
284 foreach $item (@taxaccounts) {
286 $form->round_amount($linetotal * $form->{"${item}_rate"} /
287 (1 + abs($form->{"${item}_rate"})),
290 $taxaccounts{$item} += $taxamount;
291 $taxdiff += $taxamount;
293 $taxbase{$item} += $taxbase;
295 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
297 foreach $item (@taxaccounts) {
298 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
299 $taxbase{$item} += $taxbase;
303 foreach $item (@taxaccounts) {
304 $taxaccounts{$item} +=
305 $taxamount * $form->{"${item}_rate"} / $taxrate;
306 $taxbase{$item} += $taxbase;
310 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
312 $project_id = 'NULL';
313 if ($form->{"projectnumber_$i"}) {
314 $project_id = $form->{"projectnumber_$i"};
317 ($form->{"reqdate_$i"}) ? qq|'$form->{"reqdate_$i"}'| : "NULL";
319 # get pricegroup_id and save ist
320 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_drag_$i"};
323 # save detail record in orderitems table
324 $query = qq|INSERT INTO orderitems (|;
325 $query .= "id, " if $form->{"orderitems_id_$i"};
326 $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
327 unit, reqdate, project_id, serialnumber, ship, pricegroup_id)
329 $query .= qq|$form->{"orderitems_id_$i"},|
330 if $form->{"orderitems_id_$i"};
331 $query .= qq|$form->{id}, $form->{"id_$i"},
332 '$form->{"description_$i"}', $form->{"qty_$i"},
333 $fxsellprice, $form->{"discount_$i"},
334 '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
335 '$form->{"serialnumber_$i"}', $form->{"ship_$i"},
337 $dbh->do($query) || $form->dberror($query);
339 $form->{"sellprice_$i"} = $fxsellprice;
340 $form->{"discount_$i"} *= 100;
344 # set values which could be empty
345 map { $form->{$_} *= 1 }
346 qw(vendor_id customer_id taxincluded closed quotation);
348 $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
352 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
354 $amount = $form->round_amount($netamount + $tax, 2);
355 $netamount = $form->round_amount($netamount, 2);
357 if ($form->{currency} eq $form->{defaultcurrency}) {
358 $form->{exchangerate} = 1;
361 $form->check_exchangerate($myconfig,
364 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
367 $form->{exchangerate} =
370 : $form->parse_amount($myconfig, $form->{exchangerate});
374 # fill in subject if there is none
375 if ($form->{type} =~ /_order$/) {
377 $form->{subject} = qq|$form->{label} $form->{ordnumber}|
378 unless $form->{subject};
381 $form->{subject} = qq|$form->{label} $form->{quonumber}|
382 unless $form->{subject};
385 # if there is a message stuff it into the intnotes
386 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
387 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
388 my $now = scalar localtime;
389 $form->{intnotes} .= qq|\r
390 \r| if $form->{intnotes};
392 $form->{intnotes} .= qq|[email]\r
395 $cc${bcc}Subject: $form->{subject}\r
397 Message: $form->{message}\r| if $form->{message};
399 ($null, $form->{department_id}) = split(/--/, $form->{department});
400 $form->{department_id} *= 1;
403 $query = qq|UPDATE oe set
404 ordnumber = '$form->{ordnumber}',
405 quonumber = '$form->{quonumber}',
406 cusordnumber = '$form->{cusordnumber}',
407 transdate = '$form->{transdate}',
408 vendor_id = $form->{vendor_id},
409 customer_id = $form->{customer_id},
411 netamount = $netamount,
413 taxincluded = '$form->{taxincluded}',
414 shippingpoint = '$form->{shippingpoint}',
415 shipvia = '$form->{shipvia}',
416 notes = '$form->{notes}',
417 intnotes = '$form->{intnotes}',
418 curr = '$form->{currency}',
419 closed = '$form->{closed}',
420 quotation = '$quotation',
421 department_id = $form->{department_id},
422 employee_id = $form->{employee_id},
423 cp_id = $form->{contact_id}
424 WHERE id = $form->{id}|;
425 $dbh->do($query) || $form->dberror($query);
427 $form->{ordtotal} = $amount;
429 if ($form->{webdav}) {
430 &webdav_folder($myconfig, $form);
434 $form->{name} = $form->{ $form->{vc} };
435 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
436 $form->add_shipto($dbh, $form->{id});
438 # save printed, emailed, queued
439 $form->save_status($dbh);
441 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
442 if ($form->{vc} eq 'customer') {
443 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
444 $form->{exchangerate}, 0);
446 if ($form->{vc} eq 'vendor') {
447 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
448 0, $form->{exchangerate});
452 if ($form->{type} =~ /_order$/) {
455 &adj_onhand($dbh, $form, $ml * -1);
456 &adj_inventory($dbh, $myconfig, $form);
459 my $rc = $dbh->commit;
462 $main::lxdebug->leave_sub();
468 $main::lxdebug->enter_sub();
470 my ($self, $myconfig, $form, $spool) = @_;
472 # connect to database
473 my $dbh = $form->dbconnect_noauto($myconfig);
476 my $query = qq|SELECT s.spoolfile FROM status s
477 WHERE s.trans_id = $form->{id}|;
478 $sth = $dbh->prepare($query);
479 $sth->execute || $self->dberror($query);
484 while (($spoolfile) = $sth->fetchrow_array) {
485 push @spoolfiles, $spoolfile;
489 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
490 WHERE o.trans_id = $form->{id}|;
491 $sth = $dbh->prepare($query);
492 $sth->execute || $self->dberror($query);
494 while (my ($id, $ship) = $sth->fetchrow_array) {
495 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
500 $query = qq|DELETE FROM inventory
501 WHERE oe_id = $form->{id}|;
502 $dbh->do($query) || $form->dberror($query);
504 # delete status entries
505 $query = qq|DELETE FROM status
506 WHERE trans_id = $form->{id}|;
507 $dbh->do($query) || $form->dberror($query);
510 $query = qq|DELETE FROM oe
511 WHERE id = $form->{id}|;
512 $dbh->do($query) || $form->dberror($query);
514 # delete individual entries
515 $query = qq|DELETE FROM orderitems
516 WHERE trans_id = $form->{id}|;
517 $dbh->do($query) || $form->dberror($query);
519 $query = qq|DELETE FROM shipto
520 WHERE trans_id = $form->{id}|;
521 $dbh->do($query) || $form->dberror($query);
523 my $rc = $dbh->commit;
527 foreach $spoolfile (@spoolfiles) {
528 unlink "$spool/$spoolfile" if $spoolfile;
532 $main::lxdebug->leave_sub();
538 $main::lxdebug->enter_sub();
540 my ($self, $myconfig, $form) = @_;
542 # connect to database
543 my $dbh = $form->dbconnect_noauto($myconfig);
549 # get default accounts and last order number
550 $query = qq|SELECT (SELECT c.accno FROM chart c
551 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
552 (SELECT c.accno FROM chart c
553 WHERE d.income_accno_id = c.id) AS income_accno,
554 (SELECT c.accno FROM chart c
555 WHERE d.expense_accno_id = c.id) AS expense_accno,
556 (SELECT c.accno FROM chart c
557 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
558 (SELECT c.accno FROM chart c
559 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
563 $query = qq|SELECT (SELECT c.accno FROM chart c
564 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
565 (SELECT c.accno FROM chart c
566 WHERE d.income_accno_id = c.id) AS income_accno,
567 (SELECT c.accno FROM chart c
568 WHERE d.expense_accno_id = c.id) AS expense_accno,
569 (SELECT c.accno FROM chart c
570 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
571 (SELECT c.accno FROM chart c
572 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
573 d.curr AS currencies,
574 current_date AS transdate, current_date AS reqdate
577 my $sth = $dbh->prepare($query);
578 $sth->execute || $form->dberror($query);
580 my $ref = $sth->fetchrow_hashref(NAME_lc);
581 map { $form->{$_} = $ref->{$_} } keys %$ref;
584 ($form->{currency}) = split /:/, $form->{currencies};
589 $query = qq|SELECT o.cp_id,o.ordnumber, o.transdate, o.reqdate,
590 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
591 o.curr AS currency, e.name AS employee, o.employee_id,
592 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
593 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
594 d.description AS department
596 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
597 LEFT JOIN employee e ON (o.employee_id = e.id)
598 LEFT JOIN department d ON (o.department_id = d.id)
599 WHERE o.id = $form->{id}|;
600 $sth = $dbh->prepare($query);
601 $sth->execute || $form->dberror($query);
603 $ref = $sth->fetchrow_hashref(NAME_lc);
604 map { $form->{$_} = $ref->{$_} } keys %$ref;
607 $query = qq|SELECT s.* FROM shipto s
608 WHERE s.trans_id = $form->{id}|;
609 $sth = $dbh->prepare($query);
610 $sth->execute || $form->dberror($query);
612 $ref = $sth->fetchrow_hashref(NAME_lc);
613 map { $form->{$_} = $ref->{$_} } keys %$ref;
616 # get printed, emailed and queued
617 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
619 WHERE s.trans_id = $form->{id}|;
620 $sth = $dbh->prepare($query);
621 $sth->execute || $form->dberror($query);
623 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
624 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
625 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
626 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
627 if $ref->{spoolfile};
630 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
632 my %oid = ('Pg' => 'oid',
633 'Oracle' => 'rowid');
635 # retrieve individual items
636 $query = qq|SELECT o.id AS orderitems_id,
637 c1.accno AS inventory_accno,
638 c2.accno AS income_accno,
639 c3.accno AS expense_accno,
640 p.partnumber, p.assembly, o.description, o.qty,
641 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
642 o.reqdate, o.project_id, o.serialnumber, o.ship,
644 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
646 JOIN parts p ON (o.parts_id = p.id)
647 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
648 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
649 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
650 LEFT JOIN project pr ON (o.project_id = pr.id)
651 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
652 WHERE o.trans_id = $form->{id}
653 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
654 $sth = $dbh->prepare($query);
655 $sth->execute || $form->dberror($query);
657 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
659 #set expense_accno=inventory_accno if they are different => bilanz
661 ($ref->{expense_accno} != $ref->{inventory_accno})
662 ? $ref->{inventory_accno}
663 : $ref->{expense_accno};
665 # get tax rates and description
667 ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
668 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
670 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
672 $stw = $dbh->prepare($query);
673 $stw->execute || $form->dberror($query);
674 $ref->{taxaccounts} = "";
675 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
677 # if ($customertax{$ref->{accno}}) {
678 $ref->{taxaccounts} .= "$ptr->{accno} ";
679 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
680 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
681 $form->{"$ptr->{accno}_description"} = $ptr->{description};
682 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
683 $form->{taxaccounts} .= "$ptr->{accno} ";
688 chop $ref->{taxaccounts};
689 push @{ $form->{form_details} }, $ref;
697 $form->lastname_used($dbh, $myconfig, $form->{vc})
698 unless $form->{"$form->{vc}_id"};
702 $form->{exchangerate} =
703 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
704 ($form->{vc} eq 'customer') ? "buy" : "sell");
706 if ($form->{webdav}) {
707 &webdav_folder($myconfig, $form);
710 my $rc = $dbh->commit;
713 $main::lxdebug->leave_sub();
719 $main::lxdebug->enter_sub();
721 my ($self, $myconfig, $form) = @_;
723 # connect to database
724 my $dbh = $form->dbconnect($myconfig);
732 my %oid = ('Pg' => 'oid',
733 'Oracle' => 'rowid');
735 # sort items by partsgroup
736 for $i (1 .. $form->{rowcount}) {
738 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
739 $form->format_string("partsgroup_$i");
740 $partsgroup = $form->{"partsgroup_$i"};
742 push @partsgroup, [$i, $partsgroup];
745 # if there is a warehouse limit picking
746 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
748 # run query to check for inventory
749 $query = qq|SELECT sum(i.qty) AS qty
752 AND i.warehouse_id = ?|;
753 $sth = $dbh->prepare($query) || $form->dberror($query);
755 for $i (1 .. $form->{rowcount}) {
756 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
758 ($qty) = $sth->fetchrow_array;
761 $form->{"qty_$i"} = 0 if $qty == 0;
763 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
764 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
770 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
773 if ($item->[1] ne $sameitem) {
774 push(@{ $form->{description} }, qq|$item->[1]|);
775 $sameitem = $item->[1];
777 map { push(@{ $form->{$_} }, "") }
778 qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
781 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
783 if ($form->{"qty_$i"} != 0) {
785 # add number, description and qty to $form->{number}, ....
786 push(@{ $form->{runningnumber} }, $i);
787 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
788 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
789 push(@{ $form->{qty} },
790 $form->format_amount($myconfig, $form->{"qty_$i"}));
791 push(@{ $form->{ship} },
792 $form->format_amount($myconfig, $form->{"ship_$i"}));
793 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
794 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
795 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
796 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
797 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
799 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
801 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
803 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
804 my ($dec) = ($sellprice =~ /\.(\d+)/);
806 my $decimalplaces = ($dec > 2) ? $dec : 2;
810 $sellprice * $form->parse_amount($myconfig,
811 $form->{"discount_$i"}) / 100,
814 # keep a netprice as well, (sellprice - discount)
815 $form->{"netprice_$i"} = $sellprice - $discount;
818 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
820 push(@{ $form->{netprice} },
821 ($form->{"netprice_$i"} != 0)
822 ? $form->format_amount(
823 $myconfig, $form->{"netprice_$i"},
830 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
832 $linetotal = ($linetotal != 0) ? $linetotal : " ";
834 push(@{ $form->{discount} }, $discount);
836 $form->{ordtotal} += $linetotal;
838 push(@{ $form->{linetotal} },
839 $form->format_amount($myconfig, $linetotal, 2));
841 my ($taxamount, $taxbase);
844 map { $taxrate += $form->{"${_}_rate"} } split / /,
845 $form->{"taxaccounts_$i"};
847 if ($form->{taxincluded}) {
850 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
851 $taxbase = $linetotal / (1 + $taxrate);
853 $taxamount = $linetotal * $taxrate;
854 $taxbase = $linetotal;
857 if ($taxamount != 0) {
858 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
859 $taxaccounts{$item} +=
860 $taxamount * $form->{"${item}_rate"} / $taxrate;
861 $taxbase{$item} += $taxbase;
865 $tax_rate = $taxrate*100;
866 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
868 if ($form->{"assembly_$i"}) {
871 # get parts and push them onto the stack
873 if ($form->{groupitems}) {
875 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
877 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
880 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
883 JOIN parts p ON (a.parts_id = p.id)
884 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
886 AND a.id = '$form->{"id_$i"}'
888 $sth = $dbh->prepare($query);
889 $sth->execute || $form->dberror($query);
891 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
892 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
893 map { push(@{ $form->{$_} }, "") }
894 qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
895 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
896 push(@{ $form->{description} }, $sameitem);
899 push(@{ $form->{description} },
900 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
902 . qq|, $ref->{partnumber}, $ref->{description}|);
904 map { push(@{ $form->{$_} }, "") }
905 qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
915 foreach $item (sort keys %taxaccounts) {
916 if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
917 push(@{ $form->{taxbase} },
918 $form->format_amount($myconfig, $taxbase{$item}, 2));
920 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
922 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
923 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
924 push(@{ $form->{taxrate} },
925 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
926 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
930 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
932 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
935 $form->{quototal} = $form->{ordtotal} =
936 $form->format_amount($myconfig, $form->{ordtotal}, 2);
939 map { $form->{$_} = $myconfig->{$_} }
940 (qw(company address tel fax signature businessnumber));
941 $form->{username} = $myconfig->{name};
945 $main::lxdebug->leave_sub();
948 sub project_description {
949 $main::lxdebug->enter_sub();
951 my ($self, $dbh, $id) = @_;
953 my $query = qq|SELECT p.description
956 my $sth = $dbh->prepare($query);
957 $sth->execute || $form->dberror($query);
959 ($_) = $sth->fetchrow_array;
963 $main::lxdebug->leave_sub();
969 $main::lxdebug->enter_sub();
971 my ($self, $myconfig, $form) = @_;
973 my $dbh = $form->dbconnect($myconfig);
976 my $query = qq|SELECT id, description
979 my $sth = $dbh->prepare($query);
980 $sth->execute || $form->dberror($query);
982 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
983 push @{ $form->{all_warehouses} }, $ref;
989 $main::lxdebug->leave_sub();
993 $main::lxdebug->enter_sub();
995 my ($self, $myconfig, $form) = @_;
997 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1001 ($null, $employee_id) = split /--/, $form->{employee};
1003 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
1005 my $dbh = $form->dbconnect_noauto($myconfig);
1011 $query = qq|SELECT o.serialnumber, o.ship
1013 WHERE o.trans_id = ?
1016 $sth = $dbh->prepare($query) || $form->dberror($query);
1018 $query = qq|SELECT sum(i.qty)
1020 WHERE i.parts_id = ?
1021 AND i.warehouse_id = ?|;
1022 $wth = $dbh->prepare($query) || $form->dberror($query);
1024 for my $i (1 .. $form->{rowcount} - 1) {
1027 (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
1029 : $form->{"ship_$i"};
1031 if ($warehouse_id && $form->{type} eq 'ship_order') {
1033 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1035 ($qty) = $wth->fetchrow_array;
1046 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1047 qty, oe_id, orderitems_id, shippingdate, employee_id)
1048 VALUES ($form->{"id_$i"}, $warehouse_id,
1049 $ship, $form->{"id"},
1050 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1052 $dbh->do($query) || $form->dberror($query);
1054 # add serialnumber, ship to orderitems
1055 $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
1057 ($serialnumber, $ship) = $sth->fetchrow_array;
1060 $serialnumber .= " " if $serialnumber;
1061 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1062 $ship += $form->{"ship_$i"};
1064 $query = qq|UPDATE orderitems SET
1065 serialnumber = '$serialnumber',
1067 WHERE trans_id = $form->{id}
1068 AND id = $form->{"orderitems_id_$i"}|;
1069 $dbh->do($query) || $form->dberror($query);
1071 # update order with ship via
1072 $query = qq|UPDATE oe SET
1073 shippingpoint = '$form->{shippingpoint}',
1074 shipvia = '$form->{shipvia}'
1075 WHERE id = $form->{id}|;
1076 $dbh->do($query) || $form->dberror($query);
1078 # update onhand for parts
1079 $form->update_balance($dbh, "parts", "onhand",
1080 qq|id = $form->{"id_$i"}|,
1081 $form->{"ship_$i"} * $ml);
1086 my $rc = $dbh->commit;
1089 $main::lxdebug->leave_sub();
1095 $main::lxdebug->enter_sub();
1097 my ($dbh, $form, $ml) = @_;
1099 my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
1101 JOIN parts p ON (p.id = oi.parts_id)
1102 WHERE oi.trans_id = $form->{id}|;
1103 my $sth = $dbh->prepare($query);
1104 $sth->execute || $form->dberror($query);
1106 $query = qq|SELECT sum(p.inventory_accno_id)
1108 JOIN assembly a ON (a.parts_id = p.id)
1110 my $ath = $dbh->prepare($query) || $form->dberror($query);
1114 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1116 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1118 # do not update if assembly consists of all services
1119 if ($ref->{assembly}) {
1120 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1122 ($ispa) = $sth->fetchrow_array;
1129 # adjust onhand in parts table
1130 $form->update_balance($dbh, "parts", "onhand",
1131 qq|id = $ref->{parts_id}|,
1132 $ref->{ship} * $ml);
1138 $main::lxdebug->leave_sub();
1142 $main::lxdebug->enter_sub();
1144 my ($dbh, $myconfig, $form) = @_;
1146 my %oid = ('Pg' => 'oid',
1147 'Oracle' => 'rowid');
1149 # increase/reduce qty in inventory table
1150 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1152 WHERE oi.trans_id = $form->{id}|;
1153 my $sth = $dbh->prepare($query);
1154 $sth->execute || $form->dberror($query);
1156 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1157 (SELECT SUM(qty) FROM inventory
1158 WHERE oe_id = $form->{id}
1159 AND orderitems_id = ?) AS total
1161 WHERE oe_id = $form->{id}
1162 AND orderitems_id = ?|;
1163 my $ith = $dbh->prepare($query) || $form->dberror($query);
1166 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1168 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1170 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1172 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1174 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1175 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1177 $form->update_balance($dbh, "inventory", "qty",
1178 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1187 # delete inventory entries if qty = 0
1188 $query = qq|DELETE FROM inventory
1189 WHERE oe_id = $form->{id}
1191 $dbh->do($query) || $form->dberror($query);
1193 $main::lxdebug->leave_sub();
1197 $main::lxdebug->enter_sub();
1199 my ($self, $myconfig, $form) = @_;
1201 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1204 my $dbh = $form->dbconnect($myconfig);
1206 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1209 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1210 WHERE p.onhand > 0|;
1212 if ($form->{partnumber}) {
1213 $var = $form->like(lc $form->{partnumber});
1215 AND lower(p.partnumber) LIKE '$var'";
1217 if ($form->{description}) {
1218 $var = $form->like(lc $form->{description});
1220 AND lower(p.description) LIKE '$var'";
1222 if ($form->{partsgroup}) {
1223 $var = $form->like(lc $form->{partsgroup});
1225 AND lower(pg.partsgroup) LIKE '$var'";
1228 $sth = $dbh->prepare($query);
1229 $sth->execute || $form->dberror($query);
1231 $query = qq|SELECT sum(i.qty), w.description, w.id
1233 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1234 WHERE i.parts_id = ?
1235 AND NOT i.warehouse_id = $warehouse_id
1236 GROUP BY w.description, w.id|;
1237 $wth = $dbh->prepare($query) || $form->dberror($query);
1239 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1241 $wth->execute($ref->{id}) || $form->dberror;
1243 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1244 push @{ $form->{all_inventory} },
1245 { 'id' => $ref->{id},
1246 'partnumber' => $ref->{partnumber},
1247 'description' => $ref->{description},
1248 'partsgroup' => $ref->{partsgroup},
1250 'warehouse_id' => $warehouse_id,
1251 'warehouse' => $warehouse }
1261 @{ $form->{all_inventory} } =
1262 sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
1263 @{ $form->{all_inventory} };
1265 $main::lxdebug->leave_sub();
1267 return @{ $form->{all_inventory} };
1271 $main::lxdebug->enter_sub();
1273 my ($self, $myconfig, $form) = @_;
1275 my $dbh = $form->dbconnect_noauto($myconfig);
1277 my $query = qq|INSERT INTO inventory
1278 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1279 VALUES (?, ?, ?, ?, ?)|;
1280 $sth = $dbh->prepare($query) || $form->dberror($query);
1282 $form->get_employee($dbh);
1287 $shippingdate = "$a[5]-$a[4]-$a[3]";
1289 for my $i (1 .. $form->{rowcount}) {
1290 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1292 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1297 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
1298 $shippingdate, $form->{employee_id})
1304 $sth->execute($form->{"warehouse_id_$i"},
1305 $form->{"id_$i"}, $qty * -1, $shippingdate,
1306 $form->{employee_id})
1313 my $rc = $dbh->commit;
1316 $main::lxdebug->leave_sub();
1322 $main::lxdebug->enter_sub();
1324 my ($myconfig, $form) = @_;
1327 $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
1328 if ($form->{type} eq "sales_quotation");
1329 $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
1330 if ($form->{type} eq "sales_order");
1331 $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
1332 if ($form->{type} eq "request_quotation");
1333 $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
1334 if ($form->{type} eq "purchase_order");
1338 mkdir($path, 0770) or die "can't make directory $!\n";
1342 foreach $file (@files) {
1343 $file =~ /\/([^\/]*)$/;
1345 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1347 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1348 $form->{WEBDAV}{$fname} = $link;
1353 $main::lxdebug->leave_sub();