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,
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,
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)
81 push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
83 if ($form->{department_id}) {
84 push @where, qq|dord.department_id = ?|;
85 push @values, conv_i($form->{department_id});
88 if ($form->{project_id}) {
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});
96 if ($form->{"${vc}_id"}) {
97 push @where, qq|dord.${vc}_id = ?|;
98 push @values, $form->{"${vc}_id"};
100 } elsif ($form->{$vc}) {
101 push @where, qq|ct.name ILIKE ?|;
102 push @values, '%' . $form->{$vc} . '%';
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});
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};
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} . '%';
121 if (($form->{open} || $form->{closed}) &&
122 ($form->{open} ne $form->{closed})) {
123 push @where, ($form->{open} ? "NOT " : "") . "COALESCE(dord.closed, FALSE)";
126 if (($form->{notdelivered} || $form->{delivered}) &&
127 ($form->{notdelivered} ne $form->{delivered})) {
128 push @where, ($form->{delivered} ? "" : "NOT ") . "COALESCE(dord.delivered, FALSE)";
131 if($form->{transdatefrom}) {
132 push @where, qq|dord.transdate >= ?|;
133 push @values, conv_date($form->{transdatefrom});
136 if($form->{transdateto}) {
137 push @where, qq|dord.transdate <= ?|;
138 push @values, conv_date($form->{transdateto});
142 $query .= " WHERE " . join(" AND ", map { "($_)" } @where);
145 my %allowed_sort_columns = (
146 "transdate" => "dord.transdate",
147 "reqdate" => "dord.reqdate",
149 "donumber" => "dord.donumber",
150 "ordnumber" => "dord.ordnumber",
152 "employee" => "e.name",
153 "salesman" => "sm.name",
154 "shipvia" => "dord.shipvia",
155 "transaction_description" => "dord.transaction_description",
156 "department" => "lower(dep.description)",
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}};
165 $query .= qq| ORDER by | . $sortorder . " $sortdir";
167 $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
169 if (scalar @{ $form->{DO} }) {
173 WHERE NOT COALESCE(quotation, FALSE)
175 AND (COALESCE(${vc}_id, 0) != 0)|;
177 my $sth = prepare_query($form, $dbh, $query);
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();
188 $main::lxdebug->leave_sub();
192 $main::lxdebug->enter_sub();
196 my $myconfig = \%main::myconfig;
197 my $form = $main::form;
199 # connect to database, turn off autocommit
200 my $dbh = $form->get_standard_dbh($myconfig);
202 my ($query, @values, $sth, $null);
204 my $all_units = AM->retrieve_units($myconfig, $form);
205 $form->{all_units} = $all_units;
207 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
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});
214 my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
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}));
221 $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
222 do_query($form, $dbh, $query, conv_i($form->{id}));
224 $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
225 do_query($form, $dbh, $query, conv_i($form->{id}));
229 $query = qq|SELECT nextval('id')|;
230 ($form->{id}) = selectrow_query($form, $dbh, $query);
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}));
239 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
240 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
243 my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
244 my @part_ids = keys %part_id_map;
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);
252 my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
253 my $h_item_id = prepare_query($form, $dbh, $q_item_id);
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);
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);
270 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
272 for my $i (1 .. $form->{rowcount}) {
273 next if (!$form->{"id_$i"});
275 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
277 my $item_unit = $part_unit_map{$form->{"id_$i"}};
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};
283 my $baseqty = $form->{"qty_$i"} * $basefactor;
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"});
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);
293 $items_reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
295 do_statement($form, $h_item_id, $q_item_id);
296 my ($item_id) = $h_item_id->fetchrow_array();
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
302 my $pricegroup_id = $form->{"pricegroup_id_$i"} * 1;
303 $pricegroup_id = undef if !$pricegroup_id;
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"}),
318 do_statement($form, $h_item, $q_item, @values);
320 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
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);
328 CVar->save_custom_variables(module => 'IC',
329 sub_module => 'delivery_order_items',
330 trans_id => $item_id,
331 configs => $ic_cvar_configs,
333 name_prefix => 'ic_',
334 name_postfix => "_$i",
338 $h_item_id->finish();
340 $h_item_stock->finish();
343 # reqdate is last items reqdate (?: old behaviour) if not already set
344 $form->{reqdate} ||= $items_reqdate;
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 = ?)
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);
372 $form->{name} = $form->{ $form->{vc} };
373 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
375 if (!$form->{shipto_id}) {
376 $form->add_shipto($dbh, $form->{id}, "DO");
379 # save printed, emailed, queued
380 $form->save_status($dbh);
382 # Link this delivery order to the quotations it was created from.
383 RecordLinks->create_links('dbh' => $dbh,
385 'from_table' => 'oe',
386 'from_ids' => $form->{convert_from_oe_ids},
387 'to_table' => 'delivery_orders',
388 'to_id' => $form->{id},
390 delete $form->{convert_from_oe_ids};
392 $self->mark_orders_if_delivered('do_id' => $form->{id},
393 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
396 my $rc = $dbh->commit();
398 $form->{saved_donumber} = $form->{donumber};
400 Common::webdav_folder($form);
402 $main::lxdebug->leave_sub();
407 sub mark_orders_if_delivered {
408 $main::lxdebug->enter_sub();
413 Common::check_params(\%params, qw(do_id type));
415 my $myconfig = \%main::myconfig;
416 my $form = $main::form;
418 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
420 my @links = RecordLinks->get_links('dbh' => $dbh,
421 'from_table' => 'oe',
422 'to_table' => 'delivery_orders',
423 'to_id' => $params{do_id});
425 my $oe_id = @links ? $links[0]->{from_id} : undef;
427 return $main::lxdebug->leave_sub() if (!$oe_id);
429 my $all_units = AM->retrieve_all_units();
431 my $query = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit
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);
437 my %shipped = $self->get_shipped_qty('type' => $params{type},
441 while (my $ref = $sth->fetchrow_hashref()) {
442 $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor};
444 if ($ordered{$ref->{parts_id}}) {
445 $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty};
447 $ordered{$ref->{parts_id}} = $ref;
453 map { $_->{baseqty} = $_->{qty} * $all_units->{$_->{unit}}->{factor} / $all_units->{$_->{partunit}}->{factor} } values %shipped;
456 foreach my $part (values %ordered) {
457 if (!$shipped{$part->{parts_id}} || ($shipped{$part->{parts_id}}->{baseqty} < $part->{baseqty})) {
464 $query = qq|UPDATE oe
467 do_query($form, $dbh, $query, $oe_id);
468 $dbh->commit() if (!$params{dbh});
471 $main::lxdebug->leave_sub();
475 $main::lxdebug->enter_sub();
480 Common::check_params(\%params, qw(ids));
482 if (('ARRAY' ne ref $params{ids}) || !scalar @{ $params{ids} }) {
483 $main::lxdebug->leave_sub();
487 my $myconfig = \%main::myconfig;
488 my $form = $main::form;
490 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
492 my $query = qq|UPDATE delivery_orders SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar(@{ $params{ids} })) . qq|)|;
494 do_query($form, $dbh, $query, map { conv_i($_) } @{ $params{ids} });
496 $dbh->commit() unless ($params{dbh});
498 $main::lxdebug->leave_sub();
502 $main::lxdebug->enter_sub();
506 my $myconfig = \%main::myconfig;
507 my $form = $main::form;
508 my $spool = $::lx_office_conf{paths}->{spool};
510 # connect to database
511 my $dbh = $form->get_standard_dbh($myconfig);
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}));
521 while (($spoolfile) = $sth->fetchrow_array) {
522 push @spoolfiles, $spoolfile;
527 @values = (conv_i($form->{id}));
529 # delete status entries
530 $query = qq|DELETE FROM status
532 do_query($form, $dbh, $query, @values);
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 = ?
540 do_query($form, $dbh, $query, @values);
542 # delete individual entries
543 $query = qq|DELETE FROM delivery_order_items
544 WHERE delivery_order_id = ?|;
545 do_query($form, $dbh, $query, @values);
548 $query = qq|DELETE FROM delivery_orders
550 do_query($form, $dbh, $query, @values);
552 $query = qq|DELETE FROM shipto
553 WHERE trans_id = ? AND module = 'DO'|;
554 do_query($form, $dbh, $query, @values);
556 my $rc = $dbh->commit();
559 foreach $spoolfile (@spoolfiles) {
560 unlink "$spool/$spoolfile" if $spoolfile;
564 $main::lxdebug->leave_sub();
570 $main::lxdebug->enter_sub();
575 my $myconfig = \%main::myconfig;
576 my $form = $main::form;
578 # connect to database
579 my $dbh = $form->get_standard_dbh($myconfig);
581 my ($query, $query_add, @values, $sth, $ref);
583 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
586 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
588 my $mode = !$params{ids} ? 'default' : ref $params{ids} eq 'ARRAY' ? 'multi' : 'single';
590 if ($mode eq 'default') {
591 $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate|);
592 map { $form->{$_} = $ref->{$_} } keys %$ref;
594 # if reqdate is not set from oe-workflow, set it to transdate (which is current date)
595 $form->{reqdate} ||= $form->{transdate};
598 $form->lastname_used($dbh, $myconfig, $vc) unless $form->{"${vc}_id"};
600 $main::lxdebug->leave_sub();
605 my @do_ids = map { conv_i($_) } ($mode eq 'multi' ? @{ $params{ids} } : ($params{ids}));
606 my $do_ids_placeholders = join(', ', ('?') x scalar(@do_ids));
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
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,
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);
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"})) {
633 $main::lxdebug->leave_sub();
638 map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
639 $form->{donumber_array} .= $form->{donumber} . ' ';
643 $form->{donumber_array} =~ s/\s*$//g;
645 $form->{saved_donumber} = $form->{donumber};
647 # if not given, fill transdate with current_date
648 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
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});
654 $ref = $sth->fetchrow_hashref("NAME_lc");
656 map { $form->{$_} = $ref->{$_} } keys %$ref;
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}));
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};
669 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
675 # retrieve individual items
676 # this query looks up all information about the items
677 # stuff different from the whole will not be overwritten, but saved with a suffix.
679 qq|SELECT doi.id AS delivery_order_items_id,
680 p.partnumber, p.assembly, p.listprice, doi.description, doi.qty,
681 doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.notes AS partnotes,
682 doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
683 doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
684 doi.price_factor_id, doi.price_factor, doi.marge_price_factor, doi.pricegroup_id,
685 pr.projectnumber, dord.transdate AS dord_transdate,
687 FROM delivery_order_items doi
688 JOIN parts p ON (doi.parts_id = p.id)
689 JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
690 LEFT JOIN project pr ON (doi.project_id = pr.id)
691 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
692 WHERE doi.delivery_order_id IN ($do_ids_placeholders)
695 $form->{form_details} = selectall_hashref_query($form, $dbh, $query, @do_ids);
697 # Retrieve custom variables.
698 foreach my $doi (@{ $form->{form_details} }) {
699 my $cvars = CVar->get_custom_variables(dbh => $dbh,
701 sub_module => 'delivery_order_items',
702 trans_id => $doi->{delivery_order_items_id},
704 map { $doi->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
707 if ($mode eq 'single') {
708 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
711 qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber, bestbefore
712 FROM delivery_order_items_stock
713 WHERE delivery_order_item_id = ?|;
714 my $sth = prepare_query($form, $dbh, $query);
716 foreach my $doi (@{ $form->{form_details} }) {
717 do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
719 while (my $ref = $sth->fetchrow_hashref()) {
720 push @{ $requests }, $ref;
723 $doi->{"stock_${in_out}"} = YAML::Dump($requests);
729 Common::webdav_folder($form);
731 $main::lxdebug->leave_sub();
737 $main::lxdebug->enter_sub();
739 my ($self, $myconfig, $form) = @_;
741 # connect to database
742 my $dbh = $form->get_standard_dbh($myconfig);
752 my (@project_ids, %projectnumbers, %projectdescriptions);
754 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
756 # sort items by partsgroup
757 for $i (1 .. $form->{rowcount}) {
759 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
760 $partsgroup = $form->{"partsgroup_$i"};
762 push @partsgroup, [$i, $partsgroup];
763 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
767 $query = "SELECT id, projectnumber, description FROM project WHERE id IN (" .
768 join(", ", map("?", @project_ids)) . ")";
769 $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
770 while (my $ref = $sth->fetchrow_hashref()) {
771 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
772 $projectdescriptions{$ref->{id}} = $ref->{description};
777 $form->{"globalprojectnumber"} =
778 $projectnumbers{$form->{"globalproject_id"}};
779 $form->{"globalprojectdescription"} =
780 $projectdescriptions{$form->{"globalproject_id"}};
782 my $q_pg = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
784 JOIN parts p ON (a.parts_id = p.id)
785 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
788 my $h_pg = prepare_query($form, $dbh, $q_pg);
790 my $q_bin_wh = qq|SELECT (SELECT description FROM bin WHERE id = ?) AS bin,
791 (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
792 my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
794 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
798 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
800 $form->{TEMPLATE_ARRAYS} = { };
801 IC->prepare_parts_for_printing(myconfig => $myconfig, form => $form);
804 qw(runningnumber number description longdescription qty unit
805 partnotes serialnumber reqdate projectnumber projectdescription
806 si_runningnumber si_number si_description
807 si_warehouse si_bin si_chargenumber si_bestbefore si_qty si_unit weight lineweight);
809 map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays);
811 push @arrays, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
813 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
814 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
818 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
821 next if (!$form->{"id_$i"});
825 if ($item->[1] ne $sameitem) {
826 push(@{ $form->{description} }, qq|$item->[1]|);
827 $sameitem = $item->[1];
829 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
832 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
834 # add number, description and qty to $form->{number}, ....
836 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
838 push @{ $form->{TEMPLATE_ARRAYS}{runningnumber} }, $position;
839 push @{ $form->{TEMPLATE_ARRAYS}{number} }, $form->{"partnumber_$i"};
840 push @{ $form->{TEMPLATE_ARRAYS}{description} }, $form->{"description_$i"};
841 push @{ $form->{TEMPLATE_ARRAYS}{longdescription} }, $form->{"longdescription_$i"};
842 push @{ $form->{TEMPLATE_ARRAYS}{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"});
843 push @{ $form->{TEMPLATE_ARRAYS}{qty_nofmt} }, $form->{"qty_$i"};
844 push @{ $form->{TEMPLATE_ARRAYS}{unit} }, $form->{"unit_$i"};
845 push @{ $form->{TEMPLATE_ARRAYS}{partnotes} }, $form->{"partnotes_$i"};
846 push @{ $form->{TEMPLATE_ARRAYS}{serialnumber} }, $form->{"serialnumber_$i"};
847 push @{ $form->{TEMPLATE_ARRAYS}{reqdate} }, $form->{"reqdate_$i"};
848 push @{ $form->{TEMPLATE_ARRAYS}{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}};
849 push @{ $form->{TEMPLATE_ARRAYS}{projectdescription} },
850 $projectdescriptions{$form->{"project_id_$i"}};
852 my $lineweight = $form->{"qty_$i"} * $form->{"weight_$i"};
853 $totalweight += $lineweight;
854 push @{ $form->{TEMPLATE_ARRAYS}->{weight} }, $form->format_amount($myconfig, $form->{"weight_$i"}, 3);
855 push @{ $form->{TEMPLATE_ARRAYS}->{weight_nofmt} }, $form->{"weight_$i"};
856 push @{ $form->{TEMPLATE_ARRAYS}->{lineweight} }, $form->format_amount($myconfig, $lineweight, 3);
857 push @{ $form->{TEMPLATE_ARRAYS}->{lineweight_nofmt} }, $lineweight;
859 if ($form->{"assembly_$i"}) {
862 # get parts and push them onto the stack
864 if ($form->{groupitems}) {
866 qq|ORDER BY pg.partsgroup, a.oid|;
868 $sortorder = qq|ORDER BY a.oid|;
871 do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
873 while (my $ref = $h_pg->fetchrow_hashref("NAME_lc")) {
874 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
875 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
876 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
877 push(@{ $form->{TEMPLATE_ARRAYS}->{description} }, $sameitem);
879 push(@{ $form->{TEMPLATE_ARRAYS}->{"description"} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq| -- $ref->{partnumber}, $ref->{description}|);
881 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
885 if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
886 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
888 foreach my $si (@{ $stock_info }) {
891 do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
892 my $bin_wh = $h_bin_wh->fetchrow_hashref();
894 push @{ $form->{TEMPLATE_ARRAYS}{si_runningnumber}[$position-1] }, $num_si;
895 push @{ $form->{TEMPLATE_ARRAYS}{si_number}[$position-1] }, $form->{"partnumber_$i"};
896 push @{ $form->{TEMPLATE_ARRAYS}{si_description}[$position-1] }, $form->{"description_$i"};
897 push @{ $form->{TEMPLATE_ARRAYS}{si_warehouse}[$position-1] }, $bin_wh->{warehouse};
898 push @{ $form->{TEMPLATE_ARRAYS}{si_bin}[$position-1] }, $bin_wh->{bin};
899 push @{ $form->{TEMPLATE_ARRAYS}{si_chargenumber}[$position-1] }, $si->{chargenumber};
900 push @{ $form->{TEMPLATE_ARRAYS}{si_bestbefore}[$position-1] }, $si->{bestbefore};
901 push @{ $form->{TEMPLATE_ARRAYS}{si_qty}[$position-1] }, $form->format_amount($myconfig, $si->{qty} * 1);
902 push @{ $form->{TEMPLATE_ARRAYS}{si_qty_nofmt}[$position-1] }, $si->{qty} * 1;
903 push @{ $form->{TEMPLATE_ARRAYS}{si_unit}[$position-1] }, $si->{unit};
907 push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
908 CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
909 for @{ $ic_cvar_configs };
912 $form->{totalweight} = $form->format_amount($myconfig, $totalweight, 3);
913 $form->{totalweight_nofmt} = $totalweight;
914 my $defaults = AM->get_defaults();
915 $form->{weightunit} = $defaults->{weightunit};
920 $form->{username} = $myconfig->{name};
922 $main::lxdebug->leave_sub();
925 sub project_description {
926 $main::lxdebug->enter_sub();
928 my ($self, $dbh, $id) = @_;
930 my $form = $main::form;
932 my $query = qq|SELECT description FROM project WHERE id = ?|;
933 my ($value) = selectrow_query($form, $dbh, $query, $id);
935 $main::lxdebug->leave_sub();
940 sub unpack_stock_information {
941 $main::lxdebug->enter_sub();
946 Common::check_params_x(\%params, qw(packed));
950 eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
952 $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
954 foreach my $entry (@{ $unpacked }) {
955 next if ('HASH' eq ref $entry);
960 $main::lxdebug->leave_sub();
965 sub get_item_availability {
966 $::lxdebug->enter_sub;
971 Common::check_params(\%params, qw(parts_id));
973 my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
976 qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, SUM(qty) AS qty, i.parts_id,
977 w.description AS warehousedescription,
978 b.description AS bindescription
980 LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
981 LEFT JOIN bin b ON (i.bin_id = b.id)
982 WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
983 GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, i.parts_id, w.description, b.description
985 ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber), i.bestbefore
987 my $contents = selectall_hashref_query($::form, $::form->get_standard_dbh, $query, @parts_ids);
989 $::lxdebug->leave_sub;
991 return @{ $contents };
995 sub check_stock_availability {
996 $main::lxdebug->enter_sub();
1001 Common::check_params(\%params, qw(requests parts_id));
1003 my $myconfig = \%main::myconfig;
1004 my $form = $main::form;
1006 my $dbh = $form->get_standard_dbh($myconfig);
1008 my $units = AM->retrieve_units($myconfig, $form);
1010 my ($partunit) = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
1011 my $unit_factor = $units->{$partunit}->{factor} || 1;
1013 my @contents = $self->get_item_availability(%params);
1017 foreach my $sinfo (@{ $params{requests} }) {
1020 foreach my $row (@contents) {
1021 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
1022 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1023 ($row->{chargenumber} ne $sinfo->{chargenumber}) ||
1024 ($row->{bestbefore} ne $sinfo->{bestbefore}));
1028 my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
1030 if ($base_qty > $row->{qty}) {
1031 $sinfo->{error} = 1;
1032 push @errors, $sinfo;
1038 push @errors, $sinfo if (!$found);
1041 $main::lxdebug->leave_sub();
1046 sub transfer_in_out {
1047 $main::lxdebug->enter_sub();
1052 Common::check_params(\%params, qw(direction requests));
1054 if (!@{ $params{requests} }) {
1055 $main::lxdebug->leave_sub();
1059 my $myconfig = \%main::myconfig;
1060 my $form = $main::form;
1062 my $prefix = $params{direction} eq 'in' ? 'dst' : 'src';
1066 foreach my $request (@{ $params{requests} }) {
1068 'parts_id' => $request->{parts_id},
1069 "${prefix}_warehouse_id" => $request->{warehouse_id},
1070 "${prefix}_bin_id" => $request->{bin_id},
1071 'chargenumber' => $request->{chargenumber},
1072 'bestbefore' => $request->{bestbefore},
1073 'qty' => $request->{qty},
1074 'unit' => $request->{unit},
1075 'oe_id' => $form->{id},
1076 'shippingdate' => 'current_date',
1077 'transfer_type' => $params{direction} eq 'in' ? 'stock' : 'shipped',
1078 'project_id' => $request->{project_id},
1082 WH->transfer(@transfers);
1084 $main::lxdebug->leave_sub();
1087 sub get_shipped_qty {
1088 $main::lxdebug->enter_sub();
1093 Common::check_params(\%params, qw(type oe_id));
1095 my $myconfig = \%main::myconfig;
1096 my $form = $main::form;
1098 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1100 my @links = RecordLinks->get_links('dbh' => $dbh,
1101 'from_table' => 'oe',
1102 'from_id' => $params{oe_id},
1103 'to_table' => 'delivery_orders');
1104 my @values = map { $_->{to_id} } @links;
1106 if (!scalar @values) {
1107 $main::lxdebug->leave_sub();
1112 qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit
1113 FROM delivery_order_items doi
1114 LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id)
1115 LEFT JOIN parts p ON (doi.parts_id = p.id)
1116 WHERE o.id IN (| . join(', ', ('?') x scalar @values) . qq|)|;
1119 my $entries = selectall_hashref_query($form, $dbh, $query, @values);
1120 my $all_units = AM->retrieve_all_units();
1122 foreach my $entry (@{ $entries }) {
1123 $entry->{qty} *= AM->convert_unit($entry->{unit}, $entry->{partunit}, $all_units);
1125 if (!$ship{$entry->{parts_id}}) {
1126 $ship{$entry->{parts_id}} = $entry;
1128 $ship{$entry->{parts_id}}->{qty} += $entry->{qty};
1132 $main::lxdebug->leave_sub();
1137 sub is_marked_as_delivered {
1138 $main::lxdebug->enter_sub();
1143 Common::check_params(\%params, qw(id));
1145 my $myconfig = \%main::myconfig;
1146 my $form = $main::form;
1148 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1150 my ($delivered) = selectfirst_array_query($form, $dbh, qq|SELECT delivered FROM delivery_orders WHERE id = ?|, conv_i($params{id}));
1152 $main::lxdebug->leave_sub();
1154 return $delivered ? 1 : 0;