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.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,
66 FROM delivery_orders dord
67 LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
68 LEFT JOIN employee e ON (dord.employee_id = e.id)
69 LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
70 LEFT JOIN project pr ON (dord.globalproject_id = pr.id)|;
72 push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
74 my $department_id = (split /--/, $form->{department})[1];
76 push @where, qq|dord.department_id = ?|;
77 push @values, conv_i($department_id);
80 if ($form->{project_id}) {
82 qq|(dord.globalproject_id = ?) OR EXISTS
83 (SELECT * FROM delivery_order_items doi
84 WHERE (doi.project_id = ?) AND (oi.delivery_order_id = dord.id))|;
85 push @values, conv_i($form->{project_id}), conv_i($form->{project_id});
88 if ($form->{"${vc}_id"}) {
89 push @where, qq|dord.${vc}_id = ?|;
90 push @values, $form->{"${vc}_id"};
92 } elsif ($form->{$vc}) {
93 push @where, qq|ct.name ILIKE ?|;
94 push @values, '%' . $form->{$vc} . '%';
97 foreach my $item (qw(employee_id salesman_id)) {
98 next unless ($form->{$item});
99 push @where, "dord.$item = ?";
100 push @values, conv_i($form->{$item});
103 foreach my $item (qw(donumber ordnumber cusordnumber transaction_description)) {
104 next unless ($form->{$item});
105 push @where, qq|dord.$item ILIKE ?|;
106 push @values, '%' . $form->{$item} . '%';
109 if (!($form->{open} && $form->{closed})) {
110 push @where, ($form->{open} ? "NOT " : "") . "COALESCE(dord.closed, FALSE)";
113 if (($form->{notdelivered} || $form->{delivered}) &&
114 ($form->{notdelivered} ne $form->{delivered})) {
115 push @where, ($form->{delivered} ? "" : "NOT ") . "COALESCE(dord.delivered, FALSE)";
118 if($form->{transdatefrom}) {
119 push @where, qq|dord.transdate >= ?|;
120 push @values, conv_date($form->{transdatefrom});
123 if($form->{transdateto}) {
124 push @where, qq|dord.transdate <= ?|;
125 push @values, conv_date($form->{transdateto});
129 $query .= " WHERE " . join(" AND ", map { "($_)" } @where);
132 my %allowed_sort_columns = (
133 "transdate" => "dord.transdate",
135 "donumber" => "dord.donumber",
136 "ordnumber" => "dord.ordnumber",
138 "employee" => "e.name",
139 "salesman" => "sm.name",
140 "shipvia" => "dord.shipvia",
141 "transaction_description" => "dord.transaction_description"
144 my $sortoder = "dord.id";
145 if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
146 $sortorder = $allowed_sort_columns{$form->{sort}};
149 $query .= qq| ORDER by | . $sortorder;
151 $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
153 if (scalar @{ $form->{DO} }) {
157 WHERE NOT COALESCE(quotation, FALSE)
159 AND (COALESCE(${vc}_id, 0) != 0)|;
161 my $sth = prepare_query($form, $dbh, $query);
163 foreach my $dord (@{ $form->{DO} }) {
164 next unless ($dord->{ordnumber});
165 do_statement($form, $sth, $query, $dord->{ordnumber});
166 ($dord->{oe_id}) = $sth->fetchrow_array();
172 $main::lxdebug->leave_sub();
176 $main::lxdebug->enter_sub();
180 my $myconfig = \%main::myconfig;
181 my $form = $main::form;
183 # connect to database, turn off autocommit
184 my $dbh = $form->get_standard_dbh($myconfig);
186 my ($query, @values, $sth, $null);
188 my $all_units = AM->retrieve_units($myconfig, $form);
189 $form->{all_units} = $all_units;
191 $form->{donumber} = $form->update_defaults($myconfig, $form->{type} eq 'sales_delivery_order' ? 'sdonumber' : 'pdonumber', $dbh) unless $form->{donumber};
192 $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
193 $form->get_employee($dbh) unless ($form->{employee_id});
195 my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
199 $query = qq|DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = ?)|;
200 do_query($form, $dbh, $query, conv_i($form->{id}));
202 $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
203 do_query($form, $dbh, $query, conv_i($form->{id}));
205 $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
206 do_query($form, $dbh, $query, conv_i($form->{id}));
210 $query = qq|SELECT nextval('id')|;
211 ($form->{id}) = selectrow_query($form, $dbh, $query);
213 $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id) VALUES (?, '', ?)|;
214 do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}));
220 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
221 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
224 my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
225 my @part_ids = keys %part_id_map;
229 $query = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
230 %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
233 my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
234 my $h_item_id = prepare_query($form, $dbh, $q_item_id);
237 qq|INSERT INTO delivery_order_items (
238 id, delivery_order_id, parts_id, description, longdescription, qty, base_qty,
239 sellprice, discount, unit, reqdate, project_id, serialnumber,
240 ordnumber, transdate, cusordnumber,
241 lastcost, price_factor_id, price_factor, marge_price_factor)
242 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
243 (SELECT factor FROM price_factors WHERE id = ?), ?)|;
244 my $h_item = prepare_query($form, $dbh, $q_item);
247 qq|INSERT INTO delivery_order_items_stock (delivery_order_item_id, qty, unit, warehouse_id, bin_id, chargenumber)
248 VALUES (?, ?, ?, ?, ?, ?)|;
249 my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
251 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
253 for my $i (1 .. $form->{rowcount}) {
254 next if (!$form->{"id_$i"});
256 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
258 my $item_unit = $part_unit_map{$form->{"id_$i"}};
261 if (defined($all_units->{$item_unit}->{factor}) && (($all_units->{$item_unit}->{factor} * 1) != 0)) {
262 $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
264 my $baseqty = $form->{"qty_$i"} * $basefactor;
266 $form->{"lastcost_$i"} *= 1;
268 # set values to 0 if nothing entered
269 $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"}) / 100;
270 $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
272 $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
273 $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
275 $reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
277 do_statement($form, $h_item_id, $q_item_id);
278 my ($item_id) = $h_item_id->fetchrow_array();
280 # save detail record in delivery_order_items table
281 @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
282 $form->{"description_$i"}, $form->{"longdescription_$i"},
283 $form->{"qty_$i"}, $baseqty,
284 $form->{"sellprice_$i"}, $form->{"discount_$i"},
285 $form->{"unit_$i"}, conv_date($reqdate), conv_i($form->{"project_id_$i"}),
286 $form->{"serialnumber_$i"},
287 $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
288 $form->{"cusordnumber_$i"},
289 $form->{"lastcost_$i"},
290 conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
291 conv_i($form->{"marge_price_factor_$i"}));
292 do_statement($form, $h_item, $q_item, @values);
294 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
296 foreach my $sinfo (@{ $stock_info }) {
297 @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
298 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber});
299 do_statement($form, $h_item_stock, $q_item_stock, @values);
303 $h_item_id->finish();
305 $h_item_stock->finish();
307 ($null, $form->{department_id}) = split(/--/, $form->{department});
311 qq|UPDATE delivery_orders SET
312 donumber = ?, ordnumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
313 customer_id = ?, reqdate = ?,
314 shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
315 delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
316 globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
320 @values = ($form->{donumber}, $form->{ordnumber},
321 $form->{cusordnumber}, conv_date($form->{transdate}),
322 conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
323 conv_date($reqdate), $form->{shippingpoint}, $form->{shipvia},
324 $form->{notes}, $form->{intnotes},
325 $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
326 conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
327 conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
328 conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
329 $form->{transaction_description},
330 $form->{type} =~ /^sales/ ? 't' : 'f',
331 conv_i($form->{id}));
332 do_query($form, $dbh, $query, @values);
335 $form->{name} = $form->{ $form->{vc} };
336 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
338 if (!$form->{shipto_id}) {
339 $form->add_shipto($dbh, $form->{id}, "DO");
342 # save printed, emailed, queued
343 $form->save_status($dbh);
345 $self->mark_order_if_delivered('do_id' => $form->{id},
346 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
349 my $rc = $dbh->commit();
351 $form->{saved_donumber} = $form->{donumber};
353 Common::webdav_folder($form) if ($main::webdav);
355 $main::lxdebug->leave_sub();
360 sub mark_order_if_delivered {
361 $main::lxdebug->enter_sub();
366 Common::check_params(\%params, qw(do_id type));
368 my $myconfig = \%main::myconfig;
369 my $form = $main::form;
371 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
373 my $query = qq|SELECT ordnumber FROM delivery_orders WHERE id = ?|;
374 my ($ordnumber) = selectfirst_array_query($form, $dbh, $query, conv_i($params{do_id}));
376 return $main::lxdebug->leave_sub() if (!$ordnumber);
378 my $vc = $params{type} eq 'purchase' ? 'vendor' : 'customer';
379 $query = qq|SELECT id
381 WHERE NOT COALESCE(quotation, FALSE)
382 AND (COALESCE(${vc}_id, 0) > 0)
387 my ($oe_id) = selectfirst_array_query($form, $dbh, $query, $ordnumber);
389 return $main::lxdebug->leave_sub() if (!$oe_id);
391 my $all_units = AM->retrieve_all_units();
393 my %shipped = $self->get_shipped_qty('type' => $params{type},
394 'ordnumber' => $ordnumber,);
397 $query = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit
399 LEFT JOIN parts p ON (oi.parts_id = p.id)
400 WHERE (oi.trans_id = ?)|;
401 my $sth = prepare_execute_query($form, $dbh, $query, $oe_id);
403 while (my $ref = $sth->fetchrow_hashref()) {
404 $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor};
406 if ($ordered{$ref->{parts_id}}) {
407 $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty};
409 $ordered{$ref->{parts_id}} = $ref;
415 map { $_->{baseqty} = $_->{qty} * $all_units->{$_->{unit}}->{factor} / $all_units->{$_->{partunit}}->{factor} } values %shipped;
418 foreach my $part (values %ordered) {
419 if (!$shipped{$part->{parts_id}} || ($shipped{$part->{parts_id}}->{baseqty} < $part->{baseqty})) {
426 $query = qq|UPDATE oe
427 SET delivered = TRUE, closed = TRUE
429 do_query($form, $dbh, $query, $oe_id);
431 $dbh->commit() if (!$params{dbh});
434 $main::lxdebug->leave_sub();
438 $main::lxdebug->enter_sub();
443 Common::check_params(\%params, qw(ids));
445 if (('ARRAY' ne ref $params{ids}) || !scalar @{ $params{ids} }) {
446 $main::lxdebug->leave_sub();
450 my $myconfig = \%main::myconfig;
451 my $form = $main::form;
453 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
455 my $query = qq|UPDATE delivery_orders SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar(@{ $params{ids} })) . qq|)|;
457 do_query($form, $dbh, $query, map { conv_i($_) } @{ $params{ids} });
459 $dbh->commit() unless ($params{dbh});
461 $main::lxdebug->leave_sub();
465 $main::lxdebug->enter_sub();
469 my $myconfig = \%main::myconfig;
470 my $form = $main::form;
471 my $spool = $main::spool;
473 # connect to database
474 my $dbh = $form->get_standard_dbh($myconfig);
477 my $query = qq|SELECT s.spoolfile FROM status s WHERE s.trans_id = ?|;
478 my $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
483 while (($spoolfile) = $sth->fetchrow_array) {
484 push @spoolfiles, $spoolfile;
489 @values = (conv_i($form->{id}));
491 # delete status entries
492 $query = qq|DELETE FROM status
494 do_query($form, $dbh, $query, @values);
496 # delete individual entries
497 $query = qq|DELETE FROM delivery_order_items_stock
498 WHERE delivery_order_item_id IN (
499 SELECT id FROM delivery_order_items
500 WHERE delivery_order_id = ?
502 do_query($form, $dbh, $query, @values);
504 # delete individual entries
505 $query = qq|DELETE FROM delivery_order_items
506 WHERE delivery_order_id = ?|;
507 do_query($form, $dbh, $query, @values);
510 $query = qq|DELETE FROM delivery_orders
512 do_query($form, $dbh, $query, @values);
514 $query = qq|DELETE FROM shipto
515 WHERE trans_id = ? AND module = 'DO'|;
516 do_query($form, $dbh, $query, @values);
518 my $rc = $dbh->commit();
521 foreach $spoolfile (@spoolfiles) {
522 unlink "$spool/$spoolfile" if $spoolfile;
526 $main::lxdebug->leave_sub();
532 $main::lxdebug->enter_sub();
537 my $myconfig = \%main::myconfig;
538 my $form = $main::form;
540 # connect to database
541 my $dbh = $form->get_standard_dbh($myconfig);
543 my ($query, $query_add, @values, $sth, $ref);
545 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
547 my $mode = !$params{ids} ? 'default' : ref $params{ids} eq 'ARRAY' ? 'multi' : 'single';
549 if ($mode eq 'default') {
550 $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate, current_date AS reqdate|);
551 map { $form->{$_} = $ref->{$_} } keys %$ref;
554 $form->lastname_used($dbh, $myconfig, $vc) unless $form->{"${vc}_id"};
556 $main::lxdebug->leave_sub();
561 my @do_ids = map { conv_i($_) } ($mode eq 'multi' ? @{ $params{ids} } : ($params{ids}));
562 my $do_ids_placeholders = join(', ', ('?') x scalar(@do_ids));
564 # retrieve order for single id
565 # NOTE: this query is intended to fetch all information only ONCE.
566 # so if any of these infos is important (or even different) for any item,
567 # it will be killed out and then has to be fetched from the item scope query further down
569 qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.transdate, dord.reqdate,
570 dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
571 e.name AS employee, dord.employee_id, dord.salesman_id,
572 dord.${vc}_id, cv.name AS ${vc},
573 dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
574 d.description AS department, dord.language_id,
576 dord.globalproject_id, dord.delivered, dord.transaction_description
577 FROM delivery_orders dord
578 JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
579 LEFT JOIN employee e ON (dord.employee_id = e.id)
580 LEFT JOIN department d ON (dord.department_id = d.id)
581 WHERE dord.id IN ($do_ids_placeholders)|;
582 $sth = prepare_execute_query($form, $dbh, $query, @do_ids);
584 delete $form->{"${vc}_id"};
585 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
586 if ($form->{"${vc}_id"} && ($ref->{"${vc}_id"} != $form->{"${vc}_id"})) {
588 $main::lxdebug->leave_sub();
593 map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
597 $form->{saved_donumber} = $form->{donumber};
599 # if not given, fill transdate with current_date
600 $form->{transdate} = $form->current_date($myconfig) unless $form->{transdate};
602 if ($mode eq 'single') {
603 $query = qq|SELECT s.* FROM shipto s WHERE s.trans_id = ? AND s.module = 'DO'|;
604 $sth = prepare_execute_query($form, $dbh, $query, $form->{id});
606 $ref = $sth->fetchrow_hashref(NAME_lc);
608 map { $form->{$_} = $ref->{$_} } keys %$ref;
611 # get printed, emailed and queued
612 $query = qq|SELECT s.printed, s.emailed, s.spoolfile, s.formname FROM status s WHERE s.trans_id = ?|;
613 $sth = prepare_execute_query($form, $dbh, $query, conv_i($form->{id}));
615 while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
616 $form->{printed} .= "$ref->{formname} " if $ref->{printed};
617 $form->{emailed} .= "$ref->{formname} " if $ref->{emailed};
618 $form->{queued} .= "$ref->{formname} $ref->{spoolfile} " if $ref->{spoolfile};
621 map { $form->{$_} =~ s/ +$//g } qw(printed emailed queued);
627 my %oid = ('Pg' => 'oid',
628 'Oracle' => 'rowid');
630 # retrieve individual items
631 # this query looks up all information about the items
632 # stuff different from the whole will not be overwritten, but saved with a suffix.
634 qq|SELECT doi.id AS delivery_order_items_id,
635 p.partnumber, p.assembly, doi.description, doi.qty,
636 doi.sellprice, doi.parts_id AS id, doi.unit, doi.discount, p.bin, p.notes AS partnotes,
637 doi.reqdate, doi.project_id, doi.serialnumber, doi.lastcost,
638 doi.ordnumber, doi.transdate, doi.cusordnumber, doi.longdescription,
639 doi.price_factor_id, doi.price_factor, doi.marge_price_factor,
642 FROM delivery_order_items doi
643 JOIN parts p ON (doi.parts_id = p.id)
644 JOIN delivery_orders dord ON (doi.delivery_order_id = dord.id)
645 LEFT JOIN project pr ON (doi.project_id = pr.id)
646 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
647 WHERE doi.delivery_order_id IN ($do_ids_placeholders)
648 ORDER BY doi.$oid{$myconfig->{dbdriver}}|;
650 $form->{form_details} = selectall_hashref_query($form, $dbh, $query, @do_ids);
652 if ($mode eq 'single') {
653 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
656 qq|SELECT qty, unit, bin_id, warehouse_id, chargenumber
657 FROM delivery_order_items_stock
658 WHERE delivery_order_item_id = ?|;
659 my $sth = prepare_query($form, $dbh, $query);
661 foreach my $doi (@{ $form->{form_details} }) {
662 do_statement($form, $sth, $query, conv_i($doi->{delivery_order_items_id}));
664 while (my $ref = $sth->fetchrow_hashref()) {
665 push @{ $requests }, $ref;
668 $doi->{"stock_${in_out}"} = YAML::Dump($requests);
674 Common::webdav_folder($form) if ($main::webdav);
676 $main::lxdebug->leave_sub();
682 $main::lxdebug->enter_sub();
686 my $myconfig = \%main::myconfig;
687 my $form = $main::form;
689 # connect to database
690 my $dbh = $form->get_standard_dbh($myconfig);
700 my %oid = ('Pg' => 'oid',
701 'Oracle' => 'rowid');
703 my (@project_ids, %projectnumbers);
705 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
707 # sort items by partsgroup
708 for $i (1 .. $form->{rowcount}) {
710 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
711 $partsgroup = $form->{"partsgroup_$i"};
713 push @partsgroup, [$i, $partsgroup];
714 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
718 $query = "SELECT id, projectnumber FROM project WHERE id IN (" .
719 join(", ", map("?", @project_ids)) . ")";
720 $sth = prepare_execute_query($form, $dbh, $query, @project_ids);
721 while (my $ref = $sth->fetchrow_hashref()) {
722 $projectnumbers{$ref->{id}} = $ref->{projectnumber};
727 $form->{"globalprojectnumber"} =
728 $projectnumbers{$form->{"globalproject_id"}};
730 my $q_pg = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
732 JOIN parts p ON (a.parts_id = p.id)
733 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
735 AND a.id = ? $sortorder|;
736 my $h_pg = prepare_query($form, $dbh, $q_pg);
738 my $q_bin_wh = qq|SELECT (SELECT description FROM bin WHERE id = ?) AS bin,
739 (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
740 my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
742 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
747 qw(runningnumber number description longdescription qty unit
748 partnotes serialnumber reqdate projectnumber
749 si_runningnumber si_number si_description
750 si_warehouse si_bin si_chargenumber si_qty si_unit);
753 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
756 next if (!$form->{"id_$i"});
760 if ($item->[1] ne $sameitem) {
761 push(@{ $form->{description} }, qq|$item->[1]|);
762 $sameitem = $item->[1];
764 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
767 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
769 # add number, description and qty to $form->{number}, ....
771 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
773 push @{ $form->{runningnumber} }, $position;
774 push @{ $form->{number} }, $form->{"partnumber_$i"};
775 push @{ $form->{description} }, $form->{"description_$i"};
776 push @{ $form->{longdescription} }, $form->{"longdescription_$i"};
777 push @{ $form->{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"});
778 push @{ $form->{unit} }, $form->{"unit_$i"};
779 push @{ $form->{partnotes} }, $form->{"partnotes_$i"};
780 push @{ $form->{serialnumber} }, $form->{"serialnumber_$i"};
781 push @{ $form->{reqdate} }, $form->{"reqdate_$i"};
782 push @{ $form->{projectnumber} }, $projectnumbers{$form->{"project_id_$i"}};
784 if ($form->{"assembly_$i"}) {
787 # get parts and push them onto the stack
789 if ($form->{groupitems}) {
791 qq|ORDER BY pg.partsgroup, a.$oid{$myconfig->{dbdriver}}|;
793 $sortorder = qq|ORDER BY a.$oid{$myconfig->{dbdriver}}|;
796 do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
798 while (my $ref = $h_pg->fetchrow_hashref(NAME_lc)) {
799 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
800 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
801 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
802 push(@{ $form->{description} }, $sameitem);
805 push(@{ $form->{description} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq|, $ref->{partnumber}, $ref->{description}|);
807 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
811 if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
812 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
814 foreach my $si (@{ $stock_info }) {
817 do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
818 my $bin_wh = $h_bin_wh->fetchrow_hashref();
820 push @{ $form->{si_runningnumber} }, $num_si;
821 push @{ $form->{si_number} }, $form->{"partnumber_$i"};
822 push @{ $form->{si_description} }, $form->{"description_$i"};
823 push @{ $form->{si_warehouse} }, $bin_wh->{warehouse};
824 push @{ $form->{si_bin} }, $bin_wh->{bin};
825 push @{ $form->{si_chargenumber} }, $si->{chargenumber};
826 push @{ $form->{si_qty} }, $form->format_amount($myconfig, $si->{qty} * 1);
827 push @{ $form->{si_unit} }, $si->{unit};
835 $form->{username} = $myconfig->{name};
837 $main::lxdebug->leave_sub();
840 sub project_description {
841 $main::lxdebug->enter_sub();
843 my ($self, $dbh, $id) = @_;
845 my $query = qq|SELECT description FROM project WHERE id = ?|;
846 my ($value) = selectrow_query($form, $dbh, $query, $id);
848 $main::lxdebug->leave_sub();
853 sub unpack_stock_information {
854 $main::lxdebug->enter_sub();
859 Common::check_params_x(\%params, qw(packed));
863 eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
865 $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
867 foreach my $entry (@{ $unpacked }) {
868 next if ('HASH' eq ref $entry);
873 $main::lxdebug->leave_sub();
878 sub get_item_availability {
879 $main::lxdebug->enter_sub();
884 Common::check_params(\%params, qw(parts_id));
886 my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
887 my $form = $main::form;
890 qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, SUM(qty) AS qty, i.parts_id,
891 w.description AS warehousedescription,
892 b.description AS bindescription
894 LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
895 LEFT JOIN bin b ON (i.bin_id = b.id)
896 WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
898 GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.parts_id, w.description, b.description
899 ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber)|;
901 my $contents = selectall_hashref_query($form, $form->get_standard_dbh($myconfig), $query, @parts_ids);
903 $main::lxdebug->leave_sub();
905 return @{ $contents };
909 sub check_stock_availability {
910 $main::lxdebug->enter_sub();
915 Common::check_params(\%params, qw(requests parts_id));
917 my $myconfig = \%main::myconfig;
918 my $form = $main::form;
920 my $dbh = $form->get_standard_dbh($myconfig);
922 my $units = AM->retrieve_units($myconfig, $form);
924 my ($partunit) = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
925 my $unit_factor = $units->{$partunit}->{factor} || 1;
927 my @contents = $self->get_item_availability(%params);
931 foreach my $sinfo (@{ $params{requests} }) {
934 foreach my $row (@contents) {
935 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
936 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
937 ($row->{chargenumber} ne $sinfo->{chargenumber}));
941 my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
943 if ($base_qty > $row->{qty}) {
945 push @errors, $sinfo;
951 push @errors, $sinfo if (!$found);
954 $main::lxdebug->leave_sub();
959 sub transfer_in_out {
960 $main::lxdebug->enter_sub();
965 Common::check_params(\%params, qw(direction requests));
967 if (!@{ $params{requests} }) {
968 $main::lxdebug->leave_sub();
972 my $myconfig = \%main::myconfig;
973 my $form = $main::form;
975 my $prefix = $params{direction} eq 'in' ? 'dst' : 'src';
979 foreach my $request (@{ $params{requests} }) {
981 'parts_id' => $request->{parts_id},
982 "${prefix}_warehouse_id" => $request->{warehouse_id},
983 "${prefix}_bin_id" => $request->{bin_id},
984 'chargenumber' => $request->{chargenumber},
985 'qty' => $request->{qty},
986 'unit' => $request->{unit},
987 'oe_id' => $form->{id},
988 'shippingdate' => 'current_date',
989 'transfer_type' => $params{direction} eq 'in' ? 'stock' : 'shipped',
993 WH->transfer(@transfers);
995 $main::lxdebug->leave_sub();
998 sub get_shipped_qty {
999 $main::lxdebug->enter_sub();
1004 Common::check_params(\%params, qw(type ordnumber));
1006 my $myconfig = \%main::myconfig;
1007 my $form = $main::form;
1009 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1011 my $notsales = $params{type} eq 'sales' ? '' : 'NOT';
1014 qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit
1015 FROM delivery_order_items doi
1016 LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id)
1017 LEFT JOIN parts p ON (doi.parts_id = p.id)
1018 WHERE ($notsales o.is_sales)
1019 AND (o.ordnumber = ?)|;
1022 my $entries = selectall_hashref_query($form, $dbh, $query, $params{ordnumber});
1023 my $all_units = AM->retrieve_all_units();
1025 foreach my $entry (@{ $entries }) {
1026 $entry->{qty} *= $all_units->{$entry->{unit}}->{factor} / $all_units->{$entry->{partunit}}->{factor};
1028 if (!$ship{$entry->{parts_id}}) {
1029 $ship{$entry->{parts_id}} = $entry;
1031 $ship{$entry->{parts_id}}->{qty} += $entry->{qty};
1035 $main::lxdebug->leave_sub();
1040 sub is_marked_as_delivered {
1041 $main::lxdebug->enter_sub();
1046 Common::check_params(\%params, qw(id));
1048 my $myconfig = \%main::myconfig;
1049 my $form = $main::form;
1051 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1053 my ($delivered) = selectfirst_array_query($form, $dbh, qq|SELECT delivered FROM delivery_orders WHERE id = ?|, conv_i($params{id}));
1055 $main::lxdebug->leave_sub();
1057 return $delivered ? 1 : 0;