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 multiple rowcounts (== collective order) then check if the
201 # there were more than one customer (in that case OE::retrieve removes
202 # the content from the field)
203 $form->error($locale->text('Collective Orders only work for orders from one customer!'))
204 if $form->{rowcount} && $form->{type} eq 'sales_order'
205 && defined $form->{customer} && $form->{customer} eq '';
207 $form->{"$form->{vc}_id"} ||= $form->{"all_$form->{vc}"}->[0]->{id} if $form->{"all_$form->{vc}"};
209 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
210 $form->{shipto} = 1 if $form->{id};
212 # get customer / vendor
213 IR->get_vendor(\%myconfig, \%$form) if $form->{type} =~ /(purchase_order|request_quotation)/;
214 IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
216 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
217 $form->restore_vars(qw(taxincluded)) if $form->{id};
218 $form->restore_vars(qw(salesman_id)) if $editing;
219 $form->{forex} = $form->{exchangerate};
220 $form->{employee} = "$form->{employee}--$form->{employee_id}";
222 # build vendor/customer drop down comatibility... don't ask
223 $form->{"old$form->{vc}"} = $form->{vc};
224 $form->{"select$form->{vc}"} = 1;
226 $lxdebug->leave_sub();
230 $lxdebug->enter_sub();
234 $form->{formname} ||= $form->{type};
236 $form->{rowcount} = 0;
237 foreach $ref (@{ $form->{form_details} }) {
239 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{$ref};
241 for my $i (1 .. $form->{rowcount}) {
242 $form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
243 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($form->{id} ? 100 : 1));
244 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
245 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
248 $lxdebug->leave_sub();
252 $lxdebug->enter_sub();
257 # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
260 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
262 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
263 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
265 map { $form->{$_} = H($form->{$_}) }
266 qw(shippingpoint shipvia notes intnotes shiptoname
267 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
268 shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
270 # use JavaScript Calendar or not
271 $form->{jsscript} = 1;
272 $TMPL_VAR{button1} = qq|
273 <td nowrap><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
274 <input type=button name=transdate id="trigger1" value=| . $locale->text('button') . qq|></td>
276 $TMPL_VAR{button2} = qq|
277 <td nowrap width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
278 <input type=button name=reqdate name=reqdate id="trigger2" value=| . $locale->text('button') . qq|></td>
281 $TMPL_VAR{jsscript} = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
283 # openclosed checkboxes
285 push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
286 $form->{"delivered"} ? "checked" : "", $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
287 push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
288 $form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
289 $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
291 # set option selected
292 foreach $item ($form->{vc}, 'currency', 'department', ($form->{vc} eq "customer" ? 'customer' : 'vendor')) {
293 $form->{"select$item"} =~ s/ selected//;
294 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
297 #quote select[customer|vendor] Bug 133
298 $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
300 #substitute \n and \r to \s (bug 543)
301 $form->{"select$form->{vc}"} =~ s/[\n\r]/ /g;
303 my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
305 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
306 $form->get_lists("contacts" => "ALL_CONTACTS",
307 "shipto" => "ALL_SHIPTO",
308 "projects" => { "key" => "ALL_PROJECTS",
310 "old_id" => \@old_project_ids },
311 "employees" => "ALL_EMPLOYEES",
312 "salesmen" => "ALL_SALESMEN",
313 "taxzones" => "ALL_TAXZONES",
314 "payments" => "ALL_PAYMENTS",
315 "currencies" => "ALL_CURRENCIES",
316 "departments" => "ALL_DEPARTMENTS",
317 $vc => { key => "ALL_" . uc($vc),
318 limit => $myconfig{vclimit} + 1 },
319 "price_factors" => "ALL_PRICE_FACTORS");
322 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
323 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
324 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
325 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
328 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
329 $TMPL_VAR{vclimit} = $myconfig{vclimit};
330 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
331 push @custom_hiddens, "$form->{vc}_id";
332 push @custom_hiddens, "old$form->{vc}";
333 push @custom_hiddens, "select$form->{vc}";
335 # currencies and exchangerate
336 @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
337 %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
338 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
339 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency} && $form->{exchangerate};
340 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
341 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
342 push @custom_hiddens, "forex";
343 push @custom_hiddens, "exchangerate" if $form->{forex};
346 $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
347 $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
350 $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
352 push @custom_hiddens, "ordnumber" if $form->{type} =~ /_quotation$/;
353 push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
355 $credittext = $locale->text('Credit Limit exceeded!!!');
357 my $follow_up_vc = $form->{ $form->{vc} eq 'customer' ? 'customer' : 'vendor' };
358 $follow_up_vc =~ s/--.*?//;
359 $TMPL_VAR{follow_up_trans_info} = ($form->{type} =~ /_quotation$/ ? $form->{quonumber} : $form->{ordnumber}) . " ($follow_up_vc)";
362 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
364 if (scalar @{ $follow_ups }) {
365 $TMPL_VAR{num_follow_ups} = scalar @{ $follow_ups };
366 $TMPL_VAR{num_due_follow_ups} = sum map { $_->{due} * 1 } @{ $follow_ups };
370 $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
371 : ($form->{resubmit}) ? "document.oe.submit()"
372 : ($creditwarning) ? "alert('$credittext')"
375 $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
376 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
377 $TMPL_VAR{onload} = $onload;
379 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
380 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
381 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
385 $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
386 qw(id action type vc formname media format proforma queued printed emailed
387 title creditlimit creditremaining tradediscount business
388 max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
389 shiptocity shiptocountry shiptocontact shiptophone shiptofax
390 shiptodepartment_1 shiptodepartment_2 shiptoemail
391 message email subject cc bcc taxpart taxservice taxaccounts),
393 map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ]; # deleted: discount
397 is_sales => scalar ($form->{type} =~ /^sales_/), # these vars are exported, so that the template
398 is_order => scalar ($form->{type} =~ /_order$/), # may determine what to show
399 is_sales_quo => scalar ($form->{type} =~ /sales_quotation$/),
400 is_req_quo => scalar ($form->{type} =~ /request_quotation$/),
401 is_sales_ord => scalar ($form->{type} =~ /sales_order$/),
402 is_pur_ord => scalar ($form->{type} =~ /purchase_order$/),
405 print $form->parse_html_template("oe/form_header", { %TMPL_VAR });
407 $lxdebug->leave_sub();
411 $lxdebug->enter_sub();
415 $form->{invtotal} = $form->{invsubtotal};
417 $rows = max 2, $form->numtextrows($form->{notes}, 25, 8);
418 $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
419 $rows = max $rows, $introws;
421 $TMPL_VAR{notes} = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
422 $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
424 if (!$form->{taxincluded}) {
426 foreach $item (split / /, $form->{taxaccounts}) {
427 if ($form->{"${item}_base"}) {
428 $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
429 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
431 $TMPL_VAR{tax} .= qq|
433 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
434 <td align=right>$form->{"${item}_total"}</td>
439 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
442 foreach $item (split / /, $form->{taxaccounts}) {
443 if ($form->{"${item}_base"}) {
444 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
445 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
446 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
447 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
449 $TMPL_VAR{tax} .= qq|
451 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
452 <td align=right>$form->{"${item}_total"}</td>
455 <th align=right>Nettobetrag</th>
456 <td align=right>$form->{"${item}_netto"}</td>
462 $form->{oldinvtotal} = $form->{invtotal};
464 print $form->parse_html_template("oe/form_footer", {
467 print_options => print_options(inline => 1),
468 label_edit => $locale->text("Edit the $form->{type}"),
469 label_workflow => $locale->text("Workflow $form->{type}"),
472 $lxdebug->leave_sub();
476 $lxdebug->enter_sub();
478 my ($recursive_call) = shift;
482 set_headings($form->{"id"} ? "edit" : "add");
484 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
487 $payment_id = $form->{payment_id} if $form->{payment_id};
489 &check_name($form->{vc});
491 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
494 $buysell = 'sell' if ($form->{vc} eq 'vendor');
495 $form->{exchangerate} = $exchangerate if
496 $form->{forex} = $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
498 $exchangerate = $form->{exchangerate} || 1;
500 ##################### process items ######################################
502 $i = $form->{rowcount};
503 if ( ($form->{"partnumber_$i"} eq "")
504 && ($form->{"description_$i"} eq "")
505 && ($form->{"partsgroup_$i"} eq "")) {
507 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
512 if ($form->{type} =~ /^sales/) {
513 IS->retrieve_item(\%myconfig, \%$form);
515 IR->retrieve_item(\%myconfig, \%$form);
518 my $rows = scalar @{ $form->{item_list} };
520 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
523 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
532 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
533 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
534 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
535 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
536 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
538 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
540 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
541 $decimalplaces = max 2, length $1;
544 $form->{"sellprice_$i"} = $sellprice;
546 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
547 $form->{"sellprice_$i"} /= $exchangerate; # if there is an exchange rate adjust sellprice
550 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
551 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
552 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
553 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
555 $form->{creditremaining} -= $amount;
557 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
558 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
560 # get pricegroups for parts
561 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
563 # build up html code for prices_$i
570 # ok, so this is a new part
571 # ask if it is a part or service item
573 if ( $form->{"partsgroup_$i"}
574 && ($form->{"partsnumber_$i"} eq "")
575 && ($form->{"description_$i"} eq "")) {
577 $form->{"discount_$i"} = "";
581 $form->{"id_$i"} = 0;
586 ##################### process items ######################################
589 $lxdebug->leave_sub();
593 $lxdebug->enter_sub();
597 if ($form->{type} eq 'purchase_order') {
598 $form->{title} = $locale->text('Purchase Orders');
599 $form->{vc} = 'vendor';
600 $ordlabel = $locale->text('Order Number');
601 $ordnumber = 'ordnumber';
602 $employee = $locale->text('Employee');
605 if ($form->{type} eq 'request_quotation') {
606 $form->{title} = $locale->text('Request for Quotations');
607 $form->{vc} = 'vendor';
608 $ordlabel = $locale->text('RFQ Number');
609 $ordnumber = 'quonumber';
610 $employee = $locale->text('Employee');
613 if ($form->{type} eq 'sales_order') {
614 $form->{title} = $locale->text('Sales Orders');
615 $form->{vc} = 'customer';
616 $ordlabel = $locale->text('Order Number');
617 $ordnumber = 'ordnumber';
618 $employee = $locale->text('Employee');
621 if ($form->{type} eq 'sales_quotation') {
622 $form->{title} = $locale->text('Quotations');
623 $form->{vc} = 'customer';
624 $ordlabel = $locale->text('Quotation Number');
625 $ordnumber = 'quonumber';
626 $employee = $locale->text('Employee');
629 # setup vendor / customer selection
630 $form->all_vc(\%myconfig, $form->{vc},
631 ($form->{vc} eq 'customer') ? "AR" : "AP");
634 if (@{ $form->{all_departments} }) {
635 $form->{selectdepartment} = "<option>\n";
638 $form->{selectdepartment} .=
639 "<option>$_->{description}--$_->{id}</option>\n"
640 } (@{ $form->{all_departments} });
645 <th align=right nowrap>| . $locale->text('Department') . qq|</th>
646 <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
648 | if $form->{selectdepartment};
651 if (($form->{"type"} eq "sales_order") ||
652 ($form->{"type"} eq "purchase_order")) {
655 <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
656 <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
657 <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
658 <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
663 # use JavaScript Calendar or not
664 $form->{jsscript} = 1;
668 <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
669 <input type=button name=transdatefrom id="trigger3" value=|
670 . $locale->text('button') . qq|></td>
673 <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
674 <input type=button name=transdateto name=transdateto id="trigger4" value=|
675 . $locale->text('button') . qq|></td>
680 Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
681 "transdateto", "BL", "trigger4");
683 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
685 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
687 "employees" => "ALL_EMPLOYEES",
688 "salesmen" => "ALL_SALESMEN",
689 $vc => "ALL_" . uc($vc)
694 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
695 push(@values, $item->{"id"});
696 $labels{$item->{"id"}} = $item->{"projectnumber"};
699 NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
700 '-labels' => \%labels));
705 foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
706 push(@values, $item->{"id"});
707 $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
711 my %labels_salesmen = ();
712 my @values_salesmen = ('');
713 foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
714 push(@values_salesmen, $item->{"id"});
715 $labels_salesmen{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
718 my $employee_block = qq|
720 <th align="right">| . $locale->text('Employee') . qq|</th>
721 <td>| . NTI($cgi->popup_menu('-name' => 'employee_id', '-values' => \@values, '-labels' => \%labels)) . qq|</td>
724 <th align="right">| . $locale->text('Salesman') . qq|</th>
726 NTI($cgi->popup_menu('-name' => 'salesman_id',
727 '-values' => \@values_salesmen,
728 '-labels' => \%labels_salesmen)) . qq|
735 foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
736 push(@values, $item->{name}.qq|--|.$item->{"id"});
737 $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
740 my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
742 $myconfig{vclimit} <= scalar(@values)
743 ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">|
744 : NTI($cgi->popup_menu('-name' => "$form->{vc}",
745 '-default' => $form->{"old$form->{vc}"},
746 '-onChange' => 'document.getElementById(\'update_button\').click();',
747 '-values' => \@values,
748 '-labels' => \%labels));
754 <form method=post action=$form->{script}>
758 <th class=listtop>$form->{title}</th>
765 <th align=right>$vc_label</th>
766 <td colspan=3>$vc</td>
770 <th align=right>$ordlabel</th>
771 <td colspan=3><input name="$ordnumber" size=20></td>
775 <th align="right">| . $locale->text('Transaction description') . qq|</th>
776 <td colspan="3"><input name="transaction_description" size=20></td>
779 <th align="right">| . $locale->text("Project Number") . qq|</th>
780 <td colspan="3">$projectnumber</td>
783 <th align=right>| . $locale->text('From') . qq|</th> $button1
784 <th align=right>| . $locale->text('Bis') . qq|</th> $button2
786 <input type=hidden name=sort value=transdate>
788 <th align=right>| . $locale->text('Include in Report') . qq|</th>
792 <td><input type="checkbox" name="open" value="1" id="open" checked> <label for="open">| . $locale->text("Open") . qq|</td>
793 <td><input type="checkbox" name="closed" value="1" id="closed"> <label for="closed">| . $locale->text("Closed") . qq|</td>
797 <td><input name="l_id" class=checkbox type=checkbox value=Y> | . $locale->text('ID') . qq|</td>
798 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
801 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Date') . qq|</td>
802 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Required by') . qq|</td>
805 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
806 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
808 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> | . $locale->text('Ship via') . qq|</td>
811 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> | . $locale->text('Amount') . qq|</td>
812 <td><input name="l_tax" class=checkbox type=checkbox value=Y> | . $locale->text('Tax') . qq|</td>
813 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> | . $locale->text('Total') . qq|</td>
816 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag') . qq|</td>
817 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag prozentual') . qq|</td>
820 <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> | . $locale->text('Project Number') . qq|</td>
821 <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> | . $locale->text('Transaction description') . qq|</td>
824 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> | . $locale->text('Subtotal') . qq|</td>
825 <td><input name="l_salesman" class="checkbox" type="checkbox" value="Y"> | . $locale->text('Salesman') . qq|</td>
833 <tr><td colspan=4><hr size=3 noshade></td></tr>
839 <input type=hidden name=nextsub value=orders>
840 <input type=hidden name=vc value=$form->{vc}>
841 <input type=hidden name=type value=$form->{type}>
843 <input class=submit type=submit name=action value="|
844 . $locale->text('Continue') . qq|">
851 $lxdebug->leave_sub();
854 sub create_subtotal_row {
855 $lxdebug->enter_sub();
857 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
859 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
861 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
863 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
865 map { $totals->{$_} = 0 } @{ $subtotal_columns };
867 $lxdebug->leave_sub();
873 $lxdebug->enter_sub();
877 $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
879 ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
881 $form->{sort} ||= 'transdate';
883 OE->transactions(\%myconfig, \%$form);
885 $form->{rowcount} = scalar @{ $form->{OE} };
888 "transdate", "reqdate",
894 "shipvia", "globalprojectnumber",
895 "transaction_description", "open",
896 "delivered", "marge_total", "marge_percent"
899 # only show checkboxes if gotten here via sales_order form.
900 my $allow_multiple_orders = $form->{type} eq 'sales_order';
901 if ($allow_multiple_orders) {
902 unshift @columns, "ids";
905 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
906 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
908 my $attachment_basename;
909 if ($form->{vc} eq 'vendor') {
910 if ($form->{type} eq 'purchase_order') {
911 $form->{title} = $locale->text('Purchase Orders');
912 $attachment_basename = $locale->text('purchase_order_list');
914 $form->{title} = $locale->text('Request for Quotations');
915 $attachment_basename = $locale->text('rfq_list');
919 if ($form->{type} eq 'sales_order') {
920 $form->{title} = $locale->text('Sales Orders');
921 $attachment_basename = $locale->text('sales_order_list');
923 $form->{title} = $locale->text('Quotations');
924 $attachment_basename = $locale->text('quotation_list');
928 my $report = SL::ReportGenerator->new(\%myconfig, $form);
930 my @hidden_variables = map { "l_${_}" } @columns;
931 push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
932 transaction_description transdatefrom transdateto type vc employee_id salesman_id);
934 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
937 'ids' => { 'text' => '', },
938 'transdate' => { 'text' => $locale->text('Date'), },
939 'reqdate' => { 'text' => $locale->text('Required by'), },
940 'id' => { 'text' => $locale->text('ID'), },
941 'ordnumber' => { 'text' => $locale->text('Order'), },
942 'quonumber' => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
943 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
944 'netamount' => { 'text' => $locale->text('Amount'), },
945 'tax' => { 'text' => $locale->text('Tax'), },
946 'amount' => { 'text' => $locale->text('Total'), },
947 'curr' => { 'text' => $locale->text('Curr'), },
948 'employee' => { 'text' => $locale->text('Employee'), },
949 'salesman' => { 'text' => $locale->text('Salesman'), },
950 'shipvia' => { 'text' => $locale->text('Ship via'), },
951 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
952 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
953 'open' => { 'text' => $locale->text('Open'), },
954 'delivered' => { 'text' => $locale->text('Delivered'), },
955 'marge_total' => { 'text' => $locale->text('Ertrag'), },
956 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), }
959 foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia)) {
960 $column_defs{$name}->{link} = $href . "&sort=$name";
963 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
965 $form->{"l_type"} = "Y";
966 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
967 $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
969 $report->set_columns(%column_defs);
970 $report->set_column_order(@columns);
971 $report->set_export_options('orders', @hidden_variables);
972 $report->set_sort_indicator($form->{sort}, 1);
975 my ($department) = split m/--/, $form->{department};
977 push @options, $locale->text('Customer') . " : $form->{customer}" if $form->{customer};
978 push @options, $locale->text('Vendor') . " : $form->{vendor}" if $form->{vendor};
979 push @options, $locale->text('Department') . " : $department" if $form->{department};
980 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if $form->{ordnumber};
981 push @options, $locale->text('Notes') . " : $form->{notes}" if $form->{notes};
982 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if $form->{transaction_description};
983 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if $form->{transdatefrom};
984 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if $form->{transdateto};
985 push @options, $locale->text('Open') if $form->{open};
986 push @options, $locale->text('Closed') if $form->{closed};
987 push @options, $locale->text('Delivered') if $form->{delivered};
988 push @options, $locale->text('Not delivered') if $form->{notdelivered};
990 $report->set_options('top_info_text' => join("\n", @options),
991 'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
992 'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
993 'output_format' => 'HTML',
994 'title' => $form->{title},
995 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
997 $report->set_options_from_form();
999 # add sort and escape callback, this one we use for the add sub
1000 $form->{callback} = $href .= "&sort=$form->{sort}";
1002 # escape callback for href
1003 $callback = $form->escape($href);
1005 my @subtotal_columns = qw(netamount amount marge_total marge_percent);
1007 my %totals = map { $_ => 0 } @subtotal_columns;
1008 my %subtotals = map { $_ => 0 } @subtotal_columns;
1012 my $edit_url = build_std_url('action=edit', 'type', 'vc');
1014 foreach $oe (@{ $form->{OE} }) {
1015 map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
1017 $oe->{tax} = $oe->{amount} - $oe->{netamount};
1018 $oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
1019 $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
1021 map { $subtotals{$_} += $oe->{$_};
1022 $totals{$_} += $oe->{$_} } @subtotal_columns;
1024 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1025 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1027 map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
1031 foreach my $column (@columns) {
1032 next if ($column eq 'ids');
1034 'data' => $oe->{$column},
1035 'align' => $column_alignment{$column},
1040 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
1041 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
1042 'valign' => 'center',
1043 'align' => 'center',
1046 $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1048 my $row_set = [ $row ];
1050 if (($form->{l_subtotal} eq 'Y')
1051 && (($idx == (scalar @{ $form->{OE} } - 1))
1052 || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
1053 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1056 $report->add_data($row_set);
1061 $report->add_separator();
1062 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1064 $report->generate_with_headers();
1066 $lxdebug->leave_sub();
1069 sub check_delivered_flag {
1070 $lxdebug->enter_sub();
1074 if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
1075 return $lxdebug->leave_sub();
1078 my $all_delivered = 0;
1080 foreach my $i (1 .. $form->{rowcount}) {
1081 next if (!$form->{"id_$i"});
1083 if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
1092 $form->{delivered} = 1 if $all_delivered;
1094 $lxdebug->leave_sub();
1097 sub save_and_close {
1098 $lxdebug->enter_sub();
1102 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1104 if ($form->{type} =~ /_order$/) {
1105 $form->isblank("transdate", $locale->text('Order Date missing!'));
1107 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1110 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1111 $form->{$idx} =~ s/^\s*//g;
1112 $form->{$idx} =~ s/\s*$//g;
1114 $msg = ucfirst $form->{vc};
1115 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1117 # $locale->text('Customer missing!');
1118 # $locale->text('Vendor missing!');
1120 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1121 if ($form->{currency} ne $form->{defaultcurrency});
1125 if($form->{payment_id}) {
1126 $payment_id = $form->{payment_id};
1129 # if the name changed get new values
1130 if (&check_name($form->{vc})) {
1131 if($form->{payment_id} eq "") {
1132 $form->{payment_id} = $payment_id;
1138 $form->{id} = 0 if $form->{saveasnew};
1140 # this is for the internal notes section for the [email] Subject
1141 if ($form->{type} =~ /_order$/) {
1142 if ($form->{type} eq 'sales_order') {
1143 $form->{label} = $locale->text('Sales Order');
1145 $numberfld = "sonumber";
1146 $ordnumber = "ordnumber";
1148 $form->{label} = $locale->text('Purchase Order');
1150 $numberfld = "ponumber";
1151 $ordnumber = "ordnumber";
1154 $err = $locale->text('Cannot save order!');
1156 check_delivered_flag();
1159 if ($form->{type} eq 'sales_quotation') {
1160 $form->{label} = $locale->text('Quotation');
1162 $numberfld = "sqnumber";
1163 $ordnumber = "quonumber";
1165 $form->{label} = $locale->text('Request for Quotation');
1167 $numberfld = "rfqnumber";
1168 $ordnumber = "quonumber";
1171 $err = $locale->text('Cannot save quotation!');
1175 # get new number in sequence if no number is given or if saveasnew was requested
1176 if (!$form->{$ordnumber} || $form->{saveasnew}) {
1177 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1182 $form->error($err) if (!OE->save(\%myconfig, \%$form));
1184 # saving the history
1185 if(!exists $form->{addition}) {
1186 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1187 $form->{addition} = "SAVED";
1188 $form->save_history($form->dbconnect(\%myconfig));
1190 # /saving the history
1192 $form->redirect($form->{label} . " $form->{$ordnumber} " .
1193 $locale->text('saved!'));
1195 $lxdebug->leave_sub();
1199 $lxdebug->enter_sub();
1203 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1206 if ($form->{type} =~ /_order$/) {
1207 $form->isblank("transdate", $locale->text('Order Date missing!'));
1209 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1212 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1213 $form->{$idx} =~ s/^\s*//g;
1214 $form->{$idx} =~ s/\s*$//g;
1216 $msg = ucfirst $form->{vc};
1217 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1219 # $locale->text('Customer missing!');
1220 # $locale->text('Vendor missing!');
1222 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1223 if ($form->{currency} ne $form->{defaultcurrency});
1227 if($form->{payment_id}) {
1228 $payment_id = $form->{payment_id};
1231 # if the name changed get new values
1232 if (&check_name($form->{vc})) {
1233 if($form->{payment_id} eq "") {
1234 $form->{payment_id} = $payment_id;
1240 $form->{id} = 0 if $form->{saveasnew};
1242 # this is for the internal notes section for the [email] Subject
1243 if ($form->{type} =~ /_order$/) {
1244 if ($form->{type} eq 'sales_order') {
1245 $form->{label} = $locale->text('Sales Order');
1247 $numberfld = "sonumber";
1248 $ordnumber = "ordnumber";
1250 $form->{label} = $locale->text('Purchase Order');
1252 $numberfld = "ponumber";
1253 $ordnumber = "ordnumber";
1256 $err = $locale->text('Cannot save order!');
1258 check_delivered_flag();
1261 if ($form->{type} eq 'sales_quotation') {
1262 $form->{label} = $locale->text('Quotation');
1264 $numberfld = "sqnumber";
1265 $ordnumber = "quonumber";
1267 $form->{label} = $locale->text('Request for Quotation');
1269 $numberfld = "rfqnumber";
1270 $ordnumber = "quonumber";
1273 $err = $locale->text('Cannot save quotation!');
1277 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1278 unless $form->{$ordnumber};
1282 OE->save(\%myconfig, \%$form);
1284 # saving the history
1285 if(!exists $form->{addition}) {
1286 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1287 $form->{addition} = "SAVED";
1288 $form->save_history($form->dbconnect(\%myconfig));
1290 # /saving the history
1292 $form->{simple_save} = 1;
1293 if(!$form->{print_and_save}) {
1294 set_headings("edit");
1298 $lxdebug->leave_sub();
1302 $lxdebug->enter_sub();
1308 if ($form->{type} =~ /_order$/) {
1309 $msg = $locale->text('Are you sure you want to delete Order Number');
1310 $ordnumber = 'ordnumber';
1312 $msg = $locale->text('Are you sure you want to delete Quotation Number');
1313 $ordnumber = 'quonumber';
1319 <form method=post action=$form->{script}>
1322 # delete action variable
1323 map { delete $form->{$_} } qw(action header);
1325 foreach $key (keys %$form) {
1326 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1327 $form->{$key} =~ s/\"/"/g;
1328 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1332 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1334 <h4>$msg $form->{$ordnumber}</h4>
1336 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1337 <input name=action class=submit type=submit value="|
1338 . $locale->text('Yes') . qq|">
1339 <button class=submit type=button onclick="history.back()">|
1340 . $locale->text('No') . qq|</button>
1347 $lxdebug->leave_sub();
1350 sub delete_order_quotation {
1351 $lxdebug->enter_sub();
1355 if ($form->{type} =~ /_order$/) {
1356 $msg = $locale->text('Order deleted!');
1357 $err = $locale->text('Cannot delete order!');
1359 $msg = $locale->text('Quotation deleted!');
1360 $err = $locale->text('Cannot delete quotation!');
1362 if (OE->delete(\%myconfig, \%$form, $spool)){
1363 # saving the history
1364 if(!exists $form->{addition}) {
1365 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1366 $form->{addition} = "DELETED";
1367 $form->save_history($form->dbconnect(\%myconfig));
1369 # /saving the history
1375 $lxdebug->leave_sub();
1379 $lxdebug->enter_sub();
1382 $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
1384 $form->{old_employee_id} = $form->{employee_id};
1385 $form->{old_salesman_id} = $form->{salesman_id};
1387 if ($form->{type} =~ /_order$/) {
1389 # these checks only apply if the items don't bring their own ordnumbers/transdates.
1390 # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1391 $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1392 if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1393 $form->isblank("transdate", $locale->text('Order Date missing!'))
1394 if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1396 # also copy deliverydate from the order
1397 $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1398 $form->{orddate} = $form->{transdate};
1400 $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1401 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1402 $form->{ordnumber} = "";
1403 $form->{quodate} = $form->{transdate};
1406 $payment_id = $form->{payment_id} if $form->{payment_id};
1408 # if the name changed get new values
1409 if (&check_name($form->{vc})) {
1410 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1415 $form->{cp_id} *= 1;
1417 for $i (1 .. $form->{rowcount}) {
1418 for (qw(ship qty sellprice listprice basefactor)) {
1419 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1423 if ( $form->{type} =~ /_order/
1424 && $form->{currency} ne $form->{defaultcurrency}) {
1426 # check if we need a new exchangerate
1427 $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1429 $orddate = $form->current_date(\%myconfig);
1430 $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1432 if (!$exchangerate) {
1433 &backorder_exchangerate($orddate, $buysell);
1438 # close orders/quotations
1439 $form->{closed} = 1;
1441 # save order if one ordnumber has been given
1442 # if not it's most likely a collective order, which can't be saved back
1443 # so they just have to be closed
1444 if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
1445 OE->close_order(\%myconfig, \%$form) if ($form->{id});
1447 OE->close_orders(\%myconfig, \%$form);
1450 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1451 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1454 $form->{closed} = 0;
1455 $form->{rowcount}--;
1456 $form->{shipto} = 1;
1458 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1460 if ($form->{type} =~ /_order$/) {
1461 $form->{exchangerate} = $exchangerate;
1465 if ( $form->{type} eq 'purchase_order'
1466 || $form->{type} eq 'request_quotation') {
1467 $form->{title} = $locale->text('Add Vendor Invoice');
1468 $form->{script} = 'ir.pl';
1473 if ( $form->{type} eq 'sales_order'
1474 || $form->{type} eq 'sales_quotation') {
1475 $form->{title} = $locale->text('Add Sales Invoice');
1476 $form->{script} = 'is.pl';
1481 # bo creates the id, reset it
1482 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1483 $form->{ $form->{vc} } =~ s/--.*//g;
1484 $form->{type} = "invoice";
1487 $locale = new Locale "$myconfig{countrycode}", "$script";
1489 require "bin/mozilla/$form->{script}";
1491 map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1493 $currency = $form->{currency};
1496 $form->{currency} = $currency;
1497 $form->{exchangerate} = "";
1498 $form->{forex} = "";
1499 $form->{exchangerate} = $exchangerate
1503 $form->check_exchangerate(
1504 \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1507 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1512 for $i (1 .. $form->{rowcount}) {
1513 $form->{"discount_$i"} =
1514 $form->format_amount(\%myconfig, $form->{"discount_$i"});
1516 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1518 $decimalplaces = ($dec > 2) ? $dec : 2;
1520 # copy delivery date from reqdate for order -> invoice conversion
1521 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1522 unless $form->{"deliverydate_$i"};
1524 $form->{"sellprice_$i"} =
1525 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1528 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1529 $dec_qty = length $dec_qty;
1531 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1533 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1534 qw(partnumber description unit);
1540 $lxdebug->leave_sub();
1543 sub backorder_exchangerate {
1544 $lxdebug->enter_sub();
1548 my ($orddate, $buysell) = @_;
1555 <form method=post action=$form->{script}>
1558 # delete action variable
1559 map { delete $form->{$_} } qw(action header exchangerate);
1561 foreach $key (keys %$form) {
1562 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1563 $form->{$key} =~ s/\"/"/g;
1564 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1567 $form->{title} = $locale->text('Add Exchangerate');
1571 <input type=hidden name=exchangeratedate value=$orddate>
1572 <input type=hidden name=buysell value=$buysell>
1575 <tr><th class=listtop>$form->{title}</th></tr>
1576 <tr height="5"></tr>
1581 <th align=right>| . $locale->text('Currency') . qq|</th>
1582 <td>$form->{currency}</td>
1585 <th align=right>| . $locale->text('Date') . qq|</th>
1589 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1590 <td><input name=exchangerate size=11></td>
1600 <input type=hidden name=nextsub value=save_exchangerate>
1602 <input name=action class=submit type=submit value="|
1603 . $locale->text('Continue') . qq|">
1611 $lxdebug->leave_sub();
1614 sub save_exchangerate {
1615 $lxdebug->enter_sub();
1617 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1618 $form->{exchangerate} =
1619 $form->parse_amount(\%myconfig, $form->{exchangerate});
1620 $form->save_exchangerate(\%myconfig, $form->{currency},
1621 $form->{exchangeratedate},
1622 $form->{exchangerate}, $form->{buysell});
1626 $lxdebug->leave_sub();
1629 sub create_backorder {
1630 $lxdebug->enter_sub();
1632 $form->{shipped} = 1;
1634 # figure out if we need to create a backorder
1635 # items aren't saved if qty != 0
1637 for $i (1 .. $form->{rowcount}) {
1638 $totalqty += $qty = $form->{"qty_$i"};
1639 $totalship += $ship = $form->{"ship_$i"};
1641 $form->{"qty_$i"} = $qty - $ship;
1644 if ($totalship == 0) {
1645 map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1646 $form->{ordtotal} = 0;
1647 $form->{shipped} = 0;
1651 if ($totalqty == $totalship) {
1652 map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1653 $form->{ordtotal} = 0;
1658 qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1661 for $i (1 .. $form->{rowcount}) {
1663 $form->{"${_}_$i"} =
1664 $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1665 } qw(sellprice discount);
1670 OE->save(\%myconfig, \%$form);
1672 # rebuild rows for invoice
1676 for $i (1 .. $form->{rowcount}) {
1677 $form->{"qty_$i"} = $form->{"ship_$i"};
1679 if ($form->{"qty_$i"}) {
1682 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1687 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1688 $form->{rowcount} = $count;
1690 $lxdebug->leave_sub();
1694 $lxdebug->enter_sub();
1698 $form->{saveasnew} = 1;
1699 $form->{closed} = 0;
1700 map { delete $form->{$_} } qw(printed emailed queued);
1702 # Let Lx-Office assign a new order number if the user hasn't changed the
1703 # previous one. If it has been changed manually then use it as-is.
1704 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1705 $form->{$idx} =~ s/^\s*//g;
1706 $form->{$idx} =~ s/\s*$//g;
1707 if ($form->{saved_xyznumber} &&
1708 ($form->{saved_xyznumber} eq $form->{$idx})) {
1709 delete($form->{$idx});
1714 $lxdebug->leave_sub();
1717 sub check_for_direct_delivery_yes {
1718 $lxdebug->enter_sub();
1722 $form->{direct_delivery_checked} = 1;
1723 delete @{$form}{grep /^shipto/, keys %{ $form }};
1724 map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1725 $form->{shipto} = 1;
1727 $lxdebug->leave_sub();
1730 sub check_for_direct_delivery_no {
1731 $lxdebug->enter_sub();
1735 $form->{direct_delivery_checked} = 1;
1736 delete @{$form}{grep /^shipto/, keys %{ $form }};
1739 $lxdebug->leave_sub();
1742 sub check_for_direct_delivery {
1743 $lxdebug->enter_sub();
1747 if ($form->{direct_delivery_checked}
1748 || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1749 $lxdebug->leave_sub();
1753 if ($form->{shipto_id}) {
1754 Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1757 map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1760 delete $form->{action};
1761 $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
1764 print $form->parse_html_template("oe/check_for_direct_delivery");
1766 $lxdebug->leave_sub();
1771 sub purchase_order {
1772 $lxdebug->enter_sub();
1775 $auth->assert('purchase_order_edit');
1777 if ($form->{type} eq 'sales_order') {
1778 check_for_direct_delivery();
1781 if ( $form->{type} eq 'sales_quotation'
1782 || $form->{type} eq 'request_quotation') {
1783 OE->close_order(\%myconfig, \%$form);
1786 if ($form->{type} =~ /^sales_/) {
1787 delete($form->{ordnumber});
1790 $form->{cp_id} *= 1;
1792 $form->{title} = $locale->text('Add Purchase Order');
1793 $form->{vc} = "vendor";
1794 $form->{type} = "purchase_order";
1798 $lxdebug->leave_sub();
1802 $lxdebug->enter_sub();
1805 $auth->assert('sales_order_edit');
1807 if ( $form->{type} eq 'sales_quotation'
1808 || $form->{type} eq 'request_quotation') {
1809 OE->close_order(\%myconfig, $form);
1812 if ($form->{type} eq "purchase_order") {
1813 delete($form->{ordnumber});
1816 $form->{cp_id} *= 1;
1818 $form->{title} = $locale->text('Add Sales Order');
1819 $form->{vc} = "customer";
1820 $form->{type} = "sales_order";
1824 $lxdebug->leave_sub();
1828 $lxdebug->enter_sub();
1831 $auth->assert('purchase_order_edit | sales_order_edit');
1833 $form->{transdate} = $form->current_date(\%myconfig);
1834 delete $form->{duedate};
1836 $form->{closed} = 0;
1838 $form->{old_employee_id} = $form->{employee_id};
1839 $form->{old_salesman_id} = $form->{salesman_id};
1842 map { delete $form->{$_} }
1843 qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
1845 for $i (1 .. $form->{rowcount}) {
1846 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1847 $form->{"${_}_${i}"})
1848 if ($form->{"${_}_${i}"}) }
1849 qw(ship qty sellprice listprice basefactor));
1857 for $i (1 .. $form->{rowcount} - 1) {
1858 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1859 qw(partnumber description unit);
1864 $lxdebug->leave_sub();
1867 sub delivery_order {
1868 $lxdebug->enter_sub();
1870 if ($form->{type} =~ /^sales/) {
1871 $auth->assert('sales_delivery_order_edit');
1873 $form->{vc} = 'customer';
1874 $form->{type} = 'sales_delivery_order';
1877 $auth->assert('purchase_delivery_order_edit');
1879 $form->{vc} = 'vendor';
1880 $form->{type} = 'purchase_delivery_order';
1883 require "bin/mozilla/do.pl";
1885 $form->{cp_id} *= 1;
1886 $form->{transdate} = $form->current_date(\%myconfig);
1887 delete $form->{duedate};
1889 $form->{closed} = 0;
1891 $form->{old_employee_id} = $form->{employee_id};
1892 $form->{old_salesman_id} = $form->{salesman_id};
1895 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal);
1897 for $i (1 .. $form->{rowcount}) {
1898 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
1901 my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
1907 map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;
1911 $lxdebug->leave_sub();
1915 $lxdebug->enter_sub();
1919 $form->{print_and_save} = 1;
1923 my $saved_form = save_form();
1927 restore_form($saved_form, 0, qw(id ordnumber quonumber));
1931 $lxdebug->leave_sub();
1935 call_sub($form->{yes_nextsub});
1939 call_sub($form->{no_nextsub});
1942 ######################################################################################################
1944 # ###############################################################################################
1946 $lxdebug->enter_sub();
1950 retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
1952 $form->{"taxaccounts"} =~ s/\s*$//;
1953 $form->{"taxaccounts"} =~ s/^\s*//;
1954 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1955 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1957 $form->{"taxaccounts"} = "";
1959 for my $i (1 .. $form->{"rowcount"}) {
1960 IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
1963 $form->{rowcount}++;
1964 $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
1966 $form->language_payment(\%myconfig);
1968 Common::webdav_folder($form) if ($webdav);
1973 display_row($form->{rowcount}) if $form->{rowcount};
1977 $lxdebug->leave_sub();
1980 sub report_for_todo_list {
1981 $lxdebug->enter_sub();
1983 my $quotations = OE->transactions_for_todo_list();
1986 if (@{ $quotations }) {
1987 my $edit_url = build_std_url('script=oe.pl', 'action=edit', 'type=sales_quotation', 'vc=customer');
1989 $content = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
1990 'edit_url' => $edit_url });
1993 $lxdebug->leave_sub();