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);
41 use SL::MoreCommon qw(ary_diff);
43 use SL::ReportGenerator;
44 use List::Util qw(max reduce sum);
47 require "bin/mozilla/io.pl";
48 require "bin/mozilla/arap.pl";
49 require "bin/mozilla/reportgenerator.pl";
56 # $locale->text('Edit the purchase_order');
57 # $locale->text('Edit the sales_order');
58 # $locale->text('Edit the request_quotation');
59 # $locale->text('Edit the sales_quotation');
61 # $locale->text('Workflow purchase_order');
62 # $locale->text('Workflow sales_order');
63 # $locale->text('Workflow request_quotation');
64 # $locale->text('Workflow sales_quotation');
67 'sales_order' => 'sales_order_edit',
68 'purchase_order' => 'purchase_order_edit',
69 'request_quotation' => 'request_quotation_edit',
70 'sales_quotation' => 'sales_quotation_edit',
74 my $right = $oe_access_map->{$form->{type}};
75 $right ||= 'DOES_NOT_EXIST';
77 $auth->assert($right);
81 $lxdebug->enter_sub();
87 if ($form->{type} eq 'purchase_order') {
88 $form->{title} = $action eq "edit" ?
89 $locale->text('Edit Purchase Order') :
90 $locale->text('Add Purchase Order');
91 $form->{heading} = $locale->text('Purchase Order');
92 $form->{vc} = 'vendor';
94 if ($form->{type} eq 'sales_order') {
95 $form->{title} = $action eq "edit" ?
96 $locale->text('Edit Sales Order') :
97 $locale->text('Add Sales Order');
98 $form->{heading} = $locale->text('Sales Order');
99 $form->{vc} = 'customer';
101 if ($form->{type} eq 'request_quotation') {
102 $form->{title} = $action eq "edit" ?
103 $locale->text('Edit Request for Quotation') :
104 $locale->text('Add Request for Quotation');
105 $form->{heading} = $locale->text('Request for Quotation');
106 $form->{vc} = 'vendor';
108 if ($form->{type} eq 'sales_quotation') {
109 $form->{title} = $action eq "edit" ?
110 $locale->text('Edit Quotation') :
111 $locale->text('Add Quotation');
112 $form->{heading} = $locale->text('Quotation');
113 $form->{vc} = 'customer';
116 $lxdebug->leave_sub();
120 $lxdebug->enter_sub();
127 "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
128 unless $form->{callback};
134 $lxdebug->leave_sub();
138 $lxdebug->enter_sub();
142 # show history button
143 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
144 #/show hhistory button
146 $form->{simple_save} = 0;
148 set_headings("edit");
150 # editing without stuff to edit? try adding it first
151 if ($form->{rowcount} && !$form->{print_and_save}) {
152 map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
156 undef $form->{rowcount};
158 $lxdebug->leave_sub();
161 } elsif (!$form->{id}) {
163 $lxdebug->leave_sub();
167 if ($form->{print_and_save}) {
168 $form->{action} = "print";
169 $form->{resubmit} = 1;
170 $language_id = $form->{language_id};
171 $printer_id = $form->{printer_id};
174 set_headings("edit");
178 $form->{rowcount} = 0;
179 foreach $ref (@{ $form->{form_details} }) {
181 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{$ref};
186 if ($form->{print_and_save}) {
187 $form->{language_id} = $language_id;
188 $form->{printer_id} = $printer_id;
193 $lxdebug->leave_sub();
197 $lxdebug->enter_sub();
201 # get customer/vendor
202 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
204 # retrieve order/quotation
205 $form->{webdav} = $webdav;
206 $form->{jsscript} = 1;
208 my $editing = $form->{id};
210 OE->retrieve(\%myconfig, \%$form);
212 # if multiple rowcounts (== collective order) then check if the
213 # there were more than one customer (in that case OE::retrieve removes
214 # the content from the field)
215 $form->error($locale->text('Collective Orders only work for orders from one customer!'))
216 if $form->{rowcount} && $form->{type} eq 'sales_order'
217 && defined $form->{customer} && $form->{customer} eq '';
219 $form->{"$form->{vc}_id"} ||= $form->{"all_$form->{vc}"}->[0]->{id} if $form->{"all_$form->{vc}"};
221 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
222 $form->{shipto} = 1 if $form->{id};
224 # get customer / vendor
225 IR->get_vendor(\%myconfig, \%$form) if $form->{type} =~ /(purchase_order|request_quotation)/;
226 IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
228 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
229 $form->restore_vars(qw(taxincluded)) if $form->{id};
230 $form->restore_vars(qw(salesman_id)) if $editing;
231 $form->{forex} = $form->{exchangerate};
232 $form->{employee} = "$form->{employee}--$form->{employee_id}";
234 # build vendor/customer drop down comatibility... don't ask
235 if (@{ $form->{"all_$form->{vc}"} }) {
236 $form->{"select$form->{vc}"} = 1;
237 $form->{$form->{vc}} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
240 $form->{"old$form->{vc}"} = $form->{$form->{vc}};
242 if ($form->{"old$form->{vc}"} !~ m/--\d+$/ && $form->{"$form->{vc}_id"}) {
243 $form->{"old$form->{vc}"} .= qq|--$form->{"$form->{vc}_id"}|
246 $lxdebug->leave_sub();
250 $lxdebug->enter_sub();
254 $form->{formname} ||= $form->{type};
256 # format discounts if values come from db. either as single id, or as a collective order
257 my $format_discounts = $form->{id} || $form->{convert_from_oe_ids};
259 for my $i (1 .. $form->{rowcount}) {
260 $form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
261 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($format_discounts ? 100 : 1));
262 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
263 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
266 $lxdebug->leave_sub();
270 $lxdebug->enter_sub();
275 # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
278 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
280 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
281 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
283 # use JavaScript Calendar or not
284 $form->{jsscript} = 1;
286 # openclosed checkboxes
288 push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
289 $form->{"delivered"} ? "checked" : "", $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
290 push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
291 $form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
292 $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
295 my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
297 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
298 $form->get_lists("contacts" => "ALL_CONTACTS",
299 "shipto" => "ALL_SHIPTO",
300 "projects" => { "key" => "ALL_PROJECTS",
302 "old_id" => \@old_project_ids },
303 "employees" => "ALL_EMPLOYEES",
304 "salesmen" => "ALL_SALESMEN",
305 "taxzones" => "ALL_TAXZONES",
306 "payments" => "ALL_PAYMENTS",
307 "currencies" => "ALL_CURRENCIES",
308 "departments" => "ALL_DEPARTMENTS",
309 $vc => { key => "ALL_" . uc($vc),
310 limit => $myconfig{vclimit} + 1 },
311 "price_factors" => "ALL_PRICE_FACTORS");
314 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
315 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
316 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
317 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
320 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
321 $TMPL_VAR{vclimit} = $myconfig{vclimit};
322 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
323 push @custom_hiddens, "$form->{vc}_id";
324 push @custom_hiddens, "old$form->{vc}";
325 push @custom_hiddens, "select$form->{vc}";
327 # currencies and exchangerate
328 @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
329 %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
330 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
331 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
332 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
333 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
334 push @custom_hiddens, "forex";
335 push @custom_hiddens, "exchangerate" if $form->{forex};
338 $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
339 $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
342 $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
344 push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
346 $credittext = $locale->text('Credit Limit exceeded!!!');
348 my $follow_up_vc = $form->{ $form->{vc} eq 'customer' ? 'customer' : 'vendor' };
349 $follow_up_vc =~ s/--\d*\s*$//;
350 $TMPL_VAR{follow_up_trans_info} = ($form->{type} =~ /_quotation$/ ? $form->{quonumber} : $form->{ordnumber}) . " ($follow_up_vc)";
353 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
355 if (scalar @{ $follow_ups }) {
356 $TMPL_VAR{num_follow_ups} = scalar @{ $follow_ups };
357 $TMPL_VAR{num_due_follow_ups} = sum map { $_->{due} * 1 } @{ $follow_ups };
361 $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
362 : ($form->{resubmit}) ? "document.oe.submit()"
363 : ($creditwarning) ? "alert('$credittext')"
366 $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
367 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
368 $TMPL_VAR{onload} = $onload;
370 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
371 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
372 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
376 $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
377 qw(id action type vc formname media format proforma queued printed emailed
378 title creditlimit creditremaining tradediscount business
379 max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
380 shiptocity shiptocountry shiptocontact shiptophone shiptofax
381 shiptodepartment_1 shiptodepartment_2 shiptoemail
382 message email subject cc bcc taxpart taxservice taxaccounts cursor_fokus),
384 map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ]; # deleted: discount
388 is_sales => scalar ($form->{type} =~ /^sales_/), # these vars are exported, so that the template
389 is_order => scalar ($form->{type} =~ /_order$/), # may determine what to show
390 is_sales_quo => scalar ($form->{type} =~ /sales_quotation$/),
391 is_req_quo => scalar ($form->{type} =~ /request_quotation$/),
392 is_sales_ord => scalar ($form->{type} =~ /sales_order$/),
393 is_pur_ord => scalar ($form->{type} =~ /purchase_order$/),
396 print $form->parse_html_template("oe/form_header", { %TMPL_VAR });
398 $lxdebug->leave_sub();
402 $lxdebug->enter_sub();
406 $form->{invtotal} = $form->{invsubtotal};
408 $rows = max 2, $form->numtextrows($form->{notes}, 25, 8);
409 $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
410 $rows = max $rows, $introws;
412 $TMPL_VAR{notes} = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>| . H($form->{notes}) . qq|</textarea>|;
413 $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$introws cols=35 wrap=soft>| . H($form->{intnotes}) . qq|</textarea>|;
415 if (!$form->{taxincluded}) {
417 foreach $item (split / /, $form->{taxaccounts}) {
418 if ($form->{"${item}_base"}) {
419 $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
420 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
422 $TMPL_VAR{tax} .= qq|
424 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
425 <td align=right>$form->{"${item}_total"}</td>
430 # $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0); # template does this
433 foreach $item (split / /, $form->{taxaccounts}) {
434 if ($form->{"${item}_base"}) {
435 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
436 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
437 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
438 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
440 $TMPL_VAR{tax} .= qq|
442 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
443 <td align=right>$form->{"${item}_total"}</td>
446 <th align=right>Nettobetrag</th>
447 <td align=right>$form->{"${item}_netto"}</td>
453 $form->{oldinvtotal} = $form->{invtotal};
455 print $form->parse_html_template("oe/form_footer", {
458 print_options => print_options(inline => 1),
459 label_edit => $locale->text("Edit the $form->{type}"),
460 label_workflow => $locale->text("Workflow $form->{type}"),
463 $lxdebug->leave_sub();
467 $lxdebug->enter_sub();
469 my ($recursive_call) = shift;
473 # $main::lxdebug->message(0, Dumper($form));
475 set_headings($form->{"id"} ? "edit" : "add");
477 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
480 $payment_id = $form->{payment_id} if $form->{payment_id};
482 &check_name($form->{vc});
484 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
487 $buysell = 'sell' if ($form->{vc} eq 'vendor');
488 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
489 $form->{exchangerate} = $form->{forex} if $form->{forex};
491 $exchangerate = $form->{exchangerate} || 1;
493 ##################### process items ######################################
495 $i = $form->{rowcount};
496 if ( ($form->{"partnumber_$i"} eq "")
497 && ($form->{"description_$i"} eq "")
498 && ($form->{"partsgroup_$i"} eq "")) {
500 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
505 if ($form->{type} =~ /^sales/) {
506 IS->retrieve_item(\%myconfig, \%$form);
508 IR->retrieve_item(\%myconfig, \%$form);
511 my $rows = scalar @{ $form->{item_list} };
513 # hier ist das problem fuer bug 817 $form->{discount} wird nicht durchgeschliffen
514 # ferner fallunterscheidung fuer verkauf oder einkauf s.a. bug 736 jb 04.05.2009
515 # select discount as vendor_discount from vendor ||
516 # select discount as customer_discount from customer
517 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"$form->{vc}_discount"} * 100);
520 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
529 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
530 # hier werden parts (Artikeleigenschaften) aus item_list (retrieve_item aus IS.pm)
531 # (item wahrscheinlich synonym für parts) entsprechend in die form geschrieben ...
532 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
533 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
535 # ... deswegen muss die prüfung, ob es sich um einen nicht rabattierfähigen artikel handelt später erfolgen (Bug 1136)
536 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
537 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
539 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
541 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
542 $decimalplaces = max 2, length $1;
545 $form->{"sellprice_$i"} = $sellprice;
547 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
548 $form->{"sellprice_$i"} /= $exchangerate; # if there is an exchange rate adjust sellprice
551 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
552 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
553 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
554 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
556 $form->{creditremaining} -= $amount;
558 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
559 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
561 # get pricegroups for parts
562 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
564 # build up html code for prices_$i
571 # ok, so this is a new part
572 # ask if it is a part or service item
574 if ( $form->{"partsgroup_$i"}
575 && ($form->{"partsnumber_$i"} eq "")
576 && ($form->{"description_$i"} eq "")) {
578 $form->{"discount_$i"} = "";
582 $form->{"id_$i"} = 0;
587 ##################### process items ######################################
590 $lxdebug->leave_sub();
594 $lxdebug->enter_sub();
598 if ($form->{type} eq 'purchase_order') {
599 $form->{vc} = 'vendor';
600 $form->{ordnrname} = 'ordnumber';
601 $form->{title} = $locale->text('Purchase Orders');
602 $form->{ordlabel} = $locale->text('Order Number');
604 } elsif ($form->{type} eq 'request_quotation') {
605 $form->{vc} = 'vendor';
606 $form->{ordnrname} = 'quonumber';
607 $form->{title} = $locale->text('Request for Quotations');
608 $form->{ordlabel} = $locale->text('RFQ Number');
610 } elsif ($form->{type} eq 'sales_order') {
611 $form->{vc} = 'customer';
612 $form->{ordnrname} = 'ordnumber';
613 $form->{title} = $locale->text('Sales Orders');
614 $form->{ordlabel} = $locale->text('Order Number');
616 } elsif ($form->{type} eq 'sales_quotation') {
617 $form->{vc} = 'customer';
618 $form->{ordnrname} = 'quonumber';
619 $form->{title} = $locale->text('Quotations');
620 $form->{ordlabel} = $locale->text('Quotation Number');
623 $form->show_generic_error($locale->text('oe.pl::search called with unknown type'), back_button => 1);
626 # setup vendor / customer data
627 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
628 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
629 "employees" => "ALL_EMPLOYEES",
630 "salesmen" => "ALL_SALESMEN",
631 "departments" => "ALL_DEPARTMENTS",
632 "$form->{vc}s" => "ALL_VC");
634 # constants and subs for template
635 $form->{jsscript} = 1;
636 $form->{employee_labels} = sub { $_[0]->{"name"} || $_[0]->{"login"} };
637 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
638 $form->{salesman_labels} = $form->{employee_labels};
642 print $form->parse_html_template('oe/search', { %myconfig });
644 $lxdebug->leave_sub();
647 sub create_subtotal_row {
648 $lxdebug->enter_sub();
650 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
652 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
654 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
656 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
658 map { $totals->{$_} = 0 } @{ $subtotal_columns };
660 $lxdebug->leave_sub();
666 $lxdebug->enter_sub();
670 $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
672 ($form->{ $form->{vc} }, $form->{"$form->{vc}_id"}) = split(/--/, $form->{ $form->{vc} });
674 report_generator_set_default_sort('transdate', 1);
676 OE->transactions(\%myconfig, \%$form);
678 $form->{rowcount} = scalar @{ $form->{OE} };
681 "transdate", "reqdate",
687 "shipvia", "globalprojectnumber",
688 "transaction_description", "open",
689 "delivered", "marge_total", "marge_percent",
693 # only show checkboxes if gotten here via sales_order form.
694 my $allow_multiple_orders = $form->{type} eq 'sales_order';
695 if ($allow_multiple_orders) {
696 unshift @columns, "ids";
699 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
700 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
702 my $attachment_basename;
703 if ($form->{vc} eq 'vendor') {
704 if ($form->{type} eq 'purchase_order') {
705 $form->{title} = $locale->text('Purchase Orders');
706 $attachment_basename = $locale->text('purchase_order_list');
708 $form->{title} = $locale->text('Request for Quotations');
709 $attachment_basename = $locale->text('rfq_list');
713 if ($form->{type} eq 'sales_order') {
714 $form->{title} = $locale->text('Sales Orders');
715 $attachment_basename = $locale->text('sales_order_list');
717 $form->{title} = $locale->text('Quotations');
718 $attachment_basename = $locale->text('quotation_list');
722 my $report = SL::ReportGenerator->new(\%myconfig, $form);
724 my @hidden_variables = map { "l_${_}" } @columns;
725 push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
726 transaction_description transdatefrom transdateto type vc employee_id salesman_id
727 reqdatefrom reqdateto);
729 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
732 'ids' => { 'text' => '', },
733 'transdate' => { 'text' => $locale->text('Date'), },
734 'reqdate' => { 'text' => $locale->text('Required by'), },
735 'id' => { 'text' => $locale->text('ID'), },
736 'ordnumber' => { 'text' => $locale->text('Order'), },
737 'quonumber' => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
738 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
739 'netamount' => { 'text' => $locale->text('Amount'), },
740 'tax' => { 'text' => $locale->text('Tax'), },
741 'amount' => { 'text' => $locale->text('Total'), },
742 'curr' => { 'text' => $locale->text('Curr'), },
743 'employee' => { 'text' => $locale->text('Employee'), },
744 'salesman' => { 'text' => $locale->text('Salesman'), },
745 'shipvia' => { 'text' => $locale->text('Ship via'), },
746 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
747 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
748 'open' => { 'text' => $locale->text('Open'), },
749 'delivered' => { 'text' => $locale->text('Delivered'), },
750 'marge_total' => { 'text' => $locale->text('Ertrag'), },
751 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), },
752 'country' => { 'text' => $locale->text('Country'), },
753 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
756 foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia transaction_description)) {
757 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
758 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
761 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
763 $form->{"l_type"} = "Y";
764 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
765 $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
767 $report->set_columns(%column_defs);
768 $report->set_column_order(@columns);
769 $report->set_export_options('orders', @hidden_variables, qw(sort sortdir));
770 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
773 my ($department) = split m/--/, $form->{department};
775 push @options, $locale->text('Customer') . " : $form->{customer}" if $form->{customer};
776 push @options, $locale->text('Vendor') . " : $form->{vendor}" if $form->{vendor};
777 push @options, $locale->text('Department') . " : $department" if $form->{department};
778 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if $form->{ordnumber};
779 push @options, $locale->text('Notes') . " : $form->{notes}" if $form->{notes};
780 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if $form->{transaction_description};
781 if ( $form->{transdatefrom} or $form->{transdateto} ) {
782 push @options, $locale->text('Order Date');
783 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if $form->{transdatefrom};
784 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if $form->{transdateto};
786 if ( $form->{reqdatefrom} or $form->{reqdateto} ) {
787 push @options, $locale->text('Delivery Date');
788 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{reqdatefrom}, 1) if $form->{reqdatefrom};
789 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{reqdateto}, 1) if $form->{reqdateto};
791 push @options, $locale->text('Open') if $form->{open};
792 push @options, $locale->text('Closed') if $form->{closed};
793 push @options, $locale->text('Delivered') if $form->{delivered};
794 push @options, $locale->text('Not delivered') if $form->{notdelivered};
796 $report->set_options('top_info_text' => join("\n", @options),
797 'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
798 'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
799 'output_format' => 'HTML',
800 'title' => $form->{title},
801 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
803 $report->set_options_from_form();
805 # add sort and escape callback, this one we use for the add sub
806 $form->{callback} = $href .= "&sort=$form->{sort}";
808 # escape callback for href
809 $callback = $form->escape($href);
811 my @subtotal_columns = qw(netamount amount marge_total marge_percent);
813 my %totals = map { $_ => 0 } @subtotal_columns;
814 my %subtotals = map { $_ => 0 } @subtotal_columns;
818 my $edit_url = build_std_url('action=edit', 'type', 'vc');
820 foreach $oe (@{ $form->{OE} }) {
821 map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
823 $oe->{tax} = $oe->{amount} - $oe->{netamount};
824 $oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
825 $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
827 map { $subtotals{$_} += $oe->{$_};
828 $totals{$_} += $oe->{$_} } @subtotal_columns;
830 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
831 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
833 map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
837 foreach my $column (@columns) {
838 next if ($column eq 'ids');
840 'data' => $oe->{$column},
841 'align' => $column_alignment{$column},
846 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
847 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
848 'valign' => 'center',
852 $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
854 my $row_set = [ $row ];
856 if (($form->{l_subtotal} eq 'Y')
857 && (($idx == (scalar @{ $form->{OE} } - 1))
858 || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
859 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
862 $report->add_data($row_set);
867 $report->add_separator();
868 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
870 $report->generate_with_headers();
872 $lxdebug->leave_sub();
875 sub check_delivered_flag {
876 $lxdebug->enter_sub();
880 if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
881 return $lxdebug->leave_sub();
884 my $all_delivered = 0;
886 foreach my $i (1 .. $form->{rowcount}) {
887 next if (!$form->{"id_$i"});
889 if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
898 $form->{delivered} = 1 if $all_delivered;
900 $lxdebug->leave_sub();
904 $lxdebug->enter_sub();
908 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
910 if ($form->{type} =~ /_order$/) {
911 $form->isblank("transdate", $locale->text('Order Date missing!'));
913 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
916 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
917 $form->{$idx} =~ s/^\s*//g;
918 $form->{$idx} =~ s/\s*$//g;
920 $msg = ucfirst $form->{vc};
921 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
923 # $locale->text('Customer missing!');
924 # $locale->text('Vendor missing!');
926 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
927 if ($form->{currency} ne $form->{defaultcurrency});
931 if($form->{payment_id}) {
932 $payment_id = $form->{payment_id};
935 # if the name changed get new values
936 if (&check_name($form->{vc})) {
937 if($form->{payment_id} eq "") {
938 $form->{payment_id} = $payment_id;
944 $form->{id} = 0 if $form->{saveasnew};
946 # this is for the internal notes section for the [email] Subject
947 if ($form->{type} =~ /_order$/) {
948 if ($form->{type} eq 'sales_order') {
949 $form->{label} = $locale->text('Sales Order');
951 $numberfld = "sonumber";
952 $ordnumber = "ordnumber";
954 $form->{label} = $locale->text('Purchase Order');
956 $numberfld = "ponumber";
957 $ordnumber = "ordnumber";
960 $err = $locale->text('Cannot save order!');
962 check_delivered_flag();
965 if ($form->{type} eq 'sales_quotation') {
966 $form->{label} = $locale->text('Quotation');
968 $numberfld = "sqnumber";
969 $ordnumber = "quonumber";
971 $form->{label} = $locale->text('Request for Quotation');
973 $numberfld = "rfqnumber";
974 $ordnumber = "quonumber";
977 $err = $locale->text('Cannot save quotation!');
981 # get new number in sequence if no number is given or if saveasnew was requested
982 if (!$form->{$ordnumber} || $form->{saveasnew}) {
983 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
988 $form->error($err) if (!OE->save(\%myconfig, \%$form));
991 if(!exists $form->{addition}) {
992 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
993 $form->{addition} = "SAVED";
994 $form->save_history($form->dbconnect(\%myconfig));
996 # /saving the history
998 $form->redirect($form->{label} . " $form->{$ordnumber} " .
999 $locale->text('saved!'));
1001 $lxdebug->leave_sub();
1005 $lxdebug->enter_sub();
1009 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1012 if ($form->{type} =~ /_order$/) {
1013 $form->isblank("transdate", $locale->text('Order Date missing!'));
1015 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1018 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1019 $form->{$idx} =~ s/^\s*//g;
1020 $form->{$idx} =~ s/\s*$//g;
1022 $msg = ucfirst $form->{vc};
1023 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1025 # $locale->text('Customer missing!');
1026 # $locale->text('Vendor missing!');
1028 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1029 if ($form->{currency} ne $form->{defaultcurrency});
1033 if($form->{payment_id}) {
1034 $payment_id = $form->{payment_id};
1037 # if the name changed get new values
1038 if (&check_name($form->{vc})) {
1039 if($form->{payment_id} eq "") {
1040 $form->{payment_id} = $payment_id;
1046 $form->{id} = 0 if $form->{saveasnew};
1048 # this is for the internal notes section for the [email] Subject
1049 if ($form->{type} =~ /_order$/) {
1050 if ($form->{type} eq 'sales_order') {
1051 $form->{label} = $locale->text('Sales Order');
1053 $numberfld = "sonumber";
1054 $ordnumber = "ordnumber";
1056 $form->{label} = $locale->text('Purchase Order');
1058 $numberfld = "ponumber";
1059 $ordnumber = "ordnumber";
1062 $err = $locale->text('Cannot save order!');
1064 check_delivered_flag();
1067 if ($form->{type} eq 'sales_quotation') {
1068 $form->{label} = $locale->text('Quotation');
1070 $numberfld = "sqnumber";
1071 $ordnumber = "quonumber";
1073 $form->{label} = $locale->text('Request for Quotation');
1075 $numberfld = "rfqnumber";
1076 $ordnumber = "quonumber";
1079 $err = $locale->text('Cannot save quotation!');
1083 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1084 unless $form->{$ordnumber};
1088 OE->save(\%myconfig, \%$form);
1090 # saving the history
1091 if(!exists $form->{addition}) {
1092 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1093 $form->{addition} = "SAVED";
1094 $form->save_history($form->dbconnect(\%myconfig));
1096 # /saving the history
1098 $form->{simple_save} = 1;
1099 if(!$form->{print_and_save}) {
1100 delete @{$form}{ary_diff([keys %{ $form }], [qw(login stylesheet id script type)])};
1104 $lxdebug->leave_sub();
1108 $lxdebug->enter_sub();
1114 if ($form->{type} =~ /_order$/) {
1115 $msg = $locale->text('Are you sure you want to delete Order Number');
1116 $ordnumber = 'ordnumber';
1118 $msg = $locale->text('Are you sure you want to delete Quotation Number');
1119 $ordnumber = 'quonumber';
1125 <form method=post action=$form->{script}>
1128 # delete action variable
1129 map { delete $form->{$_} } qw(action header);
1131 foreach $key (keys %$form) {
1132 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1133 $form->{$key} =~ s/\"/"/g;
1134 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1138 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1140 <h4>$msg $form->{$ordnumber}</h4>
1142 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1143 <input name=action class=submit type=submit value="|
1144 . $locale->text('Yes') . qq|">
1145 <button class=submit type=button onclick="history.back()">|
1146 . $locale->text('No') . qq|</button>
1153 $lxdebug->leave_sub();
1156 sub delete_order_quotation {
1157 $lxdebug->enter_sub();
1161 if ($form->{type} =~ /_order$/) {
1162 $msg = $locale->text('Order deleted!');
1163 $err = $locale->text('Cannot delete order!');
1165 $msg = $locale->text('Quotation deleted!');
1166 $err = $locale->text('Cannot delete quotation!');
1168 if (OE->delete(\%myconfig, \%$form, $spool)){
1169 # saving the history
1170 if(!exists $form->{addition}) {
1171 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1172 $form->{addition} = "DELETED";
1173 $form->save_history($form->dbconnect(\%myconfig));
1175 # /saving the history
1181 $lxdebug->leave_sub();
1185 $lxdebug->enter_sub();
1188 $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
1190 $form->{old_salesman_id} = $form->{salesman_id};
1191 $form->get_employee();
1194 if ($form->{type} =~ /_order$/) {
1196 # these checks only apply if the items don't bring their own ordnumbers/transdates.
1197 # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1198 $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1199 if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1200 $form->isblank("transdate", $locale->text('Order Date missing!'))
1201 if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1203 # also copy deliverydate from the order
1204 $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1205 $form->{orddate} = $form->{transdate};
1207 $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1208 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1209 $form->{ordnumber} = "";
1210 $form->{quodate} = $form->{transdate};
1213 $payment_id = $form->{payment_id} if $form->{payment_id};
1215 # if the name changed get new values
1216 if (&check_name($form->{vc})) {
1217 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1222 $form->{cp_id} *= 1;
1224 for $i (1 .. $form->{rowcount}) {
1225 for (qw(ship qty sellprice listprice basefactor)) {
1226 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1230 if ( $form->{type} =~ /_order/
1231 && $form->{currency} ne $form->{defaultcurrency}) {
1233 # check if we need a new exchangerate
1234 $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1236 $orddate = $form->current_date(\%myconfig);
1237 $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1239 if (!$exchangerate) {
1240 &backorder_exchangerate($orddate, $buysell);
1245 $form->{convert_from_oe_ids} = $form->{id};
1246 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1247 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1248 $form->{shipto} = 1;
1249 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1251 delete @{$form}{qw(id closed)};
1252 $form->{rowcount}--;
1254 if ($form->{type} =~ /_order$/) {
1255 $form->{exchangerate} = $exchangerate;
1259 if ( $form->{type} eq 'purchase_order'
1260 || $form->{type} eq 'request_quotation') {
1261 $form->{title} = $locale->text('Add Vendor Invoice');
1262 $form->{script} = 'ir.pl';
1267 if ( $form->{type} eq 'sales_order'
1268 || $form->{type} eq 'sales_quotation') {
1269 $form->{title} = $locale->text('Add Sales Invoice');
1270 $form->{script} = 'is.pl';
1275 # bo creates the id, reset it
1276 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1277 $form->{ $form->{vc} } =~ s/--.*//g;
1278 $form->{type} = "invoice";
1281 $locale = new Locale "$myconfig{countrycode}", "$script";
1283 require "bin/mozilla/$form->{script}";
1285 map { $form->{"select$_"} = "" } ($form->{vc}, "currency");
1287 $currency = $form->{currency};
1290 $form->{currency} = $currency;
1291 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1292 $form->{exchangerate} = $form->{forex} || '';
1294 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1299 for $i (1 .. $form->{rowcount}) {
1300 $form->{"discount_$i"} =
1301 $form->format_amount(\%myconfig, $form->{"discount_$i"});
1303 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1305 $decimalplaces = ($dec > 2) ? $dec : 2;
1307 # copy delivery date from reqdate for order -> invoice conversion
1308 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1309 unless $form->{"deliverydate_$i"};
1311 $form->{"sellprice_$i"} =
1312 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1315 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1316 $dec_qty = length $dec_qty;
1318 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1320 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1321 qw(partnumber description unit);
1327 $lxdebug->leave_sub();
1330 sub backorder_exchangerate {
1331 $lxdebug->enter_sub();
1335 my ($orddate, $buysell) = @_;
1342 <form method=post action=$form->{script}>
1345 # delete action variable
1346 map { delete $form->{$_} } qw(action header exchangerate);
1348 foreach $key (keys %$form) {
1349 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1350 $form->{$key} =~ s/\"/"/g;
1351 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1354 $form->{title} = $locale->text('Add Exchangerate');
1358 <input type=hidden name=exchangeratedate value=$orddate>
1359 <input type=hidden name=buysell value=$buysell>
1362 <tr><th class=listtop>$form->{title}</th></tr>
1363 <tr height="5"></tr>
1368 <th align=right>| . $locale->text('Currency') . qq|</th>
1369 <td>$form->{currency}</td>
1372 <th align=right>| . $locale->text('Date') . qq|</th>
1376 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1377 <td><input name=exchangerate size=11></td>
1387 <input type=hidden name=nextsub value=save_exchangerate>
1389 <input name=action class=submit type=submit value="|
1390 . $locale->text('Continue') . qq|">
1398 $lxdebug->leave_sub();
1401 sub save_exchangerate {
1402 $lxdebug->enter_sub();
1404 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1405 $form->{exchangerate} =
1406 $form->parse_amount(\%myconfig, $form->{exchangerate});
1407 $form->save_exchangerate(\%myconfig, $form->{currency},
1408 $form->{exchangeratedate},
1409 $form->{exchangerate}, $form->{buysell});
1413 $lxdebug->leave_sub();
1416 sub create_backorder {
1417 $lxdebug->enter_sub();
1419 $form->{shipped} = 1;
1421 # figure out if we need to create a backorder
1422 # items aren't saved if qty != 0
1424 for $i (1 .. $form->{rowcount}) {
1425 $totalqty += $qty = $form->{"qty_$i"};
1426 $totalship += $ship = $form->{"ship_$i"};
1428 $form->{"qty_$i"} = $qty - $ship;
1431 if ($totalship == 0) {
1432 map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1433 $form->{ordtotal} = 0;
1434 $form->{shipped} = 0;
1438 if ($totalqty == $totalship) {
1439 map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1440 $form->{ordtotal} = 0;
1445 qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1448 for $i (1 .. $form->{rowcount}) {
1450 $form->{"${_}_$i"} =
1451 $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1452 } qw(sellprice discount);
1457 OE->save(\%myconfig, \%$form);
1459 # rebuild rows for invoice
1463 for $i (1 .. $form->{rowcount}) {
1464 $form->{"qty_$i"} = $form->{"ship_$i"};
1466 if ($form->{"qty_$i"}) {
1469 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1474 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1475 $form->{rowcount} = $count;
1477 $lxdebug->leave_sub();
1481 $lxdebug->enter_sub();
1485 $form->{saveasnew} = 1;
1486 map { delete $form->{$_} } qw(printed emailed queued delivered closed);
1488 # Let Lx-Office assign a new order number if the user hasn't changed the
1489 # previous one. If it has been changed manually then use it as-is.
1490 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1491 $form->{$idx} =~ s/^\s*//g;
1492 $form->{$idx} =~ s/\s*$//g;
1493 if ($form->{saved_xyznumber} &&
1494 ($form->{saved_xyznumber} eq $form->{$idx})) {
1495 delete($form->{$idx});
1498 # clear reqdate unless changed
1499 if ($form->{reqdate} && $form->{id}) {
1500 my $saved_order = OE->retrieve_simple(id => $form->{id});
1501 if ($saved_order && $saved_order->{reqdate} eq $form->{reqdate}) {
1502 delete $form->{reqdate};
1507 $form->get_employee();
1511 $lxdebug->leave_sub();
1514 sub check_for_direct_delivery_yes {
1515 $lxdebug->enter_sub();
1519 $form->{direct_delivery_checked} = 1;
1520 delete @{$form}{grep /^shipto/, keys %{ $form }};
1521 map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1522 $form->{shipto} = 1;
1524 $lxdebug->leave_sub();
1527 sub check_for_direct_delivery_no {
1528 $lxdebug->enter_sub();
1532 $form->{direct_delivery_checked} = 1;
1533 delete @{$form}{grep /^shipto/, keys %{ $form }};
1536 $lxdebug->leave_sub();
1539 sub check_for_direct_delivery {
1540 $lxdebug->enter_sub();
1544 if ($form->{direct_delivery_checked}
1545 || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1546 $lxdebug->leave_sub();
1550 if ($form->{shipto_id}) {
1551 Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1554 map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1557 delete $form->{action};
1558 $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
1561 print $form->parse_html_template("oe/check_for_direct_delivery");
1563 $lxdebug->leave_sub();
1568 sub purchase_order {
1569 $lxdebug->enter_sub();
1572 $auth->assert('purchase_order_edit');
1574 if ($form->{type} eq 'sales_order') {
1575 check_for_direct_delivery();
1578 if ($form->{type} =~ /^sales_/) {
1579 delete($form->{ordnumber});
1582 $form->{cp_id} *= 1;
1584 $form->{title} = $locale->text('Add Purchase Order');
1585 $form->{vc} = "vendor";
1586 $form->{type} = "purchase_order";
1588 $form->get_employee();
1592 $lxdebug->leave_sub();
1596 $lxdebug->enter_sub();
1599 $auth->assert('sales_order_edit');
1601 if ($form->{type} eq "purchase_order") {
1602 delete($form->{ordnumber});
1605 $form->{cp_id} *= 1;
1607 $form->{title} = $locale->text('Add Sales Order');
1608 $form->{vc} = "customer";
1609 $form->{type} = "sales_order";
1611 $form->get_employee();
1615 $lxdebug->leave_sub();
1619 $lxdebug->enter_sub();
1622 $auth->assert('purchase_order_edit | sales_order_edit');
1624 $form->{transdate} = $form->current_date(\%myconfig);
1625 delete $form->{duedate};
1627 $form->{convert_from_oe_ids} = $form->{id};
1628 $form->{closed} = 0;
1630 $form->{old_employee_id} = $form->{employee_id};
1631 $form->{old_salesman_id} = $form->{salesman_id};
1634 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered
1637 for $i (1 .. $form->{rowcount}) {
1638 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor discount);
1641 my %saved_vars = map { $_ => $form->{$_} } grep { $form->{$_} } qw(currency);
1645 map { $form->{$_} = $saved_vars{$_} } keys %saved_vars;
1649 # prepare_order assumes that the discount is in db-notation (0.05) and not user-notation (5)
1650 # 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
1651 for $i (1 .. $form->{rowcount}) {
1652 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 0.01);
1656 for $i (1 .. $form->{rowcount} - 1) {
1657 map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
1662 $lxdebug->leave_sub();
1665 sub delivery_order {
1666 $lxdebug->enter_sub();
1668 if ($form->{type} =~ /^sales/) {
1669 $auth->assert('sales_delivery_order_edit');
1671 $form->{vc} = 'customer';
1672 $form->{type} = 'sales_delivery_order';
1675 $auth->assert('purchase_delivery_order_edit');
1677 $form->{vc} = 'vendor';
1678 $form->{type} = 'purchase_delivery_order';
1681 $form->get_employee();
1683 require "bin/mozilla/do.pl";
1685 $form->{script} = 'do.pl';
1686 $form->{cp_id} *= 1;
1687 $form->{convert_from_oe_ids} = $form->{id};
1688 $form->{transdate} = $form->current_date(\%myconfig);
1689 delete $form->{duedate};
1691 $form->{old_employee_id} = $form->{employee_id};
1692 $form->{old_salesman_id} = $form->{salesman_id};
1695 delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
1697 for $i (1 .. $form->{rowcount}) {
1698 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor discount);
1701 my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
1707 map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;
1711 $lxdebug->leave_sub();
1715 $lxdebug->enter_sub();
1719 $form->{print_and_save} = 1;
1723 my $saved_form = save_form();
1727 restore_form($saved_form, 0, qw(id ordnumber quonumber));
1731 $lxdebug->leave_sub();
1735 call_sub($form->{yes_nextsub});
1739 call_sub($form->{no_nextsub});
1742 ######################################################################################################
1744 # ###############################################################################################
1746 $lxdebug->enter_sub();
1750 retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
1752 $form->{"taxaccounts"} =~ s/\s*$//;
1753 $form->{"taxaccounts"} =~ s/^\s*//;
1754 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1755 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1757 $form->{"taxaccounts"} = "";
1759 for my $i (1 .. $form->{"rowcount"}) {
1760 IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
1763 $form->{rowcount}++;
1764 $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
1766 $form->language_payment(\%myconfig);
1768 Common::webdav_folder($form) if ($webdav);
1773 display_row($form->{rowcount}) if $form->{rowcount};
1777 $lxdebug->leave_sub();
1780 sub report_for_todo_list {
1781 $lxdebug->enter_sub();
1783 my $quotations = OE->transactions_for_todo_list();
1786 if (@{ $quotations }) {
1787 my $edit_url = build_std_url('script=oe.pl', 'action=edit');
1789 $content = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
1790 'edit_url' => $edit_url });
1793 $lxdebug->leave_sub();