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 #======================================================================
39 $main::lxdebug->enter_sub();
41 my ($self, $myconfig, $form) = @_;
44 my $dbh = $form->dbconnect($myconfig);
47 my $ordnumber = 'ordnumber';
49 my ($null, $department_id) = split /--/, $form->{department};
51 my $department = " AND o.department_id = $department_id" if $department_id;
53 my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
55 if ($form->{type} =~ /_quotation$/) {
57 $ordnumber = 'quonumber';
60 my $number = $form->like(lc $form->{$ordnumber});
61 my $name = $form->like(lc $form->{$form->{vc}});
63 my $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
64 o.amount, ct.name, o.netamount, o.$form->{vc}_id,
65 ex.$rate AS exchangerate,
66 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
69 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
70 LEFT JOIN employee e ON (o.employee_id = e.id)
71 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
72 AND ex.transdate = o.transdate)
73 WHERE o.quotation = '$quotation'
76 # build query if type eq (ship|receive)_order
77 if ($form->{type} =~ /(ship|receive)_order/) {
78 my ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
80 $query = qq|SELECT DISTINCT ON (o.id) o.id, o.ordnumber, o.transdate,
81 o.reqdate, o.amount, ct.name, o.netamount, o.$form->{vc}_id,
82 ex.$rate AS exchangerate,
83 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
86 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
87 JOIN orderitems oi ON (oi.trans_id = o.id)
88 JOIN parts p ON (p.id = oi.parts_id)|;
90 if ($warehouse_id && $form->{type} eq 'ship_order') {
92 JOIN inventory i ON (oi.parts_id = i.parts_id)
97 LEFT JOIN employee e ON (o.employee_id = e.id)
98 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
99 AND ex.transdate = o.transdate)
100 WHERE o.quotation = '0'
101 AND (p.inventory_accno_id > 0 OR p.assembly = '1')
102 AND oi.qty <> oi.ship
105 if ($warehouse_id && $form->{type} eq 'ship_order') {
107 AND i.warehouse_id = $warehouse_id
108 AND i.qty >= (oi.qty - oi.ship)
114 if ($form->{"$form->{vc}_id"}) {
115 $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
117 if ($form->{$form->{vc}}) {
118 $query .= " AND lower(ct.name) LIKE '$name'";
121 if (!$form->{open} && !$form->{closed}) {
122 $query .= " AND o.id = 0";
123 } elsif (!($form->{open} && $form->{closed})) {
124 $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
128 my $sortorder = join ', ', ("o.id", $form->sort_columns(transdate, $ordnumber, name));
129 $sortorder = $form->{sort} unless $sortorder;
131 $query .= " AND lower($ordnumber) LIKE '$number'" if $form->{$ordnumber};
132 $query .= " AND o.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
133 $query .= " AND o.transdate <= '$form->{transdateto}'" if $form->{transdateto};
134 $query .= " ORDER by $sortorder";
136 my $sth = $dbh->prepare($query);
137 $sth->execute || $form->dberror($query);
140 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
141 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
142 push @{ $form->{OE} }, $ref if $ref->{id} != $id{$ref->{id}};
143 $id{$ref->{id}} = $ref->{id};
149 $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 } qw(ordnumber quonumber shippingpoint shipvia notes intnotes message);
222 for my $i (1 .. $form->{rowcount}) {
224 map { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(qty ship);
226 if ($form->{"qty_$i"}) {
228 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g } qw(partnumber description unit);
230 # set values to 0 if nothing entered
231 $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
233 $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
234 $fxsellprice = $form->{"sellprice_$i"};
236 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
238 my $decimalplaces = ($dec > 2) ? $dec : 2;
240 $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"}, $decimalplaces);
241 $form->{"sellprice_$i"} = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
243 $form->{"inventory_accno_$i"} *= 1;
244 $form->{"expense_accno_$i"} *= 1;
246 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
248 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
252 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
254 if ($form->{taxincluded}) {
255 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
256 $taxbase = $linetotal - $taxamount;
257 # we are not keeping a natural price, do not round
258 $form->{"sellprice_$i"} = $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
260 $taxamount = $linetotal * $taxrate;
261 $taxbase = $linetotal;
264 if ($form->round_amount($taxrate,7) == 0) {
265 if ($form->{taxincluded}) {
266 foreach $item (@taxaccounts) {
267 $taxamount = $form->round_amount($linetotal * $form->{"${item}_rate"} / (1 + abs($form->{"${item}_rate"})), 2);
269 $taxaccounts{$item} += $taxamount;
270 $taxdiff += $taxamount;
272 $taxbase{$item} += $taxbase;
274 $taxaccounts{$taxaccounts[0]} += $taxdiff;
276 foreach $item (@taxaccounts) {
277 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
278 $taxbase{$item} += $taxbase;
282 foreach $item (@taxaccounts) {
283 $taxaccounts{$item} += $taxamount * $form->{"${item}_rate"} / $taxrate;
284 $taxbase{$item} += $taxbase;
289 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
291 $project_id = 'NULL';
292 if ($form->{"projectnumber_$i"}) {
293 $project_id = $form->{"projectnumber_$i"};
295 $reqdate = ($form->{"reqdate_$i"}) ? qq|'$form->{"reqdate_$i"}'| : "NULL";
297 # save detail record in orderitems table
298 $query = qq|INSERT INTO orderitems (|;
299 $query .= "id, " if $form->{"orderitems_id_$i"};
300 $query .= qq|trans_id, parts_id, description, qty, sellprice, discount,
301 unit, reqdate, project_id, serialnumber, ship)
303 $query .= qq|$form->{"orderitems_id_$i"},| if $form->{"orderitems_id_$i"};
304 $query .= qq|$form->{id}, $form->{"id_$i"},
305 '$form->{"description_$i"}', $form->{"qty_$i"},
306 $fxsellprice, $form->{"discount_$i"},
307 '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
308 '$form->{"serialnumber_$i"}', $form->{"ship_$i"})|;
309 $dbh->do($query) || $form->dberror($query);
311 $form->{"sellprice_$i"} = $fxsellprice;
312 $form->{"discount_$i"} *= 100;
317 # set values which could be empty
318 map { $form->{$_} *= 1 } qw(vendor_id customer_id taxincluded closed quotation);
320 $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
324 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
326 $amount = $form->round_amount($netamount + $tax, 2);
327 $netamount = $form->round_amount($netamount, 2);
329 if ($form->{currency} eq $form->{defaultcurrency}) {
330 $form->{exchangerate} = 1;
332 $exchangerate = $form->check_exchangerate($myconfig, $form->{currency}, $form->{transdate}, ($form->{vc} eq 'customer') ? 'buy' : 'sell');
335 $form->{exchangerate} = ($exchangerate) ? $exchangerate : $form->parse_amount($myconfig, $form->{exchangerate});
338 # fill in subject if there is none
339 if ($form->{type} =~ /_order$/) {
341 $form->{subject} = qq|$form->{label} $form->{ordnumber}| unless $form->{subject};
344 $form->{subject} = qq|$form->{label} $form->{quonumber}| unless $form->{subject};
347 # if there is a message stuff it into the intnotes
348 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
349 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
350 my $now = scalar localtime;
351 $form->{intnotes} .= qq|\r
352 \r| if $form->{intnotes};
354 $form->{intnotes} .= qq|[email]\r
357 $cc${bcc}Subject: $form->{subject}\r
359 Message: $form->{message}\r| if $form->{message};
361 ($null, $form->{department_id}) = split(/--/, $form->{department});
362 $form->{department_id} *= 1;
365 $query = qq|UPDATE oe set
366 ordnumber = '$form->{ordnumber}',
367 quonumber = '$form->{quonumber}',
368 cusordnumber = '$form->{cusordnumber}',
369 transdate = '$form->{transdate}',
370 vendor_id = $form->{vendor_id},
371 customer_id = $form->{customer_id},
373 netamount = $netamount,
375 taxincluded = '$form->{taxincluded}',
376 shippingpoint = '$form->{shippingpoint}',
377 shipvia = '$form->{shipvia}',
378 notes = '$form->{notes}',
379 intnotes = '$form->{intnotes}',
380 curr = '$form->{currency}',
381 closed = '$form->{closed}',
382 quotation = '$quotation',
383 department_id = $form->{department_id},
384 employee_id = $form->{employee_id},
385 cp_id = $form->{contact_id}
386 WHERE id = $form->{id}|;
387 $dbh->do($query) || $form->dberror($query);
389 $form->{ordtotal} = $amount;
391 if ($form->{webdav}) {
392 &webdav_folder($myconfig, $form);
396 $form->{name} = $form->{$form->{vc}};
397 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
398 $form->add_shipto($dbh, $form->{id});
400 # save printed, emailed, queued
401 $form->save_status($dbh);
403 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
404 if ($form->{vc} eq 'customer') {
405 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, $form->{exchangerate}, 0);
407 if ($form->{vc} eq 'vendor') {
408 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate}, 0, $form->{exchangerate});
413 if ($form->{type} =~ /_order$/) {
415 &adj_onhand($dbh, $form, $ml * -1);
416 &adj_inventory($dbh, $myconfig, $form);
419 my $rc = $dbh->commit;
422 $main::lxdebug->leave_sub();
430 $main::lxdebug->enter_sub();
432 my ($self, $myconfig, $form, $spool) = @_;
434 # connect to database
435 my $dbh = $form->dbconnect_noauto($myconfig);
438 my $query = qq|SELECT s.spoolfile FROM status s
439 WHERE s.trans_id = $form->{id}|;
440 $sth = $dbh->prepare($query);
441 $sth->execute || $self->dberror($query);
446 while (($spoolfile) = $sth->fetchrow_array) {
447 push @spoolfiles, $spoolfile;
452 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
453 WHERE o.trans_id = $form->{id}|;
454 $sth = $dbh->prepare($query);
455 $sth->execute || $self->dberror($query);
457 while (my ($id, $ship) = $sth->fetchrow_array) {
458 $form->update_balance($dbh,
467 $query = qq|DELETE FROM inventory
468 WHERE oe_id = $form->{id}|;
469 $dbh->do($query) || $form->dberror($query);
471 # delete status entries
472 $query = qq|DELETE FROM status
473 WHERE trans_id = $form->{id}|;
474 $dbh->do($query) || $form->dberror($query);
477 $query = qq|DELETE FROM oe
478 WHERE id = $form->{id}|;
479 $dbh->do($query) || $form->dberror($query);
481 # delete individual entries
482 $query = qq|DELETE FROM orderitems
483 WHERE trans_id = $form->{id}|;
484 $dbh->do($query) || $form->dberror($query);
486 $query = qq|DELETE FROM shipto
487 WHERE trans_id = $form->{id}|;
488 $dbh->do($query) || $form->dberror($query);
490 my $rc = $dbh->commit;
494 foreach $spoolfile (@spoolfiles) {
495 unlink "$spool/$spoolfile" if $spoolfile;
499 $main::lxdebug->leave_sub();
507 $main::lxdebug->enter_sub();
509 my ($self, $myconfig, $form) = @_;
511 # connect to database
512 my $dbh = $form->dbconnect_noauto($myconfig);
517 # get default accounts and last order number
518 $query = qq|SELECT (SELECT c.accno FROM chart c
519 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
520 (SELECT c.accno FROM chart c
521 WHERE d.income_accno_id = c.id) AS income_accno,
522 (SELECT c.accno FROM chart c
523 WHERE d.expense_accno_id = c.id) AS expense_accno,
524 (SELECT c.accno FROM chart c
525 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
526 (SELECT c.accno FROM chart c
527 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
531 $query = qq|SELECT (SELECT c.accno FROM chart c
532 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
533 (SELECT c.accno FROM chart c
534 WHERE d.income_accno_id = c.id) AS income_accno,
535 (SELECT c.accno FROM chart c
536 WHERE d.expense_accno_id = c.id) AS expense_accno,
537 (SELECT c.accno FROM chart c
538 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
539 (SELECT c.accno FROM chart c
540 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
541 d.curr AS currencies,
542 current_date AS transdate, current_date AS reqdate
545 my $sth = $dbh->prepare($query);
546 $sth->execute || $form->dberror($query);
548 my $ref = $sth->fetchrow_hashref(NAME_lc);
549 map { $form->{$_} = $ref->{$_} } keys %$ref;
552 ($form->{currency}) = split /:/, $form->{currencies};
557 $query = qq|SELECT o.cp_id,o.ordnumber, o.transdate, o.reqdate,
558 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
559 o.curr AS currency, e.name AS employee, o.employee_id,
560 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
561 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
562 d.description AS department
564 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
565 LEFT JOIN employee e ON (o.employee_id = e.id)
566 LEFT JOIN department d ON (o.department_id = d.id)
567 WHERE o.id = $form->{id}|;
568 $sth = $dbh->prepare($query);
569 $sth->execute || $form->dberror($query);
571 $ref = $sth->fetchrow_hashref(NAME_lc);
572 map { $form->{$_} = $ref->{$_} } keys %$ref;
576 $query = qq|SELECT s.* FROM shipto s
577 WHERE s.trans_id = $form->{id}|;
578 $sth = $dbh->prepare($query);
579 $sth->execute || $form->dberror($query);
581 $ref = $sth->fetchrow_hashref(NAME_lc);
582 map { $form->{$_} = $ref->{$_} } keys %$ref;
585 # get printed, emailed and queued
586 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
588 WHERE s.trans_id = $form->{id}|;
589 $sth = $dbh->prepare($query);
590 $sth->execute || $form->dberror($query);
592 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
593 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
594 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
595 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
598 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
601 my %oid = ( 'Pg' => 'oid',
605 # retrieve individual items
606 $query = qq|SELECT o.id AS orderitems_id,
607 c1.accno AS inventory_accno,
608 c2.accno AS income_accno,
609 c3.accno AS expense_accno,
610 p.partnumber, p.assembly, o.description, o.qty,
611 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes,
612 o.reqdate, o.project_id, o.serialnumber, o.ship,
616 JOIN parts p ON (o.parts_id = p.id)
617 LEFT JOIN chart c1 ON (p.inventory_accno_id = c1.id)
618 LEFT JOIN chart c2 ON (p.income_accno_id = c2.id)
619 LEFT JOIN chart c3 ON (p.expense_accno_id = c3.id)
620 LEFT JOIN project pr ON (o.project_id = pr.id)
621 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
622 WHERE o.trans_id = $form->{id}
623 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
624 $sth = $dbh->prepare($query);
625 $sth->execute || $form->dberror($query);
627 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
630 #set expense_accno=inventory_accno if they are different => bilanz
631 $vendor_accno = ($ref->{expense_accno}!=$ref->{inventory_accno}) ? $ref->{inventory_accno} :$ref->{expense_accno};
634 # get tax rates and description
635 $accno_id = ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
636 $query = qq|SELECT c.accno, c.description, t.rate, t.taxnumber
638 WHERE c.id=t.chart_id AND t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
640 $stw = $dbh->prepare($query);
641 $stw->execute || $form->dberror($query);
642 $ref->{taxaccounts} = "";
643 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
644 # if ($customertax{$ref->{accno}}) {
645 $ref->{taxaccounts} .= "$ptr->{accno} ";
646 if (!($form->{taxaccounts}=~/$ptr->{accno}/)) {
647 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
648 $form->{"$ptr->{accno}_description"} = $ptr->{description};
649 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
650 $form->{taxaccounts} .= "$ptr->{accno} ";
655 chop $ref->{taxaccounts};
656 push @{ $form->{form_details} }, $ref;
664 $form->lastname_used($dbh, $myconfig, $form->{vc}) unless $form->{"$form->{vc}_id"};
668 $form->{exchangerate} = $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate}, ($form->{vc} eq 'customer') ? "buy" : "sell");
670 if ($form->{webdav}) {
671 &webdav_folder($myconfig, $form);
674 my $rc = $dbh->commit;
677 $main::lxdebug->leave_sub();
685 $main::lxdebug->enter_sub();
687 my ($self, $myconfig, $form) = @_;
689 # connect to database
690 my $dbh = $form->dbconnect($myconfig);
698 my %oid = ( 'Pg' => 'oid',
699 'Oracle' => 'rowid' );
701 # sort items by partsgroup
702 for $i (1 .. $form->{rowcount}) {
704 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
705 $form->format_string("partsgroup_$i");
706 $partsgroup = $form->{"partsgroup_$i"};
708 push @partsgroup, [ $i, $partsgroup ];
711 # if there is a warehouse limit picking
712 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
713 # run query to check for inventory
714 $query = qq|SELECT sum(i.qty) AS qty
717 AND i.warehouse_id = ?|;
718 $sth = $dbh->prepare($query) || $form->dberror($query);
720 for $i (1 .. $form->{rowcount}) {
721 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
723 ($qty) = $sth->fetchrow_array;
726 $form->{"qty_$i"} = 0 if $qty == 0;
728 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
729 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
736 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
739 if ($item->[1] ne $sameitem) {
740 push(@{ $form->{description} }, qq|$item->[1]|);
741 $sameitem = $item->[1];
743 map { push(@{ $form->{$_} }, "") } qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
746 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
748 if ($form->{"qty_$i"} != 0) {
750 # add number, description and qty to $form->{number}, ....
751 push(@{ $form->{runningnumber} }, $i);
752 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
753 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
754 push(@{ $form->{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"}));
755 push(@{ $form->{ship} }, $form->format_amount($myconfig, $form->{"ship_$i"}));
756 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
757 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
758 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
759 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
760 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
762 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
764 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
766 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
767 my ($dec) = ($sellprice =~ /\.(\d+)/);
769 my $decimalplaces = ($dec > 2) ? $dec : 2;
772 my $discount = $form->round_amount($sellprice * $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100, $decimalplaces);
774 # keep a netprice as well, (sellprice - discount)
775 $form->{"netprice_$i"} = $sellprice - $discount;
777 my $linetotal = $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
779 push(@{ $form->{netprice} }, ($form->{"netprice_$i"} != 0) ? $form->format_amount($myconfig, $form->{"netprice_$i"}, $decimalplaces) : " ");
781 $discount = ($discount != 0) ? $form->format_amount($myconfig, $discount * -1, $decimalplaces) : " ";
782 $linetotal = ($linetotal != 0) ? $linetotal : " ";
784 push(@{ $form->{discount} }, $discount);
786 $form->{ordtotal} += $linetotal;
788 push(@{ $form->{linetotal} }, $form->format_amount($myconfig, $linetotal, 2));
790 my ($taxamount, $taxbase);
793 map { $taxrate += $form->{"${_}_rate"} } split / /, $form->{"taxaccounts_$i"};
795 if ($form->{taxincluded}) {
797 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
798 $taxbase = $linetotal / (1 + $taxrate);
800 $taxamount = $linetotal * $taxrate;
801 $taxbase = $linetotal;
805 if ($taxamount != 0) {
806 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
807 $taxaccounts{$item} += $taxamount * $form->{"${item}_rate"} / $taxrate;
808 $taxbase{$item} += $taxbase;
812 if ($form->{"assembly_$i"}) {
815 # get parts and push them onto the stack
817 if ($form->{groupitems}) {
818 $sortorder = qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
820 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
823 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
826 JOIN parts p ON (a.parts_id = p.id)
827 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
829 AND a.id = '$form->{"id_$i"}'
831 $sth = $dbh->prepare($query);
832 $sth->execute || $form->dberror($query);
834 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
835 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
836 map { push(@{ $form->{$_} }, "") } qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal);
837 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
838 push(@{ $form->{description} }, $sameitem);
841 push(@{ $form->{description} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq|, $ref->{partnumber}, $ref->{description}|);
843 map { push(@{ $form->{$_} }, "") } qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal);
854 foreach $item (sort keys %taxaccounts) {
855 if ($form->round_amount($taxaccounts{$item}, 2) != 0) {
856 push(@{ $form->{taxbase} }, $form->format_amount($myconfig, $taxbase{$item}, 2));
858 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
860 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
861 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
862 push(@{ $form->{taxrate} }, $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
863 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
868 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
869 $form->{ordtotal} = ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
872 $form->{quototal} = $form->{ordtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
875 map { $form->{$_} = $myconfig->{$_} } (qw(company address tel fax signature businessnumber));
876 $form->{username} = $myconfig->{name};
880 $main::lxdebug->leave_sub();
884 sub project_description {
885 $main::lxdebug->enter_sub();
887 my ($self, $dbh, $id) = @_;
889 my $query = qq|SELECT p.description
892 my $sth = $dbh->prepare($query);
893 $sth->execute || $form->dberror($query);
895 ($_) = $sth->fetchrow_array;
899 $main::lxdebug->leave_sub();
906 $main::lxdebug->enter_sub();
908 my ($self, $myconfig, $form) = @_;
910 my $dbh = $form->dbconnect($myconfig);
912 my $query = qq|SELECT id, description
915 my $sth = $dbh->prepare($query);
916 $sth->execute || $form->dberror($query);
918 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
919 push @{ $form->{all_warehouses} }, $ref;
925 $main::lxdebug->leave_sub();
930 $main::lxdebug->enter_sub();
932 my ($self, $myconfig, $form) = @_;
934 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
938 ($null, $employee_id) = split /--/, $form->{employee};
940 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
942 my $dbh = $form->dbconnect_noauto($myconfig);
948 $query = qq|SELECT o.serialnumber, o.ship
953 $sth = $dbh->prepare($query) || $form->dberror($query);
955 $query = qq|SELECT sum(i.qty)
958 AND i.warehouse_id = ?|;
959 $wth = $dbh->prepare($query) || $form->dberror($query);
962 for my $i (1 .. $form->{rowcount} - 1) {
964 $ship = (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"})) ? $form->{"qty_$i"} : $form->{"ship_$i"};
966 if ($warehouse_id && $form->{type} eq 'ship_order') {
968 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
970 ($qty) = $wth->fetchrow_array;
982 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
983 qty, oe_id, orderitems_id, shippingdate, employee_id)
984 VALUES ($form->{"id_$i"}, $warehouse_id,
985 $ship, $form->{"id"},
986 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
988 $dbh->do($query) || $form->dberror($query);
990 # add serialnumber, ship to orderitems
991 $sth->execute($form->{id}, $form->{"orderitems_id_$i"}) || $form->dberror;
992 ($serialnumber, $ship) = $sth->fetchrow_array;
995 $serialnumber .= " " if $serialnumber;
996 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
997 $ship += $form->{"ship_$i"};
999 $query = qq|UPDATE orderitems SET
1000 serialnumber = '$serialnumber',
1002 WHERE trans_id = $form->{id}
1003 AND id = $form->{"orderitems_id_$i"}|;
1004 $dbh->do($query) || $form->dberror($query);
1007 # update order with ship via
1008 $query = qq|UPDATE oe SET
1009 shippingpoint = '$form->{shippingpoint}',
1010 shipvia = '$form->{shipvia}'
1011 WHERE id = $form->{id}|;
1012 $dbh->do($query) || $form->dberror($query);
1015 # update onhand for parts
1016 $form->update_balance($dbh,
1019 qq|id = $form->{"id_$i"}|,
1020 $form->{"ship_$i"} * $ml);
1025 my $rc = $dbh->commit;
1028 $main::lxdebug->leave_sub();
1035 $main::lxdebug->enter_sub();
1037 my ($dbh, $form, $ml) = @_;
1039 my $query = qq|SELECT oi.parts_id, oi.ship, p.inventory_accno_id, p.assembly
1041 JOIN parts p ON (p.id = oi.parts_id)
1042 WHERE oi.trans_id = $form->{id}|;
1043 my $sth = $dbh->prepare($query);
1044 $sth->execute || $form->dberror($query);
1046 $query = qq|SELECT sum(p.inventory_accno_id)
1048 JOIN assembly a ON (a.parts_id = p.id)
1050 my $ath = $dbh->prepare($query) || $form->dberror($query);
1054 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1056 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1058 # do not update if assembly consists of all services
1059 if ($ref->{assembly}) {
1060 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1062 ($ispa) = $sth->fetchrow_array;
1069 # adjust onhand in parts table
1070 $form->update_balance($dbh,
1073 qq|id = $ref->{parts_id}|,
1074 $ref->{ship} * $ml);
1080 $main::lxdebug->leave_sub();
1085 $main::lxdebug->enter_sub();
1087 my ($dbh, $myconfig, $form) = @_;
1089 my %oid = ('Pg' => 'oid',
1090 'Oracle' => 'rowid');
1092 # increase/reduce qty in inventory table
1093 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1095 WHERE oi.trans_id = $form->{id}|;
1096 my $sth = $dbh->prepare($query);
1097 $sth->execute || $form->dberror($query);
1099 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1100 (SELECT SUM(qty) FROM inventory
1101 WHERE oe_id = $form->{id}
1102 AND orderitems_id = ?) AS total
1104 WHERE oe_id = $form->{id}
1105 AND orderitems_id = ?|;
1106 my $ith = $dbh->prepare($query) || $form->dberror($query);
1109 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1111 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1113 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1115 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1117 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1118 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1120 $form->update_balance($dbh,
1123 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1132 # delete inventory entries if qty = 0
1133 $query = qq|DELETE FROM inventory
1134 WHERE oe_id = $form->{id}
1136 $dbh->do($query) || $form->dberror($query);
1138 $main::lxdebug->leave_sub();
1143 $main::lxdebug->enter_sub();
1145 my ($self, $myconfig, $form) = @_;
1147 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1150 my $dbh = $form->dbconnect($myconfig);
1152 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1155 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1156 WHERE p.onhand > 0|;
1158 if ($form->{partnumber}) {
1159 $var = $form->like(lc $form->{partnumber});
1161 AND lower(p.partnumber) LIKE '$var'";
1163 if ($form->{description}) {
1164 $var = $form->like(lc $form->{description});
1166 AND lower(p.description) LIKE '$var'";
1168 if ($form->{partsgroup}) {
1169 $var = $form->like(lc $form->{partsgroup});
1171 AND lower(pg.partsgroup) LIKE '$var'";
1174 $sth = $dbh->prepare($query);
1175 $sth->execute || $form->dberror($query);
1178 $query = qq|SELECT sum(i.qty), w.description, w.id
1180 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1181 WHERE i.parts_id = ?
1182 AND NOT i.warehouse_id = $warehouse_id
1183 GROUP BY w.description, w.id|;
1184 $wth = $dbh->prepare($query) || $form->dberror($query);
1186 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1188 $wth->execute($ref->{id}) || $form->dberror;
1190 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1191 push @{ $form->{all_inventory} }, {'id' => $ref->{id},
1192 'partnumber' => $ref->{partnumber},
1193 'description' => $ref->{description},
1194 'partsgroup' => $ref->{partsgroup},
1196 'warehouse_id' => $warehouse_id,
1197 'warehouse' => $warehouse} if $qty > 0;
1206 @{ $form->{all_inventory} } = sort { $a->{$form->{sort}} cmp $b->{$form->{sort}} } @{ $form->{all_inventory} };
1208 $main::lxdebug->leave_sub();
1210 return @{ $form->{all_inventory} };
1215 $main::lxdebug->enter_sub();
1217 my ($self, $myconfig, $form) = @_;
1219 my $dbh = $form->dbconnect_noauto($myconfig);
1221 my $query = qq|INSERT INTO inventory
1222 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1223 VALUES (?, ?, ?, ?, ?)|;
1224 $sth = $dbh->prepare($query) || $form->dberror($query);
1226 $form->get_employee($dbh);
1228 my @a = localtime; $a[5] += 1900; $a[4]++;
1229 $shippingdate = "$a[5]-$a[4]-$a[3]";
1231 for my $i (1 .. $form->{rowcount}) {
1232 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1234 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1238 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty, $shippingdate, $form->{employee_id}) || $form->dberror;
1243 $sth->execute($form->{"warehouse_id_$i"}, $form->{"id_$i"}, $qty * -1, $shippingdate, $form->{employee_id}) || $form->dberror;
1249 my $rc = $dbh->commit;
1252 $main::lxdebug->leave_sub();
1258 $main::lxdebug->enter_sub();
1260 my ($myconfig, $form) = @_;
1264 $path = "webdav/angebote/".$form->{quonumber}, last SWITCH if ($form->{type} eq "sales_quotation");
1265 $path = "webdav/bestellungen/".$form->{ordnumber}, last SWITCH if ($form->{type} eq "sales_order");
1266 $path = "webdav/anfragen/".$form->{quonumber}, last SWITCH if ($form->{type} eq "request_quotation");
1267 $path = "webdav/lieferantenbestellungen/".$form->{ordnumber}, last SWITCH if ($form->{type} eq "purchase_order");
1272 mkdir ($path, 0770) or die "can't make directory $!\n";
1276 foreach $file (@files) {
1277 $file =~ /\/([^\/]*)$/;
1279 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1281 $link = "http://".$ENV{'SERVER_NAME'}."/".$lxerp."/".$file;
1282 $form->{WEBDAV}{$fname} = $link;
1288 $main::lxdebug->leave_sub();