Merge branch 'gewicht'
[kivitendo-erp.git] / SL / DO.pm
1 #====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (C) 1999-2003
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
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.
21 #
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 #======================================================================
30 #
31 # Delivery Order entry module
32 #======================================================================
33
34 package DO;
35
36 use List::Util qw(max);
37 use YAML;
38
39 use SL::AM;
40 use SL::Common;
41 use SL::CVar;
42 use SL::DBUtils;
43 use SL::RecordLinks;
44 use SL::IC;
45
46 use strict;
47
48 sub transactions {
49   $main::lxdebug->enter_sub();
50
51   my ($self)   = @_;
52
53   my $myconfig = \%main::myconfig;
54   my $form     = $main::form;
55
56   # connect to database
57   my $dbh = $form->get_standard_dbh($myconfig);
58
59   my (@where, @values, $where);
60
61   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
62
63   my $query =
64     qq|SELECT dord.id, dord.donumber, dord.ordnumber,
65          dord.transdate, dord.reqdate,
66          ct.${vc}number, ct.name, dord.${vc}_id, dord.globalproject_id,
67          dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
68          dord.transaction_description,
69          pr.projectnumber AS globalprojectnumber,
70          dep.description AS department,
71          e.name AS employee,
72          sm.name AS salesman
73        FROM delivery_orders dord
74        LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
75        LEFT JOIN employee e ON (dord.employee_id = e.id)
76        LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
77        LEFT JOIN project pr ON (dord.globalproject_id = pr.id)
78        LEFT JOIN department dep ON (dord.department_id = dep.id)
79 |;
80
81   push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
82
83   if ($form->{department_id}) {
84     push @where,  qq|dord.department_id = ?|;
85     push @values, conv_i($form->{department_id});
86   }
87
88   if ($form->{project_id}) {
89     push @where,
90       qq|(dord.globalproject_id = ?) OR EXISTS
91           (SELECT * FROM delivery_order_items doi
92            WHERE (doi.project_id = ?) AND (doi.delivery_order_id = dord.id))|;
93     push @values, conv_i($form->{project_id}), conv_i($form->{project_id});
94   }
95
96   if ($form->{"${vc}_id"}) {
97     push @where,  qq|dord.${vc}_id = ?|;
98     push @values, $form->{"${vc}_id"};
99
100   } elsif ($form->{$vc}) {
101     push @where,  qq|ct.name ILIKE ?|;
102     push @values, '%' . $form->{$vc} . '%';
103   }
104
105   foreach my $item (qw(employee_id salesman_id)) {
106     next unless ($form->{$item});
107     push @where, "dord.$item = ?";
108     push @values, conv_i($form->{$item});
109   }
110   if (!$main::auth->assert('sales_all_edit', 1)) {
111     push @where, qq|dord.employee_id = (select id from employee where login= ?)|;
112     push @values, $form->{login};
113   }
114
115   foreach my $item (qw(donumber ordnumber cusordnumber transaction_description)) {
116     next unless ($form->{$item});
117     push @where,  qq|dord.$item ILIKE ?|;
118     push @values, '%' . $form->{$item} . '%';
119   }
120
121   if (($form->{open} || $form->{closed}) &&
122       ($form->{open} ne $form->{closed})) {
123     push @where, ($form->{open} ? "NOT " : "") . "COALESCE(dord.closed, FALSE)";
124   }
125
126   if (($form->{notdelivered} || $form->{delivered}) &&
127       ($form->{notdelivered} ne $form->{delivered})) {
128     push @where, ($form->{delivered} ? "" : "NOT ") . "COALESCE(dord.delivered, FALSE)";
129   }
130
131   if($form->{transdatefrom}) {
132     push @where,  qq|dord.transdate >= ?|;
133     push @values, conv_date($form->{transdatefrom});
134   }
135
136   if($form->{transdateto}) {
137     push @where,  qq|dord.transdate <= ?|;
138     push @values, conv_date($form->{transdateto});
139   }
140
141   if (@where) {
142     $query .= " WHERE " . join(" AND ", map { "($_)" } @where);
143   }
144
145   my %allowed_sort_columns = (
146     "transdate"               => "dord.transdate",
147     "reqdate"                 => "dord.reqdate",
148     "id"                      => "dord.id",
149     "donumber"                => "dord.donumber",
150     "ordnumber"               => "dord.ordnumber",
151     "name"                    => "ct.name",
152     "employee"                => "e.name",
153     "salesman"                => "sm.name",
154     "shipvia"                 => "dord.shipvia",
155     "transaction_description" => "dord.transaction_description",
156     "department"              => "lower(dep.description)",
157   );
158
159   my $sortdir   = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
160   my $sortorder = "dord.id";
161   if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
162     $sortorder = $allowed_sort_columns{$form->{sort}};
163   }
164
165   $query .= qq| ORDER by | . $sortorder . " $sortdir";
166
167   $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
168
169   if (scalar @{ $form->{DO} }) {
170     $query =
171       qq|SELECT id
172          FROM oe
173          WHERE NOT COALESCE(quotation, FALSE)
174            AND (ordnumber = ?)
175            AND (COALESCE(${vc}_id, 0) != 0)|;
176
177     my $sth = prepare_query($form, $dbh, $query);
178
179     foreach my $dord (@{ $form->{DO} }) {
180       next unless ($dord->{ordnumber});
181       do_statement($form, $sth, $query, $dord->{ordnumber});
182       ($dord->{oe_id}) = $sth->fetchrow_array();
183     }
184
185     $sth->finish();
186   }
187
188   $main::lxdebug->leave_sub();
189 }
190
191 sub save {
192   $main::lxdebug->enter_sub();
193
194   my ($self)   = @_;
195
196   my $myconfig = \%main::myconfig;
197   my $form     = $main::form;
198
199   # connect to database, turn off autocommit
200   my $dbh = $form->get_standard_dbh($myconfig);
201
202   my ($query, @values, $sth, $null);
203
204   my $all_units = AM->retrieve_units($myconfig, $form);
205   $form->{all_units} = $all_units;
206
207   my $ic_cvar_configs = CVar->get_configs(module => 'IC',
208                                           dbh    => $dbh);
209
210   $form->{donumber}    = $form->update_defaults($myconfig, $form->{type} eq 'sales_delivery_order' ? 'sdonumber' : 'pdonumber', $dbh) unless $form->{donumber};
211   $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
212   $form->get_employee($dbh) unless ($form->{employee_id});
213
214   my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
215
216   if ($form->{id}) {
217
218     $query = qq|DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = ?)|;
219     do_query($form, $dbh, $query, conv_i($form->{id}));
220
221     $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
222     do_query($form, $dbh, $query, conv_i($form->{id}));
223
224     $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
225     do_query($form, $dbh, $query, conv_i($form->{id}));
226
227   } else {
228
229     $query = qq|SELECT nextval('id')|;
230     ($form->{id}) = selectrow_query($form, $dbh, $query);
231
232     $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id, currency_id) VALUES (?, '', ?, (SELECT currency_id FROM defaults LIMIT 1))|;
233     do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}));
234   }
235
236   my $project_id;
237   my $items_reqdate;
238
239   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
240   my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
241   my $price_factor;
242
243   my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
244   my @part_ids    = keys %part_id_map;
245   my %part_unit_map;
246
247   if (@part_ids) {
248     $query         = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
249     %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
250   }
251
252   my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
253   my $h_item_id = prepare_query($form, $dbh, $q_item_id);
254
255   my $q_item =
256     qq|INSERT INTO delivery_order_items (
257          id, delivery_order_id, parts_id, description, longdescription, qty, base_qty,
258          sellprice, discount, unit, reqdate, project_id, serialnumber,
259          ordnumber, transdate, cusordnumber,
260          lastcost, price_factor_id, price_factor, marge_price_factor, pricegroup_id)
261        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
262          (SELECT factor FROM price_factors WHERE id = ?), ?, ?)|;
263   my $h_item = prepare_query($form, $dbh, $q_item);
264
265   my $q_item_stock =
266     qq|INSERT INTO delivery_order_items_stock (delivery_order_item_id, qty, unit, warehouse_id, bin_id, chargenumber, bestbefore)
267        VALUES (?, ?, ?, ?, ?, ?, ?)|;
268   my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
269
270   my $in_out       = $form->{type} =~ /^sales/ ? 'out' : 'in';
271
272   for my $i (1 .. $form->{rowcount}) {
273     next if (!$form->{"id_$i"});
274
275     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
276
277     my $item_unit = $part_unit_map{$form->{"id_$i"}};
278
279     my $basefactor = 1;
280     if (defined($all_units->{$item_unit}->{factor}) && (($all_units->{$item_unit}->{factor} * 1) != 0)) {
281       $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
282     }
283     my $baseqty = $form->{"qty_$i"} * $basefactor;
284
285     # set values to 0 if nothing entered
286     $form->{"discount_$i"}  = $form->parse_amount($myconfig, $form->{"discount_$i"});
287     $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
288     $form->{"lastcost_$i"} = $form->parse_amount($myconfig, $form->{"lastcost_$i"});
289
290     $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
291     my $linetotal    = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
292
293     $items_reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
294
295     do_statement($form, $h_item_id, $q_item_id);
296     my ($item_id) = $h_item_id->fetchrow_array();
297
298     # Get pricegroup_id and save it. Unfortunately the interface
299     # also uses ID "0" for signalling that none is selected, but "0"
300     # must not be stored in the database. Therefore we cannot simply
301     # use conv_i().
302     my $pricegroup_id = $form->{"pricegroup_id_$i"} * 1;
303     $pricegroup_id    = undef if !$pricegroup_id;
304
305     # save detail record in delivery_order_items table
306     @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
307                $form->{"description_$i"}, $form->{"longdescription_$i"},
308                $form->{"qty_$i"}, $baseqty,
309                $form->{"sellprice_$i"}, $form->{"discount_$i"} / 100,
310                $form->{"unit_$i"}, conv_date($items_reqdate), conv_i($form->{"project_id_$i"}),
311                $form->{"serialnumber_$i"},
312                $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
313                $form->{"cusordnumber_$i"},
314                $form->{"lastcost_$i"},
315                conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
316                conv_i($form->{"marge_price_factor_$i"}),
317                $pricegroup_id);
318     do_statement($form, $h_item, $q_item, @values);
319
320     my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
321
322     foreach my $sinfo (@{ $stock_info }) {
323       @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
324                  conv_i($sinfo->{bin_id}), $sinfo->{chargenumber}, conv_date($sinfo->{bestbefore}));
325       do_statement($form, $h_item_stock, $q_item_stock, @values);
326     }
327
328     CVar->save_custom_variables(module       => 'IC',
329                                 sub_module   => 'delivery_order_items',
330                                 trans_id     => $item_id,
331                                 configs      => $ic_cvar_configs,
332                                 variables    => $form,
333                                 name_prefix  => 'ic_',
334                                 name_postfix => "_$i",
335                                 dbh          => $dbh);
336   }
337
338   $h_item_id->finish();
339   $h_item->finish();
340   $h_item_stock->finish();
341
342
343   # reqdate is last items reqdate (?: old behaviour) if not already set
344   $form->{reqdate} ||= $items_reqdate;
345   # save DO record
346   $query =
347     qq|UPDATE delivery_orders SET
348          donumber = ?, ordnumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
349          customer_id = ?, reqdate = ?,
350          shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
351          delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
352          globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
353          is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, currency_id = (SELECT id FROM currencies WHERE name = ?)
354        WHERE id = ?|;
355
356   @values = ($form->{donumber}, $form->{ordnumber},
357              $form->{cusordnumber}, conv_date($form->{transdate}),
358              conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
359              conv_date($form->{reqdate}), $form->{shippingpoint}, $form->{shipvia},
360              $form->{notes}, $form->{intnotes},
361              $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
362              conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
363              conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
364              conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
365              $form->{transaction_description},
366              $form->{type} =~ /^sales/ ? 't' : 'f',
367              conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), $form->{currency},
368              conv_i($form->{id}));
369   do_query($form, $dbh, $query, @values);
370
371   # add shipto
372   $form->{name} = $form->{ $form->{vc} };
373   $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
374
375   if (!$form->{shipto_id}) {
376     $form->add_shipto($dbh, $form->{id}, "DO");
377   }
378
379   # save printed, emailed, queued
380   $form->save_status($dbh);
381
382   # Link this delivery order to the quotations it was created from.
383   RecordLinks->create_links('dbh'        => $dbh,
384                             'mode'       => 'ids',
385                             'from_table' => 'oe',
386                             'from_ids'   => $form->{convert_from_oe_ids},
387                             'to_table'   => 'delivery_orders',
388                             'to_id'      => $form->{id},
389     );
390   delete $form->{convert_from_oe_ids};
391
392   $self->mark_orders_if_delivered('do_id' => $form->{id},
393                                   'type'  => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
394                                   'dbh'   => $dbh,);
395
396   my $rc = $dbh->commit();
397
398   $form->{saved_donumber} = $form->{donumber};
399
400   Common::webdav_folder($form);
401
402   $main::lxdebug->leave_sub();
403
404   return $rc;
405 }
406
407 sub mark_orders_if_delivered {
408   $main::lxdebug->enter_sub();
409
410   my $self   = shift;
411   my %params = @_;
412
413   Common::check_params(\%params, qw(do_id type));
414
415   my $myconfig = \%main::myconfig;
416   my $form     = $main::form;
417
418   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
419
420   my @links    = RecordLinks->get_links('dbh'        => $dbh,
421                                         'from_table' => 'oe',
422                                         'to_table'   => 'delivery_orders',
423                                         'to_id'      => $params{do_id});
424
425   my $oe_id  = @links ? $links[0]->{from_id} : undef;
426
427   return $main::lxdebug->leave_sub() if (!$oe_id);
428
429   my $all_units = AM->retrieve_all_units();
430
431   my $query     = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit
432                      FROM orderitems oi
433                      LEFT JOIN parts p ON (oi.parts_id = p.id)
434                      WHERE (oi.trans_id = ?)|;
435   my $sth       = prepare_execute_query($form, $dbh, $query, $oe_id);
436
437   my %shipped   = $self->get_shipped_qty('type'  => $params{type},
438                                          'oe_id' => $oe_id,);
439   my %ordered   = ();
440
441   while (my $ref = $sth->fetchrow_hashref()) {
442     $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor};
443
444     if ($ordered{$ref->{parts_id}}) {
445       $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty};
446     } else {
447       $ordered{$ref->{parts_id}}             = $ref;
448     }
449   }
450
451   $sth->finish();
452
453   map { $_->{baseqty} = $_->{qty} * $all_units->{$_->{unit}}->{factor} / $all_units->{$_->{partunit}}->{factor} } values %shipped;
454
455   my $delivered = 1;
456   foreach my $part (values %ordered) {
457     if (!$shipped{$part->{parts_id}} || ($shipped{$part->{parts_id}}->{baseqty} < $part->{baseqty})) {
458       $delivered = 0;
459       last;
460     }
461   }
462
463   if ($delivered) {
464     $query = qq|UPDATE oe
465                 SET delivered = TRUE
466                 WHERE id = ?|;
467     do_query($form, $dbh, $query, $oe_id);
468     $dbh->commit() if (!$params{dbh});
469   }
470
471   $main::lxdebug->leave_sub();
472 }
473
474 sub close_orders {
475   $main::lxdebug->enter_sub();
476
477   my $self     = shift;
478   my %params   = @_;
479
480   Common::check_params(\%params, qw(ids));
481
482   if (('ARRAY' ne ref $params{ids}) || !scalar @{ $params{ids} }) {
483     $main::lxdebug->leave_sub();
484     return;
485   }
486
487   my $myconfig = \%main::myconfig;
488   my $form     = $main::form;
489
490   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
491
492   my $query    = qq|UPDATE delivery_orders SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar(@{ $params{ids} })) . qq|)|;
493
494   do_query($form, $dbh, $query, map { conv_i($_) } @{ $params{ids} });
495
496   $dbh->commit() unless ($params{dbh});
497
498   $main::lxdebug->leave_sub();
499 }
500
501 sub delete {
502   $main::lxdebug->enter_sub();
503
504   my ($self)   = @_;
505
506   my $myconfig = \%main::myconfig;
507   my $form     = $main::form;
508   my $spool    = $::lx_office_conf{paths}->{spool};
509
510   # connect to database
511   my $dbh = $form->get_standard_dbh($myconfig);
512
513   # delete spool files
514   my $query = qq|SELECT s.spoolfile FROM status s WHERE s.trans_id = ?|;
515   my $sth   = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
516
517   my $spoolfile;
518   my @spoolfiles = ();
519   my @values;
520
521   while (($spoolfile) = $sth->fetchrow_array) {
522     push @spoolfiles, $spoolfile;
523   }
524   $sth->finish();
525
526   # delete-values
527   @values = (conv_i($form->{id}));
528
529   # delete status entries
530   $query = qq|DELETE FROM status
531               WHERE trans_id = ?|;
532   do_query($form, $dbh, $query, @values);
533
534   # delete individual entries
535   $query = qq|DELETE FROM delivery_order_items_stock
536               WHERE delivery_order_item_id IN (
537                 SELECT id FROM delivery_order_items
538                 WHERE delivery_order_id = ?
539               )|;
540   do_query($form, $dbh, $query, @values);
541
542   # delete individual entries
543   $query = qq|DELETE FROM delivery_order_items
544               WHERE delivery_order_id = ?|;
545   do_query($form, $dbh, $query, @values);
546
547   # delete DO record
548   $query = qq|DELETE FROM delivery_orders
549               WHERE id = ?|;
550   do_query($form, $dbh, $query, @values);
551
552   $query = qq|DELETE FROM shipto
553               WHERE trans_id = ? AND module = 'DO'|;
554   do_query($form, $dbh, $query, @values);
555
556   my $rc = $dbh->commit();
557
558   if ($rc) {
559     foreach $spoolfile (@spoolfiles) {
560       unlink "$spool/$spoolfile" if $spoolfile;
561     }
562   }
563
564   $main::lxdebug->leave_sub();
565
566   return $rc;
567 }
568
569 sub retrieve {
570   $main::lxdebug->enter_sub();
571
572   my $self     = shift;
573   my %params   = @_;
574
575   my $myconfig = \%main::myconfig;
576   my $form     = $main::form;
577
578   # connect to database
579   my $dbh = $form->get_standard_dbh($myconfig);
580
581   my ($query, $query_add, @values, $sth, $ref);
582
583   my $ic_cvar_configs = CVar->get_configs(module => 'IC',
584                                           dbh    => $dbh);
585
586   my $vc   = $params{vc} eq 'customer' ? 'customer' : 'vendor';
587
588   my $mode = !$params{ids} ? 'default' : ref $params{ids} eq 'ARRAY' ? 'multi' : 'single';
589
590   if ($mode eq 'default') {
591     $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate|);
592     map { $form->{$_} = $ref->{$_} } keys %$ref;
593
594     # if reqdate is not set from oe-workflow, set it to transdate (which is current date)
595     $form->{reqdate} ||= $form->{transdate};
596
597     # get last name used
598     $form->lastname_used($dbh, $myconfig, $vc) unless $form->{"${vc}_id"};
599
600     $main::lxdebug->leave_sub();
601
602     return 1;
603   }
604
605   my @do_ids              = map { conv_i($_) } ($mode eq 'multi' ? @{ $params{ids} } : ($params{ids}));
606   my $do_ids_placeholders = join(', ', ('?') x scalar(@do_ids));
607
608   # retrieve order for single id
609   # NOTE: this query is intended to fetch all information only ONCE.
610   # so if any of these infos is important (or even different) for any item,
611   # it will be killed out and then has to be fetched from the item scope query further down
612   $query =
613     qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.transdate, dord.reqdate,
614          dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
615          e.name AS employee, dord.employee_id, dord.salesman_id,
616          dord.${vc}_id, cv.name AS ${vc},
617          dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
618          d.description AS department, dord.language_id,
619          dord.shipto_id,
620          dord.globalproject_id, dord.delivered, dord.transaction_description,
621          dord.taxzone_id, dord.taxincluded, dord.terms, (SELECT cu.name FROM currencies cu WHERE cu.id=dord.currency_id) AS currency
622        FROM delivery_orders dord
623        JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
624        LEFT JOIN employee e ON (dord.employee_id = e.id)
625        LEFT JOIN department d ON (dord.department_id = d.id)
626        WHERE dord.id IN ($do_ids_placeholders)|;
627   $sth = prepare_execute_query($form, $dbh, $query, @do_ids);
628
629   delete $form->{"${vc}_id"};
630   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
631     if ($form->{"${vc}_id"} && ($ref->{"${vc}_id"} != $form->{"${vc}_id"})) {
632       $sth->finish();
633       $main::lxdebug->leave_sub();
634
635       return 0;
636     }
637
638     map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
639     $form->{donumber_array} .= $form->{donumber} . ' ';
640   }
641   $sth->finish();
642
643   $form->{donumber_array} =~ s/\s*$//g;
644
645   $form->{saved_donumber} = $form->{donumber};
646
647   # if not given, fill transdate with current_date
648   $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
649
650   if ($mode eq 'single') {
651     $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'DO'|;
652     $sth   = prepare_execute_query($form, $dbh, $query, $form->{id});
653
654     $ref   = $sth->fetchrow_hashref("NAME_lc");
655     delete $ref->{id};
656     map { $form->{$_} = $ref->{$_} } keys %$ref;
657     $sth->finish();
658
659     # get printed, emailed and queued
660     $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
661     $sth   = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
662
663     while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
664       $form->{printed} .= "$ref->{formname} " if $ref->{printed};
665       $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
666       $form->{queued}  .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
667     }
668     $sth->finish();
669     map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
670
671   } else {
672     delete $form->{id};
673   }
674
675   my %oid = ('Pg'     => 'oid',
676              'Oracle' => 'rowid');
677
678   # retrieve individual items
679   # this query looks up all information about the items
680   # stuff different from the whole will not be overwritten, but saved with a suffix.
681   $query =
682     qq|SELECT doi.id AS delivery_order_items_id,
683          p.partnumber, p.assembly, p.listprice, doi.description, doi.qty,
684          doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.notes AS partnotes,
685          doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
686          doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
687          doi.price_factor_id, doi.price_factor, doi.marge_price_factor, doi.pricegroup_id,
688          pr.projectnumber, dord.transdate AS dord_transdate,
689          pg.partsgroup
690        FROM delivery_order_items doi
691        JOIN parts p ON (doi.parts_id = p.id)
692        JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
693        LEFT JOIN project pr ON (doi.project_id = pr.id)
694        LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
695        WHERE doi.delivery_order_id IN ($do_ids_placeholders)
696        ORDER BY doi.$oid{$myconfig->{dbdriver}}|;
697
698   $form->{form_details} = selectall_hashref_query($form, $dbh, $query, @do_ids);
699
700   # Retrieve custom variables.
701   foreach my $doi (@{ $form->{form_details} }) {
702     my $cvars = CVar->get_custom_variables(dbh        => $dbh,
703                                            module     => 'IC',
704                                            sub_module => 'delivery_order_items',
705                                            trans_id   => $doi->{delivery_order_items_id},
706                                           );
707     map { $doi->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
708   }
709
710   if ($mode eq 'single') {
711     my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
712
713     $query =
714       qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber, bestbefore
715          FROM delivery_order_items_stock
716          WHERE delivery_order_item_id = ?|;
717     my $sth = prepare_query($form, $dbh, $query);
718
719     foreach my $doi (@{ $form->{form_details} }) {
720       do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
721       my $requests = [];
722       while (my $ref = $sth->fetchrow_hashref()) {
723         push @{ $requests }, $ref;
724       }
725
726       $doi->{"stock_${in_out}"} = YAML::Dump($requests);
727     }
728
729     $sth->finish();
730   }
731
732   Common::webdav_folder($form);
733
734   $main::lxdebug->leave_sub();
735
736   return 1;
737 }
738
739 sub order_details {
740   $main::lxdebug->enter_sub();
741
742   my ($self, $myconfig, $form) = @_;
743
744   # connect to database
745   my $dbh = $form->get_standard_dbh($myconfig);
746   my $query;
747   my @values = ();
748   my $sth;
749   my $item;
750   my $i;
751   my @partsgroup = ();
752   my $partsgroup;
753   my $position = 0;
754
755   my %oid = ('Pg'     => 'oid',
756              'Oracle' => 'rowid');
757
758   my (@project_ids, %projectnumbers, %projectdescriptions);
759
760   push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
761
762   # sort items by partsgroup
763   for $i (1 .. $form->{rowcount}) {
764     $partsgroup = "";
765     if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
766       $partsgroup = $form->{"partsgroup_$i"};
767     }
768     push @partsgroup, [$i, $partsgroup];
769     push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
770   }
771
772   if (@project_ids) {
773     $query = "SELECT id, projectnumber, description FROM project WHERE id IN (" .
774       join(", ", map("?", @project_ids)) . ")";
775     $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
776     while (my $ref = $sth->fetchrow_hashref()) {
777       $projectnumbers{$ref->{id}} = $ref->{projectnumber};
778       $projectdescriptions{$ref->{id}} = $ref->{description};
779     }
780     $sth->finish();
781   }
782
783   $form->{"globalprojectnumber"} =
784     $projectnumbers{$form->{"globalproject_id"}};
785   $form->{"globalprojectdescription"} =
786       $projectdescriptions{$form->{"globalproject_id"}};
787
788   my $q_pg     = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
789                     FROM assembly a
790                     JOIN parts p ON (a.parts_id = p.id)
791                     LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
792                     WHERE a.bom = '1'
793                       AND a.id = ?|;
794   my $h_pg     = prepare_query($form, $dbh, $q_pg);
795
796   my $q_bin_wh = qq|SELECT (SELECT description FROM bin       WHERE id = ?) AS bin,
797                            (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
798   my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
799
800   my $in_out   = $form->{type} =~ /^sales/ ? 'out' : 'in';
801
802   my $num_si   = 0;
803
804   my $ic_cvar_configs = CVar->get_configs(module => 'IC');
805
806   $form->{TEMPLATE_ARRAYS} = { };
807   IC->prepare_parts_for_printing(myconfig => $myconfig, form => $form);
808
809   my @arrays =
810     qw(runningnumber number description longdescription qty unit
811        partnotes serialnumber reqdate projectnumber projectdescription
812        si_runningnumber si_number si_description
813        si_warehouse si_bin si_chargenumber si_bestbefore si_qty si_unit weight lineweight);
814
815   map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays);
816
817   push @arrays, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
818
819   $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
820   my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
821
822   my $totalweight = 0;
823   my $sameitem = "";
824   foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
825     $i = $item->[0];
826
827     next if (!$form->{"id_$i"});
828
829     $position++;
830
831     if ($item->[1] ne $sameitem) {
832       push(@{ $form->{description} }, qq|$item->[1]|);
833       $sameitem = $item->[1];
834
835       map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
836     }
837
838     $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
839
840     # add number, description and qty to $form->{number}, ....
841
842     my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
843
844     push @{ $form->{TEMPLATE_ARRAYS}{runningnumber} },   $position;
845     push @{ $form->{TEMPLATE_ARRAYS}{number} },          $form->{"partnumber_$i"};
846     push @{ $form->{TEMPLATE_ARRAYS}{description} },     $form->{"description_$i"};
847     push @{ $form->{TEMPLATE_ARRAYS}{longdescription} }, $form->{"longdescription_$i"};
848     push @{ $form->{TEMPLATE_ARRAYS}{qty} },             $form->format_amount($myconfig, $form->{"qty_$i"});
849     push @{ $form->{TEMPLATE_ARRAYS}{qty_nofmt} },       $form->{"qty_$i"};
850     push @{ $form->{TEMPLATE_ARRAYS}{unit} },            $form->{"unit_$i"};
851     push @{ $form->{TEMPLATE_ARRAYS}{partnotes} },       $form->{"partnotes_$i"};
852     push @{ $form->{TEMPLATE_ARRAYS}{serialnumber} },    $form->{"serialnumber_$i"};
853     push @{ $form->{TEMPLATE_ARRAYS}{reqdate} },         $form->{"reqdate_$i"};
854     push @{ $form->{TEMPLATE_ARRAYS}{projectnumber} },   $projectnumbers{$form->{"project_id_$i"}};
855     push @{ $form->{TEMPLATE_ARRAYS}{projectdescription} },
856       $projectdescriptions{$form->{"project_id_$i"}};
857
858     my $lineweight = $form->{"qty_$i"} * $form->{"weight_$i"};
859     $totalweight += $lineweight;
860     push @{ $form->{TEMPLATE_ARRAYS}->{weight} },            $form->format_amount($myconfig, $form->{"weight_$i"}, 3);
861     push @{ $form->{TEMPLATE_ARRAYS}->{weight_nofmt} },      $form->{"weight_$i"};
862     push @{ $form->{TEMPLATE_ARRAYS}->{lineweight} },        $form->format_amount($myconfig, $lineweight, 3);
863     push @{ $form->{TEMPLATE_ARRAYS}->{lineweight_nofmt} },  $lineweight;
864
865     if ($form->{"assembly_$i"}) {
866       $sameitem = "";
867
868       # get parts and push them onto the stack
869       my $sortorder = "";
870       if ($form->{groupitems}) {
871         $sortorder =
872           qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
873       } else {
874         $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
875       }
876
877       do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
878
879       while (my $ref = $h_pg->fetchrow_hashref("NAME_lc")) {
880         if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
881           map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
882           $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
883           push(@{ $form->{TEMPLATE_ARRAYS}->{description} }, $sameitem);
884         }
885         push(@{ $form->{TEMPLATE_ARRAYS}->{"description"} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq| -- $ref->{partnumber}, $ref->{description}|);
886
887         map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
888       }
889     }
890
891     if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
892       my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
893
894       foreach my $si (@{ $stock_info }) {
895         $num_si++;
896
897         do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
898         my $bin_wh = $h_bin_wh->fetchrow_hashref();
899
900         push @{ $form->{TEMPLATE_ARRAYS}{si_runningnumber}[$position-1] }, $num_si;
901         push @{ $form->{TEMPLATE_ARRAYS}{si_number}[$position-1] },        $form->{"partnumber_$i"};
902         push @{ $form->{TEMPLATE_ARRAYS}{si_description}[$position-1] },   $form->{"description_$i"};
903         push @{ $form->{TEMPLATE_ARRAYS}{si_warehouse}[$position-1] },     $bin_wh->{warehouse};
904         push @{ $form->{TEMPLATE_ARRAYS}{si_bin}[$position-1] },           $bin_wh->{bin};
905         push @{ $form->{TEMPLATE_ARRAYS}{si_chargenumber}[$position-1] },  $si->{chargenumber};
906         push @{ $form->{TEMPLATE_ARRAYS}{si_bestbefore}[$position-1] },    $si->{bestbefore};
907         push @{ $form->{TEMPLATE_ARRAYS}{si_qty}[$position-1] },           $form->format_amount($myconfig, $si->{qty} * 1);
908         push @{ $form->{TEMPLATE_ARRAYS}{si_qty_nofmt}[$position-1] },     $si->{qty} * 1;
909         push @{ $form->{TEMPLATE_ARRAYS}{si_unit}[$position-1] },          $si->{unit};
910       }
911     }
912
913     push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
914       CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
915         for @{ $ic_cvar_configs };
916   }
917
918   $form->{totalweight}       = $form->format_amount($myconfig, $totalweight, 3);
919   $form->{totalweight_nofmt} = $totalweight;
920   my $defaults = AM->get_defaults();
921   $form->{weightunit}        = $defaults->{weightunit};
922
923   $h_pg->finish();
924   $h_bin_wh->finish();
925
926   $form->{username} = $myconfig->{name};
927
928   $main::lxdebug->leave_sub();
929 }
930
931 sub project_description {
932   $main::lxdebug->enter_sub();
933
934   my ($self, $dbh, $id) = @_;
935
936   my $form     =  $main::form;
937
938   my $query = qq|SELECT description FROM project WHERE id = ?|;
939   my ($value) = selectrow_query($form, $dbh, $query, $id);
940
941   $main::lxdebug->leave_sub();
942
943   return $value;
944 }
945
946 sub unpack_stock_information {
947   $main::lxdebug->enter_sub();
948
949   my $self   = shift;
950   my %params = @_;
951
952   Common::check_params_x(\%params, qw(packed));
953
954   my $unpacked;
955
956   eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
957
958   $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
959
960   foreach my $entry (@{ $unpacked }) {
961     next if ('HASH' eq ref $entry);
962     $unpacked = [];
963     last;
964   }
965
966   $main::lxdebug->leave_sub();
967
968   return $unpacked;
969 }
970
971 sub get_item_availability {
972   $::lxdebug->enter_sub;
973
974   my $self     = shift;
975   my %params   = @_;
976
977   Common::check_params(\%params, qw(parts_id));
978
979   my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
980
981   my $query     =
982     qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, SUM(qty) AS qty, i.parts_id,
983          w.description AS warehousedescription,
984          b.description AS bindescription
985        FROM inventory i
986        LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
987        LEFT JOIN bin b       ON (i.bin_id       = b.id)
988        WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
989        GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, i.parts_id, w.description, b.description
990        HAVING SUM(qty) > 0
991        ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber), i.bestbefore
992 |;
993   my $contents = selectall_hashref_query($::form, $::form->get_standard_dbh, $query, @parts_ids);
994
995   $::lxdebug->leave_sub;
996
997   return @{ $contents };
998 }
999
1000
1001 sub check_stock_availability {
1002   $main::lxdebug->enter_sub();
1003
1004   my $self     = shift;
1005   my %params   = @_;
1006
1007   Common::check_params(\%params, qw(requests parts_id));
1008
1009   my $myconfig    = \%main::myconfig;
1010   my $form        =  $main::form;
1011
1012   my $dbh         = $form->get_standard_dbh($myconfig);
1013
1014   my $units       = AM->retrieve_units($myconfig, $form);
1015
1016   my ($partunit)  = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
1017   my $unit_factor = $units->{$partunit}->{factor} || 1;
1018
1019   my @contents    = $self->get_item_availability(%params);
1020
1021   my @errors;
1022
1023   foreach my $sinfo (@{ $params{requests} }) {
1024     my $found = 0;
1025
1026     foreach my $row (@contents) {
1027       next if (($row->{bin_id}       != $sinfo->{bin_id}) ||
1028                ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1029                ($row->{chargenumber} ne $sinfo->{chargenumber}) ||
1030                ($row->{bestbefore}   ne $sinfo->{bestbefore}));
1031
1032       $found       = 1;
1033
1034       my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
1035
1036       if ($base_qty > $row->{qty}) {
1037         $sinfo->{error} = 1;
1038         push @errors, $sinfo;
1039
1040         last;
1041       }
1042     }
1043
1044     push @errors, $sinfo if (!$found);
1045   }
1046
1047   $main::lxdebug->leave_sub();
1048
1049   return @errors;
1050 }
1051
1052 sub transfer_in_out {
1053   $main::lxdebug->enter_sub();
1054
1055   my $self     = shift;
1056   my %params   = @_;
1057
1058   Common::check_params(\%params, qw(direction requests));
1059
1060   if (!@{ $params{requests} }) {
1061     $main::lxdebug->leave_sub();
1062     return;
1063   }
1064
1065   my $myconfig = \%main::myconfig;
1066   my $form     = $main::form;
1067
1068   my $prefix   = $params{direction} eq 'in' ? 'dst' : 'src';
1069
1070   my @transfers;
1071
1072   foreach my $request (@{ $params{requests} }) {
1073     push @transfers, {
1074       'parts_id'               => $request->{parts_id},
1075       "${prefix}_warehouse_id" => $request->{warehouse_id},
1076       "${prefix}_bin_id"       => $request->{bin_id},
1077       'chargenumber'           => $request->{chargenumber},
1078       'bestbefore'             => $request->{bestbefore},
1079       'qty'                    => $request->{qty},
1080       'unit'                   => $request->{unit},
1081       'oe_id'                  => $form->{id},
1082       'shippingdate'           => 'current_date',
1083       'transfer_type'          => $params{direction} eq 'in' ? 'stock' : 'shipped',
1084       'project_id'             => $request->{project_id},
1085     };
1086   }
1087
1088   WH->transfer(@transfers);
1089
1090   $main::lxdebug->leave_sub();
1091 }
1092
1093 sub get_shipped_qty {
1094   $main::lxdebug->enter_sub();
1095
1096   my $self     = shift;
1097   my %params   = @_;
1098
1099   Common::check_params(\%params, qw(type oe_id));
1100
1101   my $myconfig = \%main::myconfig;
1102   my $form     = $main::form;
1103
1104   my $dbh      = $params{dbh} || $form->get_standard_dbh($myconfig);
1105
1106   my @links    = RecordLinks->get_links('dbh'        => $dbh,
1107                                         'from_table' => 'oe',
1108                                         'from_id'    => $params{oe_id},
1109                                         'to_table'   => 'delivery_orders');
1110   my @values   = map { $_->{to_id} } @links;
1111
1112   if (!scalar @values) {
1113     $main::lxdebug->leave_sub();
1114     return ();
1115   }
1116
1117   my $query =
1118     qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit
1119        FROM delivery_order_items doi
1120        LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id)
1121        LEFT JOIN parts p ON (doi.parts_id = p.id)
1122        WHERE o.id IN (| . join(', ', ('?') x scalar @values) . qq|)|;
1123
1124   my %ship      = ();
1125   my $entries   = selectall_hashref_query($form, $dbh, $query, @values);
1126   my $all_units = AM->retrieve_all_units();
1127
1128   foreach my $entry (@{ $entries }) {
1129     $entry->{qty} *= AM->convert_unit($entry->{unit}, $entry->{partunit}, $all_units);
1130
1131     if (!$ship{$entry->{parts_id}}) {
1132       $ship{$entry->{parts_id}} = $entry;
1133     } else {
1134       $ship{$entry->{parts_id}}->{qty} += $entry->{qty};
1135     }
1136   }
1137
1138   $main::lxdebug->leave_sub();
1139
1140   return %ship;
1141 }
1142
1143 sub is_marked_as_delivered {
1144   $main::lxdebug->enter_sub();
1145
1146   my $self     = shift;
1147   my %params   = @_;
1148
1149   Common::check_params(\%params, qw(id));
1150
1151   my $myconfig    = \%main::myconfig;
1152   my $form        = $main::form;
1153
1154   my $dbh         = $params{dbh} || $form->get_standard_dbh($myconfig);
1155
1156   my ($delivered) = selectfirst_array_query($form, $dbh, qq|SELECT delivered FROM delivery_orders WHERE id = ?|, conv_i($params{id}));
1157
1158   $main::lxdebug->leave_sub();
1159
1160   return $delivered ? 1 : 0;
1161 }
1162
1163
1164 1;