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();
467 # this function closes multiple orders given in $form->{ordnumber_#}.
468 # use this for multiple orders that don't have to be saved back
469 # single orders should use OE::save instead.
471 $main::lxdebug->enter_sub();
473 my ($self, $myconfig ,$form) = @_;
476 map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} } (1 .. $form->{rowcount});
478 my $dbh = $form->dbconnect($myconfig);
479 $query = qq|UPDATE oe SET
481 WHERE ordnumber IN (|.join(', ', map{ $dbh->quote($_) }@ids).qq|)|;
482 $dbh->do($query) || $form->dberror($query);
485 $main::lxdebug->leave_sub();
489 $main::lxdebug->enter_sub();
491 my ($self, $myconfig, $form, $spool) = @_;
493 # connect to database
494 my $dbh = $form->dbconnect_noauto($myconfig);
497 my $query = qq|SELECT s.spoolfile FROM status s
498 WHERE s.trans_id = $form->{id}|;
499 $sth = $dbh->prepare($query);
500 $sth->execute || $self->dberror($query);
505 while (($spoolfile) = $sth->fetchrow_array) {
506 push @spoolfiles, $spoolfile;
510 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
511 WHERE o.trans_id = $form->{id}|;
512 $sth = $dbh->prepare($query);
513 $sth->execute || $self->dberror($query);
515 while (my ($id, $ship) = $sth->fetchrow_array) {
516 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
521 $query = qq|DELETE FROM inventory
522 WHERE oe_id = $form->{id}|;
523 $dbh->do($query) || $form->dberror($query);
525 # delete status entries
526 $query = qq|DELETE FROM status
527 WHERE trans_id = $form->{id}|;
528 $dbh->do($query) || $form->dberror($query);
531 $query = qq|DELETE FROM oe
532 WHERE id = $form->{id}|;
533 $dbh->do($query) || $form->dberror($query);
535 # delete individual entries
536 $query = qq|DELETE FROM orderitems
537 WHERE trans_id = $form->{id}|;
538 $dbh->do($query) || $form->dberror($query);
540 $query = qq|DELETE FROM shipto
541 WHERE trans_id = $form->{id}|;
542 $dbh->do($query) || $form->dberror($query);
544 my $rc = $dbh->commit;
548 foreach $spoolfile (@spoolfiles) {
549 unlink "$spool/$spoolfile" if $spoolfile;
553 $main::lxdebug->leave_sub();
559 $main::lxdebug->enter_sub();
561 my ($self, $myconfig, $form) = @_;
563 # connect to database
564 my $dbh = $form->dbconnect_noauto($myconfig);
568 # translate the ids (given by id_# and trans_id_#) into one array of ids, so we can join them later
569 map { push @ids, $form->{"trans_id_$_"} if ($form->{"id_$_"}) } (1 .. $form->{"rowcount"});
571 # if called in multi id mode, and still only got one id, switch back to single id
572 if ($form->{"rowcount"} and $#ids == 0) {
573 $form->{"id"} = $ids[0];
579 # get default accounts and last order number
580 $query = qq|SELECT (SELECT c.accno FROM chart c
581 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
582 (SELECT c.accno FROM chart c
583 WHERE d.income_accno_id = c.id) AS income_accno,
584 (SELECT c.accno FROM chart c
585 WHERE d.expense_accno_id = c.id) AS expense_accno,
586 (SELECT c.accno FROM chart c
587 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
588 (SELECT c.accno FROM chart c
589 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
593 $query = qq|SELECT (SELECT c.accno FROM chart c
594 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
595 (SELECT c.accno FROM chart c
596 WHERE d.income_accno_id = c.id) AS income_accno,
597 (SELECT c.accno FROM chart c
598 WHERE d.expense_accno_id = c.id) AS expense_accno,
599 (SELECT c.accno FROM chart c
600 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
601 (SELECT c.accno FROM chart c
602 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
603 d.curr AS currencies,
604 current_date AS transdate, current_date AS reqdate
607 my $sth = $dbh->prepare($query);
608 $sth->execute || $form->dberror($query);
610 my $ref = $sth->fetchrow_hashref(NAME_lc);
611 map { $form->{$_} = $ref->{$_} } keys %$ref;
614 ($form->{currency}) = split /:/, $form->{currencies};
616 if ($form->{id} or @ids) {
618 # retrieve order for single id
619 # NOTE: this query is intended to fetch all information only ONCE.
620 # so if any of these infos is important (or even different) for any item,
621 # it will be killed out and then has to be fetched from the item scope query further down
622 $query = qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate,
623 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
624 o.curr AS currency, e.name AS employee, o.employee_id,
625 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
626 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
627 d.description AS department
629 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
630 LEFT JOIN employee e ON (o.employee_id = e.id)
631 LEFT JOIN department d ON (o.department_id = d.id)
633 ? qq|WHERE o.id = $form->{id}|
634 : qq|WHERE o.id IN (|.join(', ', @ids).qq|)|
637 #$main::lxdebug->message(0, $query);
639 $sth = $dbh->prepare($query);
640 $sth->execute || $form->dberror($query);
642 $ref = $sth->fetchrow_hashref(NAME_lc);
643 map { $form->{$_} = $ref->{$_} } keys %$ref;
645 # destroy all entries for multiple ids that yield different information
646 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
647 map { undef $form->{$_} if ($ref->{$_} ne $form->{$_}) } keys %$ref;
650 # if not given, fill transdate with current_date
651 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
655 # shipto and pinted/mailed/queued status makes only sense for single id retrieve
657 $query = qq|SELECT s.* FROM shipto s
658 WHERE s.trans_id = $form->{id}|;
659 $sth = $dbh->prepare($query);
660 $sth->execute || $form->dberror($query);
662 $ref = $sth->fetchrow_hashref(NAME_lc);
663 map { $form->{$_} = $ref->{$_} } keys %$ref;
666 # get printed, emailed and queued
667 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
669 WHERE s.trans_id = $form->{id}|;
670 $sth = $dbh->prepare($query);
671 $sth->execute || $form->dberror($query);
673 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
674 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
675 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
676 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
679 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
682 my %oid = ('Pg' => 'oid',
683 'Oracle' => 'rowid');
685 # retrieve individual items
686 # this query looks up all information about the items
687 # stuff different from the whole will not be overwritten, but saved with a suffix.
688 $query = qq|SELECT o.id AS orderitems_id,
689 c1.accno AS inventory_accno,
690 c2.accno AS income_accno,
691 c3.accno AS expense_accno,
692 oe.ordnumber, oe.transdate, oe.cusordnumber,
693 p.partnumber, p.assembly, o.description, o.qty,
694 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
695 o.reqdate, o.project_id, o.serialnumber, o.ship,
697 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
699 JOIN parts p ON (o.parts_id = p.id)
700 JOIN oe ON (o.trans_id = oe.id)
701 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
702 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
703 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
704 LEFT JOIN project pr ON (o.project_id = pr.id)
705 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
707 ? qq|WHERE o.trans_id = $form->{id}|
708 : qq|WHERE o.trans_id IN (|.join(", ", @ids).qq|)|
710 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
712 $sth = $dbh->prepare($query);
713 $sth->execute || $form->dberror($query);
715 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
717 #set expense_accno=inventory_accno if they are different => bilanz
719 ($ref->{expense_accno} != $ref->{inventory_accno})
720 ? $ref->{inventory_accno}
721 : $ref->{expense_accno};
723 # get tax rates and description
725 ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
726 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
728 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
730 $stw = $dbh->prepare($query);
731 $stw->execute || $form->dberror($query);
732 $ref->{taxaccounts} = "";
733 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
735 # if ($customertax{$ref->{accno}}) {
736 $ref->{taxaccounts} .= "$ptr->{accno} ";
737 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
738 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
739 $form->{"$ptr->{accno}_description"} = $ptr->{description};
740 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
741 $form->{taxaccounts} .= "$ptr->{accno} ";
746 chop $ref->{taxaccounts};
747 push @{ $form->{form_details} }, $ref;
755 $form->lastname_used($dbh, $myconfig, $form->{vc})
756 unless $form->{"$form->{vc}_id"};
760 $form->{exchangerate} =
761 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
762 ($form->{vc} eq 'customer') ? "buy" : "sell");
764 if ($form->{webdav}) {
765 &webdav_folder($myconfig, $form);
768 my $rc = $dbh->commit;
771 $main::lxdebug->leave_sub();
777 $main::lxdebug->enter_sub();
779 my ($self, $myconfig, $form) = @_;
781 # connect to database
782 my $dbh = $form->dbconnect($myconfig);
790 my %oid = ('Pg' => 'oid',
791 'Oracle' => 'rowid');
793 # sort items by partsgroup
794 for $i (1 .. $form->{rowcount}) {
796 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
797 $form->format_string("partsgroup_$i");
798 $partsgroup = $form->{"partsgroup_$i"};
800 push @partsgroup, [$i, $partsgroup];
803 # if there is a warehouse limit picking
804 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
806 # run query to check for inventory
807 $query = qq|SELECT sum(i.qty) AS qty
810 AND i.warehouse_id = ?|;
811 $sth = $dbh->prepare($query) || $form->dberror($query);
813 for $i (1 .. $form->{rowcount}) {
814 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
816 ($qty) = $sth->fetchrow_array;
819 $form->{"qty_$i"} = 0 if $qty == 0;
821 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
822 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
828 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
831 if ($item->[1] ne $sameitem) {
832 push(@{ $form->{description} }, qq|$item->[1]|);
833 $sameitem = $item->[1];
835 map { push(@{ $form->{$_} }, "") }
836 qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
839 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
841 if ($form->{"qty_$i"} != 0) {
843 # add number, description and qty to $form->{number}, ....
844 push(@{ $form->{runningnumber} }, $i);
845 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
846 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
847 push(@{ $form->{qty} },
848 $form->format_amount($myconfig, $form->{"qty_$i"}));
849 push(@{ $form->{ship} },
850 $form->format_amount($myconfig, $form->{"ship_$i"}));
851 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
852 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
853 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
854 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
855 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
857 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
859 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
861 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
862 my ($dec) = ($sellprice =~ /\.(\d+)/);
864 my $decimalplaces = ($dec > 2) ? $dec : 2;
868 $sellprice * $form->parse_amount($myconfig,
869 $form->{"discount_$i"}) / 100,
872 # keep a netprice as well, (sellprice - discount)
873 $form->{"netprice_$i"} = $sellprice - $discount;
876 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
878 push(@{ $form->{netprice} },
879 ($form->{"netprice_$i"} != 0)
880 ? $form->format_amount(
881 $myconfig, $form->{"netprice_$i"},
888 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
890 $linetotal = ($linetotal != 0) ? $linetotal : " ";
892 push(@{ $form->{discount} }, $discount);
894 $form->{ordtotal} += $linetotal;
896 push(@{ $form->{linetotal} },
897 $form->format_amount($myconfig, $linetotal, 2));
899 my ($taxamount, $taxbase);
902 map { $taxrate += $form->{"${_}_rate"} } split / /,
903 $form->{"taxaccounts_$i"};
905 if ($form->{taxincluded}) {
908 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
909 $taxbase = $linetotal / (1 + $taxrate);
911 $taxamount = $linetotal * $taxrate;
912 $taxbase = $linetotal;
915 if ($taxamount != 0) {
916 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
917 $taxaccounts{$item} +=
918 $taxamount * $form->{"${item}_rate"} / $taxrate;
919 $taxbase{$item} += $taxbase;
923 $tax_rate = $taxrate*100;
924 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
926 if ($form->{"assembly_$i"}) {
929 # get parts and push them onto the stack
931 if ($form->{groupitems}) {
933 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
935 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
938 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
941 JOIN parts p ON (a.parts_id = p.id)
942 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
944 AND a.id = '$form->{"id_$i"}'
946 $sth = $dbh->prepare($query);
947 $sth->execute || $form->dberror($query);
949 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
950 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
951 map { push(@{ $form->{$_} }, "") }
952 qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
953 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
954 push(@{ $form->{description} }, $sameitem);
957 push(@{ $form->{description} },
958 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
960 . qq|, $ref->{partnumber}, $ref->{description}|);
962 map { push(@{ $form->{$_} }, "") }
963 qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
973 foreach $item (sort keys %taxaccounts) {
974 if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
975 push(@{ $form->{taxbase} },
976 $form->format_amount($myconfig, $taxbase{$item}, 2));
978 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
980 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
981 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
982 push(@{ $form->{taxrate} },
983 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
984 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
988 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
990 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
993 $form->{quototal} = $form->{ordtotal} =
994 $form->format_amount($myconfig, $form->{ordtotal}, 2);
997 map { $form->{$_} = $myconfig->{$_} }
998 (qw(company address tel fax signature businessnumber));
999 $form->{username} = $myconfig->{name};
1003 $main::lxdebug->leave_sub();
1006 sub project_description {
1007 $main::lxdebug->enter_sub();
1009 my ($self, $dbh, $id) = @_;
1011 my $query = qq|SELECT p.description
1014 my $sth = $dbh->prepare($query);
1015 $sth->execute || $form->dberror($query);
1017 ($_) = $sth->fetchrow_array;
1021 $main::lxdebug->leave_sub();
1026 sub get_warehouses {
1027 $main::lxdebug->enter_sub();
1029 my ($self, $myconfig, $form) = @_;
1031 my $dbh = $form->dbconnect($myconfig);
1034 my $query = qq|SELECT id, description
1037 my $sth = $dbh->prepare($query);
1038 $sth->execute || $form->dberror($query);
1040 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1041 push @{ $form->{all_warehouses} }, $ref;
1047 $main::lxdebug->leave_sub();
1050 sub save_inventory {
1051 $main::lxdebug->enter_sub();
1053 my ($self, $myconfig, $form) = @_;
1055 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1059 ($null, $employee_id) = split /--/, $form->{employee};
1061 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
1063 my $dbh = $form->dbconnect_noauto($myconfig);
1069 $query = qq|SELECT o.serialnumber, o.ship
1071 WHERE o.trans_id = ?
1074 $sth = $dbh->prepare($query) || $form->dberror($query);
1076 $query = qq|SELECT sum(i.qty)
1078 WHERE i.parts_id = ?
1079 AND i.warehouse_id = ?|;
1080 $wth = $dbh->prepare($query) || $form->dberror($query);
1082 for my $i (1 .. $form->{rowcount} - 1) {
1085 (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
1087 : $form->{"ship_$i"};
1089 if ($warehouse_id && $form->{type} eq 'ship_order') {
1091 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1093 ($qty) = $wth->fetchrow_array;
1104 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1105 qty, oe_id, orderitems_id, shippingdate, employee_id)
1106 VALUES ($form->{"id_$i"}, $warehouse_id,
1107 $ship, $form->{"id"},
1108 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1110 $dbh->do($query) || $form->dberror($query);
1112 # add serialnumber, ship to orderitems
1113 $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
1115 ($serialnumber, $ship) = $sth->fetchrow_array;
1118 $serialnumber .= " " if $serialnumber;
1119 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1120 $ship += $form->{"ship_$i"};
1122 $query = qq|UPDATE orderitems SET
1123 serialnumber = '$serialnumber',
1125 WHERE trans_id = $form->{id}
1126 AND id = $form->{"orderitems_id_$i"}|;
1127 $dbh->do($query) || $form->dberror($query);
1129 # update order with ship via
1130 $query = qq|UPDATE oe SET
1131 shippingpoint = '$form->{shippingpoint}',
1132 shipvia = '$form->{shipvia}'
1133 WHERE id = $form->{id}|;
1134 $dbh->do($query) || $form->dberror($query);
1136 # update onhand for parts
1137 $form->update_balance($dbh, "parts", "onhand",
1138 qq|id = $form->{"id_$i"}|,
1139 $form->{"ship_$i"} * $ml);
1144 my $rc = $dbh->commit;
1147 $main::lxdebug->leave_sub();
1153 $main::lxdebug->enter_sub();
1155 my ($dbh, $form, $ml) = @_;
1157 my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
1159 JOIN parts p ON (p.id = oi.parts_id)
1160 WHERE oi.trans_id = $form->{id}|;
1161 my $sth = $dbh->prepare($query);
1162 $sth->execute || $form->dberror($query);
1164 $query = qq|SELECT sum(p.inventory_accno_id)
1166 JOIN assembly a ON (a.parts_id = p.id)
1168 my $ath = $dbh->prepare($query) || $form->dberror($query);
1172 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1174 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1176 # do not update if assembly consists of all services
1177 if ($ref->{assembly}) {
1178 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1180 ($ispa) = $sth->fetchrow_array;
1187 # adjust onhand in parts table
1188 $form->update_balance($dbh, "parts", "onhand",
1189 qq|id = $ref->{parts_id}|,
1190 $ref->{ship} * $ml);
1196 $main::lxdebug->leave_sub();
1200 $main::lxdebug->enter_sub();
1202 my ($dbh, $myconfig, $form) = @_;
1204 my %oid = ('Pg' => 'oid',
1205 'Oracle' => 'rowid');
1207 # increase/reduce qty in inventory table
1208 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1210 WHERE oi.trans_id = $form->{id}|;
1211 my $sth = $dbh->prepare($query);
1212 $sth->execute || $form->dberror($query);
1214 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1215 (SELECT SUM(qty) FROM inventory
1216 WHERE oe_id = $form->{id}
1217 AND orderitems_id = ?) AS total
1219 WHERE oe_id = $form->{id}
1220 AND orderitems_id = ?|;
1221 my $ith = $dbh->prepare($query) || $form->dberror($query);
1224 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1226 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1228 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1230 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1232 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1233 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1235 $form->update_balance($dbh, "inventory", "qty",
1236 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1245 # delete inventory entries if qty = 0
1246 $query = qq|DELETE FROM inventory
1247 WHERE oe_id = $form->{id}
1249 $dbh->do($query) || $form->dberror($query);
1251 $main::lxdebug->leave_sub();
1255 $main::lxdebug->enter_sub();
1257 my ($self, $myconfig, $form) = @_;
1259 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1262 my $dbh = $form->dbconnect($myconfig);
1264 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1267 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1268 WHERE p.onhand > 0|;
1270 if ($form->{partnumber}) {
1271 $var = $form->like(lc $form->{partnumber});
1273 AND lower(p.partnumber) LIKE '$var'";
1275 if ($form->{description}) {
1276 $var = $form->like(lc $form->{description});
1278 AND lower(p.description) LIKE '$var'";
1280 if ($form->{partsgroup}) {
1281 $var = $form->like(lc $form->{partsgroup});
1283 AND lower(pg.partsgroup) LIKE '$var'";
1286 $sth = $dbh->prepare($query);
1287 $sth->execute || $form->dberror($query);
1289 $query = qq|SELECT sum(i.qty), w.description, w.id
1291 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1292 WHERE i.parts_id = ?
1293 AND NOT i.warehouse_id = $warehouse_id
1294 GROUP BY w.description, w.id|;
1295 $wth = $dbh->prepare($query) || $form->dberror($query);
1297 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1299 $wth->execute($ref->{id}) || $form->dberror;
1301 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1302 push @{ $form->{all_inventory} },
1303 { 'id' => $ref->{id},
1304 'partnumber' => $ref->{partnumber},
1305 'description' => $ref->{description},
1306 'partsgroup' => $ref->{partsgroup},
1308 'warehouse_id' => $warehouse_id,
1309 'warehouse' => $warehouse }
1319 @{ $form->{all_inventory} } =
1320 sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
1321 @{ $form->{all_inventory} };
1323 $main::lxdebug->leave_sub();
1325 return @{ $form->{all_inventory} };
1329 $main::lxdebug->enter_sub();
1331 my ($self, $myconfig, $form) = @_;
1333 my $dbh = $form->dbconnect_noauto($myconfig);
1335 my $query = qq|INSERT INTO inventory
1336 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1337 VALUES (?, ?, ?, ?, ?)|;
1338 $sth = $dbh->prepare($query) || $form->dberror($query);
1340 $form->get_employee($dbh);
1345 $shippingdate = "$a[5]-$a[4]-$a[3]";
1347 for my $i (1 .. $form->{rowcount}) {
1348 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1350 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1355 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
1356 $shippingdate, $form->{employee_id})
1362 $sth->execute($form->{"warehouse_id_$i"},
1363 $form->{"id_$i"}, $qty * -1, $shippingdate,
1364 $form->{employee_id})
1371 my $rc = $dbh->commit;
1374 $main::lxdebug->leave_sub();
1380 $main::lxdebug->enter_sub();
1382 my ($myconfig, $form) = @_;
1385 $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
1386 if ($form->{type} eq "sales_quotation");
1387 $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
1388 if ($form->{type} eq "sales_order");
1389 $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
1390 if ($form->{type} eq "request_quotation");
1391 $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
1392 if ($form->{type} eq "purchase_order");
1396 mkdir($path, 0770) or die "can't make directory $!\n";
1400 foreach $file (@files) {
1401 $file =~ /\/([^\/]*)$/;
1403 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1405 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1406 $form->{WEBDAV}{$fname} = $link;
1411 $main::lxdebug->leave_sub();