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';
56 my $rate = ($form->{vc} eq 'customer') ? 'buy' : 'sell';
58 if ($form->{type} =~ /_quotation$/) {
60 $ordnumber = 'quonumber';
63 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
66 qq|SELECT o.id, o.ordnumber, o.transdate, o.reqdate, | .
67 qq| o.amount, ct.name, o.netamount, o.${vc}_id, o.globalproject_id, | .
68 qq| o.closed, o.delivered, o.quonumber, o.shippingpoint, o.shipvia, | .
69 qq| ex.$rate AS exchangerate, | .
70 qq| pr.projectnumber AS globalprojectnumber, | .
71 qq| e.name AS employee | .
73 qq|JOIN $vc ct ON (o.${vc}_id = ct.id) | .
74 qq|LEFT JOIN employee e ON (o.employee_id = e.id) | .
75 qq|LEFT JOIN exchangerate ex ON (ex.curr = o.curr | .
76 qq| AND ex.transdate = o.transdate) | .
77 qq|LEFT JOIN project pr ON (o.globalproject_id = pr.id) | .
78 qq|WHERE (o.quotation = ?) |;
79 push(@values, $quotation);
81 my ($null, $department_id) = split /--/, $form->{department};
83 $query .= qq| AND o.department_id = ?|;
84 push(@values, $department_id);
87 if ($form->{"project_id"}) {
89 qq|AND ((globalproject_id = ?) OR EXISTS | .
90 qq| (SELECT * FROM orderitems oi | .
91 qq| WHERE oi.project_id = ? AND oi.trans_id = o.id))|;
92 push(@values, $form->{"project_id"}, $form->{"project_id"});
95 if ($form->{"${vc}_id"}) {
96 $query .= " AND o.${vc}_id = ?";
97 push(@values, $form->{"${vc}_id"});
99 } elsif ($form->{$vc}) {
100 $query .= " AND ct.name ILIKE ?";
101 push(@values, '%' . $form->{$vc} . '%');
104 if (!$form->{open} && !$form->{closed}) {
105 $query .= " AND o.id = 0";
106 } elsif (!($form->{open} && $form->{closed})) {
107 $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
110 if (($form->{"notdelivered"} || $form->{"delivered"}) &&
111 ($form->{"notdelivered"} ne $form->{"delivered"})) {
112 $query .= $form->{"delivered"} ?
113 " AND o.delivered " : " AND NOT o.delivered";
116 if ($form->{$ordnumber}) {
117 $query .= qq| AND $ordnumber ILIKE ?|;
118 push(@values, '%' . $form->{$ordnumber} . '%');
121 if($form->{transdatefrom}) {
122 $query .= qq| AND o.transdate >= ?|;
123 push(@values, conv_date($form->{transdatefrom}));
126 if($form->{transdateto}) {
127 $query .= qq| AND o.transdate <= ?|;
128 push(@values, conv_date($form->{transdateto}));
131 my $sortorder = join(', ', ("o.id", $form->sort_columns("transdate", $ordnumber, "name")));
132 my %allowed_sort_columns =
133 ("transdate" => "o.transdate",
134 "reqdate" => "o.reqdate",
136 "ordnumber" => "o.ordnumber",
137 "quonumber" => "o.quonumber",
139 "employee" => "e.name",
140 "shipvia" => "o.shipvia");
141 if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
142 $sortorder = $allowed_sort_columns{$form->{sort}};
144 $query .= qq| ORDER by | . $sortorder;
146 my $sth = $dbh->prepare($query);
147 $sth->execute(@values) ||
148 $form->dberror($query . " (" . join(", ", @values) . ")");
152 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
153 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
154 push @{ $form->{OE} }, $ref if $ref->{id} != $id{ $ref->{id} };
155 $id{ $ref->{id} } = $ref->{id};
161 $main::lxdebug->leave_sub();
165 $main::lxdebug->enter_sub();
167 my ($self, $myconfig, $form) = @_;
169 # connect to database, turn off autocommit
170 my $dbh = $form->dbconnect_noauto($myconfig);
172 my ($query, @values, $sth, $null);
173 my $exchangerate = 0;
175 my $all_units = AM->retrieve_units($myconfig, $form);
176 $form->{all_units} = $all_units;
178 ($null, $form->{employee_id}) = split /--/, $form->{employee};
179 unless ($form->{employee_id}) {
180 $form->get_employee($dbh);
183 my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
187 &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
189 $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
190 do_query($form, $dbh, $query, $form->{id});
192 $query = qq|DELETE FROM shipto | .
193 qq|WHERE trans_id = ? AND module = 'OE'|;
194 do_query($form, $dbh, $query, $form->{id});
198 $query = qq|SELECT nextval('id')|;
199 ($form->{id}) = selectrow_query($form, $dbh, $query);
201 $query = qq|INSERT INTO oe (id, ordnumber, employee_id) VALUES (?, '', ?)|;
202 do_query($form, $dbh, $query, $form->{id}, $form->{employee_id});
218 for my $i (1 .. $form->{rowcount}) {
220 map({ $form->{"${_}_$i"} =
221 $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(qty ship));
223 if ($form->{"id_$i"}) {
226 $query = qq|SELECT unit FROM parts WHERE id = ?|;
227 my ($item_unit) = selectrow_query($form, $dbh, $query, $form->{"id_$i"});
230 if (defined($all_units->{$item_unit}->{factor}) &&
231 (($all_units->{$item_unit}->{factor} * 1) != 0)) {
232 $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} /
233 $all_units->{$item_unit}->{factor};
235 my $baseqty = $form->{"qty_$i"} * $basefactor;
237 # set values to 0 if nothing entered
238 $form->{"discount_$i"} =
239 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
241 $form->{"sellprice_$i"} =
242 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
243 $fxsellprice = $form->{"sellprice_$i"};
245 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
247 my $decimalplaces = ($dec > 2) ? $dec : 2;
250 $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
252 $form->{"sellprice_$i"} =
253 $form->round_amount($form->{"sellprice_$i"} - $discount,
256 $form->{"inventory_accno_$i"} *= 1;
257 $form->{"expense_accno_$i"} *= 1;
260 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
262 @taxaccounts = split(/ /, $form->{"taxaccounts_$i"});
266 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
268 if ($form->{taxincluded}) {
269 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
270 $taxbase = $linetotal - $taxamount;
272 # we are not keeping a natural price, do not round
273 $form->{"sellprice_$i"} =
274 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
276 $taxamount = $linetotal * $taxrate;
277 $taxbase = $linetotal;
280 if ($form->round_amount($taxrate, 7) == 0) {
281 if ($form->{taxincluded}) {
282 foreach $item (@taxaccounts) {
284 $form->round_amount($linetotal * $form->{"${item}_rate"} /
285 (1 + abs($form->{"${item}_rate"})),
288 $taxaccounts{$item} += $taxamount;
289 $taxdiff += $taxamount;
291 $taxbase{$item} += $taxbase;
293 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
295 foreach $item (@taxaccounts) {
296 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
297 $taxbase{$item} += $taxbase;
301 foreach $item (@taxaccounts) {
302 $taxaccounts{$item} +=
303 $taxamount * $form->{"${item}_rate"} / $taxrate;
304 $taxbase{$item} += $taxbase;
308 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
311 ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
313 # get pricegroup_id and save ist
314 ($null, my $pricegroup_id) = split(/--/, $form->{"sellprice_pg_$i"});
316 $subtotal = $form->{"subtotal_$i"} * 1;
318 # save detail record in orderitems table
320 $query = qq|INSERT INTO orderitems (|;
321 if ($form->{"orderitems_id_$i"}) {
324 $query .= qq|trans_id, parts_id, description, longdescription, qty, base_qty, | .
325 qq|sellprice, discount, unit, reqdate, project_id, serialnumber, ship, | .
326 qq|pricegroup_id, ordnumber, transdate, cusordnumber, subtotal) | .
328 if($form->{"orderitems_id_$i"}) {
330 push(@values, $form->{"orderitems_id_$i"});
332 $query .= qq|?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
334 conv_i($form->{id}), conv_i($form->{"id_$i"}),
335 $form->{"description_$i"}, $form->{"longdescription_$i"},
336 $form->{"qty_$i"}, $baseqty,
337 $fxsellprice, $form->{"discount_$i"},
338 $form->{"unit_$i"}, conv_date($reqdate), conv_i($form->{"project_id_$i"}),
339 $form->{"serialnumber_$i"}, $form->{"ship_$i"}, conv_i($pricegroup_id),
340 $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
341 $form->{"cusordnumber_$i"}, $subtotal);
342 do_query($form, $dbh, $query, @values);
344 $form->{"sellprice_$i"} = $fxsellprice;
345 $form->{"discount_$i"} *= 100;
349 $reqdate = ($form->{reqdate}) ? $form->{reqdate} : undef;
353 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
355 $amount = $form->round_amount($netamount + $tax, 2);
356 $netamount = $form->round_amount($netamount, 2);
358 if ($form->{currency} eq $form->{defaultcurrency}) {
359 $form->{exchangerate} = 1;
362 $form->check_exchangerate($myconfig,
365 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
368 $form->{exchangerate} =
371 : $form->parse_amount($myconfig, $form->{exchangerate});
375 # fill in subject if there is none
376 if ($form->{type} =~ /_order$/) {
378 $form->{subject} = qq|$form->{label} $form->{ordnumber}|
379 unless $form->{subject};
382 $form->{subject} = qq|$form->{label} $form->{quonumber}|
383 unless $form->{subject};
386 # if there is a message stuff it into the intnotes
387 my $cc = "Cc: $form->{cc}\\r\n" if $form->{cc};
388 my $bcc = "Bcc: $form->{bcc}\\r\n" if $form->{bcc};
389 my $now = scalar localtime;
390 $form->{intnotes} .= qq|\r
391 \r| if $form->{intnotes};
393 $form->{intnotes} .= qq|[email]\r
396 $cc${bcc}Subject: $form->{subject}\r
398 Message: $form->{message}\r| if $form->{message};
400 ($null, $form->{department_id}) = split(/--/, $form->{department});
405 qq|ordnumber = ?, quonumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?, | .
406 qq|customer_id = ?, amount = ?, netamount = ?, reqdate = ?, taxincluded = ?, | .
407 qq|shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, curr = ?, closed = ?, | .
408 qq|delivered = ?, proforma = ?, quotation = ?, department_id = ?, language_id = ?, | .
409 qq|taxzone_id = ?, shipto_id = ?, payment_id = ?, delivery_vendor_id = ?, delivery_customer_id = ?, | .
410 qq|globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ? | .
413 @values = ($form->{ordnumber}, $form->{quonumber},
414 $form->{cusordnumber}, conv_date($form->{transdate}),
415 conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
416 $amount, $netamount, conv_date($reqdate),
417 $form->{taxincluded} ? 't' : 'f', $form->{shippingpoint},
418 $form->{shipvia}, $form->{notes}, $form->{intnotes},
419 $form->{currency}, $form->{closed} ? 't' : 'f',
420 $form->{delivered} ? "t" : "f", $form->{proforma} ? 't' : 'f',
421 $quotation, conv_i($form->{department_id}),
422 conv_i($form->{language_id}), conv_i($form->{taxzone_id}),
423 conv_i($form->{shipto_id}), conv_i($form->{payment_id}),
424 conv_i($form->{delivery_vendor_id}),
425 conv_i($form->{delivery_customer_id}),
426 conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
427 conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
428 conv_i($form->{id}));
429 do_query($form, $dbh, $query, @values);
431 $form->{ordtotal} = $amount;
434 $form->{name} = $form->{ $form->{vc} };
435 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
437 if (!$form->{shipto_id}) {
438 $form->add_shipto($dbh, $form->{id}, "OE");
441 # save printed, emailed, queued
442 $form->save_status($dbh);
444 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
445 if ($form->{vc} eq 'customer') {
446 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
447 $form->{exchangerate}, 0);
449 if ($form->{vc} eq 'vendor') {
450 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
451 0, $form->{exchangerate});
455 if ($form->{type} =~ /_order$/) {
458 &adj_onhand($dbh, $form, $ml * -1);
461 my $rc = $dbh->commit;
464 $form->{saved_xyznumber} = $form->{$form->{type} =~ /_quotation$/ ?
465 "quonumber" : "ordnumber"};
467 Common::webdav_folder($form) if ($main::webdav);
469 $main::lxdebug->leave_sub();
474 # this function closes multiple orders given in $form->{ordnumber_#}.
475 # use this for multiple orders that don't have to be saved back
476 # single orders should use OE::save instead.
478 $main::lxdebug->enter_sub();
480 my ($self, $myconfig, $form) = @_;
483 map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} }
484 (1 .. $form->{rowcount});
486 my $dbh = $form->dbconnect($myconfig);
487 $query = qq|UPDATE oe SET | .
489 qq|WHERE ordnumber IN (|
490 . join(', ', map { $dbh->quote($_) } @ids) . qq|)|;
491 $dbh->do($query) || $form->dberror($query);
494 $main::lxdebug->leave_sub();
498 $main::lxdebug->enter_sub();
500 my ($self, $myconfig, $form) = @_;
502 $main::lxdebug->leave_sub() unless ($form->{"id"});
504 my $dbh = $form->dbconnect($myconfig);
505 do_query($form, $dbh, qq|UPDATE oe SET closed = TRUE where id = ?|,
509 $main::lxdebug->leave_sub();
513 $main::lxdebug->enter_sub();
515 my ($self, $myconfig, $form, $spool) = @_;
517 # connect to database
518 my $dbh = $form->dbconnect_noauto($myconfig);
521 my $query = qq|SELECT s.spoolfile FROM status s | .
522 qq|WHERE s.trans_id = ?|;
523 my @values = (conv_i($form->{id}));
524 $sth = $dbh->prepare($query);
525 $sth->execute(@values) || $self->dberror($query);
530 while (($spoolfile) = $sth->fetchrow_array) {
531 push @spoolfiles, $spoolfile;
535 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o | .
536 qq|WHERE o.trans_id = ?|;
537 @values = (conv_i($form->{id}));
538 $sth = $dbh->prepare($query);
539 $sth->execute(@values) || $self->dberror($query);
541 while (my ($id, $ship) = $sth->fetchrow_array) {
542 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
547 @values = (conv_i($form->{id}));
550 $query = qq|DELETE FROM inventory | .
552 do_query($form, $dbh, $query, @values);
554 # delete status entries
555 $query = qq|DELETE FROM status | .
556 qq|WHERE trans_id = ?|;
557 do_query($form, $dbh, $query, @values);
560 $query = qq|DELETE FROM oe | .
562 do_query($form, $dbh, $query, @values);
564 # delete individual entries
565 $query = qq|DELETE FROM orderitems | .
566 qq|WHERE trans_id = ?|;
567 do_query($form, $dbh, $query, @values);
569 $query = qq|DELETE FROM shipto | .
570 qq|WHERE trans_id = ? AND module = 'OE'|;
571 do_query($form, $dbh, $query, @values);
573 my $rc = $dbh->commit;
577 foreach $spoolfile (@spoolfiles) {
578 unlink "$spool/$spoolfile" if $spoolfile;
582 $main::lxdebug->leave_sub();
588 $main::lxdebug->enter_sub();
590 my ($self, $myconfig, $form) = @_;
592 # connect to database
593 my $dbh = $form->dbconnect_noauto($myconfig);
595 my ($query, @values, @ids);
597 # translate the ids (given by id_# and trans_id_#) into one array of ids, so we can join them later
599 push @ids, $form->{"trans_id_$_"}
600 if ($form->{"multi_id_$_"} and $form->{"trans_id_$_"})
601 } (1 .. $form->{"rowcount"});
603 # if called in multi id mode, and still only got one id, switch back to single id
604 if ($form->{"rowcount"} and $#ids == 0) {
605 $form->{"id"} = $ids[0];
611 # get default accounts and last order number
613 qq|SELECT (SELECT c.accno FROM chart c | .
614 qq| WHERE d.inventory_accno_id = c.id) AS inventory_accno, | .
615 qq| (SELECT c.accno FROM chart c | .
616 qq| WHERE d.income_accno_id = c.id) AS income_accno, | .
617 qq| (SELECT c.accno FROM chart c | .
618 qq| WHERE d.expense_accno_id = c.id) AS expense_accno, | .
619 qq| (SELECT c.accno FROM chart c | .
620 qq| WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, | .
621 qq| (SELECT c.accno FROM chart c | .
622 qq| WHERE d.fxloss_accno_id = c.id) AS fxloss_accno, | .
623 qq|d.curr AS currencies | .
627 qq|SELECT (SELECT c.accno FROM chart c | .
628 qq| WHERE d.inventory_accno_id = c.id) AS inventory_accno, | .
629 qq| (SELECT c.accno FROM chart c | .
630 qq| WHERE d.income_accno_id = c.id) AS income_accno, | .
631 qq| (SELECT c.accno FROM chart c | .
632 qq| WHERE d.expense_accno_id = c.id) AS expense_accno, | .
633 qq| (SELECT c.accno FROM chart c | .
634 qq| WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, | .
635 qq| (SELECT c.accno FROM chart c | .
636 qq| WHERE d.fxloss_accno_id = c.id) AS fxloss_accno, | .
637 qq|d.curr AS currencies, | .
638 qq|current_date AS transdate, current_date AS reqdate | .
641 my $sth = $dbh->prepare($query);
642 $sth->execute || $form->dberror($query);
644 my $ref = $sth->fetchrow_hashref(NAME_lc);
645 map { $form->{$_} = $ref->{$_} } keys %$ref;
648 ($form->{currency}) = split(/:/, $form->{currencies});
650 # set reqdate if this is an invoice->order conversion. If someone knows a better check to ensure
651 # we come from invoices, feel free.
652 $form->{reqdate} = $form->{deliverydate}
653 if ( $form->{deliverydate}
654 and $form->{callback} =~ /action=ar_transactions/);
656 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
658 if ($form->{id} or @ids) {
660 # retrieve order for single id
661 # NOTE: this query is intended to fetch all information only ONCE.
662 # so if any of these infos is important (or even different) for any item,
663 # it will be killed out and then has to be fetched from the item scope query further down
665 qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate, | .
666 qq| o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes, | .
667 qq| o.curr AS currency, e.name AS employee, o.employee_id, o.salesman_id, | .
668 qq| o.${vc}_id, cv.name AS ${vc}, o.amount AS invtotal, | .
669 qq| o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber, | .
670 qq| d.description AS department, o.payment_id, o.language_id, o.taxzone_id, | .
671 qq| o.delivery_customer_id, o.delivery_vendor_id, o.proforma, o.shipto_id, | .
672 qq| o.globalproject_id, o.delivered | .
674 qq|JOIN ${vc} cv ON (o.${vc}_id = cv.id) | .
675 qq|LEFT JOIN employee e ON (o.employee_id = e.id) | .
676 qq|LEFT JOIN department d ON (o.department_id = d.id) | .
677 ($form->{id} ? qq|WHERE o.id = ?| :
678 qq|WHERE o.id IN (| . join(', ', map("? ", @ids)) . qq|)|);
679 @values = $form->{id} ? ($form->{id}) : @ids;
680 $sth = prepare_execute_query($form, $dbh, $query, @values);
682 $ref = $sth->fetchrow_hashref(NAME_lc);
683 map { $form->{$_} = $ref->{$_} } keys %$ref;
685 $form->{saved_xyznumber} = $form->{$form->{type} =~ /_quotation$/ ?
686 "quonumber" : "ordnumber"};
688 # set all entries for multiple ids blank that yield different information
689 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
690 map { $form->{$_} = '' if ($ref->{$_} ne $form->{$_}) } keys %$ref;
693 # if not given, fill transdate with current_date
694 $form->{transdate} = $form->current_date($myconfig)
695 unless $form->{transdate};
699 if ($form->{delivery_customer_id}) {
700 $query = qq|SELECT name FROM customer WHERE id = ?|;
701 ($form->{delivery_customer_string}) =
702 selectrow_query($form, $dbh, $query, $form->{delivery_customer_id});
705 if ($form->{delivery_vendor_id}) {
706 $query = qq|SELECT name FROM customer WHERE id = ?|;
707 ($form->{delivery_vendor_string}) =
708 selectrow_query($form, $dbh, $query, $form->{delivery_vendor_id});
711 # shipto and pinted/mailed/queued status makes only sense for single id retrieve
713 $query = qq|SELECT s.* FROM shipto s | .
714 qq|WHERE s.trans_id = ? AND s.module = 'OE'|;
715 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
717 $ref = $sth->fetchrow_hashref(NAME_lc);
719 map { $form->{$_} = $ref->{$_} } keys %$ref;
722 # get printed, emailed and queued
723 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname | .
725 qq|WHERE s.trans_id = ?|;
726 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
728 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
729 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
730 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
731 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
732 if $ref->{spoolfile};
735 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
738 my %oid = ('Pg' => 'oid',
739 'Oracle' => 'rowid');
742 $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
744 $form->{taxzone_id} = 0 unless ($form->{taxzone_id});
746 # retrieve individual items
747 # this query looks up all information about the items
748 # stuff different from the whole will not be overwritten, but saved with a suffix.
750 qq|SELECT o.id AS orderitems_id, | .
751 qq| c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid, | .
752 qq| c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid, | .
753 qq| c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid, | .
754 qq| oe.ordnumber AS ordnumber_oe, oe.transdate AS transdate_oe, oe.cusordnumber AS cusordnumber_oe, | .
755 qq| p.partnumber, p.assembly, o.description, o.qty, | .
756 qq| 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, | .
757 qq| o.reqdate, o.project_id, o.serialnumber, o.ship, | .
758 qq| o.ordnumber, o.transdate, o.cusordnumber, o.subtotal, o.longdescription, | .
759 qq| pr.projectnumber, p.formel, | .
760 qq| pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup | .
761 qq|FROM orderitems o | .
762 qq|JOIN parts p ON (o.parts_id = p.id) | .
763 qq|JOIN oe ON (o.trans_id = oe.id) | .
764 qq|LEFT JOIN chart c1 ON ((SELECT inventory_accno_id FROM buchungsgruppen WHERE id=p.buchungsgruppen_id) = c1.id) | .
765 qq|LEFT JOIN chart c2 ON ((SELECT income_accno_id_$form->{taxzone_id} FROM buchungsgruppen WHERE id=p.buchungsgruppen_id) = c2.id) | .
766 qq|LEFT JOIN chart c3 ON ((SELECT expense_accno_id_$form->{taxzone_id} FROM buchungsgruppen WHERE id=p.buchungsgruppen_id) = c3.id) | .
767 qq|LEFT JOIN project pr ON (o.project_id = pr.id) | .
768 qq|LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id) | .
769 ($form->{id} ? qq|WHERE o.trans_id = ?| :
770 qq|WHERE o.trans_id IN (| . join(", ", map("?", @ids)) . qq|)|) .
771 qq|ORDER BY o.$oid{$myconfig->{dbdriver}}|;
773 @ids = $form->{id} ? ($form->{id}) : @ids;
774 $sth = prepare_execute_query($form, $dbh, $query, @values);
776 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
777 if (!$ref->{"part_inventory_accno_id"}) {
778 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
780 delete($ref->{"part_inventory_accno_id"});
782 # in collective order, copy global ordnumber, transdate, cusordnumber into item scope
783 # unless already present there
784 # remove _oe entries afterwards
785 map { $ref->{$_} = $ref->{"${_}_oe"} if ($ref->{$_} eq '') }
786 qw|ordnumber transdate cusordnumber|
788 map { delete $ref->{$_} } qw|ordnumber_oe transdate_oe cusordnumber_oe|;
792 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >= 0)) {
794 qq|SELECT accno AS inventory_accno, | .
795 qq| new_chart_id AS inventory_new_chart, | .
796 qq| date($transdate) - valid_from AS inventory_valid | .
797 qq|FROM chart WHERE id = $ref->{inventory_new_chart}|;
798 ($ref->{inventory_accno}, $ref->{inventory_new_chart},
799 $ref->{inventory_valid}) = selectrow_query($form, $dbh, $query);
802 while ($ref->{income_new_chart} && ($ref->{income_valid} >= 0)) {
804 qq|SELECT accno AS income_accno, | .
805 qq| new_chart_id AS income_new_chart, | .
806 qq| date($transdate) - valid_from AS income_valid | .
807 qq|FROM chart WHERE id = $ref->{income_new_chart}|;
808 ($ref->{income_accno}, $ref->{income_new_chart},
809 $ref->{income_valid}) = selectrow_query($form, $dbh, $query);
812 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >= 0)) {
814 qq|SELECT accno AS expense_accno, | .
815 qq| new_chart_id AS expense_new_chart, | .
816 qq| date($transdate) - valid_from AS expense_valid | .
817 qq|FROM chart WHERE id = $ref->{expense_new_chart}|;
818 ($ref->{expense_accno}, $ref->{expense_new_chart},
819 $ref->{expense_valid}) = selectrow_query($form, $dbh, $query);
822 # delete orderitems_id in collective orders, so that they get cloned no matter what
823 delete $ref->{orderitems_id} if (@ids);
825 # get tax rates and description
827 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
829 qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber | .
830 qq|FROM tax t LEFT JOIN chart c on (c.id = t.chart_id) | .
831 qq|WHERE t.id IN (SELECT tk.tax_id FROM taxkeys tk | .
832 qq| WHERE tk.chart_id = (SELECT id FROM chart WHERE accno = ?) | .
833 qq| AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1) | .
834 qq|ORDER BY c.accno|;
835 $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
836 $ref->{taxaccounts} = "";
838 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
839 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
843 $ref->{taxaccounts} .= "$ptr->{accno} ";
844 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
845 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
846 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
847 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
848 $form->{taxaccounts} .= "$ptr->{accno} ";
853 chop $ref->{taxaccounts};
854 push @{ $form->{form_details} }, $ref;
862 $form->lastname_used($dbh, $myconfig, $form->{vc})
863 unless $form->{"$form->{vc}_id"};
867 $form->{exchangerate} =
868 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
869 ($form->{vc} eq 'customer') ? "buy" : "sell");
871 Common::webdav_folder($form) if ($main::webdav);
874 $query = qq|SELECT id, description FROM tax_zones|;
875 $form->{TAXZONE} = selectall_hashref_query($form, $dbh, $query);
877 my $rc = $dbh->commit;
880 $main::lxdebug->leave_sub();
886 $main::lxdebug->enter_sub();
888 my ($self, $myconfig, $form) = @_;
890 # connect to database
891 my $dbh = $form->dbconnect($myconfig);
897 my $nodiscount_subtotal = 0;
898 my $discount_subtotal = 0;
904 my $subtotal_header = 0;
907 my %oid = ('Pg' => 'oid',
908 'Oracle' => 'rowid');
910 my (@project_ids, %projectnumbers);
912 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
914 # sort items by partsgroup
915 for $i (1 .. $form->{rowcount}) {
917 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
918 $partsgroup = $form->{"partsgroup_$i"};
920 push @partsgroup, [$i, $partsgroup];
921 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
925 $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
926 join(", ", map("?", @project_ids)) . ")";
927 $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
928 while (my $ref = $sth->fetchrow_hashref()) {
929 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
934 $form->{"globalprojectnumber"} =
935 $projectnumbers{$form->{"globalproject_id"}};
938 qw(runningnumber number description longdescription qty ship unit bin
939 partnotes serialnumber reqdate sellprice listprice netprice
940 discount p_discount discount_sub nodiscount_sub
941 linetotal nodiscount_linetotal tax_rate projectnumber);
944 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
947 if ($item->[1] ne $sameitem) {
948 push(@{ $form->{description} }, qq|$item->[1]|);
949 $sameitem = $item->[1];
951 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
954 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
956 if ($form->{"id_$i"} != 0) {
958 # add number, description and qty to $form->{number}, ....
960 if ($form->{"subtotal_$i"} && !$subtotal_header) {
961 $subtotal_header = $i;
962 $position = int($position);
965 } elsif ($subtotal_header) {
967 $position = int($position);
968 $position = $position.".".$subposition;
970 $position = int($position);
974 push(@{ $form->{runningnumber} }, $i);
975 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
976 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
977 push(@{ $form->{longdescription} }, qq|$form->{"longdescription_$i"}|);
978 push(@{ $form->{qty} },
979 $form->format_amount($myconfig, $form->{"qty_$i"}));
980 push(@{ $form->{ship} },
981 $form->format_amount($myconfig, $form->{"ship_$i"}));
982 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
983 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
984 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
985 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
986 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
988 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
990 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
992 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
993 my ($dec) = ($sellprice =~ /\.(\d+)/);
995 my $decimalplaces = ($dec > 2) ? $dec : 2;
999 $sellprice * $form->parse_amount($myconfig,
1000 $form->{"discount_$i"}) / 100,
1004 $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
1006 # keep a netprice as well, (sellprice - discount)
1007 #$form->{"netprice_$i"} = $sellprice - $discount;
1008 $form->{"netprice_$i"} = $sellprice - $i_discount;
1009 my $nodiscount_linetotal =
1010 $form->round_amount($form->{"qty_$i"} * $sellprice, 2);
1012 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
1014 push(@{ $form->{netprice} },
1015 ($form->{"netprice_$i"} != 0)
1016 ? $form->format_amount(
1017 $myconfig, $form->{"netprice_$i"},
1024 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
1026 $linetotal = ($linetotal != 0) ? $linetotal : " ";
1028 push(@{ $form->{discount} }, $discount);
1029 push(@{ $form->{p_discount} }, $form->{"discount_$i"});
1031 $form->{ordtotal} += $linetotal;
1032 $discount_subtotal += $linetotal;
1033 $form->{nodiscount_total} += $nodiscount_linetotal;
1034 $nodiscount_subtotal += $nodiscount_linetotal;
1035 $form->{discount_total} += $form->parse_amount($myconfig, $discount);
1037 if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
1038 $discount_subtotal = $form->format_amount($myconfig, $discount_subtotal, 2);
1039 push(@{ $form->{discount_sub} }, $discount_subtotal);
1040 $nodiscount_subtotal = $form->format_amount($myconfig, $nodiscount_subtotal, 2);
1041 push(@{ $form->{nodiscount_sub} }, $nodiscount_subtotal);
1042 $discount_subtotal = 0;
1043 $nodiscount_subtotal = 0;
1044 $subtotal_header = 0;
1046 push(@{ $form->{discount_sub} }, "");
1047 push(@{ $form->{nodiscount_sub} }, "");
1050 if ($linetotal == $netto_linetotal) {
1051 $nodiscount += $linetotal;
1053 push(@{ $form->{linetotal} },
1054 $form->format_amount($myconfig, $linetotal, 2));
1055 push(@{ $form->{nodiscount_linetotal} },
1056 $form->format_amount($myconfig, $nodiscount_linetotal, 2));
1058 push(@{ $form->{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}});
1060 my ($taxamount, $taxbase);
1063 map { $taxrate += $form->{"${_}_rate"} } split(/ /, $form->{"taxaccounts_$i"});
1065 if ($form->{taxincluded}) {
1068 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
1069 $taxbase = $linetotal / (1 + $taxrate);
1071 $taxamount = $linetotal * $taxrate;
1072 $taxbase = $linetotal;
1075 if ($taxamount != 0) {
1076 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
1077 $taxaccounts{$item} +=
1078 $taxamount * $form->{"${item}_rate"} / $taxrate;
1079 $taxbase{$item} += $taxbase;
1083 $tax_rate = $taxrate * 100;
1084 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
1086 if ($form->{"assembly_$i"}) {
1089 # get parts and push them onto the stack
1091 if ($form->{groupitems}) {
1093 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
1095 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
1098 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty, | .
1099 qq|pg.partsgroup | .
1100 qq|FROM assembly a | .
1101 qq| JOIN parts p ON (a.parts_id = p.id) | .
1102 qq| LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id) | .
1103 qq| WHERE a.bom = '1' | .
1104 qq| AND a.id = ? | . $sortorder;
1105 @values = ($form->{"id_$i"});
1106 $sth = $dbh->prepare($query);
1107 $sth->execute(@values) || $form->dberror($query);
1109 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1110 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
1111 map({ push(@{ $form->{$_} }, "") }
1112 grep({ $_ ne "description" } @arrays));
1113 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
1114 push(@{ $form->{description} }, $sameitem);
1117 push(@{ $form->{description} },
1118 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
1120 . qq|, $ref->{partnumber}, $ref->{description}|);
1122 map({ push(@{ $form->{$_} }, "") }
1123 grep({ $_ ne "description" } @arrays));
1132 foreach $item (sort keys %taxaccounts) {
1133 push(@{ $form->{taxbase} },
1134 $form->format_amount($myconfig, $taxbase{$item}, 2));
1136 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
1138 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
1139 push(@{ $form->{taxdescription} }, $form->{"${item}_description"} . q{ } . 100 * $form->{"${item}_rate"} . q{%});
1140 push(@{ $form->{taxrate} },
1141 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
1142 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
1144 $yesdiscount = $form->{nodiscount_total} - $nodiscount;
1145 $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
1146 $form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
1147 $form->{nodiscount} = $form->format_amount($myconfig, $nodiscount, 2);
1148 $form->{yesdiscount} = $form->format_amount($myconfig, $yesdiscount, 2);
1150 if($form->{taxincluded}) {
1151 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal} - $tax, 2);
1154 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1157 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
1160 $form->{quototal} = $form->{ordtotal} =
1161 $form->format_amount($myconfig, $form->{ordtotal}, 2);
1163 if ($form->{type} =~ /_quotation/) {
1164 $form->set_payment_options($myconfig, $form->{quodate});
1166 $form->set_payment_options($myconfig, $form->{orddate});
1169 $form->{username} = $myconfig->{name};
1173 $main::lxdebug->leave_sub();
1176 sub project_description {
1177 $main::lxdebug->enter_sub();
1179 my ($self, $dbh, $id) = @_;
1181 my $query = qq|SELECT description FROM project WHERE id = ?|;
1182 my ($value) = selectrow_query($form, $dbh, $query, $id);
1184 $main::lxdebug->leave_sub();
1190 $main::lxdebug->enter_sub();
1192 my ($dbh, $form, $ml) = @_;
1194 my $all_units = $form->{all_units};
1197 qq|SELECT oi.parts_id, oi.ship, oi.unit, p.inventory_accno_id, p.assembly | .
1198 qq| FROM orderitems oi | .
1199 qq| JOIN parts p ON (p.id = oi.parts_id) | .
1200 qq| WHERE oi.trans_id = ?|;
1201 my @values = ($form->{id});
1202 my $sth = $dbh->prepare($query);
1203 $sth->execute(@values) || $form->dberror($query);
1206 qq|SELECT sum(p.inventory_accno_id) | .
1208 qq|JOIN assembly a ON (a.parts_id = p.id) | .
1210 my $ath = $dbh->prepare($query) || $form->dberror($query);
1214 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1215 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1217 # do not update if assembly consists of all services
1218 if ($ref->{assembly}) {
1219 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1221 ($ispa) = $sth->fetchrow_array;
1229 $query = qq|SELECT unit FROM parts WHERE id = ?|;
1230 my ($item_unit) = selectrow_query($form, $dbh, $query, $ref->{parts_id});
1233 if (defined($all_units->{$item_unit}->{factor}) &&
1234 (($all_units->{$item_unit}->{factor} * 1) != 0)) {
1235 $basefactor = $all_units->{$ref->{unit}}->{factor} /
1236 $all_units->{$item_unit}->{factor};
1238 my $baseqty = $ref->{ship} * $basefactor;
1240 # adjust onhand in parts table
1241 $form->update_balance($dbh, "parts", "onhand",
1242 qq|id = $ref->{parts_id}|,
1249 $main::lxdebug->leave_sub();