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} if $form->{sort};
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}) {
227 $form->parse_amount($myconfig, $form->{"${_}_$i"})
230 if ($form->{"qty_$i"}) {
232 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
233 qw(partnumber description unit);
235 # set values to 0 if nothing entered
236 $form->{"discount_$i"} =
237 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
239 $form->{"sellprice_$i"} =
240 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
241 $fxsellprice = $form->{"sellprice_$i"};
243 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
245 my $decimalplaces = ($dec > 2) ? $dec : 2;
248 $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
250 $form->{"sellprice_$i"} =
251 $form->round_amount($form->{"sellprice_$i"} - $discount,
254 $form->{"inventory_accno_$i"} *= 1;
255 $form->{"expense_accno_$i"} *= 1;
258 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
260 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
264 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
266 if ($form->{taxincluded}) {
267 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
268 $taxbase = $linetotal - $taxamount;
270 # we are not keeping a natural price, do not round
271 $form->{"sellprice_$i"} =
272 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
274 $taxamount = $linetotal * $taxrate;
275 $taxbase = $linetotal;
278 if ($form->round_amount($taxrate, 7) == 0) {
279 if ($form->{taxincluded}) {
280 foreach $item (@taxaccounts) {
282 $form->round_amount($linetotal * $form->{"${item}_rate"} /
283 (1 + abs($form->{"${item}_rate"})),
286 $taxaccounts{$item} += $taxamount;
287 $taxdiff += $taxamount;
289 $taxbase{$item} += $taxbase;
291 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
293 foreach $item (@taxaccounts) {
294 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
295 $taxbase{$item} += $taxbase;
299 foreach $item (@taxaccounts) {
300 $taxaccounts{$item} +=
301 $taxamount * $form->{"${item}_rate"} / $taxrate;
302 $taxbase{$item} += $taxbase;
306 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
308 $project_id = 'NULL';
309 if ($form->{"projectnumber_$i"}) {
310 $project_id = $form->{"projectnumber_$i"};
313 ($form->{"reqdate_$i"}) ? qq|'$form->{"reqdate_$i"}'| : "NULL";
315 # get pricegroup_id and save ist
316 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_drag_$i"};
319 # save detail record in orderitems table
320 $query = qq|INSERT INTO orderitems (|;
321 $query .= "id, " if $form->{"orderitems_id_$i"};
322 $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
323 unit, reqdate, project_id, serialnumber, ship, pricegroup_id,
324 ordnumber, transdate, cusordnumber)
326 $query .= qq|$form->{"orderitems_id_$i"},|
327 if $form->{"orderitems_id_$i"};
328 $query .= qq|$form->{id}, $form->{"id_$i"},
329 '$form->{"description_$i"}', $form->{"qty_$i"},
330 $fxsellprice, $form->{"discount_$i"},
331 '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
332 '$form->{"serialnumber_$i"}', $form->{"ship_$i"}, '$pricegroup_id',
333 '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}')|;
334 $dbh->do($query) || $form->dberror($query);
336 $form->{"sellprice_$i"} = $fxsellprice;
337 $form->{"discount_$i"} *= 100;
341 # set values which could be empty
342 map { $form->{$_} *= 1 }
343 qw(vendor_id customer_id taxincluded closed quotation);
345 $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
349 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
351 $amount = $form->round_amount($netamount + $tax, 2);
352 $netamount = $form->round_amount($netamount, 2);
354 if ($form->{currency} eq $form->{defaultcurrency}) {
355 $form->{exchangerate} = 1;
358 $form->check_exchangerate($myconfig,
361 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
364 $form->{exchangerate} =
367 : $form->parse_amount($myconfig, $form->{exchangerate});
371 # fill in subject if there is none
372 if ($form->{type} =~ /_order$/) {
374 $form->{subject} = qq|$form->{label} $form->{ordnumber}|
375 unless $form->{subject};
378 $form->{subject} = qq|$form->{label} $form->{quonumber}|
379 unless $form->{subject};
382 # if there is a message stuff it into the intnotes
383 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
384 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
385 my $now = scalar localtime;
386 $form->{intnotes} .= qq|\r
387 \r| if $form->{intnotes};
389 $form->{intnotes} .= qq|[email]\r
392 $cc${bcc}Subject: $form->{subject}\r
394 Message: $form->{message}\r| if $form->{message};
396 ($null, $form->{department_id}) = split(/--/, $form->{department});
397 $form->{department_id} *= 1;
400 $query = qq|UPDATE oe set
401 ordnumber = '$form->{ordnumber}',
402 quonumber = '$form->{quonumber}',
403 cusordnumber = '$form->{cusordnumber}',
404 transdate = '$form->{transdate}',
405 vendor_id = $form->{vendor_id},
406 customer_id = $form->{customer_id},
408 netamount = $netamount,
410 taxincluded = '$form->{taxincluded}',
411 shippingpoint = '$form->{shippingpoint}',
412 shipvia = '$form->{shipvia}',
413 notes = '$form->{notes}',
414 intnotes = '$form->{intnotes}',
415 curr = '$form->{currency}',
416 closed = '$form->{closed}',
417 quotation = '$quotation',
418 department_id = $form->{department_id},
419 employee_id = $form->{employee_id},
420 cp_id = $form->{contact_id}
421 WHERE id = $form->{id}|;
422 $dbh->do($query) || $form->dberror($query);
424 $form->{ordtotal} = $amount;
426 if ($form->{webdav}) {
427 &webdav_folder($myconfig, $form);
431 $form->{name} = $form->{ $form->{vc} };
432 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
433 $form->add_shipto($dbh, $form->{id});
435 # save printed, emailed, queued
436 $form->save_status($dbh);
438 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
439 if ($form->{vc} eq 'customer') {
440 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
441 $form->{exchangerate}, 0);
443 if ($form->{vc} eq 'vendor') {
444 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
445 0, $form->{exchangerate});
449 if ($form->{type} =~ /_order$/) {
452 &adj_onhand($dbh, $form, $ml * -1);
453 &adj_inventory($dbh, $myconfig, $form);
456 my $rc = $dbh->commit;
459 $main::lxdebug->leave_sub();
464 # this function closes multiple orders given in $form->{ordnumber_#}.
465 # use this for multiple orders that don't have to be saved back
466 # single orders should use OE::save instead.
468 $main::lxdebug->enter_sub();
470 my ($self, $myconfig, $form) = @_;
472 for my $i (1 .. $form->{rowcount}) {
476 $form->parse_amount($myconfig, $form->{"${_}_$i"})
479 $form->{"orderitems_id_$i"} = "";
482 if ($form->{"qty_$i"}) {
484 # set values to 0 if nothing entered
485 $form->{"discount_$i"} =
486 $form->parse_amount($myconfig, $form->{"discount_$i"});
488 $form->{"sellprice_$i"} =
489 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
494 map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} }
495 (1 .. $form->{rowcount});
497 my $dbh = $form->dbconnect($myconfig);
498 $query = qq|UPDATE oe SET
500 WHERE ordnumber IN (|
501 . join(', ', map { $dbh->quote($_) } @ids) . qq|)|;
502 $dbh->do($query) || $form->dberror($query);
505 $main::lxdebug->leave_sub();
509 $main::lxdebug->enter_sub();
511 my ($self, $myconfig, $form, $spool) = @_;
513 # connect to database
514 my $dbh = $form->dbconnect_noauto($myconfig);
517 my $query = qq|SELECT s.spoolfile FROM status s
518 WHERE s.trans_id = $form->{id}|;
519 $sth = $dbh->prepare($query);
520 $sth->execute || $self->dberror($query);
525 while (($spoolfile) = $sth->fetchrow_array) {
526 push @spoolfiles, $spoolfile;
530 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
531 WHERE o.trans_id = $form->{id}|;
532 $sth = $dbh->prepare($query);
533 $sth->execute || $self->dberror($query);
535 while (my ($id, $ship) = $sth->fetchrow_array) {
536 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
541 $query = qq|DELETE FROM inventory
542 WHERE oe_id = $form->{id}|;
543 $dbh->do($query) || $form->dberror($query);
545 # delete status entries
546 $query = qq|DELETE FROM status
547 WHERE trans_id = $form->{id}|;
548 $dbh->do($query) || $form->dberror($query);
551 $query = qq|DELETE FROM oe
552 WHERE id = $form->{id}|;
553 $dbh->do($query) || $form->dberror($query);
555 # delete individual entries
556 $query = qq|DELETE FROM orderitems
557 WHERE trans_id = $form->{id}|;
558 $dbh->do($query) || $form->dberror($query);
560 $query = qq|DELETE FROM shipto
561 WHERE trans_id = $form->{id}|;
562 $dbh->do($query) || $form->dberror($query);
564 my $rc = $dbh->commit;
568 foreach $spoolfile (@spoolfiles) {
569 unlink "$spool/$spoolfile" if $spoolfile;
573 $main::lxdebug->leave_sub();
579 $main::lxdebug->enter_sub();
581 my ($self, $myconfig, $form) = @_;
583 # connect to database
584 my $dbh = $form->dbconnect_noauto($myconfig);
588 # translate the ids (given by id_# and trans_id_#) into one array of ids, so we can join them later
590 push @ids, $form->{"trans_id_$_"}
591 if ($form->{"id_$_"} and $form->{"trans_id_$_"})
592 } (1 .. $form->{"rowcount"});
594 # if called in multi id mode, and still only got one id, switch back to single id
595 if ($form->{"rowcount"} and $#ids == 0) {
596 $form->{"id"} = $ids[0];
602 # get default accounts and last order number
603 $query = qq|SELECT (SELECT c.accno FROM chart c
604 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
605 (SELECT c.accno FROM chart c
606 WHERE d.income_accno_id = c.id) AS income_accno,
607 (SELECT c.accno FROM chart c
608 WHERE d.expense_accno_id = c.id) AS expense_accno,
609 (SELECT c.accno FROM chart c
610 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
611 (SELECT c.accno FROM chart c
612 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
616 $query = qq|SELECT (SELECT c.accno FROM chart c
617 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
618 (SELECT c.accno FROM chart c
619 WHERE d.income_accno_id = c.id) AS income_accno,
620 (SELECT c.accno FROM chart c
621 WHERE d.expense_accno_id = c.id) AS expense_accno,
622 (SELECT c.accno FROM chart c
623 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
624 (SELECT c.accno FROM chart c
625 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
626 d.curr AS currencies,
627 current_date AS transdate, current_date AS reqdate
630 my $sth = $dbh->prepare($query);
631 $sth->execute || $form->dberror($query);
633 my $ref = $sth->fetchrow_hashref(NAME_lc);
634 map { $form->{$_} = $ref->{$_} } keys %$ref;
637 ($form->{currency}) = split /:/, $form->{currencies};
639 # set reqdate if this is an invoice->order conversion. If someone knows a better check to ensure
640 # we come from invoices, feel free.
641 $form->{reqdate} = $form->{deliverydate}
642 if ( $form->{deliverydate}
643 and $form->{callback} =~ /action=ar_transactions/);
645 if ($form->{id} or @ids) {
647 # retrieve order for single id
648 # NOTE: this query is intended to fetch all information only ONCE.
649 # so if any of these infos is important (or even different) for any item,
650 # it will be killed out and then has to be fetched from the item scope query further down
651 $query = qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate,
652 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
653 o.curr AS currency, e.name AS employee, o.employee_id,
654 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
655 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
656 d.description AS department
658 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
659 LEFT JOIN employee e ON (o.employee_id = e.id)
660 LEFT JOIN department d ON (o.department_id = d.id)
663 ? qq|WHERE o.id = $form->{id}|
664 : qq|WHERE o.id IN (| . join(', ', @ids) . qq|)|);
666 #$main::lxdebug->message(0, $query);
668 $sth = $dbh->prepare($query);
669 $sth->execute || $form->dberror($query);
671 $ref = $sth->fetchrow_hashref(NAME_lc);
672 map { $form->{$_} = $ref->{$_} } keys %$ref;
674 # set all entries for multiple ids blank that yield different information
675 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
676 map { $form->{$_} = '' if ($ref->{$_} ne $form->{$_}) } keys %$ref;
679 # if not given, fill transdate with current_date
680 $form->{transdate} = $form->current_date($myconfig)
681 unless $form->{transdate};
685 # shipto and pinted/mailed/queued status makes only sense for single id retrieve
687 $query = qq|SELECT s.* FROM shipto s
688 WHERE s.trans_id = $form->{id}|;
689 $sth = $dbh->prepare($query);
690 $sth->execute || $form->dberror($query);
692 $ref = $sth->fetchrow_hashref(NAME_lc);
693 map { $form->{$_} = $ref->{$_} } keys %$ref;
696 # get printed, emailed and queued
697 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
699 WHERE s.trans_id = $form->{id}|;
700 $sth = $dbh->prepare($query);
701 $sth->execute || $form->dberror($query);
703 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
704 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
705 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
706 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
707 if $ref->{spoolfile};
710 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
713 my %oid = ('Pg' => 'oid',
714 'Oracle' => 'rowid');
716 # retrieve individual items
717 # this query looks up all information about the items
718 # stuff different from the whole will not be overwritten, but saved with a suffix.
719 $query = qq|SELECT o.id AS orderitems_id,
720 c1.accno AS inventory_accno,
721 c2.accno AS income_accno,
722 c3.accno AS expense_accno,
723 oe.ordnumber AS ordnumber_oe, oe.transdate AS transdate_oe, oe.cusordnumber AS cusordnumber_oe,
724 p.partnumber, p.assembly, o.description, o.qty,
725 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
726 o.reqdate, o.project_id, o.serialnumber, o.ship,
727 o.ordnumber, o.transdate, o.cusordnumber,
729 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
731 JOIN parts p ON (o.parts_id = p.id)
732 JOIN oe ON (o.trans_id = oe.id)
733 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
734 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
735 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
736 LEFT JOIN project pr ON (o.project_id = pr.id)
737 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
740 ? qq|WHERE o.trans_id = $form->{id}|
741 : qq|WHERE o.trans_id IN (| . join(", ", @ids) . qq|)|)
743 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
745 $sth = $dbh->prepare($query);
746 $sth->execute || $form->dberror($query);
748 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
750 # in collective order, copy global ordnumber, transdate, cusordnumber into item scope
751 # unless already present there
752 # remove _oe entries afterwards
753 map { $ref->{$_} = $ref->{"${_}_oe"} if ($ref->{$_} eq '') }
754 qw|ordnumber transdate cusordnumber|
756 map { delete $ref->{$_} } qw|ordnumber_oe transdate_oe cusordnumber_oe|;
758 #set expense_accno=inventory_accno if they are different => bilanz
760 ($ref->{expense_accno} != $ref->{inventory_accno})
761 ? $ref->{inventory_accno}
762 : $ref->{expense_accno};
764 # get tax rates and description
766 ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
767 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
769 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
771 $stw = $dbh->prepare($query);
772 $stw->execute || $form->dberror($query);
773 $ref->{taxaccounts} = "";
774 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
776 # if ($customertax{$ref->{accno}}) {
777 $ref->{taxaccounts} .= "$ptr->{accno} ";
778 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
779 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
780 $form->{"$ptr->{accno}_description"} = $ptr->{description};
781 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
782 $form->{taxaccounts} .= "$ptr->{accno} ";
787 chop $ref->{taxaccounts};
788 push @{ $form->{form_details} }, $ref;
796 $form->lastname_used($dbh, $myconfig, $form->{vc})
797 unless $form->{"$form->{vc}_id"};
801 $form->{exchangerate} =
802 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
803 ($form->{vc} eq 'customer') ? "buy" : "sell");
805 if ($form->{webdav}) {
806 &webdav_folder($myconfig, $form);
809 my $rc = $dbh->commit;
812 $main::lxdebug->leave_sub();
818 $main::lxdebug->enter_sub();
820 my ($self, $myconfig, $form) = @_;
822 # connect to database
823 my $dbh = $form->dbconnect($myconfig);
831 my %oid = ('Pg' => 'oid',
832 'Oracle' => 'rowid');
834 # sort items by partsgroup
835 for $i (1 .. $form->{rowcount}) {
837 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
838 $form->format_string("partsgroup_$i");
839 $partsgroup = $form->{"partsgroup_$i"};
841 push @partsgroup, [$i, $partsgroup];
844 # if there is a warehouse limit picking
845 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
847 # run query to check for inventory
848 $query = qq|SELECT sum(i.qty) AS qty
851 AND i.warehouse_id = ?|;
852 $sth = $dbh->prepare($query) || $form->dberror($query);
854 for $i (1 .. $form->{rowcount}) {
855 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
857 ($qty) = $sth->fetchrow_array;
860 $form->{"qty_$i"} = 0 if $qty == 0;
862 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
863 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
869 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
872 if ($item->[1] ne $sameitem) {
873 push(@{ $form->{description} }, qq|$item->[1]|);
874 $sameitem = $item->[1];
876 map { push(@{ $form->{$_} }, "") }
877 qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
880 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
882 if ($form->{"qty_$i"} != 0) {
884 # add number, description and qty to $form->{number}, ....
885 push(@{ $form->{runningnumber} }, $i);
886 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
887 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
888 push(@{ $form->{qty} },
889 $form->format_amount($myconfig, $form->{"qty_$i"}));
890 push(@{ $form->{ship} },
891 $form->format_amount($myconfig, $form->{"ship_$i"}));
892 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
893 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
894 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
895 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
896 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
898 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
900 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
902 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
903 my ($dec) = ($sellprice =~ /\.(\d+)/);
905 my $decimalplaces = ($dec > 2) ? $dec : 2;
909 $sellprice * $form->parse_amount($myconfig,
910 $form->{"discount_$i"}) / 100,
914 $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
916 # keep a netprice as well, (sellprice - discount)
917 #$form->{"netprice_$i"} = $sellprice - $discount;
918 $form->{"netprice_$i"} = $sellprice - $i_discount;
921 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
923 push(@{ $form->{netprice} },
924 ($form->{"netprice_$i"} != 0)
925 ? $form->format_amount(
926 $myconfig, $form->{"netprice_$i"},
933 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
935 $linetotal = ($linetotal != 0) ? $linetotal : " ";
937 push(@{ $form->{discount} }, $discount);
938 push(@{ $form->{p_discount} }, $form->{"discount_$i"});
940 $form->{ordtotal} += $linetotal;
942 push(@{ $form->{linetotal} },
943 $form->format_amount($myconfig, $linetotal, 2));
945 my ($taxamount, $taxbase);
948 map { $taxrate += $form->{"${_}_rate"} } split / /,
949 $form->{"taxaccounts_$i"};
951 if ($form->{taxincluded}) {
954 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
955 $taxbase = $linetotal / (1 + $taxrate);
957 $taxamount = $linetotal * $taxrate;
958 $taxbase = $linetotal;
961 if ($taxamount != 0) {
962 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
963 $taxaccounts{$item} +=
964 $taxamount * $form->{"${item}_rate"} / $taxrate;
965 $taxbase{$item} += $taxbase;
969 $tax_rate = $taxrate * 100;
970 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
972 if ($form->{"assembly_$i"}) {
975 # get parts and push them onto the stack
977 if ($form->{groupitems}) {
979 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
981 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
984 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
987 JOIN parts p ON (a.parts_id = p.id)
988 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
990 AND a.id = '$form->{"id_$i"}'
992 $sth = $dbh->prepare($query);
993 $sth->execute || $form->dberror($query);
995 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
996 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
997 map { push(@{ $form->{$_} }, "") }
998 qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
999 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
1000 push(@{ $form->{description} }, $sameitem);
1003 push(@{ $form->{description} },
1004 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
1006 . qq|, $ref->{partnumber}, $ref->{description}|);
1008 map { push(@{ $form->{$_} }, "") }
1009 qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
1019 foreach $item (sort keys %taxaccounts) {
1020 if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
1021 push(@{ $form->{taxbase} },
1022 $form->format_amount($myconfig, $taxbase{$item}, 2));
1024 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
1026 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
1027 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
1028 push(@{ $form->{taxrate} },
1029 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
1030 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
1034 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1036 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
1039 $form->{quototal} = $form->{ordtotal} =
1040 $form->format_amount($myconfig, $form->{ordtotal}, 2);
1042 # myconfig variables
1043 map { $form->{$_} = $myconfig->{$_} }
1044 (qw(company address tel fax signature businessnumber));
1045 $form->{username} = $myconfig->{name};
1049 $main::lxdebug->leave_sub();
1052 sub project_description {
1053 $main::lxdebug->enter_sub();
1055 my ($self, $dbh, $id) = @_;
1057 my $query = qq|SELECT p.description
1060 my $sth = $dbh->prepare($query);
1061 $sth->execute || $form->dberror($query);
1063 ($_) = $sth->fetchrow_array;
1067 $main::lxdebug->leave_sub();
1072 sub get_warehouses {
1073 $main::lxdebug->enter_sub();
1075 my ($self, $myconfig, $form) = @_;
1077 my $dbh = $form->dbconnect($myconfig);
1080 my $query = qq|SELECT id, description
1083 my $sth = $dbh->prepare($query);
1084 $sth->execute || $form->dberror($query);
1086 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1087 push @{ $form->{all_warehouses} }, $ref;
1093 $main::lxdebug->leave_sub();
1096 sub save_inventory {
1097 $main::lxdebug->enter_sub();
1099 my ($self, $myconfig, $form) = @_;
1101 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1105 ($null, $employee_id) = split /--/, $form->{employee};
1107 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
1109 my $dbh = $form->dbconnect_noauto($myconfig);
1115 $query = qq|SELECT o.serialnumber, o.ship
1117 WHERE o.trans_id = ?
1120 $sth = $dbh->prepare($query) || $form->dberror($query);
1122 $query = qq|SELECT sum(i.qty)
1124 WHERE i.parts_id = ?
1125 AND i.warehouse_id = ?|;
1126 $wth = $dbh->prepare($query) || $form->dberror($query);
1128 for my $i (1 .. $form->{rowcount} - 1) {
1131 (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
1133 : $form->{"ship_$i"};
1135 if ($warehouse_id && $form->{type} eq 'ship_order') {
1137 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1139 ($qty) = $wth->fetchrow_array;
1150 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1151 qty, oe_id, orderitems_id, shippingdate, employee_id)
1152 VALUES ($form->{"id_$i"}, $warehouse_id,
1153 $ship, $form->{"id"},
1154 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1156 $dbh->do($query) || $form->dberror($query);
1158 # add serialnumber, ship to orderitems
1159 $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
1161 ($serialnumber, $ship) = $sth->fetchrow_array;
1164 $serialnumber .= " " if $serialnumber;
1165 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1166 $ship += $form->{"ship_$i"};
1168 $query = qq|UPDATE orderitems SET
1169 serialnumber = '$serialnumber',
1171 WHERE trans_id = $form->{id}
1172 AND id = $form->{"orderitems_id_$i"}|;
1173 $dbh->do($query) || $form->dberror($query);
1175 # update order with ship via
1176 $query = qq|UPDATE oe SET
1177 shippingpoint = '$form->{shippingpoint}',
1178 shipvia = '$form->{shipvia}'
1179 WHERE id = $form->{id}|;
1180 $dbh->do($query) || $form->dberror($query);
1182 # update onhand for parts
1183 $form->update_balance($dbh, "parts", "onhand",
1184 qq|id = $form->{"id_$i"}|,
1185 $form->{"ship_$i"} * $ml);
1190 my $rc = $dbh->commit;
1193 $main::lxdebug->leave_sub();
1199 $main::lxdebug->enter_sub();
1201 my ($dbh, $form, $ml) = @_;
1203 my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
1205 JOIN parts p ON (p.id = oi.parts_id)
1206 WHERE oi.trans_id = $form->{id}|;
1207 my $sth = $dbh->prepare($query);
1208 $sth->execute || $form->dberror($query);
1210 $query = qq|SELECT sum(p.inventory_accno_id)
1212 JOIN assembly a ON (a.parts_id = p.id)
1214 my $ath = $dbh->prepare($query) || $form->dberror($query);
1218 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1220 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1222 # do not update if assembly consists of all services
1223 if ($ref->{assembly}) {
1224 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1226 ($ispa) = $sth->fetchrow_array;
1233 # adjust onhand in parts table
1234 $form->update_balance($dbh, "parts", "onhand",
1235 qq|id = $ref->{parts_id}|,
1236 $ref->{ship} * $ml);
1242 $main::lxdebug->leave_sub();
1246 $main::lxdebug->enter_sub();
1248 my ($dbh, $myconfig, $form) = @_;
1250 my %oid = ('Pg' => 'oid',
1251 'Oracle' => 'rowid');
1253 # increase/reduce qty in inventory table
1254 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1256 WHERE oi.trans_id = $form->{id}|;
1257 my $sth = $dbh->prepare($query);
1258 $sth->execute || $form->dberror($query);
1260 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1261 (SELECT SUM(qty) FROM inventory
1262 WHERE oe_id = $form->{id}
1263 AND orderitems_id = ?) AS total
1265 WHERE oe_id = $form->{id}
1266 AND orderitems_id = ?|;
1267 my $ith = $dbh->prepare($query) || $form->dberror($query);
1270 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1272 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1274 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1276 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1278 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1279 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1281 $form->update_balance($dbh, "inventory", "qty",
1282 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1291 # delete inventory entries if qty = 0
1292 $query = qq|DELETE FROM inventory
1293 WHERE oe_id = $form->{id}
1295 $dbh->do($query) || $form->dberror($query);
1297 $main::lxdebug->leave_sub();
1301 $main::lxdebug->enter_sub();
1303 my ($self, $myconfig, $form) = @_;
1305 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1308 my $dbh = $form->dbconnect($myconfig);
1310 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1313 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1314 WHERE p.onhand > 0|;
1316 if ($form->{partnumber}) {
1317 $var = $form->like(lc $form->{partnumber});
1319 AND lower(p.partnumber) LIKE '$var'";
1321 if ($form->{description}) {
1322 $var = $form->like(lc $form->{description});
1324 AND lower(p.description) LIKE '$var'";
1326 if ($form->{partsgroup}) {
1327 $var = $form->like(lc $form->{partsgroup});
1329 AND lower(pg.partsgroup) LIKE '$var'";
1332 $sth = $dbh->prepare($query);
1333 $sth->execute || $form->dberror($query);
1335 $query = qq|SELECT sum(i.qty), w.description, w.id
1337 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1338 WHERE i.parts_id = ?
1339 AND NOT i.warehouse_id = $warehouse_id
1340 GROUP BY w.description, w.id|;
1341 $wth = $dbh->prepare($query) || $form->dberror($query);
1343 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1345 $wth->execute($ref->{id}) || $form->dberror;
1347 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1348 push @{ $form->{all_inventory} },
1349 { 'id' => $ref->{id},
1350 'partnumber' => $ref->{partnumber},
1351 'description' => $ref->{description},
1352 'partsgroup' => $ref->{partsgroup},
1354 'warehouse_id' => $warehouse_id,
1355 'warehouse' => $warehouse }
1365 @{ $form->{all_inventory} } =
1366 sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
1367 @{ $form->{all_inventory} };
1369 $main::lxdebug->leave_sub();
1371 return @{ $form->{all_inventory} };
1375 $main::lxdebug->enter_sub();
1377 my ($self, $myconfig, $form) = @_;
1379 my $dbh = $form->dbconnect_noauto($myconfig);
1381 my $query = qq|INSERT INTO inventory
1382 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1383 VALUES (?, ?, ?, ?, ?)|;
1384 $sth = $dbh->prepare($query) || $form->dberror($query);
1386 $form->get_employee($dbh);
1391 $shippingdate = "$a[5]-$a[4]-$a[3]";
1393 for my $i (1 .. $form->{rowcount}) {
1394 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1396 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1401 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
1402 $shippingdate, $form->{employee_id})
1408 $sth->execute($form->{"warehouse_id_$i"},
1409 $form->{"id_$i"}, $qty * -1, $shippingdate,
1410 $form->{employee_id})
1417 my $rc = $dbh->commit;
1420 $main::lxdebug->leave_sub();
1426 $main::lxdebug->enter_sub();
1428 my ($myconfig, $form) = @_;
1431 $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
1432 if ($form->{type} eq "sales_quotation");
1433 $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
1434 if ($form->{type} eq "sales_order");
1435 $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
1436 if ($form->{type} eq "request_quotation");
1437 $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
1438 if ($form->{type} eq "purchase_order");
1442 mkdir($path, 0770) or die "can't make directory $!\n";
1446 foreach $file (@files) {
1447 $file =~ /\/([^\/]*)$/;
1449 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1451 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1452 $form->{WEBDAV}{$fname} = $link;
1457 $main::lxdebug->leave_sub();