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| o.transaction_description, | .
70 qq| o.marge_total, o.marge_percent, | .
71 qq| ex.$rate AS exchangerate, | .
72 qq| pr.projectnumber AS globalprojectnumber, | .
73 qq| e.name AS employee | .
75 qq|JOIN $vc ct ON (o.${vc}_id = ct.id) | .
76 qq|LEFT JOIN employee e ON (o.employee_id = e.id) | .
77 qq|LEFT JOIN exchangerate ex ON (ex.curr = o.curr | .
78 qq| AND ex.transdate = o.transdate) | .
79 qq|LEFT JOIN project pr ON (o.globalproject_id = pr.id) | .
80 qq|WHERE (o.quotation = ?) |;
81 push(@values, $quotation);
83 my ($null, $department_id) = split /--/, $form->{department};
85 $query .= qq| AND o.department_id = ?|;
86 push(@values, $department_id);
89 if ($form->{"project_id"}) {
91 qq|AND ((globalproject_id = ?) OR EXISTS | .
92 qq| (SELECT * FROM orderitems oi | .
93 qq| WHERE oi.project_id = ? AND oi.trans_id = o.id))|;
94 push(@values, $form->{"project_id"}, $form->{"project_id"});
97 if ($form->{"${vc}_id"}) {
98 $query .= " AND o.${vc}_id = ?";
99 push(@values, $form->{"${vc}_id"});
101 } elsif ($form->{$vc}) {
102 $query .= " AND ct.name ILIKE ?";
103 push(@values, '%' . $form->{$vc} . '%');
106 if ($form->{employee_id}) {
107 $query .= " AND o.employee_id = ?";
108 push @values, conv_i($form->{employee_id});
111 if (!$form->{open} && !$form->{closed}) {
112 $query .= " AND o.id = 0";
113 } elsif (!($form->{open} && $form->{closed})) {
114 $query .= ($form->{open}) ? " AND o.closed = '0'" : " AND o.closed = '1'";
117 if (($form->{"notdelivered"} || $form->{"delivered"}) &&
118 ($form->{"notdelivered"} ne $form->{"delivered"})) {
119 $query .= $form->{"delivered"} ?
120 " AND o.delivered " : " AND NOT o.delivered";
123 if ($form->{$ordnumber}) {
124 $query .= qq| AND o.$ordnumber ILIKE ?|;
125 push(@values, '%' . $form->{$ordnumber} . '%');
128 if($form->{transdatefrom}) {
129 $query .= qq| AND o.transdate >= ?|;
130 push(@values, conv_date($form->{transdatefrom}));
133 if($form->{transdateto}) {
134 $query .= qq| AND o.transdate <= ?|;
135 push(@values, conv_date($form->{transdateto}));
138 if ($form->{transaction_description}) {
139 $query .= qq| AND o.transaction_description ILIKE ?|;
140 push(@values, '%' . $form->{transaction_description} . '%');
143 my $sortorder = join(', ', ("o.id", $form->sort_columns("transdate", $ordnumber, "name")));
144 my %allowed_sort_columns =
145 ("transdate" => "o.transdate",
146 "reqdate" => "o.reqdate",
148 "ordnumber" => "o.ordnumber",
149 "quonumber" => "o.quonumber",
151 "employee" => "e.name",
152 "shipvia" => "o.shipvia",
153 "transaction_description" => "o.transaction_description");
154 if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
155 $sortorder = $allowed_sort_columns{$form->{sort}};
157 $query .= qq| ORDER by | . $sortorder;
159 my $sth = $dbh->prepare($query);
160 $sth->execute(@values) ||
161 $form->dberror($query . " (" . join(", ", @values) . ")");
165 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
166 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
167 push @{ $form->{OE} }, $ref if $ref->{id} != $id{ $ref->{id} };
168 $id{ $ref->{id} } = $ref->{id};
174 $main::lxdebug->leave_sub();
178 $main::lxdebug->enter_sub();
180 my ($self, $myconfig, $form) = @_;
182 # connect to database, turn off autocommit
183 my $dbh = $form->dbconnect_noauto($myconfig);
185 my ($query, @values, $sth, $null);
186 my $exchangerate = 0;
188 my $all_units = AM->retrieve_units($myconfig, $form);
189 $form->{all_units} = $all_units;
191 $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
192 unless ($form->{employee_id}) {
193 $form->get_employee($dbh);
196 my $ml = ($form->{type} eq 'sales_order') ? 1 : -1;
200 &adj_onhand($dbh, $form, $ml) if $form->{type} =~ /_order$/;
202 $query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
203 do_query($form, $dbh, $query, $form->{id});
205 $query = qq|DELETE FROM shipto | .
206 qq|WHERE trans_id = ? AND module = 'OE'|;
207 do_query($form, $dbh, $query, $form->{id});
211 $query = qq|SELECT nextval('id')|;
212 ($form->{id}) = selectrow_query($form, $dbh, $query);
214 $query = qq|INSERT INTO oe (id, ordnumber, employee_id) VALUES (?, '', ?)|;
215 do_query($form, $dbh, $query, $form->{id}, $form->{employee_id});
231 for my $i (1 .. $form->{rowcount}) {
233 map({ $form->{"${_}_$i"} =
234 $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(qty ship));
236 if ($form->{"id_$i"}) {
239 $query = qq|SELECT unit FROM parts WHERE id = ?|;
240 my ($item_unit) = selectrow_query($form, $dbh, $query, $form->{"id_$i"});
243 if (defined($all_units->{$item_unit}->{factor}) &&
244 (($all_units->{$item_unit}->{factor} * 1) != 0)) {
245 $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} /
246 $all_units->{$item_unit}->{factor};
248 my $baseqty = $form->{"qty_$i"} * $basefactor;
250 $form->{"marge_percent_$i"} = $form->parse_amount($myconfig, $form->{"marge_percent_$i"}) * 1;
251 $form->{"marge_absolut_$i"} = $form->parse_amount($myconfig, $form->{"marge_absolut_$i"}) * 1;
252 $form->{"lastcost_$i"} = $form->{"lastcost_$i"} * 1;
254 # set values to 0 if nothing entered
255 $form->{"discount_$i"} =
256 $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
258 $form->{"sellprice_$i"} =
259 $form->parse_amount($myconfig, $form->{"sellprice_$i"});
260 $fxsellprice = $form->{"sellprice_$i"};
262 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
264 my $decimalplaces = ($dec > 2) ? $dec : 2;
267 $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"},
269 $form->{"sellprice_$i"} =
270 $form->round_amount($form->{"sellprice_$i"} - $discount,
273 $form->{"inventory_accno_$i"} *= 1;
274 $form->{"expense_accno_$i"} *= 1;
277 $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"}, 2);
279 @taxaccounts = split(/ /, $form->{"taxaccounts_$i"});
283 map { $taxrate += $form->{"${_}_rate"} } @taxaccounts;
285 if ($form->{taxincluded}) {
286 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
287 $taxbase = $linetotal - $taxamount;
289 # we are not keeping a natural price, do not round
290 $form->{"sellprice_$i"} =
291 $form->{"sellprice_$i"} * (1 / (1 + $taxrate));
293 $taxamount = $linetotal * $taxrate;
294 $taxbase = $linetotal;
297 if ($form->round_amount($taxrate, 7) == 0) {
298 if ($form->{taxincluded}) {
299 foreach $item (@taxaccounts) {
301 $form->round_amount($linetotal * $form->{"${item}_rate"} /
302 (1 + abs($form->{"${item}_rate"})),
305 $taxaccounts{$item} += $taxamount;
306 $taxdiff += $taxamount;
308 $taxbase{$item} += $taxbase;
310 $taxaccounts{ $taxaccounts[0] } += $taxdiff;
312 foreach $item (@taxaccounts) {
313 $taxaccounts{$item} += $linetotal * $form->{"${item}_rate"};
314 $taxbase{$item} += $taxbase;
318 foreach $item (@taxaccounts) {
319 $taxaccounts{$item} +=
320 $taxamount * $form->{"${item}_rate"} / $taxrate;
321 $taxbase{$item} += $taxbase;
325 $netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
328 ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
330 # get pricegroup_id and save ist
331 ($null, my $pricegroup_id) = split(/--/, $form->{"sellprice_pg_$i"});
334 # save detail record in orderitems table
336 $query = qq|INSERT INTO orderitems (|;
337 if ($form->{"orderitems_id_$i"}) {
340 $query .= qq|trans_id, parts_id, description, longdescription, qty, base_qty, | .
341 qq|sellprice, discount, unit, reqdate, project_id, serialnumber, ship, | .
342 qq|pricegroup_id, ordnumber, transdate, cusordnumber, subtotal, | .
343 qq|marge_percent, marge_total, lastcost) | .
345 if($form->{"orderitems_id_$i"}) {
347 push(@values, $form->{"orderitems_id_$i"});
349 $query .= qq|?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
351 conv_i($form->{id}), conv_i($form->{"id_$i"}),
352 $form->{"description_$i"}, $form->{"longdescription_$i"},
353 $form->{"qty_$i"}, $baseqty,
354 $fxsellprice, $form->{"discount_$i"},
355 $form->{"unit_$i"}, conv_date($reqdate), conv_i($form->{"project_id_$i"}),
356 $form->{"serialnumber_$i"}, $form->{"ship_$i"}, conv_i($pricegroup_id),
357 $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
358 $form->{"cusordnumber_$i"}, $form->{"subtotal_$i"} ? 't' : 'f',
359 $form->{"marge_percent_$i"}, $form->{"marge_absolut_$i"},
360 $form->{"lastcost_$i"});
361 do_query($form, $dbh, $query, @values);
363 $form->{"sellprice_$i"} = $fxsellprice;
364 $form->{"discount_$i"} *= 100;
368 $reqdate = ($form->{reqdate}) ? $form->{reqdate} : undef;
372 map { $tax += $form->round_amount($taxaccounts{$_}, 2) } keys %taxaccounts;
374 $amount = $form->round_amount($netamount + $tax, 2);
375 $netamount = $form->round_amount($netamount, 2);
377 if ($form->{currency} eq $form->{defaultcurrency}) {
378 $form->{exchangerate} = 1;
381 $form->check_exchangerate($myconfig,
384 ($form->{vc} eq 'customer') ? 'buy' : 'sell');
387 $form->{exchangerate} =
390 : $form->parse_amount($myconfig, $form->{exchangerate});
392 my $quotation = $form->{type} =~ /_order$/ ? 'f' : 't';
394 ($null, $form->{department_id}) = split(/--/, $form->{department});
399 ordnumber = ?, quonumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
400 customer_id = ?, amount = ?, netamount = ?, reqdate = ?, taxincluded = ?,
401 shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, curr = ?, closed = ?,
402 delivered = ?, proforma = ?, quotation = ?, department_id = ?, language_id = ?,
403 taxzone_id = ?, shipto_id = ?, payment_id = ?, delivery_vendor_id = ?, delivery_customer_id = ?,
404 globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?, marge_total = ?, marge_percent = ?
407 @values = ($form->{ordnumber}, $form->{quonumber},
408 $form->{cusordnumber}, conv_date($form->{transdate}),
409 conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
410 $amount, $netamount, conv_date($reqdate),
411 $form->{taxincluded} ? 't' : 'f', $form->{shippingpoint},
412 $form->{shipvia}, $form->{notes}, $form->{intnotes},
413 substr($form->{currency}, 0, 3), $form->{closed} ? 't' : 'f',
414 $form->{delivered} ? "t" : "f", $form->{proforma} ? 't' : 'f',
415 $quotation, conv_i($form->{department_id}),
416 conv_i($form->{language_id}), conv_i($form->{taxzone_id}),
417 conv_i($form->{shipto_id}), conv_i($form->{payment_id}),
418 conv_i($form->{delivery_vendor_id}),
419 conv_i($form->{delivery_customer_id}),
420 conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
421 conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
422 $form->{transaction_description},
423 $form->{marge_total} * 1, $form->{marge_percent} * 1,
424 conv_i($form->{id}));
425 do_query($form, $dbh, $query, @values);
427 $form->{ordtotal} = $amount;
430 $form->{name} = $form->{ $form->{vc} };
431 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
433 if (!$form->{shipto_id}) {
434 $form->add_shipto($dbh, $form->{id}, "OE");
437 # save printed, emailed, queued
438 $form->save_status($dbh);
440 if (($form->{currency} ne $form->{defaultcurrency}) && !$exchangerate) {
441 if ($form->{vc} eq 'customer') {
442 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
443 $form->{exchangerate}, 0);
445 if ($form->{vc} eq 'vendor') {
446 $form->update_exchangerate($dbh, $form->{currency}, $form->{transdate},
447 0, $form->{exchangerate});
451 if ($form->{type} =~ /_order$/) {
454 &adj_onhand($dbh, $form, $ml * -1);
457 my $rc = $dbh->commit;
460 $form->{saved_xyznumber} = $form->{$form->{type} =~ /_quotation$/ ?
461 "quonumber" : "ordnumber"};
463 Common::webdav_folder($form) if ($main::webdav);
465 $main::lxdebug->leave_sub();
470 # this function closes multiple orders given in $form->{ordnumber_#}.
471 # use this for multiple orders that don't have to be saved back
472 # single orders should use OE::save instead.
474 $main::lxdebug->enter_sub();
476 my ($self, $myconfig, $form) = @_;
479 map { push @ids, $form->{"ordnumber_$_"} if $form->{"ordnumber_$_"} }
480 (1 .. $form->{rowcount});
482 my $dbh = $form->dbconnect($myconfig);
483 $query = qq|UPDATE oe SET | .
485 qq|WHERE ordnumber IN (|
486 . join(', ', map { $dbh->quote($_) } @ids) . qq|)|;
487 $dbh->do($query) || $form->dberror($query);
490 $main::lxdebug->leave_sub();
494 $main::lxdebug->enter_sub();
496 my ($self, $myconfig, $form) = @_;
498 $main::lxdebug->leave_sub() unless ($form->{"id"});
500 my $dbh = $form->dbconnect($myconfig);
501 do_query($form, $dbh, qq|UPDATE oe SET closed = TRUE where id = ?|,
505 $main::lxdebug->leave_sub();
509 $main::lxdebug->enter_sub();
511 my ($self, $myconfig, $form, $spool) = @_;
513 # connect to database
514 my $dbh = $form->dbconnect_noauto($myconfig);
517 my $query = qq|SELECT s.spoolfile FROM status s | .
518 qq|WHERE s.trans_id = ?|;
519 my @values = (conv_i($form->{id}));
520 $sth = $dbh->prepare($query);
521 $sth->execute(@values) || $self->dberror($query);
526 while (($spoolfile) = $sth->fetchrow_array) {
527 push @spoolfiles, $spoolfile;
531 $query = qq|SELECT o.parts_id, o.ship FROM orderitems o | .
532 qq|WHERE o.trans_id = ?|;
533 @values = (conv_i($form->{id}));
534 $sth = $dbh->prepare($query);
535 $sth->execute(@values) || $self->dberror($query);
537 while (my ($id, $ship) = $sth->fetchrow_array) {
538 $form->update_balance($dbh, "parts", "onhand", qq|id = $id|, $ship * -1);
543 @values = (conv_i($form->{id}));
546 $query = qq|DELETE FROM inventory | .
548 do_query($form, $dbh, $query, @values);
550 # delete status entries
551 $query = qq|DELETE FROM status | .
552 qq|WHERE trans_id = ?|;
553 do_query($form, $dbh, $query, @values);
556 $query = qq|DELETE FROM oe | .
558 do_query($form, $dbh, $query, @values);
560 # delete individual entries
561 $query = qq|DELETE FROM orderitems | .
562 qq|WHERE trans_id = ?|;
563 do_query($form, $dbh, $query, @values);
565 $query = qq|DELETE FROM shipto | .
566 qq|WHERE trans_id = ? AND module = 'OE'|;
567 do_query($form, $dbh, $query, @values);
569 my $rc = $dbh->commit;
573 foreach $spoolfile (@spoolfiles) {
574 unlink "$spool/$spoolfile" if $spoolfile;
578 $main::lxdebug->leave_sub();
584 $main::lxdebug->enter_sub();
586 my ($self, $myconfig, $form) = @_;
588 # connect to database
589 my $dbh = $form->dbconnect_noauto($myconfig);
591 my ($query, @values, @ids);
593 # translate the ids (given by id_# and trans_id_#) into one array of ids, so we can join them later
595 push @ids, $form->{"trans_id_$_"}
596 if ($form->{"multi_id_$_"} and $form->{"trans_id_$_"})
597 } (1 .. $form->{"rowcount"});
599 # if called in multi id mode, and still only got one id, switch back to single id
600 if ($form->{"rowcount"} and $#ids == 0) {
601 $form->{"id"} = $ids[0];
607 # get default accounts and last order number
609 qq|SELECT (SELECT c.accno FROM chart c | .
610 qq| WHERE d.inventory_accno_id = c.id) AS inventory_accno, | .
611 qq| (SELECT c.accno FROM chart c | .
612 qq| WHERE d.income_accno_id = c.id) AS income_accno, | .
613 qq| (SELECT c.accno FROM chart c | .
614 qq| WHERE d.expense_accno_id = c.id) AS expense_accno, | .
615 qq| (SELECT c.accno FROM chart c | .
616 qq| WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, | .
617 qq| (SELECT c.accno FROM chart c | .
618 qq| WHERE d.fxloss_accno_id = c.id) AS fxloss_accno, | .
619 qq|d.curr AS currencies | .
623 qq|SELECT (SELECT c.accno FROM chart c | .
624 qq| WHERE d.inventory_accno_id = c.id) AS inventory_accno, | .
625 qq| (SELECT c.accno FROM chart c | .
626 qq| WHERE d.income_accno_id = c.id) AS income_accno, | .
627 qq| (SELECT c.accno FROM chart c | .
628 qq| WHERE d.expense_accno_id = c.id) AS expense_accno, | .
629 qq| (SELECT c.accno FROM chart c | .
630 qq| WHERE d.fxgain_accno_id = c.id) AS fxgain_accno, | .
631 qq| (SELECT c.accno FROM chart c | .
632 qq| WHERE d.fxloss_accno_id = c.id) AS fxloss_accno, | .
633 qq|d.curr AS currencies, | .
634 qq|current_date AS transdate, current_date AS reqdate | .
637 my $sth = $dbh->prepare($query);
638 $sth->execute || $form->dberror($query);
640 my $ref = $sth->fetchrow_hashref(NAME_lc);
641 map { $form->{$_} = $ref->{$_} } keys %$ref;
644 ($form->{currency}) = split(/:/, $form->{currencies});
646 # set reqdate if this is an invoice->order conversion. If someone knows a better check to ensure
647 # we come from invoices, feel free.
648 $form->{reqdate} = $form->{deliverydate}
649 if ( $form->{deliverydate}
650 and $form->{callback} =~ /action=ar_transactions/);
652 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
654 if ($form->{id} or @ids) {
656 # retrieve order for single id
657 # NOTE: this query is intended to fetch all information only ONCE.
658 # so if any of these infos is important (or even different) for any item,
659 # it will be killed out and then has to be fetched from the item scope query further down
661 qq|SELECT o.cp_id, o.ordnumber, o.transdate, o.reqdate, | .
662 qq| o.taxincluded, o.shippingpoint, o.shipvia, o.notes, o.intnotes, | .
663 qq| o.curr AS currency, e.name AS employee, o.employee_id, o.salesman_id, | .
664 qq| o.${vc}_id, cv.name AS ${vc}, o.amount AS invtotal, | .
665 qq| o.closed, o.reqdate, o.quonumber, o.department_id, o.cusordnumber, | .
666 qq| d.description AS department, o.payment_id, o.language_id, o.taxzone_id, | .
667 qq| o.delivery_customer_id, o.delivery_vendor_id, o.proforma, o.shipto_id, | .
668 qq| o.globalproject_id, o.delivered, o.transaction_description | .
670 qq|JOIN ${vc} cv ON (o.${vc}_id = cv.id) | .
671 qq|LEFT JOIN employee e ON (o.employee_id = e.id) | .
672 qq|LEFT JOIN department d ON (o.department_id = d.id) | .
673 ($form->{id} ? qq|WHERE o.id = ?| :
674 qq|WHERE o.id IN (| . join(', ', map("? ", @ids)) . qq|)|);
675 @values = $form->{id} ? ($form->{id}) : @ids;
676 $sth = prepare_execute_query($form, $dbh, $query, @values);
678 $ref = $sth->fetchrow_hashref(NAME_lc);
679 map { $form->{$_} = $ref->{$_} } keys %$ref;
681 $form->{saved_xyznumber} = $form->{$form->{type} =~ /_quotation$/ ?
682 "quonumber" : "ordnumber"};
684 # set all entries for multiple ids blank that yield different information
685 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
686 map { $form->{$_} = '' if ($ref->{$_} ne $form->{$_}) } keys %$ref;
689 # if not given, fill transdate with current_date
690 $form->{transdate} = $form->current_date($myconfig)
691 unless $form->{transdate};
695 if ($form->{delivery_customer_id}) {
696 $query = qq|SELECT name FROM customer WHERE id = ?|;
697 ($form->{delivery_customer_string}) =
698 selectrow_query($form, $dbh, $query, $form->{delivery_customer_id});
701 if ($form->{delivery_vendor_id}) {
702 $query = qq|SELECT name FROM customer WHERE id = ?|;
703 ($form->{delivery_vendor_string}) =
704 selectrow_query($form, $dbh, $query, $form->{delivery_vendor_id});
707 # shipto and pinted/mailed/queued status makes only sense for single id retrieve
709 $query = qq|SELECT s.* FROM shipto s | .
710 qq|WHERE s.trans_id = ? AND s.module = 'OE'|;
711 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
713 $ref = $sth->fetchrow_hashref(NAME_lc);
715 map { $form->{$_} = $ref->{$_} } keys %$ref;
718 # get printed, emailed and queued
719 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname | .
721 qq|WHERE s.trans_id = ?|;
722 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
724 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
725 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
726 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
727 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} "
728 if $ref->{spoolfile};
731 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
734 my %oid = ('Pg' => 'oid',
735 'Oracle' => 'rowid');
738 $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
740 $form->{taxzone_id} = 0 unless ($form->{taxzone_id});
742 # retrieve individual items
743 # this query looks up all information about the items
744 # stuff different from the whole will not be overwritten, but saved with a suffix.
746 qq|SELECT o.id AS orderitems_id, | .
747 qq| c1.accno AS inventory_accno, c1.new_chart_id AS inventory_new_chart, date($transdate) - c1.valid_from as inventory_valid, | .
748 qq| c2.accno AS income_accno, c2.new_chart_id AS income_new_chart, date($transdate) - c2.valid_from as income_valid, | .
749 qq| c3.accno AS expense_accno, c3.new_chart_id AS expense_new_chart, date($transdate) - c3.valid_from as expense_valid, | .
750 qq| oe.ordnumber AS ordnumber_oe, oe.transdate AS transdate_oe, oe.cusordnumber AS cusordnumber_oe, | .
751 qq| p.partnumber, p.assembly, o.description, o.qty, | .
752 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, | .
753 qq| o.reqdate, o.project_id, o.serialnumber, o.ship, o.lastcost, | .
754 qq| o.ordnumber, o.transdate, o.cusordnumber, o.subtotal, o.longdescription, | .
755 qq| pr.projectnumber, p.formel, | .
756 qq| pg.partsgroup, o.pricegroup_id, (SELECT pricegroup FROM pricegroup WHERE id=o.pricegroup_id) as pricegroup | .
757 qq|FROM orderitems o | .
758 qq|JOIN parts p ON (o.parts_id = p.id) | .
759 qq|JOIN oe ON (o.trans_id = oe.id) | .
760 qq|LEFT JOIN chart c1 ON ((SELECT inventory_accno_id FROM buchungsgruppen WHERE id=p.buchungsgruppen_id) = c1.id) | .
761 qq|LEFT JOIN chart c2 ON ((SELECT income_accno_id_$form->{taxzone_id} FROM buchungsgruppen WHERE id=p.buchungsgruppen_id) = c2.id) | .
762 qq|LEFT JOIN chart c3 ON ((SELECT expense_accno_id_$form->{taxzone_id} FROM buchungsgruppen WHERE id=p.buchungsgruppen_id) = c3.id) | .
763 qq|LEFT JOIN project pr ON (o.project_id = pr.id) | .
764 qq|LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id) | .
765 ($form->{id} ? qq|WHERE o.trans_id = ?| :
766 qq|WHERE o.trans_id IN (| . join(", ", map("?", @ids)) . qq|)|) .
767 qq|ORDER BY o.$oid{$myconfig->{dbdriver}}|;
769 @ids = $form->{id} ? ($form->{id}) : @ids;
770 $sth = prepare_execute_query($form, $dbh, $query, @values);
772 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
773 if (!$ref->{"part_inventory_accno_id"}) {
774 map({ delete($ref->{$_}); } qw(inventory_accno inventory_new_chart inventory_valid));
776 delete($ref->{"part_inventory_accno_id"});
778 # in collective order, copy global ordnumber, transdate, cusordnumber into item scope
779 # unless already present there
780 # remove _oe entries afterwards
781 map { $ref->{$_} = $ref->{"${_}_oe"} if ($ref->{$_} eq '') }
782 qw|ordnumber transdate cusordnumber|
784 map { delete $ref->{$_} } qw|ordnumber_oe transdate_oe cusordnumber_oe|;
788 while ($ref->{inventory_new_chart} && ($ref->{inventory_valid} >= 0)) {
790 qq|SELECT accno AS inventory_accno, | .
791 qq| new_chart_id AS inventory_new_chart, | .
792 qq| date($transdate) - valid_from AS inventory_valid | .
793 qq|FROM chart WHERE id = $ref->{inventory_new_chart}|;
794 ($ref->{inventory_accno}, $ref->{inventory_new_chart},
795 $ref->{inventory_valid}) = selectrow_query($form, $dbh, $query);
798 while ($ref->{income_new_chart} && ($ref->{income_valid} >= 0)) {
800 qq|SELECT accno AS income_accno, | .
801 qq| new_chart_id AS income_new_chart, | .
802 qq| date($transdate) - valid_from AS income_valid | .
803 qq|FROM chart WHERE id = $ref->{income_new_chart}|;
804 ($ref->{income_accno}, $ref->{income_new_chart},
805 $ref->{income_valid}) = selectrow_query($form, $dbh, $query);
808 while ($ref->{expense_new_chart} && ($ref->{expense_valid} >= 0)) {
810 qq|SELECT accno AS expense_accno, | .
811 qq| new_chart_id AS expense_new_chart, | .
812 qq| date($transdate) - valid_from AS expense_valid | .
813 qq|FROM chart WHERE id = $ref->{expense_new_chart}|;
814 ($ref->{expense_accno}, $ref->{expense_new_chart},
815 $ref->{expense_valid}) = selectrow_query($form, $dbh, $query);
818 # delete orderitems_id in collective orders, so that they get cloned no matter what
819 delete $ref->{orderitems_id} if (@ids);
821 # get tax rates and description
823 ($form->{vc} eq "customer") ? $ref->{income_accno} : $ref->{expense_accno};
825 qq|SELECT c.accno, t.taxdescription, t.rate, t.taxnumber | .
826 qq|FROM tax t LEFT JOIN chart c on (c.id = t.chart_id) | .
827 qq|WHERE t.id IN (SELECT tk.tax_id FROM taxkeys tk | .
828 qq| WHERE tk.chart_id = (SELECT id FROM chart WHERE accno = ?) | .
829 qq| AND startdate <= $transdate ORDER BY startdate DESC LIMIT 1) | .
830 qq|ORDER BY c.accno|;
831 $stw = prepare_execute_query($form, $dbh, $query, $accno_id);
832 $ref->{taxaccounts} = "";
834 while ($ptr = $stw->fetchrow_hashref(NAME_lc)) {
835 if (($ptr->{accno} eq "") && ($ptr->{rate} == 0)) {
839 $ref->{taxaccounts} .= "$ptr->{accno} ";
840 if (!($form->{taxaccounts} =~ /$ptr->{accno}/)) {
841 $form->{"$ptr->{accno}_rate"} = $ptr->{rate};
842 $form->{"$ptr->{accno}_description"} = $ptr->{taxdescription};
843 $form->{"$ptr->{accno}_taxnumber"} = $ptr->{taxnumber};
844 $form->{taxaccounts} .= "$ptr->{accno} ";
849 chop $ref->{taxaccounts};
850 push @{ $form->{form_details} }, $ref;
858 $form->lastname_used($dbh, $myconfig, $form->{vc})
859 unless $form->{"$form->{vc}_id"};
863 $form->{exchangerate} =
864 $form->get_exchangerate($dbh, $form->{currency}, $form->{transdate},
865 ($form->{vc} eq 'customer') ? "buy" : "sell");
867 Common::webdav_folder($form) if ($main::webdav);
869 my $rc = $dbh->commit;
872 $main::lxdebug->leave_sub();
878 $main::lxdebug->enter_sub();
880 my ($self, $myconfig, $form) = @_;
882 # connect to database
883 my $dbh = $form->dbconnect($myconfig);
889 my $nodiscount_subtotal = 0;
890 my $discount_subtotal = 0;
896 my $subtotal_header = 0;
899 my %oid = ('Pg' => 'oid',
900 'Oracle' => 'rowid');
902 my (@project_ids, %projectnumbers);
904 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
906 # sort items by partsgroup
907 for $i (1 .. $form->{rowcount}) {
909 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
910 $partsgroup = $form->{"partsgroup_$i"};
912 push @partsgroup, [$i, $partsgroup];
913 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
917 $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
918 join(", ", map("?", @project_ids)) . ")";
919 $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
920 while (my $ref = $sth->fetchrow_hashref()) {
921 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
926 $form->{"globalprojectnumber"} =
927 $projectnumbers{$form->{"globalproject_id"}};
930 qw(runningnumber number description longdescription qty ship unit bin
931 partnotes serialnumber reqdate sellprice listprice netprice
932 discount p_discount discount_sub nodiscount_sub
933 linetotal nodiscount_linetotal tax_rate projectnumber);
936 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
939 if ($item->[1] ne $sameitem) {
940 push(@{ $form->{description} }, qq|$item->[1]|);
941 $sameitem = $item->[1];
943 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
946 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
948 if ($form->{"id_$i"} != 0) {
950 # add number, description and qty to $form->{number}, ....
952 if ($form->{"subtotal_$i"} && !$subtotal_header) {
953 $subtotal_header = $i;
954 $position = int($position);
957 } elsif ($subtotal_header) {
959 $position = int($position);
960 $position = $position.".".$subposition;
962 $position = int($position);
966 push(@{ $form->{runningnumber} }, $i);
967 push(@{ $form->{number} }, qq|$form->{"partnumber_$i"}|);
968 push(@{ $form->{description} }, qq|$form->{"description_$i"}|);
969 push(@{ $form->{longdescription} }, qq|$form->{"longdescription_$i"}|);
970 push(@{ $form->{qty} },
971 $form->format_amount($myconfig, $form->{"qty_$i"}));
972 push(@{ $form->{ship} },
973 $form->format_amount($myconfig, $form->{"ship_$i"}));
974 push(@{ $form->{unit} }, qq|$form->{"unit_$i"}|);
975 push(@{ $form->{bin} }, qq|$form->{"bin_$i"}|);
976 push(@{ $form->{"partnotes"} }, qq|$form->{"partnotes_$i"}|);
977 push(@{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}|);
978 push(@{ $form->{reqdate} }, qq|$form->{"reqdate_$i"}|);
980 push(@{ $form->{sellprice} }, $form->{"sellprice_$i"});
982 push(@{ $form->{listprice} }, $form->{"listprice_$i"});
984 my $sellprice = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
985 my ($dec) = ($sellprice =~ /\.(\d+)/);
987 my $decimalplaces = ($dec > 2) ? $dec : 2;
991 $sellprice * $form->parse_amount($myconfig,
992 $form->{"discount_$i"}) / 100,
996 $form->round_amount($form->{"qty_$i"} * $i_discount, $decimalplaces);
998 # keep a netprice as well, (sellprice - discount)
999 #$form->{"netprice_$i"} = $sellprice - $discount;
1000 $form->{"netprice_$i"} = $sellprice - $i_discount;
1001 my $nodiscount_linetotal =
1002 $form->round_amount($form->{"qty_$i"} * $sellprice, 2);
1004 $form->round_amount($form->{"qty_$i"} * $form->{"netprice_$i"}, 2);
1006 push(@{ $form->{netprice} },
1007 ($form->{"netprice_$i"} != 0)
1008 ? $form->format_amount(
1009 $myconfig, $form->{"netprice_$i"},
1016 ? $form->format_amount($myconfig, $discount * -1, $decimalplaces)
1018 $linetotal = ($linetotal != 0) ? $linetotal : " ";
1020 push(@{ $form->{discount} }, $discount);
1021 push(@{ $form->{p_discount} }, $form->{"discount_$i"});
1023 $form->{ordtotal} += $linetotal;
1024 $discount_subtotal += $linetotal;
1025 $form->{nodiscount_total} += $nodiscount_linetotal;
1026 $nodiscount_subtotal += $nodiscount_linetotal;
1027 $form->{discount_total} += $form->parse_amount($myconfig, $discount);
1029 if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
1030 $discount_subtotal = $form->format_amount($myconfig, $discount_subtotal, 2);
1031 push(@{ $form->{discount_sub} }, $discount_subtotal);
1032 $nodiscount_subtotal = $form->format_amount($myconfig, $nodiscount_subtotal, 2);
1033 push(@{ $form->{nodiscount_sub} }, $nodiscount_subtotal);
1034 $discount_subtotal = 0;
1035 $nodiscount_subtotal = 0;
1036 $subtotal_header = 0;
1038 push(@{ $form->{discount_sub} }, "");
1039 push(@{ $form->{nodiscount_sub} }, "");
1042 if ($linetotal == $netto_linetotal) {
1043 $nodiscount += $linetotal;
1045 push(@{ $form->{linetotal} },
1046 $form->format_amount($myconfig, $linetotal, 2));
1047 push(@{ $form->{nodiscount_linetotal} },
1048 $form->format_amount($myconfig, $nodiscount_linetotal, 2));
1050 push(@{ $form->{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}});
1052 my ($taxamount, $taxbase);
1055 map { $taxrate += $form->{"${_}_rate"} } split(/ /, $form->{"taxaccounts_$i"});
1057 if ($form->{taxincluded}) {
1060 $taxamount = $linetotal * $taxrate / (1 + $taxrate);
1061 $taxbase = $linetotal / (1 + $taxrate);
1063 $taxamount = $linetotal * $taxrate;
1064 $taxbase = $linetotal;
1067 if ($taxamount != 0) {
1068 foreach my $item (split / /, $form->{"taxaccounts_$i"}) {
1069 $taxaccounts{$item} +=
1070 $taxamount * $form->{"${item}_rate"} / $taxrate;
1071 $taxbase{$item} += $taxbase;
1075 $tax_rate = $taxrate * 100;
1076 push(@{ $form->{tax_rate} }, qq|$tax_rate|);
1078 if ($form->{"assembly_$i"}) {
1081 # get parts and push them onto the stack
1083 if ($form->{groupitems}) {
1085 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
1087 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
1090 $query = qq|SELECT p.partnumber, p.description, p.unit, a.qty, | .
1091 qq|pg.partsgroup | .
1092 qq|FROM assembly a | .
1093 qq| JOIN parts p ON (a.parts_id = p.id) | .
1094 qq| LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id) | .
1095 qq| WHERE a.bom = '1' | .
1096 qq| AND a.id = ? | . $sortorder;
1097 @values = ($form->{"id_$i"});
1098 $sth = $dbh->prepare($query);
1099 $sth->execute(@values) || $form->dberror($query);
1101 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1102 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
1103 map({ push(@{ $form->{$_} }, "") }
1104 grep({ $_ ne "description" } @arrays));
1105 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
1106 push(@{ $form->{description} }, $sameitem);
1109 push(@{ $form->{description} },
1110 $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}
1112 . qq|, $ref->{partnumber}, $ref->{description}|);
1114 map({ push(@{ $form->{$_} }, "") }
1115 grep({ $_ ne "description" } @arrays));
1124 foreach $item (sort keys %taxaccounts) {
1125 push(@{ $form->{taxbase} },
1126 $form->format_amount($myconfig, $taxbase{$item}, 2));
1128 $tax += $taxamount = $form->round_amount($taxaccounts{$item}, 2);
1130 push(@{ $form->{tax} }, $form->format_amount($myconfig, $taxamount, 2));
1131 push(@{ $form->{taxdescription} }, $form->{"${item}_description"} . q{ } . 100 * $form->{"${item}_rate"} . q{%});
1132 push(@{ $form->{taxrate} },
1133 $form->format_amount($myconfig, $form->{"${item}_rate"} * 100));
1134 push(@{ $form->{taxnumber} }, $form->{"${item}_taxnumber"});
1136 $yesdiscount = $form->{nodiscount_total} - $nodiscount;
1137 $form->{nodiscount_subtotal} = $form->format_amount($myconfig, $form->{nodiscount_total}, 2);
1138 $form->{discount_total} = $form->format_amount($myconfig, $form->{discount_total}, 2);
1139 $form->{nodiscount} = $form->format_amount($myconfig, $nodiscount, 2);
1140 $form->{yesdiscount} = $form->format_amount($myconfig, $yesdiscount, 2);
1142 if($form->{taxincluded}) {
1143 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal} - $tax, 2);
1146 $form->{subtotal} = $form->format_amount($myconfig, $form->{ordtotal}, 2);
1149 ($form->{taxincluded}) ? $form->{ordtotal} : $form->{ordtotal} + $tax;
1152 $form->{quototal} = $form->{ordtotal} =
1153 $form->format_amount($myconfig, $form->{ordtotal}, 2);
1155 if ($form->{type} =~ /_quotation/) {
1156 $form->set_payment_options($myconfig, $form->{quodate});
1158 $form->set_payment_options($myconfig, $form->{orddate});
1161 $form->{username} = $myconfig->{name};
1165 $main::lxdebug->leave_sub();
1168 sub project_description {
1169 $main::lxdebug->enter_sub();
1171 my ($self, $dbh, $id) = @_;
1173 my $query = qq|SELECT description FROM project WHERE id = ?|;
1174 my ($value) = selectrow_query($form, $dbh, $query, $id);
1176 $main::lxdebug->leave_sub();
1182 $main::lxdebug->enter_sub();
1184 my ($dbh, $form, $ml) = @_;
1186 my $all_units = $form->{all_units};
1189 qq|SELECT oi.parts_id, oi.ship, oi.unit, p.inventory_accno_id, p.assembly | .
1190 qq| FROM orderitems oi | .
1191 qq| JOIN parts p ON (p.id = oi.parts_id) | .
1192 qq| WHERE oi.trans_id = ?|;
1193 my @values = ($form->{id});
1194 my $sth = $dbh->prepare($query);
1195 $sth->execute(@values) || $form->dberror($query);
1198 qq|SELECT sum(p.inventory_accno_id) | .
1200 qq|JOIN assembly a ON (a.parts_id = p.id) | .
1202 my $ath = $dbh->prepare($query) || $form->dberror($query);
1206 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
1207 if ($ref->{inventory_accno_id} || $ref->{assembly}) {
1209 # do not update if assembly consists of all services
1210 if ($ref->{assembly}) {
1211 $ath->execute($ref->{parts_id}) || $form->dberror($query);
1213 ($ispa) = $sth->fetchrow_array;
1221 $query = qq|SELECT unit FROM parts WHERE id = ?|;
1222 my ($item_unit) = selectrow_query($form, $dbh, $query, $ref->{parts_id});
1225 if (defined($all_units->{$item_unit}->{factor}) &&
1226 (($all_units->{$item_unit}->{factor} * 1) != 0)) {
1227 $basefactor = $all_units->{$ref->{unit}}->{factor} /
1228 $all_units->{$item_unit}->{factor};
1230 my $baseqty = $ref->{ship} * $basefactor;
1232 # adjust onhand in parts table
1233 $form->update_balance($dbh, "parts", "onhand",
1234 qq|id = $ref->{parts_id}|,
1241 $main::lxdebug->leave_sub();