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::ReportGenerator;
42 use List::Util qw(max reduce sum);
44 require "bin/mozilla/io.pl";
45 require "bin/mozilla/arap.pl";
46 require "bin/mozilla/reportgenerator.pl";
53 # $locale->text('Edit the purchase_order');
54 # $locale->text('Edit the sales_order');
55 # $locale->text('Edit the request_quotation');
56 # $locale->text('Edit the sales_quotation');
58 # $locale->text('Workflow purchase_order');
59 # $locale->text('Workflow sales_order');
60 # $locale->text('Workflow request_quotation');
61 # $locale->text('Workflow sales_quotation');
64 'sales_order' => 'sales_order_edit',
65 'purchase_order' => 'purchase_order_edit',
66 'request_quotation' => 'request_quotation_edit',
67 'sales_quotation' => 'sales_quotation_edit',
71 my $right = $oe_access_map->{$form->{type}};
72 $right ||= 'DOES_NOT_EXIST';
74 $auth->assert($right);
78 $lxdebug->enter_sub();
84 if ($form->{type} eq 'purchase_order') {
85 $form->{title} = $action eq "edit" ?
86 $locale->text('Edit Purchase Order') :
87 $locale->text('Add Purchase Order');
88 $form->{heading} = $locale->text('Purchase Order');
89 $form->{vc} = 'vendor';
91 if ($form->{type} eq 'sales_order') {
92 $form->{title} = $action eq "edit" ?
93 $locale->text('Edit Sales Order') :
94 $locale->text('Add Sales Order');
95 $form->{heading} = $locale->text('Sales Order');
96 $form->{vc} = 'customer';
98 if ($form->{type} eq 'request_quotation') {
99 $form->{title} = $action eq "edit" ?
100 $locale->text('Edit Request for Quotation') :
101 $locale->text('Add Request for Quotation');
102 $form->{heading} = $locale->text('Request for Quotation');
103 $form->{vc} = 'vendor';
105 if ($form->{type} eq 'sales_quotation') {
106 $form->{title} = $action eq "edit" ?
107 $locale->text('Edit Quotation') :
108 $locale->text('Add Quotation');
109 $form->{heading} = $locale->text('Quotation');
110 $form->{vc} = 'customer';
113 $lxdebug->leave_sub();
117 $lxdebug->enter_sub();
124 "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
125 unless $form->{callback};
131 $lxdebug->leave_sub();
135 $lxdebug->enter_sub();
139 # show history button
140 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
141 #/show hhistory button
143 $form->{simple_save} = 0;
145 set_headings("edit");
147 # editing without stuff to edit? try adding it first
148 if ($form->{rowcount} && !$form->{print_and_save}) {
149 map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
153 undef $form->{rowcount};
155 $lxdebug->leave_sub();
158 } elsif (!$form->{id}) {
160 $lxdebug->leave_sub();
164 if ($form->{print_and_save}) {
165 $form->{action} = "print";
166 $form->{resubmit} = 1;
167 $language_id = $form->{language_id};
168 $printer_id = $form->{printer_id};
171 set_headings("edit");
175 if ($form->{print_and_save}) {
176 $form->{language_id} = $language_id;
177 $form->{printer_id} = $printer_id;
181 $lxdebug->leave_sub();
185 $lxdebug->enter_sub();
189 # get customer/vendor
190 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
192 # retrieve order/quotation
193 $form->{webdav} = $webdav;
194 $form->{jsscript} = 1;
196 my $editing = $form->{id};
198 OE->retrieve(\%myconfig, \%$form);
200 if ($form->{payment_id}) {
201 $payment_id = $form->{payment_id};
203 if ($form->{language_id}) {
204 $language_id = $form->{language_id};
206 if ($form->{taxzone_id}) {
207 $taxzone_id = $form->{taxzone_id};
210 $salesman_id = $form->{salesman_id} if ($editing);
213 # if multiple rowcounts (== collective order) then check if the
214 # there were more than one customer (in that case OE::retrieve removes
215 # the content from the field)
216 if ( $form->{rowcount}
217 && $form->{type} eq 'sales_order'
218 && defined $form->{customer}
219 && $form->{customer} eq '') {
221 # $main::lxdebug->message(0, "Detected Edit order with concurrent customers");
224 'Collective Orders only work for orders from one customer!')
228 $taxincluded = $form->{taxincluded};
229 $form->{shipto} = 1 if $form->{id};
231 if ($form->{"all_$form->{vc}"}) {
232 unless ($form->{"$form->{vc}_id"}) {
233 $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
237 $cp_id = $form->{cp_id};
238 $intnotes = $form->{intnotes};
240 # get customer / vendor
241 if ($form->{type} =~ /(purchase_order|request_quotation)/) {
242 IR->get_vendor(\%myconfig, \%$form);
244 #quote all_vendor Bug 133
245 foreach $ref (@{ $form->{all_vendor} }) {
246 $ref->{name} = $form->quote($ref->{name});
250 if ($form->{type} =~ /sales_(order|quotation)/) {
251 IS->get_customer(\%myconfig, \%$form);
253 #quote all_vendor Bug 133
254 foreach $ref (@{ $form->{all_customer} }) {
255 $ref->{name} = $form->quote($ref->{name});
259 $form->{cp_id} = $cp_id;
262 $form->{payment_id} = $payment_id;
265 $form->{language_id} = $language_id;
268 $form->{taxzone_id} = $taxzone_id;
270 $form->{intnotes} = $intnotes if $intnotes;
271 ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
272 $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
274 # build the popup menus
275 if (@{ $form->{"all_$form->{vc}"} }) {
276 $form->{ $form->{vc} } =
277 qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
278 map { $form->{"select$form->{vc}"} .=
279 "<option>$_->{name}--$_->{id}</option>\n" }
280 (@{ $form->{"all_$form->{vc}"} });
283 $form->{taxincluded} = $taxincluded if ($form->{id});
286 if (@{ $form->{all_departments} }) {
287 $form->{selectdepartment} = "<option>\n";
288 $form->{department} = "$form->{department}--$form->{department_id}";
291 $form->{selectdepartment} .= "<option>$_->{description}--$_->{id}</option>\n"
292 } @{ $form->{all_departments} };
295 $form->{employee} = "$form->{employee}--$form->{employee_id}";
298 $form->{forex} = $form->{exchangerate};
300 $form->{salesman_id} = $salesman_id if ($editing);
302 $lxdebug->leave_sub();
306 $lxdebug->enter_sub();
310 $form->{formname} = $form->{type} unless $form->{formname};
313 foreach $ref (@{ $form->{form_details} }) {
314 $form->{rowcount} = ++$i;
315 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
317 for my $i (1 .. $form->{rowcount}) {
318 $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
319 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($form->{id} ? 100 : 1));
320 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
321 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
322 map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
325 $lxdebug->leave_sub();
329 $lxdebug->enter_sub();
334 # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
337 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
338 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
340 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
342 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
343 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
345 map { $form->{$_} = H($form->{$_}) }
346 qw(shippingpoint shipvia notes intnotes shiptoname
347 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
348 shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
350 # use JavaScript Calendar or not
351 $form->{jsscript} = 1;
352 $TMPL_VAR{button1} = qq|
353 <td nowrap><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
354 <input type=button name=transdate id="trigger1" value=| . $locale->text('button') . qq|></td>
356 $TMPL_VAR{button2} = qq|
357 <td nowrap width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
358 <input type=button name=reqdate name=reqdate id="trigger2" value=| . $locale->text('button') . qq|></td>
361 $TMPL_VAR{jsscript} = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
363 # openclosed checkboxes
365 push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
366 $form->{"delivered"} ? "checked" : "", $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
367 push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
368 $form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
369 $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
371 # set option selected
372 foreach $item ($form->{vc}, 'currency', 'department', ($form->{vc} eq "customer" ? 'customer' : 'vendor')) {
373 $form->{"select$item"} =~ s/ selected//;
374 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
377 #quote select[customer|vendor] Bug 133
378 $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
380 #substitute \n and \r to \s (bug 543)
381 $form->{"select$form->{vc}"} =~ s/[\n\r]/ /g;
383 my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
385 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
386 $form->get_lists("contacts" => "ALL_CONTACTS",
387 "shipto" => "ALL_SHIPTO",
388 "projects" => { "key" => "ALL_PROJECTS",
390 "old_id" => \@old_project_ids },
391 "employees" => "ALL_EMPLOYEES",
392 "salesmen" => "ALL_SALESMEN",
393 "taxzones" => "ALL_TAXZONES",
394 "payments" => "ALL_PAYMENTS",
395 "currencies" => "ALL_CURRENCIES",
396 $vc => { key => "ALL_" . uc($vc),
397 limit => $myconfig{vclimit} + 1 },
398 "price_factors" => "ALL_PRICE_FACTORS");
401 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
402 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
403 $TMPL_VAR{contact_labels} = sub { $_[0]->{"cp_name"} . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
406 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
407 $TMPL_VAR{vclimit} = $myconfig{vclimit};
408 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
409 push @custom_hiddens, "$form->{vc}_id";
410 push @custom_hiddens, "old$form->{vc}";
411 push @custom_hiddens, "select$form->{vc}";
413 # currencies and exchangerate
414 @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
415 %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
416 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
417 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency} && $form->{exchangerate};
418 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
419 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
420 push @custom_hiddens, "forex";
421 push @custom_hiddens, "exchangerate" if $form->{forex};
424 $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
425 $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
428 $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
430 push @custom_hiddens, "ordnumber" if $form->{type} =~ /_quotation$/;
431 push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
433 $credittext = $locale->text('Credit Limit exceeded!!!');
435 my $follow_up_vc = $form->{ $form->{vc} eq 'customer' ? 'customer' : 'vendor' };
436 $follow_up_vc =~ s/--.*?//;
437 $TMPL_VAR{follow_up_trans_info} = ($form->{type} =~ /_quotation$/ ? $form->{quonumber} : $form->{ordnumber}) . " ($follow_up_vc)";
440 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
442 if (scalar @{ $follow_ups }) {
443 $TMPL_VAR{num_follow_ups} = scalar @{ $follow_ups };
444 $TMPL_VAR{num_due_follow_ups} = sum map { $_->{due} * 1 } @{ $follow_ups };
448 $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
449 : ($form->{resubmit}) ? "document.oe.submit()"
450 : ($creditwarning) ? "alert('$credittext')"
453 $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
454 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
455 $TMPL_VAR{onload} = $onload;
457 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
458 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
459 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
463 $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
464 qw(id action type vc formname media format proforma queued printed emailed
465 title creditlimit creditremaining tradediscount business
466 max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
467 shiptocity shiptocountry shiptocontact shiptophone shiptofax
468 shiptodepartment_1 shiptodepartment_2 shiptoemail
469 message email subject cc bcc taxpart taxservice taxaccounts),
471 map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ]; # deleted: discount
475 is_sales => scalar ($form->{type} =~ /^sales_/), # these vars are exported, so that the template
476 is_order => scalar ($form->{type} =~ /_order$/), # may determine what to show
477 is_sales_quo => scalar ($form->{type} =~ /sales_quotation$/),
478 is_req_quo => scalar ($form->{type} =~ /request_quotation$/),
479 is_sales_ord => scalar ($form->{type} =~ /sales_order$/),
480 is_pur_ord => scalar ($form->{type} =~ /purchase_order$/),
483 print $form->parse_html_template("oe/orders_header", { %TMPL_VAR });
485 $lxdebug->leave_sub();
489 $lxdebug->enter_sub();
493 $form->{invtotal} = $form->{invsubtotal};
495 $rows = max 2, $form->numtextrows($form->{notes}, 25, 8);
496 $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
497 $rows = max $rows, $introws;
499 $TMPL_VAR{notes} = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
500 $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
502 if (!$form->{taxincluded}) {
504 foreach $item (split / /, $form->{taxaccounts}) {
505 if ($form->{"${item}_base"}) {
506 $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
507 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
509 $TMPL_VAR{tax} .= qq|
511 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
512 <td align=right>$form->{"${item}_total"}</td>
517 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
520 foreach $item (split / /, $form->{taxaccounts}) {
521 if ($form->{"${item}_base"}) {
522 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
523 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
524 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
525 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
527 $TMPL_VAR{tax} .= qq|
529 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
530 <td align=right>$form->{"${item}_total"}</td>
533 <th align=right>Nettobetrag</th>
534 <td align=right>$form->{"${item}_netto"}</td>
540 $form->{oldinvtotal} = $form->{invtotal};
542 print $form->parse_html_template("oe/orders_footer", {
545 print_options => print_options(inline => 1),
546 label_edit => $locale->text("Edit the $form->{type}"),
547 label_workflow => $locale->text("Workflow $form->{type}"),
550 $lxdebug->leave_sub();
554 $lxdebug->enter_sub();
556 my ($recursive_call) = shift;
560 set_headings($form->{"id"} ? "edit" : "add");
562 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
565 $payment_id = $form->{payment_id} if $form->{payment_id};
567 &check_name($form->{vc});
569 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
572 $buysell = 'sell' if ($form->{vc} eq 'vendor');
573 $form->{exchangerate} = $exchangerate if
574 $form->{forex} = $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
576 $exchangerate = $form->{exchangerate} || 1;
578 ##################### process items ######################################
580 $i = $form->{rowcount};
581 if ( ($form->{"partnumber_$i"} eq "")
582 && ($form->{"description_$i"} eq "")
583 && ($form->{"partsgroup_$i"} eq "")) {
585 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
590 if ($form->{type} =~ /^sales/) {
591 IS->retrieve_item(\%myconfig, \%$form);
593 IR->retrieve_item(\%myconfig, \%$form);
596 my $rows = scalar @{ $form->{item_list} };
598 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
601 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
610 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
611 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
612 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
613 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
614 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
616 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
618 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
619 $decimalplaces = max 2, length $1;
622 $form->{"sellprice_$i"} = $sellprice;
624 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
625 $form->{"sellprice_$i"} /= $exchangerate; # if there is an exchange rate adjust sellprice
628 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
629 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
630 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
631 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
633 $form->{creditremaining} -= $amount;
635 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
636 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
638 # get pricegroups for parts
639 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
641 # build up html code for prices_$i
648 # ok, so this is a new part
649 # ask if it is a part or service item
651 if ( $form->{"partsgroup_$i"}
652 && ($form->{"partsnumber_$i"} eq "")
653 && ($form->{"description_$i"} eq "")) {
655 $form->{"discount_$i"} = "";
659 $form->{"id_$i"} = 0;
664 ##################### process items ######################################
667 $lxdebug->leave_sub();
671 $lxdebug->enter_sub();
675 if ($form->{type} eq 'purchase_order') {
676 $form->{title} = $locale->text('Purchase Orders');
677 $form->{vc} = 'vendor';
678 $ordlabel = $locale->text('Order Number');
679 $ordnumber = 'ordnumber';
680 $employee = $locale->text('Employee');
683 if ($form->{type} eq 'request_quotation') {
684 $form->{title} = $locale->text('Request for Quotations');
685 $form->{vc} = 'vendor';
686 $ordlabel = $locale->text('RFQ Number');
687 $ordnumber = 'quonumber';
688 $employee = $locale->text('Employee');
691 if ($form->{type} eq 'sales_order') {
692 $form->{title} = $locale->text('Sales Orders');
693 $form->{vc} = 'customer';
694 $ordlabel = $locale->text('Order Number');
695 $ordnumber = 'ordnumber';
696 $employee = $locale->text('Employee');
699 if ($form->{type} eq 'sales_quotation') {
700 $form->{title} = $locale->text('Quotations');
701 $form->{vc} = 'customer';
702 $ordlabel = $locale->text('Quotation Number');
703 $ordnumber = 'quonumber';
704 $employee = $locale->text('Employee');
707 # setup vendor / customer selection
708 $form->all_vc(\%myconfig, $form->{vc},
709 ($form->{vc} eq 'customer') ? "AR" : "AP");
712 if (@{ $form->{all_departments} }) {
713 $form->{selectdepartment} = "<option>\n";
716 $form->{selectdepartment} .=
717 "<option>$_->{description}--$_->{id}</option>\n"
718 } (@{ $form->{all_departments} });
723 <th align=right nowrap>| . $locale->text('Department') . qq|</th>
724 <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
726 | if $form->{selectdepartment};
729 if (($form->{"type"} eq "sales_order") ||
730 ($form->{"type"} eq "purchase_order")) {
733 <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
734 <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
735 <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
736 <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
741 # use JavaScript Calendar or not
742 $form->{jsscript} = 1;
746 <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
747 <input type=button name=transdatefrom id="trigger3" value=|
748 . $locale->text('button') . qq|></td>
751 <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
752 <input type=button name=transdateto name=transdateto id="trigger4" value=|
753 . $locale->text('button') . qq|></td>
758 Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
759 "transdateto", "BL", "trigger4");
761 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
763 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
765 "employees" => "ALL_EMPLOYEES",
766 "salesmen" => "ALL_SALESMEN",
767 $vc => "ALL_" . uc($vc)
772 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
773 push(@values, $item->{"id"});
774 $labels{$item->{"id"}} = $item->{"projectnumber"};
777 NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
778 '-labels' => \%labels));
783 foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
784 push(@values, $item->{"id"});
785 $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
789 my %labels_salesmen = ();
790 my @values_salesmen = ('');
791 foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
792 push(@values_salesmen, $item->{"id"});
793 $labels_salesmen{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
796 my $employee_block = qq|
798 <th align="right">| . $locale->text('Employee') . qq|</th>
799 <td>| . NTI($cgi->popup_menu('-name' => 'employee_id', '-values' => \@values, '-labels' => \%labels)) . qq|</td>
802 <th align="right">| . $locale->text('Salesman') . qq|</th>
804 NTI($cgi->popup_menu('-name' => 'salesman_id',
805 '-values' => \@values_salesmen,
806 '-labels' => \%labels_salesmen)) . qq|
813 foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
814 push(@values, $item->{name}.qq|--|.$item->{"id"});
815 $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
818 my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
820 $myconfig{vclimit} <= scalar(@values)
821 ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">|
822 : NTI($cgi->popup_menu('-name' => "$form->{vc}",
823 '-default' => $form->{"old$form->{vc}"},
824 '-onChange' => 'document.getElementById(\'update_button\').click();',
825 '-values' => \@values,
826 '-labels' => \%labels));
832 <form method=post action=$form->{script}>
836 <th class=listtop>$form->{title}</th>
843 <th align=right>$vc_label</th>
844 <td colspan=3>$vc</td>
848 <th align=right>$ordlabel</th>
849 <td colspan=3><input name="$ordnumber" size=20></td>
853 <th align="right">| . $locale->text('Transaction description') . qq|</th>
854 <td colspan="3"><input name="transaction_description" size=20></td>
857 <th align="right">| . $locale->text("Project Number") . qq|</th>
858 <td colspan="3">$projectnumber</td>
861 <th align=right>| . $locale->text('From') . qq|</th> $button1
862 <th align=right>| . $locale->text('Bis') . qq|</th> $button2
864 <input type=hidden name=sort value=transdate>
866 <th align=right>| . $locale->text('Include in Report') . qq|</th>
870 <td><input type="checkbox" name="open" value="1" id="open" checked> <label for="open">| . $locale->text("Open") . qq|</td>
871 <td><input type="checkbox" name="closed" value="1" id="closed"> <label for="closed">| . $locale->text("Closed") . qq|</td>
875 <td><input name="l_id" class=checkbox type=checkbox value=Y> | . $locale->text('ID') . qq|</td>
876 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
879 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Date') . qq|</td>
880 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Required by') . qq|</td>
883 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
884 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
886 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> | . $locale->text('Ship via') . qq|</td>
889 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> | . $locale->text('Amount') . qq|</td>
890 <td><input name="l_tax" class=checkbox type=checkbox value=Y> | . $locale->text('Tax') . qq|</td>
891 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> | . $locale->text('Total') . qq|</td>
894 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag') . qq|</td>
895 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag prozentual') . qq|</td>
898 <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> | . $locale->text('Project Number') . qq|</td>
899 <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> | . $locale->text('Transaction description') . qq|</td>
902 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> | . $locale->text('Subtotal') . qq|</td>
903 <td><input name="l_salesman" class="checkbox" type="checkbox" value="Y"> | . $locale->text('Salesman') . qq|</td>
911 <tr><td colspan=4><hr size=3 noshade></td></tr>
917 <input type=hidden name=nextsub value=orders>
918 <input type=hidden name=vc value=$form->{vc}>
919 <input type=hidden name=type value=$form->{type}>
921 <input class=submit type=submit name=action value="|
922 . $locale->text('Continue') . qq|">
929 $lxdebug->leave_sub();
932 sub create_subtotal_row {
933 $lxdebug->enter_sub();
935 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
937 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
939 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
941 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
943 map { $totals->{$_} = 0 } @{ $subtotal_columns };
945 $lxdebug->leave_sub();
951 $lxdebug->enter_sub();
955 $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
957 ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
959 $form->{sort} ||= 'transdate';
961 OE->transactions(\%myconfig, \%$form);
963 $form->{rowcount} = scalar @{ $form->{OE} };
966 "transdate", "reqdate",
972 "shipvia", "globalprojectnumber",
973 "transaction_description", "open",
974 "delivered", "marge_total", "marge_percent"
977 # only show checkboxes if gotten here via sales_order form.
978 my $allow_multiple_orders = $form->{type} eq 'sales_order';
979 if ($allow_multiple_orders) {
980 unshift @columns, "ids";
983 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
984 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
986 my $attachment_basename;
987 if ($form->{vc} eq 'vendor') {
988 if ($form->{type} eq 'purchase_order') {
989 $form->{title} = $locale->text('Purchase Orders');
990 $attachment_basename = $locale->text('purchase_order_list');
992 $form->{title} = $locale->text('Request for Quotations');
993 $attachment_basename = $locale->text('rfq_list');
997 if ($form->{type} eq 'sales_order') {
998 $form->{title} = $locale->text('Sales Orders');
999 $attachment_basename = $locale->text('sales_order_list');
1001 $form->{title} = $locale->text('Quotations');
1002 $attachment_basename = $locale->text('quotation_list');
1006 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1008 my @hidden_variables = map { "l_${_}" } @columns;
1009 push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
1010 transaction_description transdatefrom transdateto type vc employee_id salesman_id);
1012 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
1015 'ids' => { 'text' => '', },
1016 'transdate' => { 'text' => $locale->text('Date'), },
1017 'reqdate' => { 'text' => $locale->text('Required by'), },
1018 'id' => { 'text' => $locale->text('ID'), },
1019 'ordnumber' => { 'text' => $locale->text('Order'), },
1020 'quonumber' => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
1021 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1022 'netamount' => { 'text' => $locale->text('Amount'), },
1023 'tax' => { 'text' => $locale->text('Tax'), },
1024 'amount' => { 'text' => $locale->text('Total'), },
1025 'curr' => { 'text' => $locale->text('Curr'), },
1026 'employee' => { 'text' => $locale->text('Employee'), },
1027 'salesman' => { 'text' => $locale->text('Salesman'), },
1028 'shipvia' => { 'text' => $locale->text('Ship via'), },
1029 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
1030 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1031 'open' => { 'text' => $locale->text('Open'), },
1032 'delivered' => { 'text' => $locale->text('Delivered'), },
1033 'marge_total' => { 'text' => $locale->text('Ertrag'), },
1034 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), }
1037 foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia)) {
1038 $column_defs{$name}->{link} = $href . "&sort=$name";
1041 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
1043 $form->{"l_type"} = "Y";
1044 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1045 $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
1047 $report->set_columns(%column_defs);
1048 $report->set_column_order(@columns);
1050 $report->set_export_options('orders', @hidden_variables);
1052 $report->set_sort_indicator($form->{sort}, 1);
1055 if ($form->{customer}) {
1056 push @options, $locale->text('Customer') . " : $form->{customer}";
1058 if ($form->{vendor}) {
1059 push @options, $locale->text('Vendor') . " : $form->{vendor}";
1061 if ($form->{department}) {
1062 ($department) = split /--/, $form->{department};
1063 push @options, $locale->text('Department') . " : $department";
1065 if ($form->{ordnumber}) {
1066 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1068 if ($form->{notes}) {
1069 push @options, $locale->text('Notes') . " : $form->{notes}";
1071 if ($form->{transaction_description}) {
1072 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1074 if ($form->{transdatefrom}) {
1075 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1077 if ($form->{transdateto}) {
1078 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
1080 if ($form->{open}) {
1081 push @options, $locale->text('Open');
1083 if ($form->{closed}) {
1084 push @options, $locale->text('Closed');
1086 if ($form->{delivered}) {
1087 push @options, $locale->text('Delivered');
1089 if ($form->{notdelivered}) {
1090 push @options, $locale->text('Not delivered');
1093 $report->set_options('top_info_text' => join("\n", @options),
1094 'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
1095 'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
1096 'output_format' => 'HTML',
1097 'title' => $form->{title},
1098 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1100 $report->set_options_from_form();
1102 # add sort and escape callback, this one we use for the add sub
1103 $form->{callback} = $href .= "&sort=$form->{sort}";
1105 # escape callback for href
1106 $callback = $form->escape($href);
1108 my @subtotal_columns = qw(netamount amount marge_total marge_percent);
1110 my %totals = map { $_ => 0 } @subtotal_columns;
1111 my %subtotals = map { $_ => 0 } @subtotal_columns;
1115 my $edit_url = build_std_url('action=edit', 'type', 'vc');
1117 foreach $oe (@{ $form->{OE} }) {
1118 map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
1120 $oe->{tax} = $oe->{amount} - $oe->{netamount};
1121 $oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
1122 $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
1124 map { $subtotals{$_} += $oe->{$_};
1125 $totals{$_} += $oe->{$_} } @subtotal_columns;
1127 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1128 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1130 map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
1134 foreach my $column (@columns) {
1135 next if ($column eq 'ids');
1137 'data' => $oe->{$column},
1138 'align' => $column_alignment{$column},
1143 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
1144 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
1145 'valign' => 'center',
1146 'align' => 'center',
1149 $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1151 my $row_set = [ $row ];
1153 if (($form->{l_subtotal} eq 'Y')
1154 && (($idx == (scalar @{ $form->{OE} } - 1))
1155 || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
1156 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1159 $report->add_data($row_set);
1164 $report->add_separator();
1165 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1167 $report->generate_with_headers();
1169 $lxdebug->leave_sub();
1172 sub check_delivered_flag {
1173 $lxdebug->enter_sub();
1177 if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
1178 return $lxdebug->leave_sub();
1181 my $all_delivered = 0;
1183 foreach my $i (1 .. $form->{rowcount}) {
1184 next if (!$form->{"id_$i"});
1186 if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
1195 $form->{delivered} = 1 if $all_delivered;
1197 $lxdebug->leave_sub();
1200 sub save_and_close {
1201 $lxdebug->enter_sub();
1205 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1207 if ($form->{type} =~ /_order$/) {
1208 $form->isblank("transdate", $locale->text('Order Date missing!'));
1210 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1213 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1214 $form->{$idx} =~ s/^\s*//g;
1215 $form->{$idx} =~ s/\s*$//g;
1217 $msg = ucfirst $form->{vc};
1218 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1220 # $locale->text('Customer missing!');
1221 # $locale->text('Vendor missing!');
1223 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1224 if ($form->{currency} ne $form->{defaultcurrency});
1228 if($form->{payment_id}) {
1229 $payment_id = $form->{payment_id};
1232 # if the name changed get new values
1233 if (&check_name($form->{vc})) {
1234 if($form->{payment_id} eq "") {
1235 $form->{payment_id} = $payment_id;
1241 $form->{id} = 0 if $form->{saveasnew};
1243 # this is for the internal notes section for the [email] Subject
1244 if ($form->{type} =~ /_order$/) {
1245 if ($form->{type} eq 'sales_order') {
1246 $form->{label} = $locale->text('Sales Order');
1248 $numberfld = "sonumber";
1249 $ordnumber = "ordnumber";
1251 $form->{label} = $locale->text('Purchase Order');
1253 $numberfld = "ponumber";
1254 $ordnumber = "ordnumber";
1257 $err = $locale->text('Cannot save order!');
1259 check_delivered_flag();
1262 if ($form->{type} eq 'sales_quotation') {
1263 $form->{label} = $locale->text('Quotation');
1265 $numberfld = "sqnumber";
1266 $ordnumber = "quonumber";
1268 $form->{label} = $locale->text('Request for Quotation');
1270 $numberfld = "rfqnumber";
1271 $ordnumber = "quonumber";
1274 $err = $locale->text('Cannot save quotation!');
1278 # get new number in sequence if no number is given or if saveasnew was requested
1279 if (!$form->{$ordnumber} || $form->{saveasnew}) {
1280 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1285 $form->error($err) if (!OE->save(\%myconfig, \%$form));
1287 # saving the history
1288 if(!exists $form->{addition}) {
1289 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1290 $form->{addition} = "SAVED";
1291 $form->save_history($form->dbconnect(\%myconfig));
1293 # /saving the history
1295 $form->redirect($form->{label} . " $form->{$ordnumber} " .
1296 $locale->text('saved!'));
1298 $lxdebug->leave_sub();
1302 $lxdebug->enter_sub();
1306 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1309 if ($form->{type} =~ /_order$/) {
1310 $form->isblank("transdate", $locale->text('Order Date missing!'));
1312 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1315 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1316 $form->{$idx} =~ s/^\s*//g;
1317 $form->{$idx} =~ s/\s*$//g;
1319 $msg = ucfirst $form->{vc};
1320 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1322 # $locale->text('Customer missing!');
1323 # $locale->text('Vendor missing!');
1325 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1326 if ($form->{currency} ne $form->{defaultcurrency});
1330 if($form->{payment_id}) {
1331 $payment_id = $form->{payment_id};
1334 # if the name changed get new values
1335 if (&check_name($form->{vc})) {
1336 if($form->{payment_id} eq "") {
1337 $form->{payment_id} = $payment_id;
1343 $form->{id} = 0 if $form->{saveasnew};
1345 # this is for the internal notes section for the [email] Subject
1346 if ($form->{type} =~ /_order$/) {
1347 if ($form->{type} eq 'sales_order') {
1348 $form->{label} = $locale->text('Sales Order');
1350 $numberfld = "sonumber";
1351 $ordnumber = "ordnumber";
1353 $form->{label} = $locale->text('Purchase Order');
1355 $numberfld = "ponumber";
1356 $ordnumber = "ordnumber";
1359 $err = $locale->text('Cannot save order!');
1361 check_delivered_flag();
1364 if ($form->{type} eq 'sales_quotation') {
1365 $form->{label} = $locale->text('Quotation');
1367 $numberfld = "sqnumber";
1368 $ordnumber = "quonumber";
1370 $form->{label} = $locale->text('Request for Quotation');
1372 $numberfld = "rfqnumber";
1373 $ordnumber = "quonumber";
1376 $err = $locale->text('Cannot save quotation!');
1380 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1381 unless $form->{$ordnumber};
1385 OE->save(\%myconfig, \%$form);
1387 # saving the history
1388 if(!exists $form->{addition}) {
1389 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1390 $form->{addition} = "SAVED";
1391 $form->save_history($form->dbconnect(\%myconfig));
1393 # /saving the history
1395 $form->{simple_save} = 1;
1396 if(!$form->{print_and_save}) {
1397 set_headings("edit");
1401 $lxdebug->leave_sub();
1405 $lxdebug->enter_sub();
1411 if ($form->{type} =~ /_order$/) {
1412 $msg = $locale->text('Are you sure you want to delete Order Number');
1413 $ordnumber = 'ordnumber';
1415 $msg = $locale->text('Are you sure you want to delete Quotation Number');
1416 $ordnumber = 'quonumber';
1422 <form method=post action=$form->{script}>
1425 # delete action variable
1426 map { delete $form->{$_} } qw(action header);
1428 foreach $key (keys %$form) {
1429 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1430 $form->{$key} =~ s/\"/"/g;
1431 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1435 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1437 <h4>$msg $form->{$ordnumber}</h4>
1439 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1440 <input name=action class=submit type=submit value="|
1441 . $locale->text('Yes') . qq|">
1442 <button class=submit type=button onclick="history.back()">|
1443 . $locale->text('No') . qq|</button>
1450 $lxdebug->leave_sub();
1453 sub delete_order_quotation {
1454 $lxdebug->enter_sub();
1458 if ($form->{type} =~ /_order$/) {
1459 $msg = $locale->text('Order deleted!');
1460 $err = $locale->text('Cannot delete order!');
1462 $msg = $locale->text('Quotation deleted!');
1463 $err = $locale->text('Cannot delete quotation!');
1465 if (OE->delete(\%myconfig, \%$form, $spool)){
1466 # saving the history
1467 if(!exists $form->{addition}) {
1468 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1469 $form->{addition} = "DELETED";
1470 $form->save_history($form->dbconnect(\%myconfig));
1472 # /saving the history
1478 $lxdebug->leave_sub();
1482 $lxdebug->enter_sub();
1485 $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
1487 $form->{old_employee_id} = $form->{employee_id};
1488 $form->{old_salesman_id} = $form->{salesman_id};
1490 if ($form->{type} =~ /_order$/) {
1492 # these checks only apply if the items don't bring their own ordnumbers/transdates.
1493 # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1494 $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1495 if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1496 $form->isblank("transdate", $locale->text('Order Date missing!'))
1497 if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1499 # also copy deliverydate from the order
1500 $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1501 $form->{orddate} = $form->{transdate};
1503 $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1504 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1505 $form->{ordnumber} = "";
1506 $form->{quodate} = $form->{transdate};
1509 $payment_id = $form->{payment_id} if $form->{payment_id};
1511 # if the name changed get new values
1512 if (&check_name($form->{vc})) {
1513 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1518 $form->{cp_id} *= 1;
1520 for $i (1 .. $form->{rowcount}) {
1521 for (qw(ship qty sellprice listprice basefactor)) {
1522 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1526 if ( $form->{type} =~ /_order/
1527 && $form->{currency} ne $form->{defaultcurrency}) {
1529 # check if we need a new exchangerate
1530 $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1532 $orddate = $form->current_date(\%myconfig);
1533 $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1535 if (!$exchangerate) {
1536 &backorder_exchangerate($orddate, $buysell);
1541 # close orders/quotations
1542 $form->{closed} = 1;
1544 # save order if one ordnumber has been given
1545 # if not it's most likely a collective order, which can't be saved back
1546 # so they just have to be closed
1547 if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
1548 OE->close_order(\%myconfig, \%$form) if ($form->{id});
1550 OE->close_orders(\%myconfig, \%$form);
1553 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1554 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1557 $form->{closed} = 0;
1558 $form->{rowcount}--;
1559 $form->{shipto} = 1;
1561 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1563 if ($form->{type} =~ /_order$/) {
1564 $form->{exchangerate} = $exchangerate;
1568 if ( $form->{type} eq 'purchase_order'
1569 || $form->{type} eq 'request_quotation') {
1570 $form->{title} = $locale->text('Add Vendor Invoice');
1571 $form->{script} = 'ir.pl';
1576 if ( $form->{type} eq 'sales_order'
1577 || $form->{type} eq 'sales_quotation') {
1578 $form->{title} = $locale->text('Add Sales Invoice');
1579 $form->{script} = 'is.pl';
1584 # bo creates the id, reset it
1585 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1586 $form->{ $form->{vc} } =~ s/--.*//g;
1587 $form->{type} = "invoice";
1590 $locale = new Locale "$myconfig{countrycode}", "$script";
1592 require "bin/mozilla/$form->{script}";
1594 map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1596 $currency = $form->{currency};
1599 $form->{currency} = $currency;
1600 $form->{exchangerate} = "";
1601 $form->{forex} = "";
1602 $form->{exchangerate} = $exchangerate
1606 $form->check_exchangerate(
1607 \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1610 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1615 for $i (1 .. $form->{rowcount}) {
1616 $form->{"discount_$i"} =
1617 $form->format_amount(\%myconfig, $form->{"discount_$i"});
1619 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1621 $decimalplaces = ($dec > 2) ? $dec : 2;
1623 # copy delivery date from reqdate for order -> invoice conversion
1624 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1625 unless $form->{"deliverydate_$i"};
1627 $form->{"sellprice_$i"} =
1628 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1631 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1632 $dec_qty = length $dec_qty;
1634 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1636 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1637 qw(partnumber description unit);
1643 $lxdebug->leave_sub();
1646 sub backorder_exchangerate {
1647 $lxdebug->enter_sub();
1651 my ($orddate, $buysell) = @_;
1658 <form method=post action=$form->{script}>
1661 # delete action variable
1662 map { delete $form->{$_} } qw(action header exchangerate);
1664 foreach $key (keys %$form) {
1665 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1666 $form->{$key} =~ s/\"/"/g;
1667 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1670 $form->{title} = $locale->text('Add Exchangerate');
1674 <input type=hidden name=exchangeratedate value=$orddate>
1675 <input type=hidden name=buysell value=$buysell>
1678 <tr><th class=listtop>$form->{title}</th></tr>
1679 <tr height="5"></tr>
1684 <th align=right>| . $locale->text('Currency') . qq|</th>
1685 <td>$form->{currency}</td>
1688 <th align=right>| . $locale->text('Date') . qq|</th>
1692 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1693 <td><input name=exchangerate size=11></td>
1703 <input type=hidden name=nextsub value=save_exchangerate>
1705 <input name=action class=submit type=submit value="|
1706 . $locale->text('Continue') . qq|">
1714 $lxdebug->leave_sub();
1717 sub save_exchangerate {
1718 $lxdebug->enter_sub();
1720 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1721 $form->{exchangerate} =
1722 $form->parse_amount(\%myconfig, $form->{exchangerate});
1723 $form->save_exchangerate(\%myconfig, $form->{currency},
1724 $form->{exchangeratedate},
1725 $form->{exchangerate}, $form->{buysell});
1729 $lxdebug->leave_sub();
1732 sub create_backorder {
1733 $lxdebug->enter_sub();
1735 $form->{shipped} = 1;
1737 # figure out if we need to create a backorder
1738 # items aren't saved if qty != 0
1740 for $i (1 .. $form->{rowcount}) {
1741 $totalqty += $qty = $form->{"qty_$i"};
1742 $totalship += $ship = $form->{"ship_$i"};
1744 $form->{"qty_$i"} = $qty - $ship;
1747 if ($totalship == 0) {
1748 map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1749 $form->{ordtotal} = 0;
1750 $form->{shipped} = 0;
1754 if ($totalqty == $totalship) {
1755 map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1756 $form->{ordtotal} = 0;
1761 qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1764 for $i (1 .. $form->{rowcount}) {
1766 $form->{"${_}_$i"} =
1767 $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1768 } qw(sellprice discount);
1773 OE->save(\%myconfig, \%$form);
1775 # rebuild rows for invoice
1779 for $i (1 .. $form->{rowcount}) {
1780 $form->{"qty_$i"} = $form->{"ship_$i"};
1782 if ($form->{"qty_$i"}) {
1785 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1790 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1791 $form->{rowcount} = $count;
1793 $lxdebug->leave_sub();
1797 $lxdebug->enter_sub();
1801 $form->{saveasnew} = 1;
1802 $form->{closed} = 0;
1803 map { delete $form->{$_} } qw(printed emailed queued);
1805 # Let Lx-Office assign a new order number if the user hasn't changed the
1806 # previous one. If it has been changed manually then use it as-is.
1807 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1808 $form->{$idx} =~ s/^\s*//g;
1809 $form->{$idx} =~ s/\s*$//g;
1810 if ($form->{saved_xyznumber} &&
1811 ($form->{saved_xyznumber} eq $form->{$idx})) {
1812 delete($form->{$idx});
1817 $lxdebug->leave_sub();
1820 sub check_for_direct_delivery_yes {
1821 $lxdebug->enter_sub();
1825 $form->{direct_delivery_checked} = 1;
1826 delete @{$form}{grep /^shipto/, keys %{ $form }};
1827 map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1828 $form->{shipto} = 1;
1830 $lxdebug->leave_sub();
1833 sub check_for_direct_delivery_no {
1834 $lxdebug->enter_sub();
1838 $form->{direct_delivery_checked} = 1;
1839 delete @{$form}{grep /^shipto/, keys %{ $form }};
1842 $lxdebug->leave_sub();
1845 sub check_for_direct_delivery {
1846 $lxdebug->enter_sub();
1850 if ($form->{direct_delivery_checked}
1851 || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1852 $lxdebug->leave_sub();
1856 if ($form->{shipto_id}) {
1857 Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1860 map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1863 delete $form->{action};
1864 $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
1867 print $form->parse_html_template("oe/check_for_direct_delivery");
1869 $lxdebug->leave_sub();
1874 sub purchase_order {
1875 $lxdebug->enter_sub();
1878 $auth->assert('purchase_order_edit');
1880 if ($form->{type} eq 'sales_order') {
1881 check_for_direct_delivery();
1884 if ( $form->{type} eq 'sales_quotation'
1885 || $form->{type} eq 'request_quotation') {
1886 OE->close_order(\%myconfig, \%$form);
1889 if ($form->{type} =~ /^sales_/) {
1890 delete($form->{ordnumber});
1893 $form->{cp_id} *= 1;
1895 $form->{title} = $locale->text('Add Purchase Order');
1896 $form->{vc} = "vendor";
1897 $form->{type} = "purchase_order";
1901 $lxdebug->leave_sub();
1905 $lxdebug->enter_sub();
1908 $auth->assert('sales_order_edit');
1910 if ( $form->{type} eq 'sales_quotation'
1911 || $form->{type} eq 'request_quotation') {
1912 OE->close_order(\%myconfig, $form);
1915 if ($form->{type} eq "purchase_order") {
1916 delete($form->{ordnumber});
1919 $form->{cp_id} *= 1;
1921 $form->{title} = $locale->text('Add Sales Order');
1922 $form->{vc} = "customer";
1923 $form->{type} = "sales_order";
1927 $lxdebug->leave_sub();
1931 $lxdebug->enter_sub();
1934 $auth->assert('purchase_order_edit | sales_order_edit');
1936 $form->{transdate} = $form->current_date(\%myconfig);
1937 delete $form->{duedate};
1939 $form->{closed} = 0;
1941 $form->{old_employee_id} = $form->{employee_id};
1942 $form->{old_salesman_id} = $form->{salesman_id};
1945 map { delete $form->{$_} }
1946 qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
1948 for $i (1 .. $form->{rowcount}) {
1949 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1950 $form->{"${_}_${i}"})
1951 if ($form->{"${_}_${i}"}) }
1952 qw(ship qty sellprice listprice basefactor));
1960 for $i (1 .. $form->{rowcount} - 1) {
1961 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1962 qw(partnumber description unit);
1967 $lxdebug->leave_sub();
1970 sub delivery_order {
1971 $lxdebug->enter_sub();
1973 if ($form->{type} =~ /^sales/) {
1974 $auth->assert('sales_delivery_order_edit');
1976 $form->{vc} = 'customer';
1977 $form->{type} = 'sales_delivery_order';
1980 $auth->assert('purchase_delivery_order_edit');
1982 $form->{vc} = 'vendor';
1983 $form->{type} = 'purchase_delivery_order';
1986 require "bin/mozilla/do.pl";
1988 $form->{cp_id} *= 1;
1989 $form->{transdate} = $form->current_date(\%myconfig);
1990 delete $form->{duedate};
1992 $form->{closed} = 0;
1994 $form->{old_employee_id} = $form->{employee_id};
1995 $form->{old_salesman_id} = $form->{salesman_id};
1998 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal);
2000 for $i (1 .. $form->{rowcount}) {
2001 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
2004 my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
2010 map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;
2014 $lxdebug->leave_sub();
2018 $lxdebug->enter_sub();
2022 $form->{print_and_save} = 1;
2026 my $saved_form = save_form();
2030 restore_form($saved_form, 0, qw(id ordnumber quonumber));
2034 $lxdebug->leave_sub();
2038 call_sub($form->{yes_nextsub});
2042 call_sub($form->{no_nextsub});
2045 ######################################################################################################
2047 # ###############################################################################################
2049 $lxdebug->enter_sub();
2053 retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
2055 $form->{"taxaccounts"} =~ s/\s*$//;
2056 $form->{"taxaccounts"} =~ s/^\s*//;
2057 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
2058 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
2060 $form->{"taxaccounts"} = "";
2062 for my $i (1 .. $form->{"rowcount"}) {
2063 IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
2066 $form->{rowcount}++;
2067 $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
2069 $form->language_payment(\%myconfig);
2071 Common::webdav_folder($form) if ($webdav);
2076 display_row($form->{rowcount}) if $form->{rowcount};
2080 $lxdebug->leave_sub();
2083 sub report_for_todo_list {
2084 $lxdebug->enter_sub();
2086 my $quotations = OE->transactions_for_todo_list();
2089 if (@{ $quotations }) {
2090 my $edit_url = build_std_url('script=oe.pl', 'action=edit', 'type=sales_quotation', 'vc=customer');
2092 $content = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
2093 'edit_url' => $edit_url });
2096 $lxdebug->leave_sub();