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) 1998-2003
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
32 #======================================================================
34 use POSIX qw(strftime);
42 use SL::ReportGenerator;
43 use List::Util qw(max reduce sum);
46 require "bin/mozilla/io.pl";
47 require "bin/mozilla/arap.pl";
48 require "bin/mozilla/reportgenerator.pl";
55 # $locale->text('Edit the purchase_order');
56 # $locale->text('Edit the sales_order');
57 # $locale->text('Edit the request_quotation');
58 # $locale->text('Edit the sales_quotation');
60 # $locale->text('Workflow purchase_order');
61 # $locale->text('Workflow sales_order');
62 # $locale->text('Workflow request_quotation');
63 # $locale->text('Workflow sales_quotation');
66 'sales_order' => 'sales_order_edit',
67 'purchase_order' => 'purchase_order_edit',
68 'request_quotation' => 'request_quotation_edit',
69 'sales_quotation' => 'sales_quotation_edit',
73 my $right = $oe_access_map->{$form->{type}};
74 $right ||= 'DOES_NOT_EXIST';
76 $auth->assert($right);
80 $lxdebug->enter_sub();
86 if ($form->{type} eq 'purchase_order') {
87 $form->{title} = $action eq "edit" ?
88 $locale->text('Edit Purchase Order') :
89 $locale->text('Add Purchase Order');
90 $form->{heading} = $locale->text('Purchase Order');
91 $form->{vc} = 'vendor';
93 if ($form->{type} eq 'sales_order') {
94 $form->{title} = $action eq "edit" ?
95 $locale->text('Edit Sales Order') :
96 $locale->text('Add Sales Order');
97 $form->{heading} = $locale->text('Sales Order');
98 $form->{vc} = 'customer';
100 if ($form->{type} eq 'request_quotation') {
101 $form->{title} = $action eq "edit" ?
102 $locale->text('Edit Request for Quotation') :
103 $locale->text('Add Request for Quotation');
104 $form->{heading} = $locale->text('Request for Quotation');
105 $form->{vc} = 'vendor';
107 if ($form->{type} eq 'sales_quotation') {
108 $form->{title} = $action eq "edit" ?
109 $locale->text('Edit Quotation') :
110 $locale->text('Add Quotation');
111 $form->{heading} = $locale->text('Quotation');
112 $form->{vc} = 'customer';
115 $lxdebug->leave_sub();
119 $lxdebug->enter_sub();
126 "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
127 unless $form->{callback};
133 $lxdebug->leave_sub();
137 $lxdebug->enter_sub();
141 # show history button
142 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
143 #/show hhistory button
145 $form->{simple_save} = 0;
147 set_headings("edit");
149 # editing without stuff to edit? try adding it first
150 if ($form->{rowcount} && !$form->{print_and_save}) {
151 map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
155 undef $form->{rowcount};
157 $lxdebug->leave_sub();
160 } elsif (!$form->{id}) {
162 $lxdebug->leave_sub();
166 if ($form->{print_and_save}) {
167 $form->{action} = "print";
168 $form->{resubmit} = 1;
169 $language_id = $form->{language_id};
170 $printer_id = $form->{printer_id};
173 set_headings("edit");
177 $form->{rowcount} = 0;
178 foreach $ref (@{ $form->{form_details} }) {
180 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{$ref};
185 if ($form->{print_and_save}) {
186 $form->{language_id} = $language_id;
187 $form->{printer_id} = $printer_id;
192 $lxdebug->leave_sub();
196 $lxdebug->enter_sub();
200 # get customer/vendor
201 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
203 # retrieve order/quotation
204 $form->{webdav} = $webdav;
205 $form->{jsscript} = 1;
207 my $editing = $form->{id};
209 OE->retrieve(\%myconfig, \%$form);
211 # if multiple rowcounts (== collective order) then check if the
212 # there were more than one customer (in that case OE::retrieve removes
213 # the content from the field)
214 $form->error($locale->text('Collective Orders only work for orders from one customer!'))
215 if $form->{rowcount} && $form->{type} eq 'sales_order'
216 && defined $form->{customer} && $form->{customer} eq '';
218 $form->{"$form->{vc}_id"} ||= $form->{"all_$form->{vc}"}->[0]->{id} if $form->{"all_$form->{vc}"};
220 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
221 $form->{shipto} = 1 if $form->{id};
223 # get customer / vendor
224 IR->get_vendor(\%myconfig, \%$form) if $form->{type} =~ /(purchase_order|request_quotation)/;
225 IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
227 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
228 $form->restore_vars(qw(taxincluded)) if $form->{id};
229 $form->restore_vars(qw(salesman_id)) if $editing;
230 $form->{forex} = $form->{exchangerate};
231 $form->{employee} = "$form->{employee}--$form->{employee_id}";
233 # build vendor/customer drop down comatibility... don't ask
234 if (@{ $form->{"all_$form->{vc}"} }) {
235 $form->{"select$form->{vc}"} = 1;
236 $form->{$form->{vc}} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
239 $form->{"old$form->{vc}"} = $form->{$form->{vc}};
241 if ($form->{"old$form->{vc}"} !~ m/--\d+$/ && $form->{"$form->{vc}_id"}) {
242 $form->{"old$form->{vc}"} .= qq|--$form->{"$form->{vc}_id"}|
245 $lxdebug->leave_sub();
249 $lxdebug->enter_sub();
253 $form->{formname} ||= $form->{type};
255 # format discounts if values come from db. either as single id, or as a collective order
256 my $format_discounts = $form->{id} || $form->{convert_from_oe_ids};
258 for my $i (1 .. $form->{rowcount}) {
259 $form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
260 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($format_discounts ? 100 : 1));
261 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
262 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
265 $lxdebug->leave_sub();
269 $lxdebug->enter_sub();
274 # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
277 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
279 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
280 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
282 # use JavaScript Calendar or not
283 $form->{jsscript} = 1;
285 # openclosed checkboxes
287 push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
288 $form->{"delivered"} ? "checked" : "", $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
289 push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
290 $form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
291 $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
294 my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
296 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
297 $form->get_lists("contacts" => "ALL_CONTACTS",
298 "shipto" => "ALL_SHIPTO",
299 "projects" => { "key" => "ALL_PROJECTS",
301 "old_id" => \@old_project_ids },
302 "employees" => "ALL_EMPLOYEES",
303 "salesmen" => "ALL_SALESMEN",
304 "taxzones" => "ALL_TAXZONES",
305 "payments" => "ALL_PAYMENTS",
306 "currencies" => "ALL_CURRENCIES",
307 "departments" => "ALL_DEPARTMENTS",
308 $vc => { key => "ALL_" . uc($vc),
309 limit => $myconfig{vclimit} + 1 },
310 "price_factors" => "ALL_PRICE_FACTORS");
313 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
314 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
315 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
316 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
319 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
320 $TMPL_VAR{vclimit} = $myconfig{vclimit};
321 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
322 push @custom_hiddens, "$form->{vc}_id";
323 push @custom_hiddens, "old$form->{vc}";
324 push @custom_hiddens, "select$form->{vc}";
326 # currencies and exchangerate
327 @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
328 %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
329 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
330 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
331 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
332 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
333 push @custom_hiddens, "forex";
334 push @custom_hiddens, "exchangerate" if $form->{forex};
337 $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
338 $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
341 $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
343 push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
345 $credittext = $locale->text('Credit Limit exceeded!!!');
347 my $follow_up_vc = $form->{ $form->{vc} eq 'customer' ? 'customer' : 'vendor' };
348 $follow_up_vc =~ s/--\d*\s*$//;
349 $TMPL_VAR{follow_up_trans_info} = ($form->{type} =~ /_quotation$/ ? $form->{quonumber} : $form->{ordnumber}) . " ($follow_up_vc)";
352 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
354 if (scalar @{ $follow_ups }) {
355 $TMPL_VAR{num_follow_ups} = scalar @{ $follow_ups };
356 $TMPL_VAR{num_due_follow_ups} = sum map { $_->{due} * 1 } @{ $follow_ups };
360 $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
361 : ($form->{resubmit}) ? "document.oe.submit()"
362 : ($creditwarning) ? "alert('$credittext')"
365 $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
366 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
367 $TMPL_VAR{onload} = $onload;
369 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
370 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
371 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
375 $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
376 qw(id action type vc formname media format proforma queued printed emailed
377 title creditlimit creditremaining tradediscount business
378 max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
379 shiptocity shiptocountry shiptocontact shiptophone shiptofax
380 shiptodepartment_1 shiptodepartment_2 shiptoemail
381 message email subject cc bcc taxpart taxservice taxaccounts cursor_fokus),
383 map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ]; # deleted: discount
387 is_sales => scalar ($form->{type} =~ /^sales_/), # these vars are exported, so that the template
388 is_order => scalar ($form->{type} =~ /_order$/), # may determine what to show
389 is_sales_quo => scalar ($form->{type} =~ /sales_quotation$/),
390 is_req_quo => scalar ($form->{type} =~ /request_quotation$/),
391 is_sales_ord => scalar ($form->{type} =~ /sales_order$/),
392 is_pur_ord => scalar ($form->{type} =~ /purchase_order$/),
395 print $form->parse_html_template("oe/form_header", { %TMPL_VAR });
397 $lxdebug->leave_sub();
401 $lxdebug->enter_sub();
405 $form->{invtotal} = $form->{invsubtotal};
407 $rows = max 2, $form->numtextrows($form->{notes}, 25, 8);
408 $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
409 $rows = max $rows, $introws;
411 $TMPL_VAR{notes} = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>| . H($form->{notes}) . qq|</textarea>|;
412 $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$introws cols=35 wrap=soft>| . H($form->{intnotes}) . qq|</textarea>|;
414 if (!$form->{taxincluded}) {
416 foreach $item (split / /, $form->{taxaccounts}) {
417 if ($form->{"${item}_base"}) {
418 $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
419 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
421 $TMPL_VAR{tax} .= qq|
423 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
424 <td align=right>$form->{"${item}_total"}</td>
429 # $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0); # template does this
432 foreach $item (split / /, $form->{taxaccounts}) {
433 if ($form->{"${item}_base"}) {
434 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
435 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
436 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
437 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
439 $TMPL_VAR{tax} .= qq|
441 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
442 <td align=right>$form->{"${item}_total"}</td>
445 <th align=right>Nettobetrag</th>
446 <td align=right>$form->{"${item}_netto"}</td>
452 $form->{oldinvtotal} = $form->{invtotal};
454 print $form->parse_html_template("oe/form_footer", {
457 print_options => print_options(inline => 1),
458 label_edit => $locale->text("Edit the $form->{type}"),
459 label_workflow => $locale->text("Workflow $form->{type}"),
462 $lxdebug->leave_sub();
466 $lxdebug->enter_sub();
468 my ($recursive_call) = shift;
472 # $main::lxdebug->message(0, Dumper($form));
474 set_headings($form->{"id"} ? "edit" : "add");
476 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
479 $payment_id = $form->{payment_id} if $form->{payment_id};
481 &check_name($form->{vc});
483 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
486 $buysell = 'sell' if ($form->{vc} eq 'vendor');
487 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
488 $form->{exchangerate} = $form->{forex} if $form->{forex};
490 $exchangerate = $form->{exchangerate} || 1;
492 ##################### process items ######################################
494 $i = $form->{rowcount};
495 if ( ($form->{"partnumber_$i"} eq "")
496 && ($form->{"description_$i"} eq "")
497 && ($form->{"partsgroup_$i"} eq "")) {
499 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
504 if ($form->{type} =~ /^sales/) {
505 IS->retrieve_item(\%myconfig, \%$form);
507 IR->retrieve_item(\%myconfig, \%$form);
510 my $rows = scalar @{ $form->{item_list} };
512 # hier ist das problem fuer bug 817 $form->{discount} wird nicht durchgeschliffen
513 # ferner fallunterscheidung fuer verkauf oder einkauf s.a. bug 736 jb 04.05.2009
514 # select discount as vendor_discount from vendor ||
515 # select discount as customer_discount from customer
516 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"$form->{vc}_discount"} * 100);
519 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
528 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
529 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
530 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
531 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
532 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
534 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
536 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
537 $decimalplaces = max 2, length $1;
540 $form->{"sellprice_$i"} = $sellprice;
542 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
543 $form->{"sellprice_$i"} /= $exchangerate; # if there is an exchange rate adjust sellprice
546 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
547 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
548 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
549 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
551 $form->{creditremaining} -= $amount;
553 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
554 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
556 # get pricegroups for parts
557 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
559 # build up html code for prices_$i
566 # ok, so this is a new part
567 # ask if it is a part or service item
569 if ( $form->{"partsgroup_$i"}
570 && ($form->{"partsnumber_$i"} eq "")
571 && ($form->{"description_$i"} eq "")) {
573 $form->{"discount_$i"} = "";
577 $form->{"id_$i"} = 0;
582 ##################### process items ######################################
585 $lxdebug->leave_sub();
589 $lxdebug->enter_sub();
593 if ($form->{type} eq 'purchase_order') {
594 $form->{vc} = 'vendor';
595 $form->{ordnrname} = 'ordnumber';
596 $form->{title} = $locale->text('Purchase Orders');
597 $form->{ordlabel} = $locale->text('Order Number');
599 } elsif ($form->{type} eq 'request_quotation') {
600 $form->{vc} = 'vendor';
601 $form->{ordnrname} = 'quonumber';
602 $form->{title} = $locale->text('Request for Quotations');
603 $form->{ordlabel} = $locale->text('RFQ Number');
605 } elsif ($form->{type} eq 'sales_order') {
606 $form->{vc} = 'customer';
607 $form->{ordnrname} = 'ordnumber';
608 $form->{title} = $locale->text('Sales Orders');
609 $form->{ordlabel} = $locale->text('Order Number');
611 } elsif ($form->{type} eq 'sales_quotation') {
612 $form->{vc} = 'customer';
613 $form->{ordnrname} = 'quonumber';
614 $form->{title} = $locale->text('Quotations');
615 $form->{ordlabel} = $locale->text('Quotation Number');
618 $form->show_generic_error($locale->text('oe.pl::search called with unknown type'), back_button => 1);
621 # setup vendor / customer data
622 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
623 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
624 "employees" => "ALL_EMPLOYEES",
625 "salesmen" => "ALL_SALESMEN",
626 "departments" => "ALL_DEPARTMENTS",
627 "$form->{vc}s" => "ALL_VC");
629 # constants and subs for template
630 $form->{jsscript} = 1;
631 $form->{employee_labels} = sub { $_[0]->{"name"} || $_[0]->{"login"} };
632 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
633 $form->{salesman_labels} = $form->{employee_labels};
637 print $form->parse_html_template('oe/search', { %myconfig });
639 $lxdebug->leave_sub();
642 sub create_subtotal_row {
643 $lxdebug->enter_sub();
645 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
647 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
649 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
651 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
653 map { $totals->{$_} = 0 } @{ $subtotal_columns };
655 $lxdebug->leave_sub();
661 $lxdebug->enter_sub();
665 $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
667 ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
669 report_generator_set_default_sort('transdate', 1);
671 OE->transactions(\%myconfig, \%$form);
673 $form->{rowcount} = scalar @{ $form->{OE} };
676 "transdate", "reqdate",
682 "shipvia", "globalprojectnumber",
683 "transaction_description", "open",
684 "delivered", "marge_total", "marge_percent"
687 # only show checkboxes if gotten here via sales_order form.
688 my $allow_multiple_orders = $form->{type} eq 'sales_order';
689 if ($allow_multiple_orders) {
690 unshift @columns, "ids";
693 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
694 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
696 my $attachment_basename;
697 if ($form->{vc} eq 'vendor') {
698 if ($form->{type} eq 'purchase_order') {
699 $form->{title} = $locale->text('Purchase Orders');
700 $attachment_basename = $locale->text('purchase_order_list');
702 $form->{title} = $locale->text('Request for Quotations');
703 $attachment_basename = $locale->text('rfq_list');
707 if ($form->{type} eq 'sales_order') {
708 $form->{title} = $locale->text('Sales Orders');
709 $attachment_basename = $locale->text('sales_order_list');
711 $form->{title} = $locale->text('Quotations');
712 $attachment_basename = $locale->text('quotation_list');
716 my $report = SL::ReportGenerator->new(\%myconfig, $form);
718 my @hidden_variables = map { "l_${_}" } @columns;
719 push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
720 transaction_description transdatefrom transdateto type vc employee_id salesman_id
721 reqdatefrom reqdateto);
723 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
726 'ids' => { 'text' => '', },
727 'transdate' => { 'text' => $locale->text('Date'), },
728 'reqdate' => { 'text' => $locale->text('Required by'), },
729 'id' => { 'text' => $locale->text('ID'), },
730 'ordnumber' => { 'text' => $locale->text('Order'), },
731 'quonumber' => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
732 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
733 'netamount' => { 'text' => $locale->text('Amount'), },
734 'tax' => { 'text' => $locale->text('Tax'), },
735 'amount' => { 'text' => $locale->text('Total'), },
736 'curr' => { 'text' => $locale->text('Curr'), },
737 'employee' => { 'text' => $locale->text('Employee'), },
738 'salesman' => { 'text' => $locale->text('Salesman'), },
739 'shipvia' => { 'text' => $locale->text('Ship via'), },
740 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
741 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
742 'open' => { 'text' => $locale->text('Open'), },
743 'delivered' => { 'text' => $locale->text('Delivered'), },
744 'marge_total' => { 'text' => $locale->text('Ertrag'), },
745 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), }
748 foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia transaction_description)) {
749 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
750 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
753 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
755 $form->{"l_type"} = "Y";
756 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
757 $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
759 $report->set_columns(%column_defs);
760 $report->set_column_order(@columns);
761 $report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
762 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
765 my ($department) = split m/--/, $form->{department};
767 push @options, $locale->text('Customer') . " : $form->{customer}" if $form->{customer};
768 push @options, $locale->text('Vendor') . " : $form->{vendor}" if $form->{vendor};
769 push @options, $locale->text('Department') . " : $department" if $form->{department};
770 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if $form->{ordnumber};
771 push @options, $locale->text('Notes') . " : $form->{notes}" if $form->{notes};
772 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if $form->{transaction_description};
773 if ( $form->{transdatefrom} or $form->{transdateto} ) {
774 push @options, $locale->text('Order Date');
775 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if $form->{transdatefrom};
776 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if $form->{transdateto};
778 if ( $form->{reqdatefrom} or $form->{reqdateto} ) {
779 push @options, $locale->text('Delivery Date');
780 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{reqdatefrom}, 1) if $form->{reqdatefrom};
781 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{reqdateto}, 1) if $form->{reqdateto};
783 push @options, $locale->text('Open') if $form->{open};
784 push @options, $locale->text('Closed') if $form->{closed};
785 push @options, $locale->text('Delivered') if $form->{delivered};
786 push @options, $locale->text('Not delivered') if $form->{notdelivered};
788 $report->set_options('top_info_text' => join("\n", @options),
789 'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
790 'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
791 'output_format' => 'HTML',
792 'title' => $form->{title},
793 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
795 $report->set_options_from_form();
797 # add sort and escape callback, this one we use for the add sub
798 $form->{callback} = $href .= "&sort=$form->{sort}";
800 # escape callback for href
801 $callback = $form->escape($href);
803 my @subtotal_columns = qw(netamount amount marge_total marge_percent);
805 my %totals = map { $_ => 0 } @subtotal_columns;
806 my %subtotals = map { $_ => 0 } @subtotal_columns;
810 my $edit_url = build_std_url('action=edit', 'type', 'vc');
812 foreach $oe (@{ $form->{OE} }) {
813 map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
815 $oe->{tax} = $oe->{amount} - $oe->{netamount};
816 $oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
817 $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
819 map { $subtotals{$_} += $oe->{$_};
820 $totals{$_} += $oe->{$_} } @subtotal_columns;
822 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
823 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
825 map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
829 foreach my $column (@columns) {
830 next if ($column eq 'ids');
832 'data' => $oe->{$column},
833 'align' => $column_alignment{$column},
838 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
839 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
840 'valign' => 'center',
844 $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
846 my $row_set = [ $row ];
848 if (($form->{l_subtotal} eq 'Y')
849 && (($idx == (scalar @{ $form->{OE} } - 1))
850 || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
851 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
854 $report->add_data($row_set);
859 $report->add_separator();
860 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
862 $report->generate_with_headers();
864 $lxdebug->leave_sub();
867 sub check_delivered_flag {
868 $lxdebug->enter_sub();
872 if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
873 return $lxdebug->leave_sub();
876 my $all_delivered = 0;
878 foreach my $i (1 .. $form->{rowcount}) {
879 next if (!$form->{"id_$i"});
881 if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
890 $form->{delivered} = 1 if $all_delivered;
892 $lxdebug->leave_sub();
896 $lxdebug->enter_sub();
900 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
902 if ($form->{type} =~ /_order$/) {
903 $form->isblank("transdate", $locale->text('Order Date missing!'));
905 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
908 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
909 $form->{$idx} =~ s/^\s*//g;
910 $form->{$idx} =~ s/\s*$//g;
912 $msg = ucfirst $form->{vc};
913 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
915 # $locale->text('Customer missing!');
916 # $locale->text('Vendor missing!');
918 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
919 if ($form->{currency} ne $form->{defaultcurrency});
923 if($form->{payment_id}) {
924 $payment_id = $form->{payment_id};
927 # if the name changed get new values
928 if (&check_name($form->{vc})) {
929 if($form->{payment_id} eq "") {
930 $form->{payment_id} = $payment_id;
936 $form->{id} = 0 if $form->{saveasnew};
938 # this is for the internal notes section for the [email] Subject
939 if ($form->{type} =~ /_order$/) {
940 if ($form->{type} eq 'sales_order') {
941 $form->{label} = $locale->text('Sales Order');
943 $numberfld = "sonumber";
944 $ordnumber = "ordnumber";
946 $form->{label} = $locale->text('Purchase Order');
948 $numberfld = "ponumber";
949 $ordnumber = "ordnumber";
952 $err = $locale->text('Cannot save order!');
954 check_delivered_flag();
957 if ($form->{type} eq 'sales_quotation') {
958 $form->{label} = $locale->text('Quotation');
960 $numberfld = "sqnumber";
961 $ordnumber = "quonumber";
963 $form->{label} = $locale->text('Request for Quotation');
965 $numberfld = "rfqnumber";
966 $ordnumber = "quonumber";
969 $err = $locale->text('Cannot save quotation!');
973 # get new number in sequence if no number is given or if saveasnew was requested
974 if (!$form->{$ordnumber} || $form->{saveasnew}) {
975 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
980 $form->error($err) if (!OE->save(\%myconfig, \%$form));
983 if(!exists $form->{addition}) {
984 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
985 $form->{addition} = "SAVED";
986 $form->save_history($form->dbconnect(\%myconfig));
988 # /saving the history
990 $form->redirect($form->{label} . " $form->{$ordnumber} " .
991 $locale->text('saved!'));
993 $lxdebug->leave_sub();
997 $lxdebug->enter_sub();
1001 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1004 if ($form->{type} =~ /_order$/) {
1005 $form->isblank("transdate", $locale->text('Order Date missing!'));
1007 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1010 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1011 $form->{$idx} =~ s/^\s*//g;
1012 $form->{$idx} =~ s/\s*$//g;
1014 $msg = ucfirst $form->{vc};
1015 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1017 # $locale->text('Customer missing!');
1018 # $locale->text('Vendor missing!');
1020 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1021 if ($form->{currency} ne $form->{defaultcurrency});
1025 if($form->{payment_id}) {
1026 $payment_id = $form->{payment_id};
1029 # if the name changed get new values
1030 if (&check_name($form->{vc})) {
1031 if($form->{payment_id} eq "") {
1032 $form->{payment_id} = $payment_id;
1038 $form->{id} = 0 if $form->{saveasnew};
1040 # this is for the internal notes section for the [email] Subject
1041 if ($form->{type} =~ /_order$/) {
1042 if ($form->{type} eq 'sales_order') {
1043 $form->{label} = $locale->text('Sales Order');
1045 $numberfld = "sonumber";
1046 $ordnumber = "ordnumber";
1048 $form->{label} = $locale->text('Purchase Order');
1050 $numberfld = "ponumber";
1051 $ordnumber = "ordnumber";
1054 $err = $locale->text('Cannot save order!');
1056 check_delivered_flag();
1059 if ($form->{type} eq 'sales_quotation') {
1060 $form->{label} = $locale->text('Quotation');
1062 $numberfld = "sqnumber";
1063 $ordnumber = "quonumber";
1065 $form->{label} = $locale->text('Request for Quotation');
1067 $numberfld = "rfqnumber";
1068 $ordnumber = "quonumber";
1071 $err = $locale->text('Cannot save quotation!');
1075 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1076 unless $form->{$ordnumber};
1080 OE->save(\%myconfig, \%$form);
1082 # saving the history
1083 if(!exists $form->{addition}) {
1084 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1085 $form->{addition} = "SAVED";
1086 $form->save_history($form->dbconnect(\%myconfig));
1088 # /saving the history
1090 $form->{simple_save} = 1;
1091 if(!$form->{print_and_save}) {
1092 set_headings("edit");
1096 $lxdebug->leave_sub();
1100 $lxdebug->enter_sub();
1106 if ($form->{type} =~ /_order$/) {
1107 $msg = $locale->text('Are you sure you want to delete Order Number');
1108 $ordnumber = 'ordnumber';
1110 $msg = $locale->text('Are you sure you want to delete Quotation Number');
1111 $ordnumber = 'quonumber';
1117 <form method=post action=$form->{script}>
1120 # delete action variable
1121 map { delete $form->{$_} } qw(action header);
1123 foreach $key (keys %$form) {
1124 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1125 $form->{$key} =~ s/\"/"/g;
1126 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1130 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1132 <h4>$msg $form->{$ordnumber}</h4>
1134 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1135 <input name=action class=submit type=submit value="|
1136 . $locale->text('Yes') . qq|">
1137 <button class=submit type=button onclick="history.back()">|
1138 . $locale->text('No') . qq|</button>
1145 $lxdebug->leave_sub();
1148 sub delete_order_quotation {
1149 $lxdebug->enter_sub();
1153 if ($form->{type} =~ /_order$/) {
1154 $msg = $locale->text('Order deleted!');
1155 $err = $locale->text('Cannot delete order!');
1157 $msg = $locale->text('Quotation deleted!');
1158 $err = $locale->text('Cannot delete quotation!');
1160 if (OE->delete(\%myconfig, \%$form, $spool)){
1161 # saving the history
1162 if(!exists $form->{addition}) {
1163 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1164 $form->{addition} = "DELETED";
1165 $form->save_history($form->dbconnect(\%myconfig));
1167 # /saving the history
1173 $lxdebug->leave_sub();
1177 $lxdebug->enter_sub();
1180 $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
1182 $form->{old_employee_id} = $form->{employee_id};
1183 $form->{old_salesman_id} = $form->{salesman_id};
1185 if ($form->{type} =~ /_order$/) {
1187 # these checks only apply if the items don't bring their own ordnumbers/transdates.
1188 # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1189 $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1190 if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1191 $form->isblank("transdate", $locale->text('Order Date missing!'))
1192 if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1194 # also copy deliverydate from the order
1195 $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1196 $form->{orddate} = $form->{transdate};
1198 $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1199 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1200 $form->{ordnumber} = "";
1201 $form->{quodate} = $form->{transdate};
1204 $payment_id = $form->{payment_id} if $form->{payment_id};
1206 # if the name changed get new values
1207 if (&check_name($form->{vc})) {
1208 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1213 $form->{cp_id} *= 1;
1215 for $i (1 .. $form->{rowcount}) {
1216 for (qw(ship qty sellprice listprice basefactor)) {
1217 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1221 if ( $form->{type} =~ /_order/
1222 && $form->{currency} ne $form->{defaultcurrency}) {
1224 # check if we need a new exchangerate
1225 $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1227 $orddate = $form->current_date(\%myconfig);
1228 $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1230 if (!$exchangerate) {
1231 &backorder_exchangerate($orddate, $buysell);
1236 $form->{convert_from_oe_ids} = $form->{id};
1237 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1238 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1239 $form->{shipto} = 1;
1240 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1242 delete @{$form}{qw(id closed)};
1243 $form->{rowcount}--;
1245 if ($form->{type} =~ /_order$/) {
1246 $form->{exchangerate} = $exchangerate;
1250 if ( $form->{type} eq 'purchase_order'
1251 || $form->{type} eq 'request_quotation') {
1252 $form->{title} = $locale->text('Add Vendor Invoice');
1253 $form->{script} = 'ir.pl';
1258 if ( $form->{type} eq 'sales_order'
1259 || $form->{type} eq 'sales_quotation') {
1260 $form->{title} = $locale->text('Add Sales Invoice');
1261 $form->{script} = 'is.pl';
1266 # bo creates the id, reset it
1267 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1268 $form->{ $form->{vc} } =~ s/--.*//g;
1269 $form->{type} = "invoice";
1272 $locale = new Locale "$myconfig{countrycode}", "$script";
1274 require "bin/mozilla/$form->{script}";
1276 map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1278 $currency = $form->{currency};
1281 $form->{currency} = $currency;
1282 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1283 $form->{exchangerate} = $form->{forex} || '';
1285 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1290 for $i (1 .. $form->{rowcount}) {
1291 $form->{"discount_$i"} =
1292 $form->format_amount(\%myconfig, $form->{"discount_$i"});
1294 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1296 $decimalplaces = ($dec > 2) ? $dec : 2;
1298 # copy delivery date from reqdate for order -> invoice conversion
1299 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1300 unless $form->{"deliverydate_$i"};
1302 $form->{"sellprice_$i"} =
1303 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1306 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1307 $dec_qty = length $dec_qty;
1309 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1311 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1312 qw(partnumber description unit);
1318 $lxdebug->leave_sub();
1321 sub backorder_exchangerate {
1322 $lxdebug->enter_sub();
1326 my ($orddate, $buysell) = @_;
1333 <form method=post action=$form->{script}>
1336 # delete action variable
1337 map { delete $form->{$_} } qw(action header exchangerate);
1339 foreach $key (keys %$form) {
1340 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1341 $form->{$key} =~ s/\"/"/g;
1342 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1345 $form->{title} = $locale->text('Add Exchangerate');
1349 <input type=hidden name=exchangeratedate value=$orddate>
1350 <input type=hidden name=buysell value=$buysell>
1353 <tr><th class=listtop>$form->{title}</th></tr>
1354 <tr height="5"></tr>
1359 <th align=right>| . $locale->text('Currency') . qq|</th>
1360 <td>$form->{currency}</td>
1363 <th align=right>| . $locale->text('Date') . qq|</th>
1367 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1368 <td><input name=exchangerate size=11></td>
1378 <input type=hidden name=nextsub value=save_exchangerate>
1380 <input name=action class=submit type=submit value="|
1381 . $locale->text('Continue') . qq|">
1389 $lxdebug->leave_sub();
1392 sub save_exchangerate {
1393 $lxdebug->enter_sub();
1395 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1396 $form->{exchangerate} =
1397 $form->parse_amount(\%myconfig, $form->{exchangerate});
1398 $form->save_exchangerate(\%myconfig, $form->{currency},
1399 $form->{exchangeratedate},
1400 $form->{exchangerate}, $form->{buysell});
1404 $lxdebug->leave_sub();
1407 sub create_backorder {
1408 $lxdebug->enter_sub();
1410 $form->{shipped} = 1;
1412 # figure out if we need to create a backorder
1413 # items aren't saved if qty != 0
1415 for $i (1 .. $form->{rowcount}) {
1416 $totalqty += $qty = $form->{"qty_$i"};
1417 $totalship += $ship = $form->{"ship_$i"};
1419 $form->{"qty_$i"} = $qty - $ship;
1422 if ($totalship == 0) {
1423 map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1424 $form->{ordtotal} = 0;
1425 $form->{shipped} = 0;
1429 if ($totalqty == $totalship) {
1430 map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1431 $form->{ordtotal} = 0;
1436 qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1439 for $i (1 .. $form->{rowcount}) {
1441 $form->{"${_}_$i"} =
1442 $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1443 } qw(sellprice discount);
1448 OE->save(\%myconfig, \%$form);
1450 # rebuild rows for invoice
1454 for $i (1 .. $form->{rowcount}) {
1455 $form->{"qty_$i"} = $form->{"ship_$i"};
1457 if ($form->{"qty_$i"}) {
1460 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1465 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1466 $form->{rowcount} = $count;
1468 $lxdebug->leave_sub();
1472 $lxdebug->enter_sub();
1476 $form->{saveasnew} = 1;
1477 $form->{closed} = 0;
1478 map { delete $form->{$_} } qw(printed emailed queued);
1480 # Let Lx-Office assign a new order number if the user hasn't changed the
1481 # previous one. If it has been changed manually then use it as-is.
1482 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1483 $form->{$idx} =~ s/^\s*//g;
1484 $form->{$idx} =~ s/\s*$//g;
1485 if ($form->{saved_xyznumber} &&
1486 ($form->{saved_xyznumber} eq $form->{$idx})) {
1487 delete($form->{$idx});
1492 $lxdebug->leave_sub();
1495 sub check_for_direct_delivery_yes {
1496 $lxdebug->enter_sub();
1500 $form->{direct_delivery_checked} = 1;
1501 delete @{$form}{grep /^shipto/, keys %{ $form }};
1502 map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1503 $form->{shipto} = 1;
1505 $lxdebug->leave_sub();
1508 sub check_for_direct_delivery_no {
1509 $lxdebug->enter_sub();
1513 $form->{direct_delivery_checked} = 1;
1514 delete @{$form}{grep /^shipto/, keys %{ $form }};
1517 $lxdebug->leave_sub();
1520 sub check_for_direct_delivery {
1521 $lxdebug->enter_sub();
1525 if ($form->{direct_delivery_checked}
1526 || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1527 $lxdebug->leave_sub();
1531 if ($form->{shipto_id}) {
1532 Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1535 map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1538 delete $form->{action};
1539 $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
1542 print $form->parse_html_template("oe/check_for_direct_delivery");
1544 $lxdebug->leave_sub();
1549 sub purchase_order {
1550 $lxdebug->enter_sub();
1553 $auth->assert('purchase_order_edit');
1555 if ($form->{type} eq 'sales_order') {
1556 check_for_direct_delivery();
1559 if ($form->{type} =~ /^sales_/) {
1560 delete($form->{ordnumber});
1563 $form->{cp_id} *= 1;
1565 $form->{title} = $locale->text('Add Purchase Order');
1566 $form->{vc} = "vendor";
1567 $form->{type} = "purchase_order";
1571 $lxdebug->leave_sub();
1575 $lxdebug->enter_sub();
1578 $auth->assert('sales_order_edit');
1580 if ($form->{type} eq "purchase_order") {
1581 delete($form->{ordnumber});
1584 $form->{cp_id} *= 1;
1586 $form->{title} = $locale->text('Add Sales Order');
1587 $form->{vc} = "customer";
1588 $form->{type} = "sales_order";
1592 $lxdebug->leave_sub();
1596 $lxdebug->enter_sub();
1599 $auth->assert('purchase_order_edit | sales_order_edit');
1601 $form->{transdate} = $form->current_date(\%myconfig);
1602 delete $form->{duedate};
1604 $form->{convert_from_oe_ids} = $form->{id};
1605 $form->{closed} = 0;
1607 $form->{old_employee_id} = $form->{employee_id};
1608 $form->{old_salesman_id} = $form->{salesman_id};
1611 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered
1614 for $i (1 .. $form->{rowcount}) {
1615 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor discount);
1618 my %saved_vars = map { $_ => $form->{$_} } grep { $form->{$_} } qw(currency);
1622 map { $form->{$_} = $saved_vars{$_} } keys %saved_vars;
1626 # prepare_order assumes that the discount is in db-notation (0.05) and not user-notation (5)
1627 # and therefore multiplies the values by 100 in the case of reading from db or making an order from several quotation, so we convert this back into percent-notation for the user interface by multiplying with 0.01
1628 for $i (1 .. $form->{rowcount}) {
1629 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 0.01);
1633 for $i (1 .. $form->{rowcount} - 1) {
1634 map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
1639 $lxdebug->leave_sub();
1642 sub delivery_order {
1643 $lxdebug->enter_sub();
1645 if ($form->{type} =~ /^sales/) {
1646 $auth->assert('sales_delivery_order_edit');
1648 $form->{vc} = 'customer';
1649 $form->{type} = 'sales_delivery_order';
1652 $auth->assert('purchase_delivery_order_edit');
1654 $form->{vc} = 'vendor';
1655 $form->{type} = 'purchase_delivery_order';
1658 require "bin/mozilla/do.pl";
1660 $form->{script} = 'do.pl';
1661 $form->{cp_id} *= 1;
1662 $form->{convert_from_oe_ids} = $form->{id};
1663 $form->{transdate} = $form->current_date(\%myconfig);
1664 delete $form->{duedate};
1666 $form->{old_employee_id} = $form->{employee_id};
1667 $form->{old_salesman_id} = $form->{salesman_id};
1670 delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
1672 for $i (1 .. $form->{rowcount}) {
1673 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor discount);
1676 my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
1682 map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;
1686 $lxdebug->leave_sub();
1690 $lxdebug->enter_sub();
1694 $form->{print_and_save} = 1;
1698 my $saved_form = save_form();
1702 restore_form($saved_form, 0, qw(id ordnumber quonumber));
1706 $lxdebug->leave_sub();
1710 call_sub($form->{yes_nextsub});
1714 call_sub($form->{no_nextsub});
1717 ######################################################################################################
1719 # ###############################################################################################
1721 $lxdebug->enter_sub();
1725 retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
1727 $form->{"taxaccounts"} =~ s/\s*$//;
1728 $form->{"taxaccounts"} =~ s/^\s*//;
1729 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1730 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1732 $form->{"taxaccounts"} = "";
1734 for my $i (1 .. $form->{"rowcount"}) {
1735 IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
1738 $form->{rowcount}++;
1739 $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
1741 $form->language_payment(\%myconfig);
1743 Common::webdav_folder($form) if ($webdav);
1748 display_row($form->{rowcount}) if $form->{rowcount};
1752 $lxdebug->leave_sub();
1755 sub report_for_todo_list {
1756 $lxdebug->enter_sub();
1758 my $quotations = OE->transactions_for_todo_list();
1761 if (@{ $quotations }) {
1762 my $edit_url = build_std_url('script=oe.pl', 'action=edit');
1764 $content = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
1765 'edit_url' => $edit_url });
1768 $lxdebug->leave_sub();