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);
42 use SL::DB::DeliveryOrder;
45 use SL::HTML::Restrict;
53 $main::lxdebug->enter_sub();
57 my $myconfig = \%main::myconfig;
58 my $form = $main::form;
61 my $dbh = $form->get_standard_dbh($myconfig);
63 my (@where, @values, $where);
65 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
68 qq|SELECT dord.id, dord.donumber, dord.ordnumber, dord.cusordnumber,
69 dord.transdate, dord.reqdate,
70 ct.${vc}number, ct.name, ct.business_id,
71 dord.${vc}_id, dord.globalproject_id,
72 dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
73 dord.transaction_description, dord.itime::DATE AS insertdate,
74 pr.projectnumber AS globalprojectnumber,
75 dep.description AS department,
78 FROM delivery_orders dord
79 LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
80 LEFT JOIN contacts cp ON (dord.cp_id = cp.cp_id)
81 LEFT JOIN employee e ON (dord.employee_id = e.id)
82 LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
83 LEFT JOIN project pr ON (dord.globalproject_id = pr.id)
84 LEFT JOIN department dep ON (dord.department_id = dep.id)
87 push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
89 if ($form->{department_id}) {
90 push @where, qq|dord.department_id = ?|;
91 push @values, conv_i($form->{department_id});
94 if ($form->{project_id}) {
96 qq|(dord.globalproject_id = ?) OR EXISTS
97 (SELECT * FROM delivery_order_items doi
98 WHERE (doi.project_id = ?) AND (doi.delivery_order_id = dord.id))|;
99 push @values, conv_i($form->{project_id}), conv_i($form->{project_id});
102 if ($form->{"business_id"}) {
103 push @where, qq|ct.business_id = ?|;
104 push @values, conv_i($form->{"business_id"});
107 if ($form->{"${vc}_id"}) {
108 push @where, qq|dord.${vc}_id = ?|;
109 push @values, $form->{"${vc}_id"};
111 } elsif ($form->{$vc}) {
112 push @where, qq|ct.name ILIKE ?|;
113 push @values, '%' . $form->{$vc} . '%';
116 if ($form->{"cp_name"}) {
117 push @where, "(cp.cp_name ILIKE ? OR cp.cp_givenname ILIKE ?)";
118 push @values, ('%' . $form->{"cp_name"} . '%')x2;
121 foreach my $item (qw(employee_id salesman_id)) {
122 next unless ($form->{$item});
123 push @where, "dord.$item = ?";
124 push @values, conv_i($form->{$item});
126 if (!$main::auth->assert('sales_all_edit', 1)) {
127 push @where, qq|dord.employee_id = (select id from employee where login= ?)|;
128 push @values, $::myconfig{login};
131 foreach my $item (qw(donumber ordnumber cusordnumber transaction_description)) {
132 next unless ($form->{$item});
133 push @where, qq|dord.$item ILIKE ?|;
134 push @values, '%' . $form->{$item} . '%';
137 if (($form->{open} || $form->{closed}) &&
138 ($form->{open} ne $form->{closed})) {
139 push @where, ($form->{open} ? "NOT " : "") . "COALESCE(dord.closed, FALSE)";
142 if (($form->{notdelivered} || $form->{delivered}) &&
143 ($form->{notdelivered} ne $form->{delivered})) {
144 push @where, ($form->{delivered} ? "" : "NOT ") . "COALESCE(dord.delivered, FALSE)";
147 if ($form->{serialnumber}) {
148 push @where, 'dord.id IN (SELECT doi.delivery_order_id FROM delivery_order_items doi WHERE doi.serialnumber LIKE ?)';
149 push @values, '%' . $form->{serialnumber} . '%';
152 if($form->{transdatefrom}) {
153 push @where, qq|dord.transdate >= ?|;
154 push @values, conv_date($form->{transdatefrom});
157 if($form->{transdateto}) {
158 push @where, qq|dord.transdate <= ?|;
159 push @values, conv_date($form->{transdateto});
162 if($form->{reqdatefrom}) {
163 push @where, qq|dord.reqdate >= ?|;
164 push @values, conv_date($form->{reqdatefrom});
167 if($form->{reqdateto}) {
168 push @where, qq|dord.reqdate <= ?|;
169 push @values, conv_date($form->{reqdateto});
172 if($form->{insertdatefrom}) {
173 push @where, qq|dord.itime::DATE >= ?|;
174 push@values, conv_date($form->{insertdatefrom});
177 if($form->{insertdateto}) {
178 push @where, qq|dord.itime::DATE <= ?|;
179 push @values, conv_date($form->{insertdateto});
183 $query .= " WHERE " . join(" AND ", map { "($_)" } @where);
186 my %allowed_sort_columns = (
187 "transdate" => "dord.transdate",
188 "reqdate" => "dord.reqdate",
190 "donumber" => "dord.donumber",
191 "ordnumber" => "dord.ordnumber",
193 "employee" => "e.name",
194 "salesman" => "sm.name",
195 "shipvia" => "dord.shipvia",
196 "transaction_description" => "dord.transaction_description",
197 "department" => "lower(dep.description)",
198 "insertdate" => "dord.itime",
201 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
202 my $sortorder = "dord.id";
203 if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
204 $sortorder = $allowed_sort_columns{$form->{sort}};
207 $query .= qq| ORDER by | . $sortorder . " $sortdir";
209 $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
211 if (scalar @{ $form->{DO} }) {
215 WHERE NOT COALESCE(quotation, FALSE)
217 AND (COALESCE(${vc}_id, 0) != 0)|;
219 my $sth = prepare_query($form, $dbh, $query);
221 foreach my $dord (@{ $form->{DO} }) {
222 next unless ($dord->{ordnumber});
223 do_statement($form, $sth, $query, $dord->{ordnumber});
224 ($dord->{oe_id}) = $sth->fetchrow_array();
230 $main::lxdebug->leave_sub();
234 $main::lxdebug->enter_sub();
238 my $myconfig = \%main::myconfig;
239 my $form = $main::form;
241 # connect to database, turn off autocommit
242 my $dbh = $form->get_standard_dbh($myconfig);
243 my $restricter = SL::HTML::Restrict->create;
245 my ($query, @values, $sth, $null);
247 my $all_units = AM->retrieve_units($myconfig, $form);
248 $form->{all_units} = $all_units;
250 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
253 my $trans_number = SL::TransNumber->new(type => $form->{type}, dbh => $dbh, number => $form->{donumber}, id => $form->{id});
254 $form->{donumber} ||= $trans_number->create_unique;
255 $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
256 $form->get_employee($dbh) unless ($form->{employee_id});
258 my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
260 my (@processed_doi, @processed_dois);
264 # only delete shipto complete
265 $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
266 do_query($form, $dbh, $query, conv_i($form->{id}));
270 $query = qq|SELECT nextval('id')|;
271 ($form->{id}) = selectrow_query($form, $dbh, $query);
273 $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id, currency_id, taxzone_id) VALUES (?, '', ?, (SELECT currency_id FROM defaults LIMIT 1), ?)|;
274 do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}), $form->{taxzone_id});
280 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
281 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
284 my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
285 my @part_ids = keys %part_id_map;
289 $query = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
290 %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
293 UPDATE delivery_order_items SET
294 delivery_order_id = ?, position = ?, parts_id = ?, description = ?, longdescription = ?, qty = ?, base_qty = ?,
295 sellprice = ?, discount = ?, unit = ?, reqdate = ?, project_id = ?, serialnumber = ?,
296 ordnumber = ?, transdate = ?, cusordnumber = ?,
297 lastcost = ? , price_factor_id = ?, price_factor = (SELECT factor FROM price_factors where id = ?),
298 marge_price_factor = ?, pricegroup_id = ?, active_price_source = ?, active_discount_source = ?
301 my $h_item = prepare_query($form, $dbh, $q_item);
303 my $q_item_stock = <<SQL;
304 UPDATE delivery_order_items_stock SET
305 delivery_order_item_id = ?, qty = ?, unit = ?, warehouse_id = ?,
306 bin_id = ?, chargenumber = ?, bestbefore = ?
309 my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
311 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
313 for my $i (1 .. $form->{rowcount}) {
314 next if (!$form->{"id_$i"});
316 CVar->get_non_editable_ic_cvars(form => $form,
319 sub_module => 'delivery_order_items',
320 may_converted_from => ['orderitems', 'delivery_order_items']);
324 if (!$form->{"delivery_order_items_id_$i"}) {
325 # there is no persistent id, therefore create one with all necessary constraints
326 my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
327 my $h_item_id = prepare_query($form, $dbh, $q_item_id);
328 do_statement($form, $h_item_id, $q_item_id);
329 $form->{"delivery_order_items_id_$i"} = $h_item_id->fetchrow_array();
330 $query = qq|INSERT INTO delivery_order_items (id, delivery_order_id, position, parts_id) VALUES (?, ?, ?, ?)|;
331 do_query($form, $dbh, $query, conv_i($form->{"delivery_order_items_id_$i"}),
332 conv_i($form->{"id"}), conv_i($position), conv_i($form->{"id_$i"}));
333 $h_item_id->finish();
336 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
338 my $item_unit = $part_unit_map{$form->{"id_$i"}};
341 if (defined($all_units->{$item_unit}->{factor}) && (($all_units->{$item_unit}->{factor} * 1) != 0)) {
342 $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
344 my $baseqty = $form->{"qty_$i"} * $basefactor;
346 # set values to 0 if nothing entered
347 $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"});
348 $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
349 $form->{"lastcost_$i"} = $form->parse_amount($myconfig, $form->{"lastcost_$i"});
351 $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
352 my $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
354 $items_reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
357 # Get pricegroup_id and save it. Unfortunately the interface
358 # also uses ID "0" for signalling that none is selected, but "0"
359 # must not be stored in the database. Therefore we cannot simply
361 my $pricegroup_id = $form->{"pricegroup_id_$i"} * 1;
362 $pricegroup_id = undef if !$pricegroup_id;
364 # save detail record in delivery_order_items table
365 @values = (conv_i($form->{id}), conv_i($position), conv_i($form->{"id_$i"}),
366 $form->{"description_$i"}, $restricter->process($form->{"longdescription_$i"}),
367 $form->{"qty_$i"}, $baseqty,
368 $form->{"sellprice_$i"}, $form->{"discount_$i"} / 100,
369 $form->{"unit_$i"}, conv_date($items_reqdate), conv_i($form->{"project_id_$i"}),
370 $form->{"serialnumber_$i"},
371 $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
372 $form->{"cusordnumber_$i"},
373 $form->{"lastcost_$i"},
374 conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
375 conv_i($form->{"marge_price_factor_$i"}),
377 $form->{"active_price_source_$i"}, $form->{"active_discount_source_$i"},
378 conv_i($form->{"delivery_order_items_id_$i"}));
379 do_statement($form, $h_item, $q_item, @values);
380 push @processed_doi, $form->{"delivery_order_items_id_$i"}; # transaction safe?
382 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
384 foreach my $sinfo (@{ $stock_info }) {
385 # if we have stock_info, we have to check for persistents entries
386 if (!$sinfo->{"delivery_order_items_stock_id"}) {
387 my $q_item_stock_id = qq|SELECT nextval('id')|;
388 my $h_item_stock_id = prepare_query($form, $dbh, $q_item_stock_id);
389 do_statement($form, $h_item_stock_id, $q_item_stock_id);
390 $sinfo->{"delivery_order_items_stock_id"} = $h_item_stock_id->fetchrow_array();
391 $query = qq|INSERT INTO delivery_order_items_stock (id, delivery_order_item_id, qty, unit, warehouse_id, bin_id)
392 VALUES (?, ?, ?, ?, ?, ?)|;
393 do_query($form, $dbh, $query, conv_i($sinfo->{"delivery_order_items_stock_id"}),
394 conv_i($form->{"delivery_order_items_id_$i"}), $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
395 conv_i($sinfo->{bin_id}));
396 $h_item_stock_id->finish();
397 # write back the id to the form (important if only transfer was clicked (id fk for invoice)
398 $form->{"stock_${in_out}_$i"} = YAML::Dump($stock_info);
400 @values = ($form->{"delivery_order_items_id_$i"}, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
401 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber}, conv_date($sinfo->{bestbefore}),
402 conv_i($sinfo->{"delivery_order_items_stock_id"}));
403 do_statement($form, $h_item_stock, $q_item_stock, @values);
404 push @processed_dois, $sinfo->{"delivery_order_items_stock_id"};
407 CVar->save_custom_variables(module => 'IC',
408 sub_module => 'delivery_order_items',
409 trans_id => $form->{"delivery_order_items_id_$i"},
410 configs => $ic_cvar_configs,
412 name_prefix => 'ic_',
413 name_postfix => "_$i",
416 # link order items with doi, for future extension look at foreach IS.pm
417 if (!$form->{saveasnew} && $form->{"converted_from_orderitems_id_$i"}) {
418 RecordLinks->create_links('dbh' => $dbh,
420 'from_table' => 'orderitems',
421 'from_ids' => $form->{"converted_from_orderitems_id_$i"},
422 'to_table' => 'delivery_order_items',
423 'to_id' => $form->{"delivery_order_items_id_$i"},
426 delete $form->{"converted_from_orderitems_id_$i"};
429 # 1. search for orphaned dois; processed_dois may be empty (no transfer) TODO: be supersafe and alter same statement for doi and oi
430 $query = sprintf 'SELECT id FROM delivery_order_items_stock WHERE delivery_order_item_id in
431 (select id from delivery_order_items where delivery_order_id = ?)';
432 $query .= sprintf ' AND NOT id IN (%s)', join ', ', ("?") x scalar @processed_dois if (scalar @processed_dois);
433 @values = (conv_i($form->{id}), map { conv_i($_) } @processed_dois);
434 my @orphaned_dois_ids = map { $_->{id} } selectall_hashref_query($form, $dbh, $query, @values);
435 if (scalar @orphaned_dois_ids) {
436 # clean up delivery_order_items_stock
437 $query = sprintf 'DELETE FROM delivery_order_items_stock WHERE id IN (%s)', join ', ', ("?") x scalar @orphaned_dois_ids;
438 do_query($form, $dbh, $query, @orphaned_dois_ids);
440 # 2. search for orphaned doi
441 $query = sprintf 'SELECT id FROM delivery_order_items WHERE delivery_order_id = ? AND NOT id IN (%s)', join ', ', ("?") x scalar @processed_doi;
442 @values = (conv_i($form->{id}), map { conv_i($_) } @processed_doi);
443 my @orphaned_ids = map { $_->{id} } selectall_hashref_query($form, $dbh, $query, @values);
444 if (scalar @orphaned_ids) {
445 # clean up delivery_order_items
446 $query = sprintf 'DELETE FROM delivery_order_items WHERE id IN (%s)', join ', ', ("?") x scalar @orphaned_ids;
447 do_query($form, $dbh, $query, @orphaned_ids);
450 $h_item_stock->finish();
453 # reqdate is last items reqdate (?: old behaviour) if not already set
454 $form->{reqdate} ||= $items_reqdate;
457 qq|UPDATE delivery_orders SET
458 donumber = ?, ordnumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
459 customer_id = ?, reqdate = ?,
460 shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
461 delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
462 globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
463 is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, currency_id = (SELECT id FROM currencies WHERE name = ?),
467 @values = ($form->{donumber}, $form->{ordnumber},
468 $form->{cusordnumber}, conv_date($form->{transdate}),
469 conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
470 conv_date($form->{reqdate}), $form->{shippingpoint}, $form->{shipvia},
471 $restricter->process($form->{notes}), $form->{intnotes},
472 $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
473 conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
474 conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
475 conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
476 $form->{transaction_description},
477 $form->{type} =~ /^sales/ ? 't' : 'f',
478 conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), $form->{currency},
479 conv_i($form->{delivery_term_id}),
480 conv_i($form->{id}));
481 do_query($form, $dbh, $query, @values);
483 $form->{name} = $form->{ $form->{vc} };
484 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
487 if (!$form->{shipto_id}) {
488 $form->add_shipto($dbh, $form->{id}, "DO");
491 # save printed, emailed, queued
492 $form->save_status($dbh);
494 # Link this delivery order to the quotations it was created from.
495 RecordLinks->create_links('dbh' => $dbh,
497 'from_table' => 'oe',
498 'from_ids' => $form->{convert_from_oe_ids},
499 'to_table' => 'delivery_orders',
500 'to_id' => $form->{id},
502 delete $form->{convert_from_oe_ids};
504 $self->mark_orders_if_delivered('do_id' => $form->{id},
505 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
508 my $rc = $dbh->commit();
510 $form->{saved_donumber} = $form->{donumber};
511 $form->{saved_ordnumber} = $form->{ordnumber};
512 $form->{saved_cusordnumber} = $form->{cusordnumber};
514 Common::webdav_folder($form);
516 $main::lxdebug->leave_sub();
521 sub mark_orders_if_delivered {
522 $main::lxdebug->enter_sub();
527 Common::check_params(\%params, qw(do_id type));
529 my $myconfig = \%main::myconfig;
530 my $form = $main::form;
532 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
534 my @links = RecordLinks->get_links('dbh' => $dbh,
535 'from_table' => 'oe',
536 'to_table' => 'delivery_orders',
537 'to_id' => $params{do_id});
539 my $oe_id = @links ? $links[0]->{from_id} : undef;
541 return $main::lxdebug->leave_sub() if (!$oe_id);
543 my $all_units = AM->retrieve_all_units();
545 my $query = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit
547 LEFT JOIN parts p ON (oi.parts_id = p.id)
548 WHERE (oi.trans_id = ?)|;
549 my $sth = prepare_execute_query($form, $dbh, $query, $oe_id);
551 my %shipped = $self->get_shipped_qty('type' => $params{type},
555 while (my $ref = $sth->fetchrow_hashref()) {
556 $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor};
558 if ($ordered{$ref->{parts_id}}) {
559 $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty};
561 $ordered{$ref->{parts_id}} = $ref;
567 map { $_->{baseqty} = $_->{qty} * $all_units->{$_->{unit}}->{factor} / $all_units->{$_->{partunit}}->{factor} } values %shipped;
570 foreach my $part (values %ordered) {
571 if (!$shipped{$part->{parts_id}} || ($shipped{$part->{parts_id}}->{baseqty} < $part->{baseqty})) {
578 $query = qq|UPDATE oe
581 do_query($form, $dbh, $query, $oe_id);
582 $dbh->commit() if (!$params{dbh});
585 $main::lxdebug->leave_sub();
589 $main::lxdebug->enter_sub();
594 Common::check_params(\%params, qw(ids));
596 if (('ARRAY' ne ref $params{ids}) || !scalar @{ $params{ids} }) {
597 $main::lxdebug->leave_sub();
601 my $myconfig = \%main::myconfig;
602 my $form = $main::form;
604 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
606 my $query = qq|UPDATE delivery_orders SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar(@{ $params{ids} })) . qq|)|;
608 do_query($form, $dbh, $query, map { conv_i($_) } @{ $params{ids} });
610 $dbh->commit() unless ($params{dbh});
612 $main::lxdebug->leave_sub();
616 $main::lxdebug->enter_sub();
620 my $myconfig = \%main::myconfig;
621 my $form = $main::form;
622 my $spool = $::lx_office_conf{paths}->{spool};
624 my $rc = SL::DB::Order->new->db->with_transaction(sub {
625 my @spoolfiles = grep { $_ } map { $_->spoolfile } @{ SL::DB::Manager::Status->get_all(where => [ trans_id => $form->{id} ]) };
627 SL::DB::DeliveryOrder->new(id => $form->{id})->delete;
629 my $spool = $::lx_office_conf{paths}->{spool};
630 unlink map { "$spool/$_" } @spoolfiles if $spool;
635 $main::lxdebug->leave_sub();
641 $main::lxdebug->enter_sub();
646 my $myconfig = \%main::myconfig;
647 my $form = $main::form;
649 # connect to database
650 my $dbh = $form->get_standard_dbh($myconfig);
652 my ($query, $query_add, @values, $sth, $ref);
654 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
657 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
659 my $mode = !$params{ids} ? 'default' : ref $params{ids} eq 'ARRAY' ? 'multi' : 'single';
661 if ($mode eq 'default') {
662 $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate|);
663 map { $form->{$_} = $ref->{$_} } keys %$ref;
665 # if reqdate is not set from oe-workflow, set it to transdate (which is current date)
666 $form->{reqdate} ||= $form->{transdate};
669 $form->lastname_used($dbh, $myconfig, $vc) unless $form->{"${vc}_id"};
671 $main::lxdebug->leave_sub();
676 my @do_ids = map { conv_i($_) } ($mode eq 'multi' ? @{ $params{ids} } : ($params{ids}));
677 my $do_ids_placeholders = join(', ', ('?') x scalar(@do_ids));
679 # retrieve order for single id
680 # NOTE: this query is intended to fetch all information only ONCE.
681 # so if any of these infos is important (or even different) for any item,
682 # it will be killed out and then has to be fetched from the item scope query further down
684 qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.transdate, dord.reqdate,
685 dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
686 e.name AS employee, dord.employee_id, dord.salesman_id,
687 dord.${vc}_id, cv.name AS ${vc},
688 dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
689 d.description AS department, dord.language_id,
691 dord.globalproject_id, dord.delivered, dord.transaction_description,
692 dord.taxzone_id, dord.taxincluded, dord.terms, (SELECT cu.name FROM currencies cu WHERE cu.id=dord.currency_id) AS currency,
693 dord.delivery_term_id, dord.itime::DATE AS insertdate
694 FROM delivery_orders dord
695 JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
696 LEFT JOIN employee e ON (dord.employee_id = e.id)
697 LEFT JOIN department d ON (dord.department_id = d.id)
698 WHERE dord.id IN ($do_ids_placeholders)|;
699 $sth = prepare_execute_query($form, $dbh, $query, @do_ids);
701 delete $form->{"${vc}_id"};
703 $form->{ordnumber_array} = ' ';
704 $form->{cusordnumber_array} = ' ';
705 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
706 if ($form->{"${vc}_id"} && ($ref->{"${vc}_id"} != $form->{"${vc}_id"})) {
708 $main::lxdebug->leave_sub();
713 map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
714 $form->{donumber_array} .= $form->{donumber} . ' ';
715 $pos = index($form->{ordnumber_array},' ' . $form->{ordnumber} . ' ');
717 $form->{ordnumber_array} .= $form->{ordnumber} . ' ';
719 $pos = index($form->{cusordnumber_array},' ' . $form->{cusordnumber} . ' ');
721 $form->{cusordnumber_array} .= $form->{cusordnumber} . ' ';
726 $form->{donumber_array} =~ s/\s*$//g;
727 $form->{ordnumber_array} =~ s/ //;
728 $form->{ordnumber_array} =~ s/\s*$//g;
729 $form->{cusordnumber_array} =~ s/ //;
730 $form->{cusordnumber_array} =~ s/\s*$//g;
732 $form->{saved_donumber} = $form->{donumber};
733 $form->{saved_ordnumber} = $form->{ordnumber};
734 $form->{saved_cusordnumber} = $form->{cusordnumber};
736 # if not given, fill transdate with current_date
737 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
739 if ($mode eq 'single') {
740 $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'DO'|;
741 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
743 $ref = $sth->fetchrow_hashref("NAME_lc");
745 map { $form->{$_} = $ref->{$_} } keys %$ref;
748 # get printed, emailed and queued
749 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
750 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
752 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
753 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
754 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
755 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
758 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
764 # retrieve individual items
765 # this query looks up all information about the items
766 # stuff different from the whole will not be overwritten, but saved with a suffix.
768 qq|SELECT doi.id AS delivery_order_items_id,
769 p.partnumber, p.assembly, p.listprice, doi.description, doi.qty,
770 doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.notes AS partnotes,
771 doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
772 doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
773 doi.price_factor_id, doi.price_factor, doi.marge_price_factor, doi.pricegroup_id,
774 doi.active_price_source, doi.active_discount_source,
775 pr.projectnumber, dord.transdate AS dord_transdate, dord.donumber,
777 FROM delivery_order_items doi
778 JOIN parts p ON (doi.parts_id = p.id)
779 JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
780 LEFT JOIN project pr ON (doi.project_id = pr.id)
781 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
782 WHERE doi.delivery_order_id IN ($do_ids_placeholders)
783 ORDER BY doi.delivery_order_id, doi.position|;
785 $form->{form_details} = selectall_hashref_query($form, $dbh, $query, @do_ids);
787 # Retrieve custom variables.
788 foreach my $doi (@{ $form->{form_details} }) {
789 my $cvars = CVar->get_custom_variables(dbh => $dbh,
791 sub_module => 'delivery_order_items',
792 trans_id => $doi->{delivery_order_items_id},
794 map { $doi->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
797 if ($mode eq 'single') {
798 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
801 qq|SELECT id as delivery_order_items_stock_id, qty, unit, bin_id,
802 warehouse_id, chargenumber, bestbefore
803 FROM delivery_order_items_stock
804 WHERE delivery_order_item_id = ?|;
805 my $sth = prepare_query($form, $dbh, $query);
807 foreach my $doi (@{ $form->{form_details} }) {
808 do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
810 while (my $ref = $sth->fetchrow_hashref()) {
811 push @{ $requests }, $ref;
814 $doi->{"stock_${in_out}"} = YAML::Dump($requests);
820 Common::webdav_folder($form);
822 $main::lxdebug->leave_sub();
828 $main::lxdebug->enter_sub();
830 my ($self, $myconfig, $form) = @_;
832 # connect to database
833 my $dbh = $form->get_standard_dbh($myconfig);
842 my $subtotal_header = 0;
848 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
850 # sort items by partsgroup
851 for $i (1 .. $form->{rowcount}) {
853 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
854 $partsgroup = $form->{"partsgroup_$i"};
856 push @partsgroup, [$i, $partsgroup];
857 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
863 $projects = SL::DB::Manager::Project->get_all(query => [ id => \@project_ids ]);
864 %projects_by_id = map { $_->id => $_ } @$projects;
867 if ($projects_by_id{$form->{"globalproject_id"}}) {
868 $form->{globalprojectnumber} = $projects_by_id{$form->{"globalproject_id"}}->projectnumber;
869 $form->{globalprojectdescription} = $projects_by_id{$form->{"globalproject_id"}}->description;
871 for (@{ $projects_by_id{$form->{"globalproject_id"}}->cvars_by_config }) {
872 $form->{"project_cvar_" . $_->config->name} = $_->value_as_text;
876 my $q_pg = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
878 JOIN parts p ON (a.parts_id = p.id)
879 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
882 my $h_pg = prepare_query($form, $dbh, $q_pg);
884 my $q_bin_wh = qq|SELECT (SELECT description FROM bin WHERE id = ?) AS bin,
885 (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
886 my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
888 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
892 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
893 my $project_cvar_configs = CVar->get_configs(module => 'Projects');
895 $form->{TEMPLATE_ARRAYS} = { };
896 IC->prepare_parts_for_printing(myconfig => $myconfig, form => $form);
899 qw(runningnumber number description longdescription qty unit
900 partnotes serialnumber reqdate projectnumber projectdescription
902 si_runningnumber si_number si_description
903 si_warehouse si_bin si_chargenumber si_bestbefore
904 si_qty si_qty_nofmt si_unit);
906 map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays);
908 push @arrays, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
909 push @arrays, map { "project_cvar_$_->{name}" } @{ $project_cvar_configs };
911 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
912 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
916 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
919 next if (!$form->{"id_$i"});
921 if ($item->[1] ne $sameitem) {
922 push(@{ $form->{TEMPLATE_ARRAYS}->{description} }, qq|$item->[1]|);
923 $sameitem = $item->[1];
925 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" && $_ !~ /^si_/} @arrays));
926 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, []) } grep({ $_ =~ /^si_/} @arrays));
930 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
932 # add number, description and qty to $form->{number}, ....
933 if ($form->{"subtotal_$i"} && !$subtotal_header) {
934 $subtotal_header = $i;
935 $position = int($position);
938 } elsif ($subtotal_header) {
940 $position = int($position);
941 $position = $position.".".$subposition;
943 $position = int($position);
949 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
950 my $project = $projects_by_id{$form->{"project_id_$i"}} || SL::DB::Project->new;
952 push @{ $form->{TEMPLATE_ARRAYS}{runningnumber} }, $position;
953 push @{ $form->{TEMPLATE_ARRAYS}{number} }, $form->{"partnumber_$i"};
954 push @{ $form->{TEMPLATE_ARRAYS}{description} }, $form->{"description_$i"};
955 push @{ $form->{TEMPLATE_ARRAYS}{longdescription} }, $form->{"longdescription_$i"};
956 push @{ $form->{TEMPLATE_ARRAYS}{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"});
957 push @{ $form->{TEMPLATE_ARRAYS}{qty_nofmt} }, $form->{"qty_$i"};
958 push @{ $form->{TEMPLATE_ARRAYS}{unit} }, $form->{"unit_$i"};
959 push @{ $form->{TEMPLATE_ARRAYS}{partnotes} }, $form->{"partnotes_$i"};
960 push @{ $form->{TEMPLATE_ARRAYS}{serialnumber} }, $form->{"serialnumber_$i"};
961 push @{ $form->{TEMPLATE_ARRAYS}{reqdate} }, $form->{"reqdate_$i"};
962 push @{ $form->{TEMPLATE_ARRAYS}{projectnumber} }, $project->projectnumber;
963 push @{ $form->{TEMPLATE_ARRAYS}{projectdescription} }, $project->description;
965 if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
966 $subtotal_header = 0;
969 my $lineweight = $form->{"qty_$i"} * $form->{"weight_$i"};
970 $totalweight += $lineweight;
971 push @{ $form->{TEMPLATE_ARRAYS}->{weight} }, $form->format_amount($myconfig, $form->{"weight_$i"}, 3);
972 push @{ $form->{TEMPLATE_ARRAYS}->{weight_nofmt} }, $form->{"weight_$i"};
973 push @{ $form->{TEMPLATE_ARRAYS}->{lineweight} }, $form->format_amount($myconfig, $lineweight, 3);
974 push @{ $form->{TEMPLATE_ARRAYS}->{lineweight_nofmt} }, $lineweight;
976 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
978 foreach my $si (@{ $stock_info }) {
981 do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
982 my $bin_wh = $h_bin_wh->fetchrow_hashref();
984 push @{ $form->{TEMPLATE_ARRAYS}{si_runningnumber}[$si_position-1] }, $num_si;
985 push @{ $form->{TEMPLATE_ARRAYS}{si_number}[$si_position-1] }, $form->{"partnumber_$i"};
986 push @{ $form->{TEMPLATE_ARRAYS}{si_description}[$si_position-1] }, $form->{"description_$i"};
987 push @{ $form->{TEMPLATE_ARRAYS}{si_warehouse}[$si_position-1] }, $bin_wh->{warehouse};
988 push @{ $form->{TEMPLATE_ARRAYS}{si_bin}[$si_position-1] }, $bin_wh->{bin};
989 push @{ $form->{TEMPLATE_ARRAYS}{si_chargenumber}[$si_position-1] }, $si->{chargenumber};
990 push @{ $form->{TEMPLATE_ARRAYS}{si_bestbefore}[$si_position-1] }, $si->{bestbefore};
991 push @{ $form->{TEMPLATE_ARRAYS}{si_qty}[$si_position-1] }, $form->format_amount($myconfig, $si->{qty} * 1);
992 push @{ $form->{TEMPLATE_ARRAYS}{si_qty_nofmt}[$si_position-1] }, $si->{qty} * 1;
993 push @{ $form->{TEMPLATE_ARRAYS}{si_unit}[$si_position-1] }, $si->{unit};
996 if ($form->{"assembly_$i"}) {
999 # get parts and push them onto the stack
1001 if ($form->{groupitems}) {
1003 qq|ORDER BY pg.partsgroup, a.oid|;
1005 $sortorder = qq|ORDER BY a.oid|;
1008 do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
1010 while (my $ref = $h_pg->fetchrow_hashref("NAME_lc")) {
1011 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
1012 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" && $_ !~ /^si_/} @arrays));
1013 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, []) } grep({ $_ =~ /^si_/} @arrays));
1014 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
1015 push(@{ $form->{TEMPLATE_ARRAYS}->{description} }, $sameitem);
1019 push(@{ $form->{TEMPLATE_ARRAYS}->{"description"} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq| -- $ref->{partnumber}, $ref->{description}|);
1020 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" && $_ !~ /^si_/} @arrays));
1021 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, []) } grep({ $_ =~ /^si_/} @arrays));
1026 CVar->get_non_editable_ic_cvars(form => $form,
1029 sub_module => 'delivery_order_items',
1030 may_converted_from => ['orderitems', 'delivery_order_items']);
1032 push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
1033 CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
1034 for @{ $ic_cvar_configs };
1036 push @{ $form->{TEMPLATE_ARRAYS}->{"project_cvar_" . $_->config->name} }, $_->value_as_text for @{ $project->cvars_by_config };
1039 $form->{totalweight} = $form->format_amount($myconfig, $totalweight, 3);
1040 $form->{totalweight_nofmt} = $totalweight;
1041 my $defaults = AM->get_defaults();
1042 $form->{weightunit} = $defaults->{weightunit};
1045 $h_bin_wh->finish();
1047 $form->{delivery_term} = SL::DB::Manager::DeliveryTerm->find_by(id => $form->{delivery_term_id} || undef);
1048 $form->{delivery_term}->description_long($form->{delivery_term}->translated_attribute('description_long', $form->{language_id})) if $form->{delivery_term} && $form->{language_id};
1050 $form->{username} = $myconfig->{name};
1052 $main::lxdebug->leave_sub();
1055 sub project_description {
1056 $main::lxdebug->enter_sub();
1058 my ($self, $dbh, $id) = @_;
1060 my $form = $main::form;
1062 my $query = qq|SELECT description FROM project WHERE id = ?|;
1063 my ($value) = selectrow_query($form, $dbh, $query, $id);
1065 $main::lxdebug->leave_sub();
1070 sub unpack_stock_information {
1071 $main::lxdebug->enter_sub();
1076 Common::check_params_x(\%params, qw(packed));
1080 eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
1082 $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
1084 foreach my $entry (@{ $unpacked }) {
1085 next if ('HASH' eq ref $entry);
1090 $main::lxdebug->leave_sub();
1095 sub get_item_availability {
1096 $::lxdebug->enter_sub;
1101 Common::check_params(\%params, qw(parts_id));
1103 my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
1106 qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, SUM(qty) AS qty, i.parts_id,
1107 w.description AS warehousedescription,
1108 b.description AS bindescription
1110 LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
1111 LEFT JOIN bin b ON (i.bin_id = b.id)
1112 WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
1113 GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, i.parts_id, w.description, b.description
1115 ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber), i.bestbefore
1117 my $contents = selectall_hashref_query($::form, $::form->get_standard_dbh, $query, @parts_ids);
1119 $::lxdebug->leave_sub;
1121 return @{ $contents };
1125 sub check_stock_availability {
1126 $main::lxdebug->enter_sub();
1131 Common::check_params(\%params, qw(requests parts_id));
1133 my $myconfig = \%main::myconfig;
1134 my $form = $main::form;
1136 my $dbh = $form->get_standard_dbh($myconfig);
1138 my $units = AM->retrieve_units($myconfig, $form);
1140 my ($partunit) = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
1141 my $unit_factor = $units->{$partunit}->{factor} || 1;
1143 my @contents = $self->get_item_availability(%params);
1147 foreach my $sinfo (@{ $params{requests} }) {
1150 foreach my $row (@contents) {
1151 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
1152 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1153 ($row->{chargenumber} ne $sinfo->{chargenumber}) ||
1154 ($row->{bestbefore} ne $sinfo->{bestbefore}));
1158 my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
1160 if ($base_qty > $row->{qty}) {
1161 $sinfo->{error} = 1;
1162 push @errors, $sinfo;
1168 push @errors, $sinfo if (!$found);
1171 $main::lxdebug->leave_sub();
1176 sub transfer_in_out {
1177 $main::lxdebug->enter_sub();
1182 Common::check_params(\%params, qw(direction requests));
1184 if (!@{ $params{requests} }) {
1185 $main::lxdebug->leave_sub();
1189 my $myconfig = \%main::myconfig;
1190 my $form = $main::form;
1192 my $prefix = $params{direction} eq 'in' ? 'dst' : 'src';
1196 foreach my $request (@{ $params{requests} }) {
1198 'parts_id' => $request->{parts_id},
1199 "${prefix}_warehouse_id" => $request->{warehouse_id},
1200 "${prefix}_bin_id" => $request->{bin_id},
1201 'chargenumber' => $request->{chargenumber},
1202 'bestbefore' => $request->{bestbefore},
1203 'qty' => $request->{qty},
1204 'unit' => $request->{unit},
1205 'oe_id' => $form->{id},
1206 'shippingdate' => 'current_date',
1207 'transfer_type' => $params{direction} eq 'in' ? 'stock' : 'shipped',
1208 'project_id' => $request->{project_id},
1209 'delivery_order_items_stock_id' => $request->{delivery_order_items_stock_id},
1210 'comment' => $request->{comment},
1214 WH->transfer(@transfers);
1216 $main::lxdebug->leave_sub();
1219 sub get_shipped_qty {
1220 $main::lxdebug->enter_sub();
1225 Common::check_params(\%params, qw(type oe_id));
1227 my $myconfig = \%main::myconfig;
1228 my $form = $main::form;
1230 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1232 my @links = RecordLinks->get_links('dbh' => $dbh,
1233 'from_table' => 'oe',
1234 'from_id' => $params{oe_id},
1235 'to_table' => 'delivery_orders');
1236 my @values = map { $_->{to_id} } @links;
1238 if (!scalar @values) {
1239 $main::lxdebug->leave_sub();
1244 qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit
1245 FROM delivery_order_items doi
1246 LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id)
1247 LEFT JOIN parts p ON (doi.parts_id = p.id)
1248 WHERE o.id IN (| . join(', ', ('?') x scalar @values) . qq|)|;
1251 my $entries = selectall_hashref_query($form, $dbh, $query, @values);
1252 my $all_units = AM->retrieve_all_units();
1254 foreach my $entry (@{ $entries }) {
1255 $entry->{qty} *= AM->convert_unit($entry->{unit}, $entry->{partunit}, $all_units);
1257 if (!$ship{$entry->{parts_id}}) {
1258 $ship{$entry->{parts_id}} = $entry;
1260 $ship{$entry->{parts_id}}->{qty} += $entry->{qty};
1264 $main::lxdebug->leave_sub();
1269 sub is_marked_as_delivered {
1270 $main::lxdebug->enter_sub();
1275 Common::check_params(\%params, qw(id));
1277 my $myconfig = \%main::myconfig;
1278 my $form = $main::form;
1280 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1282 my ($delivered) = selectfirst_array_query($form, $dbh, qq|SELECT delivered FROM delivery_orders WHERE id = ?|, conv_i($params{id}));
1284 $main::lxdebug->leave_sub();
1286 return $delivered ? 1 : 0;