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 #======================================================================
40 $main::lxdebug->enter_sub();
42 my ($self, $myconfig, $form) = @_;
45 my $dbh = $form->dbconnect($myconfig);
48 my $ordnumber = 'ordnumber';
50 my ($null, $department_id) = split /--/, $form->{department};
52 my $department = " AND o.department_id = $department_id" if $department_id;
54 my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
56 if ($form->{type} =~ /_quotation$/) {
58 $ordnumber = 'quonumber';
61 my $number = $form->like(lc $form->{$ordnumber});
62 my $name = $form->like(lc $form->{ $form->{vc} });
64 my $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
65 o.amount, ct.name, o.netamount, o.$form->{vc}_id,
66 ex.$rate AS exchangerate,
67 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
70 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
71 LEFT JOIN employee e ON (o.employee_id = e.id)
72 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
73 AND ex.transdate = o.transdate)
74 WHERE o.quotation = '$quotation'
77 # build query if type eq (ship|receive)_order
78 if ($form->{type} =~ /(ship|receive)_order/) {
79 my ($warehouse, $warehouse_id) = split /--/, $form->{warehouse};
81 $query = qq|SELECT DISTINCT ON (o.id) o.id, o.ordnumber, o.transdate,
82 o.reqdate, o.amount, ct.name, o.netamount, o.$form->{vc}_id,
83 ex.$rate AS exchangerate,
84 o.closed, o.quonumber, o.shippingpoint, o.shipvia,
87 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
88 JOIN orderitems oi ON (oi.trans_id = o.id)
89 JOIN parts p ON (p.id = oi.parts_id)|;
91 if ($warehouse_id && $form->{type} eq 'ship_order') {
93 JOIN inventory i ON (oi.parts_id = i.parts_id)
98 LEFT JOIN employee e ON (o.employee_id = e.id)
99 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
100 AND ex.transdate = o.transdate)
101 WHERE o.quotation = '0'
102 AND (p.inventory_accno_id > 0 OR p.assembly = '1')
103 AND oi.qty <> oi.ship
106 if ($warehouse_id && $form->{type} eq 'ship_order') {
108 AND i.warehouse_id = $warehouse_id
109 AND i.qty >= (oi.qty - oi.ship)
115 if ($form->{"$form->{vc}_id"}) {
116 $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
118 if ($form->{ $form->{vc} }) {
119 $query .= " AND lower(ct.name) LIKE '$name'";
122 if (!$form->{open} && !$form->{closed}) {
123 $query .= " AND o.id = 0";
124 } elsif (!($form->{open} && $form->{closed})) {
125 $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
128 my $sortorder = join ', ',
129 ("o.id", $form->sort_columns(transdate, $ordnumber, name));
130 $sortorder = $form->{sort} if $form->{sort};
132 $query .= " AND lower($ordnumber) LIKE '$number'" if $form->{$ordnumber};
133 $query .= " AND o.transdate >= '$form->{transdatefrom}'"
134 if $form->{transdatefrom};
135 $query .= " AND o.transdate <= '$form->{transdateto}'"
136 if $form->{transdateto};
137 $query .= " ORDER by $sortorder";
139 my $sth = $dbh->prepare($query);
140 $sth->execute || $form->dberror($query);
143 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
144 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
145 push @{ $form->{OE} }, $ref if $ref->{id} != $id{ $ref->{id} };
146 $id{ $ref->{id} } = $ref->{id};
152 $main::lxdebug->leave_sub();
156 $main::lxdebug->enter_sub();
158 my ($self, $myconfig, $form) = @_;
160 # connect to database, turn off autocommit
161 my $dbh = $form->dbconnect_noauto($myconfig);
163 my ($query, $sth, $null);
164 my $exchangerate = 0;
166 my $service_units = AM->retrieve_units($myconfig,$form,"service");
167 my $part_units = AM->retrieve_units($myconfig,$form,"dimension");
168 $form->{service_units} =$service_units;
169 $form->{part_units} =$part_units;
171 ($null, $form->{employee_id}) = split /--/, $form->{employee};
172 unless ($form->{employee_id}) {
173 $form->get_employee($dbh);
176 $form->{contact_id} = $form->{cp_id};
177 $form->{contact_id} *= 1;
178 $form->{payment_id} *= 1;
179 $form->{language_id} *= 1;
180 $form->{shipto_id} *= 1;
181 $form->{delivery_customer_id} *= 1;
182 $form->{delivery_vendor_id} *= 1;
184 my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
188 &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
190 $query = qq|DELETE FROM orderitems
191 WHERE trans_id = $form->{id}|;
192 $dbh->do($query) || $form->dberror($query);
194 $query = qq|DELETE FROM shipto
195 WHERE trans_id = $form->{id} AND module = 'OE'|;
196 $dbh->do($query) || $form->dberror($query);
200 my $uid = rand() . time;
202 $uid .= $form->{login};
204 $uid = substr($uid, 2, 75);
206 $query = qq|INSERT INTO oe (ordnumber, employee_id)
207 VALUES ('$uid', $form->{employee_id})|;
208 $dbh->do($query) || $form->dberror($query);
210 $query = qq|SELECT o.id FROM oe o
211 WHERE o.ordnumber = '$uid'|;
212 $sth = $dbh->prepare($query);
213 $sth->execute || $form->dberror($query);
215 ($form->{id}) = $sth->fetchrow_array;
219 map { $form->{$_} =~ s/\'/\'\'/g }
220 qw(ordnumber quonumber shippingpoint shipvia notes intnotes message);
235 for my $i (1 .. $form->{rowcount}) {
239 $form->parse_amount($myconfig, $form->{"${_}_$i"})
242 if ($form->{"qty_$i"}) {
245 $query = qq|SELECT p.unit
247 WHERE p.id = $form->{"id_$i"}|;
248 $sth = $dbh->prepare($query);
249 $sth->execute || $form->dberror($query);
251 my ($item_unit) = $sth->fetchrow_array();
254 if ($form->{"inventory_accno_$i"}) {
255 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
256 $basefactor = $part_units->{$form->{"unit_$i"}}->{factor} / $part_units->{$item_unit}->{factor};
260 $baseqty = $form->{"qty_$i"} * $basefactor;
262 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
263 $basefactor = $service_units->{$form->{"unit_$i"}}->{factor} / $service_units->{$item_unit}->{factor};
267 $baseqty = $form->{"qty_$i"} * $basefactor;
270 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
271 qw(partnumber description unit);
273 # set values to 0 if nothing entered
274 $form->{"discount_$i"} =
275 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
277 $form->{"sellprice_$i"} =
278 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
279 $fxsellprice = $form->{"sellprice_$i"};
281 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
283 my $decimalplaces = ($dec > 2) ? $dec : 2;
286 $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
288 $form->{"sellprice_$i"} =
289 $form->round_amount($form->{"sellprice_$i"} - $discount,
292 $form->{"inventory_accno_$i"} *= 1;
293 $form->{"expense_accno_$i"} *= 1;
296 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
298 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
302 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
304 if ($form->{taxincluded}) {
305 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
306 $taxbase = $linetotal - $taxamount;
308 # we are not keeping a natural price, do not round
309 $form->{"sellprice_$i"} =
310 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
312 $taxamount = $linetotal * $taxrate;
313 $taxbase = $linetotal;
316 if ($form->round_amount($taxrate, 7) == 0) {
317 if ($form->{taxincluded}) {
318 foreach $item (@taxaccounts) {
320 $form->round_amount($linetotal * $form->{"${item}_rate"} /
321 (1 + abs($form->{"${item}_rate"})),
324 $taxaccounts{$item} += $taxamount;
325 $taxdiff += $taxamount;
327 $taxbase{$item} += $taxbase;
329 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
331 foreach $item (@taxaccounts) {
332 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
333 $taxbase{$item} += $taxbase;
337 foreach $item (@taxaccounts) {
338 $taxaccounts{$item} +=
339 $taxamount * $form->{"${item}_rate"} / $taxrate;
340 $taxbase{$item} += $taxbase;
344 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
346 $project_id = 'NULL';
347 if ($form->{"projectnumber_$i"}) {
348 $project_id = $form->{"projectnumber_$i"};
351 ($form->{"reqdate_$i"}) ? qq|'$form->{"reqdate_$i"}'| : "NULL";
353 # get pricegroup_id and save ist
354 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_pg_$i"};
356 $subtotal = $form->{"subtotal_$i"} * 1;
358 # save detail record in orderitems table
359 $query = qq|INSERT INTO orderitems (|;
360 $query .= "id, " if $form->{"orderitems_id_$i"};
361 $query .= qq|trans_id, parts_id, description, longdescription, qty, base_qty, sellprice, discount,
362 unit, reqdate, project_id, serialnumber, ship, pricegroup_id,
363 ordnumber, transdate, cusordnumber, subtotal)
365 $query .= qq|$form->{"orderitems_id_$i"},|
366 if $form->{"orderitems_id_$i"};
367 $query .= qq|$form->{id}, $form->{"id_$i"},
368 '$form->{"description_$i"}', '$form->{"longdescription_$i"}', $form->{"qty_$i"}, $baseqty,
369 $fxsellprice, $form->{"discount_$i"},
370 '$form->{"unit_$i"}', $reqdate, (SELECT id from project where projectnumber = '$project_id'),
371 '$form->{"serialnumber_$i"}', $form->{"ship_$i"}, '$pricegroup_id',
372 '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}', '$subtotal')|;
373 $dbh->do($query) || $form->dberror($query);
375 $form->{"sellprice_$i"} = $fxsellprice;
376 $form->{"discount_$i"} *= 100;
380 # set values which could be empty
381 map { $form->{$_} *= 1 }
382 qw(vendor_id customer_id taxincluded closed quotation);
384 $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
388 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
390 $amount = $form->round_amount($netamount + $tax, 2);
391 $netamount = $form->round_amount($netamount, 2);
393 if ($form->{currency} eq $form->{defaultcurrency}) {
394 $form->{exchangerate} = 1;
397 $form->check_exchangerate($myconfig,
400 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
403 $form->{exchangerate} =
406 : $form->parse_amount($myconfig, $form->{exchangerate});
410 # fill in subject if there is none
411 if ($form->{type} =~ /_order$/) {
413 $form->{subject} = qq|$form->{label} $form->{ordnumber}|
414 unless $form->{subject};
417 $form->{subject} = qq|$form->{label} $form->{quonumber}|
418 unless $form->{subject};
421 # if there is a message stuff it into the intnotes
422 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
423 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
424 my $now = scalar localtime;
425 $form->{intnotes} .= qq|\r
426 \r| if $form->{intnotes};
428 $form->{intnotes} .= qq|[email]\r
431 $cc${bcc}Subject: $form->{subject}\r
433 Message: $form->{message}\r| if $form->{message};
435 ($null, $form->{department_id}) = split(/--/, $form->{department});
436 $form->{department_id} *= 1;
437 $form->{payment_id} *= 1;
438 $form->{language_id} *= 1;
439 $form->{taxzone_id} *= 1;
440 $form->{proforma} *= 1;
445 $query = qq|UPDATE oe set
446 ordnumber = '$form->{ordnumber}',
447 quonumber = '$form->{quonumber}',
448 cusordnumber = '$form->{cusordnumber}',
449 transdate = '$form->{transdate}',
450 vendor_id = $form->{vendor_id},
451 customer_id = $form->{customer_id},
453 netamount = $netamount,
455 taxincluded = '$form->{taxincluded}',
456 shippingpoint = '$form->{shippingpoint}',
457 shipvia = '$form->{shipvia}',
458 notes = '$form->{notes}',
459 intnotes = '$form->{intnotes}',
460 curr = '$form->{currency}',
461 closed = '$form->{closed}',
462 proforma = '$form->{proforma}',
463 quotation = '$quotation',
464 department_id = $form->{department_id},
465 language_id = $form->{language_id},
466 taxzone_id = $form->{taxzone_id},
467 shipto_id = $form->{shipto_id},
468 payment_id = $form->{payment_id},
469 delivery_vendor_id = $form->{delivery_vendor_id},
470 delivery_customer_id = $form->{delivery_customer_id},
471 employee_id = $form->{employee_id},
472 cp_id = $form->{contact_id}
473 WHERE id = $form->{id}|;
474 $dbh->do($query) || $form->dberror($query);
476 $form->{ordtotal} = $amount;
478 if ($form->{webdav}) {
479 &webdav_folder($myconfig, $form);
483 $form->{name} = $form->{ $form->{vc} };
484 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
486 if (!$form->{shipto_id}) {
487 $form->add_shipto($dbh, $form->{id}, "OE");
490 # save printed, emailed, queued
491 $form->save_status($dbh);
493 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
494 if ($form->{vc} eq 'customer') {
495 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
496 $form->{exchangerate}, 0);
498 if ($form->{vc} eq 'vendor') {
499 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
500 0, $form->{exchangerate});
504 if ($form->{type} =~ /_order$/) {
507 &adj_onhand($dbh, $form, $ml * -1);
508 &adj_inventory($dbh, $myconfig, $form);
511 my $rc = $dbh->commit;
514 $main::lxdebug->leave_sub();
519 # this function closes multiple orders given in $form->{ordnumber_#}.
520 # use this for multiple orders that don't have to be saved back
521 # single orders should use OE::save instead.
523 $main::lxdebug->enter_sub();
525 my ($self, $myconfig, $form) = @_;
527 for my $i (1 .. $form->{rowcount}) {
531 $form->parse_amount($myconfig, $form->{"${_}_$i"})
534 $form->{"orderitems_id_$i"} = "";
537 if ($form->{"qty_$i"}) {
539 # set values to 0 if nothing entered
540 $form->{"discount_$i"} =
541 $form->parse_amount($myconfig, $form->{"discount_$i"});
543 $form->{"sellprice_$i"} =
544 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
549 map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} }
550 (1 .. $form->{rowcount});
552 my $dbh = $form->dbconnect($myconfig);
553 $query = qq|UPDATE oe SET
555 WHERE ordnumber IN (|
556 . join(', ', map { $dbh->quote($_) } @ids) . qq|)|;
557 $dbh->do($query) || $form->dberror($query);
560 $main::lxdebug->leave_sub();
564 $main::lxdebug->enter_sub();
566 my ($self, $myconfig, $form, $spool) = @_;
568 # connect to database
569 my $dbh = $form->dbconnect_noauto($myconfig);
572 my $query = qq|SELECT s.spoolfile FROM status s
573 WHERE s.trans_id = $form->{id}|;
574 $sth = $dbh->prepare($query);
575 $sth->execute || $self->dberror($query);
580 while (($spoolfile) = $sth->fetchrow_array) {
581 push @spoolfiles, $spoolfile;
585 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
586 WHERE o.trans_id = $form->{id}|;
587 $sth = $dbh->prepare($query);
588 $sth->execute || $self->dberror($query);
590 while (my ($id, $ship) = $sth->fetchrow_array) {
591 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
596 $query = qq|DELETE FROM inventory
597 WHERE oe_id = $form->{id}|;
598 $dbh->do($query) || $form->dberror($query);
600 # delete status entries
601 $query = qq|DELETE FROM status
602 WHERE trans_id = $form->{id}|;
603 $dbh->do($query) || $form->dberror($query);
606 $query = qq|DELETE FROM oe
607 WHERE id = $form->{id}|;
608 $dbh->do($query) || $form->dberror($query);
610 # delete individual entries
611 $query = qq|DELETE FROM orderitems
612 WHERE trans_id = $form->{id}|;
613 $dbh->do($query) || $form->dberror($query);
615 $query = qq|DELETE FROM shipto
616 WHERE trans_id = $form->{id} AND module = 'OE'|;
617 $dbh->do($query) || $form->dberror($query);
619 my $rc = $dbh->commit;
623 foreach $spoolfile (@spoolfiles) {
624 unlink "$spool/$spoolfile" if $spoolfile;
628 $main::lxdebug->leave_sub();
634 $main::lxdebug->enter_sub();
636 my ($self, $myconfig, $form) = @_;
638 # connect to database
639 my $dbh = $form->dbconnect_noauto($myconfig);
643 # translate the ids (given by id_# and trans_id_#) into one array of ids, so we can join them later
645 push @ids, $form->{"trans_id_$_"}
646 if ($form->{"id_$_"} and $form->{"trans_id_$_"})
647 } (1 .. $form->{"rowcount"});
649 # if called in multi id mode, and still only got one id, switch back to single id
650 if ($form->{"rowcount"} and $#ids == 0) {
651 $form->{"id"} = $ids[0];
657 # get default accounts and last order number
658 $query = qq|SELECT (SELECT c.accno FROM chart c
659 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
660 (SELECT c.accno FROM chart c
661 WHERE d.income_accno_id = c.id) AS income_accno,
662 (SELECT c.accno FROM chart c
663 WHERE d.expense_accno_id = c.id) AS expense_accno,
664 (SELECT c.accno FROM chart c
665 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
666 (SELECT c.accno FROM chart c
667 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
671 $query = qq|SELECT (SELECT c.accno FROM chart c
672 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
673 (SELECT c.accno FROM chart c
674 WHERE d.income_accno_id = c.id) AS income_accno,
675 (SELECT c.accno FROM chart c
676 WHERE d.expense_accno_id = c.id) AS expense_accno,
677 (SELECT c.accno FROM chart c
678 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
679 (SELECT c.accno FROM chart c
680 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
681 d.curr AS currencies,
682 current_date AS transdate, current_date AS reqdate
685 my $sth = $dbh->prepare($query);
686 $sth->execute || $form->dberror($query);
688 my $ref = $sth->fetchrow_hashref(NAME_lc);
689 map { $form->{$_} = $ref->{$_} } keys %$ref;
692 ($form->{currency}) = split /:/, $form->{currencies};
694 # set reqdate if this is an invoice->order conversion. If someone knows a better check to ensure
695 # we come from invoices, feel free.
696 $form->{reqdate} = $form->{deliverydate}
697 if ( $form->{deliverydate}
698 and $form->{callback} =~ /action=ar_transactions/);
700 if ($form->{id} or @ids) {
702 # retrieve order for single id
703 # NOTE: this query is intended to fetch all information only ONCE.
704 # so if any of these infos is important (or even different) for any item,
705 # it will be killed out and then has to be fetched from the item scope query further down
706 $query = qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate,
707 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
708 o.curr AS currency, e.name AS employee, o.employee_id,
709 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
710 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
711 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
713 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
714 LEFT JOIN employee e ON (o.employee_id = e.id)
715 LEFT JOIN department d ON (o.department_id = d.id)
718 ? qq|WHERE o.id = $form->{id}|
719 : qq|WHERE o.id IN (| . join(', ', @ids) . qq|)|);
721 #$main::lxdebug->message(0, $query);
723 $sth = $dbh->prepare($query);
724 $sth->execute || $form->dberror($query);
726 $ref = $sth->fetchrow_hashref(NAME_lc);
727 map { $form->{$_} = $ref->{$_} } keys %$ref;
731 # set all entries for multiple ids blank that yield different information
732 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
733 map { $form->{$_} = '' if ($ref->{$_} ne $form->{$_}) } keys %$ref;
736 # if not given, fill transdate with current_date
737 $form->{transdate} = $form->current_date($myconfig)
738 unless $form->{transdate};
742 if ($form->{delivery_customer_id}) {
743 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_customer_id}|;
744 $sth = $dbh->prepare($query);
745 $sth->execute || $form->dberror($query);
746 ($form->{delivery_customer_string}) = $sth->fetchrow_array();
750 if ($form->{delivery_vendor_id}) {
751 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_vendor_id}|;
752 $sth = $dbh->prepare($query);
753 $sth->execute || $form->dberror($query);
754 ($form->{delivery_vendor_string}) = $sth->fetchrow_array();
758 # shipto and pinted/mailed/queued status makes only sense for single id retrieve
760 $query = qq|SELECT s.* FROM shipto s
761 WHERE s.trans_id = $form->{id} AND s.module = 'OE'|;
762 $sth = $dbh->prepare($query);
763 $sth->execute || $form->dberror($query);
765 $ref = $sth->fetchrow_hashref(NAME_lc);
767 map { $form->{$_} = $ref->{$_} } keys %$ref;
770 # get printed, emailed and queued
771 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
773 WHERE s.trans_id = $form->{id}|;
774 $sth = $dbh->prepare($query);
775 $sth->execute || $form->dberror($query);
777 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
778 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
779 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
780 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
781 if $ref->{spoolfile};
784 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
787 my %oid = ('Pg' => 'oid',
788 'Oracle' => 'rowid');
790 my $transdate = "'$form->{transdate}'";
792 $transdate = "current_date";
794 if(!$form->{taxzone_id}) {
795 $form->{taxzone_id} = 0;
797 # retrieve individual items
798 # this query looks up all information about the items
799 # stuff different from the whole will not be overwritten, but saved with a suffix.
800 $query = qq|SELECT o.id AS orderitems_id,
801 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
802 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
803 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
804 oe.ordnumber AS ordnumber_oe, oe.transdate AS transdate_oe, oe.cusordnumber AS cusordnumber_oe,
805 p.partnumber, p.assembly, o.description, o.qty,
806 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,
807 o.reqdate, o.project_id, o.serialnumber, o.ship,
808 o.ordnumber, o.transdate, o.cusordnumber, o.subtotal, o.longdescription,
809 pr.projectnumber, p.alu, p.formel,
810 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
812 JOIN parts p ON (o.parts_id = p.id)
813 JOIN oe ON (o.trans_id = oe.id)
814 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
815 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
816 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
817 LEFT JOIN project pr ON (o.project_id = pr.id)
818 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
821 ? qq|WHERE o.trans_id = $form->{id}|
822 : qq|WHERE o.trans_id IN (| . join(", ", @ids) . qq|)|)
824 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
826 $sth = $dbh->prepare($query);
827 $sth->execute || $form->dberror($query);
829 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
830 if (!$ref->{"part_inventory_accno_id"}) {
831 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
833 delete($ref->{"part_inventory_accno_id"});
835 # in collective order, copy global ordnumber, transdate, cusordnumber into item scope
836 # unless already present there
837 # remove _oe entries afterwards
838 map { $ref->{$_} = $ref->{"${_}_oe"} if ($ref->{$_} eq '') }
839 qw|ordnumber transdate cusordnumber|
841 map { delete $ref->{$_} } qw|ordnumber_oe transdate_oe cusordnumber_oe|;
845 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
846 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}|;
847 my $stw = $dbh->prepare($query);
848 $stw->execute || $form->dberror($query);
849 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
853 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
854 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}|;
855 my $stw = $dbh->prepare($query);
856 $stw->execute || $form->dberror($query);
857 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
861 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
862 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}|;
863 my $stw = $dbh->prepare($query);
864 $stw->execute || $form->dberror($query);
865 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
869 # delete orderitems_id in collective orders, so that they get cloned no matter what
870 delete $ref->{orderitems_id} if (@ids);
872 #set expense_accno=inventory_accno if they are different => bilanz
874 ($ref->{expense_accno} != $ref->{inventory_accno})
875 ? $ref->{inventory_accno}
876 : $ref->{expense_accno};
878 # get tax rates and description
880 ($form->{vc} eq "customer") ? $ref->{income_accno} : $vendor_accno;
881 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
882 FROM tax t LEFT JOIN chart c ON (c.id=t.chart_id)
883 WHERE t.taxkey in (SELECT taxkey_id from chart where accno = '$accno_id')
885 $stw = $dbh->prepare($query);
886 $stw->execute || $form->dberror($query);
887 $ref->{taxaccounts} = "";
889 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
891 # if ($customertax{$ref->{accno}}) {
892 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
896 $ref->{taxaccounts} .= "$ptr->{accno} ";
897 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
898 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
899 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
900 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
901 $form->{taxaccounts} .= "$ptr->{accno} ";
906 chop $ref->{taxaccounts};
907 push @{ $form->{form_details} }, $ref;
915 $form->lastname_used($dbh, $myconfig, $form->{vc})
916 unless $form->{"$form->{vc}_id"};
920 $form->{exchangerate} =
921 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
922 ($form->{vc} eq 'customer') ? "buy" : "sell");
924 if ($form->{webdav}) {
925 &webdav_folder($myconfig, $form);
929 $query = qq|SELECT id, description
931 $sth = $dbh->prepare($query);
932 $sth->execute || $form->dberror($query);
935 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
936 push @{ $form->{TAXZONE} }, $ref;
941 my $rc = $dbh->commit;
944 $main::lxdebug->leave_sub();
950 $main::lxdebug->enter_sub();
952 my ($self, $myconfig, $form) = @_;
954 # connect to database
955 my $dbh = $form->dbconnect($myconfig);
960 my $nodiscount_subtotal = 0;
961 my $discount_subtotal = 0;
967 my $subtotal_header = 0;
970 my %oid = ('Pg' => 'oid',
971 'Oracle' => 'rowid');
973 # sort items by partsgroup
974 for $i (1 .. $form->{rowcount}) {
976 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
977 $partsgroup = $form->{"partsgroup_$i"};
979 push @partsgroup, [$i, $partsgroup];
982 # if there is a warehouse limit picking
983 if ($form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/) {
985 # run query to check for inventory
986 $query = qq|SELECT sum(i.qty) AS qty
989 AND i.warehouse_id = ?|;
990 $sth = $dbh->prepare($query) || $form->dberror($query);
992 for $i (1 .. $form->{rowcount}) {
993 $sth->execute($form->{"id_$i"}, $form->{warehouse_id}) || $form->dberror;
995 ($qty) = $sth->fetchrow_array;
998 $form->{"qty_$i"} = 0 if $qty == 0;
1000 if ($form->parse_amount($myconfig, $form->{"ship_$i"}) > $qty) {
1001 $form->{"ship_$i"} = $form->format_amount($myconfig, $qty);
1007 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
1010 if ($item->[1] ne $sameitem) {
1011 push(@{ $form->{description} }, qq|$item->[1]|);
1012 $sameitem = $item->[1];
1014 map { push(@{ $form->{$_} }, "") }
1015 qw(runningnumber number qty ship unit bin partnotes serialnumber reqdate sellprice listprice netprice discount linetotal);
1018 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
1020 if ($form->{"qty_$i"} != 0) {
1022 # add number, description and qty to $form->{number}, ....
1024 if ($form->{"subtotal_$i"} && !$subtotal_header) {
1025 $subtotal_header = $i;
1026 $position = int($position);
1029 } elsif ($subtotal_header) {
1031 $position = int($position);
1032 $position = $position.".".$subposition;
1034 $position = int($position);
1038 push(@{ $form->{runningnumber} }, $i);
1039 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
1040 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
1041 push(@{ $form->{longdescription} }, qq|$form->{"longdescription_$i"}|);
1042 push(@{ $form->{qty} },
1043 $form->format_amount($myconfig, $form->{"qty_$i"}));
1044 push(@{ $form->{ship} },
1045 $form->format_amount($myconfig, $form->{"ship_$i"}));
1046 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
1047 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
1048 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
1049 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
1050 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
1052 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
1054 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
1056 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
1057 my ($dec) = ($sellprice =~ /\.(\d+)/);
1059 my $decimalplaces = ($dec > 2) ? $dec : 2;
1062 $form->round_amount(
1063 $sellprice * $form->parse_amount($myconfig,
1064 $form->{"discount_$i"}) / 100,
1068 $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
1070 # keep a netprice as well, (sellprice - discount)
1071 #$form->{"netprice_$i"} = $sellprice - $discount;
1072 $form->{"netprice_$i"} = $sellprice - $i_discount;
1073 my $nodiscount_linetotal =
1074 $form->round_amount($form->{"qty_$i"} * $sellprice, 2);
1076 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
1078 push(@{ $form->{netprice} },
1079 ($form->{"netprice_$i"} != 0)
1080 ? $form->format_amount(
1081 $myconfig, $form->{"netprice_$i"},
1088 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
1090 $linetotal = ($linetotal != 0) ? $linetotal : " ";
1092 push(@{ $form->{discount} }, $discount);
1093 push(@{ $form->{p_discount} }, $form->{"discount_$i"});
1095 $form->{ordtotal} += $linetotal;
1096 $discount_subtotal += $linetotal;
1097 $form->{nodiscount_total} += $nodiscount_linetotal;
1098 $nodiscount_subtotal += $nodiscount_linetotal;
1099 $form->{discount_total} += $form->parse_amount($myconfig, $discount);
1101 if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
1102 $discount_subtotal = $form->format_amount($myconfig, $discount_subtotal, 2);
1103 push(@{ $form->{discount_sub} }, $discount_subtotal);
1104 $nodiscount_subtotal = $form->format_amount($myconfig, $nodiscount_subtotal, 2);
1105 push(@{ $form->{nodiscount_sub} }, $nodiscount_subtotal);
1106 $discount_subtotal = 0;
1107 $nodiscount_subtotal = 0;
1108 $subtotal_header = 0;
1110 push(@{ $form->{discount_sub} }, "");
1111 push(@{ $form->{nodiscount_sub} }, "");
1114 if ($linetotal == $netto_linetotal) {
1115 $nodiscount += $linetotal;
1117 push(@{ $form->{linetotal} },
1118 $form->format_amount($myconfig, $linetotal, 2));
1119 push(@{ $form->{nodiscount_linetotal} },
1120 $form->format_amount($myconfig, $nodiscount_linetotal, 2));
1122 my ($taxamount, $taxbase);
1125 map { $taxrate += $form->{"${_}_rate"} } split / /,
1126 $form->{"taxaccounts_$i"};
1128 if ($form->{taxincluded}) {
1131 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
1132 $taxbase = $linetotal / (1 + $taxrate);
1134 $taxamount = $linetotal * $taxrate;
1135 $taxbase = $linetotal;
1138 if ($taxamount != 0) {
1139 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
1140 $taxaccounts{$item} +=
1141 $taxamount * $form->{"${item}_rate"} / $taxrate;
1142 $taxbase{$item} += $taxbase;
1146 $tax_rate = $taxrate * 100;
1147 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
1149 if ($form->{"assembly_$i"}) {
1152 # get parts and push them onto the stack
1154 if ($form->{groupitems}) {
1156 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
1158 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
1161 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
1164 JOIN parts p ON (a.parts_id = p.id)
1165 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1167 AND a.id = '$form->{"id_$i"}'
1169 $sth = $dbh->prepare($query);
1170 $sth->execute || $form->dberror($query);
1172 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1173 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
1174 map { push(@{ $form->{$_} }, "") }
1175 qw(runningnumber ship bin serialnumber number unit bin qty reqdate sellprice listprice netprice discount linetotal nodiscount_linetotal);
1176 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
1177 push(@{ $form->{description} }, $sameitem);
1180 push(@{ $form->{description} },
1181 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
1183 . qq|, $ref->{partnumber}, $ref->{description}|);
1185 map { push(@{ $form->{$_} }, "") }
1186 qw(number unit qty runningnumber ship bin serialnumber reqdate sellprice listprice netprice discount linetotal nodiscount_linetotal);
1196 foreach $item (sort keys %taxaccounts) {
1197 push(@{ $form->{taxbase} },
1198 $form->format_amount($myconfig, $taxbase{$item}, 2));
1200 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
1202 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
1203 push(@{ $form->{taxdescription} }, $form->{"${item}_description"});
1204 push(@{ $form->{taxrate} },
1205 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
1206 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
1208 $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
1209 $yesdiscount = $form->{nodiscount_total} - $nodiscount;
1210 $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
1211 $form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
1212 $form->{nodiscount} = $form->format_amount($myconfig, $nodiscount, 2);
1213 $form->{yesdiscount} = $form->format_amount($myconfig, $yesdiscount, 2);
1215 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1217 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
1220 $form->{quototal} = $form->{ordtotal} =
1221 $form->format_amount($myconfig, $form->{ordtotal}, 2);
1223 if ($form->{type} =~ /_quotation/) {
1224 $form->set_payment_options($myconfig, $form->{quodate});
1226 $form->set_payment_options($myconfig, $form->{orddate});
1229 # myconfig variables
1230 map { $form->{$_} = $myconfig->{$_} }
1231 (qw(company address tel fax signature businessnumber));
1232 $form->{username} = $myconfig->{name};
1236 $main::lxdebug->leave_sub();
1239 sub project_description {
1240 $main::lxdebug->enter_sub();
1242 my ($self, $dbh, $id) = @_;
1244 my $query = qq|SELECT p.description
1247 my $sth = $dbh->prepare($query);
1248 $sth->execute || $form->dberror($query);
1250 ($_) = $sth->fetchrow_array;
1254 $main::lxdebug->leave_sub();
1259 sub get_warehouses {
1260 $main::lxdebug->enter_sub();
1262 my ($self, $myconfig, $form) = @_;
1264 my $dbh = $form->dbconnect($myconfig);
1267 my $query = qq|SELECT id, description
1270 my $sth = $dbh->prepare($query);
1271 $sth->execute || $form->dberror($query);
1273 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1274 push @{ $form->{all_warehouses} }, $ref;
1280 $main::lxdebug->leave_sub();
1283 sub save_inventory {
1284 $main::lxdebug->enter_sub();
1286 my ($self, $myconfig, $form) = @_;
1288 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1292 ($null, $employee_id) = split /--/, $form->{employee};
1294 my $ml = ($form->{type} eq 'ship_order') ? -1 : 1;
1296 my $dbh = $form->dbconnect_noauto($myconfig);
1302 $query = qq|SELECT o.serialnumber, o.ship
1304 WHERE o.trans_id = ?
1307 $sth = $dbh->prepare($query) || $form->dberror($query);
1309 $query = qq|SELECT sum(i.qty)
1311 WHERE i.parts_id = ?
1312 AND i.warehouse_id = ?|;
1313 $wth = $dbh->prepare($query) || $form->dberror($query);
1315 for my $i (1 .. $form->{rowcount} - 1) {
1318 (abs($form->{"ship_$i"}) > abs($form->{"qty_$i"}))
1320 : $form->{"ship_$i"};
1322 if ($warehouse_id && $form->{type} eq 'ship_order') {
1324 $wth->execute($form->{"id_$i"}, $warehouse_id) || $form->dberror;
1326 ($qty) = $wth->fetchrow_array;
1337 $query = qq|INSERT INTO inventory (parts_id, warehouse_id,
1338 qty, oe_id, orderitems_id, shippingdate, employee_id)
1339 VALUES ($form->{"id_$i"}, $warehouse_id,
1340 $ship, $form->{"id"},
1341 $form->{"orderitems_id_$i"}, '$form->{shippingdate}',
1343 $dbh->do($query) || $form->dberror($query);
1345 # add serialnumber, ship to orderitems
1346 $sth->execute($form->{id}, $form->{"orderitems_id_$i"})
1348 ($serialnumber, $ship) = $sth->fetchrow_array;
1351 $serialnumber .= " " if $serialnumber;
1352 $serialnumber .= qq|$form->{"serialnumber_$i"}|;
1353 $ship += $form->{"ship_$i"};
1355 $query = qq|UPDATE orderitems SET
1356 serialnumber = '$serialnumber',
1358 WHERE trans_id = $form->{id}
1359 AND id = $form->{"orderitems_id_$i"}|;
1360 $dbh->do($query) || $form->dberror($query);
1362 # update order with ship via
1363 $query = qq|UPDATE oe SET
1364 shippingpoint = '$form->{shippingpoint}',
1365 shipvia = '$form->{shipvia}'
1366 WHERE id = $form->{id}|;
1367 $dbh->do($query) || $form->dberror($query);
1369 # update onhand for parts
1370 $form->update_balance($dbh, "parts", "onhand",
1371 qq|id = $form->{"id_$i"}|,
1372 $form->{"ship_$i"} * $ml);
1377 my $rc = $dbh->commit;
1380 $main::lxdebug->leave_sub();
1386 $main::lxdebug->enter_sub();
1388 my ($dbh, $form, $ml) = @_;
1390 my $service_units = $form->{service_units};
1391 my $part_units = $form->{part_units};
1393 my $query = qq|SELECT oi.parts_id, oi.ship, oi.unit, p.inventory_accno_id, p.assembly
1395 JOIN parts p ON (p.id = oi.parts_id)
1396 WHERE oi.trans_id = $form->{id}|;
1397 my $sth = $dbh->prepare($query);
1398 $sth->execute || $form->dberror($query);
1400 $query = qq|SELECT sum(p.inventory_accno_id)
1402 JOIN assembly a ON (a.parts_id = p.id)
1404 my $ath = $dbh->prepare($query) || $form->dberror($query);
1408 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1409 #print(STDERR "Bin in Schleife $ref->{inventory_accno_id}\n");
1411 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1413 # do not update if assembly consists of all services
1414 if ($ref->{assembly}) {
1415 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1417 ($ispa) = $sth->fetchrow_array;
1425 $query = qq|SELECT p.unit
1427 WHERE p.id = $ref->{parts_id}|;
1428 my $stw = $dbh->prepare($query);
1429 $stw->execute || $form->dberror($query);
1431 my ($item_unit) = $stw->fetchrow_array();
1434 if ($ref->{inventory_accno_id}) {
1435 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
1436 $basefactor = $part_units->{$ref->{unit}}->{factor} / $part_units->{$item_unit}->{factor};
1440 $baseqty = $ref->{ship} * $basefactor;
1442 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
1443 $basefactor = $service_units->{$ref->{unit}}->{factor} / $part_units->{$item_unit}->{factor};
1447 $baseqty = $ref->{ship} * $basefactor;
1449 #print(STDERR "$baseqty Basismenge\n");
1451 # adjust onhand in parts table
1452 $form->update_balance($dbh, "parts", "onhand",
1453 qq|id = $ref->{parts_id}|,
1460 $main::lxdebug->leave_sub();
1464 $main::lxdebug->enter_sub();
1466 my ($dbh, $myconfig, $form) = @_;
1468 my %oid = ('Pg' => 'oid',
1469 'Oracle' => 'rowid');
1471 # increase/reduce qty in inventory table
1472 my $query = qq|SELECT oi.id, oi.parts_id, oi.ship
1474 WHERE oi.trans_id = $form->{id}|;
1475 my $sth = $dbh->prepare($query);
1476 $sth->execute || $form->dberror($query);
1478 $query = qq|SELECT $oid{$myconfig->{dbdriver}} AS oid, qty,
1479 (SELECT SUM(qty) FROM inventory
1480 WHERE oe_id = $form->{id}
1481 AND orderitems_id = ?) AS total
1483 WHERE oe_id = $form->{id}
1484 AND orderitems_id = ?|;
1485 my $ith = $dbh->prepare($query) || $form->dberror($query);
1488 my $ml = ($form->{type} =~ /(ship|sales)_order/) ? -1 : 1;
1490 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1492 $ith->execute($ref->{id}, $ref->{id}) || $form->dberror($query);
1494 while (my $inv = $ith->fetchrow_hashref(NAME_lc)) {
1496 if (($qty = (($inv->{total} * $ml) - $ref->{ship})) >= 0) {
1497 $qty = $inv->{qty} if ($qty > ($inv->{qty} * $ml));
1499 $form->update_balance($dbh, "inventory", "qty",
1500 qq|$oid{$myconfig->{dbdriver}} = $inv->{oid}|,
1509 # delete inventory entries if qty = 0
1510 $query = qq|DELETE FROM inventory
1511 WHERE oe_id = $form->{id}
1513 $dbh->do($query) || $form->dberror($query);
1515 $main::lxdebug->leave_sub();
1519 $main::lxdebug->enter_sub();
1521 my ($self, $myconfig, $form) = @_;
1523 my ($null, $warehouse_id) = split /--/, $form->{warehouse};
1526 my $dbh = $form->dbconnect($myconfig);
1528 my $query = qq|SELECT p.id, p.partnumber, p.description, p.onhand,
1531 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1532 WHERE p.onhand > 0|;
1534 if ($form->{partnumber}) {
1535 $var = $form->like(lc $form->{partnumber});
1537 AND lower(p.partnumber) LIKE '$var'";
1539 if ($form->{description}) {
1540 $var = $form->like(lc $form->{description});
1542 AND lower(p.description) LIKE '$var'";
1544 if ($form->{partsgroup}) {
1545 $var = $form->like(lc $form->{partsgroup});
1547 AND lower(pg.partsgroup) LIKE '$var'";
1550 $sth = $dbh->prepare($query);
1551 $sth->execute || $form->dberror($query);
1553 $query = qq|SELECT sum(i.qty), w.description, w.id
1555 LEFT JOIN warehouse w ON (w.id = i.warehouse_id)
1556 WHERE i.parts_id = ?
1557 AND NOT i.warehouse_id = $warehouse_id
1558 GROUP BY w.description, w.id|;
1559 $wth = $dbh->prepare($query) || $form->dberror($query);
1561 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
1563 $wth->execute($ref->{id}) || $form->dberror;
1565 while (($qty, $warehouse, $warehouse_id) = $wth->fetchrow_array) {
1566 push @{ $form->{all_inventory} },
1567 { 'id' => $ref->{id},
1568 'partnumber' => $ref->{partnumber},
1569 'description' => $ref->{description},
1570 'partsgroup' => $ref->{partsgroup},
1572 'warehouse_id' => $warehouse_id,
1573 'warehouse' => $warehouse }
1583 @{ $form->{all_inventory} } =
1584 sort { $a->{ $form->{sort} } cmp $b->{ $form->{sort} } }
1585 @{ $form->{all_inventory} };
1587 $main::lxdebug->leave_sub();
1589 return @{ $form->{all_inventory} };
1593 $main::lxdebug->enter_sub();
1595 my ($self, $myconfig, $form) = @_;
1597 my $dbh = $form->dbconnect_noauto($myconfig);
1599 my $query = qq|INSERT INTO inventory
1600 (warehouse_id, parts_id, qty, shippingdate, employee_id)
1601 VALUES (?, ?, ?, ?, ?)|;
1602 $sth = $dbh->prepare($query) || $form->dberror($query);
1604 $form->get_employee($dbh);
1609 $shippingdate = "$a[5]-$a[4]-$a[3]";
1611 for my $i (1 .. $form->{rowcount}) {
1612 $qty = $form->parse_amount($myconfig, $form->{"transfer_$i"});
1614 $qty = $form->{"qty_$i"} if ($qty > $form->{"qty_$i"});
1619 $sth->execute($form->{warehouse_id}, $form->{"id_$i"}, $qty,
1620 $shippingdate, $form->{employee_id})
1626 $sth->execute($form->{"warehouse_id_$i"},
1627 $form->{"id_$i"}, $qty * -1, $shippingdate,
1628 $form->{employee_id})
1635 my $rc = $dbh->commit;
1638 $main::lxdebug->leave_sub();
1644 $main::lxdebug->enter_sub();
1646 my ($myconfig, $form) = @_;
1649 $path = "webdav/angebote/" . $form->{quonumber}, last SWITCH
1650 if ($form->{type} eq "sales_quotation");
1651 $path = "webdav/bestellungen/" . $form->{ordnumber}, last SWITCH
1652 if ($form->{type} eq "sales_order");
1653 $path = "webdav/anfragen/" . $form->{quonumber}, last SWITCH
1654 if ($form->{type} eq "request_quotation");
1655 $path = "webdav/lieferantenbestellungen/" . $form->{ordnumber}, last SWITCH
1656 if ($form->{type} eq "purchase_order");
1660 mkdir($path, 0770) or die "can't make directory $!\n";
1664 foreach $file (@files) {
1665 $file =~ /\/([^\/]*)$/;
1667 $ENV{'SCRIPT_NAME'} =~ /\/([^\/]*)\//;
1669 $link = "http://" . $ENV{'SERVER_NAME'} . "/" . $lxerp . "/" . $file;
1670 $form->{WEBDAV}{$fname} = $link;
1675 $main::lxdebug->leave_sub();