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);
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,
328 ordnumber, transdate, cusordnumber)
330 $query .= qq|$form->{"orderitems_id_$i"},|
331 if $form->{"orderitems_id_$i"};
332 $query .= qq|$form->{id}, $form->{"id_$i"},
333 '$form->{"description_$i"}', $form->{"qty_$i"},
334 $fxsellprice, $form->{"discount_$i"},
335 '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
336 '$form->{"serialnumber_$i"}', $form->{"ship_$i"}, '$pricegroup_id',
337 '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}')|;
338 $dbh->do($query) || $form->dberror($query);
340 $form->{"sellprice_$i"} = $fxsellprice;
341 $form->{"discount_$i"} *= 100;
345 # set values which could be empty
346 map { $form->{$_} *= 1 }
347 qw(vendor_id customer_id taxincluded closed quotation);
349 $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
353 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
355 $amount = $form->round_amount($netamount + $tax, 2);
356 $netamount = $form->round_amount($netamount, 2);
358 if ($form->{currency} eq $form->{defaultcurrency}) {
359 $form->{exchangerate} = 1;
362 $form->check_exchangerate($myconfig,
365 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
368 $form->{exchangerate} =
371 : $form->parse_amount($myconfig, $form->{exchangerate});
375 # fill in subject if there is none
376 if ($form->{type} =~ /_order$/) {
378 $form->{subject} = qq|$form->{label} $form->{ordnumber}|
379 unless $form->{subject};
382 $form->{subject} = qq|$form->{label} $form->{quonumber}|
383 unless $form->{subject};
386 # if there is a message stuff it into the intnotes
387 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
388 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
389 my $now = scalar localtime;
390 $form->{intnotes} .= qq|\r
391 \r| if $form->{intnotes};
393 $form->{intnotes} .= qq|[email]\r
396 $cc${bcc}Subject: $form->{subject}\r
398 Message: $form->{message}\r| if $form->{message};
400 ($null, $form->{department_id}) = split(/--/, $form->{department});
401 $form->{department_id} *= 1;
404 $query = qq|UPDATE oe set
405 ordnumber = '$form->{ordnumber}',
406 quonumber = '$form->{quonumber}',
407 cusordnumber = '$form->{cusordnumber}',
408 transdate = '$form->{transdate}',
409 vendor_id = $form->{vendor_id},
410 customer_id = $form->{customer_id},
412 netamount = $netamount,
414 taxincluded = '$form->{taxincluded}',
415 shippingpoint = '$form->{shippingpoint}',
416 shipvia = '$form->{shipvia}',
417 notes = '$form->{notes}',
418 intnotes = '$form->{intnotes}',
419 curr = '$form->{currency}',
420 closed = '$form->{closed}',
421 quotation = '$quotation',
422 department_id = $form->{department_id},
423 employee_id = $form->{employee_id},
424 cp_id = $form->{contact_id}
425 WHERE id = $form->{id}|;
426 $dbh->do($query) || $form->dberror($query);
428 $form->{ordtotal} = $amount;
430 if ($form->{webdav}) {
431 &webdav_folder($myconfig, $form);
435 $form->{name} = $form->{ $form->{vc} };
436 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
437 $form->add_shipto($dbh, $form->{id});
439 # save printed, emailed, queued
440 $form->save_status($dbh);
442 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
443 if ($form->{vc} eq 'customer') {
444 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
445 $form->{exchangerate}, 0);
447 if ($form->{vc} eq 'vendor') {
448 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
449 0, $form->{exchangerate});
453 if ($form->{type} =~ /_order$/) {
456 &adj_onhand($dbh, $form, $ml * -1);
457 &adj_inventory($dbh, $myconfig, $form);
460 my $rc = $dbh->commit;
463 $main::lxdebug->leave_sub();
468 # this function closes multiple orders given in $form->{ordnumber_#}.
469 # use this for multiple orders that don't have to be saved back
470 # single orders should use OE::save instead.
472 $main::lxdebug->enter_sub();
474 my ($self, $myconfig ,$form) = @_;
476 for my $i (1 .. $form->{rowcount}) {
479 $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"})
482 $form->{"orderitems_id_$i"} = "";
485 if ($form->{"qty_$i"}) {
487 # set values to 0 if nothing entered
488 $form->{"discount_$i"} =
489 $form->parse_amount($myconfig, $form->{"discount_$i"});
491 $form->{"sellprice_$i"} =
492 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
496 map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} } (1 .. $form->{rowcount});
498 my $dbh = $form->dbconnect($myconfig);
499 $query = qq|UPDATE oe SET
501 WHERE ordnumber IN (|.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
589 map { push @ids, $form->{"trans_id_$_"} if ($form->{"id_$_"} and $form->{"trans_id_$_"}) } (1 .. $form->{"rowcount"});
591 # if called in multi id mode, and still only got one id, switch back to single id
592 if ($form->{"rowcount"} and $#ids == 0) {
593 $form->{"id"} = $ids[0];
599 # get default accounts and last order number
600 $query = qq|SELECT (SELECT c.accno FROM chart c
601 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
602 (SELECT c.accno FROM chart c
603 WHERE d.income_accno_id = c.id) AS income_accno,
604 (SELECT c.accno FROM chart c
605 WHERE d.expense_accno_id = c.id) AS expense_accno,
606 (SELECT c.accno FROM chart c
607 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
608 (SELECT c.accno FROM chart c
609 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
613 $query = qq|SELECT (SELECT c.accno FROM chart c
614 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
615 (SELECT c.accno FROM chart c
616 WHERE d.income_accno_id = c.id) AS income_accno,
617 (SELECT c.accno FROM chart c
618 WHERE d.expense_accno_id = c.id) AS expense_accno,
619 (SELECT c.accno FROM chart c
620 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
621 (SELECT c.accno FROM chart c
622 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
623 d.curr AS currencies,
624 current_date AS transdate, current_date AS reqdate
627 my $sth = $dbh->prepare($query);
628 $sth->execute || $form->dberror($query);
630 my $ref = $sth->fetchrow_hashref(NAME_lc);
631 map { $form->{$_} = $ref->{$_} } keys %$ref;
634 ($form->{currency}) = split /:/, $form->{currencies};
636 if ($form->{id} or @ids) {
638 # retrieve order for single id
639 # NOTE: this query is intended to fetch all information only ONCE.
640 # so if any of these infos is important (or even different) for any item,
641 # it will be killed out and then has to be fetched from the item scope query further down
642 $query = qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate,
643 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
644 o.curr AS currency, e.name AS employee, o.employee_id,
645 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
646 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
647 d.description AS department
649 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
650 LEFT JOIN employee e ON (o.employee_id = e.id)
651 LEFT JOIN department d ON (o.department_id = d.id)
653 ? qq|WHERE o.id = $form->{id}|
654 : qq|WHERE o.id IN (|.join(', ', @ids).qq|)|
657 #$main::lxdebug->message(0, $query);
659 $sth = $dbh->prepare($query);
660 $sth->execute || $form->dberror($query);
662 $ref = $sth->fetchrow_hashref(NAME_lc);
663 map { $form->{$_} = $ref->{$_} } keys %$ref;
665 # set all entries for multiple ids blank that yield different information
666 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
667 map { $form->{$_} = '' if ($ref->{$_} ne $form->{$_}) } keys %$ref;
670 # if not given, fill transdate with current_date
671 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
675 # shipto and pinted/mailed/queued status makes only sense for single id retrieve
677 $query = qq|SELECT s.* FROM shipto s
678 WHERE s.trans_id = $form->{id}|;
679 $sth = $dbh->prepare($query);
680 $sth->execute || $form->dberror($query);
682 $ref = $sth->fetchrow_hashref(NAME_lc);
683 map { $form->{$_} = $ref->{$_} } keys %$ref;
686 # get printed, emailed and queued
687 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
689 WHERE s.trans_id = $form->{id}|;
690 $sth = $dbh->prepare($query);
691 $sth->execute || $form->dberror($query);
693 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
694 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
695 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
696 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
699 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
702 my %oid = ('Pg' => 'oid',
703 'Oracle' => 'rowid');
705 # retrieve individual items
706 # this query looks up all information about the items
707 # stuff different from the whole will not be overwritten, but saved with a suffix.
708 $query = qq|SELECT o.id AS orderitems_id,
709 c1.accno AS inventory_accno,
710 c2.accno AS income_accno,
711 c3.accno AS expense_accno,
712 oe.ordnumber AS ordnumber_oe, oe.transdate AS transdate_oe, oe.cusordnumber AS cusordnumber_oe,
713 p.partnumber, p.assembly, o.description, o.qty,
714 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
715 o.reqdate, o.project_id, o.serialnumber, o.ship,
716 o.ordnumber, o.transdate, o.cusordnumber,
718 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
720 JOIN parts p ON (o.parts_id = p.id)
721 JOIN oe ON (o.trans_id = oe.id)
722 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
723 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
724 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
725 LEFT JOIN project pr ON (o.project_id = pr.id)
726 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
728 ? qq|WHERE o.trans_id = $form->{id}|
729 : qq|WHERE o.trans_id IN (|.join(", ", @ids).qq|)|
731 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
733 $sth = $dbh->prepare($query);
734 $sth->execute || $form->dberror($query);
736 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
738 # in collective order, copy global ordnumber, transdate, cusordnumber into item scope
739 # unless already present there
740 # remove _oe entries afterwards
741 map { $ref->{$_} = $ref->{"${_}_oe"} if ($ref->{$_} eq '') }
742 qw|ordnumber transdate cusordnumber| if (@ids);
743 map{ delete $ref->{$_} }
744 qw|ordnumber_oe transdate_oe cusordnumber_oe|;
746 #set expense_accno=inventory_accno if they are different => bilanz
748 ($ref->{expense_accno} != $ref->{inventory_accno})
749 ? $ref->{inventory_accno}
750 : $ref->{expense_accno};
752 # get tax rates and description
754 ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
755 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
757 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
759 $stw = $dbh->prepare($query);
760 $stw->execute || $form->dberror($query);
761 $ref->{taxaccounts} = "";
762 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
764 # if ($customertax{$ref->{accno}}) {
765 $ref->{taxaccounts} .= "$ptr->{accno} ";
766 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
767 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
768 $form->{"$ptr->{accno}_description"} = $ptr->{description};
769 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
770 $form->{taxaccounts} .= "$ptr->{accno} ";
775 chop $ref->{taxaccounts};
776 push @{ $form->{form_details} }, $ref;
784 $form->lastname_used($dbh, $myconfig, $form->{vc})
785 unless $form->{"$form->{vc}_id"};
789 $form->{exchangerate} =
790 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
791 ($form->{vc} eq 'customer') ? "buy" : "sell");
793 if ($form->{webdav}) {
794 &webdav_folder($myconfig, $form);
797 my $rc = $dbh->commit;
800 $main::lxdebug->leave_sub();
806 $main::lxdebug->enter_sub();
808 my ($self, $myconfig, $form) = @_;
810 # connect to database
811 my $dbh = $form->dbconnect($myconfig);
819 my %oid = ('Pg' => 'oid',
820 'Oracle' => 'rowid');
822 # sort items by partsgroup
823 for $i (1 .. $form->{rowcount}) {
825 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
826 $form->format_string("partsgroup_$i");
827 $partsgroup = $form->{"partsgroup_$i"};
829 push @partsgroup, [$i, $partsgroup];
832 # if there is a warehouse limit picking
833 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
835 # run query to check for inventory
836 $query = qq|SELECT sum(i.qty) AS qty
839 AND i.warehouse_id = ?|;
840 $sth = $dbh->prepare($query) || $form->dberror($query);
842 for $i (1 .. $form->{rowcount}) {
843 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
845 ($qty) = $sth->fetchrow_array;
848 $form->{"qty_$i"} = 0 if $qty == 0;
850 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
851 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
857 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
860 if ($item->[1] ne $sameitem) {
861 push(@{ $form->{description} }, qq|$item->[1]|);
862 $sameitem = $item->[1];
864 map { push(@{ $form->{$_} }, "") }
865 qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
868 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
870 if ($form->{"qty_$i"} != 0) {
872 # add number, description and qty to $form->{number}, ....
873 push(@{ $form->{runningnumber} }, $i);
874 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
875 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
876 push(@{ $form->{qty} },
877 $form->format_amount($myconfig, $form->{"qty_$i"}));
878 push(@{ $form->{ship} },
879 $form->format_amount($myconfig, $form->{"ship_$i"}));
880 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
881 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
882 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
883 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
884 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
886 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
888 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
890 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
891 my ($dec) = ($sellprice =~ /\.(\d+)/);
893 my $decimalplaces = ($dec > 2) ? $dec : 2;
897 $sellprice * $form->parse_amount($myconfig,
898 $form->{"discount_$i"}) / 100,
901 # keep a netprice as well, (sellprice - discount)
902 $form->{"netprice_$i"} = $sellprice - $discount;
905 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
907 push(@{ $form->{netprice} },
908 ($form->{"netprice_$i"} != 0)
909 ? $form->format_amount(
910 $myconfig, $form->{"netprice_$i"},
917 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
919 $linetotal = ($linetotal != 0) ? $linetotal : " ";
921 push(@{ $form->{discount} }, $discount);
923 $form->{ordtotal} += $linetotal;
925 push(@{ $form->{linetotal} },
926 $form->format_amount($myconfig, $linetotal, 2));
928 my ($taxamount, $taxbase);
931 map { $taxrate += $form->{"${_}_rate"} } split / /,
932 $form->{"taxaccounts_$i"};
934 if ($form->{taxincluded}) {
937 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
938 $taxbase = $linetotal / (1 + $taxrate);
940 $taxamount = $linetotal * $taxrate;
941 $taxbase = $linetotal;
944 if ($taxamount != 0) {
945 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
946 $taxaccounts{$item} +=
947 $taxamount * $form->{"${item}_rate"} / $taxrate;
948 $taxbase{$item} += $taxbase;
952 $tax_rate = $taxrate*100;
953 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
955 if ($form->{"assembly_$i"}) {
958 # get parts and push them onto the stack
960 if ($form->{groupitems}) {
962 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
964 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
967 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
970 JOIN parts p ON (a.parts_id = p.id)
971 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
973 AND a.id = '$form->{"id_$i"}'
975 $sth = $dbh->prepare($query);
976 $sth->execute || $form->dberror($query);
978 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
979 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
980 map { push(@{ $form->{$_} }, "") }
981 qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
982 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
983 push(@{ $form->{description} }, $sameitem);
986 push(@{ $form->{description} },
987 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
989 . qq|, $ref->{partnumber}, $ref->{description}|);
991 map { push(@{ $form->{$_} }, "") }
992 qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
1002 foreach $item (sort keys %taxaccounts) {
1003 if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
1004 push(@{ $form->{taxbase} },
1005 $form->format_amount($myconfig, $taxbase{$item}, 2));
1007 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
1009 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
1010 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
1011 push(@{ $form->{taxrate} },
1012 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
1013 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
1017 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1019 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
1022 $form->{quototal} = $form->{ordtotal} =
1023 $form->format_amount($myconfig, $form->{ordtotal}, 2);
1025 # myconfig variables
1026 map { $form->{$_} = $myconfig->{$_} }
1027 (qw(company address tel fax signature businessnumber));
1028 $form->{username} = $myconfig->{name};
1032 $main::lxdebug->leave_sub();
1035 sub project_description {
1036 $main::lxdebug->enter_sub();
1038 my ($self, $dbh, $id) = @_;
1040 my $query = qq|SELECT p.description
1043 my $sth = $dbh->prepare($query);
1044 $sth->execute || $form->dberror($query);
1046 ($_) = $sth->fetchrow_array;
1050 $main::lxdebug->leave_sub();
1055 sub get_warehouses {
1056 $main::lxdebug->enter_sub();
1058 my ($self, $myconfig, $form) = @_;
1060 my $dbh = $form->dbconnect($myconfig);
1063 my $query = qq|SELECT id, description
1066 my $sth = $dbh->prepare($query);
1067 $sth->execute || $form->dberror($query);
1069 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1070 push @{ $form->{all_warehouses} }, $ref;
1076 $main::lxdebug->leave_sub();
1079 sub save_inventory {
1080 $main::lxdebug->enter_sub();
1082 my ($self, $myconfig, $form) = @_;
1084 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1088 ($null, $employee_id) = split /--/, $form->{employee};
1090 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
1092 my $dbh = $form->dbconnect_noauto($myconfig);
1098 $query = qq|SELECT o.serialnumber, o.ship
1100 WHERE o.trans_id = ?
1103 $sth = $dbh->prepare($query) || $form->dberror($query);
1105 $query = qq|SELECT sum(i.qty)
1107 WHERE i.parts_id = ?
1108 AND i.warehouse_id = ?|;
1109 $wth = $dbh->prepare($query) || $form->dberror($query);
1111 for my $i (1 .. $form->{rowcount} - 1) {
1114 (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
1116 : $form->{"ship_$i"};
1118 if ($warehouse_id && $form->{type} eq 'ship_order') {
1120 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1122 ($qty) = $wth->fetchrow_array;
1133 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1134 qty, oe_id, orderitems_id, shippingdate, employee_id)
1135 VALUES ($form->{"id_$i"}, $warehouse_id,
1136 $ship, $form->{"id"},
1137 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1139 $dbh->do($query) || $form->dberror($query);
1141 # add serialnumber, ship to orderitems
1142 $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
1144 ($serialnumber, $ship) = $sth->fetchrow_array;
1147 $serialnumber .= " " if $serialnumber;
1148 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1149 $ship += $form->{"ship_$i"};
1151 $query = qq|UPDATE orderitems SET
1152 serialnumber = '$serialnumber',
1154 WHERE trans_id = $form->{id}
1155 AND id = $form->{"orderitems_id_$i"}|;
1156 $dbh->do($query) || $form->dberror($query);
1158 # update order with ship via
1159 $query = qq|UPDATE oe SET
1160 shippingpoint = '$form->{shippingpoint}',
1161 shipvia = '$form->{shipvia}'
1162 WHERE id = $form->{id}|;
1163 $dbh->do($query) || $form->dberror($query);
1165 # update onhand for parts
1166 $form->update_balance($dbh, "parts", "onhand",
1167 qq|id = $form->{"id_$i"}|,
1168 $form->{"ship_$i"} * $ml);
1173 my $rc = $dbh->commit;
1176 $main::lxdebug->leave_sub();
1182 $main::lxdebug->enter_sub();
1184 my ($dbh, $form, $ml) = @_;
1186 my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
1188 JOIN parts p ON (p.id = oi.parts_id)
1189 WHERE oi.trans_id = $form->{id}|;
1190 my $sth = $dbh->prepare($query);
1191 $sth->execute || $form->dberror($query);
1193 $query = qq|SELECT sum(p.inventory_accno_id)
1195 JOIN assembly a ON (a.parts_id = p.id)
1197 my $ath = $dbh->prepare($query) || $form->dberror($query);
1201 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1203 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1205 # do not update if assembly consists of all services
1206 if ($ref->{assembly}) {
1207 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1209 ($ispa) = $sth->fetchrow_array;
1216 # adjust onhand in parts table
1217 $form->update_balance($dbh, "parts", "onhand",
1218 qq|id = $ref->{parts_id}|,
1219 $ref->{ship} * $ml);
1225 $main::lxdebug->leave_sub();
1229 $main::lxdebug->enter_sub();
1231 my ($dbh, $myconfig, $form) = @_;
1233 my %oid = ('Pg' => 'oid',
1234 'Oracle' => 'rowid');
1236 # increase/reduce qty in inventory table
1237 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1239 WHERE oi.trans_id = $form->{id}|;
1240 my $sth = $dbh->prepare($query);
1241 $sth->execute || $form->dberror($query);
1243 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1244 (SELECT SUM(qty) FROM inventory
1245 WHERE oe_id = $form->{id}
1246 AND orderitems_id = ?) AS total
1248 WHERE oe_id = $form->{id}
1249 AND orderitems_id = ?|;
1250 my $ith = $dbh->prepare($query) || $form->dberror($query);
1253 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1255 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1257 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1259 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1261 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1262 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1264 $form->update_balance($dbh, "inventory", "qty",
1265 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1274 # delete inventory entries if qty = 0
1275 $query = qq|DELETE FROM inventory
1276 WHERE oe_id = $form->{id}
1278 $dbh->do($query) || $form->dberror($query);
1280 $main::lxdebug->leave_sub();
1284 $main::lxdebug->enter_sub();
1286 my ($self, $myconfig, $form) = @_;
1288 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1291 my $dbh = $form->dbconnect($myconfig);
1293 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1296 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1297 WHERE p.onhand > 0|;
1299 if ($form->{partnumber}) {
1300 $var = $form->like(lc $form->{partnumber});
1302 AND lower(p.partnumber) LIKE '$var'";
1304 if ($form->{description}) {
1305 $var = $form->like(lc $form->{description});
1307 AND lower(p.description) LIKE '$var'";
1309 if ($form->{partsgroup}) {
1310 $var = $form->like(lc $form->{partsgroup});
1312 AND lower(pg.partsgroup) LIKE '$var'";
1315 $sth = $dbh->prepare($query);
1316 $sth->execute || $form->dberror($query);
1318 $query = qq|SELECT sum(i.qty), w.description, w.id
1320 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1321 WHERE i.parts_id = ?
1322 AND NOT i.warehouse_id = $warehouse_id
1323 GROUP BY w.description, w.id|;
1324 $wth = $dbh->prepare($query) || $form->dberror($query);
1326 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1328 $wth->execute($ref->{id}) || $form->dberror;
1330 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1331 push @{ $form->{all_inventory} },
1332 { 'id' => $ref->{id},
1333 'partnumber' => $ref->{partnumber},
1334 'description' => $ref->{description},
1335 'partsgroup' => $ref->{partsgroup},
1337 'warehouse_id' => $warehouse_id,
1338 'warehouse' => $warehouse }
1348 @{ $form->{all_inventory} } =
1349 sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
1350 @{ $form->{all_inventory} };
1352 $main::lxdebug->leave_sub();
1354 return @{ $form->{all_inventory} };
1358 $main::lxdebug->enter_sub();
1360 my ($self, $myconfig, $form) = @_;
1362 my $dbh = $form->dbconnect_noauto($myconfig);
1364 my $query = qq|INSERT INTO inventory
1365 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1366 VALUES (?, ?, ?, ?, ?)|;
1367 $sth = $dbh->prepare($query) || $form->dberror($query);
1369 $form->get_employee($dbh);
1374 $shippingdate = "$a[5]-$a[4]-$a[3]";
1376 for my $i (1 .. $form->{rowcount}) {
1377 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1379 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1384 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
1385 $shippingdate, $form->{employee_id})
1391 $sth->execute($form->{"warehouse_id_$i"},
1392 $form->{"id_$i"}, $qty * -1, $shippingdate,
1393 $form->{employee_id})
1400 my $rc = $dbh->commit;
1403 $main::lxdebug->leave_sub();
1409 $main::lxdebug->enter_sub();
1411 my ($myconfig, $form) = @_;
1414 $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
1415 if ($form->{type} eq "sales_quotation");
1416 $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
1417 if ($form->{type} eq "sales_order");
1418 $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
1419 if ($form->{type} eq "request_quotation");
1420 $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
1421 if ($form->{type} eq "purchase_order");
1425 mkdir($path, 0770) or die "can't make directory $!\n";
1429 foreach $file (@files) {
1430 $file =~ /\/([^\/]*)$/;
1432 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1434 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1435 $form->{WEBDAV}{$fname} = $link;
1440 $main::lxdebug->leave_sub();