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"};
318 # save detail record in orderitems table
319 $query = qq|INSERT INTO orderitems (|;
320 $query .= "id, " if $form->{"orderitems_id_$i"};
321 $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
322 unit, reqdate, project_id, serialnumber, ship, pricegroup_id)
324 $query .= qq|$form->{"orderitems_id_$i"},|
325 if $form->{"orderitems_id_$i"};
326 $query .= qq|$form->{id}, $form->{"id_$i"},
327 '$form->{"description_$i"}', $form->{"qty_$i"},
328 $fxsellprice, $form->{"discount_$i"},
329 '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
330 '$form->{"serialnumber_$i"}', $form->{"ship_$i"},
332 $dbh->do($query) || $form->dberror($query);
334 $form->{"sellprice_$i"} = $fxsellprice;
335 $form->{"discount_$i"} *= 100;
339 # set values which could be empty
340 map { $form->{$_} *= 1 }
341 qw(vendor_id customer_id taxincluded closed quotation);
343 $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
347 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
349 $amount = $form->round_amount($netamount + $tax, 2);
350 $netamount = $form->round_amount($netamount, 2);
352 if ($form->{currency} eq $form->{defaultcurrency}) {
353 $form->{exchangerate} = 1;
356 $form->check_exchangerate($myconfig,
359 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
362 $form->{exchangerate} =
365 : $form->parse_amount($myconfig, $form->{exchangerate});
369 # fill in subject if there is none
370 if ($form->{type} =~ /_order$/) {
372 $form->{subject} = qq|$form->{label} $form->{ordnumber}|
373 unless $form->{subject};
376 $form->{subject} = qq|$form->{label} $form->{quonumber}|
377 unless $form->{subject};
380 # if there is a message stuff it into the intnotes
381 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
382 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
383 my $now = scalar localtime;
384 $form->{intnotes} .= qq|\r
385 \r| if $form->{intnotes};
387 $form->{intnotes} .= qq|[email]\r
390 $cc${bcc}Subject: $form->{subject}\r
392 Message: $form->{message}\r| if $form->{message};
394 ($null, $form->{department_id}) = split(/--/, $form->{department});
395 $form->{department_id} *= 1;
398 $query = qq|UPDATE oe set
399 ordnumber = '$form->{ordnumber}',
400 quonumber = '$form->{quonumber}',
401 cusordnumber = '$form->{cusordnumber}',
402 transdate = '$form->{transdate}',
403 vendor_id = $form->{vendor_id},
404 customer_id = $form->{customer_id},
406 netamount = $netamount,
408 taxincluded = '$form->{taxincluded}',
409 shippingpoint = '$form->{shippingpoint}',
410 shipvia = '$form->{shipvia}',
411 notes = '$form->{notes}',
412 intnotes = '$form->{intnotes}',
413 curr = '$form->{currency}',
414 closed = '$form->{closed}',
415 quotation = '$quotation',
416 department_id = $form->{department_id},
417 employee_id = $form->{employee_id},
418 cp_id = $form->{contact_id}
419 WHERE id = $form->{id}|;
420 $dbh->do($query) || $form->dberror($query);
422 $form->{ordtotal} = $amount;
424 if ($form->{webdav}) {
425 &webdav_folder($myconfig, $form);
429 $form->{name} = $form->{ $form->{vc} };
430 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
431 $form->add_shipto($dbh, $form->{id});
433 # save printed, emailed, queued
434 $form->save_status($dbh);
436 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
437 if ($form->{vc} eq 'customer') {
438 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
439 $form->{exchangerate}, 0);
441 if ($form->{vc} eq 'vendor') {
442 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
443 0, $form->{exchangerate});
447 if ($form->{type} =~ /_order$/) {
450 &adj_onhand($dbh, $form, $ml * -1);
451 &adj_inventory($dbh, $myconfig, $form);
454 my $rc = $dbh->commit;
457 $main::lxdebug->leave_sub();
462 # this function closes multiple orders given in $form->{ordnumber_#}.
463 # use this for multiple orders that don't have to be saved back
464 # single orders should use OE::save instead.
466 $main::lxdebug->enter_sub();
468 my ($self, $myconfig ,$form) = @_;
470 for my $i (1 .. $form->{rowcount}) {
473 $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"})
476 $form->{"orderitems_id_$i"} = "";
479 if ($form->{"qty_$i"}) {
481 # set values to 0 if nothing entered
482 $form->{"discount_$i"} =
483 $form->parse_amount($myconfig, $form->{"discount_$i"});
485 $form->{"sellprice_$i"} =
486 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
490 map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} } (1 .. $form->{rowcount});
492 my $dbh = $form->dbconnect($myconfig);
493 $query = qq|UPDATE oe SET
495 WHERE ordnumber IN (|.join(', ', map{ $dbh->quote($_) }@ids).qq|)|;
496 $dbh->do($query) || $form->dberror($query);
499 $main::lxdebug->leave_sub();
503 $main::lxdebug->enter_sub();
505 my ($self, $myconfig, $form, $spool) = @_;
507 # connect to database
508 my $dbh = $form->dbconnect_noauto($myconfig);
511 my $query = qq|SELECT s.spoolfile FROM status s
512 WHERE s.trans_id = $form->{id}|;
513 $sth = $dbh->prepare($query);
514 $sth->execute || $self->dberror($query);
519 while (($spoolfile) = $sth->fetchrow_array) {
520 push @spoolfiles, $spoolfile;
524 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
525 WHERE o.trans_id = $form->{id}|;
526 $sth = $dbh->prepare($query);
527 $sth->execute || $self->dberror($query);
529 while (my ($id, $ship) = $sth->fetchrow_array) {
530 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
535 $query = qq|DELETE FROM inventory
536 WHERE oe_id = $form->{id}|;
537 $dbh->do($query) || $form->dberror($query);
539 # delete status entries
540 $query = qq|DELETE FROM status
541 WHERE trans_id = $form->{id}|;
542 $dbh->do($query) || $form->dberror($query);
545 $query = qq|DELETE FROM oe
546 WHERE id = $form->{id}|;
547 $dbh->do($query) || $form->dberror($query);
549 # delete individual entries
550 $query = qq|DELETE FROM orderitems
551 WHERE trans_id = $form->{id}|;
552 $dbh->do($query) || $form->dberror($query);
554 $query = qq|DELETE FROM shipto
555 WHERE trans_id = $form->{id}|;
556 $dbh->do($query) || $form->dberror($query);
558 my $rc = $dbh->commit;
562 foreach $spoolfile (@spoolfiles) {
563 unlink "$spool/$spoolfile" if $spoolfile;
567 $main::lxdebug->leave_sub();
573 $main::lxdebug->enter_sub();
575 my ($self, $myconfig, $form) = @_;
577 # connect to database
578 my $dbh = $form->dbconnect_noauto($myconfig);
582 # translate the ids (given by id_# and trans_id_#) into one array of ids, so we can join them later
583 map { push @ids, $form->{"trans_id_$_"} if ($form->{"id_$_"}) } (1 .. $form->{"rowcount"});
585 # if called in multi id mode, and still only got one id, switch back to single id
586 if ($form->{"rowcount"} and $#ids == 0) {
587 $form->{"id"} = $ids[0];
593 # get default accounts and last order number
594 $query = qq|SELECT (SELECT c.accno FROM chart c
595 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
596 (SELECT c.accno FROM chart c
597 WHERE d.income_accno_id = c.id) AS income_accno,
598 (SELECT c.accno FROM chart c
599 WHERE d.expense_accno_id = c.id) AS expense_accno,
600 (SELECT c.accno FROM chart c
601 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
602 (SELECT c.accno FROM chart c
603 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
607 $query = qq|SELECT (SELECT c.accno FROM chart c
608 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
609 (SELECT c.accno FROM chart c
610 WHERE d.income_accno_id = c.id) AS income_accno,
611 (SELECT c.accno FROM chart c
612 WHERE d.expense_accno_id = c.id) AS expense_accno,
613 (SELECT c.accno FROM chart c
614 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
615 (SELECT c.accno FROM chart c
616 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
617 d.curr AS currencies,
618 current_date AS transdate, current_date AS reqdate
621 my $sth = $dbh->prepare($query);
622 $sth->execute || $form->dberror($query);
624 my $ref = $sth->fetchrow_hashref(NAME_lc);
625 map { $form->{$_} = $ref->{$_} } keys %$ref;
628 ($form->{currency}) = split /:/, $form->{currencies};
630 if ($form->{id} or @ids) {
632 # retrieve order for single id
633 # NOTE: this query is intended to fetch all information only ONCE.
634 # so if any of these infos is important (or even different) for any item,
635 # it will be killed out and then has to be fetched from the item scope query further down
636 $query = qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate,
637 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
638 o.curr AS currency, e.name AS employee, o.employee_id,
639 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
640 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
641 d.description AS department
643 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
644 LEFT JOIN employee e ON (o.employee_id = e.id)
645 LEFT JOIN department d ON (o.department_id = d.id)
647 ? qq|WHERE o.id = $form->{id}|
648 : qq|WHERE o.id IN (|.join(', ', @ids).qq|)|
651 #$main::lxdebug->message(0, $query);
653 $sth = $dbh->prepare($query);
654 $sth->execute || $form->dberror($query);
656 $ref = $sth->fetchrow_hashref(NAME_lc);
657 map { $form->{$_} = $ref->{$_} } keys %$ref;
659 # destroy all entries for multiple ids that yield different information
660 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
661 map { undef $form->{$_} if ($ref->{$_} ne $form->{$_}) } keys %$ref;
664 # if not given, fill transdate with current_date
665 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
669 # shipto and pinted/mailed/queued status makes only sense for single id retrieve
671 $query = qq|SELECT s.* FROM shipto s
672 WHERE s.trans_id = $form->{id}|;
673 $sth = $dbh->prepare($query);
674 $sth->execute || $form->dberror($query);
676 $ref = $sth->fetchrow_hashref(NAME_lc);
677 map { $form->{$_} = $ref->{$_} } keys %$ref;
680 # get printed, emailed and queued
681 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
683 WHERE s.trans_id = $form->{id}|;
684 $sth = $dbh->prepare($query);
685 $sth->execute || $form->dberror($query);
687 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
688 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
689 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
690 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
693 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
696 my %oid = ('Pg' => 'oid',
697 'Oracle' => 'rowid');
699 # retrieve individual items
700 # this query looks up all information about the items
701 # stuff different from the whole will not be overwritten, but saved with a suffix.
702 $query = qq|SELECT o.id AS orderitems_id,
703 c1.accno AS inventory_accno,
704 c2.accno AS income_accno,
705 c3.accno AS expense_accno,
706 oe.ordnumber, oe.transdate, oe.cusordnumber,
707 p.partnumber, p.assembly, o.description, o.qty,
708 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
709 o.reqdate, o.project_id, o.serialnumber, o.ship,
711 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
713 JOIN parts p ON (o.parts_id = p.id)
714 JOIN oe ON (o.trans_id = oe.id)
715 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
716 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
717 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
718 LEFT JOIN project pr ON (o.project_id = pr.id)
719 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
721 ? qq|WHERE o.trans_id = $form->{id}|
722 : qq|WHERE o.trans_id IN (|.join(", ", @ids).qq|)|
724 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
726 $sth = $dbh->prepare($query);
727 $sth->execute || $form->dberror($query);
729 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
731 #set expense_accno=inventory_accno if they are different => bilanz
733 ($ref->{expense_accno} != $ref->{inventory_accno})
734 ? $ref->{inventory_accno}
735 : $ref->{expense_accno};
737 # get tax rates and description
739 ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
740 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
742 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
744 $stw = $dbh->prepare($query);
745 $stw->execute || $form->dberror($query);
746 $ref->{taxaccounts} = "";
747 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
749 # if ($customertax{$ref->{accno}}) {
750 $ref->{taxaccounts} .= "$ptr->{accno} ";
751 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
752 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
753 $form->{"$ptr->{accno}_description"} = $ptr->{description};
754 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
755 $form->{taxaccounts} .= "$ptr->{accno} ";
760 chop $ref->{taxaccounts};
761 push @{ $form->{form_details} }, $ref;
769 $form->lastname_used($dbh, $myconfig, $form->{vc})
770 unless $form->{"$form->{vc}_id"};
774 $form->{exchangerate} =
775 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
776 ($form->{vc} eq 'customer') ? "buy" : "sell");
778 if ($form->{webdav}) {
779 &webdav_folder($myconfig, $form);
782 my $rc = $dbh->commit;
785 $main::lxdebug->leave_sub();
791 $main::lxdebug->enter_sub();
793 my ($self, $myconfig, $form) = @_;
795 # connect to database
796 my $dbh = $form->dbconnect($myconfig);
804 my %oid = ('Pg' => 'oid',
805 'Oracle' => 'rowid');
807 # sort items by partsgroup
808 for $i (1 .. $form->{rowcount}) {
810 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
811 $form->format_string("partsgroup_$i");
812 $partsgroup = $form->{"partsgroup_$i"};
814 push @partsgroup, [$i, $partsgroup];
817 # if there is a warehouse limit picking
818 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
820 # run query to check for inventory
821 $query = qq|SELECT sum(i.qty) AS qty
824 AND i.warehouse_id = ?|;
825 $sth = $dbh->prepare($query) || $form->dberror($query);
827 for $i (1 .. $form->{rowcount}) {
828 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
830 ($qty) = $sth->fetchrow_array;
833 $form->{"qty_$i"} = 0 if $qty == 0;
835 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
836 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
842 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
845 if ($item->[1] ne $sameitem) {
846 push(@{ $form->{description} }, qq|$item->[1]|);
847 $sameitem = $item->[1];
849 map { push(@{ $form->{$_} }, "") }
850 qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
853 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
855 if ($form->{"qty_$i"} != 0) {
857 # add number, description and qty to $form->{number}, ....
858 push(@{ $form->{runningnumber} }, $i);
859 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
860 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
861 push(@{ $form->{qty} },
862 $form->format_amount($myconfig, $form->{"qty_$i"}));
863 push(@{ $form->{ship} },
864 $form->format_amount($myconfig, $form->{"ship_$i"}));
865 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
866 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
867 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
868 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
869 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
871 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
873 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
875 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
876 my ($dec) = ($sellprice =~ /\.(\d+)/);
878 my $decimalplaces = ($dec > 2) ? $dec : 2;
882 $sellprice * $form->parse_amount($myconfig,
883 $form->{"discount_$i"}) / 100,
886 # keep a netprice as well, (sellprice - discount)
887 $form->{"netprice_$i"} = $sellprice - $discount;
890 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
892 push(@{ $form->{netprice} },
893 ($form->{"netprice_$i"} != 0)
894 ? $form->format_amount(
895 $myconfig, $form->{"netprice_$i"},
902 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
904 $linetotal = ($linetotal != 0) ? $linetotal : " ";
906 push(@{ $form->{discount} }, $discount);
908 $form->{ordtotal} += $linetotal;
910 push(@{ $form->{linetotal} },
911 $form->format_amount($myconfig, $linetotal, 2));
913 my ($taxamount, $taxbase);
916 map { $taxrate += $form->{"${_}_rate"} } split / /,
917 $form->{"taxaccounts_$i"};
919 if ($form->{taxincluded}) {
922 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
923 $taxbase = $linetotal / (1 + $taxrate);
925 $taxamount = $linetotal * $taxrate;
926 $taxbase = $linetotal;
929 if ($taxamount != 0) {
930 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
931 $taxaccounts{$item} +=
932 $taxamount * $form->{"${item}_rate"} / $taxrate;
933 $taxbase{$item} += $taxbase;
937 $tax_rate = $taxrate*100;
938 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
940 if ($form->{"assembly_$i"}) {
943 # get parts and push them onto the stack
945 if ($form->{groupitems}) {
947 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
949 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
952 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
955 JOIN parts p ON (a.parts_id = p.id)
956 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
958 AND a.id = '$form->{"id_$i"}'
960 $sth = $dbh->prepare($query);
961 $sth->execute || $form->dberror($query);
963 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
964 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
965 map { push(@{ $form->{$_} }, "") }
966 qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
967 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
968 push(@{ $form->{description} }, $sameitem);
971 push(@{ $form->{description} },
972 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
974 . qq|, $ref->{partnumber}, $ref->{description}|);
976 map { push(@{ $form->{$_} }, "") }
977 qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
987 foreach $item (sort keys %taxaccounts) {
988 if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
989 push(@{ $form->{taxbase} },
990 $form->format_amount($myconfig, $taxbase{$item}, 2));
992 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
994 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
995 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
996 push(@{ $form->{taxrate} },
997 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
998 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
1002 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1004 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
1007 $form->{quototal} = $form->{ordtotal} =
1008 $form->format_amount($myconfig, $form->{ordtotal}, 2);
1010 # myconfig variables
1011 map { $form->{$_} = $myconfig->{$_} }
1012 (qw(company address tel fax signature businessnumber));
1013 $form->{username} = $myconfig->{name};
1017 $main::lxdebug->leave_sub();
1020 sub project_description {
1021 $main::lxdebug->enter_sub();
1023 my ($self, $dbh, $id) = @_;
1025 my $query = qq|SELECT p.description
1028 my $sth = $dbh->prepare($query);
1029 $sth->execute || $form->dberror($query);
1031 ($_) = $sth->fetchrow_array;
1035 $main::lxdebug->leave_sub();
1040 sub get_warehouses {
1041 $main::lxdebug->enter_sub();
1043 my ($self, $myconfig, $form) = @_;
1045 my $dbh = $form->dbconnect($myconfig);
1048 my $query = qq|SELECT id, description
1051 my $sth = $dbh->prepare($query);
1052 $sth->execute || $form->dberror($query);
1054 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1055 push @{ $form->{all_warehouses} }, $ref;
1061 $main::lxdebug->leave_sub();
1064 sub save_inventory {
1065 $main::lxdebug->enter_sub();
1067 my ($self, $myconfig, $form) = @_;
1069 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1073 ($null, $employee_id) = split /--/, $form->{employee};
1075 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
1077 my $dbh = $form->dbconnect_noauto($myconfig);
1083 $query = qq|SELECT o.serialnumber, o.ship
1085 WHERE o.trans_id = ?
1088 $sth = $dbh->prepare($query) || $form->dberror($query);
1090 $query = qq|SELECT sum(i.qty)
1092 WHERE i.parts_id = ?
1093 AND i.warehouse_id = ?|;
1094 $wth = $dbh->prepare($query) || $form->dberror($query);
1096 for my $i (1 .. $form->{rowcount} - 1) {
1099 (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
1101 : $form->{"ship_$i"};
1103 if ($warehouse_id && $form->{type} eq 'ship_order') {
1105 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1107 ($qty) = $wth->fetchrow_array;
1118 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1119 qty, oe_id, orderitems_id, shippingdate, employee_id)
1120 VALUES ($form->{"id_$i"}, $warehouse_id,
1121 $ship, $form->{"id"},
1122 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1124 $dbh->do($query) || $form->dberror($query);
1126 # add serialnumber, ship to orderitems
1127 $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
1129 ($serialnumber, $ship) = $sth->fetchrow_array;
1132 $serialnumber .= " " if $serialnumber;
1133 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1134 $ship += $form->{"ship_$i"};
1136 $query = qq|UPDATE orderitems SET
1137 serialnumber = '$serialnumber',
1139 WHERE trans_id = $form->{id}
1140 AND id = $form->{"orderitems_id_$i"}|;
1141 $dbh->do($query) || $form->dberror($query);
1143 # update order with ship via
1144 $query = qq|UPDATE oe SET
1145 shippingpoint = '$form->{shippingpoint}',
1146 shipvia = '$form->{shipvia}'
1147 WHERE id = $form->{id}|;
1148 $dbh->do($query) || $form->dberror($query);
1150 # update onhand for parts
1151 $form->update_balance($dbh, "parts", "onhand",
1152 qq|id = $form->{"id_$i"}|,
1153 $form->{"ship_$i"} * $ml);
1158 my $rc = $dbh->commit;
1161 $main::lxdebug->leave_sub();
1167 $main::lxdebug->enter_sub();
1169 my ($dbh, $form, $ml) = @_;
1171 my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
1173 JOIN parts p ON (p.id = oi.parts_id)
1174 WHERE oi.trans_id = $form->{id}|;
1175 my $sth = $dbh->prepare($query);
1176 $sth->execute || $form->dberror($query);
1178 $query = qq|SELECT sum(p.inventory_accno_id)
1180 JOIN assembly a ON (a.parts_id = p.id)
1182 my $ath = $dbh->prepare($query) || $form->dberror($query);
1186 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1188 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1190 # do not update if assembly consists of all services
1191 if ($ref->{assembly}) {
1192 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1194 ($ispa) = $sth->fetchrow_array;
1201 # adjust onhand in parts table
1202 $form->update_balance($dbh, "parts", "onhand",
1203 qq|id = $ref->{parts_id}|,
1204 $ref->{ship} * $ml);
1210 $main::lxdebug->leave_sub();
1214 $main::lxdebug->enter_sub();
1216 my ($dbh, $myconfig, $form) = @_;
1218 my %oid = ('Pg' => 'oid',
1219 'Oracle' => 'rowid');
1221 # increase/reduce qty in inventory table
1222 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1224 WHERE oi.trans_id = $form->{id}|;
1225 my $sth = $dbh->prepare($query);
1226 $sth->execute || $form->dberror($query);
1228 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1229 (SELECT SUM(qty) FROM inventory
1230 WHERE oe_id = $form->{id}
1231 AND orderitems_id = ?) AS total
1233 WHERE oe_id = $form->{id}
1234 AND orderitems_id = ?|;
1235 my $ith = $dbh->prepare($query) || $form->dberror($query);
1238 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1240 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1242 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1244 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1246 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1247 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1249 $form->update_balance($dbh, "inventory", "qty",
1250 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1259 # delete inventory entries if qty = 0
1260 $query = qq|DELETE FROM inventory
1261 WHERE oe_id = $form->{id}
1263 $dbh->do($query) || $form->dberror($query);
1265 $main::lxdebug->leave_sub();
1269 $main::lxdebug->enter_sub();
1271 my ($self, $myconfig, $form) = @_;
1273 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1276 my $dbh = $form->dbconnect($myconfig);
1278 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1281 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1282 WHERE p.onhand > 0|;
1284 if ($form->{partnumber}) {
1285 $var = $form->like(lc $form->{partnumber});
1287 AND lower(p.partnumber) LIKE '$var'";
1289 if ($form->{description}) {
1290 $var = $form->like(lc $form->{description});
1292 AND lower(p.description) LIKE '$var'";
1294 if ($form->{partsgroup}) {
1295 $var = $form->like(lc $form->{partsgroup});
1297 AND lower(pg.partsgroup) LIKE '$var'";
1300 $sth = $dbh->prepare($query);
1301 $sth->execute || $form->dberror($query);
1303 $query = qq|SELECT sum(i.qty), w.description, w.id
1305 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1306 WHERE i.parts_id = ?
1307 AND NOT i.warehouse_id = $warehouse_id
1308 GROUP BY w.description, w.id|;
1309 $wth = $dbh->prepare($query) || $form->dberror($query);
1311 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1313 $wth->execute($ref->{id}) || $form->dberror;
1315 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1316 push @{ $form->{all_inventory} },
1317 { 'id' => $ref->{id},
1318 'partnumber' => $ref->{partnumber},
1319 'description' => $ref->{description},
1320 'partsgroup' => $ref->{partsgroup},
1322 'warehouse_id' => $warehouse_id,
1323 'warehouse' => $warehouse }
1333 @{ $form->{all_inventory} } =
1334 sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
1335 @{ $form->{all_inventory} };
1337 $main::lxdebug->leave_sub();
1339 return @{ $form->{all_inventory} };
1343 $main::lxdebug->enter_sub();
1345 my ($self, $myconfig, $form) = @_;
1347 my $dbh = $form->dbconnect_noauto($myconfig);
1349 my $query = qq|INSERT INTO inventory
1350 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1351 VALUES (?, ?, ?, ?, ?)|;
1352 $sth = $dbh->prepare($query) || $form->dberror($query);
1354 $form->get_employee($dbh);
1359 $shippingdate = "$a[5]-$a[4]-$a[3]";
1361 for my $i (1 .. $form->{rowcount}) {
1362 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1364 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1369 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
1370 $shippingdate, $form->{employee_id})
1376 $sth->execute($form->{"warehouse_id_$i"},
1377 $form->{"id_$i"}, $qty * -1, $shippingdate,
1378 $form->{employee_id})
1385 my $rc = $dbh->commit;
1388 $main::lxdebug->leave_sub();
1394 $main::lxdebug->enter_sub();
1396 my ($myconfig, $form) = @_;
1399 $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
1400 if ($form->{type} eq "sales_quotation");
1401 $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
1402 if ($form->{type} eq "sales_order");
1403 $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
1404 if ($form->{type} eq "request_quotation");
1405 $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
1406 if ($form->{type} eq "purchase_order");
1410 mkdir($path, 0770) or die "can't make directory $!\n";
1414 foreach $file (@files) {
1415 $file =~ /\/([^\/]*)$/;
1417 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1419 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1420 $form->{WEBDAV}{$fname} = $link;
1425 $main::lxdebug->leave_sub();