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 #======================================================================
31 # Delivery Order entry module
32 #======================================================================
36 use List::Util qw(max);
49 $main::lxdebug->enter_sub();
53 my $myconfig = \%main::myconfig;
54 my $form = $main::form;
57 my $dbh = $form->get_standard_dbh($myconfig);
59 my (@where, @values, $where);
61 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
64 qq|SELECT dord.id, dord.donumber, dord.ordnumber, dord.transdate,
65 ct.${vc}number, ct.name, dord.${vc}_id, dord.globalproject_id,
66 dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
67 dord.transaction_description,
68 pr.projectnumber AS globalprojectnumber,
69 dep.description AS department,
72 FROM delivery_orders dord
73 LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
74 LEFT JOIN employee e ON (dord.employee_id = e.id)
75 LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
76 LEFT JOIN project pr ON (dord.globalproject_id = pr.id)
77 LEFT JOIN department dep ON (dord.department_id = dep.id)
80 push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
82 if ($form->{department_id}) {
83 push @where, qq|dord.department_id = ?|;
84 push @values, conv_i($form->{department_id});
87 if ($form->{project_id}) {
89 qq|(dord.globalproject_id = ?) OR EXISTS
90 (SELECT * FROM delivery_order_items doi
91 WHERE (doi.project_id = ?) AND (doi.delivery_order_id = dord.id))|;
92 push @values, conv_i($form->{project_id}), conv_i($form->{project_id});
95 if ($form->{"${vc}_id"}) {
96 push @where, qq|dord.${vc}_id = ?|;
97 push @values, $form->{"${vc}_id"};
99 } elsif ($form->{$vc}) {
100 push @where, qq|ct.name ILIKE ?|;
101 push @values, '%' . $form->{$vc} . '%';
104 foreach my $item (qw(employee_id salesman_id)) {
105 next unless ($form->{$item});
106 push @where, "dord.$item = ?";
107 push @values, conv_i($form->{$item});
109 if (!$main::auth->assert('sales_all_edit', 1)) {
110 push @where, qq|dord.employee_id = (select id from employee where login= ?)|;
111 push @values, $form->{login};
114 foreach my $item (qw(donumber ordnumber cusordnumber transaction_description)) {
115 next unless ($form->{$item});
116 push @where, qq|dord.$item ILIKE ?|;
117 push @values, '%' . $form->{$item} . '%';
120 if (($form->{open} || $form->{closed}) &&
121 ($form->{open} ne $form->{closed})) {
122 push @where, ($form->{open} ? "NOT " : "") . "COALESCE(dord.closed, FALSE)";
125 if (($form->{notdelivered} || $form->{delivered}) &&
126 ($form->{notdelivered} ne $form->{delivered})) {
127 push @where, ($form->{delivered} ? "" : "NOT ") . "COALESCE(dord.delivered, FALSE)";
130 if($form->{transdatefrom}) {
131 push @where, qq|dord.transdate >= ?|;
132 push @values, conv_date($form->{transdatefrom});
135 if($form->{transdateto}) {
136 push @where, qq|dord.transdate <= ?|;
137 push @values, conv_date($form->{transdateto});
141 $query .= " WHERE " . join(" AND ", map { "($_)" } @where);
144 my %allowed_sort_columns = (
145 "transdate" => "dord.transdate",
147 "donumber" => "dord.donumber",
148 "ordnumber" => "dord.ordnumber",
150 "employee" => "e.name",
151 "salesman" => "sm.name",
152 "shipvia" => "dord.shipvia",
153 "transaction_description" => "dord.transaction_description",
154 "department" => "lower(dep.description)",
157 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
158 my $sortorder = "dord.id";
159 if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
160 $sortorder = $allowed_sort_columns{$form->{sort}};
163 $query .= qq| ORDER by | . $sortorder . " $sortdir";
165 $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
167 if (scalar @{ $form->{DO} }) {
171 WHERE NOT COALESCE(quotation, FALSE)
173 AND (COALESCE(${vc}_id, 0) != 0)|;
175 my $sth = prepare_query($form, $dbh, $query);
177 foreach my $dord (@{ $form->{DO} }) {
178 next unless ($dord->{ordnumber});
179 do_statement($form, $sth, $query, $dord->{ordnumber});
180 ($dord->{oe_id}) = $sth->fetchrow_array();
186 $main::lxdebug->leave_sub();
190 $main::lxdebug->enter_sub();
194 my $myconfig = \%main::myconfig;
195 my $form = $main::form;
197 # connect to database, turn off autocommit
198 my $dbh = $form->get_standard_dbh($myconfig);
200 my ($query, @values, $sth, $null);
202 my $all_units = AM->retrieve_units($myconfig, $form);
203 $form->{all_units} = $all_units;
205 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
208 $form->{donumber} = $form->update_defaults($myconfig, $form->{type} eq 'sales_delivery_order' ? 'sdonumber' : 'pdonumber', $dbh) unless $form->{donumber};
209 $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
210 $form->get_employee($dbh) unless ($form->{employee_id});
212 my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
216 $query = qq|DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = ?)|;
217 do_query($form, $dbh, $query, conv_i($form->{id}));
219 $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
220 do_query($form, $dbh, $query, conv_i($form->{id}));
222 $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
223 do_query($form, $dbh, $query, conv_i($form->{id}));
227 $query = qq|SELECT nextval('id')|;
228 ($form->{id}) = selectrow_query($form, $dbh, $query);
230 $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id) VALUES (?, '', ?)|;
231 do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}));
237 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
238 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
241 my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
242 my @part_ids = keys %part_id_map;
246 $query = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
247 %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
250 my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
251 my $h_item_id = prepare_query($form, $dbh, $q_item_id);
254 qq|INSERT INTO delivery_order_items (
255 id, delivery_order_id, parts_id, description, longdescription, qty, base_qty,
256 sellprice, discount, unit, reqdate, project_id, serialnumber,
257 ordnumber, transdate, cusordnumber,
258 lastcost, price_factor_id, price_factor, marge_price_factor, pricegroup_id)
259 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
260 (SELECT factor FROM price_factors WHERE id = ?), ?, ?)|;
261 my $h_item = prepare_query($form, $dbh, $q_item);
264 qq|INSERT INTO delivery_order_items_stock (delivery_order_item_id, qty, unit, warehouse_id, bin_id, chargenumber, bestbefore)
265 VALUES (?, ?, ?, ?, ?, ?, ?)|;
266 my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
268 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
270 for my $i (1 .. $form->{rowcount}) {
271 next if (!$form->{"id_$i"});
273 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
275 my $item_unit = $part_unit_map{$form->{"id_$i"}};
278 if (defined($all_units->{$item_unit}->{factor}) && (($all_units->{$item_unit}->{factor} * 1) != 0)) {
279 $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
281 my $baseqty = $form->{"qty_$i"} * $basefactor;
283 # set values to 0 if nothing entered
284 $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"});
285 $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
286 $form->{"lastcost_$i"} = $form->parse_amount($myconfig, $form->{"lastcost_$i"});
288 $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
289 my $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
291 $items_reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
293 do_statement($form, $h_item_id, $q_item_id);
294 my ($item_id) = $h_item_id->fetchrow_array();
296 # Get pricegroup_id and save it. Unfortunately the interface
297 # also uses ID "0" for signalling that none is selected, but "0"
298 # must not be stored in the database. Therefore we cannot simply
300 my $pricegroup_id = $form->{"pricegroup_id_$i"} * 1;
301 $pricegroup_id = undef if !$pricegroup_id;
303 # save detail record in delivery_order_items table
304 @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
305 $form->{"description_$i"}, $form->{"longdescription_$i"},
306 $form->{"qty_$i"}, $baseqty,
307 $form->{"sellprice_$i"}, $form->{"discount_$i"} / 100,
308 $form->{"unit_$i"}, conv_date($items_reqdate), conv_i($form->{"project_id_$i"}),
309 $form->{"serialnumber_$i"},
310 $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
311 $form->{"cusordnumber_$i"},
312 $form->{"lastcost_$i"},
313 conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
314 conv_i($form->{"marge_price_factor_$i"}),
316 do_statement($form, $h_item, $q_item, @values);
318 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
320 foreach my $sinfo (@{ $stock_info }) {
321 @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
322 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber}, conv_date($sinfo->{bestbefore}));
323 do_statement($form, $h_item_stock, $q_item_stock, @values);
326 CVar->save_custom_variables(module => 'IC',
327 sub_module => 'delivery_order_items',
328 trans_id => $item_id,
329 configs => $ic_cvar_configs,
331 name_prefix => 'ic_',
332 name_postfix => "_$i",
336 $h_item_id->finish();
338 $h_item_stock->finish();
341 # reqdate is last items reqdate (?: old behaviour) if not already set
342 $form->{reqdate} ||= $items_reqdate;
345 qq|UPDATE delivery_orders SET
346 donumber = ?, ordnumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
347 customer_id = ?, reqdate = ?,
348 shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
349 delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
350 globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
351 is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, curr = ?
354 @values = ($form->{donumber}, $form->{ordnumber},
355 $form->{cusordnumber}, conv_date($form->{transdate}),
356 conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
357 conv_date($form->{reqdate}), $form->{shippingpoint}, $form->{shipvia},
358 $form->{notes}, $form->{intnotes},
359 $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
360 conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
361 conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
362 conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
363 $form->{transaction_description},
364 $form->{type} =~ /^sales/ ? 't' : 'f',
365 conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), substr($form->{currency}, 0, 3),
366 conv_i($form->{id}));
367 do_query($form, $dbh, $query, @values);
370 $form->{name} = $form->{ $form->{vc} };
371 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
373 if (!$form->{shipto_id}) {
374 $form->add_shipto($dbh, $form->{id}, "DO");
377 # save printed, emailed, queued
378 $form->save_status($dbh);
380 # Link this delivery order to the quotations it was created from.
381 RecordLinks->create_links('dbh' => $dbh,
383 'from_table' => 'oe',
384 'from_ids' => $form->{convert_from_oe_ids},
385 'to_table' => 'delivery_orders',
386 'to_id' => $form->{id},
388 delete $form->{convert_from_oe_ids};
390 $self->mark_orders_if_delivered('do_id' => $form->{id},
391 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
394 my $rc = $dbh->commit();
396 $form->{saved_donumber} = $form->{donumber};
398 Common::webdav_folder($form);
400 $main::lxdebug->leave_sub();
405 sub mark_orders_if_delivered {
406 $main::lxdebug->enter_sub();
411 Common::check_params(\%params, qw(do_id type));
413 my $myconfig = \%main::myconfig;
414 my $form = $main::form;
416 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
418 my @links = RecordLinks->get_links('dbh' => $dbh,
419 'from_table' => 'oe',
420 'to_table' => 'delivery_orders',
421 'to_id' => $params{do_id});
423 my $oe_id = @links ? $links[0]->{from_id} : undef;
425 return $main::lxdebug->leave_sub() if (!$oe_id);
427 my $all_units = AM->retrieve_all_units();
429 my $query = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit
431 LEFT JOIN parts p ON (oi.parts_id = p.id)
432 WHERE (oi.trans_id = ?)|;
433 my $sth = prepare_execute_query($form, $dbh, $query, $oe_id);
435 my %shipped = $self->get_shipped_qty('type' => $params{type},
439 while (my $ref = $sth->fetchrow_hashref()) {
440 $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor};
442 if ($ordered{$ref->{parts_id}}) {
443 $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty};
445 $ordered{$ref->{parts_id}} = $ref;
451 map { $_->{baseqty} = $_->{qty} * $all_units->{$_->{unit}}->{factor} / $all_units->{$_->{partunit}}->{factor} } values %shipped;
454 foreach my $part (values %ordered) {
455 if (!$shipped{$part->{parts_id}} || ($shipped{$part->{parts_id}}->{baseqty} < $part->{baseqty})) {
462 $query = qq|UPDATE oe
465 do_query($form, $dbh, $query, $oe_id);
466 $dbh->commit() if (!$params{dbh});
469 $main::lxdebug->leave_sub();
473 $main::lxdebug->enter_sub();
478 Common::check_params(\%params, qw(ids));
480 if (('ARRAY' ne ref $params{ids}) || !scalar @{ $params{ids} }) {
481 $main::lxdebug->leave_sub();
485 my $myconfig = \%main::myconfig;
486 my $form = $main::form;
488 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
490 my $query = qq|UPDATE delivery_orders SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar(@{ $params{ids} })) . qq|)|;
492 do_query($form, $dbh, $query, map { conv_i($_) } @{ $params{ids} });
494 $dbh->commit() unless ($params{dbh});
496 $main::lxdebug->leave_sub();
500 $main::lxdebug->enter_sub();
504 my $myconfig = \%main::myconfig;
505 my $form = $main::form;
506 my $spool = $::lx_office_conf{paths}->{spool};
508 # connect to database
509 my $dbh = $form->get_standard_dbh($myconfig);
512 my $query = qq|SELECT s.spoolfile FROM status s WHERE s.trans_id = ?|;
513 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
519 while (($spoolfile) = $sth->fetchrow_array) {
520 push @spoolfiles, $spoolfile;
525 @values = (conv_i($form->{id}));
527 # delete status entries
528 $query = qq|DELETE FROM status
530 do_query($form, $dbh, $query, @values);
532 # delete individual entries
533 $query = qq|DELETE FROM delivery_order_items_stock
534 WHERE delivery_order_item_id IN (
535 SELECT id FROM delivery_order_items
536 WHERE delivery_order_id = ?
538 do_query($form, $dbh, $query, @values);
540 # delete individual entries
541 $query = qq|DELETE FROM delivery_order_items
542 WHERE delivery_order_id = ?|;
543 do_query($form, $dbh, $query, @values);
546 $query = qq|DELETE FROM delivery_orders
548 do_query($form, $dbh, $query, @values);
550 $query = qq|DELETE FROM shipto
551 WHERE trans_id = ? AND module = 'DO'|;
552 do_query($form, $dbh, $query, @values);
554 my $rc = $dbh->commit();
557 foreach $spoolfile (@spoolfiles) {
558 unlink "$spool/$spoolfile" if $spoolfile;
562 $main::lxdebug->leave_sub();
568 $main::lxdebug->enter_sub();
573 my $myconfig = \%main::myconfig;
574 my $form = $main::form;
576 # connect to database
577 my $dbh = $form->get_standard_dbh($myconfig);
579 my ($query, $query_add, @values, $sth, $ref);
581 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
584 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
586 my $mode = !$params{ids} ? 'default' : ref $params{ids} eq 'ARRAY' ? 'multi' : 'single';
588 if ($mode eq 'default') {
589 $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate|);
590 map { $form->{$_} = $ref->{$_} } keys %$ref;
592 # if reqdate is not set from oe-workflow, set it to transdate (which is current date)
593 $form->{reqdate} ||= $form->{transdate};
596 $form->lastname_used($dbh, $myconfig, $vc) unless $form->{"${vc}_id"};
598 $main::lxdebug->leave_sub();
603 my @do_ids = map { conv_i($_) } ($mode eq 'multi' ? @{ $params{ids} } : ($params{ids}));
604 my $do_ids_placeholders = join(', ', ('?') x scalar(@do_ids));
606 # retrieve order for single id
607 # NOTE: this query is intended to fetch all information only ONCE.
608 # so if any of these infos is important (or even different) for any item,
609 # it will be killed out and then has to be fetched from the item scope query further down
611 qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.transdate, dord.reqdate,
612 dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
613 e.name AS employee, dord.employee_id, dord.salesman_id,
614 dord.${vc}_id, cv.name AS ${vc},
615 dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
616 d.description AS department, dord.language_id,
618 dord.globalproject_id, dord.delivered, dord.transaction_description,
619 dord.taxzone_id, dord.taxincluded, dord.terms, dord.curr AS currency
620 FROM delivery_orders dord
621 JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
622 LEFT JOIN employee e ON (dord.employee_id = e.id)
623 LEFT JOIN department d ON (dord.department_id = d.id)
624 WHERE dord.id IN ($do_ids_placeholders)|;
625 $sth = prepare_execute_query($form, $dbh, $query, @do_ids);
627 delete $form->{"${vc}_id"};
628 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
629 if ($form->{"${vc}_id"} && ($ref->{"${vc}_id"} != $form->{"${vc}_id"})) {
631 $main::lxdebug->leave_sub();
636 map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
637 $form->{donumber_array} .= $form->{donumber} . ' ';
641 # remove any trailing whitespace
642 $form->{currency} =~ s/\s*$//;
644 $form->{donumber_array} =~ s/\s*$//g;
646 $form->{saved_donumber} = $form->{donumber};
648 # if not given, fill transdate with current_date
649 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
651 if ($mode eq 'single') {
652 $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'DO'|;
653 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
655 $ref = $sth->fetchrow_hashref("NAME_lc");
657 map { $form->{$_} = $ref->{$_} } keys %$ref;
660 # get printed, emailed and queued
661 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
662 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
664 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
665 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
666 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
667 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
670 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
676 my %oid = ('Pg' => 'oid',
677 'Oracle' => 'rowid');
679 # retrieve individual items
680 # this query looks up all information about the items
681 # stuff different from the whole will not be overwritten, but saved with a suffix.
683 qq|SELECT doi.id AS delivery_order_items_id,
684 p.partnumber, p.assembly, p.listprice, doi.description, doi.qty,
685 doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.bin, p.notes AS partnotes,
686 doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
687 doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
688 doi.price_factor_id, doi.price_factor, doi.marge_price_factor, doi.pricegroup_id,
689 pr.projectnumber, dord.transdate AS dord_transdate,
691 FROM delivery_order_items doi
692 JOIN parts p ON (doi.parts_id = p.id)
693 JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
694 LEFT JOIN project pr ON (doi.project_id = pr.id)
695 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
696 WHERE doi.delivery_order_id IN ($do_ids_placeholders)
697 ORDER BY doi.$oid{$myconfig->{dbdriver}}|;
699 $form->{form_details} = selectall_hashref_query($form, $dbh, $query, @do_ids);
701 # Retrieve custom variables.
702 foreach my $doi (@{ $form->{form_details} }) {
703 my $cvars = CVar->get_custom_variables(dbh => $dbh,
705 sub_module => 'delivery_order_items',
706 trans_id => $doi->{delivery_order_items_id},
708 map { $doi->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
711 if ($mode eq 'single') {
712 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
715 qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber, bestbefore
716 FROM delivery_order_items_stock
717 WHERE delivery_order_item_id = ?|;
718 my $sth = prepare_query($form, $dbh, $query);
720 foreach my $doi (@{ $form->{form_details} }) {
721 do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
723 while (my $ref = $sth->fetchrow_hashref()) {
724 push @{ $requests }, $ref;
727 $doi->{"stock_${in_out}"} = YAML::Dump($requests);
733 Common::webdav_folder($form);
735 $main::lxdebug->leave_sub();
741 $main::lxdebug->enter_sub();
745 my $myconfig = \%main::myconfig;
746 my $form = $main::form;
748 # connect to database
749 my $dbh = $form->get_standard_dbh($myconfig);
759 my %oid = ('Pg' => 'oid',
760 'Oracle' => 'rowid');
762 my (@project_ids, %projectnumbers, %projectdescriptions);
764 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
766 # sort items by partsgroup
767 for $i (1 .. $form->{rowcount}) {
769 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
770 $partsgroup = $form->{"partsgroup_$i"};
772 push @partsgroup, [$i, $partsgroup];
773 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
777 $query = "SELECT id, projectnumber, description FROM project WHERE id IN (" .
778 join(", ", map("?", @project_ids)) . ")";
779 $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
780 while (my $ref = $sth->fetchrow_hashref()) {
781 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
782 $projectdescriptions{$ref->{id}} = $ref->{description};
787 $form->{"globalprojectnumber"} =
788 $projectnumbers{$form->{"globalproject_id"}};
789 $form->{"globalprojectdescription"} =
790 $projectdescriptions{$form->{"globalproject_id"}};
792 my $q_pg = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
794 JOIN parts p ON (a.parts_id = p.id)
795 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
798 my $h_pg = prepare_query($form, $dbh, $q_pg);
800 my $q_bin_wh = qq|SELECT (SELECT description FROM bin WHERE id = ?) AS bin,
801 (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
802 my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
804 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
808 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
810 $form->{TEMPLATE_ARRAYS} = { };
811 IC->prepare_parts_for_printing();
814 qw(runningnumber number description longdescription qty unit
815 partnotes serialnumber reqdate projectnumber projectdescription
816 si_runningnumber si_number si_description
817 si_warehouse si_bin si_chargenumber si_bestbefore si_qty si_unit);
819 map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays);
821 push @arrays, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
823 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
824 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
827 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
830 next if (!$form->{"id_$i"});
834 if ($item->[1] ne $sameitem) {
835 push(@{ $form->{description} }, qq|$item->[1]|);
836 $sameitem = $item->[1];
838 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
841 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
843 # add number, description and qty to $form->{number}, ....
845 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
847 push @{ $form->{TEMPLATE_ARRAYS}{runningnumber} }, $position;
848 push @{ $form->{TEMPLATE_ARRAYS}{number} }, $form->{"partnumber_$i"};
849 push @{ $form->{TEMPLATE_ARRAYS}{description} }, $form->{"description_$i"};
850 push @{ $form->{TEMPLATE_ARRAYS}{longdescription} }, $form->{"longdescription_$i"};
851 push @{ $form->{TEMPLATE_ARRAYS}{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"});
852 push @{ $form->{TEMPLATE_ARRAYS}{qty_nofmt} }, $form->{"qty_$i"};
853 push @{ $form->{TEMPLATE_ARRAYS}{unit} }, $form->{"unit_$i"};
854 push @{ $form->{TEMPLATE_ARRAYS}{partnotes} }, $form->{"partnotes_$i"};
855 push @{ $form->{TEMPLATE_ARRAYS}{serialnumber} }, $form->{"serialnumber_$i"};
856 push @{ $form->{TEMPLATE_ARRAYS}{reqdate} }, $form->{"reqdate_$i"};
857 push @{ $form->{TEMPLATE_ARRAYS}{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}};
858 push @{ $form->{TEMPLATE_ARRAYS}{projectdescription} },
859 $projectdescriptions{$form->{"project_id_$i"}};
861 if ($form->{"assembly_$i"}) {
864 # get parts and push them onto the stack
866 if ($form->{groupitems}) {
868 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
870 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
873 do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
875 while (my $ref = $h_pg->fetchrow_hashref("NAME_lc")) {
876 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
877 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
878 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
879 push(@{ $form->{TEMPLATE_ARRAYS}->{description} }, $sameitem);
881 push(@{ $form->{TEMPLATE_ARRAYS}->{"description"} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq| -- $ref->{partnumber}, $ref->{description}|);
883 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
887 if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
888 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
890 foreach my $si (@{ $stock_info }) {
893 do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
894 my $bin_wh = $h_bin_wh->fetchrow_hashref();
896 push @{ $form->{TEMPLATE_ARRAYS}{si_runningnumber}[$position-1] }, $num_si;
897 push @{ $form->{TEMPLATE_ARRAYS}{si_number}[$position-1] }, $form->{"partnumber_$i"};
898 push @{ $form->{TEMPLATE_ARRAYS}{si_description}[$position-1] }, $form->{"description_$i"};
899 push @{ $form->{TEMPLATE_ARRAYS}{si_warehouse}[$position-1] }, $bin_wh->{warehouse};
900 push @{ $form->{TEMPLATE_ARRAYS}{si_bin}[$position-1] }, $bin_wh->{bin};
901 push @{ $form->{TEMPLATE_ARRAYS}{si_chargenumber}[$position-1] }, $si->{chargenumber};
902 push @{ $form->{TEMPLATE_ARRAYS}{si_bestbefore}[$position-1] }, $si->{bestbefore};
903 push @{ $form->{TEMPLATE_ARRAYS}{si_qty}[$position-1] }, $form->format_amount($myconfig, $si->{qty} * 1);
904 push @{ $form->{TEMPLATE_ARRAYS}{si_qty_nofmt}[$position-1] }, $si->{qty} * 1;
905 push @{ $form->{TEMPLATE_ARRAYS}{si_unit}[$position-1] }, $si->{unit};
909 push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
910 CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
911 for @{ $ic_cvar_configs };
917 $form->{username} = $myconfig->{name};
919 $main::lxdebug->leave_sub();
922 sub project_description {
923 $main::lxdebug->enter_sub();
925 my ($self, $dbh, $id) = @_;
927 my $form = $main::form;
929 my $query = qq|SELECT description FROM project WHERE id = ?|;
930 my ($value) = selectrow_query($form, $dbh, $query, $id);
932 $main::lxdebug->leave_sub();
937 sub unpack_stock_information {
938 $main::lxdebug->enter_sub();
943 Common::check_params_x(\%params, qw(packed));
947 eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
949 $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
951 foreach my $entry (@{ $unpacked }) {
952 next if ('HASH' eq ref $entry);
957 $main::lxdebug->leave_sub();
962 sub get_item_availability {
963 $::lxdebug->enter_sub;
968 Common::check_params(\%params, qw(parts_id));
970 my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
973 qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, SUM(qty) AS qty, i.parts_id,
974 w.description AS warehousedescription,
975 b.description AS bindescription
977 LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
978 LEFT JOIN bin b ON (i.bin_id = b.id)
979 WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
980 GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, i.parts_id, w.description, b.description
982 ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber), i.bestbefore
984 my $contents = selectall_hashref_query($::form, $::form->get_standard_dbh, $query, @parts_ids);
986 $::lxdebug->leave_sub;
988 return @{ $contents };
992 sub check_stock_availability {
993 $main::lxdebug->enter_sub();
998 Common::check_params(\%params, qw(requests parts_id));
1000 my $myconfig = \%main::myconfig;
1001 my $form = $main::form;
1003 my $dbh = $form->get_standard_dbh($myconfig);
1005 my $units = AM->retrieve_units($myconfig, $form);
1007 my ($partunit) = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
1008 my $unit_factor = $units->{$partunit}->{factor} || 1;
1010 my @contents = $self->get_item_availability(%params);
1014 foreach my $sinfo (@{ $params{requests} }) {
1017 foreach my $row (@contents) {
1018 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
1019 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1020 ($row->{chargenumber} ne $sinfo->{chargenumber}) ||
1021 ($row->{bestbefore} ne $sinfo->{bestbefore}));
1025 my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
1027 if ($base_qty > $row->{qty}) {
1028 $sinfo->{error} = 1;
1029 push @errors, $sinfo;
1035 push @errors, $sinfo if (!$found);
1038 $main::lxdebug->leave_sub();
1043 sub transfer_in_out {
1044 $main::lxdebug->enter_sub();
1049 Common::check_params(\%params, qw(direction requests));
1051 if (!@{ $params{requests} }) {
1052 $main::lxdebug->leave_sub();
1056 my $myconfig = \%main::myconfig;
1057 my $form = $main::form;
1059 my $prefix = $params{direction} eq 'in' ? 'dst' : 'src';
1063 foreach my $request (@{ $params{requests} }) {
1065 'parts_id' => $request->{parts_id},
1066 "${prefix}_warehouse_id" => $request->{warehouse_id},
1067 "${prefix}_bin_id" => $request->{bin_id},
1068 'chargenumber' => $request->{chargenumber},
1069 'bestbefore' => $request->{bestbefore},
1070 'qty' => $request->{qty},
1071 'unit' => $request->{unit},
1072 'oe_id' => $form->{id},
1073 'shippingdate' => 'current_date',
1074 'transfer_type' => $params{direction} eq 'in' ? 'stock' : 'shipped',
1075 'project_id' => $request->{project_id},
1079 WH->transfer(@transfers);
1081 $main::lxdebug->leave_sub();
1084 sub get_shipped_qty {
1085 $main::lxdebug->enter_sub();
1090 Common::check_params(\%params, qw(type oe_id));
1092 my $myconfig = \%main::myconfig;
1093 my $form = $main::form;
1095 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1097 my @links = RecordLinks->get_links('dbh' => $dbh,
1098 'from_table' => 'oe',
1099 'from_id' => $params{oe_id},
1100 'to_table' => 'delivery_orders');
1101 my @values = map { $_->{to_id} } @links;
1103 if (!scalar @values) {
1104 $main::lxdebug->leave_sub();
1109 qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit
1110 FROM delivery_order_items doi
1111 LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id)
1112 LEFT JOIN parts p ON (doi.parts_id = p.id)
1113 WHERE o.id IN (| . join(', ', ('?') x scalar @values) . qq|)|;
1116 my $entries = selectall_hashref_query($form, $dbh, $query, @values);
1117 my $all_units = AM->retrieve_all_units();
1119 foreach my $entry (@{ $entries }) {
1120 $entry->{qty} *= AM->convert_unit($entry->{unit}, $entry->{partunit}, $all_units);
1122 if (!$ship{$entry->{parts_id}}) {
1123 $ship{$entry->{parts_id}} = $entry;
1125 $ship{$entry->{parts_id}}->{qty} += $entry->{qty};
1129 $main::lxdebug->leave_sub();
1134 sub is_marked_as_delivered {
1135 $main::lxdebug->enter_sub();
1140 Common::check_params(\%params, qw(id));
1142 my $myconfig = \%main::myconfig;
1143 my $form = $main::form;
1145 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1147 my ($delivered) = selectfirst_array_query($form, $dbh, qq|SELECT delivered FROM delivery_orders WHERE id = ?|, conv_i($params{id}));
1149 $main::lxdebug->leave_sub();
1151 return $delivered ? 1 : 0;