1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger, Accounting
9 # Copyright (c) 1998-2003
11 # Author: Dieter Simader
12 # Email: dsimader@sql-ledger.org
13 # Web: http://www.sql-ledger.org
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
32 #======================================================================
34 use POSIX qw(strftime);
42 use SL::ReportGenerator;
43 use List::Util qw(max reduce sum);
45 require "bin/mozilla/io.pl";
46 require "bin/mozilla/arap.pl";
47 require "bin/mozilla/reportgenerator.pl";
54 # $locale->text('Edit the purchase_order');
55 # $locale->text('Edit the sales_order');
56 # $locale->text('Edit the request_quotation');
57 # $locale->text('Edit the sales_quotation');
59 # $locale->text('Workflow purchase_order');
60 # $locale->text('Workflow sales_order');
61 # $locale->text('Workflow request_quotation');
62 # $locale->text('Workflow sales_quotation');
65 'sales_order' => 'sales_order_edit',
66 'purchase_order' => 'purchase_order_edit',
67 'request_quotation' => 'request_quotation_edit',
68 'sales_quotation' => 'sales_quotation_edit',
72 my $right = $oe_access_map->{$form->{type}};
73 $right ||= 'DOES_NOT_EXIST';
75 $auth->assert($right);
79 $lxdebug->enter_sub();
85 if ($form->{type} eq 'purchase_order') {
86 $form->{title} = $action eq "edit" ?
87 $locale->text('Edit Purchase Order') :
88 $locale->text('Add Purchase Order');
89 $form->{heading} = $locale->text('Purchase Order');
90 $form->{vc} = 'vendor';
92 if ($form->{type} eq 'sales_order') {
93 $form->{title} = $action eq "edit" ?
94 $locale->text('Edit Sales Order') :
95 $locale->text('Add Sales Order');
96 $form->{heading} = $locale->text('Sales Order');
97 $form->{vc} = 'customer';
99 if ($form->{type} eq 'request_quotation') {
100 $form->{title} = $action eq "edit" ?
101 $locale->text('Edit Request for Quotation') :
102 $locale->text('Add Request for Quotation');
103 $form->{heading} = $locale->text('Request for Quotation');
104 $form->{vc} = 'vendor';
106 if ($form->{type} eq 'sales_quotation') {
107 $form->{title} = $action eq "edit" ?
108 $locale->text('Edit Quotation') :
109 $locale->text('Add Quotation');
110 $form->{heading} = $locale->text('Quotation');
111 $form->{vc} = 'customer';
114 $lxdebug->leave_sub();
118 $lxdebug->enter_sub();
125 "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}"
126 unless $form->{callback};
132 $lxdebug->leave_sub();
136 $lxdebug->enter_sub();
140 # show history button
141 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
142 #/show hhistory button
144 $form->{simple_save} = 0;
146 set_headings("edit");
148 # editing without stuff to edit? try adding it first
149 if ($form->{rowcount} && !$form->{print_and_save}) {
150 map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
154 undef $form->{rowcount};
156 $lxdebug->leave_sub();
159 } elsif (!$form->{id}) {
161 $lxdebug->leave_sub();
165 if ($form->{print_and_save}) {
166 $form->{action} = "print";
167 $form->{resubmit} = 1;
168 $language_id = $form->{language_id};
169 $printer_id = $form->{printer_id};
172 set_headings("edit");
176 $form->{rowcount} = 0;
177 foreach $ref (@{ $form->{form_details} }) {
179 map { $form->{"${_}_$form->{rowcount}"} = $ref->{$_} } keys %{$ref};
184 if ($form->{print_and_save}) {
185 $form->{language_id} = $language_id;
186 $form->{printer_id} = $printer_id;
191 $lxdebug->leave_sub();
195 $lxdebug->enter_sub();
199 # get customer/vendor
200 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
202 # retrieve order/quotation
203 $form->{webdav} = $webdav;
204 $form->{jsscript} = 1;
206 my $editing = $form->{id};
208 OE->retrieve(\%myconfig, \%$form);
210 # if multiple rowcounts (== collective order) then check if the
211 # there were more than one customer (in that case OE::retrieve removes
212 # the content from the field)
213 $form->error($locale->text('Collective Orders only work for orders from one customer!'))
214 if $form->{rowcount} && $form->{type} eq 'sales_order'
215 && defined $form->{customer} && $form->{customer} eq '';
217 $form->{"$form->{vc}_id"} ||= $form->{"all_$form->{vc}"}->[0]->{id} if $form->{"all_$form->{vc}"};
219 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes));
220 $form->{shipto} = 1 if $form->{id};
222 # get customer / vendor
223 IR->get_vendor(\%myconfig, \%$form) if $form->{type} =~ /(purchase_order|request_quotation)/;
224 IS->get_customer(\%myconfig, \%$form) if $form->{type} =~ /sales_(order|quotation)/;
226 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id));
227 $form->restore_vars(qw(taxincluded)) if $form->{id};
228 $form->restore_vars(qw(salesman_id)) if $editing;
229 $form->{forex} = $form->{exchangerate};
230 $form->{employee} = "$form->{employee}--$form->{employee_id}";
232 # build vendor/customer drop down comatibility... don't ask
233 if (@{ $form->{"all_$form->{vc}"} }) {
234 $form->{"select$form->{vc}"} = 1;
235 $form->{$form->{vc}} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
238 $form->{"old$form->{vc}"} = $form->{$form->{vc}};
239 $form->{"old$form->{vc}"} .= qq|--$form->{"$form->{vc}_id"}| unless ($form->{"old$form->{vc}"} =~ m/--\d+$/);
241 $lxdebug->leave_sub();
245 $lxdebug->enter_sub();
249 $form->{formname} ||= $form->{type};
251 for my $i (1 .. $form->{rowcount}) {
252 $form->{"reqdate_$i"} ||= $form->{"deliverydate_$i"};
253 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($form->{id} ? 100 : 1));
254 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
255 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
258 $lxdebug->leave_sub();
262 $lxdebug->enter_sub();
267 # Container for template variables. Unfortunately this has to be visible in form_footer too, so not my.
270 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
272 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
273 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
275 map { $form->{$_} = H($form->{$_}) }
276 qw(shippingpoint shipvia notes intnotes shiptoname
277 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
278 shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
280 # use JavaScript Calendar or not
281 $form->{jsscript} = 1;
282 $TMPL_VAR{button1} = qq|
283 <td nowrap><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
284 <input type=button name=transdate id="trigger1" value=| . $locale->text('button') . qq|></td>
286 $TMPL_VAR{button2} = qq|
287 <td nowrap width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
288 <input type=button name=reqdate name=reqdate id="trigger2" value=| . $locale->text('button') . qq|></td>
291 $TMPL_VAR{jsscript} = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
293 # openclosed checkboxes
295 push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
296 $form->{"delivered"} ? "checked" : "", $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
297 push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
298 $form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
299 $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
301 # set option selected
302 foreach $item ($form->{vc}, 'currency', 'department', ($form->{vc} eq "customer" ? 'customer' : 'vendor')) {
303 $form->{"select$item"} =~ s/ selected//;
304 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
307 #quote select[customer|vendor] Bug 133
308 $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
310 #substitute \n and \r to \s (bug 543)
311 $form->{"select$form->{vc}"} =~ s/[\n\r]/ /g;
313 my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
315 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
316 $form->get_lists("contacts" => "ALL_CONTACTS",
317 "shipto" => "ALL_SHIPTO",
318 "projects" => { "key" => "ALL_PROJECTS",
320 "old_id" => \@old_project_ids },
321 "employees" => "ALL_EMPLOYEES",
322 "salesmen" => "ALL_SALESMEN",
323 "taxzones" => "ALL_TAXZONES",
324 "payments" => "ALL_PAYMENTS",
325 "currencies" => "ALL_CURRENCIES",
326 "departments" => "ALL_DEPARTMENTS",
327 $vc => { key => "ALL_" . uc($vc),
328 limit => $myconfig{vclimit} + 1 },
329 "price_factors" => "ALL_PRICE_FACTORS");
332 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
333 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
334 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
335 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
338 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
339 $TMPL_VAR{vclimit} = $myconfig{vclimit};
340 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
341 push @custom_hiddens, "$form->{vc}_id";
342 push @custom_hiddens, "old$form->{vc}";
343 push @custom_hiddens, "select$form->{vc}";
345 # currencies and exchangerate
346 @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
347 %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
348 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
349 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
350 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
351 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
352 push @custom_hiddens, "forex";
353 push @custom_hiddens, "exchangerate" if $form->{forex};
356 $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
357 $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
360 $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
362 push @custom_hiddens, "ordnumber" if $form->{type} =~ /_quotation$/;
363 push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
365 $credittext = $locale->text('Credit Limit exceeded!!!');
367 my $follow_up_vc = $form->{ $form->{vc} eq 'customer' ? 'customer' : 'vendor' };
368 $follow_up_vc =~ s/--.*?//;
369 $TMPL_VAR{follow_up_trans_info} = ($form->{type} =~ /_quotation$/ ? $form->{quonumber} : $form->{ordnumber}) . " ($follow_up_vc)";
372 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
374 if (scalar @{ $follow_ups }) {
375 $TMPL_VAR{num_follow_ups} = scalar @{ $follow_ups };
376 $TMPL_VAR{num_due_follow_ups} = sum map { $_->{due} * 1 } @{ $follow_ups };
380 $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
381 : ($form->{resubmit}) ? "document.oe.submit()"
382 : ($creditwarning) ? "alert('$credittext')"
385 $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
386 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
387 $TMPL_VAR{onload} = $onload;
389 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
390 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
391 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
395 $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
396 qw(id action type vc formname media format proforma queued printed emailed
397 title creditlimit creditremaining tradediscount business
398 max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
399 shiptocity shiptocountry shiptocontact shiptophone shiptofax
400 shiptodepartment_1 shiptodepartment_2 shiptoemail
401 message email subject cc bcc taxpart taxservice taxaccounts cursor_fokus),
403 map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ]; # deleted: discount
407 is_sales => scalar ($form->{type} =~ /^sales_/), # these vars are exported, so that the template
408 is_order => scalar ($form->{type} =~ /_order$/), # may determine what to show
409 is_sales_quo => scalar ($form->{type} =~ /sales_quotation$/),
410 is_req_quo => scalar ($form->{type} =~ /request_quotation$/),
411 is_sales_ord => scalar ($form->{type} =~ /sales_order$/),
412 is_pur_ord => scalar ($form->{type} =~ /purchase_order$/),
415 print $form->parse_html_template("oe/form_header", { %TMPL_VAR });
417 $lxdebug->leave_sub();
421 $lxdebug->enter_sub();
425 $form->{invtotal} = $form->{invsubtotal};
427 $rows = max 2, $form->numtextrows($form->{notes}, 25, 8);
428 $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
429 $rows = max $rows, $introws;
431 $TMPL_VAR{notes} = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
432 $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
434 if (!$form->{taxincluded}) {
436 foreach $item (split / /, $form->{taxaccounts}) {
437 if ($form->{"${item}_base"}) {
438 $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
439 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
441 $TMPL_VAR{tax} .= qq|
443 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
444 <td align=right>$form->{"${item}_total"}</td>
449 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
452 foreach $item (split / /, $form->{taxaccounts}) {
453 if ($form->{"${item}_base"}) {
454 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
455 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
456 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
457 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
459 $TMPL_VAR{tax} .= qq|
461 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
462 <td align=right>$form->{"${item}_total"}</td>
465 <th align=right>Nettobetrag</th>
466 <td align=right>$form->{"${item}_netto"}</td>
472 $form->{oldinvtotal} = $form->{invtotal};
474 print $form->parse_html_template("oe/form_footer", {
477 print_options => print_options(inline => 1),
478 label_edit => $locale->text("Edit the $form->{type}"),
479 label_workflow => $locale->text("Workflow $form->{type}"),
482 $lxdebug->leave_sub();
486 $lxdebug->enter_sub();
488 my ($recursive_call) = shift;
492 set_headings($form->{"id"} ? "edit" : "add");
494 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate) unless $recursive_call;
497 $payment_id = $form->{payment_id} if $form->{payment_id};
499 &check_name($form->{vc});
501 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
504 $buysell = 'sell' if ($form->{vc} eq 'vendor');
505 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
506 $form->{exchangerate} = $form->{forex} if $form->{forex};
508 $exchangerate = $form->{exchangerate} || 1;
510 ##################### process items ######################################
512 $i = $form->{rowcount};
513 if ( ($form->{"partnumber_$i"} eq "")
514 && ($form->{"description_$i"} eq "")
515 && ($form->{"partsgroup_$i"} eq "")) {
517 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
522 if ($form->{type} =~ /^sales/) {
523 IS->retrieve_item(\%myconfig, \%$form);
525 IR->retrieve_item(\%myconfig, \%$form);
528 my $rows = scalar @{ $form->{item_list} };
530 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
533 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
542 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
543 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
544 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
545 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
546 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
548 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
550 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
551 $decimalplaces = max 2, length $1;
554 $form->{"sellprice_$i"} = $sellprice;
556 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
557 $form->{"sellprice_$i"} /= $exchangerate; # if there is an exchange rate adjust sellprice
560 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
561 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
562 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
563 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
565 $form->{creditremaining} -= $amount;
567 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
568 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
570 # get pricegroups for parts
571 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
573 # build up html code for prices_$i
580 # ok, so this is a new part
581 # ask if it is a part or service item
583 if ( $form->{"partsgroup_$i"}
584 && ($form->{"partsnumber_$i"} eq "")
585 && ($form->{"description_$i"} eq "")) {
587 $form->{"discount_$i"} = "";
591 $form->{"id_$i"} = 0;
596 ##################### process items ######################################
599 $lxdebug->leave_sub();
603 $lxdebug->enter_sub();
607 if ($form->{type} eq 'purchase_order') {
608 $form->{title} = $locale->text('Purchase Orders');
609 $form->{vc} = 'vendor';
610 $ordlabel = $locale->text('Order Number');
611 $ordnumber = 'ordnumber';
612 $employee = $locale->text('Employee');
615 if ($form->{type} eq 'request_quotation') {
616 $form->{title} = $locale->text('Request for Quotations');
617 $form->{vc} = 'vendor';
618 $ordlabel = $locale->text('RFQ Number');
619 $ordnumber = 'quonumber';
620 $employee = $locale->text('Employee');
623 if ($form->{type} eq 'sales_order') {
624 $form->{title} = $locale->text('Sales Orders');
625 $form->{vc} = 'customer';
626 $ordlabel = $locale->text('Order Number');
627 $ordnumber = 'ordnumber';
628 $employee = $locale->text('Employee');
631 if ($form->{type} eq 'sales_quotation') {
632 $form->{title} = $locale->text('Quotations');
633 $form->{vc} = 'customer';
634 $ordlabel = $locale->text('Quotation Number');
635 $ordnumber = 'quonumber';
636 $employee = $locale->text('Employee');
639 # setup vendor / customer selection
640 $form->all_vc(\%myconfig, $form->{vc},
641 ($form->{vc} eq 'customer') ? "AR" : "AP");
644 if (@{ $form->{all_departments} }) {
645 $form->{selectdepartment} = "<option>\n";
648 $form->{selectdepartment} .=
649 "<option>$_->{description}--$_->{id}</option>\n"
650 } (@{ $form->{all_departments} });
655 <th align=right nowrap>| . $locale->text('Department') . qq|</th>
656 <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
658 | if $form->{selectdepartment};
661 if (($form->{"type"} eq "sales_order") ||
662 ($form->{"type"} eq "purchase_order")) {
665 <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
666 <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
667 <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
668 <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
673 # use JavaScript Calendar or not
674 $form->{jsscript} = 1;
678 <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
679 <input type=button name=transdatefrom id="trigger3" value=|
680 . $locale->text('button') . qq|></td>
683 <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
684 <input type=button name=transdateto name=transdateto id="trigger4" value=|
685 . $locale->text('button') . qq|></td>
690 Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
691 "transdateto", "BL", "trigger4");
693 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
695 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
697 "employees" => "ALL_EMPLOYEES",
698 "salesmen" => "ALL_SALESMEN",
699 $vc => "ALL_" . uc($vc)
704 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
705 push(@values, $item->{"id"});
706 $labels{$item->{"id"}} = $item->{"projectnumber"};
709 NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
710 '-labels' => \%labels));
715 foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
716 push(@values, $item->{"id"});
717 $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
721 my %labels_salesmen = ();
722 my @values_salesmen = ('');
723 foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
724 push(@values_salesmen, $item->{"id"});
725 $labels_salesmen{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
728 my $employee_block = qq|
730 <th align="right">| . $locale->text('Employee') . qq|</th>
731 <td>| . NTI($cgi->popup_menu('-name' => 'employee_id', '-values' => \@values, '-labels' => \%labels)) . qq|</td>
734 <th align="right">| . $locale->text('Salesman') . qq|</th>
736 NTI($cgi->popup_menu('-name' => 'salesman_id',
737 '-values' => \@values_salesmen,
738 '-labels' => \%labels_salesmen)) . qq|
745 foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
746 push(@values, $item->{name}.qq|--|.$item->{"id"});
747 $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
750 my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
752 $myconfig{vclimit} <= scalar(@values)
753 ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">|
754 : NTI($cgi->popup_menu('-name' => "$form->{vc}",
755 '-default' => $form->{"old$form->{vc}"},
756 '-onChange' => 'document.getElementById(\'update_button\').click();',
757 '-values' => \@values,
758 '-labels' => \%labels));
764 <form method=post action=$form->{script}>
768 <th class=listtop>$form->{title}</th>
775 <th align=right>$vc_label</th>
776 <td colspan=3>$vc</td>
780 <th align=right>$ordlabel</th>
781 <td colspan=3><input name="$ordnumber" size=20></td>
785 <th align="right">| . $locale->text('Transaction description') . qq|</th>
786 <td colspan="3"><input name="transaction_description" size=20></td>
789 <th align="right">| . $locale->text("Project Number") . qq|</th>
790 <td colspan="3">$projectnumber</td>
793 <th align=right>| . $locale->text('From') . qq|</th> $button1
794 <th align=right>| . $locale->text('Bis') . qq|</th> $button2
796 <input type=hidden name=sort value=transdate>
798 <th align=right>| . $locale->text('Include in Report') . qq|</th>
802 <td><input type="checkbox" name="open" value="1" id="open" checked> <label for="open">| . $locale->text("Open") . qq|</td>
803 <td><input type="checkbox" name="closed" value="1" id="closed"> <label for="closed">| . $locale->text("Closed") . qq|</td>
807 <td><input name="l_id" class=checkbox type=checkbox value=Y> | . $locale->text('ID') . qq|</td>
808 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
811 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Date') . qq|</td>
812 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Required by') . qq|</td>
815 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
816 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
818 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> | . $locale->text('Ship via') . qq|</td>
821 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> | . $locale->text('Amount') . qq|</td>
822 <td><input name="l_tax" class=checkbox type=checkbox value=Y> | . $locale->text('Tax') . qq|</td>
823 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> | . $locale->text('Total') . qq|</td>
826 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag') . qq|</td>
827 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag prozentual') . qq|</td>
830 <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> | . $locale->text('Project Number') . qq|</td>
831 <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> | . $locale->text('Transaction description') . qq|</td>
834 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> | . $locale->text('Subtotal') . qq|</td>
835 <td><input name="l_salesman" class="checkbox" type="checkbox" value="Y"> | . $locale->text('Salesman') . qq|</td>
843 <tr><td colspan=4><hr size=3 noshade></td></tr>
849 <input type=hidden name=nextsub value=orders>
850 <input type=hidden name=vc value=$form->{vc}>
851 <input type=hidden name=type value=$form->{type}>
853 <input class=submit type=submit name=action value="|
854 . $locale->text('Continue') . qq|">
861 $lxdebug->leave_sub();
864 sub create_subtotal_row {
865 $lxdebug->enter_sub();
867 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
869 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
871 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
873 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
875 map { $totals->{$_} = 0 } @{ $subtotal_columns };
877 $lxdebug->leave_sub();
883 $lxdebug->enter_sub();
887 $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
889 ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
891 report_generator_set_default_sort('transdate', 1);
893 OE->transactions(\%myconfig, \%$form);
895 $form->{rowcount} = scalar @{ $form->{OE} };
898 "transdate", "reqdate",
904 "shipvia", "globalprojectnumber",
905 "transaction_description", "open",
906 "delivered", "marge_total", "marge_percent"
909 # only show checkboxes if gotten here via sales_order form.
910 my $allow_multiple_orders = $form->{type} eq 'sales_order';
911 if ($allow_multiple_orders) {
912 unshift @columns, "ids";
915 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
916 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
918 my $attachment_basename;
919 if ($form->{vc} eq 'vendor') {
920 if ($form->{type} eq 'purchase_order') {
921 $form->{title} = $locale->text('Purchase Orders');
922 $attachment_basename = $locale->text('purchase_order_list');
924 $form->{title} = $locale->text('Request for Quotations');
925 $attachment_basename = $locale->text('rfq_list');
929 if ($form->{type} eq 'sales_order') {
930 $form->{title} = $locale->text('Sales Orders');
931 $attachment_basename = $locale->text('sales_order_list');
933 $form->{title} = $locale->text('Quotations');
934 $attachment_basename = $locale->text('quotation_list');
938 my $report = SL::ReportGenerator->new(\%myconfig, $form);
940 my @hidden_variables = map { "l_${_}" } @columns;
941 push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
942 transaction_description transdatefrom transdateto type vc employee_id salesman_id);
944 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
947 'ids' => { 'text' => '', },
948 'transdate' => { 'text' => $locale->text('Date'), },
949 'reqdate' => { 'text' => $locale->text('Required by'), },
950 'id' => { 'text' => $locale->text('ID'), },
951 'ordnumber' => { 'text' => $locale->text('Order'), },
952 'quonumber' => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
953 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
954 'netamount' => { 'text' => $locale->text('Amount'), },
955 'tax' => { 'text' => $locale->text('Tax'), },
956 'amount' => { 'text' => $locale->text('Total'), },
957 'curr' => { 'text' => $locale->text('Curr'), },
958 'employee' => { 'text' => $locale->text('Employee'), },
959 'salesman' => { 'text' => $locale->text('Salesman'), },
960 'shipvia' => { 'text' => $locale->text('Ship via'), },
961 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
962 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
963 'open' => { 'text' => $locale->text('Open'), },
964 'delivered' => { 'text' => $locale->text('Delivered'), },
965 'marge_total' => { 'text' => $locale->text('Ertrag'), },
966 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), }
969 foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia)) {
970 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
971 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
974 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
976 $form->{"l_type"} = "Y";
977 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
978 $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
980 $report->set_columns(%column_defs);
981 $report->set_column_order(@columns);
982 $report->set_export_options('orders', @hidden_variables);
983 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
986 my ($department) = split m/--/, $form->{department};
988 push @options, $locale->text('Customer') . " : $form->{customer}" if $form->{customer};
989 push @options, $locale->text('Vendor') . " : $form->{vendor}" if $form->{vendor};
990 push @options, $locale->text('Department') . " : $department" if $form->{department};
991 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if $form->{ordnumber};
992 push @options, $locale->text('Notes') . " : $form->{notes}" if $form->{notes};
993 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if $form->{transaction_description};
994 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if $form->{transdatefrom};
995 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if $form->{transdateto};
996 push @options, $locale->text('Open') if $form->{open};
997 push @options, $locale->text('Closed') if $form->{closed};
998 push @options, $locale->text('Delivered') if $form->{delivered};
999 push @options, $locale->text('Not delivered') if $form->{notdelivered};
1001 $report->set_options('top_info_text' => join("\n", @options),
1002 'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
1003 'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
1004 'output_format' => 'HTML',
1005 'title' => $form->{title},
1006 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1008 $report->set_options_from_form();
1010 # add sort and escape callback, this one we use for the add sub
1011 $form->{callback} = $href .= "&sort=$form->{sort}";
1013 # escape callback for href
1014 $callback = $form->escape($href);
1016 my @subtotal_columns = qw(netamount amount marge_total marge_percent);
1018 my %totals = map { $_ => 0 } @subtotal_columns;
1019 my %subtotals = map { $_ => 0 } @subtotal_columns;
1023 my $edit_url = build_std_url('action=edit', 'type', 'vc');
1025 foreach $oe (@{ $form->{OE} }) {
1026 map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
1028 $oe->{tax} = $oe->{amount} - $oe->{netamount};
1029 $oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
1030 $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
1032 map { $subtotals{$_} += $oe->{$_};
1033 $totals{$_} += $oe->{$_} } @subtotal_columns;
1035 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1036 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1038 map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
1042 foreach my $column (@columns) {
1043 next if ($column eq 'ids');
1045 'data' => $oe->{$column},
1046 'align' => $column_alignment{$column},
1051 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
1052 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
1053 'valign' => 'center',
1054 'align' => 'center',
1057 $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1059 my $row_set = [ $row ];
1061 if (($form->{l_subtotal} eq 'Y')
1062 && (($idx == (scalar @{ $form->{OE} } - 1))
1063 || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
1064 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1067 $report->add_data($row_set);
1072 $report->add_separator();
1073 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1075 $report->generate_with_headers();
1077 $lxdebug->leave_sub();
1080 sub check_delivered_flag {
1081 $lxdebug->enter_sub();
1085 if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
1086 return $lxdebug->leave_sub();
1089 my $all_delivered = 0;
1091 foreach my $i (1 .. $form->{rowcount}) {
1092 next if (!$form->{"id_$i"});
1094 if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
1103 $form->{delivered} = 1 if $all_delivered;
1105 $lxdebug->leave_sub();
1108 sub save_and_close {
1109 $lxdebug->enter_sub();
1113 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1115 if ($form->{type} =~ /_order$/) {
1116 $form->isblank("transdate", $locale->text('Order Date missing!'));
1118 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1121 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1122 $form->{$idx} =~ s/^\s*//g;
1123 $form->{$idx} =~ s/\s*$//g;
1125 $msg = ucfirst $form->{vc};
1126 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1128 # $locale->text('Customer missing!');
1129 # $locale->text('Vendor missing!');
1131 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1132 if ($form->{currency} ne $form->{defaultcurrency});
1136 if($form->{payment_id}) {
1137 $payment_id = $form->{payment_id};
1140 # if the name changed get new values
1141 if (&check_name($form->{vc})) {
1142 if($form->{payment_id} eq "") {
1143 $form->{payment_id} = $payment_id;
1149 $form->{id} = 0 if $form->{saveasnew};
1151 # this is for the internal notes section for the [email] Subject
1152 if ($form->{type} =~ /_order$/) {
1153 if ($form->{type} eq 'sales_order') {
1154 $form->{label} = $locale->text('Sales Order');
1156 $numberfld = "sonumber";
1157 $ordnumber = "ordnumber";
1159 $form->{label} = $locale->text('Purchase Order');
1161 $numberfld = "ponumber";
1162 $ordnumber = "ordnumber";
1165 $err = $locale->text('Cannot save order!');
1167 check_delivered_flag();
1170 if ($form->{type} eq 'sales_quotation') {
1171 $form->{label} = $locale->text('Quotation');
1173 $numberfld = "sqnumber";
1174 $ordnumber = "quonumber";
1176 $form->{label} = $locale->text('Request for Quotation');
1178 $numberfld = "rfqnumber";
1179 $ordnumber = "quonumber";
1182 $err = $locale->text('Cannot save quotation!');
1186 # get new number in sequence if no number is given or if saveasnew was requested
1187 if (!$form->{$ordnumber} || $form->{saveasnew}) {
1188 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1193 $form->error($err) if (!OE->save(\%myconfig, \%$form));
1195 # saving the history
1196 if(!exists $form->{addition}) {
1197 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1198 $form->{addition} = "SAVED";
1199 $form->save_history($form->dbconnect(\%myconfig));
1201 # /saving the history
1203 $form->redirect($form->{label} . " $form->{$ordnumber} " .
1204 $locale->text('saved!'));
1206 $lxdebug->leave_sub();
1210 $lxdebug->enter_sub();
1214 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1217 if ($form->{type} =~ /_order$/) {
1218 $form->isblank("transdate", $locale->text('Order Date missing!'));
1220 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1223 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1224 $form->{$idx} =~ s/^\s*//g;
1225 $form->{$idx} =~ s/\s*$//g;
1227 $msg = ucfirst $form->{vc};
1228 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1230 # $locale->text('Customer missing!');
1231 # $locale->text('Vendor missing!');
1233 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1234 if ($form->{currency} ne $form->{defaultcurrency});
1238 if($form->{payment_id}) {
1239 $payment_id = $form->{payment_id};
1242 # if the name changed get new values
1243 if (&check_name($form->{vc})) {
1244 if($form->{payment_id} eq "") {
1245 $form->{payment_id} = $payment_id;
1251 $form->{id} = 0 if $form->{saveasnew};
1253 # this is for the internal notes section for the [email] Subject
1254 if ($form->{type} =~ /_order$/) {
1255 if ($form->{type} eq 'sales_order') {
1256 $form->{label} = $locale->text('Sales Order');
1258 $numberfld = "sonumber";
1259 $ordnumber = "ordnumber";
1261 $form->{label} = $locale->text('Purchase Order');
1263 $numberfld = "ponumber";
1264 $ordnumber = "ordnumber";
1267 $err = $locale->text('Cannot save order!');
1269 check_delivered_flag();
1272 if ($form->{type} eq 'sales_quotation') {
1273 $form->{label} = $locale->text('Quotation');
1275 $numberfld = "sqnumber";
1276 $ordnumber = "quonumber";
1278 $form->{label} = $locale->text('Request for Quotation');
1280 $numberfld = "rfqnumber";
1281 $ordnumber = "quonumber";
1284 $err = $locale->text('Cannot save quotation!');
1288 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1289 unless $form->{$ordnumber};
1293 OE->save(\%myconfig, \%$form);
1295 # saving the history
1296 if(!exists $form->{addition}) {
1297 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1298 $form->{addition} = "SAVED";
1299 $form->save_history($form->dbconnect(\%myconfig));
1301 # /saving the history
1303 $form->{simple_save} = 1;
1304 if(!$form->{print_and_save}) {
1305 set_headings("edit");
1309 $lxdebug->leave_sub();
1313 $lxdebug->enter_sub();
1319 if ($form->{type} =~ /_order$/) {
1320 $msg = $locale->text('Are you sure you want to delete Order Number');
1321 $ordnumber = 'ordnumber';
1323 $msg = $locale->text('Are you sure you want to delete Quotation Number');
1324 $ordnumber = 'quonumber';
1330 <form method=post action=$form->{script}>
1333 # delete action variable
1334 map { delete $form->{$_} } qw(action header);
1336 foreach $key (keys %$form) {
1337 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1338 $form->{$key} =~ s/\"/"/g;
1339 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1343 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1345 <h4>$msg $form->{$ordnumber}</h4>
1347 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1348 <input name=action class=submit type=submit value="|
1349 . $locale->text('Yes') . qq|">
1350 <button class=submit type=button onclick="history.back()">|
1351 . $locale->text('No') . qq|</button>
1358 $lxdebug->leave_sub();
1361 sub delete_order_quotation {
1362 $lxdebug->enter_sub();
1366 if ($form->{type} =~ /_order$/) {
1367 $msg = $locale->text('Order deleted!');
1368 $err = $locale->text('Cannot delete order!');
1370 $msg = $locale->text('Quotation deleted!');
1371 $err = $locale->text('Cannot delete quotation!');
1373 if (OE->delete(\%myconfig, \%$form, $spool)){
1374 # saving the history
1375 if(!exists $form->{addition}) {
1376 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1377 $form->{addition} = "DELETED";
1378 $form->save_history($form->dbconnect(\%myconfig));
1380 # /saving the history
1386 $lxdebug->leave_sub();
1390 $lxdebug->enter_sub();
1393 $auth->assert($form->{type} eq 'purchase_order' || $form->{type} eq 'request_quotation' ? 'vendor_invoice_edit' : 'invoice_edit');
1395 $form->{old_employee_id} = $form->{employee_id};
1396 $form->{old_salesman_id} = $form->{salesman_id};
1398 if ($form->{type} =~ /_order$/) {
1400 # these checks only apply if the items don't bring their own ordnumbers/transdates.
1401 # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1402 $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1403 if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1404 $form->isblank("transdate", $locale->text('Order Date missing!'))
1405 if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1407 # also copy deliverydate from the order
1408 $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1409 $form->{orddate} = $form->{transdate};
1411 $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1412 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1413 $form->{ordnumber} = "";
1414 $form->{quodate} = $form->{transdate};
1417 $payment_id = $form->{payment_id} if $form->{payment_id};
1419 # if the name changed get new values
1420 if (&check_name($form->{vc})) {
1421 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1426 $form->{cp_id} *= 1;
1428 for $i (1 .. $form->{rowcount}) {
1429 for (qw(ship qty sellprice listprice basefactor)) {
1430 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1434 if ( $form->{type} =~ /_order/
1435 && $form->{currency} ne $form->{defaultcurrency}) {
1437 # check if we need a new exchangerate
1438 $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1440 $orddate = $form->current_date(\%myconfig);
1441 $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1443 if (!$exchangerate) {
1444 &backorder_exchangerate($orddate, $buysell);
1449 $form->{convert_from_oe_ids} = $form->{id};
1450 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1451 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1452 $form->{shipto} = 1;
1453 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1455 delete @{$form}{qw(id closed)};
1456 $form->{rowcount}--;
1458 if ($form->{type} =~ /_order$/) {
1459 $form->{exchangerate} = $exchangerate;
1463 if ( $form->{type} eq 'purchase_order'
1464 || $form->{type} eq 'request_quotation') {
1465 $form->{title} = $locale->text('Add Vendor Invoice');
1466 $form->{script} = 'ir.pl';
1471 if ( $form->{type} eq 'sales_order'
1472 || $form->{type} eq 'sales_quotation') {
1473 $form->{title} = $locale->text('Add Sales Invoice');
1474 $form->{script} = 'is.pl';
1479 # bo creates the id, reset it
1480 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1481 $form->{ $form->{vc} } =~ s/--.*//g;
1482 $form->{type} = "invoice";
1485 $locale = new Locale "$myconfig{countrycode}", "$script";
1487 require "bin/mozilla/$form->{script}";
1489 map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1491 $currency = $form->{currency};
1494 $form->{currency} = $currency;
1495 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1496 $form->{exchangerate} = $form->{forex} || '';
1498 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1503 for $i (1 .. $form->{rowcount}) {
1504 $form->{"discount_$i"} =
1505 $form->format_amount(\%myconfig, $form->{"discount_$i"});
1507 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1509 $decimalplaces = ($dec > 2) ? $dec : 2;
1511 # copy delivery date from reqdate for order -> invoice conversion
1512 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1513 unless $form->{"deliverydate_$i"};
1515 $form->{"sellprice_$i"} =
1516 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1519 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1520 $dec_qty = length $dec_qty;
1522 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1524 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1525 qw(partnumber description unit);
1531 $lxdebug->leave_sub();
1534 sub backorder_exchangerate {
1535 $lxdebug->enter_sub();
1539 my ($orddate, $buysell) = @_;
1546 <form method=post action=$form->{script}>
1549 # delete action variable
1550 map { delete $form->{$_} } qw(action header exchangerate);
1552 foreach $key (keys %$form) {
1553 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1554 $form->{$key} =~ s/\"/"/g;
1555 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1558 $form->{title} = $locale->text('Add Exchangerate');
1562 <input type=hidden name=exchangeratedate value=$orddate>
1563 <input type=hidden name=buysell value=$buysell>
1566 <tr><th class=listtop>$form->{title}</th></tr>
1567 <tr height="5"></tr>
1572 <th align=right>| . $locale->text('Currency') . qq|</th>
1573 <td>$form->{currency}</td>
1576 <th align=right>| . $locale->text('Date') . qq|</th>
1580 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1581 <td><input name=exchangerate size=11></td>
1591 <input type=hidden name=nextsub value=save_exchangerate>
1593 <input name=action class=submit type=submit value="|
1594 . $locale->text('Continue') . qq|">
1602 $lxdebug->leave_sub();
1605 sub save_exchangerate {
1606 $lxdebug->enter_sub();
1608 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1609 $form->{exchangerate} =
1610 $form->parse_amount(\%myconfig, $form->{exchangerate});
1611 $form->save_exchangerate(\%myconfig, $form->{currency},
1612 $form->{exchangeratedate},
1613 $form->{exchangerate}, $form->{buysell});
1617 $lxdebug->leave_sub();
1620 sub create_backorder {
1621 $lxdebug->enter_sub();
1623 $form->{shipped} = 1;
1625 # figure out if we need to create a backorder
1626 # items aren't saved if qty != 0
1628 for $i (1 .. $form->{rowcount}) {
1629 $totalqty += $qty = $form->{"qty_$i"};
1630 $totalship += $ship = $form->{"ship_$i"};
1632 $form->{"qty_$i"} = $qty - $ship;
1635 if ($totalship == 0) {
1636 map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1637 $form->{ordtotal} = 0;
1638 $form->{shipped} = 0;
1642 if ($totalqty == $totalship) {
1643 map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1644 $form->{ordtotal} = 0;
1649 qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1652 for $i (1 .. $form->{rowcount}) {
1654 $form->{"${_}_$i"} =
1655 $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1656 } qw(sellprice discount);
1661 OE->save(\%myconfig, \%$form);
1663 # rebuild rows for invoice
1667 for $i (1 .. $form->{rowcount}) {
1668 $form->{"qty_$i"} = $form->{"ship_$i"};
1670 if ($form->{"qty_$i"}) {
1673 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1678 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1679 $form->{rowcount} = $count;
1681 $lxdebug->leave_sub();
1685 $lxdebug->enter_sub();
1689 $form->{saveasnew} = 1;
1690 $form->{closed} = 0;
1691 map { delete $form->{$_} } qw(printed emailed queued);
1693 # Let Lx-Office assign a new order number if the user hasn't changed the
1694 # previous one. If it has been changed manually then use it as-is.
1695 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1696 $form->{$idx} =~ s/^\s*//g;
1697 $form->{$idx} =~ s/\s*$//g;
1698 if ($form->{saved_xyznumber} &&
1699 ($form->{saved_xyznumber} eq $form->{$idx})) {
1700 delete($form->{$idx});
1705 $lxdebug->leave_sub();
1708 sub check_for_direct_delivery_yes {
1709 $lxdebug->enter_sub();
1713 $form->{direct_delivery_checked} = 1;
1714 delete @{$form}{grep /^shipto/, keys %{ $form }};
1715 map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1716 $form->{shipto} = 1;
1718 $lxdebug->leave_sub();
1721 sub check_for_direct_delivery_no {
1722 $lxdebug->enter_sub();
1726 $form->{direct_delivery_checked} = 1;
1727 delete @{$form}{grep /^shipto/, keys %{ $form }};
1730 $lxdebug->leave_sub();
1733 sub check_for_direct_delivery {
1734 $lxdebug->enter_sub();
1738 if ($form->{direct_delivery_checked}
1739 || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1740 $lxdebug->leave_sub();
1744 if ($form->{shipto_id}) {
1745 Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1748 map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1751 delete $form->{action};
1752 $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ($_ ne 'login') && ($_ ne 'password') && (ref $_ eq "") } keys %{ $form } ];
1755 print $form->parse_html_template("oe/check_for_direct_delivery");
1757 $lxdebug->leave_sub();
1762 sub purchase_order {
1763 $lxdebug->enter_sub();
1766 $auth->assert('purchase_order_edit');
1768 if ($form->{type} eq 'sales_order') {
1769 check_for_direct_delivery();
1772 if ($form->{type} =~ /^sales_/) {
1773 delete($form->{ordnumber});
1776 $form->{cp_id} *= 1;
1778 $form->{title} = $locale->text('Add Purchase Order');
1779 $form->{vc} = "vendor";
1780 $form->{type} = "purchase_order";
1784 $lxdebug->leave_sub();
1788 $lxdebug->enter_sub();
1791 $auth->assert('sales_order_edit');
1793 if ($form->{type} eq "purchase_order") {
1794 delete($form->{ordnumber});
1797 $form->{cp_id} *= 1;
1799 $form->{title} = $locale->text('Add Sales Order');
1800 $form->{vc} = "customer";
1801 $form->{type} = "sales_order";
1805 $lxdebug->leave_sub();
1809 $lxdebug->enter_sub();
1812 $auth->assert('purchase_order_edit | sales_order_edit');
1814 $form->{transdate} = $form->current_date(\%myconfig);
1815 delete $form->{duedate};
1817 $form->{convert_from_oe_ids} = $form->{id};
1818 $form->{closed} = 0;
1820 $form->{old_employee_id} = $form->{employee_id};
1821 $form->{old_salesman_id} = $form->{salesman_id};
1824 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal delivered);
1826 for $i (1 .. $form->{rowcount}) {
1827 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
1830 my %saved_vars = map { $_ => $form->{$_} } grep { $form->{$_} } qw(currency);
1834 map { $form->{$_} = $saved_vars{$_} } keys %saved_vars;
1839 for $i (1 .. $form->{rowcount} - 1) {
1840 map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
1845 $lxdebug->leave_sub();
1848 sub delivery_order {
1849 $lxdebug->enter_sub();
1851 if ($form->{type} =~ /^sales/) {
1852 $auth->assert('sales_delivery_order_edit');
1854 $form->{vc} = 'customer';
1855 $form->{type} = 'sales_delivery_order';
1858 $auth->assert('purchase_delivery_order_edit');
1860 $form->{vc} = 'vendor';
1861 $form->{type} = 'purchase_delivery_order';
1864 require "bin/mozilla/do.pl";
1866 $form->{cp_id} *= 1;
1867 $form->{convert_from_oe_ids} = $form->{id};
1868 $form->{transdate} = $form->current_date(\%myconfig);
1869 delete $form->{duedate};
1871 $form->{old_employee_id} = $form->{employee_id};
1872 $form->{old_salesman_id} = $form->{salesman_id};
1875 delete @{$form}{qw(id subject message cc bcc printed emailed queued creditlimit creditremaining discount tradediscount oldinvtotal closed delivered)};
1877 for $i (1 .. $form->{rowcount}) {
1878 map { $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if ($form->{"${_}_${i}"}) } qw(ship qty sellprice listprice basefactor);
1881 my %old_values = map { $_ => $form->{$_} } qw(customer_id oldcustomer customer vendor_id oldvendor vendor);
1887 map { $form->{$_} = $old_values{$_} if ($old_values{$_}) } keys %old_values;
1891 $lxdebug->leave_sub();
1895 $lxdebug->enter_sub();
1899 $form->{print_and_save} = 1;
1903 my $saved_form = save_form();
1907 restore_form($saved_form, 0, qw(id ordnumber quonumber));
1911 $lxdebug->leave_sub();
1915 call_sub($form->{yes_nextsub});
1919 call_sub($form->{no_nextsub});
1922 ######################################################################################################
1924 # ###############################################################################################
1926 $lxdebug->enter_sub();
1930 retrieve_partunits() if ($form->{type} =~ /_delivery_order$/);
1932 $form->{"taxaccounts"} =~ s/\s*$//;
1933 $form->{"taxaccounts"} =~ s/^\s*//;
1934 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1935 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1937 $form->{"taxaccounts"} = "";
1939 for my $i (1 .. $form->{"rowcount"}) {
1940 IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
1943 $form->{rowcount}++;
1944 $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
1946 $form->language_payment(\%myconfig);
1948 Common::webdav_folder($form) if ($webdav);
1953 display_row($form->{rowcount}) if $form->{rowcount};
1957 $lxdebug->leave_sub();
1960 sub report_for_todo_list {
1961 $lxdebug->enter_sub();
1963 my $quotations = OE->transactions_for_todo_list();
1966 if (@{ $quotations }) {
1967 my $edit_url = build_std_url('script=oe.pl', 'action=edit', 'type=sales_quotation', 'vc=customer');
1969 $content = $form->parse_html_template('oe/report_for_todo_list', { 'QUOTATIONS' => $quotations,
1970 'edit_url' => $edit_url });
1973 $lxdebug->leave_sub();