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);
40 use SL::ReportGenerator;
41 use List::Util qw(max reduce);
43 require "bin/mozilla/io.pl";
44 require "bin/mozilla/arap.pl";
45 require "bin/mozilla/reportgenerator.pl";
52 # $locale->text('Edit the purchase_order');
53 # $locale->text('Edit the sales_order');
54 # $locale->text('Edit the request_quotation');
55 # $locale->text('Edit the sales_quotation');
57 # $locale->text('Workflow purchase_order');
58 # $locale->text('Workflow sales_order');
59 # $locale->text('Workflow request_quotation');
60 # $locale->text('Workflow sales_quotation');
63 'sales_order' => 'sales_order_edit',
64 'purchase_order' => 'purchase_order_edit',
65 'request_quotation' => 'request_quotation_edit',
66 'sales_quotation' => 'sales_quotation_edit',
70 my $right = $oe_access_map->{$form->{type}};
71 $right ||= 'DOES_NOT_EXIST';
73 $auth->assert($right);
77 $lxdebug->enter_sub();
83 if ($form->{type} eq 'purchase_order') {
84 $form->{title} = $action eq "edit" ?
85 $locale->text('Edit Purchase Order') :
86 $locale->text('Add Purchase Order');
87 $form->{heading} = $locale->text('Purchase Order');
88 $form->{vc} = 'vendor';
90 if ($form->{type} eq 'sales_order') {
91 $form->{title} = $action eq "edit" ?
92 $locale->text('Edit Sales Order') :
93 $locale->text('Add Sales Order');
94 $form->{heading} = $locale->text('Sales Order');
95 $form->{vc} = 'customer';
97 if ($form->{type} eq 'request_quotation') {
98 $form->{title} = $action eq "edit" ?
99 $locale->text('Edit Request for Quotation') :
100 $locale->text('Add Request for Quotation');
101 $form->{heading} = $locale->text('Request for Quotation');
102 $form->{vc} = 'vendor';
104 if ($form->{type} eq 'sales_quotation') {
105 $form->{title} = $action eq "edit" ?
106 $locale->text('Edit Quotation') :
107 $locale->text('Add Quotation');
108 $form->{heading} = $locale->text('Quotation');
109 $form->{vc} = 'customer';
112 $lxdebug->leave_sub();
116 $lxdebug->enter_sub();
123 "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
124 unless $form->{callback};
130 $lxdebug->leave_sub();
134 $lxdebug->enter_sub();
138 # show history button
139 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
140 #/show hhistory button
142 $form->{simple_save} = 0;
144 set_headings("edit");
146 # editing without stuff to edit? try adding it first
147 if ($form->{rowcount} && !$form->{print_and_save}) {
148 map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
152 undef $form->{rowcount};
154 $lxdebug->leave_sub();
157 } elsif (!$form->{id}) {
159 $lxdebug->leave_sub();
163 if ($form->{print_and_save}) {
164 $form->{action} = "print";
165 $form->{resubmit} = 1;
166 $language_id = $form->{language_id};
167 $printer_id = $form->{printer_id};
170 set_headings("edit");
174 if ($form->{print_and_save}) {
175 $form->{language_id} = $language_id;
176 $form->{printer_id} = $printer_id;
180 $lxdebug->leave_sub();
184 $lxdebug->enter_sub();
188 # get customer/vendor
189 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
191 # retrieve order/quotation
192 $form->{webdav} = $webdav;
193 $form->{jsscript} = 1;
195 my $editing = $form->{id};
197 OE->retrieve(\%myconfig, \%$form);
199 if ($form->{payment_id}) {
200 $payment_id = $form->{payment_id};
202 if ($form->{language_id}) {
203 $language_id = $form->{language_id};
205 if ($form->{taxzone_id}) {
206 $taxzone_id = $form->{taxzone_id};
209 $salesman_id = $form->{salesman_id} if ($editing);
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 if ( $form->{rowcount}
216 && $form->{type} eq 'sales_order'
217 && defined $form->{customer}
218 && $form->{customer} eq '') {
220 # $main::lxdebug->message(0, "Detected Edit order with concurrent customers");
223 'Collective Orders only work for orders from one customer!')
227 $taxincluded = $form->{taxincluded};
228 $form->{shipto} = 1 if $form->{id};
230 if ($form->{"all_$form->{vc}"}) {
231 unless ($form->{"$form->{vc}_id"}) {
232 $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
236 $cp_id = $form->{cp_id};
237 $intnotes = $form->{intnotes};
239 # get customer / vendor
240 if ($form->{type} =~ /(purchase_order|request_quotation)/) {
241 IR->get_vendor(\%myconfig, \%$form);
243 #quote all_vendor Bug 133
244 foreach $ref (@{ $form->{all_vendor} }) {
245 $ref->{name} = $form->quote($ref->{name});
249 if ($form->{type} =~ /sales_(order|quotation)/) {
250 IS->get_customer(\%myconfig, \%$form);
252 #quote all_vendor Bug 133
253 foreach $ref (@{ $form->{all_customer} }) {
254 $ref->{name} = $form->quote($ref->{name});
258 $form->{cp_id} = $cp_id;
261 $form->{payment_id} = $payment_id;
264 $form->{language_id} = $language_id;
267 $form->{taxzone_id} = $taxzone_id;
269 $form->{intnotes} = $intnotes if $intnotes;
270 ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
271 $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
273 # build the popup menus
274 if (@{ $form->{"all_$form->{vc}"} }) {
275 $form->{ $form->{vc} } =
276 qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
277 map { $form->{"select$form->{vc}"} .=
278 "<option>$_->{name}--$_->{id}</option>\n" }
279 (@{ $form->{"all_$form->{vc}"} });
282 $form->{taxincluded} = $taxincluded if ($form->{id});
285 if (@{ $form->{all_departments} }) {
286 $form->{selectdepartment} = "<option>\n";
287 $form->{department} = "$form->{department}--$form->{department_id}";
290 $form->{selectdepartment} .= "<option>$_->{description}--$_->{id}</option>\n"
291 } @{ $form->{all_departments} };
294 $form->{employee} = "$form->{employee}--$form->{employee_id}";
297 $form->{forex} = $form->{exchangerate};
299 $form->{salesman_id} = $salesman_id if ($editing);
301 $lxdebug->leave_sub();
305 $lxdebug->enter_sub();
309 $form->{formname} = $form->{type} unless $form->{formname};
312 foreach $ref (@{ $form->{form_details} }) {
313 $form->{rowcount} = ++$i;
314 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
316 for my $i (1 .. $form->{rowcount}) {
317 $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
318 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($form->{id} ? 100 : 1));
319 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
320 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
321 map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
324 $lxdebug->leave_sub();
328 $lxdebug->enter_sub();
333 # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
336 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
337 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
339 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
341 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
342 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
344 map { $form->{$_} = H($form->{$_}) }
345 qw(shippingpoint shipvia notes intnotes shiptoname
346 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
347 shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
349 # use JavaScript Calendar or not
350 $form->{jsscript} = 1;
351 $TMPL_VAR{button1} = qq|
352 <td nowrap><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
353 <input type=button name=transdate id="trigger1" value=| . $locale->text('button') . qq|></td>
355 $TMPL_VAR{button2} = qq|
356 <td nowrap width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
357 <input type=button name=reqdate name=reqdate id="trigger2" value=| . $locale->text('button') . qq|></td>
360 $TMPL_VAR{jsscript} = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
362 # openclosed checkboxes
364 push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
365 $form->{"delivered"} ? "checked" : "", $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
366 push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
367 $form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
368 $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
370 # set option selected
371 foreach $item ($form->{vc}, 'currency', 'department', ($form->{vc} eq "customer" ? 'customer' : 'vendor')) {
372 $form->{"select$item"} =~ s/ selected//;
373 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
376 #quote select[customer|vendor] Bug 133
377 $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
379 #substitute \n and \r to \s (bug 543)
380 $form->{"select$form->{vc}"} =~ s/[\n\r]/ /g;
382 my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
384 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
385 $form->get_lists("contacts" => "ALL_CONTACTS",
386 "shipto" => "ALL_SHIPTO",
387 "projects" => { "key" => "ALL_PROJECTS",
389 "old_id" => \@old_project_ids },
390 "employees" => "ALL_EMPLOYEES",
391 "salesmen" => "ALL_SALESMEN",
392 "taxzones" => "ALL_TAXZONES",
393 "payments" => "ALL_PAYMENTS",
394 "currencies" => "ALL_CURRENCIES",
395 $vc => { key => "ALL_" . uc($vc),
396 limit => $myconfig{vclimit} + 1 },
397 "price_factors" => "ALL_PRICE_FACTORS");
400 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
401 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
402 $TMPL_VAR{contact_labels} = sub { $_[0]->{"cp_name"} . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
405 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
406 $TMPL_VAR{vclimit} = $myconfig{vclimit};
407 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
408 push @custom_hiddens, "$form->{vc}_id";
409 push @custom_hiddens, "old$form->{vc}";
410 push @custom_hiddens, "select$form->{vc}";
412 # currencies and exchangerate
413 @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
414 %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
415 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
416 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency} && $form->{exchangerate};
417 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
418 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
419 push @custom_hiddens, "forex";
420 push @custom_hiddens, "exchangerate" if $form->{forex};
423 $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
424 $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
427 $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
429 push @custom_hiddens, "ordnumber" if $form->{type} =~ /_quotation$/;
430 push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
432 $credittext = $locale->text('Credit Limit exceeded!!!');
434 $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
435 : ($form->{resubmit}) ? "document.oe.submit()"
436 : ($creditwarning) ? "alert('$credittext')"
439 $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
440 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
441 $TMPL_VAR{onload} = $onload;
443 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
444 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
445 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
449 $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
450 qw(id action type vc formname media format proforma queued printed emailed
451 title creditlimit creditremaining tradediscount business
452 max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
453 shiptocity shiptocountry shiptocontact shiptophone shiptofax
454 shiptodepartment_1 shiptodepartment_2 shiptoemail
455 message email subject cc bcc taxpart taxservice taxaccounts),
457 map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ]; # deleted: discount
461 is_sales => scalar ($form->{type} =~ /^sales_/), # these vars are exported, so that the template
462 is_order => scalar ($form->{type} =~ /_order$/), # may determine what to show
463 is_sales_quo => scalar ($form->{type} =~ /sales_quotation$/),
464 is_req_quo => scalar ($form->{type} =~ /request_quotation$/),
465 is_sales_ord => scalar ($form->{type} =~ /sales_order$/),
466 is_pur_ord => scalar ($form->{type} =~ /purchase_order$/),
469 print $form->parse_html_template("oe/orders_header", { %TMPL_VAR });
471 $lxdebug->leave_sub();
475 $lxdebug->enter_sub();
479 $form->{invtotal} = $form->{invsubtotal};
481 $rows = max 2, $form->numtextrows($form->{notes}, 25, 8);
482 $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
483 $rows = max $rows, $introws;
485 $TMPL_VAR{notes} = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
486 $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
488 if (!$form->{taxincluded}) {
490 foreach $item (split / /, $form->{taxaccounts}) {
491 if ($form->{"${item}_base"}) {
492 $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
493 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
495 $TMPL_VAR{tax} .= qq|
497 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
498 <td align=right>$form->{"${item}_total"}</td>
503 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
506 foreach $item (split / /, $form->{taxaccounts}) {
507 if ($form->{"${item}_base"}) {
508 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
509 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
510 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
511 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
513 $TMPL_VAR{tax} .= qq|
515 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
516 <td align=right>$form->{"${item}_total"}</td>
519 <th align=right>Nettobetrag</th>
520 <td align=right>$form->{"${item}_netto"}</td>
526 $form->{oldinvtotal} = $form->{invtotal};
528 print $form->parse_html_template("oe/orders_footer", {
531 print_options => print_options(inline => 1),
532 label_edit => $locale->text("Edit the $form->{type}"),
533 label_workflow => $locale->text("Workflow $form->{type}"),
536 $lxdebug->leave_sub();
540 $lxdebug->enter_sub();
542 my ($recursive_call) = shift;
546 set_headings($form->{"id"} ? "edit" : "add");
548 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
551 $payment_id = $form->{payment_id} if $form->{payment_id};
553 &check_name($form->{vc});
555 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
558 $buysell = 'sell' if ($form->{vc} eq 'vendor');
559 $form->{exchangerate} = $exchangerate if
560 $form->{forex} = $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
562 $exchangerate = $form->{exchangerate} || 1;
564 ##################### process items ######################################
566 $i = $form->{rowcount};
567 if ( ($form->{"partnumber_$i"} eq "")
568 && ($form->{"description_$i"} eq "")
569 && ($form->{"partsgroup_$i"} eq "")) {
571 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
576 if ($form->{type} =~ /^sales/) {
577 IS->retrieve_item(\%myconfig, \%$form);
579 IR->retrieve_item(\%myconfig, \%$form);
582 my $rows = scalar @{ $form->{item_list} };
584 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
587 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
596 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
597 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
598 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
599 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
600 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
602 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
604 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
605 $decimalplaces = max 2, length $1;
608 $form->{"sellprice_$i"} = $sellprice;
610 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
611 $form->{"sellprice_$i"} /= $exchangerate; # if there is an exchange rate adjust sellprice
614 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
615 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
616 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
617 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
619 $form->{creditremaining} -= $amount;
621 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
622 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
624 # get pricegroups for parts
625 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
627 # build up html code for prices_$i
634 # ok, so this is a new part
635 # ask if it is a part or service item
637 if ( $form->{"partsgroup_$i"}
638 && ($form->{"partsnumber_$i"} eq "")
639 && ($form->{"description_$i"} eq "")) {
641 $form->{"discount_$i"} = "";
645 $form->{"id_$i"} = 0;
650 ##################### process items ######################################
653 $lxdebug->leave_sub();
657 $lxdebug->enter_sub();
661 if ($form->{type} eq 'purchase_order') {
662 $form->{title} = $locale->text('Purchase Orders');
663 $form->{vc} = 'vendor';
664 $ordlabel = $locale->text('Order Number');
665 $ordnumber = 'ordnumber';
666 $employee = $locale->text('Employee');
669 if ($form->{type} eq 'request_quotation') {
670 $form->{title} = $locale->text('Request for Quotations');
671 $form->{vc} = 'vendor';
672 $ordlabel = $locale->text('RFQ Number');
673 $ordnumber = 'quonumber';
674 $employee = $locale->text('Employee');
677 if ($form->{type} eq 'sales_order') {
678 $form->{title} = $locale->text('Sales Orders');
679 $form->{vc} = 'customer';
680 $ordlabel = $locale->text('Order Number');
681 $ordnumber = 'ordnumber';
682 $employee = $locale->text('Employee');
685 if ($form->{type} eq 'sales_quotation') {
686 $form->{title} = $locale->text('Quotations');
687 $form->{vc} = 'customer';
688 $ordlabel = $locale->text('Quotation Number');
689 $ordnumber = 'quonumber';
690 $employee = $locale->text('Employee');
693 # setup vendor / customer selection
694 $form->all_vc(\%myconfig, $form->{vc},
695 ($form->{vc} eq 'customer') ? "AR" : "AP");
698 if (@{ $form->{all_departments} }) {
699 $form->{selectdepartment} = "<option>\n";
702 $form->{selectdepartment} .=
703 "<option>$_->{description}--$_->{id}</option>\n"
704 } (@{ $form->{all_departments} });
709 <th align=right nowrap>| . $locale->text('Department') . qq|</th>
710 <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
712 | if $form->{selectdepartment};
715 if (($form->{"type"} eq "sales_order") ||
716 ($form->{"type"} eq "purchase_order")) {
719 <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
720 <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
721 <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
722 <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
727 # use JavaScript Calendar or not
728 $form->{jsscript} = 1;
732 <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
733 <input type=button name=transdatefrom id="trigger3" value=|
734 . $locale->text('button') . qq|></td>
737 <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
738 <input type=button name=transdateto name=transdateto id="trigger4" value=|
739 . $locale->text('button') . qq|></td>
744 Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
745 "transdateto", "BL", "trigger4");
747 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
749 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
751 "employees" => "ALL_EMPLOYEES",
752 "salesmen" => "ALL_SALESMEN",
753 $vc => "ALL_" . uc($vc)
758 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
759 push(@values, $item->{"id"});
760 $labels{$item->{"id"}} = $item->{"projectnumber"};
763 NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
764 '-labels' => \%labels));
769 foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
770 push(@values, $item->{"id"});
771 $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
775 my %labels_salesmen = ();
776 my @values_salesmen = ('');
777 foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
778 push(@values_salesmen, $item->{"id"});
779 $labels_salesmen{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
782 my $employee_block = qq|
784 <th align="right">| . $locale->text('Employee') . qq|</th>
785 <td>| . NTI($cgi->popup_menu('-name' => 'employee_id', '-values' => \@values, '-labels' => \%labels)) . qq|</td>
788 <th align="right">| . $locale->text('Salesman') . qq|</th>
790 NTI($cgi->popup_menu('-name' => 'salesman_id',
791 '-values' => \@values_salesmen,
792 '-labels' => \%labels_salesmen)) . qq|
799 foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
800 push(@values, $item->{name}.qq|--|.$item->{"id"});
801 $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
804 my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
806 $myconfig{vclimit} <= scalar(@values)
807 ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">|
808 : NTI($cgi->popup_menu('-name' => "$form->{vc}",
809 '-default' => $form->{"old$form->{vc}"},
810 '-onChange' => 'document.getElementById(\'update_button\').click();',
811 '-values' => \@values,
812 '-labels' => \%labels));
818 <form method=post action=$form->{script}>
822 <th class=listtop>$form->{title}</th>
829 <th align=right>$vc_label</th>
830 <td colspan=3>$vc</td>
834 <th align=right>$ordlabel</th>
835 <td colspan=3><input name="$ordnumber" size=20></td>
839 <th align="right">| . $locale->text('Transaction description') . qq|</th>
840 <td colspan="3"><input name="transaction_description" size=20></td>
843 <th align="right">| . $locale->text("Project Number") . qq|</th>
844 <td colspan="3">$projectnumber</td>
847 <th align=right>| . $locale->text('From') . qq|</th> $button1
848 <th align=right>| . $locale->text('Bis') . qq|</th> $button2
850 <input type=hidden name=sort value=transdate>
852 <th align=right>| . $locale->text('Include in Report') . qq|</th>
856 <td><input type="checkbox" name="open" value="1" id="open" checked> <label for="open">| . $locale->text("Open") . qq|</td>
857 <td><input type="checkbox" name="closed" value="1" id="closed"> <label for="closed">| . $locale->text("Closed") . qq|</td>
861 <td><input name="l_id" class=checkbox type=checkbox value=Y> | . $locale->text('ID') . qq|</td>
862 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
865 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Date') . qq|</td>
866 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Required by') . qq|</td>
869 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
870 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
872 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> | . $locale->text('Ship via') . qq|</td>
875 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> | . $locale->text('Amount') . qq|</td>
876 <td><input name="l_tax" class=checkbox type=checkbox value=Y> | . $locale->text('Tax') . qq|</td>
877 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> | . $locale->text('Total') . qq|</td>
880 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag') . qq|</td>
881 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag prozentual') . qq|</td>
884 <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> | . $locale->text('Project Number') . qq|</td>
885 <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> | . $locale->text('Transaction description') . qq|</td>
888 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> | . $locale->text('Subtotal') . qq|</td>
889 <td><input name="l_salesman" class="checkbox" type="checkbox" value="Y"> | . $locale->text('Salesman') . qq|</td>
897 <tr><td colspan=4><hr size=3 noshade></td></tr>
903 <input type=hidden name=nextsub value=orders>
904 <input type=hidden name=vc value=$form->{vc}>
905 <input type=hidden name=type value=$form->{type}>
907 <input class=submit type=submit name=action value="|
908 . $locale->text('Continue') . qq|">
915 $lxdebug->leave_sub();
918 sub create_subtotal_row {
919 $lxdebug->enter_sub();
921 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
923 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
925 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
927 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
929 map { $totals->{$_} = 0 } @{ $subtotal_columns };
931 $lxdebug->leave_sub();
937 $lxdebug->enter_sub();
941 $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
943 ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
945 $form->{sort} ||= 'transdate';
947 OE->transactions(\%myconfig, \%$form);
949 $form->{rowcount} = scalar @{ $form->{OE} };
952 "transdate", "reqdate",
958 "shipvia", "globalprojectnumber",
959 "transaction_description", "open",
960 "delivered", "marge_total", "marge_percent"
963 # only show checkboxes if gotten here via sales_order form.
964 my $allow_multiple_orders = $form->{type} eq 'sales_order';
965 if ($allow_multiple_orders) {
966 unshift @columns, "ids";
969 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
970 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
972 my $attachment_basename;
973 if ($form->{vc} eq 'vendor') {
974 if ($form->{type} eq 'purchase_order') {
975 $form->{title} = $locale->text('Purchase Orders');
976 $attachment_basename = $locale->text('purchase_order_list');
978 $form->{title} = $locale->text('Request for Quotations');
979 $attachment_basename = $locale->text('rfq_list');
983 if ($form->{type} eq 'sales_order') {
984 $form->{title} = $locale->text('Sales Orders');
985 $attachment_basename = $locale->text('sales_order_list');
987 $form->{title} = $locale->text('Quotations');
988 $attachment_basename = $locale->text('quotation_list');
992 my $report = SL::ReportGenerator->new(\%myconfig, $form);
994 my @hidden_variables = map { "l_${_}" } @columns;
995 push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
996 transaction_description transdatefrom transdateto type vc employee_id salesman_id);
998 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
1001 'ids' => { 'text' => '', },
1002 'transdate' => { 'text' => $locale->text('Date'), },
1003 'reqdate' => { 'text' => $locale->text('Required by'), },
1004 'id' => { 'text' => $locale->text('ID'), },
1005 'ordnumber' => { 'text' => $locale->text('Order'), },
1006 'quonumber' => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
1007 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1008 'netamount' => { 'text' => $locale->text('Amount'), },
1009 'tax' => { 'text' => $locale->text('Tax'), },
1010 'amount' => { 'text' => $locale->text('Total'), },
1011 'curr' => { 'text' => $locale->text('Curr'), },
1012 'employee' => { 'text' => $locale->text('Employee'), },
1013 'salesman' => { 'text' => $locale->text('Salesman'), },
1014 'shipvia' => { 'text' => $locale->text('Ship via'), },
1015 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
1016 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1017 'open' => { 'text' => $locale->text('Open'), },
1018 'delivered' => { 'text' => $locale->text('Delivered'), },
1019 'marge_total' => { 'text' => $locale->text('Ertrag'), },
1020 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), }
1023 foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia)) {
1024 $column_defs{$name}->{link} = $href . "&sort=$name";
1027 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
1029 $form->{"l_type"} = "Y";
1030 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1031 $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
1033 $report->set_columns(%column_defs);
1034 $report->set_column_order(@columns);
1036 $report->set_export_options('orders', @hidden_variables);
1038 $report->set_sort_indicator($form->{sort}, 1);
1041 if ($form->{customer}) {
1042 push @options, $locale->text('Customer') . " : $form->{customer}";
1044 if ($form->{vendor}) {
1045 push @options, $locale->text('Vendor') . " : $form->{vendor}";
1047 if ($form->{department}) {
1048 ($department) = split /--/, $form->{department};
1049 push @options, $locale->text('Department') . " : $department";
1051 if ($form->{ordnumber}) {
1052 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1054 if ($form->{notes}) {
1055 push @options, $locale->text('Notes') . " : $form->{notes}";
1057 if ($form->{transaction_description}) {
1058 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1060 if ($form->{transdatefrom}) {
1061 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1063 if ($form->{transdateto}) {
1064 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
1066 if ($form->{open}) {
1067 push @options, $locale->text('Open');
1069 if ($form->{closed}) {
1070 push @options, $locale->text('Closed');
1072 if ($form->{delivered}) {
1073 push @options, $locale->text('Delivered');
1075 if ($form->{notdelivered}) {
1076 push @options, $locale->text('Not delivered');
1079 $report->set_options('top_info_text' => join("\n", @options),
1080 'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
1081 'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
1082 'output_format' => 'HTML',
1083 'title' => $form->{title},
1084 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1086 $report->set_options_from_form();
1088 # add sort and escape callback, this one we use for the add sub
1089 $form->{callback} = $href .= "&sort=$form->{sort}";
1091 # escape callback for href
1092 $callback = $form->escape($href);
1094 my @subtotal_columns = qw(netamount amount marge_total marge_percent);
1096 my %totals = map { $_ => 0 } @subtotal_columns;
1097 my %subtotals = map { $_ => 0 } @subtotal_columns;
1101 my $edit_url = build_std_url('action=edit', 'type', 'vc');
1103 foreach $oe (@{ $form->{OE} }) {
1104 map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
1106 $oe->{tax} = $oe->{amount} - $oe->{netamount};
1107 $oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
1108 $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
1110 map { $subtotals{$_} += $oe->{$_};
1111 $totals{$_} += $oe->{$_} } @subtotal_columns;
1113 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1114 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1116 map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
1120 foreach my $column (@columns) {
1121 next if ($column eq 'ids');
1123 'data' => $oe->{$column},
1124 'align' => $column_alignment{$column},
1129 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
1130 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
1131 'valign' => 'center',
1132 'align' => 'center',
1135 $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1137 my $row_set = [ $row ];
1139 if (($form->{l_subtotal} eq 'Y')
1140 && (($idx == (scalar @{ $form->{OE} } - 1))
1141 || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
1142 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1145 $report->add_data($row_set);
1150 $report->add_separator();
1151 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1153 $report->generate_with_headers();
1155 $lxdebug->leave_sub();
1158 sub check_delivered_flag {
1159 $lxdebug->enter_sub();
1163 if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
1164 return $lxdebug->leave_sub();
1167 my $all_delivered = 0;
1169 foreach my $i (1 .. $form->{rowcount}) {
1170 next if (!$form->{"id_$i"});
1172 if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
1181 $form->{delivered} = 1 if $all_delivered;
1183 $lxdebug->leave_sub();
1186 sub save_and_close {
1187 $lxdebug->enter_sub();
1191 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1193 if ($form->{type} =~ /_order$/) {
1194 $form->isblank("transdate", $locale->text('Order Date missing!'));
1196 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1199 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1200 $form->{$idx} =~ s/^\s*//g;
1201 $form->{$idx} =~ s/\s*$//g;
1203 $msg = ucfirst $form->{vc};
1204 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1206 # $locale->text('Customer missing!');
1207 # $locale->text('Vendor missing!');
1209 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1210 if ($form->{currency} ne $form->{defaultcurrency});
1214 if($form->{payment_id}) {
1215 $payment_id = $form->{payment_id};
1218 # if the name changed get new values
1219 if (&check_name($form->{vc})) {
1220 if($form->{payment_id} eq "") {
1221 $form->{payment_id} = $payment_id;
1227 $form->{id} = 0 if $form->{saveasnew};
1229 # this is for the internal notes section for the [email] Subject
1230 if ($form->{type} =~ /_order$/) {
1231 if ($form->{type} eq 'sales_order') {
1232 $form->{label} = $locale->text('Sales Order');
1234 $numberfld = "sonumber";
1235 $ordnumber = "ordnumber";
1237 $form->{label} = $locale->text('Purchase Order');
1239 $numberfld = "ponumber";
1240 $ordnumber = "ordnumber";
1243 $err = $locale->text('Cannot save order!');
1245 check_delivered_flag();
1248 if ($form->{type} eq 'sales_quotation') {
1249 $form->{label} = $locale->text('Quotation');
1251 $numberfld = "sqnumber";
1252 $ordnumber = "quonumber";
1254 $form->{label} = $locale->text('Request for Quotation');
1256 $numberfld = "rfqnumber";
1257 $ordnumber = "quonumber";
1260 $err = $locale->text('Cannot save quotation!');
1264 # get new number in sequence if no number is given or if saveasnew was requested
1265 if (!$form->{$ordnumber} || $form->{saveasnew}) {
1266 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1271 $form->error($err) if (!OE->save(\%myconfig, \%$form));
1273 # saving the history
1274 if(!exists $form->{addition}) {
1275 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1276 $form->{addition} = "SAVED";
1277 $form->save_history($form->dbconnect(\%myconfig));
1279 # /saving the history
1281 $form->redirect($form->{label} . " $form->{$ordnumber} " .
1282 $locale->text('saved!'));
1284 $lxdebug->leave_sub();
1288 $lxdebug->enter_sub();
1292 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1295 if ($form->{type} =~ /_order$/) {
1296 $form->isblank("transdate", $locale->text('Order Date missing!'));
1298 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1301 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1302 $form->{$idx} =~ s/^\s*//g;
1303 $form->{$idx} =~ s/\s*$//g;
1305 $msg = ucfirst $form->{vc};
1306 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1308 # $locale->text('Customer missing!');
1309 # $locale->text('Vendor missing!');
1311 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1312 if ($form->{currency} ne $form->{defaultcurrency});
1316 if($form->{payment_id}) {
1317 $payment_id = $form->{payment_id};
1320 # if the name changed get new values
1321 if (&check_name($form->{vc})) {
1322 if($form->{payment_id} eq "") {
1323 $form->{payment_id} = $payment_id;
1329 $form->{id} = 0 if $form->{saveasnew};
1331 # this is for the internal notes section for the [email] Subject
1332 if ($form->{type} =~ /_order$/) {
1333 if ($form->{type} eq 'sales_order') {
1334 $form->{label} = $locale->text('Sales Order');
1336 $numberfld = "sonumber";
1337 $ordnumber = "ordnumber";
1339 $form->{label} = $locale->text('Purchase Order');
1341 $numberfld = "ponumber";
1342 $ordnumber = "ordnumber";
1345 $err = $locale->text('Cannot save order!');
1347 check_delivered_flag();
1350 if ($form->{type} eq 'sales_quotation') {
1351 $form->{label} = $locale->text('Quotation');
1353 $numberfld = "sqnumber";
1354 $ordnumber = "quonumber";
1356 $form->{label} = $locale->text('Request for Quotation');
1358 $numberfld = "rfqnumber";
1359 $ordnumber = "quonumber";
1362 $err = $locale->text('Cannot save quotation!');
1366 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1367 unless $form->{$ordnumber};
1371 OE->save(\%myconfig, \%$form);
1373 # saving the history
1374 if(!exists $form->{addition}) {
1375 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1376 $form->{addition} = "SAVED";
1377 $form->save_history($form->dbconnect(\%myconfig));
1379 # /saving the history
1381 $form->{simple_save} = 1;
1382 if(!$form->{print_and_save}) {
1383 set_headings("edit");
1387 $lxdebug->leave_sub();
1391 $lxdebug->enter_sub();
1397 if ($form->{type} =~ /_order$/) {
1398 $msg = $locale->text('Are you sure you want to delete Order Number');
1399 $ordnumber = 'ordnumber';
1401 $msg = $locale->text('Are you sure you want to delete Quotation Number');
1402 $ordnumber = 'quonumber';
1408 <form method=post action=$form->{script}>
1411 # delete action variable
1412 map { delete $form->{$_} } qw(action header);
1414 foreach $key (keys %$form) {
1415 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1416 $form->{$key} =~ s/\"/"/g;
1417 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1421 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1423 <h4>$msg $form->{$ordnumber}</h4>
1425 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1426 <input name=action class=submit type=submit value="|
1427 . $locale->text('Yes') . qq|">
1428 <button class=submit type=button onclick="history.back()">|
1429 . $locale->text('No') . qq|</button>
1436 $lxdebug->leave_sub();
1439 sub delete_order_quotation {
1440 $lxdebug->enter_sub();
1444 if ($form->{type} =~ /_order$/) {
1445 $msg = $locale->text('Order deleted!');
1446 $err = $locale->text('Cannot delete order!');
1448 $msg = $locale->text('Quotation deleted!');
1449 $err = $locale->text('Cannot delete quotation!');
1451 if (OE->delete(\%myconfig, \%$form, $spool)){
1452 # saving the history
1453 if(!exists $form->{addition}) {
1454 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1455 $form->{addition} = "DELETED";
1456 $form->save_history($form->dbconnect(\%myconfig));
1458 # /saving the history
1464 $lxdebug->leave_sub();
1468 $lxdebug->enter_sub();
1471 $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
1473 $form->{old_employee_id} = $form->{employee_id};
1474 $form->{old_salesman_id} = $form->{salesman_id};
1476 if ($form->{type} =~ /_order$/) {
1478 # these checks only apply if the items don't bring their own ordnumbers/transdates.
1479 # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1480 $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1481 if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1482 $form->isblank("transdate", $locale->text('Order Date missing!'))
1483 if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1485 # also copy deliverydate from the order
1486 $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1487 $form->{orddate} = $form->{transdate};
1489 $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1490 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1491 $form->{ordnumber} = "";
1492 $form->{quodate} = $form->{transdate};
1495 $payment_id = $form->{payment_id} if $form->{payment_id};
1497 # if the name changed get new values
1498 if (&check_name($form->{vc})) {
1499 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1504 $form->{cp_id} *= 1;
1506 for $i (1 .. $form->{rowcount}) {
1507 for (qw(ship qty sellprice listprice basefactor)) {
1508 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1512 if ( $form->{type} =~ /_order/
1513 && $form->{currency} ne $form->{defaultcurrency}) {
1515 # check if we need a new exchangerate
1516 $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1518 $orddate = $form->current_date(\%myconfig);
1519 $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1521 if (!$exchangerate) {
1522 &backorder_exchangerate($orddate, $buysell);
1527 # close orders/quotations
1528 $form->{closed} = 1;
1530 # save order if one ordnumber has been given
1531 # if not it's most likely a collective order, which can't be saved back
1532 # so they just have to be closed
1533 if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
1534 OE->close_order(\%myconfig, \%$form) if ($form->{id});
1536 OE->close_orders(\%myconfig, \%$form);
1539 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1540 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1543 $form->{closed} = 0;
1544 $form->{rowcount}--;
1545 $form->{shipto} = 1;
1547 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1549 if ($form->{type} =~ /_order$/) {
1550 $form->{exchangerate} = $exchangerate;
1554 if ( $form->{type} eq 'purchase_order'
1555 || $form->{type} eq 'request_quotation') {
1556 $form->{title} = $locale->text('Add Vendor Invoice');
1557 $form->{script} = 'ir.pl';
1562 if ( $form->{type} eq 'sales_order'
1563 || $form->{type} eq 'sales_quotation') {
1564 $form->{title} = $locale->text('Add Sales Invoice');
1565 $form->{script} = 'is.pl';
1570 # bo creates the id, reset it
1571 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1572 $form->{ $form->{vc} } =~ s/--.*//g;
1573 $form->{type} = "invoice";
1576 $locale = new Locale "$myconfig{countrycode}", "$script";
1578 require "bin/mozilla/$form->{script}";
1580 map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1582 $currency = $form->{currency};
1585 $form->{currency} = $currency;
1586 $form->{exchangerate} = "";
1587 $form->{forex} = "";
1588 $form->{exchangerate} = $exchangerate
1592 $form->check_exchangerate(
1593 \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1596 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1601 for $i (1 .. $form->{rowcount}) {
1602 $form->{"discount_$i"} =
1603 $form->format_amount(\%myconfig, $form->{"discount_$i"});
1605 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1607 $decimalplaces = ($dec > 2) ? $dec : 2;
1609 # copy delivery date from reqdate for order -> invoice conversion
1610 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1611 unless $form->{"deliverydate_$i"};
1613 $form->{"sellprice_$i"} =
1614 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1617 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1618 $dec_qty = length $dec_qty;
1620 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1622 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1623 qw(partnumber description unit);
1629 $lxdebug->leave_sub();
1632 sub backorder_exchangerate {
1633 $lxdebug->enter_sub();
1637 my ($orddate, $buysell) = @_;
1644 <form method=post action=$form->{script}>
1647 # delete action variable
1648 map { delete $form->{$_} } qw(action header exchangerate);
1650 foreach $key (keys %$form) {
1651 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1652 $form->{$key} =~ s/\"/"/g;
1653 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1656 $form->{title} = $locale->text('Add Exchangerate');
1660 <input type=hidden name=exchangeratedate value=$orddate>
1661 <input type=hidden name=buysell value=$buysell>
1664 <tr><th class=listtop>$form->{title}</th></tr>
1665 <tr height="5"></tr>
1670 <th align=right>| . $locale->text('Currency') . qq|</th>
1671 <td>$form->{currency}</td>
1674 <th align=right>| . $locale->text('Date') . qq|</th>
1678 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1679 <td><input name=exchangerate size=11></td>
1689 <input type=hidden name=nextsub value=save_exchangerate>
1691 <input name=action class=submit type=submit value="|
1692 . $locale->text('Continue') . qq|">
1700 $lxdebug->leave_sub();
1703 sub save_exchangerate {
1704 $lxdebug->enter_sub();
1706 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1707 $form->{exchangerate} =
1708 $form->parse_amount(\%myconfig, $form->{exchangerate});
1709 $form->save_exchangerate(\%myconfig, $form->{currency},
1710 $form->{exchangeratedate},
1711 $form->{exchangerate}, $form->{buysell});
1715 $lxdebug->leave_sub();
1718 sub create_backorder {
1719 $lxdebug->enter_sub();
1721 $form->{shipped} = 1;
1723 # figure out if we need to create a backorder
1724 # items aren't saved if qty != 0
1726 for $i (1 .. $form->{rowcount}) {
1727 $totalqty += $qty = $form->{"qty_$i"};
1728 $totalship += $ship = $form->{"ship_$i"};
1730 $form->{"qty_$i"} = $qty - $ship;
1733 if ($totalship == 0) {
1734 map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1735 $form->{ordtotal} = 0;
1736 $form->{shipped} = 0;
1740 if ($totalqty == $totalship) {
1741 map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1742 $form->{ordtotal} = 0;
1747 qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1750 for $i (1 .. $form->{rowcount}) {
1752 $form->{"${_}_$i"} =
1753 $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1754 } qw(sellprice discount);
1759 OE->save(\%myconfig, \%$form);
1761 # rebuild rows for invoice
1765 for $i (1 .. $form->{rowcount}) {
1766 $form->{"qty_$i"} = $form->{"ship_$i"};
1768 if ($form->{"qty_$i"}) {
1771 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1776 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1777 $form->{rowcount} = $count;
1779 $lxdebug->leave_sub();
1783 $lxdebug->enter_sub();
1787 $form->{saveasnew} = 1;
1788 $form->{closed} = 0;
1789 map { delete $form->{$_} } qw(printed emailed queued);
1791 # Let Lx-Office assign a new order number if the user hasn't changed the
1792 # previous one. If it has been changed manually then use it as-is.
1793 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1794 $form->{$idx} =~ s/^\s*//g;
1795 $form->{$idx} =~ s/\s*$//g;
1796 if ($form->{saved_xyznumber} &&
1797 ($form->{saved_xyznumber} eq $form->{$idx})) {
1798 delete($form->{$idx});
1803 $lxdebug->leave_sub();
1806 sub check_for_direct_delivery_yes {
1807 $lxdebug->enter_sub();
1811 $form->{direct_delivery_checked} = 1;
1812 delete @{$form}{grep /^shipto/, keys %{ $form }};
1813 map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1814 $form->{shipto} = 1;
1816 $lxdebug->leave_sub();
1819 sub check_for_direct_delivery_no {
1820 $lxdebug->enter_sub();
1824 $form->{direct_delivery_checked} = 1;
1825 delete @{$form}{grep /^shipto/, keys %{ $form }};
1828 $lxdebug->leave_sub();
1831 sub check_for_direct_delivery {
1832 $lxdebug->enter_sub();
1836 if ($form->{direct_delivery_checked}
1837 || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1838 $lxdebug->leave_sub();
1842 if ($form->{shipto_id}) {
1843 Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1846 map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1849 delete $form->{action};
1850 $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
1853 print $form->parse_html_template("oe/check_for_direct_delivery");
1855 $lxdebug->leave_sub();
1860 sub purchase_order {
1861 $lxdebug->enter_sub();
1864 $auth->assert('purchase_order_edit');
1866 if ($form->{type} eq 'sales_order') {
1867 check_for_direct_delivery();
1870 if ( $form->{type} eq 'sales_quotation'
1871 || $form->{type} eq 'request_quotation') {
1872 OE->close_order(\%myconfig, \%$form);
1875 if ($form->{type} =~ /^sales_/) {
1876 delete($form->{ordnumber});
1879 $form->{cp_id} *= 1;
1881 $form->{title} = $locale->text('Add Purchase Order');
1882 $form->{vc} = "vendor";
1883 $form->{type} = "purchase_order";
1887 $lxdebug->leave_sub();
1891 $lxdebug->enter_sub();
1894 $auth->assert('sales_order_edit');
1896 if ( $form->{type} eq 'sales_quotation'
1897 || $form->{type} eq 'request_quotation') {
1898 OE->close_order(\%myconfig, $form);
1901 if ($form->{type} eq "purchase_order") {
1902 delete($form->{ordnumber});
1905 $form->{cp_id} *= 1;
1907 $form->{title} = $locale->text('Add Sales Order');
1908 $form->{vc} = "customer";
1909 $form->{type} = "sales_order";
1913 $lxdebug->leave_sub();
1917 $lxdebug->enter_sub();
1920 $auth->assert('purchase_order_edit | sales_order_edit');
1922 $form->{transdate} = $form->current_date(\%myconfig);
1923 delete $form->{duedate};
1925 $form->{closed} = 0;
1927 $form->{old_employee_id} = $form->{employee_id};
1928 $form->{old_salesman_id} = $form->{salesman_id};
1931 map { delete $form->{$_} }
1932 qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
1934 for $i (1 .. $form->{rowcount}) {
1935 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1936 $form->{"${_}_${i}"})
1937 if ($form->{"${_}_${i}"}) }
1938 qw(ship qty sellprice listprice basefactor));
1946 for $i (1 .. $form->{rowcount} - 1) {
1947 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1948 qw(partnumber description unit);
1953 $lxdebug->leave_sub();
1957 $lxdebug->enter_sub();
1961 $form->{print_and_save} = 1;
1965 my $saved_form = save_form();
1969 restore_form($saved_form, 0, qw(id ordnumber quonumber));
1973 $lxdebug->leave_sub();
1977 call_sub($form->{yes_nextsub});
1981 call_sub($form->{no_nextsub});
1984 ######################################################################################################
1986 # ###############################################################################################
1988 $lxdebug->enter_sub();
1992 $form->{"taxaccounts"} =~ s/\s*$//;
1993 $form->{"taxaccounts"} =~ s/^\s*//;
1994 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1995 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1997 $form->{"taxaccounts"} = "";
1999 for my $i (1 .. $form->{"rowcount"}) {
2000 IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
2003 $form->{rowcount}++;
2004 $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
2006 $form->language_payment(\%myconfig);
2008 Common::webdav_folder($form) if ($webdav);
2013 display_row($form->{rowcount}) if $form->{rowcount};
2017 $lxdebug->leave_sub();