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);
42 use SL::DB::DeliveryOrder;
45 use SL::HTML::Restrict;
53 $main::lxdebug->enter_sub();
57 my $myconfig = \%main::myconfig;
58 my $form = $main::form;
61 my $dbh = $form->get_standard_dbh($myconfig);
63 my (@where, @values, $where);
65 my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
68 qq|SELECT dord.id, dord.donumber, dord.ordnumber, dord.cusordnumber,
69 dord.transdate, dord.reqdate,
70 ct.${vc}number, ct.name, dord.${vc}_id, dord.globalproject_id,
71 dord.closed, dord.delivered, dord.shippingpoint, dord.shipvia,
72 dord.transaction_description,
73 pr.projectnumber AS globalprojectnumber,
74 dep.description AS department,
77 FROM delivery_orders dord
78 LEFT JOIN $vc ct ON (dord.${vc}_id = ct.id)
79 LEFT JOIN employee e ON (dord.employee_id = e.id)
80 LEFT JOIN employee sm ON (dord.salesman_id = sm.id)
81 LEFT JOIN project pr ON (dord.globalproject_id = pr.id)
82 LEFT JOIN department dep ON (dord.department_id = dep.id)
85 push @where, ($form->{type} eq 'sales_delivery_order' ? '' : 'NOT ') . qq|COALESCE(dord.is_sales, FALSE)|;
87 if ($form->{department_id}) {
88 push @where, qq|dord.department_id = ?|;
89 push @values, conv_i($form->{department_id});
92 if ($form->{project_id}) {
94 qq|(dord.globalproject_id = ?) OR EXISTS
95 (SELECT * FROM delivery_order_items doi
96 WHERE (doi.project_id = ?) AND (doi.delivery_order_id = dord.id))|;
97 push @values, conv_i($form->{project_id}), conv_i($form->{project_id});
100 if ($form->{"${vc}_id"}) {
101 push @where, qq|dord.${vc}_id = ?|;
102 push @values, $form->{"${vc}_id"};
104 } elsif ($form->{$vc}) {
105 push @where, qq|ct.name ILIKE ?|;
106 push @values, '%' . $form->{$vc} . '%';
109 foreach my $item (qw(employee_id salesman_id)) {
110 next unless ($form->{$item});
111 push @where, "dord.$item = ?";
112 push @values, conv_i($form->{$item});
114 if (!$main::auth->assert('sales_all_edit', 1)) {
115 push @where, qq|dord.employee_id = (select id from employee where login= ?)|;
116 push @values, $form->{login};
119 foreach my $item (qw(donumber ordnumber cusordnumber transaction_description)) {
120 next unless ($form->{$item});
121 push @where, qq|dord.$item ILIKE ?|;
122 push @values, '%' . $form->{$item} . '%';
125 if (($form->{open} || $form->{closed}) &&
126 ($form->{open} ne $form->{closed})) {
127 push @where, ($form->{open} ? "NOT " : "") . "COALESCE(dord.closed, FALSE)";
130 if (($form->{notdelivered} || $form->{delivered}) &&
131 ($form->{notdelivered} ne $form->{delivered})) {
132 push @where, ($form->{delivered} ? "" : "NOT ") . "COALESCE(dord.delivered, FALSE)";
135 if ($form->{serialnumber}) {
136 push @where, 'dord.id IN (SELECT doi.delivery_order_id FROM delivery_order_items doi WHERE doi.serialnumber LIKE ?)';
137 push @values, '%' . $form->{serialnumber} . '%';
140 if($form->{transdatefrom}) {
141 push @where, qq|dord.transdate >= ?|;
142 push @values, conv_date($form->{transdatefrom});
145 if($form->{transdateto}) {
146 push @where, qq|dord.transdate <= ?|;
147 push @values, conv_date($form->{transdateto});
150 if($form->{reqdatefrom}) {
151 push @where, qq|dord.reqdate >= ?|;
152 push @values, conv_date($form->{reqdatefrom});
155 if($form->{reqdateto}) {
156 push @where, qq|dord.reqdate <= ?|;
157 push @values, conv_date($form->{reqdateto});
161 $query .= " WHERE " . join(" AND ", map { "($_)" } @where);
164 my %allowed_sort_columns = (
165 "transdate" => "dord.transdate",
166 "reqdate" => "dord.reqdate",
168 "donumber" => "dord.donumber",
169 "ordnumber" => "dord.ordnumber",
171 "employee" => "e.name",
172 "salesman" => "sm.name",
173 "shipvia" => "dord.shipvia",
174 "transaction_description" => "dord.transaction_description",
175 "department" => "lower(dep.description)",
178 my $sortdir = !defined $form->{sortdir} ? 'ASC' : $form->{sortdir} ? 'ASC' : 'DESC';
179 my $sortorder = "dord.id";
180 if ($form->{sort} && grep($form->{sort}, keys(%allowed_sort_columns))) {
181 $sortorder = $allowed_sort_columns{$form->{sort}};
184 $query .= qq| ORDER by | . $sortorder . " $sortdir";
186 $form->{DO} = selectall_hashref_query($form, $dbh, $query, @values);
188 if (scalar @{ $form->{DO} }) {
192 WHERE NOT COALESCE(quotation, FALSE)
194 AND (COALESCE(${vc}_id, 0) != 0)|;
196 my $sth = prepare_query($form, $dbh, $query);
198 foreach my $dord (@{ $form->{DO} }) {
199 next unless ($dord->{ordnumber});
200 do_statement($form, $sth, $query, $dord->{ordnumber});
201 ($dord->{oe_id}) = $sth->fetchrow_array();
207 $main::lxdebug->leave_sub();
211 $main::lxdebug->enter_sub();
215 my $myconfig = \%main::myconfig;
216 my $form = $main::form;
218 # connect to database, turn off autocommit
219 my $dbh = $form->get_standard_dbh($myconfig);
220 my $restricter = SL::HTML::Restrict->create;
222 my ($query, @values, $sth, $null);
224 my $all_units = AM->retrieve_units($myconfig, $form);
225 $form->{all_units} = $all_units;
227 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
230 my $trans_number = SL::TransNumber->new(type => $form->{type}, dbh => $dbh, number => $form->{donumber}, id => $form->{id});
231 $form->{donumber} ||= $trans_number->create_unique;
232 $form->{employee_id} = (split /--/, $form->{employee})[1] if !$form->{employee_id};
233 $form->get_employee($dbh) unless ($form->{employee_id});
235 my $ml = ($form->{type} eq 'sales_delivery_order') ? 1 : -1;
239 $query = qq|DELETE FROM delivery_order_items_stock WHERE delivery_order_item_id IN (SELECT id FROM delivery_order_items WHERE delivery_order_id = ?)|;
240 do_query($form, $dbh, $query, conv_i($form->{id}));
242 $query = qq|DELETE FROM delivery_order_items WHERE delivery_order_id = ?|;
243 do_query($form, $dbh, $query, conv_i($form->{id}));
245 $query = qq|DELETE FROM shipto WHERE trans_id = ? AND module = 'DO'|;
246 do_query($form, $dbh, $query, conv_i($form->{id}));
250 $query = qq|SELECT nextval('id')|;
251 ($form->{id}) = selectrow_query($form, $dbh, $query);
253 $query = qq|INSERT INTO delivery_orders (id, donumber, employee_id, currency_id) VALUES (?, '', ?, (SELECT currency_id FROM defaults LIMIT 1))|;
254 do_query($form, $dbh, $query, $form->{id}, conv_i($form->{employee_id}));
260 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
261 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
264 my %part_id_map = map { $_ => 1 } grep { $_ } map { $form->{"id_$_"} } (1 .. $form->{rowcount});
265 my @part_ids = keys %part_id_map;
269 $query = qq|SELECT id, unit FROM parts WHERE id IN (| . join(', ', map { '?' } @part_ids) . qq|)|;
270 %part_unit_map = selectall_as_map($form, $dbh, $query, 'id', 'unit', @part_ids);
273 my $q_item_id = qq|SELECT nextval('delivery_order_items_id')|;
274 my $h_item_id = prepare_query($form, $dbh, $q_item_id);
277 qq|INSERT INTO delivery_order_items (
278 id, delivery_order_id, parts_id, description, longdescription, qty, base_qty,
279 sellprice, discount, unit, reqdate, project_id, serialnumber,
280 ordnumber, transdate, cusordnumber,
281 lastcost, price_factor_id, price_factor, marge_price_factor, pricegroup_id)
282 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
283 (SELECT factor FROM price_factors WHERE id = ?), ?, ?)|;
284 my $h_item = prepare_query($form, $dbh, $q_item);
287 qq|INSERT INTO delivery_order_items_stock (delivery_order_item_id, qty, unit, warehouse_id, bin_id, chargenumber, bestbefore)
288 VALUES (?, ?, ?, ?, ?, ?, ?)|;
289 my $h_item_stock = prepare_query($form, $dbh, $q_item_stock);
291 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
293 for my $i (1 .. $form->{rowcount}) {
294 next if (!$form->{"id_$i"});
296 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
298 my $item_unit = $part_unit_map{$form->{"id_$i"}};
301 if (defined($all_units->{$item_unit}->{factor}) && (($all_units->{$item_unit}->{factor} * 1) != 0)) {
302 $basefactor = $all_units->{$form->{"unit_$i"}}->{factor} / $all_units->{$item_unit}->{factor};
304 my $baseqty = $form->{"qty_$i"} * $basefactor;
306 # set values to 0 if nothing entered
307 $form->{"discount_$i"} = $form->parse_amount($myconfig, $form->{"discount_$i"});
308 $form->{"sellprice_$i"} = $form->parse_amount($myconfig, $form->{"sellprice_$i"});
309 $form->{"lastcost_$i"} = $form->parse_amount($myconfig, $form->{"lastcost_$i"});
311 $price_factor = $price_factors{ $form->{"price_factor_id_$i"} } || 1;
312 my $linetotal = $form->round_amount($form->{"sellprice_$i"} * $form->{"qty_$i"} / $price_factor, 2);
314 $items_reqdate = ($form->{"reqdate_$i"}) ? $form->{"reqdate_$i"} : undef;
316 do_statement($form, $h_item_id, $q_item_id);
317 my ($item_id) = $h_item_id->fetchrow_array();
319 # Get pricegroup_id and save it. Unfortunately the interface
320 # also uses ID "0" for signalling that none is selected, but "0"
321 # must not be stored in the database. Therefore we cannot simply
323 my $pricegroup_id = $form->{"pricegroup_id_$i"} * 1;
324 $pricegroup_id = undef if !$pricegroup_id;
326 # save detail record in delivery_order_items table
327 @values = (conv_i($item_id), conv_i($form->{id}), conv_i($form->{"id_$i"}),
328 $form->{"description_$i"}, $restricter->process($form->{"longdescription_$i"}),
329 $form->{"qty_$i"}, $baseqty,
330 $form->{"sellprice_$i"}, $form->{"discount_$i"} / 100,
331 $form->{"unit_$i"}, conv_date($items_reqdate), conv_i($form->{"project_id_$i"}),
332 $form->{"serialnumber_$i"},
333 $form->{"ordnumber_$i"}, conv_date($form->{"transdate_$i"}),
334 $form->{"cusordnumber_$i"},
335 $form->{"lastcost_$i"},
336 conv_i($form->{"price_factor_id_$i"}), conv_i($form->{"price_factor_id_$i"}),
337 conv_i($form->{"marge_price_factor_$i"}),
339 do_statement($form, $h_item, $q_item, @values);
341 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
343 foreach my $sinfo (@{ $stock_info }) {
344 @values = ($item_id, $sinfo->{qty}, $sinfo->{unit}, conv_i($sinfo->{warehouse_id}),
345 conv_i($sinfo->{bin_id}), $sinfo->{chargenumber}, conv_date($sinfo->{bestbefore}));
346 do_statement($form, $h_item_stock, $q_item_stock, @values);
349 CVar->save_custom_variables(module => 'IC',
350 sub_module => 'delivery_order_items',
351 trans_id => $item_id,
352 configs => $ic_cvar_configs,
354 name_prefix => 'ic_',
355 name_postfix => "_$i",
359 $h_item_id->finish();
361 $h_item_stock->finish();
364 # reqdate is last items reqdate (?: old behaviour) if not already set
365 $form->{reqdate} ||= $items_reqdate;
368 qq|UPDATE delivery_orders SET
369 donumber = ?, ordnumber = ?, cusordnumber = ?, transdate = ?, vendor_id = ?,
370 customer_id = ?, reqdate = ?,
371 shippingpoint = ?, shipvia = ?, notes = ?, intnotes = ?, closed = ?,
372 delivered = ?, department_id = ?, language_id = ?, shipto_id = ?,
373 globalproject_id = ?, employee_id = ?, salesman_id = ?, cp_id = ?, transaction_description = ?,
374 is_sales = ?, taxzone_id = ?, taxincluded = ?, terms = ?, currency_id = (SELECT id FROM currencies WHERE name = ?),
378 @values = ($form->{donumber}, $form->{ordnumber},
379 $form->{cusordnumber}, conv_date($form->{transdate}),
380 conv_i($form->{vendor_id}), conv_i($form->{customer_id}),
381 conv_date($form->{reqdate}), $form->{shippingpoint}, $form->{shipvia},
382 $form->{notes}, $form->{intnotes},
383 $form->{closed} ? 't' : 'f', $form->{delivered} ? "t" : "f",
384 conv_i($form->{department_id}), conv_i($form->{language_id}), conv_i($form->{shipto_id}),
385 conv_i($form->{globalproject_id}), conv_i($form->{employee_id}),
386 conv_i($form->{salesman_id}), conv_i($form->{cp_id}),
387 $form->{transaction_description},
388 $form->{type} =~ /^sales/ ? 't' : 'f',
389 conv_i($form->{taxzone_id}), $form->{taxincluded} ? 't' : 'f', conv_i($form->{terms}), $form->{currency},
390 conv_i($form->{delivery_term_id}),
391 conv_i($form->{id}));
392 do_query($form, $dbh, $query, @values);
394 $form->{name} = $form->{ $form->{vc} };
395 $form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
398 if (!$form->{shipto_id}) {
399 $form->add_shipto($dbh, $form->{id}, "DO");
402 # save printed, emailed, queued
403 $form->save_status($dbh);
405 # Link this delivery order to the quotations it was created from.
406 RecordLinks->create_links('dbh' => $dbh,
408 'from_table' => 'oe',
409 'from_ids' => $form->{convert_from_oe_ids},
410 'to_table' => 'delivery_orders',
411 'to_id' => $form->{id},
413 delete $form->{convert_from_oe_ids};
415 $self->mark_orders_if_delivered('do_id' => $form->{id},
416 'type' => $form->{type} eq 'sales_delivery_order' ? 'sales' : 'purchase',
419 my $rc = $dbh->commit();
421 $form->{saved_donumber} = $form->{donumber};
422 $form->{saved_ordnumber} = $form->{ordnumber};
423 $form->{saved_cusordnumber} = $form->{cusordnumber};
425 Common::webdav_folder($form);
427 $main::lxdebug->leave_sub();
432 sub mark_orders_if_delivered {
433 $main::lxdebug->enter_sub();
438 Common::check_params(\%params, qw(do_id type));
440 my $myconfig = \%main::myconfig;
441 my $form = $main::form;
443 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
445 my @links = RecordLinks->get_links('dbh' => $dbh,
446 'from_table' => 'oe',
447 'to_table' => 'delivery_orders',
448 'to_id' => $params{do_id});
450 my $oe_id = @links ? $links[0]->{from_id} : undef;
452 return $main::lxdebug->leave_sub() if (!$oe_id);
454 my $all_units = AM->retrieve_all_units();
456 my $query = qq|SELECT oi.parts_id, oi.qty, oi.unit, p.unit AS partunit
458 LEFT JOIN parts p ON (oi.parts_id = p.id)
459 WHERE (oi.trans_id = ?)|;
460 my $sth = prepare_execute_query($form, $dbh, $query, $oe_id);
462 my %shipped = $self->get_shipped_qty('type' => $params{type},
466 while (my $ref = $sth->fetchrow_hashref()) {
467 $ref->{baseqty} = $ref->{qty} * $all_units->{$ref->{unit}}->{factor} / $all_units->{$ref->{partunit}}->{factor};
469 if ($ordered{$ref->{parts_id}}) {
470 $ordered{$ref->{parts_id}}->{baseqty} += $ref->{baseqty};
472 $ordered{$ref->{parts_id}} = $ref;
478 map { $_->{baseqty} = $_->{qty} * $all_units->{$_->{unit}}->{factor} / $all_units->{$_->{partunit}}->{factor} } values %shipped;
481 foreach my $part (values %ordered) {
482 if (!$shipped{$part->{parts_id}} || ($shipped{$part->{parts_id}}->{baseqty} < $part->{baseqty})) {
489 $query = qq|UPDATE oe
492 do_query($form, $dbh, $query, $oe_id);
493 $dbh->commit() if (!$params{dbh});
496 $main::lxdebug->leave_sub();
500 $main::lxdebug->enter_sub();
505 Common::check_params(\%params, qw(ids));
507 if (('ARRAY' ne ref $params{ids}) || !scalar @{ $params{ids} }) {
508 $main::lxdebug->leave_sub();
512 my $myconfig = \%main::myconfig;
513 my $form = $main::form;
515 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
517 my $query = qq|UPDATE delivery_orders SET closed = TRUE WHERE id IN (| . join(', ', ('?') x scalar(@{ $params{ids} })) . qq|)|;
519 do_query($form, $dbh, $query, map { conv_i($_) } @{ $params{ids} });
521 $dbh->commit() unless ($params{dbh});
523 $main::lxdebug->leave_sub();
527 $main::lxdebug->enter_sub();
531 my $myconfig = \%main::myconfig;
532 my $form = $main::form;
533 my $spool = $::lx_office_conf{paths}->{spool};
535 my $rc = SL::DB::Order->new->db->with_transaction(sub {
536 my @spoolfiles = grep { $_ } map { $_->spoolfile } @{ SL::DB::Manager::Status->get_all(where => [ trans_id => $form->{id} ]) };
538 SL::DB::DeliveryOrder->new(id => $form->{id})->delete;
540 my $spool = $::lx_office_conf{paths}->{spool};
541 unlink map { "$spool/$_" } @spoolfiles if $spool;
546 $main::lxdebug->leave_sub();
552 $main::lxdebug->enter_sub();
557 my $myconfig = \%main::myconfig;
558 my $form = $main::form;
560 # connect to database
561 my $dbh = $form->get_standard_dbh($myconfig);
563 my ($query, $query_add, @values, $sth, $ref);
565 my $ic_cvar_configs = CVar->get_configs(module => 'IC',
568 my $vc = $params{vc} eq 'customer' ? 'customer' : 'vendor';
570 my $mode = !$params{ids} ? 'default' : ref $params{ids} eq 'ARRAY' ? 'multi' : 'single';
572 if ($mode eq 'default') {
573 $ref = selectfirst_hashref_query($form, $dbh, qq|SELECT current_date AS transdate|);
574 map { $form->{$_} = $ref->{$_} } keys %$ref;
576 # if reqdate is not set from oe-workflow, set it to transdate (which is current date)
577 $form->{reqdate} ||= $form->{transdate};
580 $form->lastname_used($dbh, $myconfig, $vc) unless $form->{"${vc}_id"};
582 $main::lxdebug->leave_sub();
587 my @do_ids = map { conv_i($_) } ($mode eq 'multi' ? @{ $params{ids} } : ($params{ids}));
588 my $do_ids_placeholders = join(', ', ('?') x scalar(@do_ids));
590 # retrieve order for single id
591 # NOTE: this query is intended to fetch all information only ONCE.
592 # so if any of these infos is important (or even different) for any item,
593 # it will be killed out and then has to be fetched from the item scope query further down
595 qq|SELECT dord.cp_id, dord.donumber, dord.ordnumber, dord.transdate, dord.reqdate,
596 dord.shippingpoint, dord.shipvia, dord.notes, dord.intnotes,
597 e.name AS employee, dord.employee_id, dord.salesman_id,
598 dord.${vc}_id, cv.name AS ${vc},
599 dord.closed, dord.reqdate, dord.department_id, dord.cusordnumber,
600 d.description AS department, dord.language_id,
602 dord.globalproject_id, dord.delivered, dord.transaction_description,
603 dord.taxzone_id, dord.taxincluded, dord.terms, (SELECT cu.name FROM currencies cu WHERE cu.id=dord.currency_id) AS currency,
604 dord.delivery_term_id
605 FROM delivery_orders dord
606 JOIN ${vc} cv ON (dord.${vc}_id = cv.id)
607 LEFT JOIN employee e ON (dord.employee_id = e.id)
608 LEFT JOIN department d ON (dord.department_id = d.id)
609 WHERE dord.id IN ($do_ids_placeholders)|;
610 $sth = prepare_execute_query($form, $dbh, $query, @do_ids);
612 delete $form->{"${vc}_id"};
614 $form->{ordnumber_array} = ' ';
615 $form->{cusordnumber_array} = ' ';
616 while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
617 if ($form->{"${vc}_id"} && ($ref->{"${vc}_id"} != $form->{"${vc}_id"})) {
619 $main::lxdebug->leave_sub();
624 map { $form->{$_} = $ref->{$_} } keys %$ref if ($ref);
625 $form->{donumber_array} .= $form->{donumber} . ' ';
626 $pos = index($form->{ordnumber_array},' ' . $form->{ordnumber} . ' ');
628 $form->{ordnumber_array} .= $form->{ordnumber} . ' ';
630 $pos = index($form->{cusordnumber_array},' ' . $form->{cusordnumber} . ' ');
632 $form->{cusordnumber_array} .= $form->{cusordnumber} . ' ';
637 $form->{donumber_array} =~ s/\s*$//g;
638 $form->{ordnumber_array} =~ s/ //;
639 $form->{ordnumber_array} =~ s/\s*$//g;
640 $form->{cusordnumber_array} =~ s/ //;
641 $form->{cusordnumber_array} =~ s/\s*$//g;
643 $form->{saved_donumber} = $form->{donumber};
644 $form->{saved_ordnumber} = $form->{ordnumber};
645 $form->{saved_cusordnumber} = $form->{cusordnumber};
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, dord.donumber,
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);
751 my $subtotal_header = 0;
756 push(@project_ids, $form->{"globalproject_id"}) if ($form->{"globalproject_id"});
758 # sort items by partsgroup
759 for $i (1 .. $form->{rowcount}) {
761 if ($form->{"partsgroup_$i"} && $form->{groupitems}) {
762 $partsgroup = $form->{"partsgroup_$i"};
764 push @partsgroup, [$i, $partsgroup];
765 push(@project_ids, $form->{"project_id_$i"}) if ($form->{"project_id_$i"});
771 $projects = SL::DB::Manager::Project->get_all(query => [ id => \@project_ids ]);
772 %projects_by_id = map { $_->id => $_ } @$projects;
775 if ($projects_by_id{$form->{"globalproject_id"}}) {
776 $form->{globalprojectnumber} = $projects_by_id{$form->{"globalproject_id"}}->projectnumber;
777 $form->{globalprojectdescription} = $projects_by_id{$form->{"globalproject_id"}}->description;
779 for (@{ $projects_by_id{$form->{"globalproject_id"}}->cvars_by_config }) {
780 $form->{"project_cvar_" . $_->config->name} = $_->value_as_text;
784 my $q_pg = qq|SELECT p.partnumber, p.description, p.unit, a.qty, pg.partsgroup
786 JOIN parts p ON (a.parts_id = p.id)
787 LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
790 my $h_pg = prepare_query($form, $dbh, $q_pg);
792 my $q_bin_wh = qq|SELECT (SELECT description FROM bin WHERE id = ?) AS bin,
793 (SELECT description FROM warehouse WHERE id = ?) AS warehouse|;
794 my $h_bin_wh = prepare_query($form, $dbh, $q_bin_wh);
796 my $in_out = $form->{type} =~ /^sales/ ? 'out' : 'in';
800 my $ic_cvar_configs = CVar->get_configs(module => 'IC');
801 my $project_cvar_configs = CVar->get_configs(module => 'Projects');
803 $form->{TEMPLATE_ARRAYS} = { };
804 IC->prepare_parts_for_printing(myconfig => $myconfig, form => $form);
807 qw(runningnumber number description longdescription qty unit
808 partnotes serialnumber reqdate projectnumber projectdescription
809 si_runningnumber si_number si_description
810 si_warehouse si_bin si_chargenumber si_bestbefore si_qty si_unit weight lineweight);
812 map { $form->{TEMPLATE_ARRAYS}->{$_} = [] } (@arrays);
814 push @arrays, map { "ic_cvar_$_->{name}" } @{ $ic_cvar_configs };
815 push @arrays, map { "project_cvar_$_->{name}" } @{ $project_cvar_configs };
817 $form->get_lists('price_factors' => 'ALL_PRICE_FACTORS');
818 my %price_factors = map { $_->{id} => $_->{factor} } @{ $form->{ALL_PRICE_FACTORS} };
822 foreach $item (sort { $a->[1] cmp $b->[1] } @partsgroup) {
825 next if (!$form->{"id_$i"});
827 if ($item->[1] ne $sameitem) {
828 push(@{ $form->{description} }, qq|$item->[1]|);
829 $sameitem = $item->[1];
831 map({ push(@{ $form->{$_} }, "") } grep({ $_ ne "description" } @arrays));
834 $form->{"qty_$i"} = $form->parse_amount($myconfig, $form->{"qty_$i"});
836 # add number, description and qty to $form->{number}, ....
837 if ($form->{"subtotal_$i"} && !$subtotal_header) {
838 $subtotal_header = $i;
839 $position = int($position);
842 } elsif ($subtotal_header) {
844 $position = int($position);
845 $position = $position.".".$subposition;
847 $position = int($position);
851 my $price_factor = $price_factors{$form->{"price_factor_id_$i"}} || { 'factor' => 1 };
852 my $project = $projects_by_id{$form->{"project_id_$i"}} || SL::DB::Project->new;
854 push @{ $form->{TEMPLATE_ARRAYS}{runningnumber} }, $position;
855 push @{ $form->{TEMPLATE_ARRAYS}{number} }, $form->{"partnumber_$i"};
856 push @{ $form->{TEMPLATE_ARRAYS}{description} }, $form->{"description_$i"};
857 push @{ $form->{TEMPLATE_ARRAYS}{longdescription} }, $form->{"longdescription_$i"};
858 push @{ $form->{TEMPLATE_ARRAYS}{qty} }, $form->format_amount($myconfig, $form->{"qty_$i"});
859 push @{ $form->{TEMPLATE_ARRAYS}{qty_nofmt} }, $form->{"qty_$i"};
860 push @{ $form->{TEMPLATE_ARRAYS}{unit} }, $form->{"unit_$i"};
861 push @{ $form->{TEMPLATE_ARRAYS}{partnotes} }, $form->{"partnotes_$i"};
862 push @{ $form->{TEMPLATE_ARRAYS}{serialnumber} }, $form->{"serialnumber_$i"};
863 push @{ $form->{TEMPLATE_ARRAYS}{reqdate} }, $form->{"reqdate_$i"};
864 push @{ $form->{TEMPLATE_ARRAYS}{projectnumber} }, $project->projectnumber;
865 push @{ $form->{TEMPLATE_ARRAYS}{projectdescription} }, $project->description;
867 if ($form->{"subtotal_$i"} && $subtotal_header && ($subtotal_header != $i)) {
868 $subtotal_header = 0;
871 my $lineweight = $form->{"qty_$i"} * $form->{"weight_$i"};
872 $totalweight += $lineweight;
873 push @{ $form->{TEMPLATE_ARRAYS}->{weight} }, $form->format_amount($myconfig, $form->{"weight_$i"}, 3);
874 push @{ $form->{TEMPLATE_ARRAYS}->{weight_nofmt} }, $form->{"weight_$i"};
875 push @{ $form->{TEMPLATE_ARRAYS}->{lineweight} }, $form->format_amount($myconfig, $lineweight, 3);
876 push @{ $form->{TEMPLATE_ARRAYS}->{lineweight_nofmt} }, $lineweight;
878 if ($form->{"assembly_$i"}) {
881 # get parts and push them onto the stack
883 if ($form->{groupitems}) {
885 qq|ORDER BY pg.partsgroup, a.oid|;
887 $sortorder = qq|ORDER BY a.oid|;
890 do_statement($form, $h_pg, $q_pg, conv_i($form->{"id_$i"}));
892 while (my $ref = $h_pg->fetchrow_hashref("NAME_lc")) {
893 if ($form->{groupitems} && $ref->{partsgroup} ne $sameitem) {
894 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
895 $sameitem = ($ref->{partsgroup}) ? $ref->{partsgroup} : "--";
896 push(@{ $form->{TEMPLATE_ARRAYS}->{description} }, $sameitem);
898 push(@{ $form->{TEMPLATE_ARRAYS}->{"description"} }, $form->format_amount($myconfig, $ref->{qty} * $form->{"qty_$i"}) . qq| -- $ref->{partnumber}, $ref->{description}|);
900 map({ push(@{ $form->{TEMPLATE_ARRAYS}->{$_} }, "") } grep({ $_ ne "description" } @arrays));
904 if ($form->{"inventory_accno_$i"} && !$form->{"assembly_$i"}) {
905 my $stock_info = DO->unpack_stock_information('packed' => $form->{"stock_${in_out}_$i"});
907 foreach my $si (@{ $stock_info }) {
910 do_statement($form, $h_bin_wh, $q_bin_wh, conv_i($si->{bin_id}), conv_i($si->{warehouse_id}));
911 my $bin_wh = $h_bin_wh->fetchrow_hashref();
913 push @{ $form->{TEMPLATE_ARRAYS}{si_runningnumber}[$position-1] }, $num_si;
914 push @{ $form->{TEMPLATE_ARRAYS}{si_number}[$position-1] }, $form->{"partnumber_$i"};
915 push @{ $form->{TEMPLATE_ARRAYS}{si_description}[$position-1] }, $form->{"description_$i"};
916 push @{ $form->{TEMPLATE_ARRAYS}{si_warehouse}[$position-1] }, $bin_wh->{warehouse};
917 push @{ $form->{TEMPLATE_ARRAYS}{si_bin}[$position-1] }, $bin_wh->{bin};
918 push @{ $form->{TEMPLATE_ARRAYS}{si_chargenumber}[$position-1] }, $si->{chargenumber};
919 push @{ $form->{TEMPLATE_ARRAYS}{si_bestbefore}[$position-1] }, $si->{bestbefore};
920 push @{ $form->{TEMPLATE_ARRAYS}{si_qty}[$position-1] }, $form->format_amount($myconfig, $si->{qty} * 1);
921 push @{ $form->{TEMPLATE_ARRAYS}{si_qty_nofmt}[$position-1] }, $si->{qty} * 1;
922 push @{ $form->{TEMPLATE_ARRAYS}{si_unit}[$position-1] }, $si->{unit};
926 push @{ $form->{TEMPLATE_ARRAYS}->{"ic_cvar_$_->{name}"} },
927 CVar->format_to_template(CVar->parse($form->{"ic_cvar_$_->{name}_$i"}, $_), $_)
928 for @{ $ic_cvar_configs };
930 push @{ $form->{TEMPLATE_ARRAYS}->{"project_cvar_" . $_->config->name} }, $_->value_as_text for @{ $project->cvars_by_config };
933 $form->{totalweight} = $form->format_amount($myconfig, $totalweight, 3);
934 $form->{totalweight_nofmt} = $totalweight;
935 my $defaults = AM->get_defaults();
936 $form->{weightunit} = $defaults->{weightunit};
941 $form->{delivery_term} = SL::DB::Manager::DeliveryTerm->find_by(id => $form->{delivery_term_id} || undef);
942 $form->{delivery_term}->description_long($form->{delivery_term}->translated_attribute('description_long', $form->{language_id})) if $form->{delivery_term} && $form->{language_id};
944 $form->{username} = $myconfig->{name};
946 $main::lxdebug->leave_sub();
949 sub project_description {
950 $main::lxdebug->enter_sub();
952 my ($self, $dbh, $id) = @_;
954 my $form = $main::form;
956 my $query = qq|SELECT description FROM project WHERE id = ?|;
957 my ($value) = selectrow_query($form, $dbh, $query, $id);
959 $main::lxdebug->leave_sub();
964 sub unpack_stock_information {
965 $main::lxdebug->enter_sub();
970 Common::check_params_x(\%params, qw(packed));
974 eval { $unpacked = $params{packed} ? YAML::Load($params{packed}) : []; };
976 $unpacked = [] if (!$unpacked || ('ARRAY' ne ref $unpacked));
978 foreach my $entry (@{ $unpacked }) {
979 next if ('HASH' eq ref $entry);
984 $main::lxdebug->leave_sub();
989 sub get_item_availability {
990 $::lxdebug->enter_sub;
995 Common::check_params(\%params, qw(parts_id));
997 my @parts_ids = 'ARRAY' eq ref $params{parts_id} ? @{ $params{parts_id} } : ($params{parts_id});
1000 qq|SELECT i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, SUM(qty) AS qty, i.parts_id,
1001 w.description AS warehousedescription,
1002 b.description AS bindescription
1004 LEFT JOIN warehouse w ON (i.warehouse_id = w.id)
1005 LEFT JOIN bin b ON (i.bin_id = b.id)
1006 WHERE (i.parts_id IN (| . join(', ', ('?') x scalar(@parts_ids)) . qq|))
1007 GROUP BY i.warehouse_id, i.bin_id, i.chargenumber, i.bestbefore, i.parts_id, w.description, b.description
1009 ORDER BY LOWER(w.description), LOWER(b.description), LOWER(i.chargenumber), i.bestbefore
1011 my $contents = selectall_hashref_query($::form, $::form->get_standard_dbh, $query, @parts_ids);
1013 $::lxdebug->leave_sub;
1015 return @{ $contents };
1019 sub check_stock_availability {
1020 $main::lxdebug->enter_sub();
1025 Common::check_params(\%params, qw(requests parts_id));
1027 my $myconfig = \%main::myconfig;
1028 my $form = $main::form;
1030 my $dbh = $form->get_standard_dbh($myconfig);
1032 my $units = AM->retrieve_units($myconfig, $form);
1034 my ($partunit) = selectrow_query($form, $dbh, qq|SELECT unit FROM parts WHERE id = ?|, conv_i($params{parts_id}));
1035 my $unit_factor = $units->{$partunit}->{factor} || 1;
1037 my @contents = $self->get_item_availability(%params);
1041 foreach my $sinfo (@{ $params{requests} }) {
1044 foreach my $row (@contents) {
1045 next if (($row->{bin_id} != $sinfo->{bin_id}) ||
1046 ($row->{warehouse_id} != $sinfo->{warehouse_id}) ||
1047 ($row->{chargenumber} ne $sinfo->{chargenumber}) ||
1048 ($row->{bestbefore} ne $sinfo->{bestbefore}));
1052 my $base_qty = $sinfo->{qty} * $units->{$sinfo->{unit}}->{factor} / $unit_factor;
1054 if ($base_qty > $row->{qty}) {
1055 $sinfo->{error} = 1;
1056 push @errors, $sinfo;
1062 push @errors, $sinfo if (!$found);
1065 $main::lxdebug->leave_sub();
1070 sub transfer_in_out {
1071 $main::lxdebug->enter_sub();
1076 Common::check_params(\%params, qw(direction requests));
1078 if (!@{ $params{requests} }) {
1079 $main::lxdebug->leave_sub();
1083 my $myconfig = \%main::myconfig;
1084 my $form = $main::form;
1086 my $prefix = $params{direction} eq 'in' ? 'dst' : 'src';
1090 foreach my $request (@{ $params{requests} }) {
1092 'parts_id' => $request->{parts_id},
1093 "${prefix}_warehouse_id" => $request->{warehouse_id},
1094 "${prefix}_bin_id" => $request->{bin_id},
1095 'chargenumber' => $request->{chargenumber},
1096 'bestbefore' => $request->{bestbefore},
1097 'qty' => $request->{qty},
1098 'unit' => $request->{unit},
1099 'oe_id' => $form->{id},
1100 'shippingdate' => 'current_date',
1101 'transfer_type' => $params{direction} eq 'in' ? 'stock' : 'shipped',
1102 'project_id' => $request->{project_id},
1106 WH->transfer(@transfers);
1108 $main::lxdebug->leave_sub();
1111 sub get_shipped_qty {
1112 $main::lxdebug->enter_sub();
1117 Common::check_params(\%params, qw(type oe_id));
1119 my $myconfig = \%main::myconfig;
1120 my $form = $main::form;
1122 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1124 my @links = RecordLinks->get_links('dbh' => $dbh,
1125 'from_table' => 'oe',
1126 'from_id' => $params{oe_id},
1127 'to_table' => 'delivery_orders');
1128 my @values = map { $_->{to_id} } @links;
1130 if (!scalar @values) {
1131 $main::lxdebug->leave_sub();
1136 qq|SELECT doi.parts_id, doi.qty, doi.unit, p.unit AS partunit
1137 FROM delivery_order_items doi
1138 LEFT JOIN delivery_orders o ON (doi.delivery_order_id = o.id)
1139 LEFT JOIN parts p ON (doi.parts_id = p.id)
1140 WHERE o.id IN (| . join(', ', ('?') x scalar @values) . qq|)|;
1143 my $entries = selectall_hashref_query($form, $dbh, $query, @values);
1144 my $all_units = AM->retrieve_all_units();
1146 foreach my $entry (@{ $entries }) {
1147 $entry->{qty} *= AM->convert_unit($entry->{unit}, $entry->{partunit}, $all_units);
1149 if (!$ship{$entry->{parts_id}}) {
1150 $ship{$entry->{parts_id}} = $entry;
1152 $ship{$entry->{parts_id}}->{qty} += $entry->{qty};
1156 $main::lxdebug->leave_sub();
1161 sub is_marked_as_delivered {
1162 $main::lxdebug->enter_sub();
1167 Common::check_params(\%params, qw(id));
1169 my $myconfig = \%main::myconfig;
1170 my $form = $main::form;
1172 my $dbh = $params{dbh} || $form->get_standard_dbh($myconfig);
1174 my ($delivered) = selectfirst_array_query($form, $dbh, qq|SELECT delivered FROM delivery_orders WHERE id = ?|, conv_i($params{id}));
1176 $main::lxdebug->leave_sub();
1178 return $delivered ? 1 : 0;