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);
40 use SL::ReportGenerator;
41 use List::Util qw(max reduce);
43 require "bin/mozilla/io.pl";
44 require "bin/mozilla/arap.pl";
45 require "bin/mozilla/reportgenerator.pl";
52 # $locale->text('Edit the purchase_order');
53 # $locale->text('Edit the sales_order');
54 # $locale->text('Edit the request_quotation');
55 # $locale->text('Edit the sales_quotation');
57 # $locale->text('Workflow purchase_order');
58 # $locale->text('Workflow sales_order');
59 # $locale->text('Workflow request_quotation');
60 # $locale->text('Workflow sales_quotation');
63 $lxdebug->enter_sub();
67 if ($form->{type} eq 'purchase_order') {
68 $form->{title} = $action eq "edit" ?
69 $locale->text('Edit Purchase Order') :
70 $locale->text('Add Purchase Order');
71 $form->{heading} = $locale->text('Purchase Order');
72 $form->{vc} = 'vendor';
74 if ($form->{type} eq 'sales_order') {
75 $form->{title} = $action eq "edit" ?
76 $locale->text('Edit Sales Order') :
77 $locale->text('Add Sales Order');
78 $form->{heading} = $locale->text('Sales Order');
79 $form->{vc} = 'customer';
81 if ($form->{type} eq 'request_quotation') {
82 $form->{title} = $action eq "edit" ?
83 $locale->text('Edit Request for Quotation') :
84 $locale->text('Add Request for Quotation');
85 $form->{heading} = $locale->text('Request for Quotation');
86 $form->{vc} = 'vendor';
88 if ($form->{type} eq 'sales_quotation') {
89 $form->{title} = $action eq "edit" ?
90 $locale->text('Edit Quotation') :
91 $locale->text('Add Quotation');
92 $form->{heading} = $locale->text('Quotation');
93 $form->{vc} = 'customer';
96 $lxdebug->leave_sub();
100 $lxdebug->enter_sub();
105 "$form->{script}?action=add&type=$form->{type}&vc=$form->{vc}&login=$form->{login}&password=$form->{password}"
106 unless $form->{callback};
112 $lxdebug->leave_sub();
116 $lxdebug->enter_sub();
117 # show history button
118 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
119 #/show hhistory button
121 $form->{simple_save} = 0;
123 set_headings("edit");
125 # editing without stuff to edit? try adding it first
126 if ($form->{rowcount} && !$form->{print_and_save}) {
127 map { $id++ if $form->{"multi_id_$_"} } (1 .. $form->{rowcount});
131 undef $form->{rowcount};
133 $lxdebug->leave_sub();
136 } elsif (!$form->{id}) {
138 $lxdebug->leave_sub();
142 if ($form->{print_and_save}) {
143 $form->{action} = "print";
144 $form->{resubmit} = 1;
145 $language_id = $form->{language_id};
146 $printer_id = $form->{printer_id};
149 set_headings("edit");
153 if ($form->{print_and_save}) {
154 $form->{language_id} = $language_id;
155 $form->{printer_id} = $printer_id;
159 $lxdebug->leave_sub();
163 $lxdebug->enter_sub();
164 # get customer/vendor
165 $form->all_vc(\%myconfig, $form->{vc}, ($form->{vc} eq 'customer') ? "AR" : "AP");
167 # retrieve order/quotation
168 $form->{webdav} = $webdav;
169 $form->{jsscript} = 1;
171 my $editing = $form->{id};
173 OE->retrieve(\%myconfig, \%$form);
175 if ($form->{payment_id}) {
176 $payment_id = $form->{payment_id};
178 if ($form->{language_id}) {
179 $language_id = $form->{language_id};
181 if ($form->{taxzone_id}) {
182 $taxzone_id = $form->{taxzone_id};
185 $salesman_id = $form->{salesman_id} if ($editing);
188 # if multiple rowcounts (== collective order) then check if the
189 # there were more than one customer (in that case OE::retrieve removes
190 # the content from the field)
191 if ( $form->{rowcount}
192 && $form->{type} eq 'sales_order'
193 && defined $form->{customer}
194 && $form->{customer} eq '') {
196 # $main::lxdebug->message(0, "Detected Edit order with concurrent customers");
199 'Collective Orders only work for orders from one customer!')
203 $taxincluded = $form->{taxincluded};
204 $form->{shipto} = 1 if $form->{id};
206 if ($form->{"all_$form->{vc}"}) {
207 unless ($form->{"$form->{vc}_id"}) {
208 $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
212 $cp_id = $form->{cp_id};
213 $intnotes = $form->{intnotes};
215 # get customer / vendor
216 if ($form->{type} =~ /(purchase_order|request_quotation)/) {
217 IR->get_vendor(\%myconfig, \%$form);
219 #quote all_vendor Bug 133
220 foreach $ref (@{ $form->{all_vendor} }) {
221 $ref->{name} = $form->quote($ref->{name});
225 if ($form->{type} =~ /sales_(order|quotation)/) {
226 IS->get_customer(\%myconfig, \%$form);
228 #quote all_vendor Bug 133
229 foreach $ref (@{ $form->{all_customer} }) {
230 $ref->{name} = $form->quote($ref->{name});
234 $form->{cp_id} = $cp_id;
237 $form->{payment_id} = $payment_id;
240 $form->{language_id} = $language_id;
243 $form->{taxzone_id} = $taxzone_id;
245 $form->{intnotes} = $intnotes if $intnotes;
246 ($form->{ $form->{vc} }) = split /--/, $form->{ $form->{vc} };
247 $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
249 # build the popup menus
250 if (@{ $form->{"all_$form->{vc}"} }) {
251 $form->{ $form->{vc} } =
252 qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
253 map { $form->{"select$form->{vc}"} .=
254 "<option>$_->{name}--$_->{id}</option>\n" }
255 (@{ $form->{"all_$form->{vc}"} });
258 $form->{taxincluded} = $taxincluded if ($form->{id});
261 if (@{ $form->{all_departments} }) {
262 $form->{selectdepartment} = "<option>\n";
263 $form->{department} = "$form->{department}--$form->{department_id}";
266 $form->{selectdepartment} .= "<option>$_->{description}--$_->{id}</option>\n"
267 } @{ $form->{all_departments} };
270 $form->{employee} = "$form->{employee}--$form->{employee_id}";
273 $form->{forex} = $form->{exchangerate};
275 $form->{salesman_id} = $salesman_id if ($editing);
277 $lxdebug->leave_sub();
281 $lxdebug->enter_sub();
282 $form->{formname} = $form->{type} unless $form->{formname};
285 foreach $ref (@{ $form->{form_details} }) {
286 $form->{rowcount} = ++$i;
287 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
289 for my $i (1 .. $form->{rowcount}) {
290 $form->{"reqdate_$i"} = $form->{"deliverydate_$i"} unless $form->{"reqdate_$i"};
291 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * ($form->{id} ? 100 : 1));
292 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
293 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
294 map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit);
297 $lxdebug->leave_sub();
301 $lxdebug->enter_sub();
305 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
306 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
308 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
310 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
311 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
313 map { $form->{$_} =~ s/\"/"/g }
314 qw(ordnumber quonumber shippingpoint shipvia notes intnotes shiptoname
315 shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact
316 shiptophone shiptofax shiptodepartment_1 shiptodepartment_2);
318 # use JavaScript Calendar or not
319 $form->{jsscript} = 1;
320 $TMPL_VAR{button1} = qq|
321 <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\">
322 <input type=button name=transdate id="trigger1" value=| . $locale->text('button') . qq|></td>
324 $TMPL_VAR{button2} = qq|
325 <td width="13"><input name=reqdate id=reqdate size=11 title="$myconfig{dateformat}" value="$form->{reqdate}" onBlur=\"check_right_date_format(this)\">
326 <input type=button name=reqdate name=reqdate id="trigger2" value=| . $locale->text('button') . qq|></td>
329 $TMPL_VAR{jsscript} = Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1", "reqdate", "BL", "trigger2");
331 # openclosed checkboxes
333 push @tmp, sprintf qq|<input name="delivered" id="delivered" type="checkbox" class="checkbox" value="1" %s><label for="delivered">%s</label>|,
334 $form->{"delivered"} ? "checked" : "", $locale->text('Delivered') if $form->{"type"} =~ /_order$/;
335 push @tmp, sprintf qq|<input name="closed" id="closed" type="checkbox" class="checkbox" value="1" %s><label for="closed">%s</label>|,
336 $form->{"closed"} ? "checked" : "", $locale->text('Closed') if $form->{id};
337 $TMPL_VAR{openclosed} = sprintf qq|<tr><td colspan=%d align=center>%s</td></tr>\n|, 2 * scalar @tmp, join "\n", @tmp if @tmp;
339 # set option selected
340 foreach $item ($form->{vc}, 'currency', 'department', ($form->{vc} eq "customer" ? 'customer' : 'vendor')) {
341 $form->{"select$item"} =~ s/ selected//;
342 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
345 #quote select[customer|vendor] Bug 133
346 $form->{"select$form->{vc}"} = $form->quote($form->{"select$form->{vc}"});
348 #substitute \n and \r to \s (bug 543)
349 $form->{"select$form->{vc}"} =~ s/[\n\r]/ /g;
351 my @old_project_ids = ($form->{"globalproject_id"}, grep { $_ } map { $form->{"project_id_$_"} } 1..$form->{"rowcount"});
353 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
354 $form->get_lists("contacts" => "ALL_CONTACTS",
355 "shipto" => "ALL_SHIPTO",
356 "projects" => { "key" => "ALL_PROJECTS",
358 "old_id" => \@old_project_ids },
359 "employees" => "ALL_EMPLOYEES",
360 "salesmen" => "ALL_SALESMEN",
361 "taxzones" => "ALL_TAXZONES",
362 "payments" => "ALL_PAYMENTS",
363 "currencies" => "ALL_CURRENCIES",
364 $vc => { key => "ALL_" . uc($vc),
365 limit => $myconfig{vclimit} + 1 },
366 "price_factors" => "ALL_PRICE_FACTORS");
369 @values = ("", map { $_->{cp_id} } @{ $form->{ALL_CONTACTS} });
370 %labels = map { $_->{cp_id} => $_->{"cp_name"} . ($_->{cp_abteilung} ? " ($_->{cp_abteilung})" : "") } @{ $form->{ALL_CONTACTS} };
371 $TMPL_VAR{contact} = NTI($cgi->popup_menu('-name' => 'cp_id', '-values' => \@values, '-style' => 'width: 250px',
372 '-labels' => \%labels, '-default' => $form->{"cp_id"})) if scalar @values > 1;
375 @values = map { "$_->{name}--$_->{id}" } @{ $form->{uc "all_$form->{vc}s" } };
376 %labels = map { +"$_->{name}--$_->{id}" => $_->{name} } @{ $form->{uc "all_$form->{vc}s" } };
377 push @custom_hiddens, "$form->{vc}_id";
378 push @custom_hiddens, "old$form->{vc}";
379 push @custom_hiddens, "select$form->{vc}";
380 $TMPL_VAR{vc} = sprintf qq|<th align="right">%s</th><td>%s<input type="button" value="?" onclick="show_vc_details('$form->{vc}')"></td>|,
381 $locale->text(ucfirst $form->{vc}),
382 ($myconfig{vclimit} <= scalar(@values))
383 ? $cgi->textfield(-value => H($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/), -name => $form->{vc})
384 : NTI($cgi->popup_menu('-name' => "$form->{vc}", '-default' => $form->{"old$form->{vc}"},
385 '-onChange' => 'document.getElementById(\'update_button\').click();',
386 '-values' => \@values, '-labels' => \%labels, '-style' => 'width: 250px'));
388 # payments (for footer)
389 @values = ("", map { $_->{id} } @{ $form->{ALL_PAYMENTS} });
390 %labels = map { $_->{id} => $_->{description} } @{ $form->{ALL_PAYMENTS} };
391 $TMPL_VAR{payments} = NTI($cgi->popup_menu('-name' => 'payment_id', '-values' => \@values, '-style' => 'width: 250px',
392 '-labels' => \%labels, '-default' => $form->{payment_id}));
395 @values = ("", map { $_->{shipto_id} } @{ $form->{ALL_SHIPTO} });
396 %labels = map { my $item=$_; $_->{shipto_id} => join "; ", grep { $_ } map { $item->{"shipto${_}" } } qw(name department_1 street city) } @{ $form->{ALL_SHIPTO} };
397 $TMPL_VAR{shipto} = NTI($cgi->popup_menu('-name' => 'shipto_id', '-values' => \@values, '-style' => 'width: 250px',
398 '-labels' => \%labels, '-default' => $form->{"shipto_id"})) if scalar @values > 1;
401 @values = ("", map { $_->{id} } @{ $form->{ALL_PROJECTS} });
402 %labels = map { $_->{id} => $_->{projectnumber} } @{ $form->{ALL_PROJECTS} };
403 $TMPL_VAR{globalprojectnumber} = NTI($cgi->popup_menu('-name' => 'globalproject_id', '-values' => \@values,
404 '-labels' => \%labels, '-default' => $form->{"globalproject_id"}));
407 @values = map { $_->{id} } @{ $form->{ALL_SALESMEN} };
408 %labels = map { $_->{id} => ($_->{name} || $_->{login}) } @{ $form->{ALL_SALESMEN} };
409 $TMPL_VAR{salesmen} = NTI($cgi->popup_menu('-name' => 'salesman_id', '-default' => $form->{"salesman_id"} ? $form->{"salesman_id"} : $form->{"employee_id"},
410 '-values' => \@values, '-labels' => \%labels)) if $form->{type} =~ /^sales_/ && scalar @values;
413 @values = map { $_->{id} } @{ $form->{ALL_EMPLOYEES} };
414 %labels = map { $_->{id} => $_->{name} || $_->{login} } @{ $form->{ALL_EMPLOYEES} };
415 $TMPL_VAR{employee} = NTI($cgi->popup_menu('-name' => 'employee_id', '-default' => $form->{"employee_id"},
416 '-values' => \@values, '-labels' => \%labels));
419 @values = map { $_->{id} } @{ $form->{ALL_TAXZONES} };
420 %labels = map { $_->{id} => $_->{description} } @{ $form->{ALL_TAXZONES} };
421 $TMPL_VAR{taxzone} = NTI($cgi->popup_menu('-name' => 'taxzone_id', '-default' => $form->{"taxzone_id"},
422 '-values' => \@values, '-labels' => \%labels, '-style' => 'width: 250px'));
424 # currencies and exchangerate
425 @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
426 %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
427 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
428 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency} && $form->{exchangerate};
429 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
430 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
431 push @custom_hiddens, "forex";
432 push @custom_hiddens, "exchangerate" if $form->{forex};
435 $creditwarning = (($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update}) ? 1 : 0;
436 $TMPL_VAR{is_credit_remaining_negativ} = ($form->{creditremaining} =~ /-/) ? "0" : "1";
439 $TMPL_VAR{business_label} = ($form->{vc} eq "customer" ? $locale->text('Customer type') : $locale->text('Vendor type'));
441 push @custom_hiddens, "ordnumber" if $form->{type} =~ /_quotation$/;
442 push @custom_hiddens, "customer_klass" if $form->{vc} eq 'customer';
444 $credittext = $locale->text('Credit Limit exceeded!!!');
446 $onload = ($form->{resubmit} && ($form->{format} eq "html")) ? "window.open('about:blank','Beleg'); document.oe.target = 'Beleg';document.oe.submit()"
447 : ($form->{resubmit}) ? "document.oe.submit()"
448 : ($creditwarning) ? "alert('$credittext')"
451 $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
452 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
453 $TMPL_VAR{onload} = $onload;
455 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
456 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
457 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
461 $TMPL_VAR{HIDDENS} = [ map { name => $_, value => $form->{$_} },
462 qw(id action type vc formname media format proforma queued printed emailed
463 title creditlimit creditremaining tradediscount business
464 max_dunning_level dunning_amount shiptoname shiptostreet shiptozipcode
465 shiptocity shiptocountry shiptocontact shiptophone shiptofax
466 shiptodepartment_1 shiptodepartment_2 shiptoemail
467 message email subject cc bcc taxpart taxservice taxaccounts),
469 map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} ]; # deleted: discount
473 is_sales => scalar ($form->{type} =~ /^sales_/), # these vars are exported, so that the template
474 is_order => scalar ($form->{type} =~ /_order$/), # may determine what to show
475 is_sales_quo => scalar ($form->{type} =~ /sales_quotation$/),
476 is_req_quo => scalar ($form->{type} =~ /request_quotation$/),
477 is_sales_ord => scalar ($form->{type} =~ /sales_order$/),
478 is_pur_ord => scalar ($form->{type} =~ /purchase_order$/),
481 print $form->parse_html_template2("oe/orders_header", { %TMPL_VAR });
483 $lxdebug->leave_sub();
487 $lxdebug->enter_sub();
489 $form->{invtotal} = $form->{invsubtotal};
491 $rows = max 2, $form->numtextrows($form->{notes}, 25, 8);
492 $introws = max 2, $form->numtextrows($form->{intnotes}, 35, 8);
493 $rows = max $rows, $introws;
495 $TMPL_VAR{notes} = qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
496 $TMPL_VAR{intnotes} = qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
498 if (!$form->{taxincluded}) {
500 foreach $item (split / /, $form->{taxaccounts}) {
501 if ($form->{"${item}_base"}) {
502 $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
503 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
505 $TMPL_VAR{tax} .= qq|
507 <th align=right>$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
508 <td align=right>$form->{"${item}_total"}</td>
513 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
516 foreach $item (split / /, $form->{taxaccounts}) {
517 if ($form->{"${item}_base"}) {
518 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
519 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
520 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
521 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
523 $TMPL_VAR{tax} .= qq|
525 <th align=right>Enthaltene $form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
526 <td align=right>$form->{"${item}_total"}</td>
529 <th align=right>Nettobetrag</th>
530 <td align=right>$form->{"${item}_netto"}</td>
536 $form->{oldinvtotal} = $form->{invtotal};
538 print $form->parse_html_template2("oe/orders_footer", {
541 print_options => print_options(inline => 1),
542 label_edit => $locale->text("Edit the $form->{type}"),
543 label_workflow => $locale->text("Workflow $form->{type}"),
546 $lxdebug->leave_sub();
550 $lxdebug->enter_sub();
552 my ($recursive_call) = shift;
554 set_headings($form->{"id"} ? "edit" : "add");
556 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
559 $payment_id = $form->{payment_id} if $form->{payment_id};
561 &check_name($form->{vc});
563 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
566 $buysell = 'sell' if ($form->{vc} eq 'vendor');
567 $form->{exchangerate} = $exchangerate if
568 $form->{forex} = $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate}, $buysell);
570 $exchangerate = $form->{exchangerate} || 1;
572 ##################### process items ######################################
574 $i = $form->{rowcount};
575 if ( ($form->{"partnumber_$i"} eq "")
576 && ($form->{"description_$i"} eq "")
577 && ($form->{"partsgroup_$i"} eq "")) {
579 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
584 if ($form->{type} =~ /^sales/) {
585 IS->retrieve_item(\%myconfig, \%$form);
587 IR->retrieve_item(\%myconfig, \%$form);
590 my $rows = scalar @{ $form->{item_list} };
592 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
595 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
604 $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
605 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
606 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
607 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
608 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
610 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
612 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
613 $decimalplaces = max 2, length $1;
616 $form->{"sellprice_$i"} = $sellprice;
618 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
619 $form->{"sellprice_$i"} /= $exchangerate; # if there is an exchange rate adjust sellprice
622 $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
623 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
624 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
625 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
627 $form->{creditremaining} -= $amount;
629 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
630 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
632 # get pricegroups for parts
633 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
635 # build up html code for prices_$i
642 # ok, so this is a new part
643 # ask if it is a part or service item
645 if ( $form->{"partsgroup_$i"}
646 && ($form->{"partsnumber_$i"} eq "")
647 && ($form->{"description_$i"} eq "")) {
649 $form->{"discount_$i"} = "";
653 $form->{"id_$i"} = 0;
658 ##################### process items ######################################
661 $lxdebug->leave_sub();
665 $lxdebug->enter_sub();
667 if ($form->{type} eq 'purchase_order') {
668 $form->{title} = $locale->text('Purchase Orders');
669 $form->{vc} = 'vendor';
670 $ordlabel = $locale->text('Order Number');
671 $ordnumber = 'ordnumber';
672 $employee = $locale->text('Employee');
675 if ($form->{type} eq 'request_quotation') {
676 $form->{title} = $locale->text('Request for Quotations');
677 $form->{vc} = 'vendor';
678 $ordlabel = $locale->text('RFQ Number');
679 $ordnumber = 'quonumber';
680 $employee = $locale->text('Employee');
683 if ($form->{type} eq 'sales_order') {
684 $form->{title} = $locale->text('Sales Orders');
685 $form->{vc} = 'customer';
686 $ordlabel = $locale->text('Order Number');
687 $ordnumber = 'ordnumber';
688 $employee = $locale->text('Employee');
691 if ($form->{type} eq 'sales_quotation') {
692 $form->{title} = $locale->text('Quotations');
693 $form->{vc} = 'customer';
694 $ordlabel = $locale->text('Quotation Number');
695 $ordnumber = 'quonumber';
696 $employee = $locale->text('Employee');
699 # setup vendor / customer selection
700 $form->all_vc(\%myconfig, $form->{vc},
701 ($form->{vc} eq 'customer') ? "AR" : "AP");
704 if (@{ $form->{all_departments} }) {
705 $form->{selectdepartment} = "<option>\n";
708 $form->{selectdepartment} .=
709 "<option>$_->{description}--$_->{id}</option>\n"
710 } (@{ $form->{all_departments} });
715 <th align=right nowrap>| . $locale->text('Department') . qq|</th>
716 <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
718 | if $form->{selectdepartment};
721 if (($form->{"type"} eq "sales_order") ||
722 ($form->{"type"} eq "purchase_order")) {
725 <td><input name="notdelivered" id="notdelivered" class="checkbox" type="checkbox" value="1" checked>
726 <label for="notdelivered">|. $locale->text('Not delivered') . qq|</label></td>
727 <td><input name="delivered" id="delivered" class="checkbox" type="checkbox" value="1" checked>
728 <label for="delivered">| . $locale->text('Delivered') . qq|</label></td>
733 # use JavaScript Calendar or not
734 $form->{jsscript} = 1;
738 <td><input name=transdatefrom id=transdatefrom size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
739 <input type=button name=transdatefrom id="trigger3" value=|
740 . $locale->text('button') . qq|></td>
743 <td><input name=transdateto id=transdateto size=11 title="$myconfig{dateformat}" onBlur=\"check_right_date_format(this)\">
744 <input type=button name=transdateto name=transdateto id="trigger4" value=|
745 . $locale->text('button') . qq|></td>
750 Form->write_trigger(\%myconfig, "2", "transdatefrom", "BR", "trigger3",
751 "transdateto", "BL", "trigger4");
753 my $vc = $form->{vc} eq "customer" ? "customers" : "vendors";
755 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
757 "employees" => "ALL_EMPLOYEES",
758 "salesmen" => "ALL_SALESMEN",
759 $vc => "ALL_" . uc($vc)
764 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
765 push(@values, $item->{"id"});
766 $labels{$item->{"id"}} = $item->{"projectnumber"};
769 NTI($cgi->popup_menu('-name' => 'project_id', '-values' => \@values,
770 '-labels' => \%labels));
775 foreach my $item (@{ $form->{"ALL_EMPLOYEES"} }) {
776 push(@values, $item->{"id"});
777 $labels{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
781 my %labels_salesmen = ();
782 my @values_salesmen = ('');
783 foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
784 push(@values_salesmen, $item->{"id"});
785 $labels_salesmen{$item->{"id"}} = $item->{"name"} ne "" ? $item->{"name"} : $item->{"login"};
788 my $employee_block = qq|
790 <th align="right">| . $locale->text('Employee') . qq|</th>
791 <td>| . NTI($cgi->popup_menu('-name' => 'employee_id', '-values' => \@values, '-labels' => \%labels)) . qq|</td>
794 <th align="right">| . $locale->text('Salesman') . qq|</th>
796 NTI($cgi->popup_menu('-name' => 'salesman_id',
797 '-values' => \@values_salesmen,
798 '-labels' => \%labels_salesmen)) . qq|
805 foreach my $item (@{ $form->{($form->{vc} eq "customer" ? "ALL_CUSTOMERS" : "ALL_VENDORS")}}) {
806 push(@values, $item->{name}.qq|--|.$item->{"id"});
807 $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
810 my $vc_label = $form->{vc} eq "customer" ? $locale->text('Customer') : $locale->text('Vendor');
812 $myconfig{vclimit} <= scalar(@values)
813 ? qq|<input type="text" value="| . H(($form->{"old$form->{vc}"} =~ /^(.*)\-\-.*$/)) . qq|" name="$form->{vc}">|
814 : NTI($cgi->popup_menu('-name' => "$form->{vc}",
815 '-default' => $form->{"old$form->{vc}"},
816 '-onChange' => 'document.getElementById(\'update_button\').click();',
817 '-values' => \@values,
818 '-labels' => \%labels));
824 <form method=post action=$form->{script}>
828 <th class=listtop>$form->{title}</th>
835 <th align=right>$vc_label</th>
836 <td colspan=3>$vc</td>
840 <th align=right>$ordlabel</th>
841 <td colspan=3><input name="$ordnumber" size=20></td>
845 <th align="right">| . $locale->text('Transaction description') . qq|</th>
846 <td colspan="3"><input name="transaction_description" size=20></td>
849 <th align="right">| . $locale->text("Project Number") . qq|</th>
850 <td colspan="3">$projectnumber</td>
853 <th align=right>| . $locale->text('From') . qq|</th> $button1
854 <th align=right>| . $locale->text('Bis') . qq|</th> $button2
856 <input type=hidden name=sort value=transdate>
858 <th align=right>| . $locale->text('Include in Report') . qq|</th>
862 <td><input type="checkbox" name="open" value="1" id="open" checked> <label for="open">| . $locale->text("Open") . qq|</td>
863 <td><input type="checkbox" name="closed" value="1" id="closed"> <label for="closed">| . $locale->text("Closed") . qq|</td>
867 <td><input name="l_id" class=checkbox type=checkbox value=Y> | . $locale->text('ID') . qq|</td>
868 <td><input name="l_$ordnumber" class=checkbox type=checkbox value=Y checked> $ordlabel</td>
869 <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Date') . qq|</td>
870 <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> | . $locale->text('Required by') . qq|</td>
873 <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vc_label</td>
874 <td><input name="l_employee" class=checkbox type=checkbox value=Y checked> $employee</td>
876 <td><input name="l_shipvia" class=checkbox type=checkbox value=Y> | . $locale->text('Ship via') . qq|</td>
879 <td><input name="l_netamount" class=checkbox type=checkbox value=Y> | . $locale->text('Amount') . qq|</td>
880 <td><input name="l_tax" class=checkbox type=checkbox value=Y> | . $locale->text('Tax') . qq|</td>
881 <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> | . $locale->text('Total') . qq|</td>
884 <td><input name="l_marge_total" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag') . qq|</td>
885 <td><input name="l_marge_percent" class=checkbox type=checkbox value=Y> | . $locale->text('Ertrag prozentual') . qq|</td>
888 <td><input name="l_globalprojectnumber" class=checkbox type=checkbox value=Y> | . $locale->text('Project Number') . qq|</td>
889 <td><input name="l_transaction_description" class=checkbox type=checkbox value=Y> | . $locale->text('Transaction description') . qq|</td>
892 <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> | . $locale->text('Subtotal') . qq|</td>
893 <td><input name="l_salesman" class="checkbox" type="checkbox" value="Y"> | . $locale->text('Salesman') . qq|</td>
901 <tr><td colspan=4><hr size=3 noshade></td></tr>
907 <input type=hidden name=nextsub value=orders>
908 <input type=hidden name=login value=$form->{login}>
909 <input type=hidden name=password value=$form->{password}>
910 <input type=hidden name=vc value=$form->{vc}>
911 <input type=hidden name=type value=$form->{type}>
913 <input class=submit type=submit name=action value="|
914 . $locale->text('Continue') . qq|">
921 $lxdebug->leave_sub();
924 sub create_subtotal_row {
925 $lxdebug->enter_sub();
927 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
929 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
931 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
933 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
935 map { $totals->{$_} = 0 } @{ $subtotal_columns };
937 $lxdebug->leave_sub();
943 $lxdebug->enter_sub();
945 $ordnumber = ($form->{type} =~ /_order$/) ? "ordnumber" : "quonumber";
947 ($form->{ $form->{vc} }, $form->{"${form->{vc}}_id"}) = split(/--/, $form->{ $form->{vc} });
949 $form->{sort} ||= 'transdate';
951 OE->transactions(\%myconfig, \%$form);
953 $form->{rowcount} = scalar @{ $form->{OE} };
956 "transdate", "reqdate",
962 "shipvia", "globalprojectnumber",
963 "transaction_description", "open",
964 "delivered", "marge_total", "marge_percent"
967 # only show checkboxes if gotten here via sales_order form.
968 my $allow_multiple_orders = $form->{type} eq 'sales_order';
969 if ($allow_multiple_orders) {
970 unshift @columns, "ids";
973 $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed});
974 $form->{l_delivered} = "Y" if ($form->{delivered} && $form->{notdelivered});
976 my $attachment_basename;
977 if ($form->{vc} eq 'vendor') {
978 if ($form->{type} eq 'purchase_order') {
979 $form->{title} = $locale->text('Purchase Orders');
980 $attachment_basename = $locale->text('purchase_order_list');
982 $form->{title} = $locale->text('Request for Quotations');
983 $attachment_basename = $locale->text('rfq_list');
987 if ($form->{type} eq 'sales_order') {
988 $form->{title} = $locale->text('Sales Orders');
989 $attachment_basename = $locale->text('sales_order_list');
991 $form->{title} = $locale->text('Quotations');
992 $attachment_basename = $locale->text('quotation_list');
996 my $report = SL::ReportGenerator->new(\%myconfig, $form);
998 my @hidden_variables = map { "l_${_}" } @columns;
999 push @hidden_variables, "l_subtotal", $form->{vc}, qw(l_closed l_notdelivered open closed delivered notdelivered ordnumber quonumber
1000 transaction_description transdatefrom transdateto type vc employee_id salesman_id);
1002 my $href = build_std_url('action=orders', grep { $form->{$_} } @hidden_variables);
1005 'ids' => { 'text' => '', },
1006 'transdate' => { 'text' => $locale->text('Date'), },
1007 'reqdate' => { 'text' => $locale->text('Required by'), },
1008 'id' => { 'text' => $locale->text('ID'), },
1009 'ordnumber' => { 'text' => $locale->text('Order'), },
1010 'quonumber' => { 'text' => $form->{type} eq "request_quotation" ? $locale->text('RFQ') : $locale->text('Quotation'), },
1011 'name' => { 'text' => $form->{vc} eq 'customer' ? $locale->text('Customer') : $locale->text('Vendor'), },
1012 'netamount' => { 'text' => $locale->text('Amount'), },
1013 'tax' => { 'text' => $locale->text('Tax'), },
1014 'amount' => { 'text' => $locale->text('Total'), },
1015 'curr' => { 'text' => $locale->text('Curr'), },
1016 'employee' => { 'text' => $locale->text('Employee'), },
1017 'salesman' => { 'text' => $locale->text('Salesman'), },
1018 'shipvia' => { 'text' => $locale->text('Ship via'), },
1019 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
1020 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1021 'open' => { 'text' => $locale->text('Open'), },
1022 'delivered' => { 'text' => $locale->text('Delivered'), },
1023 'marge_total' => { 'text' => $locale->text('Ertrag'), },
1024 'marge_percent' => { 'text' => $locale->text('Ertrag prozentual'), }
1027 foreach my $name (qw(id transdate reqdate quonumber ordnumber name employee salesman shipvia)) {
1028 $column_defs{$name}->{link} = $href . "&sort=$name";
1031 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount curr);
1033 $form->{"l_type"} = "Y";
1034 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1035 $column_defs{ids}->{visible} = $allow_multiple_orders ? 'HTML' : 0;
1037 $report->set_columns(%column_defs);
1038 $report->set_column_order(@columns);
1040 $report->set_export_options('orders', @hidden_variables);
1042 $report->set_sort_indicator($form->{sort}, 1);
1045 if ($form->{customer}) {
1046 push @options, $locale->text('Customer') . " : $form->{customer}";
1048 if ($form->{vendor}) {
1049 push @options, $locale->text('Vendor') . " : $form->{vendor}";
1051 if ($form->{department}) {
1052 ($department) = split /--/, $form->{department};
1053 push @options, $locale->text('Department') . " : $department";
1055 if ($form->{ordnumber}) {
1056 push @options, $locale->text('Order Number') . " : $form->{ordnumber}";
1058 if ($form->{notes}) {
1059 push @options, $locale->text('Notes') . " : $form->{notes}";
1061 if ($form->{transaction_description}) {
1062 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}";
1064 if ($form->{transdatefrom}) {
1065 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1);
1067 if ($form->{transdateto}) {
1068 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1);
1070 if ($form->{open}) {
1071 push @options, $locale->text('Open');
1073 if ($form->{closed}) {
1074 push @options, $locale->text('Closed');
1076 if ($form->{delivered}) {
1077 push @options, $locale->text('Delivered');
1079 if ($form->{notdelivered}) {
1080 push @options, $locale->text('Not delivered');
1083 $report->set_options('top_info_text' => join("\n", @options),
1084 'raw_top_info_text' => $form->parse_html_template('oe/orders_top'),
1085 'raw_bottom_info_text' => $form->parse_html_template('oe/orders_bottom', { 'SHOW_CONTINUE_BUTTON' => $allow_multiple_orders }),
1086 'output_format' => 'HTML',
1087 'title' => $form->{title},
1088 'attachment_basename' => $attachment_basename . strftime('_%Y%m%d', localtime time),
1090 $report->set_options_from_form();
1092 # add sort and escape callback, this one we use for the add sub
1093 $form->{callback} = $href .= "&sort=$form->{sort}";
1095 # escape callback for href
1096 $callback = $form->escape($href);
1098 my @subtotal_columns = qw(netamount amount marge_total marge_percent);
1100 my %totals = map { $_ => 0 } @subtotal_columns;
1101 my %subtotals = map { $_ => 0 } @subtotal_columns;
1105 my $edit_url = build_std_url('action=edit', 'type', 'vc');
1107 foreach $oe (@{ $form->{OE} }) {
1108 map { $oe->{$_} *= $oe->{exchangerate} } @subtotal_columns;
1110 $oe->{tax} = $oe->{amount} - $oe->{netamount};
1111 $oe->{open} = $oe->{closed} ? $locale->text('No') : $locale->text('Yes');
1112 $oe->{delivered} = $oe->{delivered} ? $locale->text('Yes') : $locale->text('No');
1114 map { $subtotals{$_} += $oe->{$_};
1115 $totals{$_} += $oe->{$_} } @subtotal_columns;
1117 $subtotals{marge_percent} = $subtotals{netamount} ? ($subtotals{marge_total} * 100 / $subtotals{netamount}) : 0;
1118 $totals{marge_percent} = $totals{netamount} ? ($totals{marge_total} * 100 / $totals{netamount} ) : 0;
1120 map { $oe->{$_} = $form->format_amount(\%myconfig, $oe->{$_}, 2) } qw(netamount tax amount marge_total marge_percent);
1124 foreach my $column (@columns) {
1125 next if ($column eq 'ids');
1127 'data' => $oe->{$column},
1128 'align' => $column_alignment{$column},
1133 'raw_data' => $cgi->hidden('-name' => "trans_id_${idx}", '-value' => $oe->{id})
1134 . $cgi->checkbox('-name' => "multi_id_${idx}", '-value' => 1, '-label' => ''),
1135 'valign' => 'center',
1136 'align' => 'center',
1139 $row->{$ordnumber}->{link} = $edit_url . "&id=" . E($oe->{id}) . "&callback=${callback}";
1141 my $row_set = [ $row ];
1143 if (($form->{l_subtotal} eq 'Y')
1144 && (($idx == (scalar @{ $form->{OE} } - 1))
1145 || ($oe->{ $form->{sort} } ne $form->{OE}->[$idx + 1]->{ $form->{sort} }))) {
1146 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1149 $report->add_data($row_set);
1154 $report->add_separator();
1155 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1157 $report->generate_with_headers();
1159 $lxdebug->leave_sub();
1162 sub check_delivered_flag {
1163 $lxdebug->enter_sub();
1165 if (($form->{type} ne 'sales_order') && ($form->{type} ne 'purchase_order')) {
1166 return $lxdebug->leave_sub();
1169 my $all_delivered = 0;
1171 foreach my $i (1 .. $form->{rowcount}) {
1172 next if (!$form->{"id_$i"});
1174 if ($form->parse_amount(\%myconfig, $form->{"qty_$i"}) == $form->parse_amount(\%myconfig, $form->{"ship_$i"})) {
1183 $form->{delivered} = 1 if $all_delivered;
1185 $lxdebug->leave_sub();
1188 sub save_and_close {
1189 $lxdebug->enter_sub();
1191 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1193 if ($form->{type} =~ /_order$/) {
1194 $form->isblank("transdate", $locale->text('Order Date missing!'));
1196 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1199 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1200 $form->{$idx} =~ s/^\s*//g;
1201 $form->{$idx} =~ s/\s*$//g;
1203 $msg = ucfirst $form->{vc};
1204 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1206 # $locale->text('Customer missing!');
1207 # $locale->text('Vendor missing!');
1209 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1210 if ($form->{currency} ne $form->{defaultcurrency});
1214 if($form->{payment_id}) {
1215 $payment_id = $form->{payment_id};
1218 # if the name changed get new values
1219 if (&check_name($form->{vc})) {
1220 if($form->{payment_id} eq "") {
1221 $form->{payment_id} = $payment_id;
1227 $form->{id} = 0 if $form->{saveasnew};
1229 # this is for the internal notes section for the [email] Subject
1230 if ($form->{type} =~ /_order$/) {
1231 if ($form->{type} eq 'sales_order') {
1232 $form->{label} = $locale->text('Sales Order');
1234 $numberfld = "sonumber";
1235 $ordnumber = "ordnumber";
1237 $form->{label} = $locale->text('Purchase Order');
1239 $numberfld = "ponumber";
1240 $ordnumber = "ordnumber";
1243 $err = $locale->text('Cannot save order!');
1245 check_delivered_flag();
1248 if ($form->{type} eq 'sales_quotation') {
1249 $form->{label} = $locale->text('Quotation');
1251 $numberfld = "sqnumber";
1252 $ordnumber = "quonumber";
1254 $form->{label} = $locale->text('Request for Quotation');
1256 $numberfld = "rfqnumber";
1257 $ordnumber = "quonumber";
1260 $err = $locale->text('Cannot save quotation!');
1264 # get new number in sequence if no number is given or if saveasnew was requested
1265 if (!$form->{$ordnumber} || $form->{saveasnew}) {
1266 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld);
1271 $form->error($err) if (!OE->save(\%myconfig, \%$form));
1273 # saving the history
1274 if(!exists $form->{addition}) {
1275 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1276 $form->{addition} = "SAVED";
1277 $form->save_history($form->dbconnect(\%myconfig));
1279 # /saving the history
1281 $form->redirect($form->{label} . " $form->{$ordnumber} " .
1282 $locale->text('saved!'));
1284 $lxdebug->leave_sub();
1288 $lxdebug->enter_sub();
1290 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1293 if ($form->{type} =~ /_order$/) {
1294 $form->isblank("transdate", $locale->text('Order Date missing!'));
1296 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1299 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1300 $form->{$idx} =~ s/^\s*//g;
1301 $form->{$idx} =~ s/\s*$//g;
1303 $msg = ucfirst $form->{vc};
1304 $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
1306 # $locale->text('Customer missing!');
1307 # $locale->text('Vendor missing!');
1309 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1310 if ($form->{currency} ne $form->{defaultcurrency});
1314 if($form->{payment_id}) {
1315 $payment_id = $form->{payment_id};
1318 # if the name changed get new values
1319 if (&check_name($form->{vc})) {
1320 if($form->{payment_id} eq "") {
1321 $form->{payment_id} = $payment_id;
1327 $form->{id} = 0 if $form->{saveasnew};
1329 # this is for the internal notes section for the [email] Subject
1330 if ($form->{type} =~ /_order$/) {
1331 if ($form->{type} eq 'sales_order') {
1332 $form->{label} = $locale->text('Sales Order');
1334 $numberfld = "sonumber";
1335 $ordnumber = "ordnumber";
1337 $form->{label} = $locale->text('Purchase Order');
1339 $numberfld = "ponumber";
1340 $ordnumber = "ordnumber";
1343 $err = $locale->text('Cannot save order!');
1345 check_delivered_flag();
1348 if ($form->{type} eq 'sales_quotation') {
1349 $form->{label} = $locale->text('Quotation');
1351 $numberfld = "sqnumber";
1352 $ordnumber = "quonumber";
1354 $form->{label} = $locale->text('Request for Quotation');
1356 $numberfld = "rfqnumber";
1357 $ordnumber = "quonumber";
1360 $err = $locale->text('Cannot save quotation!');
1364 $form->{$ordnumber} = $form->update_defaults(\%myconfig, $numberfld)
1365 unless $form->{$ordnumber};
1369 OE->save(\%myconfig, \%$form);
1371 # saving the history
1372 if(!exists $form->{addition}) {
1373 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1374 $form->{addition} = "SAVED";
1375 $form->save_history($form->dbconnect(\%myconfig));
1377 # /saving the history
1379 $form->{simple_save} = 1;
1380 if(!$form->{print_and_save}) {
1381 set_headings("edit");
1385 $lxdebug->leave_sub();
1389 $lxdebug->enter_sub();
1393 if ($form->{type} =~ /_order$/) {
1394 $msg = $locale->text('Are you sure you want to delete Order Number');
1395 $ordnumber = 'ordnumber';
1397 $msg = $locale->text('Are you sure you want to delete Quotation Number');
1398 $ordnumber = 'quonumber';
1404 <form method=post action=$form->{script}>
1407 # delete action variable
1408 map { delete $form->{$_} } qw(action header);
1410 foreach $key (keys %$form) {
1411 $form->{$key} =~ s/\"/"/g;
1412 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1416 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1418 <h4>$msg $form->{$ordnumber}</h4>
1420 <input type="hidden" name="yes_nextsub" value="delete_order_quotation">
1421 <input name=action class=submit type=submit value="|
1422 . $locale->text('Yes') . qq|">
1423 <button class=submit type=button onclick="history.back()">|
1424 . $locale->text('No') . qq|</button>
1431 $lxdebug->leave_sub();
1434 sub delete_order_quotation {
1435 $lxdebug->enter_sub();
1437 if ($form->{type} =~ /_order$/) {
1438 $msg = $locale->text('Order deleted!');
1439 $err = $locale->text('Cannot delete order!');
1441 $msg = $locale->text('Quotation deleted!');
1442 $err = $locale->text('Cannot delete quotation!');
1444 if (OE->delete(\%myconfig, \%$form, $spool)){
1445 # saving the history
1446 if(!exists $form->{addition}) {
1447 $form->{snumbers} = qq|ordnumber_| . $form->{ordnumber};
1448 $form->{addition} = "DELETED";
1449 $form->save_history($form->dbconnect(\%myconfig));
1451 # /saving the history
1457 $lxdebug->leave_sub();
1461 $lxdebug->enter_sub();
1463 $form->{old_employee_id} = $form->{employee_id};
1464 $form->{old_salesman_id} = $form->{salesman_id};
1466 if ($form->{type} =~ /_order$/) {
1468 # these checks only apply if the items don't bring their own ordnumbers/transdates.
1469 # The if clause ensures that by searching for empty ordnumber_#/transdate_# fields.
1470 $form->isblank("ordnumber", $locale->text('Order Number missing!'))
1471 if (+{ map { $form->{"ordnumber_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1472 $form->isblank("transdate", $locale->text('Order Date missing!'))
1473 if (+{ map { $form->{"transdate_$_"}, 1 } (1 .. $form->{rowcount} - 1) }->{''});
1475 # also copy deliverydate from the order
1476 $form->{deliverydate} = $form->{reqdate} if $form->{reqdate};
1477 $form->{orddate} = $form->{transdate};
1479 $form->isblank("quonumber", $locale->text('Quotation Number missing!'));
1480 $form->isblank("transdate", $locale->text('Quotation Date missing!'));
1481 $form->{ordnumber} = "";
1482 $form->{quodate} = $form->{transdate};
1485 $payment_id = $form->{payment_id} if $form->{payment_id};
1487 # if the name changed get new values
1488 if (&check_name($form->{vc})) {
1489 $form->{payment_id} = $payment_id if $form->{payment_id} eq "";
1494 $form->{cp_id} *= 1;
1496 for $i (1 .. $form->{rowcount}) {
1497 for (qw(ship qty sellprice listprice basefactor)) {
1498 $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig, $form->{"${_}_${i}"}) if $form->{"${_}_${i}"};
1502 if ( $form->{type} =~ /_order/
1503 && $form->{currency} ne $form->{defaultcurrency}) {
1505 # check if we need a new exchangerate
1506 $buysell = ($form->{type} eq 'sales_order') ? "buy" : "sell";
1508 $orddate = $form->current_date(\%myconfig);
1509 $exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $orddate, $buysell);
1511 if (!$exchangerate) {
1512 &backorder_exchangerate($orddate, $buysell);
1517 # close orders/quotations
1518 $form->{closed} = 1;
1520 # save order if one ordnumber has been given
1521 # if not it's most likely a collective order, which can't be saved back
1522 # so they just have to be closed
1523 if (($form->{ordnumber} ne '') || ($form->{quonumber} ne '')) {
1524 OE->close_order(\%myconfig, \%$form) if ($form->{id});
1526 OE->close_orders(\%myconfig, \%$form);
1529 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1530 $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1533 $form->{closed} = 0;
1534 $form->{rowcount}--;
1535 $form->{shipto} = 1;
1537 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1539 if ($form->{type} =~ /_order$/) {
1540 $form->{exchangerate} = $exchangerate;
1544 if ( $form->{type} eq 'purchase_order'
1545 || $form->{type} eq 'request_quotation') {
1546 $form->{title} = $locale->text('Add Vendor Invoice');
1547 $form->{script} = 'ir.pl';
1552 if ( $form->{type} eq 'sales_order'
1553 || $form->{type} eq 'sales_quotation') {
1554 $form->{title} = $locale->text('Add Sales Invoice');
1555 $form->{script} = 'is.pl';
1560 # bo creates the id, reset it
1561 map { delete $form->{$_} } qw(id subject message cc bcc printed emailed queued);
1562 $form->{ $form->{vc} } =~ s/--.*//g;
1563 $form->{type} = "invoice";
1566 $locale = new Locale "$myconfig{countrycode}", "$script";
1568 require "bin/mozilla/$form->{script}";
1570 map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1572 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1573 qw(creditlimit creditremaining);
1575 $currency = $form->{currency};
1578 $form->{currency} = $currency;
1579 $form->{exchangerate} = "";
1580 $form->{forex} = "";
1581 $form->{exchangerate} = $exchangerate
1585 $form->check_exchangerate(
1586 \%myconfig, $form->{currency}, $form->{invdate}, $buysell
1589 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1594 for $i (1 .. $form->{rowcount}) {
1595 $form->{"discount_$i"} =
1596 $form->format_amount(\%myconfig, $form->{"discount_$i"});
1598 ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1600 $decimalplaces = ($dec > 2) ? $dec : 2;
1602 # copy delivery date from reqdate for order -> invoice conversion
1603 $form->{"deliverydate_$i"} = $form->{"reqdate_$i"}
1604 unless $form->{"deliverydate_$i"};
1606 $form->{"sellprice_$i"} =
1607 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
1610 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
1611 $dec_qty = length $dec_qty;
1613 $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1615 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1616 qw(partnumber description unit);
1622 $lxdebug->leave_sub();
1625 sub backorder_exchangerate {
1626 $lxdebug->enter_sub();
1627 my ($orddate, $buysell) = @_;
1634 <form method=post action=$form->{script}>
1637 # delete action variable
1638 map { delete $form->{$_} } qw(action header exchangerate);
1640 foreach $key (keys %$form) {
1641 $form->{$key} =~ s/\"/"/g;
1642 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1645 $form->{title} = $locale->text('Add Exchangerate');
1649 <input type=hidden name=login value=$form->{login}>
1650 <input type=hidden name=password value=$form->{password}>
1652 <input type=hidden name=exchangeratedate value=$orddate>
1653 <input type=hidden name=buysell value=$buysell>
1656 <tr><th class=listtop>$form->{title}</th></tr>
1657 <tr height="5"></tr>
1662 <th align=right>| . $locale->text('Currency') . qq|</th>
1663 <td>$form->{currency}</td>
1666 <th align=right>| . $locale->text('Date') . qq|</th>
1670 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
1671 <td><input name=exchangerate size=11></td>
1681 <input type=hidden name=nextsub value=save_exchangerate>
1683 <input name=action class=submit type=submit value="|
1684 . $locale->text('Continue') . qq|">
1692 $lxdebug->leave_sub();
1695 sub save_exchangerate {
1696 $lxdebug->enter_sub();
1698 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'));
1699 $form->{exchangerate} =
1700 $form->parse_amount(\%myconfig, $form->{exchangerate});
1701 $form->save_exchangerate(\%myconfig, $form->{currency},
1702 $form->{exchangeratedate},
1703 $form->{exchangerate}, $form->{buysell});
1707 $lxdebug->leave_sub();
1710 sub create_backorder {
1711 $lxdebug->enter_sub();
1713 $form->{shipped} = 1;
1715 # figure out if we need to create a backorder
1716 # items aren't saved if qty != 0
1718 for $i (1 .. $form->{rowcount}) {
1719 $totalqty += $qty = $form->{"qty_$i"};
1720 $totalship += $ship = $form->{"ship_$i"};
1722 $form->{"qty_$i"} = $qty - $ship;
1725 if ($totalship == 0) {
1726 map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1727 $form->{ordtotal} = 0;
1728 $form->{shipped} = 0;
1732 if ($totalqty == $totalship) {
1733 map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1734 $form->{ordtotal} = 0;
1739 qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup)
1742 for $i (1 .. $form->{rowcount}) {
1744 $form->{"${_}_$i"} =
1745 $form->format_amount(\%myconfig, $form->{"${_}_$i"})
1746 } qw(sellprice discount);
1751 OE->save(\%myconfig, \%$form);
1753 # rebuild rows for invoice
1757 for $i (1 .. $form->{rowcount}) {
1758 $form->{"qty_$i"} = $form->{"ship_$i"};
1760 if ($form->{"qty_$i"}) {
1763 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1768 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1769 $form->{rowcount} = $count;
1771 $lxdebug->leave_sub();
1775 $lxdebug->enter_sub();
1777 $form->{saveasnew} = 1;
1778 $form->{closed} = 0;
1779 map { delete $form->{$_} } qw(printed emailed queued);
1781 # Let Lx-Office assign a new order number if the user hasn't changed the
1782 # previous one. If it has been changed manually then use it as-is.
1783 my $idx = $form->{type} =~ /_quotation$/ ? "quonumber" : "ordnumber";
1784 $form->{$idx} =~ s/^\s*//g;
1785 $form->{$idx} =~ s/\s*$//g;
1786 if ($form->{saved_xyznumber} &&
1787 ($form->{saved_xyznumber} eq $form->{$idx})) {
1788 delete($form->{$idx});
1793 $lxdebug->leave_sub();
1796 sub check_for_direct_delivery_yes {
1797 $lxdebug->enter_sub();
1799 $form->{direct_delivery_checked} = 1;
1800 delete @{$form}{grep /^shipto/, keys %{ $form }};
1801 map { s/^CFDD_//; $form->{$_} = $form->{"CFDD_${_}"} } grep /^CFDD_/, keys %{ $form };
1802 $form->{shipto} = 1;
1804 $lxdebug->leave_sub();
1807 sub check_for_direct_delivery_no {
1808 $lxdebug->enter_sub();
1810 $form->{direct_delivery_checked} = 1;
1811 delete @{$form}{grep /^shipto/, keys %{ $form }};
1814 $lxdebug->leave_sub();
1817 sub check_for_direct_delivery {
1818 $lxdebug->enter_sub();
1820 if ($form->{direct_delivery_checked}
1821 || (!$form->{shiptoname} && !$form->{shiptostreet} && !$form->{shipto_id})) {
1822 $lxdebug->leave_sub();
1826 if ($form->{shipto_id}) {
1827 Common->get_shipto_by_id(\%myconfig, $form, $form->{shipto_id}, "CFDD_");
1830 map { $form->{"CFDD_${_}"} = $form->{$_ } } grep /^shipto/, keys %{ $form };
1833 delete $form->{action};
1834 $form->{VARIABLES} = [ map { { "key" => $_, "value" => $form->{$_} } } grep { ref $_ eq "" } keys %{ $form } ];
1837 print $form->parse_html_template("oe/check_for_direct_delivery");
1839 $lxdebug->leave_sub();
1844 sub purchase_order {
1845 $lxdebug->enter_sub();
1847 if ($form->{type} eq 'sales_order') {
1848 check_for_direct_delivery();
1851 if ( $form->{type} eq 'sales_quotation'
1852 || $form->{type} eq 'request_quotation') {
1853 OE->close_order(\%myconfig, \%$form);
1856 if ($form->{type} =~ /^sales_/) {
1857 delete($form->{ordnumber});
1860 $form->{cp_id} *= 1;
1862 $form->{title} = $locale->text('Add Purchase Order');
1863 $form->{vc} = "vendor";
1864 $form->{type} = "purchase_order";
1868 $lxdebug->leave_sub();
1872 $lxdebug->enter_sub();
1874 if ( $form->{type} eq 'sales_quotation'
1875 || $form->{type} eq 'request_quotation') {
1876 OE->close_order(\%myconfig, $form);
1879 if ($form->{type} eq "purchase_order") {
1880 delete($form->{ordnumber});
1883 $form->{cp_id} *= 1;
1885 $form->{title} = $locale->text('Add Sales Order');
1886 $form->{vc} = "customer";
1887 $form->{type} = "sales_order";
1891 $lxdebug->leave_sub();
1895 $lxdebug->enter_sub();
1897 $form->{transdate} = $form->current_date(\%myconfig);
1898 delete $form->{duedate};
1900 $form->{closed} = 0;
1902 $form->{old_employee_id} = $form->{employee_id};
1903 $form->{old_salesman_id} = $form->{salesman_id};
1906 map { delete $form->{$_} }
1907 qw(id subject message cc bcc printed emailed queued customer vendor creditlimit creditremaining discount tradediscount oldinvtotal);
1909 for $i (1 .. $form->{rowcount}) {
1910 map({ $form->{"${_}_${i}"} = $form->parse_amount(\%myconfig,
1911 $form->{"${_}_${i}"})
1912 if ($form->{"${_}_${i}"}) }
1913 qw(ship qty sellprice listprice basefactor));
1921 for $i (1 .. $form->{rowcount} - 1) {
1922 map { $form->{"${_}_$i"} =~ s/\"/"/g }
1923 qw(partnumber description unit);
1926 map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, 0, "0") }
1927 qw(creditlimit creditremaining);
1931 $lxdebug->leave_sub();
1935 $lxdebug->enter_sub();
1937 $form->{print_and_save} = 1;
1941 my $saved_form = save_form();
1946 map({ $saved_vars{$_} = $form->{$_}; } qw(id ordnumber quonumber));
1947 restore_form($saved_form);
1948 map({ $form->{$_} = $saved_vars{$_}; } qw(id ordnumber quonumber));
1952 $lxdebug->leave_sub();
1956 call_sub($form->{yes_nextsub});
1960 call_sub($form->{no_nextsub});
1963 ######################################################################################################
1965 # ###############################################################################################
1967 $lxdebug->enter_sub();
1969 $form->{"taxaccounts"} =~ s/\s*$//;
1970 $form->{"taxaccounts"} =~ s/^\s*//;
1971 foreach my $accno (split(/\s*/, $form->{"taxaccounts"})) {
1972 map({ delete($form->{"${accno}_${_}"}); } qw(rate description taxnumber));
1974 $form->{"taxaccounts"} = "";
1976 for my $i (1 .. $form->{"rowcount"}) {
1977 IC->retrieve_accounts(\%myconfig, $form, $form->{"id_$i"}, $i, 1) if $form->{"id_$i"};
1980 $form->{rowcount}++;
1981 $form->{"project_id_$form->{rowcount}"} = $form->{globalproject_id};
1983 $form->language_payment(\%myconfig);
1985 Common::webdav_folder($form) if ($webdav);
1990 display_row($form->{rowcount}) if $form->{rowcount};
1994 $lxdebug->leave_sub();
1998 $lxdebug->enter_sub();
1999 my $numrows = shift;
2002 my @header_sort = qw(runningnumber partnumber description ship qty unit sellprice_pg sellprice discount linetotal);
2004 { id => 'runningnumber', width => 5, value => $locale->text('No.'), display => 1, },
2005 { id => 'partnumber', width => 12, value => $locale->text('Number'), display => 1, },
2006 { id => 'description', width => 30, value => $locale->text('Part Description'), display => 1, },
2007 { id => 'ship', width => 5, value => ($form->{type} eq 'purchase_order' ? $locale->text('Ship rcvd') : $locale->text('Ship')),
2008 display => $form->{type} =~ /sales_order/ || ($form->{type} =~ /purchase_order/ && !($lizenzen && $form->{vc} eq "customer")) , },
2009 { id => 'qty', width => 5, value => $locale->text('Qty'), display => 1, },
2010 { id => 'unit', width => 5, value => $locale->text('Unit'), display => 1, },
2011 { id => 'license', width => 10, value => $locale->text('License'), display => 0, },
2012 { id => 'serialnr', width => 10, value => $locale->text('Serial No.'), display => 0, },
2013 { id => 'projectnr', width => 10, value => $locale->text('Project'), display => 0, },
2014 { id => 'sellprice', width => 15, value => $locale->text('Price'), display => 1, },
2015 { id => 'sellprice_pg', width => 15, value => $locale->text('Pricegroup'), display => $form->{type} =~ /^sales_/, },
2016 { id => 'discount', width => undef, value => $locale->text('Discount'), display => $form->{vc} eq 'customer', },
2017 { id => 'linetotal', width => 10, value => $locale->text('Extended'), display => 1, },
2018 { id => 'bin', width => 10, value => $locale->text('Bin'), display => 0, },
2020 my @column_index = map { $_->{id} } grep { $_->{display} } @HEADER;
2023 my $dimension_units = AM->retrieve_units(\%myconfig, $form, "dimension");
2024 my $service_units = AM->retrieve_units(\%myconfig, $form, "service");
2025 my $all_units = AM->retrieve_units(\%myconfig, $form);
2027 my $colspan = scalar @column_index;
2029 $form->{invsubtotal} = 0;
2030 map { $form->{"${_}_base"} = 0 } (split(/ /, $form->{taxaccounts}));
2033 $myconfig{show_form_details} = 1 unless (defined($myconfig{show_form_details}));
2034 $form->{show_details} = $myconfig{show_form_details} unless (defined($form->{show_details}));
2037 # translations, unused commented out
2038 # $runningnumber = $locale->text('No.');
2039 $deliverydate = $locale->text('Delivery Date');
2040 $serialnumber = $locale->text('Serial No.');
2041 $projectnumber = $locale->text('Project');
2042 # $partsgroup = $locale->text('Group');
2043 $reqdate = $locale->text('Reqdate');
2044 $deliverydate = $locale->text('Required by');
2047 my %align = map { $_ => 'right' } qw(qty ship right sellprice_pg discount linetotal);
2049 $form->{marge_total} = 0;
2050 $form->{sellprice_total} = 0;
2051 $form->{lastcost_total} = 0;
2052 my %projectnumber_labels = ();
2053 my @projectnumber_values = ("");
2055 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
2056 push(@projectnumber_values, $item->{"id"});
2057 $projectnumber_labels{$item->{"id"}} = $item->{"projectnumber"};
2061 for $i (1 .. $numrows) {
2064 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(qty ship discount sellprice price_new price_old) unless ($form->{simple_save});
2067 $form->{"unit_old_$i"} ||= $form->{"unit_$i"};
2068 $form->{"selected_unit_$i"} ||= $form->{"unit_$i"};
2070 my $local_units = $form->{"inventory_accno_$i"} || $form->{"assembly_$i"} ? $dimension_units
2071 : $form->{"id_$i"} ? $service_units
2073 if ( !$local_units->{$form->{"selected_unit_$i"}} # Die ausgewaehlte Einheit ist fuer diesen Artikel nicht gueltig
2074 || !AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units)) { # (z.B. Dimensionseinheit war ausgewaehlt, es handelt sich aber
2075 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"} = $form->{"unit_$i"}; # um eine Dienstleistung). Dann keinerlei Umrechnung vornehmen.
2077 # adjust prices by unit, ignore if pricegroup changed
2078 if ((!$form->{"prices_$i"}) || ($form->{"new_pricegroup_$i"} == $form->{"old_pricegroup_$i"})) {
2079 $form->{"sellprice_$i"} *= AM->convert_unit($form->{"selected_unit_$i"}, $form->{"unit_old_$i"}, $all_units) || 1;
2080 $form->{"unit_old_$i"} = $form->{"selected_unit_$i"};
2082 my $this_unit = $form->{"unit_$i"};
2083 $this_unit = $form->{"selected_unit_$i"} if AM->convert_unit($this_unit, $form->{"selected_unit_$i"}, $all_units);
2084 $this_unit ||= "kg";
2086 $column_data{"unit"} = AM->unit_select_html($local_units, "unit_$i", $this_unit, $form->{"id_$i"} ? $form->{"unit_$i"} : undef);
2089 $form->{"sellprice_$i"} =~ /\.(\d+)/;
2090 $decimalplaces = max 2, length $1;
2092 $discount = $form->round_amount($form->{"sellprice_$i"} * $form->{"discount_$i"} / 100, $decimalplaces);
2093 $linetotal = $form->round_amount($form->{"sellprice_$i"} - $discount, $decimalplaces);
2094 $linetotal = $form->round_amount($linetotal * $form->{"qty_$i"}, 2);
2096 # convert " to "
2097 map { $form->{"${_}_$i"} =~ s/\"/"/g } qw(partnumber description unit unit_old);
2099 $column_data{runningnumber} = $cgi->textfield(-name => "runningnumber_$i", -size => 5, -value => $i); # HuT
2100 $column_data{partnumber} = $cgi->textfield(-name => "partnumber_$i", -size => 12, -value => $form->{"partnumber_$i"});
2101 $column_data{description} = ((($rows = $form->numtextrows($form->{"description_$i"}, 30, 6)) > 1) # if description is too large, use a textbox instead
2102 ? $cgi->textarea( -name => "description_$i", -default => H($form->{"description_$i"}), -rows => $rows, -columns => 30)
2103 : $cgi->textfield(-name => "description_$i", -size => 30, -value => $form->quote($form->{"description_$i"})))
2104 . $cgi->button(-value => $locale->text('L'), -onClick => "set_longdescription_window('longdescription_$i')");
2106 $form->{"qty_$i"} =~ /\.(\d+)/;
2107 my $qty_dec = length $1;
2109 $column_data{qty} = $cgi->textfield(-name => "qty_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"qty_$i"}, $qty_dec));
2110 $column_data{qty} .= $cgi->button(-onclick => "calculate_qty_selection_window('qty_$i','alu_$i', 'formel_$i', $i)", -value => $locale->text('*/'))
2111 . $cgi->hidden(-name => "formel_$i", -value => $form->{"formel_$i"}) . $cgi->hidden("-name" => "alu_$i", "-value" => $form->{"alu_$i"})
2112 if $form->{"formel_$i"};
2113 $column_data{ship} = $cgi->textfield(-name => "ship_$i", -size => 5, -value => $form->format_amount(\%myconfig, $form->{"ship_$i"}));
2115 # build in drop down list for pricesgroups
2116 if ($form->{"prices_$i"}) {
2117 $column_data{sellprice_pg} = qq|<select name="sellprice_pg_$i">$form->{"prices_$i"}</select>|;
2118 $column_data{sellprice} = $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => 'check_right_number_format(this)', -value =>
2119 (($form->{"new_pricegroup_$i"} != $form->{"old_pricegroup_$i"})
2120 ? $form->format_amount(\%myconfig, $form->{"price_new_$i"}, $decimalplaces)
2121 : $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces)));
2123 # for last row and report
2124 # set pricegroup drop down list from report menu
2125 if ($form->{"sellprice_$i"} != 0) {
2126 $form->{"pricegroup_old_$i"} = $form->{"pricegroup_id_$i"};
2127 my $default_option = $form->{"sellprice_$i"}.'--'.$form->{"pricegroup_id_$i"};
2128 $column_data{sellprice_pg} = NTI($cgi->popup_menu("sellpricepg_$i", [ $default_option ], $default_option, { $default_option => $form->{"pricegroup_$i"} || '' }));
2130 $column_data{sellprice_pg} = qq| |;
2132 $column_data{sellprice} = $cgi->textfield(-name => "sellprice_$i", -size => 10, -onBlur => "check_right_number_format(this)", -value =>
2133 $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces));
2135 $column_data{discount} = $cgi->textfield(-name => "discount_$i", -size => 3, -value => $form->format_amount(\%myconfig, $form->{"discount_$i"}));
2136 $column_data{linetotal} = $form->format_amount(\%myconfig, $linetotal, 2);
2137 $column_data{bin} = $form->{"bin_$i"};
2139 my @ROW1 = map { value => $column_data{$_}, align => $align{$_} }, @column_index;
2143 push @ROW2, { value => qq|<b>$serialnumber</b> <input name="serialnumber_$i" size="15" value="$form->{"serialnumber_$i"}">| }
2144 if $form->{type} !~ /_quotation/;
2145 push @ROW2, { value => qq|<b>$projectnumber</b> | . NTI($cgi->popup_menu('-name' => "project_id_$i", '-values' => \@projectnumber_values,
2146 '-labels' => \%projectnumber_labels, '-default' => $form->{"project_id_$i"})) };
2147 push @ROW2, { value => qq|<b>$reqdate</b> <input name="reqdate_$i" size="11" onBlur="check_right_date_format(this)" value="$form->{"reqdate_$i"}">| }
2148 if $form->{type} =~ /order/;
2149 push @ROW2, { value => sprintf qq|<b>%s</b> <input type="checkbox" name="subtotal_$i" value="1" %s>|,
2150 $locale->text('Subtotal'), $form->{"subtotal_$i"} ? 'checked' : '' };
2152 # begin marge calculations
2154 my $real_sellprice = $form->{"sellprice_$i"} - $discount;
2156 $form->{"lastcost_$i"} *= 1;
2157 $form->{"marge_percent_$i"} = 0;
2159 if ($real_sellprice && ($form->{"qty_$i"} * 1)) {
2160 $form->{"marge_percent_$i"} = ($real_sellprice - $form->{"lastcost_$i"}) * 100 / $real_sellprice;
2161 $myconfig{marge_percent_warn} ||= 15;
2162 $marge_color = 'color="#ff0000"' if $form->{"id_$i"} && ($form->{"marge_percent_$i"} < (1 * $myconfig{marge_percent_warn}));
2165 my $marge_adjust_credit_note = $form->{type} eq 'credit_note' ? -1 : 1;
2166 $form->{"marge_absolut_$i"} = ($real_sellprice - $form->{"lastcost_$i"}) * $form->{"qty_$i"} * $marge_adjust_credit_note;
2167 $form->{"marge_total"} += $form->{"marge_absolut_$i"};
2168 $form->{"lastcost_total"} += $form->{"lastcost_$i"} * $form->{"qty_$i"};
2169 $form->{"sellprice_total"} += $real_sellprice * $form->{"qty_$i"};
2171 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) } qw(marge_absolut marge_percent);
2173 push @ROW2, { value => sprintf qq|<font %s><b>%s</b> %s %s%% </font> <b>%s</b> %s <b>%s</b> %s|,
2174 $marge_color, $locale->text('Ertrag'),$form->{"marge_absolut_$i"}, $form->{"marge_percent_$i"},
2175 $locale->text('LP'), $form->format_amount(\%myconfig, $form->{"listprice_$i"}, 2),
2176 $locale->text('EK'), $form->format_amount(\%myconfig, $form->{"lastcost_$i"}, 2) }
2177 if $form->{"id_$i"} && $form->{type} =~ /^sales_/;
2178 # / marge calculations ending
2180 my @HIDDENS = map { value => $_}, (
2181 $cgi->hidden("-name" => "unit_old_$i", "-value" => $form->{"selected_unit_$i"}),
2182 $cgi->hidden("-name" => "price_new_$i", "-value" => $form->format_amount(\%myconfig, $form->{"price_new_$i"})),
2183 map { ($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } map { $_."_$i" }
2184 qw(orderitems_id bo pricegroup_old price_old id inventory_accno bin partsgroup partnotes
2185 income_accno expense_accno listprice assembly taxaccounts ordnumber transdate cusordnumber
2186 longdescription basefactor marge_absolut marge_percent lastcost)
2189 map { $form->{"${_}_base"} += $linetotal } (split(/ /, $form->{"taxaccounts_$i"}));
2191 $form->{invsubtotal} += $linetotal;
2193 push @ROWS, { ROW1 => \@ROW1, ROW2 => \@ROW2, HIDDENS => \@HIDDENS, colspan => $colspan, };
2196 print $form->parse_html_template('oe/sales_order', { ROWS => \@ROWS,
2198 show_details_checked => ($form->{show_details} ? "checked" : ""),
2199 show_details_new => !$form->{show_details},
2202 if (0 != ($form->{sellprice_total} * 1)) {
2203 $form->{marge_percent} = ($form->{sellprice_total} - $form->{lastcost_total}) / $form->{sellprice_total} * 100;
2206 $lxdebug->leave_sub();