1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger, Accounting
9 # Copyright (c) 1998-2003
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
32 #======================================================================
34 use POSIX qw(strftime);
42 use SL::ReportGenerator;
43 use List::Util qw(max reduce sum);
45 require "bin/mozilla/io.pl";
46 require "bin/mozilla/arap.pl";
47 require "bin/mozilla/reportgenerator.pl";
54 # $locale->text('Edit the purchase_order');
55 # $locale->text('Edit the sales_order');
56 # $locale->text('Edit the request_quotation');
57 # $locale->text('Edit the sales_quotation');
59 # $locale->text('Workflow purchase_order');
60 # $locale->text('Workflow sales_order');
61 # $locale->text('Workflow request_quotation');
62 # $locale->text('Workflow sales_quotation');
65 'sales_order' => 'sales_order_edit',
66 'purchase_order' => 'purchase_order_edit',
67 'request_quotation' => 'request_quotation_edit',
68 'sales_quotation' => 'sales_quotation_edit',
72 my $right = $oe_access_map->{$form->{type}};
73 $right ||= 'DOES_NOT_EXIST';
75 $auth->assert($right);
79 $lxdebug->enter_sub();
85 if ($form->{type} eq 'purchase_order') {
86 $form->{title} = $action eq "edit" ?
87 $locale->text('Edit Purchase Order') :
88 $locale->text('Add Purchase Order');
89 $form->{heading} = $locale->text('Purchase Order');
90 $form->{vc} = 'vendor';
92 if ($form->{type} eq 'sales_order') {
93 $form->{title} = $action eq "edit" ?
94 $locale->text('Edit Sales Order') :
95 $locale->text('Add Sales Order');
96 $form->{heading} = $locale->text('Sales Order');
97 $form->{vc} = 'customer';
99 if ($form->{type} eq 'request_quotation') {
100 $form->{title} = $action eq "edit" ?
101 $locale->text('Edit Request for Quotation') :
102 $locale->text('Add Request for Quotation');
103 $form->{heading} = $locale->text('Request for Quotation');
104 $form->{vc} = 'vendor';
106 if ($form->{type} eq 'sales_quotation') {
107 $form->{title} = $action eq "edit" ?
108 $locale->text('Edit Quotation') :
109 $locale->text('Add Quotation');
110 $form->{heading} = $locale->text('Quotation');
111 $form->{vc} = 'customer';
114 $lxdebug->leave_sub();
118 $lxdebug->enter_sub();
125 "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
126 unless $form->{callback};
132 $lxdebug->leave_sub();
136 $lxdebug->enter_sub();
140 # show history button
141 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
142 #/show hhistory button
144 $form->{simple_save} = 0;
146 set_headings("edit");
148 # editing without stuff to edit? try adding it first
149 if ($form->{rowcount} && !$form->{print_and_save}) {
150 map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
154 undef $form->{rowcount};
156 $lxdebug->leave_sub();
159 } elsif (!$form->{id}) {
161 $lxdebug->leave_sub();
165 if ($form->{print_and_save}) {
166 $form->{action} = "print";
167 $form->{resubmit} = 1;
168 $language_id = $form->{language_id};
169 $printer_id = $form->{printer_id};
172 set_headings("edit");
176 $form->{rowcount} = 0;
177 foreach $ref (@{ $form->{form_details} }) {
179 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{$ref};
184 if ($form->{print_and_save}) {
185 $form->{language_id} = $language_id;
186 $form->{printer_id} = $printer_id;
191 $lxdebug->leave_sub();
195 $lxdebug->enter_sub();
199 # get customer/vendor
200 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
202 # retrieve order/quotation
203 $form->{webdav} = $webdav;
204 $form->{jsscript} = 1;
206 my $editing = $form->{id};
208 OE->retrieve(\%myconfig, \%$form);
210 # if multiple rowcounts (== collective order) then check if the
211 # there were more than one customer (in that case OE::retrieve removes
212 # the content from the field)
213 $form->error($locale->text('Collective Orders only work for orders from one customer!'))
214 if $form->{rowcount} && $form->{type} eq 'sales_order'
215 && defined $form->{customer} && $form->{customer} eq '';
217 $form->{"$form->{vc}_id"} ||= $form->{"all_$form->{vc}"}->[0]->{id} if $form->{"all_$form->{vc}"};
219 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
220 $form->{shipto} = 1 if $form->{id};
222 # get customer / vendor
223 IR->get_vendor(\%myconfig, \%$form) if $form->{type} =~ /(purchase_order|request_quotation)/;
224 IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
226 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
227 $form->restore_vars(qw(taxincluded)) if $form->{id};
228 $form->restore_vars(qw(salesman_id)) if $editing;
229 $form->{forex} = $form->{exchangerate};
230 $form->{employee} = "$form->{employee}--$form->{employee_id}";
232 # build vendor/customer drop down comatibility... don't ask
233 if (@{ $form->{"all_$form->{vc}"} }) {
234 $form->{"select$form->{vc}"} = 1;
235 $form->{$form->{vc}} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
238 $form->{"old$form->{vc}"} = $form->{$form->{vc}};
239 $form->{"old$form->{vc}"} .= qq|--$form->{"$form->{vc}_id"}| unless ($form->{"old$form->{vc}"} =~ m/--\d+$/);
241 $lxdebug->leave_sub();
245 $lxdebug->enter_sub();
249 $form->{formname} ||= $form->{type};
251 for my $i (1 .. $form->{rowcount}) {
252 $form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
253 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($form->{id} ? 100 : 1));
254 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
255 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
258 $lxdebug->leave_sub();
262 $lxdebug->enter_sub();
267 # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
270 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
272 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
273 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
275 # use JavaScript Calendar or not
276 $form->{jsscript} = 1;
277 $TMPL_VAR{button1} = qq|
278 <td nowrap><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
279 <input type=button name=transdate id="trigger1" value=| . $locale->text('button') . qq|></td>
281 $TMPL_VAR{button2} = qq|
282 <td nowrap width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
283 <input type=button name=reqdate name=reqdate id="trigger2" value=| . $locale->text('button') . qq|></td>
286 $TMPL_VAR{jsscript} = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
288 # openclosed checkboxes
290 push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
291 $form->{"delivered"} ? "checked" : "", $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
292 push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
293 $form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
294 $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
296 # set option selected
297 foreach $item ($form->{vc}, 'currency', 'department', ($form->{vc} eq "customer" ? 'customer' : 'vendor')) {
298 $form->{"select$item"} =~ s/ selected//;
299 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
302 #quote select[customer|vendor] Bug 133
303 $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
305 #substitute \n and \r to \s (bug 543)
306 $form->{"select$form->{vc}"} =~ s/[\n\r]/ /g;
308 my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
310 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
311 $form->get_lists("contacts" => "ALL_CONTACTS",
312 "shipto" => "ALL_SHIPTO",
313 "projects" => { "key" => "ALL_PROJECTS",
315 "old_id" => \@old_project_ids },
316 "employees" => "ALL_EMPLOYEES",
317 "salesmen" => "ALL_SALESMEN",
318 "taxzones" => "ALL_TAXZONES",
319 "payments" => "ALL_PAYMENTS",
320 "currencies" => "ALL_CURRENCIES",
321 "departments" => "ALL_DEPARTMENTS",
322 $vc => { key => "ALL_" . uc($vc),
323 limit => $myconfig{vclimit} + 1 },
324 "price_factors" => "ALL_PRICE_FACTORS");
327 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
328 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
329 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
330 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
333 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
334 $TMPL_VAR{vclimit} = $myconfig{vclimit};
335 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
336 push @custom_hiddens, "$form->{vc}_id";
337 push @custom_hiddens, "old$form->{vc}";
338 push @custom_hiddens, "select$form->{vc}";
340 # currencies and exchangerate
341 @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
342 %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
343 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
344 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
345 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
346 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
347 push @custom_hiddens, "forex";
348 push @custom_hiddens, "exchangerate" if $form->{forex};
351 $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
352 $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
355 $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
357 push @custom_hiddens, "ordnumber" if $form->{type} =~ /_quotation$/;
358 push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
360 $credittext = $locale->text('Credit Limit exceeded!!!');
362 my $follow_up_vc = $form->{ $form->{vc} eq 'customer' ? 'customer' : 'vendor' };
363 $follow_up_vc =~ s/--.*?//;
364 $TMPL_VAR{follow_up_trans_info} = ($form->{type} =~ /_quotation$/ ? $form->{quonumber} : $form->{ordnumber}) . " ($follow_up_vc)";
367 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
369 if (scalar @{ $follow_ups }) {
370 $TMPL_VAR{num_follow_ups} = scalar @{ $follow_ups };
371 $TMPL_VAR{num_due_follow_ups} = sum map { $_->{due} * 1 } @{ $follow_ups };
375 $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
376 : ($form->{resubmit}) ? "document.oe.submit()"
377 : ($creditwarning) ? "alert('$credittext')"
380 $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
381 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
382 $TMPL_VAR{onload} = $onload;
384 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
385 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
386 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
390 $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
391 qw(id action type vc formname media format proforma queued printed emailed
392 title creditlimit creditremaining tradediscount business
393 max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
394 shiptocity shiptocountry shiptocontact shiptophone shiptofax
395 shiptodepartment_1 shiptodepartment_2 shiptoemail
396 message email subject cc bcc taxpart taxservice taxaccounts cursor_fokus),
398 map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ]; # deleted: discount
402 is_sales => scalar ($form->{type} =~ /^sales_/), # these vars are exported, so that the template
403 is_order => scalar ($form->{type} =~ /_order$/), # may determine what to show
404 is_sales_quo => scalar ($form->{type} =~ /sales_quotation$/),
405 is_req_quo => scalar ($form->{type} =~ /request_quotation$/),
406 is_sales_ord => scalar ($form->{type} =~ /sales_order$/),
407 is_pur_ord => scalar ($form->{type} =~ /purchase_order$/),
410 print $form->parse_html_template("oe/form_header", { %TMPL_VAR });
412 $lxdebug->leave_sub();
416 $lxdebug->enter_sub();
420 $form->{invtotal} = $form->{invsubtotal};
422 $rows = max 2, $form->numtextrows($form->{notes}, 25, 8);
423 $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
424 $rows = max $rows, $introws;
426 $TMPL_VAR{notes} = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>| . H($form->{notes}) . qq|</textarea>|;
427 $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$introws cols=35 wrap=soft>| . H($form->{intnotes}) . qq|</textarea>|;
429 if (!$form->{taxincluded}) {
431 foreach $item (split / /, $form->{taxaccounts}) {
432 if ($form->{"${item}_base"}) {
433 $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
434 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
436 $TMPL_VAR{tax} .= qq|
438 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
439 <td align=right>$form->{"${item}_total"}</td>
444 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
447 foreach $item (split / /, $form->{taxaccounts}) {
448 if ($form->{"${item}_base"}) {
449 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
450 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
451 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
452 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
454 $TMPL_VAR{tax} .= qq|
456 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
457 <td align=right>$form->{"${item}_total"}</td>
460 <th align=right>Nettobetrag</th>
461 <td align=right>$form->{"${item}_netto"}</td>
467 $form->{oldinvtotal} = $form->{invtotal};
469 print $form->parse_html_template("oe/form_footer", {
472 print_options => print_options(inline => 1),
473 label_edit => $locale->text("Edit the $form->{type}"),
474 label_workflow => $locale->text("Workflow $form->{type}"),
477 $lxdebug->leave_sub();
481 $lxdebug->enter_sub();
483 my ($recursive_call) = shift;
487 set_headings($form->{"id"} ? "edit" : "add");
489 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
492 $payment_id = $form->{payment_id} if $form->{payment_id};
494 &check_name($form->{vc});
496 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
499 $buysell = 'sell' if ($form->{vc} eq 'vendor');
500 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
501 $form->{exchangerate} = $form->{forex} if $form->{forex};
503 $exchangerate = $form->{exchangerate} || 1;
505 ##################### process items ######################################
507 $i = $form->{rowcount};
508 if ( ($form->{"partnumber_$i"} eq "")
509 && ($form->{"description_$i"} eq "")
510 && ($form->{"partsgroup_$i"} eq "")) {
512 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
517 if ($form->{type} =~ /^sales/) {
518 IS->retrieve_item(\%myconfig, \%$form);
520 IR->retrieve_item(\%myconfig, \%$form);
523 my $rows = scalar @{ $form->{item_list} };
525 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
528 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
537 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
538 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
539 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
540 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
541 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
543 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
545 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
546 $decimalplaces = max 2, length $1;
549 $form->{"sellprice_$i"} = $sellprice;
551 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
552 $form->{"sellprice_$i"} /= $exchangerate; # if there is an exchange rate adjust sellprice
555 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
556 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
557 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
558 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
560 $form->{creditremaining} -= $amount;
562 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
563 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
565 # get pricegroups for parts
566 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
568 # build up html code for prices_$i
575 # ok, so this is a new part
576 # ask if it is a part or service item
578 if ( $form->{"partsgroup_$i"}
579 && ($form->{"partsnumber_$i"} eq "")
580 && ($form->{"description_$i"} eq "")) {
582 $form->{"discount_$i"} = "";
586 $form->{"id_$i"} = 0;
591 ##################### process items ######################################
594 $lxdebug->leave_sub();
598 $lxdebug->enter_sub();
602 if ($form->{type} eq 'purchase_order') {
603 $form->{title} = $locale->text('Purchase Orders');
604 $form->{vc} = 'vendor';
605 $ordlabel = $locale->text('Order Number');
606 $ordnumber = 'ordnumber';
607 $employee = $locale->text('Employee');
610 if ($form->{type} eq 'request_quotation') {
611 $form->{title} = $locale->text('Request for Quotations');
612 $form->{vc} = 'vendor';
613 $ordlabel = $locale->text('RFQ Number');
614 $ordnumber = 'quonumber';
615 $employee = $locale->text('Employee');
618 if ($form->{type} eq 'sales_order') {
619 $form->{title} = $locale->text('Sales Orders');
620 $form->{vc} = 'customer';
621 $ordlabel = $locale->text('Order Number');
622 $ordnumber = 'ordnumber';
623 $employee = $locale->text('Employee');
626 if ($form->{type} eq 'sales_quotation') {
627 $form->{title} = $locale->text('Quotations');
628 $form->{vc} = 'customer';
629 $ordlabel = $locale->text('Quotation Number');
630 $ordnumber = 'quonumber';
631 $employee = $locale->text('Employee');
634 # setup vendor / customer selection
635 $form->all_vc(\%myconfig, $form->{vc},
636 ($form->{vc} eq 'customer') ? "AR" : "AP");
639 if (@{ $form->{all_departments} }) {
640 $form->{selectdepartment} = "<option>\n";
643 $form->{selectdepartment} .=
644 "<option>$_->{description}--$_->{id}</option>\n"
645 } (@{ $form->{all_departments} });
650 <th align=right nowrap>| . $locale->text('Department') . qq|</th>
651 <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
653 | if $form->{selectdepartment};
656 if (($form->{"type"} eq "sales_order") ||
657 ($form->{"type"} eq "purchase_order")) {
660 <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
661 <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
662 <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
663 <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
668 # use JavaScript Calendar or not
669 $form->{jsscript} = 1;
673 <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
674 <input type=button name=transdatefrom id="trigger3" value=|
675 . $locale->text('button') . qq|></td>
678 <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
679 <input type=button name=transdateto name=transdateto id="trigger4" value=|
680 . $locale->text('button') . qq|></td>
685 Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
686 "transdateto", "BL", "trigger4");
688 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
690 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
692 "employees" => "ALL_EMPLOYEES",
693 "salesmen" => "ALL_SALESMEN",
694 $vc => "ALL_" . uc($vc)
699 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
700 push(@values, $item->{"id"});
701 $labels{$item->{"id"}} = $item->{"projectnumber"};
704 NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
705 '-labels' => \%labels));
710 foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
711 push(@values, $item->{"id"});
712 $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
716 my %labels_salesmen = ();
717 my @values_salesmen = ('');
718 foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
719 push(@values_salesmen, $item->{"id"});
720 $labels_salesmen{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
723 my $employee_block = qq|
725 <th align="right">| . $locale->text('Employee') . qq|</th>
726 <td>| . NTI($cgi->popup_menu('-name' => 'employee_id', '-values' => \@values, '-labels' => \%labels)) . qq|</td>
729 <th align="right">| . $locale->text('Salesman') . qq|</th>
731 NTI($cgi->popup_menu('-name' => 'salesman_id',
732 '-values' => \@values_salesmen,
733 '-labels' => \%labels_salesmen)) . qq|
740 foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
741 push(@values, $item->{name}.qq|--|.$item->{"id"});
742 $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
745 my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
747 $myconfig{vclimit} <= scalar(@values)
748 ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">|
749 : NTI($cgi->popup_menu('-name' => "$form->{vc}",
750 '-default' => $form->{"old$form->{vc}"},
751 '-onChange' => 'document.getElementById(\'update_button\').click();',
752 '-values' => \@values,
753 '-labels' => \%labels));
759 <form method=post action=$form->{script}>
763 <th class=listtop>$form->{title}</th>
770 <th align=right>$vc_label</th>
771 <td colspan=3>$vc</td>
775 <th align=right>$ordlabel</th>
776 <td colspan=3><input name="$ordnumber" size=20></td>
780 <th align="right">| . $locale->text('Transaction description') . qq|</th>
781 <td colspan="3"><input name="transaction_description" size=20></td>
784 <th align="right">| . $locale->text("Project Number") . qq|</th>
785 <td colspan="3">$projectnumber</td>
788 <th align=right>| . $locale->text('From') . qq|</th> $button1
789 <th align=right>| . $locale->text('Bis') . qq|</th> $button2
791 <input type=hidden name=sort value=transdate>
793 <th align=right>| . $locale->text('Include in Report') . qq|</th>
797 <td><input type="checkbox" name="open" value="1" id="open" checked> <label for="open">| . $locale->text("Open") . qq|</td>
798 <td><input type="checkbox" name="closed" value="1" id="closed"> <label for="closed">| . $locale->text("Closed") . qq|</td>
802 <td><input name="l_id" class=checkbox type=checkbox value=Y> | . $locale->text('ID') . qq|</td>
803 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
806 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Date') . qq|</td>
807 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Required by') . qq|</td>
810 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
811 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
813 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> | . $locale->text('Ship via') . qq|</td>
816 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> | . $locale->text('Amount') . qq|</td>
817 <td><input name="l_tax" class=checkbox type=checkbox value=Y> | . $locale->text('Tax') . qq|</td>
818 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> | . $locale->text('Total') . qq|</td>
821 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag') . qq|</td>
822 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag prozentual') . qq|</td>
825 <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> | . $locale->text('Project Number') . qq|</td>
826 <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> | . $locale->text('Transaction description') . qq|</td>
829 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> | . $locale->text('Subtotal') . qq|</td>
830 <td><input name="l_salesman" class="checkbox" type="checkbox" value="Y"> | . $locale->text('Salesman') . qq|</td>
838 <tr><td colspan=4><hr size=3 noshade></td></tr>
844 <input type=hidden name=nextsub value=orders>
845 <input type=hidden name=vc value=$form->{vc}>
846 <input type=hidden name=type value=$form->{type}>
848 <input class=submit type=submit name=action value="|
849 . $locale->text('Continue') . qq|">
856 $lxdebug->leave_sub();
859 sub create_subtotal_row {
860 $lxdebug->enter_sub();
862 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
864 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
866 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
868 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
870 map { $totals->{$_} = 0 } @{ $subtotal_columns };
872 $lxdebug->leave_sub();
878 $lxdebug->enter_sub();
882 $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
884 ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
886 report_generator_set_default_sort('transdate', 1);
888 OE->transactions(\%myconfig, \%$form);
890 $form->{rowcount} = scalar @{ $form->{OE} };
893 "transdate", "reqdate",
899 "shipvia", "globalprojectnumber",
900 "transaction_description", "open",
901 "delivered", "marge_total", "marge_percent"
904 # only show checkboxes if gotten here via sales_order form.
905 my $allow_multiple_orders = $form->{type} eq 'sales_order';
906 if ($allow_multiple_orders) {
907 unshift @columns, "ids";
910 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
911 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
913 my $attachment_basename;
914 if ($form->{vc} eq 'vendor') {
915 if ($form->{type} eq 'purchase_order') {
916 $form->{title} = $locale->text('Purchase Orders');
917 $attachment_basename = $locale->text('purchase_order_list');
919 $form->{title} = $locale->text('Request for Quotations');
920 $attachment_basename = $locale->text('rfq_list');
924 if ($form->{type} eq 'sales_order') {
925 $form->{title} = $locale->text('Sales Orders');
926 $attachment_basename = $locale->text('sales_order_list');
928 $form->{title} = $locale->text('Quotations');
929 $attachment_basename = $locale->text('quotation_list');
933 my $report = SL::ReportGenerator->new(\%myconfig, $form);
935 my @hidden_variables = map { "l_${_}" } @columns;
936 push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
937 transaction_description transdatefrom transdateto type vc employee_id salesman_id);
939 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
942 'ids' => { 'text' => '', },
943 'transdate' => { 'text' => $locale->text('Date'), },
944 'reqdate' => { 'text' => $locale->text('Required by'), },
945 'id' => { 'text' => $locale->text('ID'), },
946 'ordnumber' => { 'text' => $locale->text('Order'), },
947 'quonumber' => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
948 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
949 'netamount' => { 'text' => $locale->text('Amount'), },
950 'tax' => { 'text' => $locale->text('Tax'), },
951 'amount' => { 'text' => $locale->text('Total'), },
952 'curr' => { 'text' => $locale->text('Curr'), },
953 'employee' => { 'text' => $locale->text('Employee'), },
954 'salesman' => { 'text' => $locale->text('Salesman'), },
955 'shipvia' => { 'text' => $locale->text('Ship via'), },
956 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
957 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
958 'open' => { 'text' => $locale->text('Open'), },
959 'delivered' => { 'text' => $locale->text('Delivered'), },
960 'marge_total' => { 'text' => $locale->text('Ertrag'), },
961 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), }
964 foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia)) {
965 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
966 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
969 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
971 $form->{"l_type"} = "Y";
972 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
973 $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
975 $report->set_columns(%column_defs);
976 $report->set_column_order(@columns);
977 $report->set_export_options('orders', @hidden_variables);
978 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
981 my ($department) = split m/--/, $form->{department};
983 push @options, $locale->text('Customer') . " : $form->{customer}" if $form->{customer};
984 push @options, $locale->text('Vendor') . " : $form->{vendor}" if $form->{vendor};
985 push @options, $locale->text('Department') . " : $department" if $form->{department};
986 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if $form->{ordnumber};
987 push @options, $locale->text('Notes') . " : $form->{notes}" if $form->{notes};
988 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if $form->{transaction_description};
989 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if $form->{transdatefrom};
990 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if $form->{transdateto};
991 push @options, $locale->text('Open') if $form->{open};
992 push @options, $locale->text('Closed') if $form->{closed};
993 push @options, $locale->text('Delivered') if $form->{delivered};
994 push @options, $locale->text('Not delivered') if $form->{notdelivered};
996 $report->set_options('top_info_text' => join("\n", @options),
997 'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
998 'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
999 'output_format' => 'HTML',
1000 'title' => $form->{title},
1001 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1003 $report->set_options_from_form();
1005 # add sort and escape callback, this one we use for the add sub
1006 $form->{callback} = $href .= "&sort=$form->{sort}";
1008 # escape callback for href
1009 $callback = $form->escape($href);
1011 my @subtotal_columns = qw(netamount amount marge_total marge_percent);
1013 my %totals = map { $_ => 0 } @subtotal_columns;
1014 my %subtotals = map { $_ => 0 } @subtotal_columns;
1018 my $edit_url = build_std_url('action=edit', 'type', 'vc');
1020 foreach $oe (@{ $form->{OE} }) {
1021 map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
1023 $oe->{tax} = $oe->{amount} - $oe->{netamount};
1024 $oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
1025 $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
1027 map { $subtotals{$_} += $oe->{$_};
1028 $totals{$_} += $oe->{$_} } @subtotal_columns;
1030 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1031 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1033 map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
1037 foreach my $column (@columns) {
1038 next if ($column eq 'ids');
1040 'data' => $oe->{$column},
1041 'align' => $column_alignment{$column},
1046 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
1047 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
1048 'valign' => 'center',
1049 'align' => 'center',
1052 $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1054 my $row_set = [ $row ];
1056 if (($form->{l_subtotal} eq 'Y')
1057 && (($idx == (scalar @{ $form->{OE} } - 1))
1058 || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
1059 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1062 $report->add_data($row_set);
1067 $report->add_separator();
1068 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1070 $report->generate_with_headers();
1072 $lxdebug->leave_sub();
1075 sub check_delivered_flag {
1076 $lxdebug->enter_sub();
1080 if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
1081 return $lxdebug->leave_sub();
1084 my $all_delivered = 0;
1086 foreach my $i (1 .. $form->{rowcount}) {
1087 next if (!$form->{"id_$i"});
1089 if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
1098 $form->{delivered} = 1 if $all_delivered;
1100 $lxdebug->leave_sub();
1103 sub save_and_close {
1104 $lxdebug->enter_sub();
1108 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1110 if ($form->{type} =~ /_order$/) {
1111 $form->isblank("transdate", $locale->text('Order Date missing!'));
1113 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1116 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1117 $form->{$idx} =~ s/^\s*//g;
1118 $form->{$idx} =~ s/\s*$//g;
1120 $msg = ucfirst $form->{vc};
1121 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1123 # $locale->text('Customer missing!');
1124 # $locale->text('Vendor missing!');
1126 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1127 if ($form->{currency} ne $form->{defaultcurrency});
1131 if($form->{payment_id}) {
1132 $payment_id = $form->{payment_id};
1135 # if the name changed get new values
1136 if (&check_name($form->{vc})) {
1137 if($form->{payment_id} eq "") {
1138 $form->{payment_id} = $payment_id;
1144 $form->{id} = 0 if $form->{saveasnew};
1146 # this is for the internal notes section for the [email] Subject
1147 if ($form->{type} =~ /_order$/) {
1148 if ($form->{type} eq 'sales_order') {
1149 $form->{label} = $locale->text('Sales Order');
1151 $numberfld = "sonumber";
1152 $ordnumber = "ordnumber";
1154 $form->{label} = $locale->text('Purchase Order');
1156 $numberfld = "ponumber";
1157 $ordnumber = "ordnumber";
1160 $err = $locale->text('Cannot save order!');
1162 check_delivered_flag();
1165 if ($form->{type} eq 'sales_quotation') {
1166 $form->{label} = $locale->text('Quotation');
1168 $numberfld = "sqnumber";
1169 $ordnumber = "quonumber";
1171 $form->{label} = $locale->text('Request for Quotation');
1173 $numberfld = "rfqnumber";
1174 $ordnumber = "quonumber";
1177 $err = $locale->text('Cannot save quotation!');
1181 # get new number in sequence if no number is given or if saveasnew was requested
1182 if (!$form->{$ordnumber} || $form->{saveasnew}) {
1183 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1188 $form->error($err) if (!OE->save(\%myconfig, \%$form));
1190 # saving the history
1191 if(!exists $form->{addition}) {
1192 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1193 $form->{addition} = "SAVED";
1194 $form->save_history($form->dbconnect(\%myconfig));
1196 # /saving the history
1198 $form->redirect($form->{label} . " $form->{$ordnumber} " .
1199 $locale->text('saved!'));
1201 $lxdebug->leave_sub();
1205 $lxdebug->enter_sub();
1209 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1212 if ($form->{type} =~ /_order$/) {
1213 $form->isblank("transdate", $locale->text('Order Date missing!'));
1215 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1218 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1219 $form->{$idx} =~ s/^\s*//g;
1220 $form->{$idx} =~ s/\s*$//g;
1222 $msg = ucfirst $form->{vc};
1223 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1225 # $locale->text('Customer missing!');
1226 # $locale->text('Vendor missing!');
1228 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1229 if ($form->{currency} ne $form->{defaultcurrency});
1233 if($form->{payment_id}) {
1234 $payment_id = $form->{payment_id};
1237 # if the name changed get new values
1238 if (&check_name($form->{vc})) {
1239 if($form->{payment_id} eq "") {
1240 $form->{payment_id} = $payment_id;
1246 $form->{id} = 0 if $form->{saveasnew};
1248 # this is for the internal notes section for the [email] Subject
1249 if ($form->{type} =~ /_order$/) {
1250 if ($form->{type} eq 'sales_order') {
1251 $form->{label} = $locale->text('Sales Order');
1253 $numberfld = "sonumber";
1254 $ordnumber = "ordnumber";
1256 $form->{label} = $locale->text('Purchase Order');
1258 $numberfld = "ponumber";
1259 $ordnumber = "ordnumber";
1262 $err = $locale->text('Cannot save order!');
1264 check_delivered_flag();
1267 if ($form->{type} eq 'sales_quotation') {
1268 $form->{label} = $locale->text('Quotation');
1270 $numberfld = "sqnumber";
1271 $ordnumber = "quonumber";
1273 $form->{label} = $locale->text('Request for Quotation');
1275 $numberfld = "rfqnumber";
1276 $ordnumber = "quonumber";
1279 $err = $locale->text('Cannot save quotation!');
1283 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1284 unless $form->{$ordnumber};
1288 OE->save(\%myconfig, \%$form);
1290 # saving the history
1291 if(!exists $form->{addition}) {
1292 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1293 $form->{addition} = "SAVED";
1294 $form->save_history($form->dbconnect(\%myconfig));
1296 # /saving the history
1298 $form->{simple_save} = 1;
1299 if(!$form->{print_and_save}) {
1300 set_headings("edit");
1304 $lxdebug->leave_sub();
1308 $lxdebug->enter_sub();
1314 if ($form->{type} =~ /_order$/) {
1315 $msg = $locale->text('Are you sure you want to delete Order Number');
1316 $ordnumber = 'ordnumber';
1318 $msg = $locale->text('Are you sure you want to delete Quotation Number');
1319 $ordnumber = 'quonumber';
1325 <form method=post action=$form->{script}>
1328 # delete action variable
1329 map { delete $form->{$_} } qw(action header);
1331 foreach $key (keys %$form) {
1332 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1333 $form->{$key} =~ s/\"/"/g;
1334 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1338 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1340 <h4>$msg $form->{$ordnumber}</h4>
1342 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1343 <input name=action class=submit type=submit value="|
1344 . $locale->text('Yes') . qq|">
1345 <button class=submit type=button onclick="history.back()">|
1346 . $locale->text('No') . qq|</button>
1353 $lxdebug->leave_sub();
1356 sub delete_order_quotation {
1357 $lxdebug->enter_sub();
1361 if ($form->{type} =~ /_order$/) {
1362 $msg = $locale->text('Order deleted!');
1363 $err = $locale->text('Cannot delete order!');
1365 $msg = $locale->text('Quotation deleted!');
1366 $err = $locale->text('Cannot delete quotation!');
1368 if (OE->delete(\%myconfig, \%$form, $spool)){
1369 # saving the history
1370 if(!exists $form->{addition}) {
1371 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1372 $form->{addition} = "DELETED";
1373 $form->save_history($form->dbconnect(\%myconfig));
1375 # /saving the history
1381 $lxdebug->leave_sub();
1385 $lxdebug->enter_sub();
1388 $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
1390 $form->{old_employee_id} = $form->{employee_id};
1391 $form->{old_salesman_id} = $form->{salesman_id};
1393 if ($form->{type} =~ /_order$/) {
1395 # these checks only apply if the items don't bring their own ordnumbers/transdates.
1396 # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1397 $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1398 if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1399 $form->isblank("transdate", $locale->text('Order Date missing!'))
1400 if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1402 # also copy deliverydate from the order
1403 $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1404 $form->{orddate} = $form->{transdate};
1406 $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1407 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1408 $form->{ordnumber} = "";
1409 $form->{quodate} = $form->{transdate};
1412 $payment_id = $form->{payment_id} if $form->{payment_id};
1414 # if the name changed get new values
1415 if (&check_name($form->{vc})) {
1416 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1421 $form->{cp_id} *= 1;
1423 for $i (1 .. $form->{rowcount}) {
1424 for (qw(ship qty sellprice listprice basefactor)) {
1425 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1429 if ( $form->{type} =~ /_order/
1430 && $form->{currency} ne $form->{defaultcurrency}) {
1432 # check if we need a new exchangerate
1433 $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1435 $orddate = $form->current_date(\%myconfig);
1436 $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1438 if (!$exchangerate) {
1439 &backorder_exchangerate($orddate, $buysell);
1444 $form->{convert_from_oe_ids} = $form->{id};
1445 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1446 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1447 $form->{shipto} = 1;
1448 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1450 delete @{$form}{qw(id closed)};
1451 $form->{rowcount}--;
1453 if ($form->{type} =~ /_order$/) {
1454 $form->{exchangerate} = $exchangerate;
1458 if ( $form->{type} eq 'purchase_order'
1459 || $form->{type} eq 'request_quotation') {
1460 $form->{title} = $locale->text('Add Vendor Invoice');
1461 $form->{script} = 'ir.pl';
1466 if ( $form->{type} eq 'sales_order'
1467 || $form->{type} eq 'sales_quotation') {
1468 $form->{title} = $locale->text('Add Sales Invoice');
1469 $form->{script} = 'is.pl';
1474 # bo creates the id, reset it
1475 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1476 $form->{ $form->{vc} } =~ s/--.*//g;
1477 $form->{type} = "invoice";
1480 $locale = new Locale "$myconfig{countrycode}", "$script";
1482 require "bin/mozilla/$form->{script}";
1484 map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1486 $currency = $form->{currency};
1489 $form->{currency} = $currency;
1490 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1491 $form->{exchangerate} = $form->{forex} || '';
1493 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1498 for $i (1 .. $form->{rowcount}) {
1499 $form->{"discount_$i"} =
1500 $form->format_amount(\%myconfig, $form->{"discount_$i"});
1502 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1504 $decimalplaces = ($dec > 2) ? $dec : 2;
1506 # copy delivery date from reqdate for order -> invoice conversion
1507 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1508 unless $form->{"deliverydate_$i"};
1510 $form->{"sellprice_$i"} =
1511 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1514 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1515 $dec_qty = length $dec_qty;
1517 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1519 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1520 qw(partnumber description unit);
1526 $lxdebug->leave_sub();
1529 sub backorder_exchangerate {
1530 $lxdebug->enter_sub();
1534 my ($orddate, $buysell) = @_;
1541 <form method=post action=$form->{script}>
1544 # delete action variable
1545 map { delete $form->{$_} } qw(action header exchangerate);
1547 foreach $key (keys %$form) {
1548 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1549 $form->{$key} =~ s/\"/"/g;
1550 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1553 $form->{title} = $locale->text('Add Exchangerate');
1557 <input type=hidden name=exchangeratedate value=$orddate>
1558 <input type=hidden name=buysell value=$buysell>
1561 <tr><th class=listtop>$form->{title}</th></tr>
1562 <tr height="5"></tr>
1567 <th align=right>| . $locale->text('Currency') . qq|</th>
1568 <td>$form->{currency}</td>
1571 <th align=right>| . $locale->text('Date') . qq|</th>
1575 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1576 <td><input name=exchangerate size=11></td>
1586 <input type=hidden name=nextsub value=save_exchangerate>
1588 <input name=action class=submit type=submit value="|
1589 . $locale->text('Continue') . qq|">
1597 $lxdebug->leave_sub();
1600 sub save_exchangerate {
1601 $lxdebug->enter_sub();
1603 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1604 $form->{exchangerate} =
1605 $form->parse_amount(\%myconfig, $form->{exchangerate});
1606 $form->save_exchangerate(\%myconfig, $form->{currency},
1607 $form->{exchangeratedate},
1608 $form->{exchangerate}, $form->{buysell});
1612 $lxdebug->leave_sub();
1615 sub create_backorder {
1616 $lxdebug->enter_sub();
1618 $form->{shipped} = 1;
1620 # figure out if we need to create a backorder
1621 # items aren't saved if qty != 0
1623 for $i (1 .. $form->{rowcount}) {
1624 $totalqty += $qty = $form->{"qty_$i"};
1625 $totalship += $ship = $form->{"ship_$i"};
1627 $form->{"qty_$i"} = $qty - $ship;
1630 if ($totalship == 0) {
1631 map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1632 $form->{ordtotal} = 0;
1633 $form->{shipped} = 0;
1637 if ($totalqty == $totalship) {
1638 map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1639 $form->{ordtotal} = 0;
1644 qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1647 for $i (1 .. $form->{rowcount}) {
1649 $form->{"${_}_$i"} =
1650 $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1651 } qw(sellprice discount);
1656 OE->save(\%myconfig, \%$form);
1658 # rebuild rows for invoice
1662 for $i (1 .. $form->{rowcount}) {
1663 $form->{"qty_$i"} = $form->{"ship_$i"};
1665 if ($form->{"qty_$i"}) {
1668 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1673 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1674 $form->{rowcount} = $count;
1676 $lxdebug->leave_sub();
1680 $lxdebug->enter_sub();
1684 $form->{saveasnew} = 1;
1685 $form->{closed} = 0;
1686 map { delete $form->{$_} } qw(printed emailed queued);
1688 # Let Lx-Office assign a new order number if the user hasn't changed the
1689 # previous one. If it has been changed manually then use it as-is.
1690 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1691 $form->{$idx} =~ s/^\s*//g;
1692 $form->{$idx} =~ s/\s*$//g;
1693 if ($form->{saved_xyznumber} &&
1694 ($form->{saved_xyznumber} eq $form->{$idx})) {
1695 delete($form->{$idx});
1700 $lxdebug->leave_sub();
1703 sub check_for_direct_delivery_yes {
1704 $lxdebug->enter_sub();
1708 $form->{direct_delivery_checked} = 1;
1709 delete @{$form}{grep /^shipto/, keys %{ $form }};
1710 map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1711 $form->{shipto} = 1;
1713 $lxdebug->leave_sub();
1716 sub check_for_direct_delivery_no {
1717 $lxdebug->enter_sub();
1721 $form->{direct_delivery_checked} = 1;
1722 delete @{$form}{grep /^shipto/, keys %{ $form }};
1725 $lxdebug->leave_sub();
1728 sub check_for_direct_delivery {
1729 $lxdebug->enter_sub();
1733 if ($form->{direct_delivery_checked}
1734 || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1735 $lxdebug->leave_sub();
1739 if ($form->{shipto_id}) {
1740 Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1743 map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1746 delete $form->{action};
1747 $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
1750 print $form->parse_html_template("oe/check_for_direct_delivery");
1752 $lxdebug->leave_sub();
1757 sub purchase_order {
1758 $lxdebug->enter_sub();
1761 $auth->assert('purchase_order_edit');
1763 if ($form->{type} eq 'sales_order') {
1764 check_for_direct_delivery();
1767 if ($form->{type} =~ /^sales_/) {
1768 delete($form->{ordnumber});
1771 $form->{cp_id} *= 1;
1773 $form->{title} = $locale->text('Add Purchase Order');
1774 $form->{vc} = "vendor";
1775 $form->{type} = "purchase_order";
1779 $lxdebug->leave_sub();
1783 $lxdebug->enter_sub();
1786 $auth->assert('sales_order_edit');
1788 if ($form->{type} eq "purchase_order") {
1789 delete($form->{ordnumber});
1792 $form->{cp_id} *= 1;
1794 $form->{title} = $locale->text('Add Sales Order');
1795 $form->{vc} = "customer";
1796 $form->{type} = "sales_order";
1800 $lxdebug->leave_sub();
1804 $lxdebug->enter_sub();
1807 $auth->assert('purchase_order_edit | sales_order_edit');
1809 $form->{transdate} = $form->current_date(\%myconfig);
1810 delete $form->{duedate};
1812 $form->{convert_from_oe_ids} = $form->{id};
1813 $form->{closed} = 0;
1815 $form->{old_employee_id} = $form->{employee_id};
1816 $form->{old_salesman_id} = $form->{salesman_id};
1819 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered);
1821 for $i (1 .. $form->{rowcount}) {
1822 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
1825 my %saved_vars = map { $_ => $form->{$_} } grep { $form->{$_} } qw(currency);
1829 map { $form->{$_} = $saved_vars{$_} } keys %saved_vars;
1834 for $i (1 .. $form->{rowcount} - 1) {
1835 map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
1840 $lxdebug->leave_sub();
1843 sub delivery_order {
1844 $lxdebug->enter_sub();
1846 if ($form->{type} =~ /^sales/) {
1847 $auth->assert('sales_delivery_order_edit');
1849 $form->{vc} = 'customer';
1850 $form->{type} = 'sales_delivery_order';
1853 $auth->assert('purchase_delivery_order_edit');
1855 $form->{vc} = 'vendor';
1856 $form->{type} = 'purchase_delivery_order';
1859 require "bin/mozilla/do.pl";
1861 $form->{cp_id} *= 1;
1862 $form->{convert_from_oe_ids} = $form->{id};
1863 $form->{transdate} = $form->current_date(\%myconfig);
1864 delete $form->{duedate};
1866 $form->{old_employee_id} = $form->{employee_id};
1867 $form->{old_salesman_id} = $form->{salesman_id};
1870 delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
1872 for $i (1 .. $form->{rowcount}) {
1873 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
1876 my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
1882 map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;
1886 $lxdebug->leave_sub();
1890 $lxdebug->enter_sub();
1894 $form->{print_and_save} = 1;
1898 my $saved_form = save_form();
1902 restore_form($saved_form, 0, qw(id ordnumber quonumber));
1906 $lxdebug->leave_sub();
1910 call_sub($form->{yes_nextsub});
1914 call_sub($form->{no_nextsub});
1917 ######################################################################################################
1919 # ###############################################################################################
1921 $lxdebug->enter_sub();
1925 retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
1927 $form->{"taxaccounts"} =~ s/\s*$//;
1928 $form->{"taxaccounts"} =~ s/^\s*//;
1929 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1930 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1932 $form->{"taxaccounts"} = "";
1934 for my $i (1 .. $form->{"rowcount"}) {
1935 IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
1938 $form->{rowcount}++;
1939 $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
1941 $form->language_payment(\%myconfig);
1943 Common::webdav_folder($form) if ($webdav);
1948 display_row($form->{rowcount}) if $form->{rowcount};
1952 $lxdebug->leave_sub();
1955 sub report_for_todo_list {
1956 $lxdebug->enter_sub();
1958 my $quotations = OE->transactions_for_todo_list();
1961 if (@{ $quotations }) {
1962 my $edit_url = build_std_url('script=oe.pl', 'action=edit');
1964 $content = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
1965 'edit_url' => $edit_url });
1968 $lxdebug->leave_sub();