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 # save detail record in delivery_order_items table
297 @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
298 $form->{"description_$i"}, $form->{"longdescription_$i"},
299 $form->{"qty_$i"}, $baseqty,
300 $form->{"sellprice_$i"}, $form->{"discount_$i"} / 100,
301 $form->{"unit_$i"}, conv_date($items_reqdate), conv_i($form->{"project_id_$i"}),
302 $form->{"serialnumber_$i"},
303 $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
304 $form->{"cusordnumber_$i"},
305 $form->{"lastcost_$i"},
306 conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
307 conv_i($form->{"marge_price_factor_$i"}),
308 conv_i($form->{"pricegroup_id_$i"}));
309 do_statement($form, $h_item, $q_item, @values);
311 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
313 foreach my $sinfo (@{ $stock_info }) {
314 @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
315 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber}, conv_date($sinfo->{bestbefore}));
316 do_statement($form, $h_item_stock, $q_item_stock, @values);
319 CVar->save_custom_variables(module => 'IC',
320 sub_module => 'delivery_order_items',
321 trans_id => $item_id,
322 configs => $ic_cvar_configs,
324 name_prefix => 'ic_',
325 name_postfix => "_$i",
329 $h_item_id->finish();
331 $h_item_stock->finish();
334 # reqdate is last items reqdate (?: old behaviour) if not already set
335 $form->{reqdate} ||= $items_reqdate;
338 qq|UPDATE delivery_orders SET
339 donumber = ?, ordnumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
340 customer_id = ?, reqdate = ?,
341 shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
342 delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
343 globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
344 is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, curr = ?
347 @values = ($form->{donumber}, $form->{ordnumber},
348 $form->{cusordnumber}, conv_date($form->{transdate}),
349 conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
350 conv_date($form->{reqdate}), $form->{shippingpoint}, $form->{shipvia},
351 $form->{notes}, $form->{intnotes},
352 $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
353 conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
354 conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
355 conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
356 $form->{transaction_description},
357 $form->{type} =~ /^sales/ ? 't' : 'f',
358 conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), substr($form->{currency}, 0, 3),
359 conv_i($form->{id}));
360 do_query($form, $dbh, $query, @values);
363 $form->{name} = $form->{ $form->{vc} };
364 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
366 if (!$form->{shipto_id}) {
367 $form->add_shipto($dbh, $form->{id}, "DO");
370 # save printed, emailed, queued
371 $form->save_status($dbh);
373 # Link this delivery order to the quotations it was created from.
374 RecordLinks->create_links('dbh' => $dbh,
376 'from_table' => 'oe',
377 'from_ids' => $form->{convert_from_oe_ids},
378 'to_table' => 'delivery_orders',
379 'to_id' => $form->{id},
381 delete $form->{convert_from_oe_ids};
383 $self->mark_orders_if_delivered('do_id' => $form->{id},
384 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
387 my $rc = $dbh->commit();
389 $form->{saved_donumber} = $form->{donumber};
391 Common::webdav_folder($form);
393 $main::lxdebug->leave_sub();
398 sub mark_orders_if_delivered {
399 $main::lxdebug->enter_sub();
404 Common::check_params(\%params, qw(do_id type));
406 my $myconfig = \%main::myconfig;
407 my $form = $main::form;
409 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
411 my @links = RecordLinks->get_links('dbh' => $dbh,
412 'from_table' => 'oe',
413 'to_table' => 'delivery_orders',
414 'to_id' => $params{do_id});
416 my $oe_id = @links ? $links[0]->{from_id} : undef;
418 return $main::lxdebug->leave_sub() if (!$oe_id);
420 my $all_units = AM->retrieve_all_units();
422 my $query = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit
424 LEFT JOIN parts p ON (oi.parts_id = p.id)
425 WHERE (oi.trans_id = ?)|;
426 my $sth = prepare_execute_query($form, $dbh, $query, $oe_id);
428 my %shipped = $self->get_shipped_qty('type' => $params{type},
432 while (my $ref = $sth->fetchrow_hashref()) {
433 $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor};
435 if ($ordered{$ref->{parts_id}}) {
436 $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty};
438 $ordered{$ref->{parts_id}} = $ref;
444 map { $_->{baseqty} = $_->{qty} * $all_units->{$_->{unit}}->{factor} / $all_units->{$_->{partunit}}->{factor} } values %shipped;
447 foreach my $part (values %ordered) {
448 if (!$shipped{$part->{parts_id}} || ($shipped{$part->{parts_id}}->{baseqty} < $part->{baseqty})) {
455 $query = qq|UPDATE oe
458 do_query($form, $dbh, $query, $oe_id);
459 $dbh->commit() if (!$params{dbh});
462 $main::lxdebug->leave_sub();
466 $main::lxdebug->enter_sub();
471 Common::check_params(\%params, qw(ids));
473 if (('ARRAY' ne ref $params{ids}) || !scalar @{ $params{ids} }) {
474 $main::lxdebug->leave_sub();
478 my $myconfig = \%main::myconfig;
479 my $form = $main::form;
481 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
483 my $query = qq|UPDATE delivery_orders SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar(@{ $params{ids} })) . qq|)|;
485 do_query($form, $dbh, $query, map { conv_i($_) } @{ $params{ids} });
487 $dbh->commit() unless ($params{dbh});
489 $main::lxdebug->leave_sub();
493 $main::lxdebug->enter_sub();
497 my $myconfig = \%main::myconfig;
498 my $form = $main::form;
499 my $spool = $::lx_office_conf{paths}->{spool};
501 # connect to database
502 my $dbh = $form->get_standard_dbh($myconfig);
505 my $query = qq|SELECT s.spoolfile FROM status s WHERE s.trans_id = ?|;
506 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
512 while (($spoolfile) = $sth->fetchrow_array) {
513 push @spoolfiles, $spoolfile;
518 @values = (conv_i($form->{id}));
520 # delete status entries
521 $query = qq|DELETE FROM status
523 do_query($form, $dbh, $query, @values);
525 # delete individual entries
526 $query = qq|DELETE FROM delivery_order_items_stock
527 WHERE delivery_order_item_id IN (
528 SELECT id FROM delivery_order_items
529 WHERE delivery_order_id = ?
531 do_query($form, $dbh, $query, @values);
533 # delete individual entries
534 $query = qq|DELETE FROM delivery_order_items
535 WHERE delivery_order_id = ?|;
536 do_query($form, $dbh, $query, @values);
539 $query = qq|DELETE FROM delivery_orders
541 do_query($form, $dbh, $query, @values);
543 $query = qq|DELETE FROM shipto
544 WHERE trans_id = ? AND module = 'DO'|;
545 do_query($form, $dbh, $query, @values);
547 my $rc = $dbh->commit();
550 foreach $spoolfile (@spoolfiles) {
551 unlink "$spool/$spoolfile" if $spoolfile;
555 $main::lxdebug->leave_sub();
561 $main::lxdebug->enter_sub();
566 my $myconfig = \%main::myconfig;
567 my $form = $main::form;
569 # connect to database
570 my $dbh = $form->get_standard_dbh($myconfig);
572 my ($query, $query_add, @values, $sth, $ref);
574 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
577 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
579 my $mode = !$params{ids} ? 'default' : ref $params{ids} eq 'ARRAY' ? 'multi' : 'single';
581 if ($mode eq 'default') {
582 $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate|);
583 map { $form->{$_} = $ref->{$_} } keys %$ref;
585 # if reqdate is not set from oe-workflow, set it to transdate (which is current date)
586 $form->{reqdate} ||= $form->{transdate};
589 $form->lastname_used($dbh, $myconfig, $vc) unless $form->{"${vc}_id"};
591 $main::lxdebug->leave_sub();
596 my @do_ids = map { conv_i($_) } ($mode eq 'multi' ? @{ $params{ids} } : ($params{ids}));
597 my $do_ids_placeholders = join(', ', ('?') x scalar(@do_ids));
599 # retrieve order for single id
600 # NOTE: this query is intended to fetch all information only ONCE.
601 # so if any of these infos is important (or even different) for any item,
602 # it will be killed out and then has to be fetched from the item scope query further down
604 qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.transdate, dord.reqdate,
605 dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
606 e.name AS employee, dord.employee_id, dord.salesman_id,
607 dord.${vc}_id, cv.name AS ${vc},
608 dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
609 d.description AS department, dord.language_id,
611 dord.globalproject_id, dord.delivered, dord.transaction_description,
612 dord.taxzone_id, dord.taxincluded, dord.terms, dord.curr AS currency
613 FROM delivery_orders dord
614 JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
615 LEFT JOIN employee e ON (dord.employee_id = e.id)
616 LEFT JOIN department d ON (dord.department_id = d.id)
617 WHERE dord.id IN ($do_ids_placeholders)|;
618 $sth = prepare_execute_query($form, $dbh, $query, @do_ids);
620 delete $form->{"${vc}_id"};
621 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
622 if ($form->{"${vc}_id"} && ($ref->{"${vc}_id"} != $form->{"${vc}_id"})) {
624 $main::lxdebug->leave_sub();
629 map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
630 $form->{donumber_array} .= $form->{donumber} . ' ';
634 # remove any trailing whitespace
635 $form->{currency} =~ s/\s*$//;
637 $form->{donumber_array} =~ s/\s*$//g;
639 $form->{saved_donumber} = $form->{donumber};
641 # if not given, fill transdate with current_date
642 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
644 if ($mode eq 'single') {
645 $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'DO'|;
646 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
648 $ref = $sth->fetchrow_hashref("NAME_lc");
650 map { $form->{$_} = $ref->{$_} } keys %$ref;
653 # get printed, emailed and queued
654 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
655 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
657 while ($ref = $sth->fetchrow_hashref("NAME_lc")) {
658 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
659 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
660 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
663 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
669 my %oid = ('Pg' => 'oid',
670 'Oracle' => 'rowid');
672 # retrieve individual items
673 # this query looks up all information about the items
674 # stuff different from the whole will not be overwritten, but saved with a suffix.
676 qq|SELECT doi.id AS delivery_order_items_id,
677 p.partnumber, p.assembly, p.listprice, doi.description, doi.qty,
678 doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.bin, p.notes AS partnotes,
679 doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
680 doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
681 doi.price_factor_id, doi.price_factor, doi.marge_price_factor, doi.pricegroup_id,
682 pr.projectnumber, dord.transdate AS dord_transdate,
684 FROM delivery_order_items doi
685 JOIN parts p ON (doi.parts_id = p.id)
686 JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
687 LEFT JOIN project pr ON (doi.project_id = pr.id)
688 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
689 WHERE doi.delivery_order_id IN ($do_ids_placeholders)
690 ORDER BY doi.$oid{$myconfig->{dbdriver}}|;
692 $form->{form_details} = selectall_hashref_query($form, $dbh, $query, @do_ids);
694 # Retrieve custom variables.
695 foreach my $doi (@{ $form->{form_details} }) {
696 my $cvars = CVar->get_custom_variables(dbh => $dbh,
698 sub_module => 'delivery_order_items',
699 trans_id => $doi->{delivery_order_items_id},
701 map { $doi->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
704 if ($mode eq 'single') {
705 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
708 qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber, bestbefore
709 FROM delivery_order_items_stock
710 WHERE delivery_order_item_id = ?|;
711 my $sth = prepare_query($form, $dbh, $query);
713 foreach my $doi (@{ $form->{form_details} }) {
714 do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
716 while (my $ref = $sth->fetchrow_hashref()) {
717 push @{ $requests }, $ref;
720 $doi->{"stock_${in_out}"} = YAML::Dump($requests);
726 Common::webdav_folder($form);
728 $main::lxdebug->leave_sub();
734 $main::lxdebug->enter_sub();
738 my $myconfig = \%main::myconfig;
739 my $form = $main::form;
741 # connect to database
742 my $dbh = $form->get_standard_dbh($myconfig);
752 my %oid = ('Pg' => 'oid',
753 'Oracle' => 'rowid');
755 my (@project_ids, %projectnumbers, %projectdescriptions);
757 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
759 # sort items by partsgroup
760 for $i (1 .. $form->{rowcount}) {
762 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
763 $partsgroup = $form->{"partsgroup_$i"};
765 push @partsgroup, [$i, $partsgroup];
766 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
770 $query = "SELECT id, projectnumber, description FROM project WHERE id IN (" .
771 join(", ", map("?", @project_ids)) . ")";
772 $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
773 while (my $ref = $sth->fetchrow_hashref()) {
774 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
775 $projectdescriptions{$ref->{id}} = $ref->{description};
780 $form->{"globalprojectnumber"} =
781 $projectnumbers{$form->{"globalproject_id"}};
782 $form->{"globalprojectdescription"} =
783 $projectdescriptions{$form->{"globalproject_id"}};
785 my $q_pg = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
787 JOIN parts p ON (a.parts_id = p.id)
788 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
791 my $h_pg = prepare_query($form, $dbh, $q_pg);
793 my $q_bin_wh = qq|SELECT (SELECT description FROM bin WHERE id = ?) AS bin,
794 (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
795 my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
797 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
801 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
803 $form->{TEMPLATE_ARRAYS} = { };
804 IC->prepare_parts_for_printing();
807 qw(runningnumber number description longdescription qty unit
808 partnotes serialnumber reqdate projectnumber projectdescription
809 si_runningnumber si_number si_description
810 si_warehouse si_bin si_chargenumber si_bestbefore si_qty si_unit);
812 map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays);
814 push @arrays, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
816 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
817 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
820 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
823 next if (!$form->{"id_$i"});
827 if ($item->[1] ne $sameitem) {
828 push(@{ $form->{description} }, qq|$item->[1]|);
829 $sameitem = $item->[1];
831 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
834 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
836 # add number, description and qty to $form->{number}, ....
838 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
840 push @{ $form->{TEMPLATE_ARRAYS}{runningnumber} }, $position;
841 push @{ $form->{TEMPLATE_ARRAYS}{number} }, $form->{"partnumber_$i"};
842 push @{ $form->{TEMPLATE_ARRAYS}{description} }, $form->{"description_$i"};
843 push @{ $form->{TEMPLATE_ARRAYS}{longdescription} }, $form->{"longdescription_$i"};
844 push @{ $form->{TEMPLATE_ARRAYS}{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"});
845 push @{ $form->{TEMPLATE_ARRAYS}{qty_nofmt} }, $form->{"qty_$i"};
846 push @{ $form->{TEMPLATE_ARRAYS}{unit} }, $form->{"unit_$i"};
847 push @{ $form->{TEMPLATE_ARRAYS}{partnotes} }, $form->{"partnotes_$i"};
848 push @{ $form->{TEMPLATE_ARRAYS}{serialnumber} }, $form->{"serialnumber_$i"};
849 push @{ $form->{TEMPLATE_ARRAYS}{reqdate} }, $form->{"reqdate_$i"};
850 push @{ $form->{TEMPLATE_ARRAYS}{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}};
851 push @{ $form->{TEMPLATE_ARRAYS}{projectdescription} },
852 $projectdescriptions{$form->{"project_id_$i"}};
854 if ($form->{"assembly_$i"}) {
857 # get parts and push them onto the stack
859 if ($form->{groupitems}) {
861 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
863 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
866 do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
868 while (my $ref = $h_pg->fetchrow_hashref("NAME_lc")) {
869 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
870 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
871 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
872 push(@{ $form->{description} }, $sameitem);
874 push(@{ $form->{description} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq|, $ref->{partnumber}, $ref->{description}|);
876 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
880 if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
881 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
883 foreach my $si (@{ $stock_info }) {
886 do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
887 my $bin_wh = $h_bin_wh->fetchrow_hashref();
889 push @{ $form->{TEMPLATE_ARRAYS}{si_runningnumber}[$position-1] }, $num_si;
890 push @{ $form->{TEMPLATE_ARRAYS}{si_number}[$position-1] }, $form->{"partnumber_$i"};
891 push @{ $form->{TEMPLATE_ARRAYS}{si_description}[$position-1] }, $form->{"description_$i"};
892 push @{ $form->{TEMPLATE_ARRAYS}{si_warehouse}[$position-1] }, $bin_wh->{warehouse};
893 push @{ $form->{TEMPLATE_ARRAYS}{si_bin}[$position-1] }, $bin_wh->{bin};
894 push @{ $form->{TEMPLATE_ARRAYS}{si_chargenumber}[$position-1] }, $si->{chargenumber};
895 push @{ $form->{TEMPLATE_ARRAYS}{si_bestbefore}[$position-1] }, $si->{bestbefore};
896 push @{ $form->{TEMPLATE_ARRAYS}{si_qty}[$position-1] }, $form->format_amount($myconfig, $si->{qty} * 1);
897 push @{ $form->{TEMPLATE_ARRAYS}{si_qty_nofmt}[$position-1] }, $si->{qty} * 1;
898 push @{ $form->{TEMPLATE_ARRAYS}{si_unit}[$position-1] }, $si->{unit};
902 push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
903 CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
904 for @{ $ic_cvar_configs };
910 $form->{username} = $myconfig->{name};
912 $main::lxdebug->leave_sub();
915 sub project_description {
916 $main::lxdebug->enter_sub();
918 my ($self, $dbh, $id) = @_;
920 my $form = $main::form;
922 my $query = qq|SELECT description FROM project WHERE id = ?|;
923 my ($value) = selectrow_query($form, $dbh, $query, $id);
925 $main::lxdebug->leave_sub();
930 sub unpack_stock_information {
931 $main::lxdebug->enter_sub();
936 Common::check_params_x(\%params, qw(packed));
940 eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
942 $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
944 foreach my $entry (@{ $unpacked }) {
945 next if ('HASH' eq ref $entry);
950 $main::lxdebug->leave_sub();
955 sub get_item_availability {
956 $::lxdebug->enter_sub;
961 Common::check_params(\%params, qw(parts_id));
963 my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
966 qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, SUM(qty) AS qty, i.parts_id,
967 w.description AS warehousedescription,
968 b.description AS bindescription
970 LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
971 LEFT JOIN bin b ON (i.bin_id = b.id)
972 WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
973 GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, i.parts_id, w.description, b.description
975 ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber), i.bestbefore
977 my $contents = selectall_hashref_query($::form, $::form->get_standard_dbh, $query, @parts_ids);
979 $::lxdebug->leave_sub;
981 return @{ $contents };
985 sub check_stock_availability {
986 $main::lxdebug->enter_sub();
991 Common::check_params(\%params, qw(requests parts_id));
993 my $myconfig = \%main::myconfig;
994 my $form = $main::form;
996 my $dbh = $form->get_standard_dbh($myconfig);
998 my $units = AM->retrieve_units($myconfig, $form);
1000 my ($partunit) = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
1001 my $unit_factor = $units->{$partunit}->{factor} || 1;
1003 my @contents = $self->get_item_availability(%params);
1007 foreach my $sinfo (@{ $params{requests} }) {
1010 foreach my $row (@contents) {
1011 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
1012 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1013 ($row->{chargenumber} ne $sinfo->{chargenumber}) ||
1014 ($row->{bestbefore} ne $sinfo->{bestbefore}));
1018 my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
1020 if ($base_qty > $row->{qty}) {
1021 $sinfo->{error} = 1;
1022 push @errors, $sinfo;
1028 push @errors, $sinfo if (!$found);
1031 $main::lxdebug->leave_sub();
1036 sub transfer_in_out {
1037 $main::lxdebug->enter_sub();
1042 Common::check_params(\%params, qw(direction requests));
1044 if (!@{ $params{requests} }) {
1045 $main::lxdebug->leave_sub();
1049 my $myconfig = \%main::myconfig;
1050 my $form = $main::form;
1052 my $prefix = $params{direction} eq 'in' ? 'dst' : 'src';
1056 foreach my $request (@{ $params{requests} }) {
1058 'parts_id' => $request->{parts_id},
1059 "${prefix}_warehouse_id" => $request->{warehouse_id},
1060 "${prefix}_bin_id" => $request->{bin_id},
1061 'chargenumber' => $request->{chargenumber},
1062 'bestbefore' => $request->{bestbefore},
1063 'qty' => $request->{qty},
1064 'unit' => $request->{unit},
1065 'oe_id' => $form->{id},
1066 'shippingdate' => 'current_date',
1067 'transfer_type' => $params{direction} eq 'in' ? 'stock' : 'shipped',
1068 'project_id' => $request->{project_id},
1072 WH->transfer(@transfers);
1074 $main::lxdebug->leave_sub();
1077 sub get_shipped_qty {
1078 $main::lxdebug->enter_sub();
1083 Common::check_params(\%params, qw(type oe_id));
1085 my $myconfig = \%main::myconfig;
1086 my $form = $main::form;
1088 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1090 my @links = RecordLinks->get_links('dbh' => $dbh,
1091 'from_table' => 'oe',
1092 'from_id' => $params{oe_id},
1093 'to_table' => 'delivery_orders');
1094 my @values = map { $_->{to_id} } @links;
1096 if (!scalar @values) {
1097 $main::lxdebug->leave_sub();
1102 qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit
1103 FROM delivery_order_items doi
1104 LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id)
1105 LEFT JOIN parts p ON (doi.parts_id = p.id)
1106 WHERE o.id IN (| . join(', ', ('?') x scalar @values) . qq|)|;
1109 my $entries = selectall_hashref_query($form, $dbh, $query, @values);
1110 my $all_units = AM->retrieve_all_units();
1112 foreach my $entry (@{ $entries }) {
1113 $entry->{qty} *= AM->convert_unit($entry->{unit}, $entry->{partunit}, $all_units);
1115 if (!$ship{$entry->{parts_id}}) {
1116 $ship{$entry->{parts_id}} = $entry;
1118 $ship{$entry->{parts_id}}->{qty} += $entry->{qty};
1122 $main::lxdebug->leave_sub();
1127 sub is_marked_as_delivered {
1128 $main::lxdebug->enter_sub();
1133 Common::check_params(\%params, qw(id));
1135 my $myconfig = \%main::myconfig;
1136 my $form = $main::form;
1138 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1140 my ($delivered) = selectfirst_array_query($form, $dbh, qq|SELECT delivered FROM delivery_orders WHERE id = ?|, conv_i($params{id}));
1142 $main::lxdebug->leave_sub();
1144 return $delivered ? 1 : 0;