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 #======================================================================
42 $main::lxdebug->enter_sub();
44 my ($self, $myconfig, $form) = @_;
47 my $dbh = $form->dbconnect($myconfig);
50 my $ordnumber = 'ordnumber';
52 my ($null, $department_id) = split /--/, $form->{department};
54 my $department = " AND o.department_id = $department_id" if $department_id;
57 if ($form->{"project_id"}) {
59 qq|AND ((globalproject_id = ?) OR EXISTS | .
60 qq| (SELECT * FROM orderitems oi | .
61 qq| WHERE oi.project_id = ? AND oi.trans_id = o.id))|;
62 push(@values, $form->{"project_id"}, $form->{"project_id"});
65 my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
67 if ($form->{type} =~ /_quotation$/) {
69 $ordnumber = 'quonumber';
72 my $number = $form->like(lc $form->{$ordnumber});
73 my $name = $form->like(lc $form->{ $form->{vc} });
75 my $query = qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate,
76 o.amount, ct.name, o.netamount, o.$form->{vc}_id,
77 o.globalproject_id, pr.projectnumber AS globalprojectnumber,
78 ex.$rate AS exchangerate,
79 o.closed, o.delivered, o.quonumber, o.shippingpoint, o.shipvia,
82 JOIN $form->{vc} ct ON (o.$form->{vc}_id = ct.id)
83 LEFT JOIN employee e ON (o.employee_id = e.id)
84 LEFT JOIN exchangerate ex ON (ex.curr = o.curr
85 AND ex.transdate = o.transdate)
86 LEFT JOIN project pr ON o.globalproject_id = pr.id
87 WHERE o.quotation = '$quotation'
90 if ($form->{"$form->{vc}_id"}) {
91 $query .= qq| AND o.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
93 if ($form->{ $form->{vc} }) {
94 $query .= " AND lower(ct.name) LIKE '$name'";
97 if (!$form->{open} && !$form->{closed}) {
98 $query .= " AND o.id = 0";
99 } elsif (!($form->{open} && $form->{closed})) {
100 $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
103 if (($form->{"notdelivered"} || $form->{"delivered"}) &&
104 ($form->{"notdelivered"} ne $form->{"delivered"})) {
105 $query .= $form->{"delivered"} ?
106 " AND o.delivered " : " AND NOT o.delivered";
109 my $sortorder = join ', ',
110 ("o.id", $form->sort_columns(transdate, $ordnumber, name));
111 $sortorder = $form->{sort} if $form->{sort};
113 $query .= " AND lower($ordnumber) LIKE '$number'" if $form->{$ordnumber};
114 $query .= " AND o.transdate >= '$form->{transdatefrom}'"
115 if $form->{transdatefrom};
116 $query .= " AND o.transdate <= '$form->{transdateto}'"
117 if $form->{transdateto};
118 $query .= " ORDER by $sortorder";
120 my $sth = $dbh->prepare($query);
121 $sth->execute(@values) ||
122 $form->dberror($query . " (" . join(", ", @values) . ")");
125 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
126 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
127 push @{ $form->{OE} }, $ref if $ref->{id} != $id{ $ref->{id} };
128 $id{ $ref->{id} } = $ref->{id};
134 $main::lxdebug->leave_sub();
138 $main::lxdebug->enter_sub();
140 my ($self, $myconfig, $form) = @_;
142 # connect to database, turn off autocommit
143 my $dbh = $form->dbconnect_noauto($myconfig);
145 my ($query, $sth, $null);
146 my $exchangerate = 0;
148 my $service_units = AM->retrieve_units($myconfig,$form,"service");
149 my $part_units = AM->retrieve_units($myconfig,$form,"dimension");
150 $form->{service_units} =$service_units;
151 $form->{part_units} =$part_units;
153 ($null, $form->{employee_id}) = split /--/, $form->{employee};
154 unless ($form->{employee_id}) {
155 $form->get_employee($dbh);
158 $form->{payment_id} *= 1;
159 $form->{language_id} *= 1;
160 $form->{shipto_id} *= 1;
161 $form->{delivery_customer_id} *= 1;
162 $form->{delivery_vendor_id} *= 1;
164 my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
168 &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
170 $query = qq|DELETE FROM orderitems
171 WHERE trans_id = $form->{id}|;
172 $dbh->do($query) || $form->dberror($query);
174 $query = qq|DELETE FROM shipto
175 WHERE trans_id = $form->{id} AND module = 'OE'|;
176 $dbh->do($query) || $form->dberror($query);
180 my $uid = rand() . time;
182 $uid .= $form->{login};
184 $uid = substr($uid, 2, 75);
186 $query = qq|INSERT INTO oe (ordnumber, employee_id)
187 VALUES ('$uid', $form->{employee_id})|;
188 $dbh->do($query) || $form->dberror($query);
190 $query = qq|SELECT o.id FROM oe o
191 WHERE o.ordnumber = '$uid'|;
192 $sth = $dbh->prepare($query);
193 $sth->execute || $form->dberror($query);
195 ($form->{id}) = $sth->fetchrow_array;
199 map { $form->{$_} =~ s/\'/\'\'/g }
200 qw(ordnumber quonumber shippingpoint shipvia notes intnotes message);
215 for my $i (1 .. $form->{rowcount}) {
219 $form->parse_amount($myconfig, $form->{"${_}_$i"})
222 if ($form->{"id_$i"}) {
225 $query = qq|SELECT p.unit
227 WHERE p.id = $form->{"id_$i"}|;
228 $sth = $dbh->prepare($query);
229 $sth->execute || $form->dberror($query);
231 my ($item_unit) = $sth->fetchrow_array();
234 if ($form->{"inventory_accno_$i"}) {
235 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
236 $basefactor = $part_units->{$form->{"unit_$i"}}->{factor} / $part_units->{$item_unit}->{factor};
240 $baseqty = $form->{"qty_$i"} * $basefactor;
242 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
243 $basefactor = $service_units->{$form->{"unit_$i"}}->{factor} / $service_units->{$item_unit}->{factor};
247 $baseqty = $form->{"qty_$i"} * $basefactor;
250 map { $form->{"${_}_$i"} =~ s/\'/\'\'/g }
251 qw(partnumber description unit);
253 # set values to 0 if nothing entered
254 $form->{"discount_$i"} =
255 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
257 $form->{"sellprice_$i"} =
258 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
259 $fxsellprice = $form->{"sellprice_$i"};
261 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
263 my $decimalplaces = ($dec > 2) ? $dec : 2;
266 $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
268 $form->{"sellprice_$i"} =
269 $form->round_amount($form->{"sellprice_$i"} - $discount,
272 $form->{"inventory_accno_$i"} *= 1;
273 $form->{"expense_accno_$i"} *= 1;
276 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
278 @taxaccounts = split / /, $form->{"taxaccounts_$i"};
282 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
284 if ($form->{taxincluded}) {
285 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
286 $taxbase = $linetotal - $taxamount;
288 # we are not keeping a natural price, do not round
289 $form->{"sellprice_$i"} =
290 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
292 $taxamount = $linetotal * $taxrate;
293 $taxbase = $linetotal;
296 if ($form->round_amount($taxrate, 7) == 0) {
297 if ($form->{taxincluded}) {
298 foreach $item (@taxaccounts) {
300 $form->round_amount($linetotal * $form->{"${item}_rate"} /
301 (1 + abs($form->{"${item}_rate"})),
304 $taxaccounts{$item} += $taxamount;
305 $taxdiff += $taxamount;
307 $taxbase{$item} += $taxbase;
309 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
311 foreach $item (@taxaccounts) {
312 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
313 $taxbase{$item} += $taxbase;
317 foreach $item (@taxaccounts) {
318 $taxaccounts{$item} +=
319 $taxamount * $form->{"${item}_rate"} / $taxrate;
320 $taxbase{$item} += $taxbase;
324 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
327 ($form->{"reqdate_$i"}) ? qq|'$form->{"reqdate_$i"}'| : "NULL";
329 # get pricegroup_id and save ist
330 ($null, my $pricegroup_id) = split /--/, $form->{"sellprice_pg_$i"};
332 $subtotal = $form->{"subtotal_$i"} * 1;
334 # save detail record in orderitems table
335 $query = qq|INSERT INTO orderitems (|;
336 $query .= "id, " if $form->{"orderitems_id_$i"};
337 $query .= qq|trans_id, parts_id, description, longdescription, qty, base_qty, sellprice, discount,
338 unit, reqdate, project_id, serialnumber, ship, pricegroup_id,
339 ordnumber, transdate, cusordnumber, subtotal)
341 $query .= qq|$form->{"orderitems_id_$i"},|
342 if $form->{"orderitems_id_$i"};
343 $query .= qq|$form->{id}, $form->{"id_$i"},
344 '$form->{"description_$i"}', '$form->{"longdescription_$i"}', $form->{"qty_$i"}, $baseqty,
345 $fxsellprice, $form->{"discount_$i"},
346 '$form->{"unit_$i"}', $reqdate, | . conv_i($form->{"project_id_$i"}, 'NULL') . qq|,
347 '$form->{"serialnumber_$i"}', $form->{"ship_$i"}, '$pricegroup_id',
348 '$form->{"ordnumber_$i"}', '$form->{"transdate_$i"}', '$form->{"cusordnumber_$i"}', '$subtotal')|;
349 $dbh->do($query) || $form->dberror($query);
351 $form->{"sellprice_$i"} = $fxsellprice;
352 $form->{"discount_$i"} *= 100;
356 # set values which could be empty
357 map { $form->{$_} *= 1 }
358 qw(vendor_id customer_id taxincluded closed quotation);
360 $reqdate = ($form->{reqdate}) ? qq|'$form->{reqdate}'| : "NULL";
364 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
366 $amount = $form->round_amount($netamount + $tax, 2);
367 $netamount = $form->round_amount($netamount, 2);
369 if ($form->{currency} eq $form->{defaultcurrency}) {
370 $form->{exchangerate} = 1;
373 $form->check_exchangerate($myconfig,
376 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
379 $form->{exchangerate} =
382 : $form->parse_amount($myconfig, $form->{exchangerate});
386 # fill in subject if there is none
387 if ($form->{type} =~ /_order$/) {
389 $form->{subject} = qq|$form->{label} $form->{ordnumber}|
390 unless $form->{subject};
393 $form->{subject} = qq|$form->{label} $form->{quonumber}|
394 unless $form->{subject};
397 # if there is a message stuff it into the intnotes
398 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
399 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
400 my $now = scalar localtime;
401 $form->{intnotes} .= qq|\r
402 \r| if $form->{intnotes};
404 $form->{intnotes} .= qq|[email]\r
407 $cc${bcc}Subject: $form->{subject}\r
409 Message: $form->{message}\r| if $form->{message};
411 ($null, $form->{department_id}) = split(/--/, $form->{department});
412 $form->{department_id} *= 1;
413 $form->{payment_id} *= 1;
414 $form->{language_id} *= 1;
415 $form->{taxzone_id} *= 1;
416 $form->{proforma} *= 1;
421 $query = qq|UPDATE oe set
422 ordnumber = '$form->{ordnumber}',
423 quonumber = '$form->{quonumber}',
424 cusordnumber = '$form->{cusordnumber}',
425 transdate = '$form->{transdate}',
426 vendor_id = $form->{vendor_id},
427 customer_id = $form->{customer_id},
429 netamount = $netamount,
431 taxincluded = '$form->{taxincluded}',
432 shippingpoint = '$form->{shippingpoint}',
433 shipvia = '$form->{shipvia}',
434 notes = '$form->{notes}',
435 intnotes = '$form->{intnotes}',
436 curr = '$form->{currency}',
437 closed = '$form->{closed}',
438 delivered = '| . ($form->{delivered} ? "t" : "f") . qq|',
439 proforma = '$form->{proforma}',
440 quotation = '$quotation',
441 department_id = $form->{department_id},
442 language_id = $form->{language_id},
443 taxzone_id = $form->{taxzone_id},
444 shipto_id = $form->{shipto_id},
445 payment_id = $form->{payment_id},
446 delivery_vendor_id = $form->{delivery_vendor_id},
447 delivery_customer_id = $form->{delivery_customer_id},
448 globalproject_id = | . conv_i($form->{"globalproject_id"}, 'NULL') . qq|,
449 employee_id = $form->{employee_id},
450 salesman_id = | . conv_i($form->{salesman_id}, 'NULL') . qq|,
451 cp_id = | . conv_i($form->{cp_id}, 'NULL') . qq|
452 WHERE id = $form->{id}|;
453 $dbh->do($query) || $form->dberror($query);
455 $form->{ordtotal} = $amount;
458 $form->{name} = $form->{ $form->{vc} };
459 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
461 if (!$form->{shipto_id}) {
462 $form->add_shipto($dbh, $form->{id}, "OE");
465 # save printed, emailed, queued
466 $form->save_status($dbh);
468 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
469 if ($form->{vc} eq 'customer') {
470 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
471 $form->{exchangerate}, 0);
473 if ($form->{vc} eq 'vendor') {
474 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
475 0, $form->{exchangerate});
479 if ($form->{type} =~ /_order$/) {
482 &adj_onhand($dbh, $form, $ml * -1);
485 my $rc = $dbh->commit;
488 $form->{saved_xyznumber} = $form->{$form->{type} =~ /_quotation$/ ?
489 "quonumber" : "ordnumber"};
491 Common::webdav_folder($form) if ($main::webdav);
493 $main::lxdebug->leave_sub();
498 # this function closes multiple orders given in $form->{ordnumber_#}.
499 # use this for multiple orders that don't have to be saved back
500 # single orders should use OE::save instead.
502 $main::lxdebug->enter_sub();
504 my ($self, $myconfig, $form) = @_;
507 map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} }
508 (1 .. $form->{rowcount});
510 my $dbh = $form->dbconnect($myconfig);
511 $query = qq|UPDATE oe SET
513 WHERE ordnumber IN (|
514 . join(', ', map { $dbh->quote($_) } @ids) . qq|)|;
515 $dbh->do($query) || $form->dberror($query);
518 $main::lxdebug->leave_sub();
522 $main::lxdebug->enter_sub();
524 my ($self, $myconfig, $form) = @_;
526 $main::lxdebug->leave_sub() unless ($form->{"id"});
528 my $dbh = $form->dbconnect($myconfig);
529 do_query($form, $dbh, qq|UPDATE oe SET closed = TRUE where id = ?|,
533 $main::lxdebug->leave_sub();
537 $main::lxdebug->enter_sub();
539 my ($self, $myconfig, $form, $spool) = @_;
541 # connect to database
542 my $dbh = $form->dbconnect_noauto($myconfig);
545 my $query = qq|SELECT s.spoolfile FROM status s
546 WHERE s.trans_id = $form->{id}|;
547 $sth = $dbh->prepare($query);
548 $sth->execute || $self->dberror($query);
553 while (($spoolfile) = $sth->fetchrow_array) {
554 push @spoolfiles, $spoolfile;
558 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o
559 WHERE o.trans_id = $form->{id}|;
560 $sth = $dbh->prepare($query);
561 $sth->execute || $self->dberror($query);
563 while (my ($id, $ship) = $sth->fetchrow_array) {
564 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
569 $query = qq|DELETE FROM inventory
570 WHERE oe_id = $form->{id}|;
571 $dbh->do($query) || $form->dberror($query);
573 # delete status entries
574 $query = qq|DELETE FROM status
575 WHERE trans_id = $form->{id}|;
576 $dbh->do($query) || $form->dberror($query);
579 $query = qq|DELETE FROM oe
580 WHERE id = $form->{id}|;
581 $dbh->do($query) || $form->dberror($query);
583 # delete individual entries
584 $query = qq|DELETE FROM orderitems
585 WHERE trans_id = $form->{id}|;
586 $dbh->do($query) || $form->dberror($query);
588 $query = qq|DELETE FROM shipto
589 WHERE trans_id = $form->{id} AND module = 'OE'|;
590 $dbh->do($query) || $form->dberror($query);
592 my $rc = $dbh->commit;
596 foreach $spoolfile (@spoolfiles) {
597 unlink "$spool/$spoolfile" if $spoolfile;
601 $main::lxdebug->leave_sub();
607 $main::lxdebug->enter_sub();
609 my ($self, $myconfig, $form) = @_;
611 # connect to database
612 my $dbh = $form->dbconnect_noauto($myconfig);
616 # translate the ids (given by id_# and trans_id_#) into one array of ids, so we can join them later
618 push @ids, $form->{"trans_id_$_"}
619 if ($form->{"id_$_"} and $form->{"trans_id_$_"})
620 } (1 .. $form->{"rowcount"});
622 # if called in multi id mode, and still only got one id, switch back to single id
623 if ($form->{"rowcount"} and $#ids == 0) {
624 $form->{"id"} = $ids[0];
630 # get default accounts and last order number
631 $query = qq|SELECT (SELECT c.accno FROM chart c
632 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
633 (SELECT c.accno FROM chart c
634 WHERE d.income_accno_id = c.id) AS income_accno,
635 (SELECT c.accno FROM chart c
636 WHERE d.expense_accno_id = c.id) AS expense_accno,
637 (SELECT c.accno FROM chart c
638 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
639 (SELECT c.accno FROM chart c
640 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
644 $query = qq|SELECT (SELECT c.accno FROM chart c
645 WHERE d.inventory_accno_id = c.id) AS inventory_accno,
646 (SELECT c.accno FROM chart c
647 WHERE d.income_accno_id = c.id) AS income_accno,
648 (SELECT c.accno FROM chart c
649 WHERE d.expense_accno_id = c.id) AS expense_accno,
650 (SELECT c.accno FROM chart c
651 WHERE d.fxgain_accno_id = c.id) AS fxgain_accno,
652 (SELECT c.accno FROM chart c
653 WHERE d.fxloss_accno_id = c.id) AS fxloss_accno,
654 d.curr AS currencies,
655 current_date AS transdate, current_date AS reqdate
658 my $sth = $dbh->prepare($query);
659 $sth->execute || $form->dberror($query);
661 my $ref = $sth->fetchrow_hashref(NAME_lc);
662 map { $form->{$_} = $ref->{$_} } keys %$ref;
665 ($form->{currency}) = split /:/, $form->{currencies};
667 # set reqdate if this is an invoice->order conversion. If someone knows a better check to ensure
668 # we come from invoices, feel free.
669 $form->{reqdate} = $form->{deliverydate}
670 if ( $form->{deliverydate}
671 and $form->{callback} =~ /action=ar_transactions/);
673 if ($form->{id} or @ids) {
675 # retrieve order for single id
676 # NOTE: this query is intended to fetch all information only ONCE.
677 # so if any of these infos is important (or even different) for any item,
678 # it will be killed out and then has to be fetched from the item scope query further down
679 $query = qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate,
680 o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes,
681 o.curr AS currency, e.name AS employee, o.employee_id, o.salesman_id,
682 o.$form->{vc}_id, cv.name AS $form->{vc}, o.amount AS invtotal,
683 o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber,
684 d.description AS department, o.payment_id, o.language_id, o.taxzone_id,
685 o.delivery_customer_id, o.delivery_vendor_id, o.proforma, o.shipto_id,
689 JOIN $form->{vc} cv ON (o.$form->{vc}_id = cv.id)
690 LEFT JOIN employee e ON (o.employee_id = e.id)
691 LEFT JOIN department d ON (o.department_id = d.id)
694 ? qq|WHERE o.id = $form->{id}|
695 : qq|WHERE o.id IN (| . join(', ', @ids) . qq|)|);
697 #$main::lxdebug->message(0, $query);
699 $sth = $dbh->prepare($query);
700 $sth->execute || $form->dberror($query);
702 $ref = $sth->fetchrow_hashref(NAME_lc);
703 map { $form->{$_} = $ref->{$_} } keys %$ref;
705 $form->{saved_xyznumber} = $form->{$form->{type} =~ /_quotation$/ ?
706 "quonumber" : "ordnumber"};
708 # set all entries for multiple ids blank that yield different information
709 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
710 map { $form->{$_} = '' if ($ref->{$_} ne $form->{$_}) } keys %$ref;
713 # if not given, fill transdate with current_date
714 $form->{transdate} = $form->current_date($myconfig)
715 unless $form->{transdate};
719 if ($form->{delivery_customer_id}) {
720 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_customer_id}|;
721 $sth = $dbh->prepare($query);
722 $sth->execute || $form->dberror($query);
723 ($form->{delivery_customer_string}) = $sth->fetchrow_array();
727 if ($form->{delivery_vendor_id}) {
728 $query = qq|SELECT name FROM customer WHERE id=$form->{delivery_vendor_id}|;
729 $sth = $dbh->prepare($query);
730 $sth->execute || $form->dberror($query);
731 ($form->{delivery_vendor_string}) = $sth->fetchrow_array();
735 # shipto and pinted/mailed/queued status makes only sense for single id retrieve
737 $query = qq|SELECT s.* FROM shipto s
738 WHERE s.trans_id = $form->{id} AND s.module = 'OE'|;
739 $sth = $dbh->prepare($query);
740 $sth->execute || $form->dberror($query);
742 $ref = $sth->fetchrow_hashref(NAME_lc);
744 map { $form->{$_} = $ref->{$_} } keys %$ref;
747 # get printed, emailed and queued
748 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname
750 WHERE s.trans_id = $form->{id}|;
751 $sth = $dbh->prepare($query);
752 $sth->execute || $form->dberror($query);
754 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
755 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
756 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
757 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
758 if $ref->{spoolfile};
761 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
764 my %oid = ('Pg' => 'oid',
765 'Oracle' => 'rowid');
768 $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
770 if(!$form->{taxzone_id}) {
771 $form->{taxzone_id} = 0;
773 # retrieve individual items
774 # this query looks up all information about the items
775 # stuff different from the whole will not be overwritten, but saved with a suffix.
776 $query = qq|SELECT o.id AS orderitems_id,
777 c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid,
778 c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid,
779 c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid,
780 oe.ordnumber AS ordnumber_oe, oe.transdate AS transdate_oe, oe.cusordnumber AS cusordnumber_oe,
781 p.partnumber, p.assembly, o.description, o.qty,
782 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,
783 o.reqdate, o.project_id, o.serialnumber, o.ship,
784 o.ordnumber, o.transdate, o.cusordnumber, o.subtotal, o.longdescription,
785 pr.projectnumber, p.formel,
786 pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup
788 JOIN parts p ON (o.parts_id = p.id)
789 JOIN oe ON (o.trans_id = oe.id)
790 LEFT JOIN chart c1 ON ((select inventory_accno_id from buchungsgruppen where id=p.buchungsgruppen_id) = c1.id)
791 LEFT JOIN chart c2 ON ((select income_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c2.id)
792 LEFT JOIN chart c3 ON ((select expense_accno_id_$form->{taxzone_id} from buchungsgruppen where id=p.buchungsgruppen_id) = c3.id)
793 LEFT JOIN project pr ON (o.project_id = pr.id)
794 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
797 ? qq|WHERE o.trans_id = $form->{id}|
798 : qq|WHERE o.trans_id IN (| . join(", ", @ids) . qq|)|)
800 ORDER BY o.$oid{$myconfig->{dbdriver}}|;
802 $sth = $dbh->prepare($query);
803 $sth->execute || $form->dberror($query);
805 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
806 if (!$ref->{"part_inventory_accno_id"}) {
807 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
809 delete($ref->{"part_inventory_accno_id"});
811 # in collective order, copy global ordnumber, transdate, cusordnumber into item scope
812 # unless already present there
813 # remove _oe entries afterwards
814 map { $ref->{$_} = $ref->{"${_}_oe"} if ($ref->{$_} eq '') }
815 qw|ordnumber transdate cusordnumber|
817 map { delete $ref->{$_} } qw|ordnumber_oe transdate_oe cusordnumber_oe|;
821 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >=0)) {
822 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}|;
823 my $stw = $dbh->prepare($query);
824 $stw->execute || $form->dberror($query);
825 ($ref->{inventory_accno}, $ref->{inventory_new_chart}, $ref->{inventory_valid}) = $stw->fetchrow_array;
829 while ($ref->{income_new_chart} && ($ref->{income_valid} >=0)) {
830 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}|;
831 my $stw = $dbh->prepare($query);
832 $stw->execute || $form->dberror($query);
833 ($ref->{income_accno}, $ref->{income_new_chart}, $ref->{income_valid}) = $stw->fetchrow_array;
837 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >=0)) {
838 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}|;
839 my $stw = $dbh->prepare($query);
840 $stw->execute || $form->dberror($query);
841 ($ref->{expense_accno}, $ref->{expense_new_chart}, $ref->{expense_valid}) = $stw->fetchrow_array;
845 # delete orderitems_id in collective orders, so that they get cloned no matter what
846 delete $ref->{orderitems_id} if (@ids);
848 # get tax rates and description
850 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
851 $query = qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber
852 FROM tax t LEFT JOIN chart c on (c.id=t.chart_id)
853 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)
855 $stw = $dbh->prepare($query);
856 $stw->execute || $form->dberror($query);
857 $ref->{taxaccounts} = "";
859 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
861 # if ($customertax{$ref->{accno}}) {
862 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
866 $ref->{taxaccounts} .= "$ptr->{accno} ";
867 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
868 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
869 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
870 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
871 $form->{taxaccounts} .= "$ptr->{accno} ";
876 chop $ref->{taxaccounts};
877 push @{ $form->{form_details} }, $ref;
885 $form->lastname_used($dbh, $myconfig, $form->{vc})
886 unless $form->{"$form->{vc}_id"};
890 $form->{exchangerate} =
891 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
892 ($form->{vc} eq 'customer') ? "buy" : "sell");
894 Common::webdav_folder($form) if ($main::webdav);
897 $query = qq|SELECT id, description
899 $sth = $dbh->prepare($query);
900 $sth->execute || $form->dberror($query);
903 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
904 push @{ $form->{TAXZONE} }, $ref;
909 my $rc = $dbh->commit;
912 $main::lxdebug->leave_sub();
918 $main::lxdebug->enter_sub();
920 my ($self, $myconfig, $form) = @_;
922 # connect to database
923 my $dbh = $form->dbconnect($myconfig);
928 my $nodiscount_subtotal = 0;
929 my $discount_subtotal = 0;
935 my $subtotal_header = 0;
938 my %oid = ('Pg' => 'oid',
939 'Oracle' => 'rowid');
941 my (@project_ids, %projectnumbers);
943 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
945 # sort items by partsgroup
946 for $i (1 .. $form->{rowcount}) {
948 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
949 $partsgroup = $form->{"partsgroup_$i"};
951 push @partsgroup, [$i, $partsgroup];
952 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
956 $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
957 join(", ", map({ "?" } @project_ids)) . ")";
958 $sth = $dbh->prepare($query);
959 $sth->execute(@project_ids) ||
960 $form->dberror($query . " (" . join(", ", @project_ids) . ")");
961 while (my $ref = $sth->fetchrow_hashref()) {
962 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
967 $form->{"globalprojectnumber"} =
968 $projectnumbers{$form->{"globalproject_id"}};
971 qw(runningnumber number description longdescription qty ship unit bin
972 partnotes serialnumber reqdate sellprice listprice netprice
973 discount p_discount discount_sub nodiscount_sub
974 linetotal nodiscount_linetotal tax_rate projectnumber);
977 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
980 if ($item->[1] ne $sameitem) {
981 push(@{ $form->{description} }, qq|$item->[1]|);
982 $sameitem = $item->[1];
984 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
987 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
989 if ($form->{"id_$i"} != 0) {
991 # add number, description and qty to $form->{number}, ....
993 if ($form->{"subtotal_$i"} && !$subtotal_header) {
994 $subtotal_header = $i;
995 $position = int($position);
998 } elsif ($subtotal_header) {
1000 $position = int($position);
1001 $position = $position.".".$subposition;
1003 $position = int($position);
1007 push(@{ $form->{runningnumber} }, $i);
1008 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
1009 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
1010 push(@{ $form->{longdescription} }, qq|$form->{"longdescription_$i"}|);
1011 push(@{ $form->{qty} },
1012 $form->format_amount($myconfig, $form->{"qty_$i"}));
1013 push(@{ $form->{ship} },
1014 $form->format_amount($myconfig, $form->{"ship_$i"}));
1015 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
1016 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
1017 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
1018 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
1019 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
1021 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
1023 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
1025 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
1026 my ($dec) = ($sellprice =~ /\.(\d+)/);
1028 my $decimalplaces = ($dec > 2) ? $dec : 2;
1031 $form->round_amount(
1032 $sellprice * $form->parse_amount($myconfig,
1033 $form->{"discount_$i"}) / 100,
1037 $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
1039 # keep a netprice as well, (sellprice - discount)
1040 #$form->{"netprice_$i"} = $sellprice - $discount;
1041 $form->{"netprice_$i"} = $sellprice - $i_discount;
1042 my $nodiscount_linetotal =
1043 $form->round_amount($form->{"qty_$i"} * $sellprice, 2);
1045 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
1047 push(@{ $form->{netprice} },
1048 ($form->{"netprice_$i"} != 0)
1049 ? $form->format_amount(
1050 $myconfig, $form->{"netprice_$i"},
1057 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
1059 $linetotal = ($linetotal != 0) ? $linetotal : " ";
1061 push(@{ $form->{discount} }, $discount);
1062 push(@{ $form->{p_discount} }, $form->{"discount_$i"});
1064 $form->{ordtotal} += $linetotal;
1065 $discount_subtotal += $linetotal;
1066 $form->{nodiscount_total} += $nodiscount_linetotal;
1067 $nodiscount_subtotal += $nodiscount_linetotal;
1068 $form->{discount_total} += $form->parse_amount($myconfig, $discount);
1070 if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
1071 $discount_subtotal = $form->format_amount($myconfig, $discount_subtotal, 2);
1072 push(@{ $form->{discount_sub} }, $discount_subtotal);
1073 $nodiscount_subtotal = $form->format_amount($myconfig, $nodiscount_subtotal, 2);
1074 push(@{ $form->{nodiscount_sub} }, $nodiscount_subtotal);
1075 $discount_subtotal = 0;
1076 $nodiscount_subtotal = 0;
1077 $subtotal_header = 0;
1079 push(@{ $form->{discount_sub} }, "");
1080 push(@{ $form->{nodiscount_sub} }, "");
1083 if ($linetotal == $netto_linetotal) {
1084 $nodiscount += $linetotal;
1086 push(@{ $form->{linetotal} },
1087 $form->format_amount($myconfig, $linetotal, 2));
1088 push(@{ $form->{nodiscount_linetotal} },
1089 $form->format_amount($myconfig, $nodiscount_linetotal, 2));
1091 push(@{ $form->{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}});
1093 my ($taxamount, $taxbase);
1096 map { $taxrate += $form->{"${_}_rate"} } split / /,
1097 $form->{"taxaccounts_$i"};
1099 if ($form->{taxincluded}) {
1102 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
1103 $taxbase = $linetotal / (1 + $taxrate);
1105 $taxamount = $linetotal * $taxrate;
1106 $taxbase = $linetotal;
1109 if ($taxamount != 0) {
1110 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
1111 $taxaccounts{$item} +=
1112 $taxamount * $form->{"${item}_rate"} / $taxrate;
1113 $taxbase{$item} += $taxbase;
1117 $tax_rate = $taxrate * 100;
1118 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
1120 if ($form->{"assembly_$i"}) {
1123 # get parts and push them onto the stack
1125 if ($form->{groupitems}) {
1127 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
1129 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
1132 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty,
1135 JOIN parts p ON (a.parts_id = p.id)
1136 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
1138 AND a.id = '$form->{"id_$i"}'
1140 $sth = $dbh->prepare($query);
1141 $sth->execute || $form->dberror($query);
1143 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1144 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
1145 map({ push(@{ $form->{$_} }, "") }
1146 grep({ $_ ne "description" } @arrays));
1147 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
1148 push(@{ $form->{description} }, $sameitem);
1151 push(@{ $form->{description} },
1152 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
1154 . qq|, $ref->{partnumber}, $ref->{description}|);
1156 map({ push(@{ $form->{$_} }, "") }
1157 grep({ $_ ne "description" } @arrays));
1166 foreach $item (sort keys %taxaccounts) {
1167 push(@{ $form->{taxbase} },
1168 $form->format_amount($myconfig, $taxbase{$item}, 2));
1170 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
1172 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
1173 push(@{ $form->{taxdescription} }, $form->{"${item}_description"} . q{ } . 100 * $form->{"${item}_rate"} . q{%});
1174 push(@{ $form->{taxrate} },
1175 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
1176 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
1178 $form->{subtotal} = $form->format_amount($myconfig, $form->{total}, 2);
1179 $yesdiscount = $form->{nodiscount_total} - $nodiscount;
1180 $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
1181 $form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
1182 $form->{nodiscount} = $form->format_amount($myconfig, $nodiscount, 2);
1183 $form->{yesdiscount} = $form->format_amount($myconfig, $yesdiscount, 2);
1185 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1187 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
1190 $form->{quototal} = $form->{ordtotal} =
1191 $form->format_amount($myconfig, $form->{ordtotal}, 2);
1193 if ($form->{type} =~ /_quotation/) {
1194 $form->set_payment_options($myconfig, $form->{quodate});
1196 $form->set_payment_options($myconfig, $form->{orddate});
1199 $form->{username} = $myconfig->{name};
1203 $main::lxdebug->leave_sub();
1206 sub project_description {
1207 $main::lxdebug->enter_sub();
1209 my ($self, $dbh, $id) = @_;
1211 my $query = qq|SELECT p.description
1214 my $sth = $dbh->prepare($query);
1215 $sth->execute || $form->dberror($query);
1217 ($_) = $sth->fetchrow_array;
1221 $main::lxdebug->leave_sub();
1227 $main::lxdebug->enter_sub();
1229 my ($dbh, $form, $ml) = @_;
1231 my $service_units = $form->{service_units};
1232 my $part_units = $form->{part_units};
1234 my $query = qq|SELECT oi.parts_id, oi.ship, oi.unit, p.inventory_accno_id, p.assembly
1236 JOIN parts p ON (p.id = oi.parts_id)
1237 WHERE oi.trans_id = $form->{id}|;
1238 my $sth = $dbh->prepare($query);
1239 $sth->execute || $form->dberror($query);
1241 $query = qq|SELECT sum(p.inventory_accno_id)
1243 JOIN assembly a ON (a.parts_id = p.id)
1245 my $ath = $dbh->prepare($query) || $form->dberror($query);
1249 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1250 #print(STDERR "Bin in Schleife $ref->{inventory_accno_id}\n");
1252 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1254 # do not update if assembly consists of all services
1255 if ($ref->{assembly}) {
1256 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1258 ($ispa) = $sth->fetchrow_array;
1266 $query = qq|SELECT p.unit
1268 WHERE p.id = $ref->{parts_id}|;
1269 my $stw = $dbh->prepare($query);
1270 $stw->execute || $form->dberror($query);
1272 my ($item_unit) = $stw->fetchrow_array();
1275 if ($ref->{inventory_accno_id}) {
1276 if (defined($part_units->{$item_unit}->{factor}) && $part_units->{$item_unit}->{factor} ne '' && $part_units->{$item_unit}->{factor} ne '0') {
1277 $basefactor = $part_units->{$ref->{unit}}->{factor} / $part_units->{$item_unit}->{factor};
1281 $baseqty = $ref->{ship} * $basefactor;
1283 if (defined($service_units->{$item_unit}->{factor}) && $service_units->{$item_unit}->{factor} ne '' && $service_units->{$item_unit}->{factor} ne '0') {
1284 $basefactor = $service_units->{$ref->{unit}}->{factor} / $part_units->{$item_unit}->{factor};
1288 $baseqty = $ref->{ship} * $basefactor;
1290 #print(STDERR "$baseqty Basismenge\n");
1292 # adjust onhand in parts table
1293 $form->update_balance($dbh, "parts", "onhand",
1294 qq|id = $ref->{parts_id}|,
1301 $main::lxdebug->leave_sub();