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->{"select$form->{vc}"} = 1;
226 if (@{ $form->{all_departments} }) {
227 $form->{department} = "$form->{department}--$form->{department_id}";
228 $form->{selectdepartment} = join "\n", "<option>", map "<option>$_->{description}--$_->{id}</option>", @{ $form->{all_departments} };
231 $lxdebug->leave_sub();
235 $lxdebug->enter_sub();
239 $form->{formname} ||= $form->{type};
241 foreach $ref (@{ $form->{form_details} }) {
243 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{$ref};
245 for my $i (1 .. $form->{rowcount}) {
246 $form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
247 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($form->{id} ? 100 : 1));
248 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
249 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
252 $lxdebug->leave_sub();
256 $lxdebug->enter_sub();
261 # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
264 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
265 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
267 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
269 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
270 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
272 map { $form->{$_} = H($form->{$_}) }
273 qw(shippingpoint shipvia notes intnotes shiptoname
274 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
275 shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
277 # use JavaScript Calendar or not
278 $form->{jsscript} = 1;
279 $TMPL_VAR{button1} = qq|
280 <td nowrap><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
281 <input type=button name=transdate id="trigger1" value=| . $locale->text('button') . qq|></td>
283 $TMPL_VAR{button2} = qq|
284 <td nowrap width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
285 <input type=button name=reqdate name=reqdate id="trigger2" value=| . $locale->text('button') . qq|></td>
288 $TMPL_VAR{jsscript} = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
290 # openclosed checkboxes
292 push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
293 $form->{"delivered"} ? "checked" : "", $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
294 push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
295 $form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
296 $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
298 # set option selected
299 foreach $item ($form->{vc}, 'currency', 'department', ($form->{vc} eq "customer" ? 'customer' : 'vendor')) {
300 $form->{"select$item"} =~ s/ selected//;
301 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
304 #quote select[customer|vendor] Bug 133
305 $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
307 #substitute \n and \r to \s (bug 543)
308 $form->{"select$form->{vc}"} =~ s/[\n\r]/ /g;
310 my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
312 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
313 $form->get_lists("contacts" => "ALL_CONTACTS",
314 "shipto" => "ALL_SHIPTO",
315 "projects" => { "key" => "ALL_PROJECTS",
317 "old_id" => \@old_project_ids },
318 "employees" => "ALL_EMPLOYEES",
319 "salesmen" => "ALL_SALESMEN",
320 "taxzones" => "ALL_TAXZONES",
321 "payments" => "ALL_PAYMENTS",
322 "currencies" => "ALL_CURRENCIES",
323 $vc => { key => "ALL_" . uc($vc),
324 limit => $myconfig{vclimit} + 1 },
325 "price_factors" => "ALL_PRICE_FACTORS");
328 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
329 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
330 $TMPL_VAR{contact_labels} = sub { $_[0]->{"cp_name"} . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
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} && $form->{exchangerate};
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),
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>$form->{notes}</textarea>|;
427 $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</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->{exchangerate} = $exchangerate if
501 $form->{forex} = $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
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 $form->{sort} ||= 'transdate';
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 $column_defs{$name}->{link} = $href . "&sort=$name";
968 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
970 $form->{"l_type"} = "Y";
971 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
972 $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
974 $report->set_columns(%column_defs);
975 $report->set_column_order(@columns);
976 $report->set_export_options('orders', @hidden_variables);
977 $report->set_sort_indicator($form->{sort}, 1);
980 my ($department) = split m/--/, $form->{department};
982 push @options, $locale->text('Customer') . " : $form->{customer}" if $form->{customer};
983 push @options, $locale->text('Vendor') . " : $form->{vendor}" if $form->{vendor};
984 push @options, $locale->text('Department') . " : $department" if $form->{department};
985 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if $form->{ordnumber};
986 push @options, $locale->text('Notes') . " : $form->{notes}" if $form->{notes};
987 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if $form->{transaction_description};
988 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if $form->{transdatefrom};
989 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if $form->{transdateto};
990 push @options, $locale->text('Open') if $form->{open};
991 push @options, $locale->text('Closed') if $form->{closed};
992 push @options, $locale->text('Delivered') if $form->{delivered};
993 push @options, $locale->text('Not delivered') if $form->{notdelivered};
995 $report->set_options('top_info_text' => join("\n", @options),
996 'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
997 'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
998 'output_format' => 'HTML',
999 'title' => $form->{title},
1000 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1002 $report->set_options_from_form();
1004 # add sort and escape callback, this one we use for the add sub
1005 $form->{callback} = $href .= "&sort=$form->{sort}";
1007 # escape callback for href
1008 $callback = $form->escape($href);
1010 my @subtotal_columns = qw(netamount amount marge_total marge_percent);
1012 my %totals = map { $_ => 0 } @subtotal_columns;
1013 my %subtotals = map { $_ => 0 } @subtotal_columns;
1017 my $edit_url = build_std_url('action=edit', 'type', 'vc');
1019 foreach $oe (@{ $form->{OE} }) {
1020 map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
1022 $oe->{tax} = $oe->{amount} - $oe->{netamount};
1023 $oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
1024 $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
1026 map { $subtotals{$_} += $oe->{$_};
1027 $totals{$_} += $oe->{$_} } @subtotal_columns;
1029 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1030 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1032 map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
1036 foreach my $column (@columns) {
1037 next if ($column eq 'ids');
1039 'data' => $oe->{$column},
1040 'align' => $column_alignment{$column},
1045 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
1046 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
1047 'valign' => 'center',
1048 'align' => 'center',
1051 $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1053 my $row_set = [ $row ];
1055 if (($form->{l_subtotal} eq 'Y')
1056 && (($idx == (scalar @{ $form->{OE} } - 1))
1057 || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
1058 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1061 $report->add_data($row_set);
1066 $report->add_separator();
1067 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1069 $report->generate_with_headers();
1071 $lxdebug->leave_sub();
1074 sub check_delivered_flag {
1075 $lxdebug->enter_sub();
1079 if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
1080 return $lxdebug->leave_sub();
1083 my $all_delivered = 0;
1085 foreach my $i (1 .. $form->{rowcount}) {
1086 next if (!$form->{"id_$i"});
1088 if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
1097 $form->{delivered} = 1 if $all_delivered;
1099 $lxdebug->leave_sub();
1102 sub save_and_close {
1103 $lxdebug->enter_sub();
1107 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1109 if ($form->{type} =~ /_order$/) {
1110 $form->isblank("transdate", $locale->text('Order Date missing!'));
1112 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1115 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1116 $form->{$idx} =~ s/^\s*//g;
1117 $form->{$idx} =~ s/\s*$//g;
1119 $msg = ucfirst $form->{vc};
1120 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1122 # $locale->text('Customer missing!');
1123 # $locale->text('Vendor missing!');
1125 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1126 if ($form->{currency} ne $form->{defaultcurrency});
1130 if($form->{payment_id}) {
1131 $payment_id = $form->{payment_id};
1134 # if the name changed get new values
1135 if (&check_name($form->{vc})) {
1136 if($form->{payment_id} eq "") {
1137 $form->{payment_id} = $payment_id;
1143 $form->{id} = 0 if $form->{saveasnew};
1145 # this is for the internal notes section for the [email] Subject
1146 if ($form->{type} =~ /_order$/) {
1147 if ($form->{type} eq 'sales_order') {
1148 $form->{label} = $locale->text('Sales Order');
1150 $numberfld = "sonumber";
1151 $ordnumber = "ordnumber";
1153 $form->{label} = $locale->text('Purchase Order');
1155 $numberfld = "ponumber";
1156 $ordnumber = "ordnumber";
1159 $err = $locale->text('Cannot save order!');
1161 check_delivered_flag();
1164 if ($form->{type} eq 'sales_quotation') {
1165 $form->{label} = $locale->text('Quotation');
1167 $numberfld = "sqnumber";
1168 $ordnumber = "quonumber";
1170 $form->{label} = $locale->text('Request for Quotation');
1172 $numberfld = "rfqnumber";
1173 $ordnumber = "quonumber";
1176 $err = $locale->text('Cannot save quotation!');
1180 # get new number in sequence if no number is given or if saveasnew was requested
1181 if (!$form->{$ordnumber} || $form->{saveasnew}) {
1182 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1187 $form->error($err) if (!OE->save(\%myconfig, \%$form));
1189 # saving the history
1190 if(!exists $form->{addition}) {
1191 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1192 $form->{addition} = "SAVED";
1193 $form->save_history($form->dbconnect(\%myconfig));
1195 # /saving the history
1197 $form->redirect($form->{label} . " $form->{$ordnumber} " .
1198 $locale->text('saved!'));
1200 $lxdebug->leave_sub();
1204 $lxdebug->enter_sub();
1208 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1211 if ($form->{type} =~ /_order$/) {
1212 $form->isblank("transdate", $locale->text('Order Date missing!'));
1214 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1217 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1218 $form->{$idx} =~ s/^\s*//g;
1219 $form->{$idx} =~ s/\s*$//g;
1221 $msg = ucfirst $form->{vc};
1222 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1224 # $locale->text('Customer missing!');
1225 # $locale->text('Vendor missing!');
1227 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1228 if ($form->{currency} ne $form->{defaultcurrency});
1232 if($form->{payment_id}) {
1233 $payment_id = $form->{payment_id};
1236 # if the name changed get new values
1237 if (&check_name($form->{vc})) {
1238 if($form->{payment_id} eq "") {
1239 $form->{payment_id} = $payment_id;
1245 $form->{id} = 0 if $form->{saveasnew};
1247 # this is for the internal notes section for the [email] Subject
1248 if ($form->{type} =~ /_order$/) {
1249 if ($form->{type} eq 'sales_order') {
1250 $form->{label} = $locale->text('Sales Order');
1252 $numberfld = "sonumber";
1253 $ordnumber = "ordnumber";
1255 $form->{label} = $locale->text('Purchase Order');
1257 $numberfld = "ponumber";
1258 $ordnumber = "ordnumber";
1261 $err = $locale->text('Cannot save order!');
1263 check_delivered_flag();
1266 if ($form->{type} eq 'sales_quotation') {
1267 $form->{label} = $locale->text('Quotation');
1269 $numberfld = "sqnumber";
1270 $ordnumber = "quonumber";
1272 $form->{label} = $locale->text('Request for Quotation');
1274 $numberfld = "rfqnumber";
1275 $ordnumber = "quonumber";
1278 $err = $locale->text('Cannot save quotation!');
1282 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1283 unless $form->{$ordnumber};
1287 OE->save(\%myconfig, \%$form);
1289 # saving the history
1290 if(!exists $form->{addition}) {
1291 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1292 $form->{addition} = "SAVED";
1293 $form->save_history($form->dbconnect(\%myconfig));
1295 # /saving the history
1297 $form->{simple_save} = 1;
1298 if(!$form->{print_and_save}) {
1299 set_headings("edit");
1303 $lxdebug->leave_sub();
1307 $lxdebug->enter_sub();
1313 if ($form->{type} =~ /_order$/) {
1314 $msg = $locale->text('Are you sure you want to delete Order Number');
1315 $ordnumber = 'ordnumber';
1317 $msg = $locale->text('Are you sure you want to delete Quotation Number');
1318 $ordnumber = 'quonumber';
1324 <form method=post action=$form->{script}>
1327 # delete action variable
1328 map { delete $form->{$_} } qw(action header);
1330 foreach $key (keys %$form) {
1331 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1332 $form->{$key} =~ s/\"/"/g;
1333 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1337 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1339 <h4>$msg $form->{$ordnumber}</h4>
1341 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1342 <input name=action class=submit type=submit value="|
1343 . $locale->text('Yes') . qq|">
1344 <button class=submit type=button onclick="history.back()">|
1345 . $locale->text('No') . qq|</button>
1352 $lxdebug->leave_sub();
1355 sub delete_order_quotation {
1356 $lxdebug->enter_sub();
1360 if ($form->{type} =~ /_order$/) {
1361 $msg = $locale->text('Order deleted!');
1362 $err = $locale->text('Cannot delete order!');
1364 $msg = $locale->text('Quotation deleted!');
1365 $err = $locale->text('Cannot delete quotation!');
1367 if (OE->delete(\%myconfig, \%$form, $spool)){
1368 # saving the history
1369 if(!exists $form->{addition}) {
1370 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1371 $form->{addition} = "DELETED";
1372 $form->save_history($form->dbconnect(\%myconfig));
1374 # /saving the history
1380 $lxdebug->leave_sub();
1384 $lxdebug->enter_sub();
1387 $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
1389 $form->{old_employee_id} = $form->{employee_id};
1390 $form->{old_salesman_id} = $form->{salesman_id};
1392 if ($form->{type} =~ /_order$/) {
1394 # these checks only apply if the items don't bring their own ordnumbers/transdates.
1395 # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1396 $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1397 if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1398 $form->isblank("transdate", $locale->text('Order Date missing!'))
1399 if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1401 # also copy deliverydate from the order
1402 $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1403 $form->{orddate} = $form->{transdate};
1405 $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1406 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1407 $form->{ordnumber} = "";
1408 $form->{quodate} = $form->{transdate};
1411 $payment_id = $form->{payment_id} if $form->{payment_id};
1413 # if the name changed get new values
1414 if (&check_name($form->{vc})) {
1415 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1420 $form->{cp_id} *= 1;
1422 for $i (1 .. $form->{rowcount}) {
1423 for (qw(ship qty sellprice listprice basefactor)) {
1424 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1428 if ( $form->{type} =~ /_order/
1429 && $form->{currency} ne $form->{defaultcurrency}) {
1431 # check if we need a new exchangerate
1432 $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1434 $orddate = $form->current_date(\%myconfig);
1435 $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1437 if (!$exchangerate) {
1438 &backorder_exchangerate($orddate, $buysell);
1443 # close orders/quotations
1444 $form->{closed} = 1;
1446 # save order if one ordnumber has been given
1447 # if not it's most likely a collective order, which can't be saved back
1448 # so they just have to be closed
1449 if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
1450 OE->close_order(\%myconfig, \%$form) if ($form->{id});
1452 OE->close_orders(\%myconfig, \%$form);
1455 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1456 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1459 $form->{closed} = 0;
1460 $form->{rowcount}--;
1461 $form->{shipto} = 1;
1463 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1465 if ($form->{type} =~ /_order$/) {
1466 $form->{exchangerate} = $exchangerate;
1470 if ( $form->{type} eq 'purchase_order'
1471 || $form->{type} eq 'request_quotation') {
1472 $form->{title} = $locale->text('Add Vendor Invoice');
1473 $form->{script} = 'ir.pl';
1478 if ( $form->{type} eq 'sales_order'
1479 || $form->{type} eq 'sales_quotation') {
1480 $form->{title} = $locale->text('Add Sales Invoice');
1481 $form->{script} = 'is.pl';
1486 # bo creates the id, reset it
1487 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1488 $form->{ $form->{vc} } =~ s/--.*//g;
1489 $form->{type} = "invoice";
1492 $locale = new Locale "$myconfig{countrycode}", "$script";
1494 require "bin/mozilla/$form->{script}";
1496 map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1498 $currency = $form->{currency};
1501 $form->{currency} = $currency;
1502 $form->{exchangerate} = "";
1503 $form->{forex} = "";
1504 $form->{exchangerate} = $exchangerate
1508 $form->check_exchangerate(
1509 \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1512 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1517 for $i (1 .. $form->{rowcount}) {
1518 $form->{"discount_$i"} =
1519 $form->format_amount(\%myconfig, $form->{"discount_$i"});
1521 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1523 $decimalplaces = ($dec > 2) ? $dec : 2;
1525 # copy delivery date from reqdate for order -> invoice conversion
1526 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1527 unless $form->{"deliverydate_$i"};
1529 $form->{"sellprice_$i"} =
1530 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1533 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1534 $dec_qty = length $dec_qty;
1536 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1538 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1539 qw(partnumber description unit);
1545 $lxdebug->leave_sub();
1548 sub backorder_exchangerate {
1549 $lxdebug->enter_sub();
1553 my ($orddate, $buysell) = @_;
1560 <form method=post action=$form->{script}>
1563 # delete action variable
1564 map { delete $form->{$_} } qw(action header exchangerate);
1566 foreach $key (keys %$form) {
1567 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1568 $form->{$key} =~ s/\"/"/g;
1569 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1572 $form->{title} = $locale->text('Add Exchangerate');
1576 <input type=hidden name=exchangeratedate value=$orddate>
1577 <input type=hidden name=buysell value=$buysell>
1580 <tr><th class=listtop>$form->{title}</th></tr>
1581 <tr height="5"></tr>
1586 <th align=right>| . $locale->text('Currency') . qq|</th>
1587 <td>$form->{currency}</td>
1590 <th align=right>| . $locale->text('Date') . qq|</th>
1594 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1595 <td><input name=exchangerate size=11></td>
1605 <input type=hidden name=nextsub value=save_exchangerate>
1607 <input name=action class=submit type=submit value="|
1608 . $locale->text('Continue') . qq|">
1616 $lxdebug->leave_sub();
1619 sub save_exchangerate {
1620 $lxdebug->enter_sub();
1622 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1623 $form->{exchangerate} =
1624 $form->parse_amount(\%myconfig, $form->{exchangerate});
1625 $form->save_exchangerate(\%myconfig, $form->{currency},
1626 $form->{exchangeratedate},
1627 $form->{exchangerate}, $form->{buysell});
1631 $lxdebug->leave_sub();
1634 sub create_backorder {
1635 $lxdebug->enter_sub();
1637 $form->{shipped} = 1;
1639 # figure out if we need to create a backorder
1640 # items aren't saved if qty != 0
1642 for $i (1 .. $form->{rowcount}) {
1643 $totalqty += $qty = $form->{"qty_$i"};
1644 $totalship += $ship = $form->{"ship_$i"};
1646 $form->{"qty_$i"} = $qty - $ship;
1649 if ($totalship == 0) {
1650 map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1651 $form->{ordtotal} = 0;
1652 $form->{shipped} = 0;
1656 if ($totalqty == $totalship) {
1657 map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1658 $form->{ordtotal} = 0;
1663 qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1666 for $i (1 .. $form->{rowcount}) {
1668 $form->{"${_}_$i"} =
1669 $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1670 } qw(sellprice discount);
1675 OE->save(\%myconfig, \%$form);
1677 # rebuild rows for invoice
1681 for $i (1 .. $form->{rowcount}) {
1682 $form->{"qty_$i"} = $form->{"ship_$i"};
1684 if ($form->{"qty_$i"}) {
1687 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1692 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1693 $form->{rowcount} = $count;
1695 $lxdebug->leave_sub();
1699 $lxdebug->enter_sub();
1703 $form->{saveasnew} = 1;
1704 $form->{closed} = 0;
1705 map { delete $form->{$_} } qw(printed emailed queued);
1707 # Let Lx-Office assign a new order number if the user hasn't changed the
1708 # previous one. If it has been changed manually then use it as-is.
1709 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1710 $form->{$idx} =~ s/^\s*//g;
1711 $form->{$idx} =~ s/\s*$//g;
1712 if ($form->{saved_xyznumber} &&
1713 ($form->{saved_xyznumber} eq $form->{$idx})) {
1714 delete($form->{$idx});
1719 $lxdebug->leave_sub();
1722 sub check_for_direct_delivery_yes {
1723 $lxdebug->enter_sub();
1727 $form->{direct_delivery_checked} = 1;
1728 delete @{$form}{grep /^shipto/, keys %{ $form }};
1729 map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1730 $form->{shipto} = 1;
1732 $lxdebug->leave_sub();
1735 sub check_for_direct_delivery_no {
1736 $lxdebug->enter_sub();
1740 $form->{direct_delivery_checked} = 1;
1741 delete @{$form}{grep /^shipto/, keys %{ $form }};
1744 $lxdebug->leave_sub();
1747 sub check_for_direct_delivery {
1748 $lxdebug->enter_sub();
1752 if ($form->{direct_delivery_checked}
1753 || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1754 $lxdebug->leave_sub();
1758 if ($form->{shipto_id}) {
1759 Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1762 map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1765 delete $form->{action};
1766 $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
1769 print $form->parse_html_template("oe/check_for_direct_delivery");
1771 $lxdebug->leave_sub();
1776 sub purchase_order {
1777 $lxdebug->enter_sub();
1780 $auth->assert('purchase_order_edit');
1782 if ($form->{type} eq 'sales_order') {
1783 check_for_direct_delivery();
1786 if ( $form->{type} eq 'sales_quotation'
1787 || $form->{type} eq 'request_quotation') {
1788 OE->close_order(\%myconfig, \%$form);
1791 if ($form->{type} =~ /^sales_/) {
1792 delete($form->{ordnumber});
1795 $form->{cp_id} *= 1;
1797 $form->{title} = $locale->text('Add Purchase Order');
1798 $form->{vc} = "vendor";
1799 $form->{type} = "purchase_order";
1803 $lxdebug->leave_sub();
1807 $lxdebug->enter_sub();
1810 $auth->assert('sales_order_edit');
1812 if ( $form->{type} eq 'sales_quotation'
1813 || $form->{type} eq 'request_quotation') {
1814 OE->close_order(\%myconfig, $form);
1817 if ($form->{type} eq "purchase_order") {
1818 delete($form->{ordnumber});
1821 $form->{cp_id} *= 1;
1823 $form->{title} = $locale->text('Add Sales Order');
1824 $form->{vc} = "customer";
1825 $form->{type} = "sales_order";
1829 $lxdebug->leave_sub();
1833 $lxdebug->enter_sub();
1836 $auth->assert('purchase_order_edit | sales_order_edit');
1838 $form->{transdate} = $form->current_date(\%myconfig);
1839 delete $form->{duedate};
1841 $form->{closed} = 0;
1843 $form->{old_employee_id} = $form->{employee_id};
1844 $form->{old_salesman_id} = $form->{salesman_id};
1847 map { delete $form->{$_} }
1848 qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
1850 for $i (1 .. $form->{rowcount}) {
1851 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1852 $form->{"${_}_${i}"})
1853 if ($form->{"${_}_${i}"}) }
1854 qw(ship qty sellprice listprice basefactor));
1862 for $i (1 .. $form->{rowcount} - 1) {
1863 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1864 qw(partnumber description unit);
1869 $lxdebug->leave_sub();
1872 sub delivery_order {
1873 $lxdebug->enter_sub();
1875 if ($form->{type} =~ /^sales/) {
1876 $auth->assert('sales_delivery_order_edit');
1878 $form->{vc} = 'customer';
1879 $form->{type} = 'sales_delivery_order';
1882 $auth->assert('purchase_delivery_order_edit');
1884 $form->{vc} = 'vendor';
1885 $form->{type} = 'purchase_delivery_order';
1888 require "bin/mozilla/do.pl";
1890 $form->{cp_id} *= 1;
1891 $form->{transdate} = $form->current_date(\%myconfig);
1892 delete $form->{duedate};
1894 $form->{closed} = 0;
1896 $form->{old_employee_id} = $form->{employee_id};
1897 $form->{old_salesman_id} = $form->{salesman_id};
1900 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal);
1902 for $i (1 .. $form->{rowcount}) {
1903 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
1906 my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
1912 map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;
1916 $lxdebug->leave_sub();
1920 $lxdebug->enter_sub();
1924 $form->{print_and_save} = 1;
1928 my $saved_form = save_form();
1932 restore_form($saved_form, 0, qw(id ordnumber quonumber));
1936 $lxdebug->leave_sub();
1940 call_sub($form->{yes_nextsub});
1944 call_sub($form->{no_nextsub});
1947 ######################################################################################################
1949 # ###############################################################################################
1951 $lxdebug->enter_sub();
1955 retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
1957 $form->{"taxaccounts"} =~ s/\s*$//;
1958 $form->{"taxaccounts"} =~ s/^\s*//;
1959 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1960 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1962 $form->{"taxaccounts"} = "";
1964 for my $i (1 .. $form->{"rowcount"}) {
1965 IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
1968 $form->{rowcount}++;
1969 $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
1971 $form->language_payment(\%myconfig);
1973 Common::webdav_folder($form) if ($webdav);
1978 display_row($form->{rowcount}) if $form->{rowcount};
1982 $lxdebug->leave_sub();
1985 sub report_for_todo_list {
1986 $lxdebug->enter_sub();
1988 my $quotations = OE->transactions_for_todo_list();
1991 if (@{ $quotations }) {
1992 my $edit_url = build_std_url('script=oe.pl', 'action=edit', 'type=sales_quotation', 'vc=customer');
1994 $content = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
1995 'edit_url' => $edit_url });
1998 $lxdebug->leave_sub();