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);
47 $main::lxdebug->enter_sub();
51 my $myconfig = \%main::myconfig;
52 my $form = $main::form;
55 my $dbh = $form->get_standard_dbh($myconfig);
57 my (@where, @values, $where);
59 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
62 qq|SELECT dord.id, dord.donumber, dord.ordnumber, dord.transdate,
63 ct.name, dord.${vc}_id, dord.globalproject_id,
64 dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
65 dord.transaction_description,
66 pr.projectnumber AS globalprojectnumber,
69 FROM delivery_orders dord
70 LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
71 LEFT JOIN employee e ON (dord.employee_id = e.id)
72 LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
73 LEFT JOIN project pr ON (dord.globalproject_id = pr.id)|;
75 push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
77 my $department_id = (split /--/, $form->{department})[1];
79 push @where, qq|dord.department_id = ?|;
80 push @values, conv_i($department_id);
83 if ($form->{project_id}) {
85 qq|(dord.globalproject_id = ?) OR EXISTS
86 (SELECT * FROM delivery_order_items doi
87 WHERE (doi.project_id = ?) AND (oi.delivery_order_id = dord.id))|;
88 push @values, conv_i($form->{project_id}), conv_i($form->{project_id});
91 if ($form->{"${vc}_id"}) {
92 push @where, qq|dord.${vc}_id = ?|;
93 push @values, $form->{"${vc}_id"};
95 } elsif ($form->{$vc}) {
96 push @where, qq|ct.name ILIKE ?|;
97 push @values, '%' . $form->{$vc} . '%';
100 foreach my $item (qw(employee_id salesman_id)) {
101 next unless ($form->{$item});
102 push @where, "dord.$item = ?";
103 push @values, conv_i($form->{$item});
106 foreach my $item (qw(donumber ordnumber cusordnumber transaction_description)) {
107 next unless ($form->{$item});
108 push @where, qq|dord.$item ILIKE ?|;
109 push @values, '%' . $form->{$item} . '%';
112 if (($form->{open} || $form->{closed}) &&
113 ($form->{open} ne $form->{closed})) {
114 push @where, ($form->{open} ? "NOT " : "") . "COALESCE(dord.closed, FALSE)";
117 if (($form->{notdelivered} || $form->{delivered}) &&
118 ($form->{notdelivered} ne $form->{delivered})) {
119 push @where, ($form->{delivered} ? "" : "NOT ") . "COALESCE(dord.delivered, FALSE)";
122 if($form->{transdatefrom}) {
123 push @where, qq|dord.transdate >= ?|;
124 push @values, conv_date($form->{transdatefrom});
127 if($form->{transdateto}) {
128 push @where, qq|dord.transdate <= ?|;
129 push @values, conv_date($form->{transdateto});
133 $query .= " WHERE " . join(" AND ", map { "($_)" } @where);
136 my %allowed_sort_columns = (
137 "transdate" => "dord.transdate",
139 "donumber" => "dord.donumber",
140 "ordnumber" => "dord.ordnumber",
142 "employee" => "e.name",
143 "salesman" => "sm.name",
144 "shipvia" => "dord.shipvia",
145 "transaction_description" => "dord.transaction_description"
148 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
149 my $sortorder = "dord.id";
150 if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
151 $sortorder = $allowed_sort_columns{$form->{sort}};
154 $query .= qq| ORDER by | . $sortorder . " $sortdir";
156 $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
158 if (scalar @{ $form->{DO} }) {
162 WHERE NOT COALESCE(quotation, FALSE)
164 AND (COALESCE(${vc}_id, 0) != 0)|;
166 my $sth = prepare_query($form, $dbh, $query);
168 foreach my $dord (@{ $form->{DO} }) {
169 next unless ($dord->{ordnumber});
170 do_statement($form, $sth, $query, $dord->{ordnumber});
171 ($dord->{oe_id}) = $sth->fetchrow_array();
177 $main::lxdebug->leave_sub();
181 $main::lxdebug->enter_sub();
185 my $myconfig = \%main::myconfig;
186 my $form = $main::form;
188 # connect to database, turn off autocommit
189 my $dbh = $form->get_standard_dbh($myconfig);
191 my ($query, @values, $sth, $null);
193 my $all_units = AM->retrieve_units($myconfig, $form);
194 $form->{all_units} = $all_units;
196 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
199 $form->{donumber} = $form->update_defaults($myconfig, $form->{type} eq 'sales_delivery_order' ? 'sdonumber' : 'pdonumber', $dbh) unless $form->{donumber};
200 $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
201 $form->get_employee($dbh) unless ($form->{employee_id});
203 my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
207 $query = qq|DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = ?)|;
208 do_query($form, $dbh, $query, conv_i($form->{id}));
210 $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
211 do_query($form, $dbh, $query, conv_i($form->{id}));
213 $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
214 do_query($form, $dbh, $query, conv_i($form->{id}));
218 $query = qq|SELECT nextval('id')|;
219 ($form->{id}) = selectrow_query($form, $dbh, $query);
221 $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id) VALUES (?, '', ?)|;
222 do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}));
228 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
229 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
232 my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
233 my @part_ids = keys %part_id_map;
237 $query = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
238 %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
241 my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
242 my $h_item_id = prepare_query($form, $dbh, $q_item_id);
245 qq|INSERT INTO delivery_order_items (
246 id, delivery_order_id, parts_id, description, longdescription, qty, base_qty,
247 sellprice, discount, unit, reqdate, project_id, serialnumber,
248 ordnumber, transdate, cusordnumber,
249 lastcost, price_factor_id, price_factor, marge_price_factor)
250 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
251 (SELECT factor FROM price_factors WHERE id = ?), ?)|;
252 my $h_item = prepare_query($form, $dbh, $q_item);
255 qq|INSERT INTO delivery_order_items_stock (delivery_order_item_id, qty, unit, warehouse_id, bin_id, chargenumber)
256 VALUES (?, ?, ?, ?, ?, ?)|;
257 my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
259 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
261 for my $i (1 .. $form->{rowcount}) {
262 next if (!$form->{"id_$i"});
264 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
266 my $item_unit = $part_unit_map{$form->{"id_$i"}};
269 if (defined($all_units->{$item_unit}->{factor}) && (($all_units->{$item_unit}->{factor} * 1) != 0)) {
270 $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
272 my $baseqty = $form->{"qty_$i"} * $basefactor;
274 $form->{"lastcost_$i"} *= 1;
276 # set values to 0 if nothing entered
277 $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
278 $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
280 $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
281 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
283 $reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
285 do_statement($form, $h_item_id, $q_item_id);
286 my ($item_id) = $h_item_id->fetchrow_array();
288 # save detail record in delivery_order_items table
289 @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
290 $form->{"description_$i"}, $form->{"longdescription_$i"},
291 $form->{"qty_$i"}, $baseqty,
292 $form->{"sellprice_$i"}, $form->{"discount_$i"},
293 $form->{"unit_$i"}, conv_date($reqdate), conv_i($form->{"project_id_$i"}),
294 $form->{"serialnumber_$i"},
295 $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
296 $form->{"cusordnumber_$i"},
297 $form->{"lastcost_$i"},
298 conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
299 conv_i($form->{"marge_price_factor_$i"}));
300 do_statement($form, $h_item, $q_item, @values);
302 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
304 foreach my $sinfo (@{ $stock_info }) {
305 @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
306 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber});
307 do_statement($form, $h_item_stock, $q_item_stock, @values);
310 CVar->save_custom_variables(module => 'IC',
311 sub_module => 'delivery_order_items',
312 trans_id => $item_id,
313 configs => $ic_cvar_configs,
315 name_prefix => 'ic_',
316 name_postfix => "_$i",
320 $h_item_id->finish();
322 $h_item_stock->finish();
324 ($null, $form->{department_id}) = split(/--/, $form->{department});
328 qq|UPDATE delivery_orders SET
329 donumber = ?, ordnumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
330 customer_id = ?, reqdate = ?,
331 shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
332 delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
333 globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
334 is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, curr = ?
337 @values = ($form->{donumber}, $form->{ordnumber},
338 $form->{cusordnumber}, conv_date($form->{transdate}),
339 conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
340 conv_date($reqdate), $form->{shippingpoint}, $form->{shipvia},
341 $form->{notes}, $form->{intnotes},
342 $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
343 conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
344 conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
345 conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
346 $form->{transaction_description},
347 $form->{type} =~ /^sales/ ? 't' : 'f',
348 conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), $form->{curr},
349 conv_i($form->{id}));
350 do_query($form, $dbh, $query, @values);
353 $form->{name} = $form->{ $form->{vc} };
354 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
356 if (!$form->{shipto_id}) {
357 $form->add_shipto($dbh, $form->{id}, "DO");
360 # save printed, emailed, queued
361 $form->save_status($dbh);
363 # Link this delivery order to the quotations it was created from.
364 RecordLinks->create_links('dbh' => $dbh,
366 'from_table' => 'oe',
367 'from_ids' => $form->{convert_from_oe_ids},
368 'to_table' => 'delivery_orders',
369 'to_id' => $form->{id},
371 delete $form->{convert_from_oe_ids};
373 $self->mark_orders_if_delivered('do_id' => $form->{id},
374 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
377 my $rc = $dbh->commit();
379 $form->{saved_donumber} = $form->{donumber};
381 Common::webdav_folder($form) if ($main::webdav);
383 $main::lxdebug->leave_sub();
388 sub mark_orders_if_delivered {
389 $main::lxdebug->enter_sub();
394 Common::check_params(\%params, qw(do_id type));
396 my $myconfig = \%main::myconfig;
397 my $form = $main::form;
399 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
401 my @links = RecordLinks->get_links('dbh' => $dbh,
402 'from_table' => 'oe',
403 'to_table' => 'delivery_orders',
404 'to_id' => $params{do_id});
406 my ($oe_id) = $links[0]->{from_id} if (scalar @links);
408 return $main::lxdebug->leave_sub() if (!$oe_id);
410 my $all_units = AM->retrieve_all_units();
412 my $query = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit
414 LEFT JOIN parts p ON (oi.parts_id = p.id)
415 WHERE (oi.trans_id = ?)|;
416 my $sth = prepare_execute_query($form, $dbh, $query, $oe_id);
418 my %shipped = $self->get_shipped_qty('type' => $params{type},
422 while (my $ref = $sth->fetchrow_hashref()) {
423 $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor};
425 if ($ordered{$ref->{parts_id}}) {
426 $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty};
428 $ordered{$ref->{parts_id}} = $ref;
434 map { $_->{baseqty} = $_->{qty} * $all_units->{$_->{unit}}->{factor} / $all_units->{$_->{partunit}}->{factor} } values %shipped;
437 foreach my $part (values %ordered) {
438 if (!$shipped{$part->{parts_id}} || ($shipped{$part->{parts_id}}->{baseqty} < $part->{baseqty})) {
445 $query = qq|UPDATE oe
448 do_query($form, $dbh, $query, $oe_id);
449 $dbh->commit() if (!$params{dbh});
452 $main::lxdebug->leave_sub();
456 $main::lxdebug->enter_sub();
461 Common::check_params(\%params, qw(ids));
463 if (('ARRAY' ne ref $params{ids}) || !scalar @{ $params{ids} }) {
464 $main::lxdebug->leave_sub();
468 my $myconfig = \%main::myconfig;
469 my $form = $main::form;
471 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
473 my $query = qq|UPDATE delivery_orders SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar(@{ $params{ids} })) . qq|)|;
475 do_query($form, $dbh, $query, map { conv_i($_) } @{ $params{ids} });
477 $dbh->commit() unless ($params{dbh});
479 $main::lxdebug->leave_sub();
483 $main::lxdebug->enter_sub();
487 my $myconfig = \%main::myconfig;
488 my $form = $main::form;
489 my $spool = $main::spool;
491 # connect to database
492 my $dbh = $form->get_standard_dbh($myconfig);
495 my $query = qq|SELECT s.spoolfile FROM status s WHERE s.trans_id = ?|;
496 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
501 while (($spoolfile) = $sth->fetchrow_array) {
502 push @spoolfiles, $spoolfile;
507 @values = (conv_i($form->{id}));
509 # delete status entries
510 $query = qq|DELETE FROM status
512 do_query($form, $dbh, $query, @values);
514 # delete individual entries
515 $query = qq|DELETE FROM delivery_order_items_stock
516 WHERE delivery_order_item_id IN (
517 SELECT id FROM delivery_order_items
518 WHERE delivery_order_id = ?
520 do_query($form, $dbh, $query, @values);
522 # delete individual entries
523 $query = qq|DELETE FROM delivery_order_items
524 WHERE delivery_order_id = ?|;
525 do_query($form, $dbh, $query, @values);
528 $query = qq|DELETE FROM delivery_orders
530 do_query($form, $dbh, $query, @values);
532 $query = qq|DELETE FROM shipto
533 WHERE trans_id = ? AND module = 'DO'|;
534 do_query($form, $dbh, $query, @values);
536 my $rc = $dbh->commit();
539 foreach $spoolfile (@spoolfiles) {
540 unlink "$spool/$spoolfile" if $spoolfile;
544 $main::lxdebug->leave_sub();
550 $main::lxdebug->enter_sub();
555 my $myconfig = \%main::myconfig;
556 my $form = $main::form;
558 # connect to database
559 my $dbh = $form->get_standard_dbh($myconfig);
561 my ($query, $query_add, @values, $sth, $ref);
563 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
566 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
568 my $mode = !$params{ids} ? 'default' : ref $params{ids} eq 'ARRAY' ? 'multi' : 'single';
570 if ($mode eq 'default') {
571 $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate, current_date AS reqdate|);
572 map { $form->{$_} = $ref->{$_} } keys %$ref;
575 $form->lastname_used($dbh, $myconfig, $vc) unless $form->{"${vc}_id"};
577 $main::lxdebug->leave_sub();
582 my @do_ids = map { conv_i($_) } ($mode eq 'multi' ? @{ $params{ids} } : ($params{ids}));
583 my $do_ids_placeholders = join(', ', ('?') x scalar(@do_ids));
585 # retrieve order for single id
586 # NOTE: this query is intended to fetch all information only ONCE.
587 # so if any of these infos is important (or even different) for any item,
588 # it will be killed out and then has to be fetched from the item scope query further down
590 qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.transdate, dord.reqdate,
591 dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
592 e.name AS employee, dord.employee_id, dord.salesman_id,
593 dord.${vc}_id, cv.name AS ${vc},
594 dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
595 d.description AS department, dord.language_id,
597 dord.globalproject_id, dord.delivered, dord.transaction_description,
598 dord.taxzone_id, dord.taxincluded, dord.terms, dord.curr
599 FROM delivery_orders dord
600 JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
601 LEFT JOIN employee e ON (dord.employee_id = e.id)
602 LEFT JOIN department d ON (dord.department_id = d.id)
603 WHERE dord.id IN ($do_ids_placeholders)|;
604 $sth = prepare_execute_query($form, $dbh, $query, @do_ids);
606 delete $form->{"${vc}_id"};
607 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
608 if ($form->{"${vc}_id"} && ($ref->{"${vc}_id"} != $form->{"${vc}_id"})) {
610 $main::lxdebug->leave_sub();
615 map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
616 $form->{donumber_array} .= $form->{donumber} . ' ';
620 $form->{saved_donumber} = $form->{donumber};
622 # if not given, fill transdate with current_date
623 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
625 if ($mode eq 'single') {
626 $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'DO'|;
627 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
629 $ref = $sth->fetchrow_hashref(NAME_lc);
631 map { $form->{$_} = $ref->{$_} } keys %$ref;
634 # get printed, emailed and queued
635 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
636 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
638 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
639 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
640 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
641 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
644 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
650 my %oid = ('Pg' => 'oid',
651 'Oracle' => 'rowid');
653 # retrieve individual items
654 # this query looks up all information about the items
655 # stuff different from the whole will not be overwritten, but saved with a suffix.
657 qq|SELECT doi.id AS delivery_order_items_id,
658 p.partnumber, p.assembly, doi.description, doi.qty,
659 doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.bin, p.notes AS partnotes,
660 doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
661 doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
662 doi.price_factor_id, doi.price_factor, doi.marge_price_factor,
663 pr.projectnumber, dord.transdate AS dord_transdate,
665 FROM delivery_order_items doi
666 JOIN parts p ON (doi.parts_id = p.id)
667 JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
668 LEFT JOIN project pr ON (doi.project_id = pr.id)
669 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
670 WHERE doi.delivery_order_id IN ($do_ids_placeholders)
671 ORDER BY doi.$oid{$myconfig->{dbdriver}}|;
673 $form->{form_details} = selectall_hashref_query($form, $dbh, $query, @do_ids);
675 # Retrieve custom variables.
676 foreach my $doi (@{ $form->{form_details} }) {
677 my $cvars = CVar->get_custom_variables(dbh => $dbh,
679 sub_module => 'delivery_order_items',
680 trans_id => $doi->{delivery_order_items_id},
682 map { $doi->{"ic_cvar_$_->{name}"} = $_->{value} } @{ $cvars };
685 if ($mode eq 'single') {
686 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
689 qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber
690 FROM delivery_order_items_stock
691 WHERE delivery_order_item_id = ?|;
692 my $sth = prepare_query($form, $dbh, $query);
694 foreach my $doi (@{ $form->{form_details} }) {
695 do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
697 while (my $ref = $sth->fetchrow_hashref()) {
698 push @{ $requests }, $ref;
701 $doi->{"stock_${in_out}"} = YAML::Dump($requests);
707 Common::webdav_folder($form) if ($main::webdav);
709 $main::lxdebug->leave_sub();
715 $main::lxdebug->enter_sub();
719 my $myconfig = \%main::myconfig;
720 my $form = $main::form;
722 # connect to database
723 my $dbh = $form->get_standard_dbh($myconfig);
733 my %oid = ('Pg' => 'oid',
734 'Oracle' => 'rowid');
736 my (@project_ids, %projectnumbers);
738 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
740 # sort items by partsgroup
741 for $i (1 .. $form->{rowcount}) {
743 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
744 $partsgroup = $form->{"partsgroup_$i"};
746 push @partsgroup, [$i, $partsgroup];
747 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
751 $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
752 join(", ", map("?", @project_ids)) . ")";
753 $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
754 while (my $ref = $sth->fetchrow_hashref()) {
755 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
760 $form->{"globalprojectnumber"} =
761 $projectnumbers{$form->{"globalproject_id"}};
763 my $q_pg = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
765 JOIN parts p ON (a.parts_id = p.id)
766 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
768 AND a.id = ? $sortorder|;
769 my $h_pg = prepare_query($form, $dbh, $q_pg);
771 my $q_bin_wh = qq|SELECT (SELECT description FROM bin WHERE id = ?) AS bin,
772 (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
773 my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
775 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
779 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
781 $form->{TEMPLATE_ARRAYS} = { };
782 IC->prepare_parts_for_printing();
785 qw(runningnumber number description longdescription qty unit
786 partnotes serialnumber reqdate projectnumber
787 si_runningnumber si_number si_description
788 si_warehouse si_bin si_chargenumber si_qty si_unit);
790 map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays, @tax_arrays);
792 push @arrays, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
795 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
798 next if (!$form->{"id_$i"});
802 if ($item->[1] ne $sameitem) {
803 push(@{ $form->{description} }, qq|$item->[1]|);
804 $sameitem = $item->[1];
806 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
809 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
811 # add number, description and qty to $form->{number}, ....
813 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
815 push @{ $form->{TEMPLATE_ARRAYS}{runningnumber} }, $position;
816 push @{ $form->{TEMPLATE_ARRAYS}{number} }, $form->{"partnumber_$i"};
817 push @{ $form->{TEMPLATE_ARRAYS}{description} }, $form->{"description_$i"};
818 push @{ $form->{TEMPLATE_ARRAYS}{longdescription} }, $form->{"longdescription_$i"};
819 push @{ $form->{TEMPLATE_ARRAYS}{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"});
820 push @{ $form->{TEMPLATE_ARRAYS}{unit} }, $form->{"unit_$i"};
821 push @{ $form->{TEMPLATE_ARRAYS}{partnotes} }, $form->{"partnotes_$i"};
822 push @{ $form->{TEMPLATE_ARRAYS}{serialnumber} }, $form->{"serialnumber_$i"};
823 push @{ $form->{TEMPLATE_ARRAYS}{reqdate} }, $form->{"reqdate_$i"};
824 push @{ $form->{TEMPLATE_ARRAYS}{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}};
826 if ($form->{"assembly_$i"}) {
829 # get parts and push them onto the stack
831 if ($form->{groupitems}) {
833 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
835 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
838 do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
840 while (my $ref = $h_pg->fetchrow_hashref(NAME_lc)) {
841 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
842 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
843 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
844 push(@{ $form->{description} }, $sameitem);
847 push(@{ $form->{description} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq|, $ref->{partnumber}, $ref->{description}|);
849 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
853 if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
854 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
856 foreach my $si (@{ $stock_info }) {
859 do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
860 my $bin_wh = $h_bin_wh->fetchrow_hashref();
862 push @{ $form->{TEMPLATE_ARRAYS}{si_runningnumber}[$position-1] }, $num_si;
863 push @{ $form->{TEMPLATE_ARRAYS}{si_number}[$position-1] }, $form->{"partnumber_$i"};
864 push @{ $form->{TEMPLATE_ARRAYS}{si_description}[$position-1] }, $form->{"description_$i"};
865 push @{ $form->{TEMPLATE_ARRAYS}{si_warehouse}[$position-1] }, $bin_wh->{warehouse};
866 push @{ $form->{TEMPLATE_ARRAYS}{si_bin}[$position-1] }, $bin_wh->{bin};
867 push @{ $form->{TEMPLATE_ARRAYS}{si_chargenumber}[$position-1] }, $si->{chargenumber};
868 push @{ $form->{TEMPLATE_ARRAYS}{si_qty}[$position-1] }, $form->format_amount($myconfig, $si->{qty} * 1);
869 push @{ $form->{TEMPLATE_ARRAYS}{si_unit}[$position-1] }, $si->{unit};
873 map { push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} }, $form->{"ic_cvar_$_->{name}_$i"} } @{ $ic_cvar_configs };
879 $form->{username} = $myconfig->{name};
881 $main::lxdebug->leave_sub();
884 sub project_description {
885 $main::lxdebug->enter_sub();
887 my ($self, $dbh, $id) = @_;
889 my $query = qq|SELECT description FROM project WHERE id = ?|;
890 my ($value) = selectrow_query($form, $dbh, $query, $id);
892 $main::lxdebug->leave_sub();
897 sub unpack_stock_information {
898 $main::lxdebug->enter_sub();
903 Common::check_params_x(\%params, qw(packed));
907 eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
909 $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
911 foreach my $entry (@{ $unpacked }) {
912 next if ('HASH' eq ref $entry);
917 $main::lxdebug->leave_sub();
922 sub get_item_availability {
923 $main::lxdebug->enter_sub();
928 Common::check_params(\%params, qw(parts_id));
930 my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
931 my $form = $main::form;
934 qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, SUM(qty) AS qty, i.parts_id,
935 w.description AS warehousedescription,
936 b.description AS bindescription
938 LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
939 LEFT JOIN bin b ON (i.bin_id = b.id)
940 WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
941 GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.parts_id, w.description, b.description
943 ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber)
945 my $contents = selectall_hashref_query($form, $form->get_standard_dbh($myconfig), $query, @parts_ids);
947 $main::lxdebug->leave_sub();
949 return @{ $contents };
953 sub check_stock_availability {
954 $main::lxdebug->enter_sub();
959 Common::check_params(\%params, qw(requests parts_id));
961 my $myconfig = \%main::myconfig;
962 my $form = $main::form;
964 my $dbh = $form->get_standard_dbh($myconfig);
966 my $units = AM->retrieve_units($myconfig, $form);
968 my ($partunit) = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
969 my $unit_factor = $units->{$partunit}->{factor} || 1;
971 my @contents = $self->get_item_availability(%params);
975 foreach my $sinfo (@{ $params{requests} }) {
978 foreach my $row (@contents) {
979 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
980 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
981 ($row->{chargenumber} ne $sinfo->{chargenumber}));
985 my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
987 if ($base_qty > $row->{qty}) {
989 push @errors, $sinfo;
995 push @errors, $sinfo if (!$found);
998 $main::lxdebug->leave_sub();
1003 sub transfer_in_out {
1004 $main::lxdebug->enter_sub();
1009 Common::check_params(\%params, qw(direction requests));
1011 if (!@{ $params{requests} }) {
1012 $main::lxdebug->leave_sub();
1016 my $myconfig = \%main::myconfig;
1017 my $form = $main::form;
1019 my $prefix = $params{direction} eq 'in' ? 'dst' : 'src';
1023 foreach my $request (@{ $params{requests} }) {
1025 'parts_id' => $request->{parts_id},
1026 "${prefix}_warehouse_id" => $request->{warehouse_id},
1027 "${prefix}_bin_id" => $request->{bin_id},
1028 'chargenumber' => $request->{chargenumber},
1029 'qty' => $request->{qty},
1030 'unit' => $request->{unit},
1031 'oe_id' => $form->{id},
1032 'shippingdate' => 'current_date',
1033 'transfer_type' => $params{direction} eq 'in' ? 'stock' : 'shipped',
1037 WH->transfer(@transfers);
1039 $main::lxdebug->leave_sub();
1042 sub get_shipped_qty {
1043 $main::lxdebug->enter_sub();
1048 Common::check_params(\%params, qw(type oe_id));
1050 my $myconfig = \%main::myconfig;
1051 my $form = $main::form;
1053 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1055 my @links = RecordLinks->get_links('dbh' => $dbh,
1056 'from_table' => 'oe',
1057 'from_id' => $params{oe_id},
1058 'to_table' => 'delivery_orders');
1059 my @values = map { $_->{to_id} } @links;
1061 if (!scalar @values) {
1062 $main::lxdebug->leave_sub();
1067 qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit
1068 FROM delivery_order_items doi
1069 LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id)
1070 LEFT JOIN parts p ON (doi.parts_id = p.id)
1071 WHERE o.id IN (| . join(', ', ('?') x scalar @values) . qq|)|;
1074 my $entries = selectall_hashref_query($form, $dbh, $query, @values);
1075 my $all_units = AM->retrieve_all_units();
1077 foreach my $entry (@{ $entries }) {
1078 $entry->{qty} *= $all_units->{$entry->{unit}}->{factor} / $all_units->{$entry->{partunit}}->{factor};
1080 if (!$ship{$entry->{parts_id}}) {
1081 $ship{$entry->{parts_id}} = $entry;
1083 $ship{$entry->{parts_id}}->{qty} += $entry->{qty};
1087 $main::lxdebug->leave_sub();
1092 sub is_marked_as_delivered {
1093 $main::lxdebug->enter_sub();
1098 Common::check_params(\%params, qw(id));
1100 my $myconfig = \%main::myconfig;
1101 my $form = $main::form;
1103 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1105 my ($delivered) = selectfirst_array_query($form, $dbh, qq|SELECT delivered FROM delivery_orders WHERE id = ?|, conv_i($params{id}));
1107 $main::lxdebug->leave_sub();
1109 return $delivered ? 1 : 0;