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 #======================================================================
41 $main::lxdebug->enter_sub();
43 my ($self, $myconfig, $form) = @_;
46 my $dbh = $form->dbconnect($myconfig);
49 my $ordnumber = 'ordnumber';
51 my ($null, $department_id) = split /--/, $form->{department};
53 my $department = " AND o.department_id = $department_id" if $department_id;
55 my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
57 if ($form->{type} =~ /_quotation$/) {
59 $ordnumber = 'quonumber';
62 my $number = $form->like(lc $form->{$ordnumber});
63 my $name = $form->like(lc $form->{ $form->{vc} });
65 my $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
66 o.amount, ct.name, o.netamount, o.$form->{vc}_id,
67 ex.$rate AS exchangerate,
68 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
71 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
72 LEFT JOIN employee e ON (o.employee_id = e.id)
73 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
74 AND ex.transdate = o.transdate)
75 WHERE o.quotation = '$quotation'
78 # build query if type eq (ship|receive)_order
79 if ($form->{type} =~ /(ship|receive)_order/) {
80 my ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
82 $query = qq|SELECT DISTINCT ON (o.id) o.id, o.ordnumber, o.transdate,
83 o.reqdate, o.amount, ct.name, o.netamount, o.$form->{vc}_id,
84 ex.$rate AS exchangerate,
85 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
88 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
89 JOIN orderitems oi ON (oi.trans_id = o.id)
90 JOIN parts p ON (p.id = oi.parts_id)|;
92 if ($warehouse_id && $form->{type} eq 'ship_order') {
94 JOIN inventory i ON (oi.parts_id = i.parts_id)
99 LEFT JOIN employee e ON (o.employee_id = e.id)
100 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
101 AND ex.transdate = o.transdate)
102 WHERE o.quotation = '0'
103 AND (p.inventory_accno_id > 0 OR p.assembly = '1')
104 AND oi.qty <> oi.ship
107 if ($warehouse_id && $form->{type} eq 'ship_order') {
109 AND i.warehouse_id = $warehouse_id
110 AND i.qty >= (oi.qty - oi.ship)
116 if ($form->{"$form->{vc}_id"}) {
117 $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
119 if ($form->{ $form->{vc} }) {
120 $query .= " AND lower(ct.name) LIKE '$name'";
123 if (!$form->{open} && !$form->{closed}) {
124 $query .= " AND o.id = 0";
125 } elsif (!($form->{open} && $form->{closed})) {
126 $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
129 my $sortorder = join ', ',
130 ("o.id", $form->sort_columns(transdate, $ordnumber, name));
131 $sortorder = $form->{sort} if $form->{sort};
133 $query .= " AND lower($ordnumber) LIKE '$number'" if $form->{$ordnumber};
134 $query .= " AND o.transdate >= '$form->{transdatefrom}'"
135 if $form->{transdatefrom};
136 $query .= " AND o.transdate <= '$form->{transdateto}'"
137 if $form->{transdateto};
138 $query .= " ORDER by $sortorder";
140 my $sth = $dbh->prepare($query);
141 $sth->execute || $form->dberror($query);
144 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
145 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
146 push @{ $form->{OE} }, $ref if $ref->{id} != $id{ $ref->{id} };
147 $id{ $ref->{id} } = $ref->{id};
153 $main::lxdebug->leave_sub();
157 $main::lxdebug->enter_sub();
159 my ($self, $myconfig, $form) = @_;
161 # connect to database, turn off autocommit
162 my $dbh = $form->dbconnect_noauto($myconfig);
164 my ($query, $sth, $null);
165 my $exchangerate = 0;
167 my $service_units = AM->retrieve_units($myconfig,$form,"service");
168 my $part_units = AM->retrieve_units($myconfig,$form,"dimension");
169 $form->{service_units} =$service_units;
170 $form->{part_units} =$part_units;
172 ($null, $form->{employee_id}) = split /--/, $form->{employee};
173 unless ($form->{employee_id}) {
174 $form->get_employee($dbh);
177 $form->{contact_id} = $form->{cp_id};
178 $form->{contact_id} *= 1;
179 $form->{payment_id} *= 1;
180 $form->{language_id} *= 1;
181 $form->{shipto_id} *= 1;
182 $form->{delivery_customer_id} *= 1;
183 $form->{delivery_vendor_id} *= 1;
185 my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
189 &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
191 $query = qq|DELETE FROM orderitems
192 WHERE trans_id = $form->{id}|;
193 $dbh->do($query) || $form->dberror($query);
195 $query = qq|DELETE FROM shipto
196 WHERE trans_id = $form->{id} AND module = 'OE'|;
197 $dbh->do($query) || $form->dberror($query);
201 my $uid = rand() . time;
203 $uid .= $form->{login};
205 $uid = substr($uid, 2, 75);
207 $query = qq|INSERT INTO oe (ordnumber, employee_id)
208 VALUES ('$uid', $form->{employee_id})|;
209 $dbh->do($query) || $form->dberror($query);
211 $query = qq|SELECT o.id FROM oe o
212 WHERE o.ordnumber = '$uid'|;
213 $sth = $dbh->prepare($query);
214 $sth->execute || $form->dberror($query);
216 ($form->{id}) = $sth->fetchrow_array;
220 map { $form->{$_} =~ s/\'/\'\'/g }
221 qw(ordnumber quonumber shippingpoint shipvia notes intnotes message);
236 for my $i (1 .. $form->{rowcount}) {
240 $form->parse_amount($myconfig, $form->{"${_}_$i"})
243 if ($form->{"qty_$i"}) {
246 $query = qq|SELECT p.unit
248 WHERE p.id = $form->{"id_$i"}|;
249 $sth = $dbh->prepare($query);
250 $sth->execute || $form->dberror($query);
252 my ($item_unit) = $sth->fetchrow_array();
255 if ($form->{"inventory_accno_$i"}) {
256 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
257 $basefactor = $part_units->{$form->{"unit_$i"}}->{factor} / $part_units->{$item_unit}->{factor};
261 $baseqty = $form->{"qty_$i"} * $basefactor;
263 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
264 $basefactor = $service_units->{$form->{"unit_$i"}}->{factor} / $service_units->{$item_unit}->{factor};
268 $baseqty = $form->{"qty_$i"} * $basefactor;
271 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
272 qw(partnumber description unit);
274 # set values to 0 if nothing entered
275 $form->{"discount_$i"} =
276 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
278 $form->{"sellprice_$i"} =
279 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
280 $fxsellprice = $form->{"sellprice_$i"};
282 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
284 my $decimalplaces = ($dec > 2) ? $dec : 2;
287 $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
289 $form->{"sellprice_$i"} =
290 $form->round_amount($form->{"sellprice_$i"} - $discount,
293 $form->{"inventory_accno_$i"} *= 1;
294 $form->{"expense_accno_$i"} *= 1;
297 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
299 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
303 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
305 if ($form->{taxincluded}) {
306 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
307 $taxbase = $linetotal - $taxamount;
309 # we are not keeping a natural price, do not round
310 $form->{"sellprice_$i"} =
311 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
313 $taxamount = $linetotal * $taxrate;
314 $taxbase = $linetotal;
317 if ($form->round_amount($taxrate, 7) == 0) {
318 if ($form->{taxincluded}) {
319 foreach $item (@taxaccounts) {
321 $form->round_amount($linetotal * $form->{"${item}_rate"} /
322 (1 + abs($form->{"${item}_rate"})),
325 $taxaccounts{$item} += $taxamount;
326 $taxdiff += $taxamount;
328 $taxbase{$item} += $taxbase;
330 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
332 foreach $item (@taxaccounts) {
333 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
334 $taxbase{$item} += $taxbase;
338 foreach $item (@taxaccounts) {
339 $taxaccounts{$item} +=
340 $taxamount * $form->{"${item}_rate"} / $taxrate;
341 $taxbase{$item} += $taxbase;
345 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
347 $project_id = 'NULL';
348 if ($form->{"projectnumber_$i"}) {
349 $project_id = $form->{"projectnumber_$i"};
352 ($form->{"reqdate_$i"}) ? qq|'$form->{"reqdate_$i"}'| : "NULL";
354 # get pricegroup_id and save ist
355 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_pg_$i"};
357 $subtotal = $form->{"subtotal_$i"} * 1;
359 # save detail record in orderitems table
360 $query = qq|INSERT INTO orderitems (|;
361 $query .= "id, " if $form->{"orderitems_id_$i"};
362 $query .= qq|trans_id, parts_id, description, longdescription, qty, base_qty, sellprice, discount,
363 unit, reqdate, project_id, serialnumber, ship, pricegroup_id,
364 ordnumber, transdate, cusordnumber, subtotal)
366 $query .= qq|$form->{"orderitems_id_$i"},|
367 if $form->{"orderitems_id_$i"};
368 $query .= qq|$form->{id}, $form->{"id_$i"},
369 '$form->{"description_$i"}', '$form->{"longdescription_$i"}', $form->{"qty_$i"}, $baseqty,
370 $fxsellprice, $form->{"discount_$i"},
371 '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
372 '$form->{"serialnumber_$i"}', $form->{"ship_$i"}, '$pricegroup_id',
373 '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}', '$subtotal')|;
374 $dbh->do($query) || $form->dberror($query);
376 $form->{"sellprice_$i"} = $fxsellprice;
377 $form->{"discount_$i"} *= 100;
381 # set values which could be empty
382 map { $form->{$_} *= 1 }
383 qw(vendor_id customer_id taxincluded closed quotation);
385 $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
389 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
391 $amount = $form->round_amount($netamount + $tax, 2);
392 $netamount = $form->round_amount($netamount, 2);
394 if ($form->{currency} eq $form->{defaultcurrency}) {
395 $form->{exchangerate} = 1;
398 $form->check_exchangerate($myconfig,
401 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
404 $form->{exchangerate} =
407 : $form->parse_amount($myconfig, $form->{exchangerate});
411 # fill in subject if there is none
412 if ($form->{type} =~ /_order$/) {
414 $form->{subject} = qq|$form->{label} $form->{ordnumber}|
415 unless $form->{subject};
418 $form->{subject} = qq|$form->{label} $form->{quonumber}|
419 unless $form->{subject};
422 # if there is a message stuff it into the intnotes
423 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
424 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
425 my $now = scalar localtime;
426 $form->{intnotes} .= qq|\r
427 \r| if $form->{intnotes};
429 $form->{intnotes} .= qq|[email]\r
432 $cc${bcc}Subject: $form->{subject}\r
434 Message: $form->{message}\r| if $form->{message};
436 ($null, $form->{department_id}) = split(/--/, $form->{department});
437 $form->{department_id} *= 1;
438 $form->{payment_id} *= 1;
439 $form->{language_id} *= 1;
440 $form->{taxzone_id} *= 1;
441 $form->{proforma} *= 1;
446 $query = qq|UPDATE oe set
447 ordnumber = '$form->{ordnumber}',
448 quonumber = '$form->{quonumber}',
449 cusordnumber = '$form->{cusordnumber}',
450 transdate = '$form->{transdate}',
451 vendor_id = $form->{vendor_id},
452 customer_id = $form->{customer_id},
454 netamount = $netamount,
456 taxincluded = '$form->{taxincluded}',
457 shippingpoint = '$form->{shippingpoint}',
458 shipvia = '$form->{shipvia}',
459 notes = '$form->{notes}',
460 intnotes = '$form->{intnotes}',
461 curr = '$form->{currency}',
462 closed = '$form->{closed}',
463 proforma = '$form->{proforma}',
464 quotation = '$quotation',
465 department_id = $form->{department_id},
466 language_id = $form->{language_id},
467 taxzone_id = $form->{taxzone_id},
468 shipto_id = $form->{shipto_id},
469 payment_id = $form->{payment_id},
470 delivery_vendor_id = $form->{delivery_vendor_id},
471 delivery_customer_id = $form->{delivery_customer_id},
472 employee_id = $form->{employee_id},
473 cp_id = $form->{contact_id}
474 WHERE id = $form->{id}|;
475 $dbh->do($query) || $form->dberror($query);
477 $form->{ordtotal} = $amount;
479 if ($form->{webdav}) {
480 &webdav_folder($myconfig, $form);
484 $form->{name} = $form->{ $form->{vc} };
485 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
487 if (!$form->{shipto_id}) {
488 $form->add_shipto($dbh, $form->{id}, "OE");
491 # save printed, emailed, queued
492 $form->save_status($dbh);
494 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
495 if ($form->{vc} eq 'customer') {
496 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
497 $form->{exchangerate}, 0);
499 if ($form->{vc} eq 'vendor') {
500 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
501 0, $form->{exchangerate});
505 if ($form->{type} =~ /_order$/) {
508 &adj_onhand($dbh, $form, $ml * -1);
509 &adj_inventory($dbh, $myconfig, $form);
512 my $rc = $dbh->commit;
515 $main::lxdebug->leave_sub();
520 # this function closes multiple orders given in $form->{ordnumber_#}.
521 # use this for multiple orders that don't have to be saved back
522 # single orders should use OE::save instead.
524 $main::lxdebug->enter_sub();
526 my ($self, $myconfig, $form) = @_;
528 for my $i (1 .. $form->{rowcount}) {
532 $form->parse_amount($myconfig, $form->{"${_}_$i"})
535 $form->{"orderitems_id_$i"} = "";
538 if ($form->{"qty_$i"}) {
540 # set values to 0 if nothing entered
541 $form->{"discount_$i"} =
542 $form->parse_amount($myconfig, $form->{"discount_$i"});
544 $form->{"sellprice_$i"} =
545 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
550 map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} }
551 (1 .. $form->{rowcount});
553 my $dbh = $form->dbconnect($myconfig);
554 $query = qq|UPDATE oe SET
556 WHERE ordnumber IN (|
557 . join(', ', map { $dbh->quote($_) } @ids) . qq|)|;
558 $dbh->do($query) || $form->dberror($query);
561 $main::lxdebug->leave_sub();
565 $main::lxdebug->enter_sub();
567 my ($self, $myconfig, $form) = @_;
569 $main::lxdebug->leave_sub() unless ($form->{"id"});
571 my $dbh = $form->dbconnect($myconfig);
572 do_query($form, $dbh, qq|UPDATE oe SET closed = TRUE where ordnumber = ?|,
576 $main::lxdebug->leave_sub();
580 $main::lxdebug->enter_sub();
582 my ($self, $myconfig, $form, $spool) = @_;
584 # connect to database
585 my $dbh = $form->dbconnect_noauto($myconfig);
588 my $query = qq|SELECT s.spoolfile FROM status s
589 WHERE s.trans_id = $form->{id}|;
590 $sth = $dbh->prepare($query);
591 $sth->execute || $self->dberror($query);
596 while (($spoolfile) = $sth->fetchrow_array) {
597 push @spoolfiles, $spoolfile;
601 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
602 WHERE o.trans_id = $form->{id}|;
603 $sth = $dbh->prepare($query);
604 $sth->execute || $self->dberror($query);
606 while (my ($id, $ship) = $sth->fetchrow_array) {
607 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
612 $query = qq|DELETE FROM inventory
613 WHERE oe_id = $form->{id}|;
614 $dbh->do($query) || $form->dberror($query);
616 # delete status entries
617 $query = qq|DELETE FROM status
618 WHERE trans_id = $form->{id}|;
619 $dbh->do($query) || $form->dberror($query);
622 $query = qq|DELETE FROM oe
623 WHERE id = $form->{id}|;
624 $dbh->do($query) || $form->dberror($query);
626 # delete individual entries
627 $query = qq|DELETE FROM orderitems
628 WHERE trans_id = $form->{id}|;
629 $dbh->do($query) || $form->dberror($query);
631 $query = qq|DELETE FROM shipto
632 WHERE trans_id = $form->{id} AND module = 'OE'|;
633 $dbh->do($query) || $form->dberror($query);
635 my $rc = $dbh->commit;
639 foreach $spoolfile (@spoolfiles) {
640 unlink "$spool/$spoolfile" if $spoolfile;
644 $main::lxdebug->leave_sub();
650 $main::lxdebug->enter_sub();
652 my ($self, $myconfig, $form) = @_;
654 # connect to database
655 my $dbh = $form->dbconnect_noauto($myconfig);
659 # translate the ids (given by id_# and trans_id_#) into one array of ids, so we can join them later
661 push @ids, $form->{"trans_id_$_"}
662 if ($form->{"id_$_"} and $form->{"trans_id_$_"})
663 } (1 .. $form->{"rowcount"});
665 # if called in multi id mode, and still only got one id, switch back to single id
666 if ($form->{"rowcount"} and $#ids == 0) {
667 $form->{"id"} = $ids[0];
673 # get default accounts and last order number
674 $query = qq|SELECT (SELECT c.accno FROM chart c
675 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
676 (SELECT c.accno FROM chart c
677 WHERE d.income_accno_id = c.id) AS income_accno,
678 (SELECT c.accno FROM chart c
679 WHERE d.expense_accno_id = c.id) AS expense_accno,
680 (SELECT c.accno FROM chart c
681 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
682 (SELECT c.accno FROM chart c
683 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
687 $query = qq|SELECT (SELECT c.accno FROM chart c
688 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
689 (SELECT c.accno FROM chart c
690 WHERE d.income_accno_id = c.id) AS income_accno,
691 (SELECT c.accno FROM chart c
692 WHERE d.expense_accno_id = c.id) AS expense_accno,
693 (SELECT c.accno FROM chart c
694 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
695 (SELECT c.accno FROM chart c
696 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
697 d.curr AS currencies,
698 current_date AS transdate, current_date AS reqdate
701 my $sth = $dbh->prepare($query);
702 $sth->execute || $form->dberror($query);
704 my $ref = $sth->fetchrow_hashref(NAME_lc);
705 map { $form->{$_} = $ref->{$_} } keys %$ref;
708 ($form->{currency}) = split /:/, $form->{currencies};
710 # set reqdate if this is an invoice->order conversion. If someone knows a better check to ensure
711 # we come from invoices, feel free.
712 $form->{reqdate} = $form->{deliverydate}
713 if ( $form->{deliverydate}
714 and $form->{callback} =~ /action=ar_transactions/);
716 if ($form->{id} or @ids) {
718 # retrieve order for single id
719 # NOTE: this query is intended to fetch all information only ONCE.
720 # so if any of these infos is important (or even different) for any item,
721 # it will be killed out and then has to be fetched from the item scope query further down
722 $query = qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate,
723 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
724 o.curr AS currency, e.name AS employee, o.employee_id,
725 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
726 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
727 d.description AS department, o.payment_id, o.language_id, o.taxzone_id, o.delivery_customer_id, o.delivery_vendor_id, o.proforma, o.shipto_id
729 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
730 LEFT JOIN employee e ON (o.employee_id = e.id)
731 LEFT JOIN department d ON (o.department_id = d.id)
734 ? qq|WHERE o.id = $form->{id}|
735 : qq|WHERE o.id IN (| . join(', ', @ids) . qq|)|);
737 #$main::lxdebug->message(0, $query);
739 $sth = $dbh->prepare($query);
740 $sth->execute || $form->dberror($query);
742 $ref = $sth->fetchrow_hashref(NAME_lc);
743 map { $form->{$_} = $ref->{$_} } keys %$ref;
747 # set all entries for multiple ids blank that yield different information
748 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
749 map { $form->{$_} = '' if ($ref->{$_} ne $form->{$_}) } keys %$ref;
752 # if not given, fill transdate with current_date
753 $form->{transdate} = $form->current_date($myconfig)
754 unless $form->{transdate};
758 if ($form->{delivery_customer_id}) {
759 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_customer_id}|;
760 $sth = $dbh->prepare($query);
761 $sth->execute || $form->dberror($query);
762 ($form->{delivery_customer_string}) = $sth->fetchrow_array();
766 if ($form->{delivery_vendor_id}) {
767 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_vendor_id}|;
768 $sth = $dbh->prepare($query);
769 $sth->execute || $form->dberror($query);
770 ($form->{delivery_vendor_string}) = $sth->fetchrow_array();
774 # shipto and pinted/mailed/queued status makes only sense for single id retrieve
776 $query = qq|SELECT s.* FROM shipto s
777 WHERE s.trans_id = $form->{id} AND s.module = 'OE'|;
778 $sth = $dbh->prepare($query);
779 $sth->execute || $form->dberror($query);
781 $ref = $sth->fetchrow_hashref(NAME_lc);
783 map { $form->{$_} = $ref->{$_} } keys %$ref;
786 # get printed, emailed and queued
787 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
789 WHERE s.trans_id = $form->{id}|;
790 $sth = $dbh->prepare($query);
791 $sth->execute || $form->dberror($query);
793 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
794 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
795 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
796 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
797 if $ref->{spoolfile};
800 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
803 my %oid = ('Pg' => 'oid',
804 'Oracle' => 'rowid');
807 $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
809 if(!$form->{taxzone_id}) {
810 $form->{taxzone_id} = 0;
812 # retrieve individual items
813 # this query looks up all information about the items
814 # stuff different from the whole will not be overwritten, but saved with a suffix.
815 $query = qq|SELECT o.id AS orderitems_id,
816 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
817 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
818 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
819 oe.ordnumber AS ordnumber_oe, oe.transdate AS transdate_oe, oe.cusordnumber AS cusordnumber_oe,
820 p.partnumber, p.assembly, o.description, o.qty,
821 o.sellprice, o.parts_id AS id, o.unit, o.discount, p.bin, p.notes AS partnotes, p.inventory_accno_id AS part_inventory_accno_id,
822 o.reqdate, o.project_id, o.serialnumber, o.ship,
823 o.ordnumber, o.transdate, o.cusordnumber, o.subtotal, o.longdescription,
824 pr.projectnumber, p.formel,
825 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
827 JOIN parts p ON (o.parts_id = p.id)
828 JOIN oe ON (o.trans_id = oe.id)
829 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
830 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
831 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
832 LEFT JOIN project pr ON (o.project_id = pr.id)
833 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
836 ? qq|WHERE o.trans_id = $form->{id}|
837 : qq|WHERE o.trans_id IN (| . join(", ", @ids) . qq|)|)
839 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
841 $sth = $dbh->prepare($query);
842 $sth->execute || $form->dberror($query);
844 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
845 if (!$ref->{"part_inventory_accno_id"}) {
846 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
848 delete($ref->{"part_inventory_accno_id"});
850 # in collective order, copy global ordnumber, transdate, cusordnumber into item scope
851 # unless already present there
852 # remove _oe entries afterwards
853 map { $ref->{$_} = $ref->{"${_}_oe"} if ($ref->{$_} eq '') }
854 qw|ordnumber transdate cusordnumber|
856 map { delete $ref->{$_} } qw|ordnumber_oe transdate_oe cusordnumber_oe|;
860 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
861 my $query = qq| SELECT accno AS inventory_accno, new_chart_id AS inventory_new_chart, date($transdate) - valid_from AS inventory_valid FROM chart WHERE id = $ref->{inventory_new_chart}|;
862 my $stw = $dbh->prepare($query);
863 $stw->execute || $form->dberror($query);
864 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
868 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
869 my $query = qq| SELECT accno AS income_accno, new_chart_id AS income_new_chart, date($transdate) - valid_from AS income_valid FROM chart WHERE id = $ref->{income_new_chart}|;
870 my $stw = $dbh->prepare($query);
871 $stw->execute || $form->dberror($query);
872 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
876 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
877 my $query = qq| SELECT accno AS expense_accno, new_chart_id AS expense_new_chart, date($transdate) - valid_from AS expense_valid FROM chart WHERE id = $ref->{expense_new_chart}|;
878 my $stw = $dbh->prepare($query);
879 $stw->execute || $form->dberror($query);
880 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
884 # delete orderitems_id in collective orders, so that they get cloned no matter what
885 delete $ref->{orderitems_id} if (@ids);
887 # get tax rates and description
889 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
890 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
891 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
892 WHERE t.id in (SELECT tk.tax_id from taxkeys tk where tk.chart_id = (SELECT id from chart WHERE accno='$accno_id') AND startdate<=$transdate ORDER BY startdate desc LIMIT 1)
894 $stw = $dbh->prepare($query);
895 $stw->execute || $form->dberror($query);
896 $ref->{taxaccounts} = "";
898 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
900 # if ($customertax{$ref->{accno}}) {
901 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
905 $ref->{taxaccounts} .= "$ptr->{accno} ";
906 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
907 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
908 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
909 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
910 $form->{taxaccounts} .= "$ptr->{accno} ";
915 chop $ref->{taxaccounts};
916 push @{ $form->{form_details} }, $ref;
924 $form->lastname_used($dbh, $myconfig, $form->{vc})
925 unless $form->{"$form->{vc}_id"};
929 $form->{exchangerate} =
930 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
931 ($form->{vc} eq 'customer') ? "buy" : "sell");
933 if ($form->{webdav}) {
934 &webdav_folder($myconfig, $form);
938 $query = qq|SELECT id, description
940 $sth = $dbh->prepare($query);
941 $sth->execute || $form->dberror($query);
944 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
945 push @{ $form->{TAXZONE} }, $ref;
950 my $rc = $dbh->commit;
953 $main::lxdebug->leave_sub();
959 $main::lxdebug->enter_sub();
961 my ($self, $myconfig, $form) = @_;
963 # connect to database
964 my $dbh = $form->dbconnect($myconfig);
969 my $nodiscount_subtotal = 0;
970 my $discount_subtotal = 0;
976 my $subtotal_header = 0;
979 my %oid = ('Pg' => 'oid',
980 'Oracle' => 'rowid');
982 # sort items by partsgroup
983 for $i (1 .. $form->{rowcount}) {
985 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
986 $partsgroup = $form->{"partsgroup_$i"};
988 push @partsgroup, [$i, $partsgroup];
991 # if there is a warehouse limit picking
992 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
994 # run query to check for inventory
995 $query = qq|SELECT sum(i.qty) AS qty
998 AND i.warehouse_id = ?|;
999 $sth = $dbh->prepare($query) || $form->dberror($query);
1001 for $i (1 .. $form->{rowcount}) {
1002 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
1004 ($qty) = $sth->fetchrow_array;
1007 $form->{"qty_$i"} = 0 if $qty == 0;
1009 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
1010 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
1016 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
1019 if ($item->[1] ne $sameitem) {
1020 push(@{ $form->{description} }, qq|$item->[1]|);
1021 $sameitem = $item->[1];
1023 map { push(@{ $form->{$_} }, "") }
1024 qw(runningnumber number qty ship unit bin partnotes
1025 serialnumber reqdate sellprice listprice netprice
1026 discount p_discount linetotal);
1029 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
1031 if ($form->{"qty_$i"} != 0) {
1033 # add number, description and qty to $form->{number}, ....
1035 if ($form->{"subtotal_$i"} && !$subtotal_header) {
1036 $subtotal_header = $i;
1037 $position = int($position);
1040 } elsif ($subtotal_header) {
1042 $position = int($position);
1043 $position = $position.".".$subposition;
1045 $position = int($position);
1049 push(@{ $form->{runningnumber} }, $i);
1050 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
1051 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
1052 push(@{ $form->{longdescription} }, qq|$form->{"longdescription_$i"}|);
1053 push(@{ $form->{qty} },
1054 $form->format_amount($myconfig, $form->{"qty_$i"}));
1055 push(@{ $form->{ship} },
1056 $form->format_amount($myconfig, $form->{"ship_$i"}));
1057 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
1058 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
1059 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
1060 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
1061 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
1063 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
1065 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
1067 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
1068 my ($dec) = ($sellprice =~ /\.(\d+)/);
1070 my $decimalplaces = ($dec > 2) ? $dec : 2;
1073 $form->round_amount(
1074 $sellprice * $form->parse_amount($myconfig,
1075 $form->{"discount_$i"}) / 100,
1079 $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
1081 # keep a netprice as well, (sellprice - discount)
1082 #$form->{"netprice_$i"} = $sellprice - $discount;
1083 $form->{"netprice_$i"} = $sellprice - $i_discount;
1084 my $nodiscount_linetotal =
1085 $form->round_amount($form->{"qty_$i"} * $sellprice, 2);
1087 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
1089 push(@{ $form->{netprice} },
1090 ($form->{"netprice_$i"} != 0)
1091 ? $form->format_amount(
1092 $myconfig, $form->{"netprice_$i"},
1099 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
1101 $linetotal = ($linetotal != 0) ? $linetotal : " ";
1103 push(@{ $form->{discount} }, $discount);
1104 push(@{ $form->{p_discount} }, $form->{"discount_$i"});
1106 $form->{ordtotal} += $linetotal;
1107 $discount_subtotal += $linetotal;
1108 $form->{nodiscount_total} += $nodiscount_linetotal;
1109 $nodiscount_subtotal += $nodiscount_linetotal;
1110 $form->{discount_total} += $form->parse_amount($myconfig, $discount);
1112 if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
1113 $discount_subtotal = $form->format_amount($myconfig, $discount_subtotal, 2);
1114 push(@{ $form->{discount_sub} }, $discount_subtotal);
1115 $nodiscount_subtotal = $form->format_amount($myconfig, $nodiscount_subtotal, 2);
1116 push(@{ $form->{nodiscount_sub} }, $nodiscount_subtotal);
1117 $discount_subtotal = 0;
1118 $nodiscount_subtotal = 0;
1119 $subtotal_header = 0;
1121 push(@{ $form->{discount_sub} }, "");
1122 push(@{ $form->{nodiscount_sub} }, "");
1125 if ($linetotal == $netto_linetotal) {
1126 $nodiscount += $linetotal;
1128 push(@{ $form->{linetotal} },
1129 $form->format_amount($myconfig, $linetotal, 2));
1130 push(@{ $form->{nodiscount_linetotal} },
1131 $form->format_amount($myconfig, $nodiscount_linetotal, 2));
1133 my ($taxamount, $taxbase);
1136 map { $taxrate += $form->{"${_}_rate"} } split / /,
1137 $form->{"taxaccounts_$i"};
1139 if ($form->{taxincluded}) {
1142 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
1143 $taxbase = $linetotal / (1 + $taxrate);
1145 $taxamount = $linetotal * $taxrate;
1146 $taxbase = $linetotal;
1149 if ($taxamount != 0) {
1150 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
1151 $taxaccounts{$item} +=
1152 $taxamount * $form->{"${item}_rate"} / $taxrate;
1153 $taxbase{$item} += $taxbase;
1157 $tax_rate = $taxrate * 100;
1158 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
1160 if ($form->{"assembly_$i"}) {
1163 # get parts and push them onto the stack
1165 if ($form->{groupitems}) {
1167 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
1169 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
1172 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
1175 JOIN parts p ON (a.parts_id = p.id)
1176 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1178 AND a.id = '$form->{"id_$i"}'
1180 $sth = $dbh->prepare($query);
1181 $sth->execute || $form->dberror($query);
1183 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1184 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
1185 map { push(@{ $form->{$_} }, "") }
1186 qw(runningnumber ship bin serialnumber number unit bin qty
1187 reqdate sellprice listprice netprice discount p_discount
1188 linetotal nodiscount_linetotal);
1189 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
1190 push(@{ $form->{description} }, $sameitem);
1193 push(@{ $form->{description} },
1194 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
1196 . qq|, $ref->{partnumber}, $ref->{description}|);
1198 map { push(@{ $form->{$_} }, "") }
1199 qw(number unit qty runningnumber ship bin serialnumber reqdate
1200 sellprice listprice netprice discount p_discount linetotal
1201 nodiscount_linetotal);
1211 foreach $item (sort keys %taxaccounts) {
1212 push(@{ $form->{taxbase} },
1213 $form->format_amount($myconfig, $taxbase{$item}, 2));
1215 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
1217 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
1218 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
1219 push(@{ $form->{taxrate} },
1220 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
1221 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
1223 $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
1224 $yesdiscount = $form->{nodiscount_total} - $nodiscount;
1225 $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
1226 $form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
1227 $form->{nodiscount} = $form->format_amount($myconfig, $nodiscount, 2);
1228 $form->{yesdiscount} = $form->format_amount($myconfig, $yesdiscount, 2);
1230 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1232 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
1235 $form->{quototal} = $form->{ordtotal} =
1236 $form->format_amount($myconfig, $form->{ordtotal}, 2);
1238 if ($form->{type} =~ /_quotation/) {
1239 $form->set_payment_options($myconfig, $form->{quodate});
1241 $form->set_payment_options($myconfig, $form->{orddate});
1244 # myconfig variables
1245 map { $form->{$_} = $myconfig->{$_} }
1246 (qw(company address tel fax signature businessnumber));
1247 $form->{username} = $myconfig->{name};
1251 $main::lxdebug->leave_sub();
1254 sub project_description {
1255 $main::lxdebug->enter_sub();
1257 my ($self, $dbh, $id) = @_;
1259 my $query = qq|SELECT p.description
1262 my $sth = $dbh->prepare($query);
1263 $sth->execute || $form->dberror($query);
1265 ($_) = $sth->fetchrow_array;
1269 $main::lxdebug->leave_sub();
1274 sub get_warehouses {
1275 $main::lxdebug->enter_sub();
1277 my ($self, $myconfig, $form) = @_;
1279 my $dbh = $form->dbconnect($myconfig);
1282 my $query = qq|SELECT id, description
1285 my $sth = $dbh->prepare($query);
1286 $sth->execute || $form->dberror($query);
1288 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1289 push @{ $form->{all_warehouses} }, $ref;
1295 $main::lxdebug->leave_sub();
1298 sub save_inventory {
1299 $main::lxdebug->enter_sub();
1301 my ($self, $myconfig, $form) = @_;
1303 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1307 ($null, $employee_id) = split /--/, $form->{employee};
1309 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
1311 my $dbh = $form->dbconnect_noauto($myconfig);
1317 $query = qq|SELECT o.serialnumber, o.ship
1319 WHERE o.trans_id = ?
1322 $sth = $dbh->prepare($query) || $form->dberror($query);
1324 $query = qq|SELECT sum(i.qty)
1326 WHERE i.parts_id = ?
1327 AND i.warehouse_id = ?|;
1328 $wth = $dbh->prepare($query) || $form->dberror($query);
1330 for my $i (1 .. $form->{rowcount} - 1) {
1333 (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
1335 : $form->{"ship_$i"};
1337 if ($warehouse_id && $form->{type} eq 'ship_order') {
1339 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1341 ($qty) = $wth->fetchrow_array;
1352 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1353 qty, oe_id, orderitems_id, shippingdate, employee_id)
1354 VALUES ($form->{"id_$i"}, $warehouse_id,
1355 $ship, $form->{"id"},
1356 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1358 $dbh->do($query) || $form->dberror($query);
1360 # add serialnumber, ship to orderitems
1361 $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
1363 ($serialnumber, $ship) = $sth->fetchrow_array;
1366 $serialnumber .= " " if $serialnumber;
1367 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1368 $ship += $form->{"ship_$i"};
1370 $query = qq|UPDATE orderitems SET
1371 serialnumber = '$serialnumber',
1373 WHERE trans_id = $form->{id}
1374 AND id = $form->{"orderitems_id_$i"}|;
1375 $dbh->do($query) || $form->dberror($query);
1377 # update order with ship via
1378 $query = qq|UPDATE oe SET
1379 shippingpoint = '$form->{shippingpoint}',
1380 shipvia = '$form->{shipvia}'
1381 WHERE id = $form->{id}|;
1382 $dbh->do($query) || $form->dberror($query);
1384 # update onhand for parts
1385 $form->update_balance($dbh, "parts", "onhand",
1386 qq|id = $form->{"id_$i"}|,
1387 $form->{"ship_$i"} * $ml);
1392 my $rc = $dbh->commit;
1395 $main::lxdebug->leave_sub();
1401 $main::lxdebug->enter_sub();
1403 my ($dbh, $form, $ml) = @_;
1405 my $service_units = $form->{service_units};
1406 my $part_units = $form->{part_units};
1408 my $query = qq|SELECT oi.parts_id, oi.ship, oi.unit, p.inventory_accno_id, p.assembly
1410 JOIN parts p ON (p.id = oi.parts_id)
1411 WHERE oi.trans_id = $form->{id}|;
1412 my $sth = $dbh->prepare($query);
1413 $sth->execute || $form->dberror($query);
1415 $query = qq|SELECT sum(p.inventory_accno_id)
1417 JOIN assembly a ON (a.parts_id = p.id)
1419 my $ath = $dbh->prepare($query) || $form->dberror($query);
1423 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1424 #print(STDERR "Bin in Schleife $ref->{inventory_accno_id}\n");
1426 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1428 # do not update if assembly consists of all services
1429 if ($ref->{assembly}) {
1430 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1432 ($ispa) = $sth->fetchrow_array;
1440 $query = qq|SELECT p.unit
1442 WHERE p.id = $ref->{parts_id}|;
1443 my $stw = $dbh->prepare($query);
1444 $stw->execute || $form->dberror($query);
1446 my ($item_unit) = $stw->fetchrow_array();
1449 if ($ref->{inventory_accno_id}) {
1450 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
1451 $basefactor = $part_units->{$ref->{unit}}->{factor} / $part_units->{$item_unit}->{factor};
1455 $baseqty = $ref->{ship} * $basefactor;
1457 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
1458 $basefactor = $service_units->{$ref->{unit}}->{factor} / $part_units->{$item_unit}->{factor};
1462 $baseqty = $ref->{ship} * $basefactor;
1464 #print(STDERR "$baseqty Basismenge\n");
1466 # adjust onhand in parts table
1467 $form->update_balance($dbh, "parts", "onhand",
1468 qq|id = $ref->{parts_id}|,
1475 $main::lxdebug->leave_sub();
1479 $main::lxdebug->enter_sub();
1481 my ($dbh, $myconfig, $form) = @_;
1483 my %oid = ('Pg' => 'oid',
1484 'Oracle' => 'rowid');
1486 # increase/reduce qty in inventory table
1487 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1489 WHERE oi.trans_id = $form->{id}|;
1490 my $sth = $dbh->prepare($query);
1491 $sth->execute || $form->dberror($query);
1493 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1494 (SELECT SUM(qty) FROM inventory
1495 WHERE oe_id = $form->{id}
1496 AND orderitems_id = ?) AS total
1498 WHERE oe_id = $form->{id}
1499 AND orderitems_id = ?|;
1500 my $ith = $dbh->prepare($query) || $form->dberror($query);
1503 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1505 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1507 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1509 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1511 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1512 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1514 $form->update_balance($dbh, "inventory", "qty",
1515 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1524 # delete inventory entries if qty = 0
1525 $query = qq|DELETE FROM inventory
1526 WHERE oe_id = $form->{id}
1528 $dbh->do($query) || $form->dberror($query);
1530 $main::lxdebug->leave_sub();
1534 $main::lxdebug->enter_sub();
1536 my ($self, $myconfig, $form) = @_;
1538 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1541 my $dbh = $form->dbconnect($myconfig);
1543 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1546 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1547 WHERE p.onhand > 0|;
1549 if ($form->{partnumber}) {
1550 $var = $form->like(lc $form->{partnumber});
1552 AND lower(p.partnumber) LIKE '$var'";
1554 if ($form->{description}) {
1555 $var = $form->like(lc $form->{description});
1557 AND lower(p.description) LIKE '$var'";
1559 if ($form->{partsgroup}) {
1560 $var = $form->like(lc $form->{partsgroup});
1562 AND lower(pg.partsgroup) LIKE '$var'";
1565 $sth = $dbh->prepare($query);
1566 $sth->execute || $form->dberror($query);
1568 $query = qq|SELECT sum(i.qty), w.description, w.id
1570 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1571 WHERE i.parts_id = ?
1572 AND NOT i.warehouse_id = $warehouse_id
1573 GROUP BY w.description, w.id|;
1574 $wth = $dbh->prepare($query) || $form->dberror($query);
1576 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1578 $wth->execute($ref->{id}) || $form->dberror;
1580 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1581 push @{ $form->{all_inventory} },
1582 { 'id' => $ref->{id},
1583 'partnumber' => $ref->{partnumber},
1584 'description' => $ref->{description},
1585 'partsgroup' => $ref->{partsgroup},
1587 'warehouse_id' => $warehouse_id,
1588 'warehouse' => $warehouse }
1598 @{ $form->{all_inventory} } =
1599 sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
1600 @{ $form->{all_inventory} };
1602 $main::lxdebug->leave_sub();
1604 return @{ $form->{all_inventory} };
1608 $main::lxdebug->enter_sub();
1610 my ($self, $myconfig, $form) = @_;
1612 my $dbh = $form->dbconnect_noauto($myconfig);
1614 my $query = qq|INSERT INTO inventory
1615 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1616 VALUES (?, ?, ?, ?, ?)|;
1617 $sth = $dbh->prepare($query) || $form->dberror($query);
1619 $form->get_employee($dbh);
1624 $shippingdate = "$a[5]-$a[4]-$a[3]";
1626 for my $i (1 .. $form->{rowcount}) {
1627 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1629 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1634 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
1635 $shippingdate, $form->{employee_id})
1641 $sth->execute($form->{"warehouse_id_$i"},
1642 $form->{"id_$i"}, $qty * -1, $shippingdate,
1643 $form->{employee_id})
1650 my $rc = $dbh->commit;
1653 $main::lxdebug->leave_sub();
1659 $main::lxdebug->enter_sub();
1661 my ($myconfig, $form) = @_;
1664 $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
1665 if ($form->{type} eq "sales_quotation");
1666 $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
1667 if ($form->{type} eq "sales_order");
1668 $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
1669 if ($form->{type} eq "request_quotation");
1670 $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
1671 if ($form->{type} eq "purchase_order");
1675 mkdir($path, 0770) or die "can't make directory $!\n";
1679 foreach $file (@files) {
1680 $file =~ /\/([^\/]*)$/;
1682 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1684 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1685 $form->{WEBDAV}{$fname} = $link;
1690 $main::lxdebug->leave_sub();