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);
44 $main::lxdebug->enter_sub();
48 my $myconfig = \%main::myconfig;
49 my $form = $main::form;
52 my $dbh = $form->get_standard_dbh($myconfig);
54 my (@where, @values, $where);
56 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
59 qq|SELECT dord.id, dord.donumber, dord.ordnumber, dord.oreqnumber, dord.transdate,
60 ct.name, dord.${vc}_id, dord.globalproject_id,
61 dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
62 dord.transaction_description,
63 pr.projectnumber AS globalprojectnumber,
68 FROM delivery_orders dord
69 LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
70 LEFT JOIN employee e ON (dord.employee_id = e.id)
71 LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
72 LEFT JOIN project pr ON (dord.globalproject_id = pr.id)
73 LEFT JOIN order_request oreq ON (dord.oreqnumber = oreq.oreqnumber)
74 LEFT JOIN oe ON ((dord.ordnumber = oe.ordnumber) AND NOT COALESCE(oe.quotation, FALSE))|;
76 push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
78 my $department_id = (split /--/, $form->{department})[1];
80 push @where, qq|dord.department_id = ?|;
81 push @values, conv_i($department_id);
84 if ($form->{project_id}) {
86 qq|(dord.globalproject_id = ?) OR EXISTS
87 (SELECT * FROM delivery_order_items doi
88 WHERE (doi.project_id = ?) AND (oi.delivery_order_id = dord.id))|;
89 push @values, conv_i($form->{project_id}), conv_i($form->{project_id});
92 if ($form->{"${vc}_id"}) {
93 push @where, qq|dord.${vc}_id = ?|;
94 push @values, $form->{"${vc}_id"};
96 } elsif ($form->{$vc}) {
97 push @where, qq|ct.name ILIKE ?|;
98 push @values, '%' . $form->{$vc} . '%';
101 foreach my $item (qw(employee_id salesman_id)) {
102 next unless ($form->{$item});
103 push @where, "dord.$item = ?";
104 push @values, conv_i($form->{$item});
107 foreach my $item (qw(donumber ordnumber cusordnumber transaction_description)) {
108 next unless ($form->{$item});
109 push @where, qq|dord.$item ILIKE ?|;
110 push @values, '%' . $form->{$item} . '%';
113 if (!($form->{open} && $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",
141 "oreqnumber" => "dord.oreqnumber",
143 "employee" => "e.name",
144 "salesman" => "sm.name",
145 "shipvia" => "dord.shipvia",
146 "transaction_description" => "dord.transaction_description"
149 my $sortoder = "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;
156 $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
158 $main::lxdebug->dump(0, "DO", $form->{DO});
160 $main::lxdebug->leave_sub();
164 $main::lxdebug->enter_sub();
168 my $myconfig = \%main::myconfig;
169 my $form = $main::form;
171 # connect to database, turn off autocommit
172 my $dbh = $form->get_standard_dbh($myconfig);
174 my ($query, @values, $sth, $null);
176 my $all_units = AM->retrieve_units($myconfig, $form);
177 $form->{all_units} = $all_units;
179 $form->{donumber} = $form->update_defaults($myconfig, $form->{type} eq 'sales_delivery_order' ? 'sdonumber' : 'pdonumber', $dbh) unless $form->{donumber};
180 $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
181 $form->get_employee($dbh) unless ($form->{employee_id});
183 my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
187 $query = qq|DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = ?)|;
188 do_query($form, $dbh, $query, conv_i($form->{id}));
190 $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
191 do_query($form, $dbh, $query, conv_i($form->{id}));
193 $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
194 do_query($form, $dbh, $query, conv_i($form->{id}));
198 $query = qq|SELECT nextval('id')|;
199 ($form->{id}) = selectrow_query($form, $dbh, $query);
201 $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id) VALUES (?, '', ?)|;
202 do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}));
208 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
209 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
212 my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
213 my @part_ids = keys %part_id_map;
217 $query = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
218 %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
221 my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
222 my $h_item_id = prepare_query($form, $dbh, $q_item_id);
225 qq|INSERT INTO delivery_order_items (
226 id, delivery_order_id, parts_id, description, longdescription, qty, base_qty,
227 sellprice, discount, unit, reqdate, project_id, serialnumber,
228 ordnumber, transdate, cusordnumber,
229 lastcost, price_factor_id, price_factor, marge_price_factor,
230 v_partnumber, v_description)
231 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
232 (SELECT factor FROM price_factors WHERE id = ?), ?, ?, ?)|;
233 my $h_item = prepare_query($form, $dbh, $q_item);
236 qq|INSERT INTO delivery_order_items_stock (delivery_order_item_id, qty, unit, warehouse_id, bin_id, chargenumber)
237 VALUES (?, ?, ?, ?, ?, ?)|;
238 my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
240 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
242 for my $i (1 .. $form->{rowcount}) {
243 next if (!$form->{"id_$i"});
245 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
247 my $item_unit = $part_unit_map{$form->{"id_$i"}};
250 if (defined($all_units->{$item_unit}->{factor}) && (($all_units->{$item_unit}->{factor} * 1) != 0)) {
251 $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
253 my $baseqty = $form->{"qty_$i"} * $basefactor;
255 $form->{"lastcost_$i"} *= 1;
257 # set values to 0 if nothing entered
258 $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
259 $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
261 $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
262 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
264 $reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
266 do_statement($form, $h_item_id, $q_item_id);
267 my ($item_id) = $h_item_id->fetchrow_array();
269 # save detail record in delivery_order_items table
270 @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
271 $form->{"description_$i"}, $form->{"longdescription_$i"},
272 $form->{"qty_$i"}, $baseqty,
273 $form->{"sellprice_$i"}, $form->{"discount_$i"},
274 $form->{"unit_$i"}, conv_date($reqdate), conv_i($form->{"project_id_$i"}),
275 $form->{"serialnumber_$i"},
276 $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
277 $form->{"cusordnumber_$i"},
278 $form->{"lastcost_$i"},
279 conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
280 conv_i($form->{"marge_price_factor_$i"}),
281 $form->{"v_partnumber_$i"}, $form->{"v_description_$i"});
282 do_statement($form, $h_item, $q_item, @values);
284 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
286 foreach my $sinfo (@{ $stock_info }) {
287 @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
288 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber});
289 do_statement($form, $h_item_stock, $q_item_stock, @values);
293 $h_item_id->finish();
295 $h_item_stock->finish();
297 ($null, $form->{department_id}) = split(/--/, $form->{department});
301 qq|UPDATE delivery_orders SET
302 donumber = ?, ordnumber = ?, cusordnumber = ?, oreqnumber = ?, transdate = ?, vendor_id = ?,
303 customer_id = ?, reqdate = ?,
304 shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
305 delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
306 globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
310 @values = ($form->{donumber}, $form->{ordnumber},
311 $form->{cusordnumber}, $form->{oreqnumber}, conv_date($form->{transdate}),
312 conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
313 conv_date($reqdate), $form->{shippingpoint}, $form->{shipvia},
314 $form->{notes}, $form->{intnotes},
315 $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
316 conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
317 conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
318 conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
319 $form->{transaction_description},
320 $form->{type} =~ /^sales/ ? 't' : 'f',
321 conv_i($form->{id}));
322 do_query($form, $dbh, $query, @values);
325 $form->{name} = $form->{ $form->{vc} };
326 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
328 if (!$form->{shipto_id}) {
329 $form->add_shipto($dbh, $form->{id}, "DO");
332 # save printed, emailed, queued
333 $form->save_status($dbh);
335 my $rc = $dbh->commit();
337 $form->{saved_donumber} = $form->{donumber};
339 Common::webdav_folder($form) if ($main::webdav);
341 $main::lxdebug->leave_sub();
347 $main::lxdebug->enter_sub();
351 my $myconfig = \%main::myconfig;
352 my $form = $main::form;
354 return $main::lxdebug->leave_sub() unless ($form->{id});
356 my $dbh = $form->get_standard_dbh($myconfig);
357 do_query($form, $dbh, qq|UPDATE do SET closed = TRUE where id = ?|, conv_i($form->{id}));
360 $main::lxdebug->leave_sub();
364 $main::lxdebug->enter_sub();
368 my $myconfig = \%main::myconfig;
369 my $form = $main::form;
370 my $spool = $main::spool;
372 # connect to database
373 my $dbh = $form->get_standard_dbh($myconfig);
376 my $query = qq|SELECT s.spoolfile FROM status s WHERE s.trans_id = ?|;
377 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
382 while (($spoolfile) = $sth->fetchrow_array) {
383 push @spoolfiles, $spoolfile;
388 @values = (conv_i($form->{id}));
390 # delete status entries
391 $query = qq|DELETE FROM status
393 do_query($form, $dbh, $query, @values);
395 # delete individual entries
396 $query = qq|DELETE FROM delivery_order_items_stock
397 WHERE delivery_order_item_id IN (
398 SELECT id FROM delivery_order_items
399 WHERE delivery_order_id = ?
401 do_query($form, $dbh, $query, @values);
403 # delete individual entries
404 $query = qq|DELETE FROM delivery_order_items
405 WHERE delivery_order_id = ?|;
406 do_query($form, $dbh, $query, @values);
409 $query = qq|DELETE FROM delivery_orders
411 do_query($form, $dbh, $query, @values);
413 $query = qq|DELETE FROM shipto
414 WHERE trans_id = ? AND module = 'DO'|;
415 do_query($form, $dbh, $query, @values);
417 my $rc = $dbh->commit();
420 foreach $spoolfile (@spoolfiles) {
421 unlink "$spool/$spoolfile" if $spoolfile;
425 $main::lxdebug->leave_sub();
431 $main::lxdebug->enter_sub();
435 my $myconfig = \%main::myconfig;
436 my $form = $main::form;
438 # connect to database
439 my $dbh = $form->get_standard_dbh($myconfig);
441 my ($query, $query_add, @values, $sth, $ref);
444 $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate, current_date AS reqdate|);
445 map { $form->{$_} = $ref->{$_} } keys %$ref;
448 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
452 # retrieve order for single id
453 # NOTE: this query is intended to fetch all information only ONCE.
454 # so if any of these infos is important (or even different) for any item,
455 # it will be killed out and then has to be fetched from the item scope query further down
457 qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.oreqnumber, dord.transdate, dord.reqdate,
458 dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
459 e.name AS employee, dord.employee_id, dord.salesman_id,
460 dord.${vc}_id, cv.name AS ${vc},
461 dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
462 d.description AS department, dord.language_id,
464 dord.globalproject_id, dord.delivered, dord.transaction_description
465 FROM delivery_orders dord
466 JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
467 LEFT JOIN employee e ON (dord.employee_id = e.id)
468 LEFT JOIN department d ON (dord.department_id = d.id)
470 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
472 $ref = $sth->fetchrow_hashref(NAME_lc);
475 map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
477 $form->{saved_donumber} = $form->{donumber};
479 # if not given, fill transdate with current_date
480 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
482 $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'DO'|;
483 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
485 $ref = $sth->fetchrow_hashref(NAME_lc);
487 map { $form->{$_} = $ref->{$_} } keys %$ref;
490 # get printed, emailed and queued
491 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
492 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
494 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
495 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
496 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
497 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
500 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
502 my %oid = ('Pg' => 'oid',
503 'Oracle' => 'rowid');
505 my $transdate = $form->{transdate} ? $dbh->quote($form->{transdate}) : "current_date";
507 # retrieve individual items
508 # this query looks up all information about the items
509 # stuff different from the whole will not be overwritten, but saved with a suffix.
511 qq|SELECT doi.id AS delivery_order_items_id,
512 p.partnumber, p.assembly, doi.description, doi.qty,
513 doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.bin, p.notes AS partnotes,
514 doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
515 doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
516 doi.price_factor_id, doi.price_factor, doi.marge_price_factor,
517 doi.v_partnumber, doi.v_description,
520 FROM delivery_order_items doi
521 JOIN parts p ON (doi.parts_id = p.id)
522 JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
523 LEFT JOIN project pr ON (doi.project_id = pr.id)
524 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
525 WHERE doi.delivery_order_id = ?
526 ORDER BY doi.$oid{$myconfig->{dbdriver}}|;
528 $form->{form_details} = selectall_hashref_query($form, $dbh, $query, conv_i($form->{id}));
530 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
533 qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber
534 FROM delivery_order_items_stock
535 WHERE delivery_order_item_id = ?|;
536 my $sth = prepare_query($form, $dbh, $query);
538 foreach my $doi (@{ $form->{form_details} }) {
539 do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
541 while (my $ref = $sth->fetchrow_hashref()) {
542 push @{ $requests }, $ref;
545 $doi->{"stock_${in_out}"} = YAML::Dump($requests);
552 $form->lastname_used($dbh, $myconfig, $form->{vc}) unless $form->{"$form->{vc}_id"};
556 Common::webdav_folder($form) if ($main::webdav);
558 $main::lxdebug->leave_sub();
562 $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);
580 my %oid = ('Pg' => 'oid',
581 'Oracle' => 'rowid');
583 my (@project_ids, %projectnumbers);
585 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
587 # sort items by partsgroup
588 for $i (1 .. $form->{rowcount}) {
590 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
591 $partsgroup = $form->{"partsgroup_$i"};
593 push @partsgroup, [$i, $partsgroup];
594 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
598 $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
599 join(", ", map("?", @project_ids)) . ")";
600 $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
601 while (my $ref = $sth->fetchrow_hashref()) {
602 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
607 $form->{"globalprojectnumber"} =
608 $projectnumbers{$form->{"globalproject_id"}};
610 my $q_pg = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
612 JOIN parts p ON (a.parts_id = p.id)
613 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
615 AND a.id = ? $sortorder|;
616 my $h_pg = prepare_query($form, $dbh, $q_pg);
618 my $q_bin_wh = qq|SELECT (SELECT description FROM bin WHERE id = ?) AS bin,
619 (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
620 my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
622 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
627 qw(runningnumber number description longdescription qty unit
628 partnotes serialnumber reqdate projectnumber
629 si_runningnumber si_number si_description
630 si_warehouse si_bin si_chargenumber si_qty si_unit
631 v_partnumber v_description);
634 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
637 next if (!$form->{"id_$i"});
641 if ($item->[1] ne $sameitem) {
642 push(@{ $form->{description} }, qq|$item->[1]|);
643 $sameitem = $item->[1];
645 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
648 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
650 # add number, description and qty to $form->{number}, ....
652 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
654 push @{ $form->{runningnumber} }, $position;
655 push @{ $form->{number} }, $form->{"partnumber_$i"};
656 push @{ $form->{description} }, $form->{"description_$i"};
657 push @{ $form->{longdescription} }, $form->{"longdescription_$i"};
658 push @{ $form->{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"});
659 push @{ $form->{unit} }, $form->{"unit_$i"};
660 push @{ $form->{partnotes} }, $form->{"partnotes_$i"};
661 push @{ $form->{serialnumber} }, $form->{"serialnumber_$i"};
662 push @{ $form->{reqdate} }, $form->{"reqdate_$i"};
663 push @{ $form->{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}};
664 push @{ $form->{v_partnumber} }, $form->{"v_partnumber_$i"};
665 push @{ $form->{v_description} }, $form->{"v_description_$i"};
667 if ($form->{"assembly_$i"}) {
670 # get parts and push them onto the stack
672 if ($form->{groupitems}) {
674 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
676 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
679 do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
681 while (my $ref = $h_pg->fetchrow_hashref(NAME_lc)) {
682 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
683 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
684 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
685 push(@{ $form->{description} }, $sameitem);
688 push(@{ $form->{description} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq|, $ref->{partnumber}, $ref->{description}|);
690 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
694 if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
695 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
697 foreach my $si (@{ $stock_info }) {
700 do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
701 my $bin_wh = $h_bin_wh->fetchrow_hashref();
703 push @{ $form->{si_runningnumber} }, $num_si;
704 push @{ $form->{si_number} }, $form->{"partnumber_$i"};
705 push @{ $form->{si_description} }, $form->{"description_$i"};
706 push @{ $form->{si_warehouse} }, $bin_wh->{warehouse};
707 push @{ $form->{si_bin} }, $bin_wh->{bin};
708 push @{ $form->{si_chargenumber} }, $si->{chargenumber};
709 push @{ $form->{si_qty} }, $form->format_amount($myconfig, $si->{qty} * 1);
710 push @{ $form->{si_unit} }, $si->{unit};
718 $form->{username} = $myconfig->{name};
720 $main::lxdebug->leave_sub();
723 sub project_description {
724 $main::lxdebug->enter_sub();
726 my ($self, $dbh, $id) = @_;
728 my $query = qq|SELECT description FROM project WHERE id = ?|;
729 my ($value) = selectrow_query($form, $dbh, $query, $id);
731 $main::lxdebug->leave_sub();
736 sub unpack_stock_information {
737 $main::lxdebug->enter_sub();
742 Common::check_params_x(\%params, qw(packed));
746 eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
748 $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
750 foreach my $entry (@{ $unpacked }) {
751 next if ('HASH' eq ref $entry);
756 $main::lxdebug->leave_sub();
761 sub get_item_availability {
762 $main::lxdebug->enter_sub();
767 Common::check_params(\%params, qw(parts_id));
769 my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
770 my $form = $main::form;
773 qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, SUM(qty) AS qty, i.parts_id,
774 w.description AS warehousedescription,
775 b.description AS bindescription
777 LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
778 LEFT JOIN bin b ON (i.bin_id = b.id)
779 WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
781 GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.parts_id, w.description, b.description
782 ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber)|;
784 my $contents = selectall_hashref_query($form, $form->get_standard_dbh($myconfig), $query, @parts_ids);
786 $main::lxdebug->leave_sub();
788 return @{ $contents };
792 sub check_stock_availability {
793 $main::lxdebug->enter_sub();
798 Common::check_params(\%params, qw(requests parts_id));
800 my $myconfig = \%main::myconfig;
801 my $form = $main::form;
803 my $dbh = $form->get_standard_dbh($myconfig);
805 my $units = AM->retrieve_units($myconfig, $form, "dimension");
807 my ($partunit) = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
808 my $unit_factor = $units->{$partunit}->{factor} || 1;
810 my @contents = $self->get_item_availability(%params);
814 foreach my $sinfo (@{ $params{requests} }) {
817 foreach my $row (@contents) {
818 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
819 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
820 ($row->{chargenumber} ne $sinfo->{chargenumber}));
824 my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
826 if ($base_qty > $row->{qty}) {
828 push @errors, $sinfo;
834 push @errors, $sinfo if (!$found);
837 $main::lxdebug->leave_sub();
842 sub transfer_in_out {
843 $main::lxdebug->enter_sub();
848 Common::check_params(\%params, qw(direction requests));
850 if (!@{ $params{requests} }) {
851 $main::lxdebug->leave_sub();
855 my $myconfig = \%main::myconfig;
856 my $form = $main::form;
858 my $prefix = $params{direction} eq 'in' ? 'dst' : 'src';
862 foreach my $request (@{ $params{requests} }) {
864 'parts_id' => $request->{parts_id},
865 "${prefix}_warehouse_id" => $request->{warehouse_id},
866 "${prefix}_bin_id" => $request->{bin_id},
867 'chargenumber' => $request->{chargenumber},
868 'qty' => $request->{qty},
869 'unit' => $request->{unit},
870 'oe_id' => $form->{id},
871 'shippingdate' => 'current_date',
872 'transfer_type' => $params{direction} eq 'in' ? 'stock' : 'shipped',
876 WH->transfer(@transfers);
878 $main::lxdebug->leave_sub();