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-2002
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 #======================================================================
30 # Inventory invoicing module
32 #======================================================================
38 use List::Util qw(max sum);
40 require "bin/mozilla/io.pl";
41 require "bin/mozilla/invoice_io.pl";
42 require "bin/mozilla/arap.pl";
43 require "bin/mozilla/drafts.pl";
56 $main::lxdebug->enter_sub();
58 my $form = $main::form;
59 my $locale = $main::locale;
61 $main::auth->assert('invoice_edit');
63 return $main::lxdebug->leave_sub() if (load_draft_maybe());
65 if ($form->{type} eq "credit_note") {
66 $form->{title} = $locale->text('Add Credit Note');
68 if ($form->{storno}) {
69 $form->{title} = $locale->text('Add Storno Credit Note');
72 $form->{title} = $locale->text('Add Sales Invoice');
77 $form->{callback} = "$form->{script}?action=add&type=$form->{type}" unless $form->{callback};
79 $form->{jsscript} = "date";
85 $main::lxdebug->leave_sub();
89 $main::lxdebug->enter_sub();
91 my $form = $main::form;
92 my $locale = $main::locale;
94 $main::auth->assert('invoice_edit');
97 $form->{javascript} = qq|<script type="text/javascript" src="js/show_history.js"></script>|;
98 #/show hhistory button
100 if ($form->{type} eq "credit_note") {
101 $form->{title} = $locale->text('Edit Credit Note');
102 $form->{title} = $locale->text('Edit Storno Credit Note') if $form->{storno};
104 $form->{title} = $locale->text('Edit Sales Invoice');
105 $form->{title} = $locale->text('Edit Storno Invoice') if $form->{storno};
108 my ($language_id, $printer_id);
109 if ($form->{print_and_post}) {
110 $form->{action} = "print";
111 $form->{resubmit} = 1;
112 $language_id = $form->{language_id};
113 $printer_id = $form->{printer_id};
117 if ($form->{print_and_post}) {
118 $form->{language_id} = $language_id;
119 $form->{printer_id} = $printer_id;
124 $main::lxdebug->leave_sub();
128 $main::lxdebug->enter_sub();
130 my $form = $main::form;
131 my %myconfig = %main::myconfig;
133 $main::auth->assert('invoice_edit');
135 $form->{vc} = 'customer';
138 $form->{webdav} = $main::webdav;
139 $form->{lizenzen} = $main::lizenzen;
141 $form->create_links("AR", \%myconfig, "customer");
143 if ($form->{all_customer}) {
144 unless ($form->{customer_id}) {
145 $form->{customer_id} = $form->{all_customer}->[0]->{id};
146 $form->{salesman_id} = $form->{all_customer}->[0]->{salesman_id};
151 if ($form->{payment_id}) {
152 $payment_id = $form->{payment_id};
155 if ($form->{language_id}) {
156 $language_id = $form->{language_id};
159 if ($form->{taxzone_id}) {
160 $taxzone_id = $form->{taxzone_id};
167 if ($form->{shipto_id}) {
168 $shipto_id = $form->{shipto_id};
171 my $cp_id = $form->{cp_id};
172 IS->get_customer(\%myconfig, \%$form);
174 #quote all_customer Bug 133
175 foreach my $ref (@{ $form->{all_customer} }) {
176 $ref->{name} = $form->quote($ref->{name});
181 IS->retrieve_invoice(\%myconfig, \%$form);
182 $form->{cp_id} = $cp_id;
185 $form->{payment_id} = $payment_id;
188 $form->{language_id} = $language_id;
191 $form->{taxzone_id} = $taxzone_id;
194 $form->{shipto_id} = $shipto_id;
197 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
200 if ($form->{all_departments}) {
201 $form->{selectdepartment} = "<option>\n";
202 $form->{department} = "$form->{department}--$form->{department_id}";
205 $form->{selectdepartment} .=
206 "<option>$_->{description}--$_->{id}</option>\n"
207 } (@{ $form->{all_departments} });
210 $form->{employee} = "$form->{employee}--$form->{employee_id}";
213 $form->{forex} = $form->{exchangerate};
214 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
216 foreach my $key (keys %{ $form->{AR_links} }) {
217 foreach my $ref (@{ $form->{AR_links}{$key} }) {
218 $form->{"select$key"} .=
219 "<option>$ref->{accno}--$ref->{description}</option>\n";
222 if ($key eq "AR_paid") {
223 next unless $form->{acc_trans}{$key};
224 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
225 $form->{"AR_paid_$i"} =
226 "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
229 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
230 $form->{"datepaid_$i"} =
231 $form->{acc_trans}{$key}->[$i - 1]->{transdate};
232 $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
233 $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
234 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
235 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
237 $form->{paidaccounts} = $i;
241 "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
246 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
248 $form->{AR} = $form->{AR_1} unless $form->{id};
251 ($form->datetonum($form->{invdate}, \%myconfig) <=
252 $form->datetonum($form->{closedto}, \%myconfig));
254 $main::lxdebug->leave_sub();
257 sub prepare_invoice {
258 $main::lxdebug->enter_sub();
260 my $form = $main::form;
261 my %myconfig = %main::myconfig;
263 $main::auth->assert('invoice_edit');
265 if ($form->{type} eq "credit_note") {
266 $form->{type} = "credit_note";
267 $form->{formname} = "credit_note";
269 $form->{type} = "invoice";
270 $form->{formname} = "invoice";
277 foreach my $ref (@{ $form->{invoice_details} }) {
280 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
282 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
283 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
285 my $decimalplaces = ($dec > 2) ? $dec : 2;
287 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
288 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
289 $dec_qty = length $dec_qty;
291 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
293 $form->{rowcount} = $i;
297 # get pricegroups for parts
298 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
299 set_pricegroup($_) for 1 .. $form->{rowcount};
301 $main::lxdebug->leave_sub();
305 $main::lxdebug->enter_sub();
307 my $form = $main::form;
308 my %myconfig = %main::myconfig;
309 my $locale = $main::locale;
310 my $cgi = $main::cgi;
312 $main::auth->assert('invoice_edit');
317 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
318 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
320 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
321 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
323 $payment = qq|<option value=""></option>|;
324 foreach my $item (@{ $form->{payment_terms} }) {
325 if ($form->{payment_id} eq $item->{id}) {
326 $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
328 $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
332 my $set_duedate_url = "$form->{script}?action=set_duedate";
334 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
336 my @old_project_ids = ($form->{"globalproject_id"});
337 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
339 $form->get_lists("contacts" => "ALL_CONTACTS",
340 "shipto" => "ALL_SHIPTO",
341 "projects" => { "key" => "ALL_PROJECTS",
343 "old_id" => \@old_project_ids },
344 "employees" => "ALL_EMPLOYEES",
345 "salesmen" => "ALL_SALESMEN",
346 "taxzones" => "ALL_TAXZONES",
347 "currencies" => "ALL_CURRENCIES",
348 "customers" => "ALL_CUSTOMERS",
349 "price_factors" => "ALL_PRICE_FACTORS");
351 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
352 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
353 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
354 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
357 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
358 $TMPL_VAR{vclimit} = $myconfig{vclimit};
359 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('$form->{vc}', '', @{[ $form->{vc} eq 'vendor' ? 1 : 0 ]}, 0)";
360 push @custom_hiddens, "$form->{vc}_id";
361 push @custom_hiddens, "old$form->{vc}";
362 push @custom_hiddens, "select$form->{vc}";
364 # currencies and exchangerate
365 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
366 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
367 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
368 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
369 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
370 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
371 push @custom_hiddens, "forex";
372 push @custom_hiddens, "exchangerate" if $form->{forex};
374 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
375 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
377 $form->{fokus} = "invoice.customer";
379 my $follow_up_vc = $form->{customer};
380 $follow_up_vc =~ s/--\d*\s*$//;
381 $TMPL_VAR{customer_name} = $follow_up_vc;
383 # set option selected
384 foreach my $item (qw(AR)) {
385 $form->{"select$item"} =~ s/ selected//;
386 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
389 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
390 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
391 $TMPL_VAR{dateformat} = $myconfig{dateformat};
392 $TMPL_VAR{numberformat} = $myconfig{numberformat};
395 $TMPL_VAR{HIDDEN} = [qw(
396 id action type media format queued printed emailed title vc discount
397 creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
398 max_dunning_level dunning_amount
399 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
400 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
401 convert_from_do_ids convert_from_oe_ids
403 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
405 $form->{jsscript} = 1;
408 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
410 $main::lxdebug->leave_sub();
414 $main::lxdebug->enter_sub();
416 my $form = $main::form;
417 my %myconfig = %main::myconfig;
418 my $locale = $main::locale;
419 my $cgi = $main::cgi;
421 $main::auth->assert('invoice_edit');
423 $form->{invtotal} = $form->{invsubtotal};
425 my ($rows, $introws);
426 if (($rows = $form->numtextrows($form->{notes}, 26, 8)) < 2) {
429 if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
432 $rows = ($rows > $introws) ? $rows : $introws;
434 qq|<textarea name="notes" rows="$rows" cols="26" wrap="soft">$form->{notes}</textarea>|;
436 qq|<textarea name="intnotes" rows="$rows" cols="35" wrap="soft">$form->{intnotes}</textarea>|;
438 $form->{taxincluded} = ($form->{taxincluded} ? "checked" : "");
440 my $taxincluded = "";
441 if ($form->{taxaccounts}) {
443 <input name="taxincluded" class="checkbox" type="checkbox" $form->{taxincluded}> <b>|
444 . $locale->text('Tax Included') . qq|</b><br><br>|;
447 my ($tax, $subtotal);
448 if (!$form->{taxincluded}) {
450 foreach my $item (split / /, $form->{taxaccounts}) {
451 if ($form->{"${item}_base"}) {
452 $form->{"${item}_total"} =
454 $form->{"${item}_base"} * $form->{"${item}_rate"},
456 $form->{invtotal} += $form->{"${item}_total"};
457 $form->{"${item}_total"} =
458 $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
462 <th align="right">$form->{"${item}_description"} |
463 . $form->{"${item}_rate"} * 100 .qq|%</th>
464 <td align="right">$form->{"${item}_total"}</td>
470 $form->{invsubtotal} =
471 $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
475 <th align="right">| . $locale->text('Subtotal') . qq|</th>
476 <td align="right">$form->{invsubtotal}</td>
482 if ($form->{taxincluded}) {
483 foreach my $item (split / /, $form->{taxaccounts}) {
484 if ($form->{"${item}_base"}) {
485 $form->{"${item}_total"} =
487 ($form->{"${item}_base"} * $form->{"${item}_rate"} /
488 (1 + $form->{"${item}_rate"})
491 $form->{"${item}_netto"} =
493 ($form->{"${item}_base"} - $form->{"${item}_total"}),
495 $form->{"${item}_total"} =
496 $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
497 $form->{"${item}_netto"} =
498 $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
502 <th align="right">Enthaltene $form->{"${item}_description"} |
503 . $form->{"${item}_rate"} * 100 .qq|%</th>
504 <td align="right">$form->{"${item}_total"}</td>
507 <th align="right">Nettobetrag</th>
508 <td align="right">$form->{"${item}_netto"}</td>
516 $form->{oldinvtotal} = $form->{invtotal};
518 $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
520 my $follow_ups_block;
522 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
524 if (@{ $follow_ups} ) {
525 my $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
526 $follow_ups_block = qq|
528 <td colspan="2">| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</td>
542 <th align="left">| . $locale->text('Notes') . qq|</th>
543 <th align="left">| . $locale->text('Internal Notes') . qq|</th>
544 <th align="right">| . $locale->text('Payment Terms') . qq|</th>
549 <td><select name="payment_id" onChange="if (this.value) set_duedate(['payment_id__' + this.value, 'invdate__' + invdate.value],['duedate'])">$payment
558 <th align=left>| . $locale->text('Ertrag') . qq|</th>
559 <td>| . $form->format_amount(\%myconfig, $form->{marge_total}, 2, 0) . qq|</td>
562 <th align=left>| . $locale->text('Ertrag prozentual') . qq|</th>
563 <td>| . $form->format_amount(\%myconfig, $form->{marge_percent}, 2, 0) . qq| %</td>
565 <input type=hidden name="marge_total" value="$form->{"marge_total"}">
566 <input type=hidden name="marge_percent" value="$form->{"marge_percent"}">
575 <th align="right">| . $locale->text('Total') . qq|</th>
576 <td align="right">$form->{invtotal}</td>
589 <td><hr size="3" noshade></td>
592 <th class="listtop" align="left">Dokumente im Webdav-Repository</th>
595 <td align="left" width="30%"><b>Dateiname</b></td>
596 <td align="left" width="70%"><b>Webdavlink</b></td>
598 foreach my $file (@{ $form->{WEBDAV} }) {
601 <td align="left">$file->{name}</td>
602 <td align="left"><a href="$file->{link}">$file->{type}</a></td>
613 if ($form->{type} eq "credit_note") {
618 <tr class="listheading">
619 <th colspan="6" class="listheading">|
620 . $locale->text('Payments') . qq|</th>
628 <tr class="listheading">
629 <th colspan="6" class="listheading">|
630 . $locale->text('Incoming Payments') . qq|</th>
636 if ($form->{currency} eq $form->{defaultcurrency}) {
637 @column_index = qw(datepaid source memo paid AR_paid);
639 @column_index = qw(datepaid source memo paid exchangerate AR_paid);
643 $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
644 $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
645 $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
646 $column_data{AR_paid} = "<th>" . $locale->text('Account') . "</th>";
647 $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
648 $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
653 map { print "$column_data{$_}\n" } @column_index;
661 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
662 for my $i (1 .. $form->{paidaccounts}) {
667 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
668 $form->{"selectAR_paid_$i"} =~
669 s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
672 $totalpaid += $form->{"paid_$i"};
673 if ($form->{"paid_$i"}) {
675 $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
677 $form->{"exchangerate_$i"} =
678 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
680 if ($form->{"exchangerate_$i"} == 0) {
681 $form->{"exchangerate_$i"} = "";
683 my $exchangerate = qq| |;
684 if ($form->{currency} ne $form->{defaultcurrency}) {
685 if ($form->{"forex_$i"}) {
686 $exchangerate = qq|<input type="hidden" name="exchangerate_$i" value="$form->{"exchangerate_$i"}">$form->{"exchangerate_$i"}|;
688 $exchangerate = qq|<input name="exchangerate_$i" size="10" value="$form->{"exchangerate_$i"}">|;
692 $exchangerate .= qq|<input type="hidden" name="forex_$i" value="$form->{"forex_$i"}">|;
694 $column_data{"paid_$i"} =
695 qq|<td align="center"><input name="paid_$i" size="11" value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
696 $column_data{"exchangerate_$i"} = qq|<td align="center">$exchangerate</td>|;
697 $column_data{"AR_paid_$i"} =
698 qq|<td align="center"><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
699 $column_data{"datepaid_$i"} =
700 qq|<td align="center"><input id="datepaid_$i" name="datepaid_$i" size="11" title="$myconfig{dateformat}" value="$form->{"datepaid_$i"}" onBlur=\"check_right_date_format(this)\">
701 <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
702 $column_data{"source_$i"} =
703 qq|<td align=center><input name="source_$i" size="11" value="$form->{"source_$i"}"></td>|;
704 $column_data{"memo_$i"} =
705 qq|<td align="center"><input name="memo_$i" size="11" value="$form->{"memo_$i"}"></td>|;
707 map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
710 push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
713 my $paid_missing = $form->{oldinvtotal} - $totalpaid;
719 <td align="center">| . $locale->text('Total') . qq|</td>
720 <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
725 <td align="center">| . $locale->text('Missing amount') . qq|</td>
726 <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
730 map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } qw(paidaccounts selectAR_paid oldinvtotal));
731 print qq|<input type="hidden" name="oldtotalpaid" value="$totalpaid">
736 <td><hr size="3" noshade></td>
750 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
751 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
754 my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && (($totalpaid == 0) || ($totalpaid eq ""));
757 <input class="submit" type="submit" accesskey="u" name="action" id="update_button" value="|
758 . $locale->text('Update') . qq|">
759 <input class="submit" type="submit" name="action" value="|
760 . $locale->text('Ship to') . qq|">
761 <input class="submit" type="submit" name="action" value="|
762 . $locale->text('Print') . qq|">
763 <input class="submit" type="submit" name="action" value="|
764 . $locale->text('E-mail') . qq|"> |;
765 print qq|<input class="submit" type="submit" name="action" value="|
766 . $locale->text('Storno') . qq|"> | if ($show_storno);
767 print qq|<input class="submit" type="submit" name="action" value="|
768 . $locale->text('Post Payment') . qq|">
770 print qq|<input class="submit" type="submit" name="action" value="|
771 . $locale->text('Use As Template') . qq|">
773 if ($form->{id} && !($form->{type} eq "credit_note")) {
775 <input class="submit" type="submit" name="action" value="|
776 . $locale->text('Credit Note') . qq|">
779 if ($form->{radier}) {
781 <input class="submit" type="submit" name="action" value="|
782 . $locale->text('Delete') . qq|">
787 if ($invdate > $closedto) {
789 <input class="submit" type="submit" name="action" value="|
790 . $locale->text('Order') . qq|">
795 <input type="button" class="submit" onclick="follow_up_window()" value="|
796 . $locale->text('Follow-Up')
800 if ($invdate > $closedto) {
801 print qq|<input class="submit" type="submit" name="action" id="update_button" value="|
802 . $locale->text('Update') . qq|">
803 <input class="submit" type="submit" name="action" value="|
804 . $locale->text('Ship to') . qq|">
805 <input class="submit" type="submit" name="action" value="|
806 . $locale->text('Preview') . qq|">
807 <input class="submit" type="submit" name="action" value="|
808 . $locale->text('E-mail') . qq|">
809 <input class="submit" type="submit" name="action" value="|
810 . $locale->text('Print and Post') . qq|">
811 <input class="submit" type="submit" name="action" value="|
812 . $locale->text('Post') . qq|"> | .
813 NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
814 '-class' => 'submit'));
818 # button for saving history
819 if($form->{id} ne "") {
821 <input type="button" class="submit" onclick="set_history_window(|
823 . qq|);" name="history" id="history" value="|
824 . $locale->text('history')
827 # /button for saving history
829 # mark_as_paid button
830 if($form->{id} ne "") {
831 print qq|<input type="submit" class="submit" name="action" value="|
832 . $locale->text('mark as paid') . qq|">|;
834 # /mark_as_paid button
835 print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
838 <input type="hidden" name="rowcount" value="$form->{rowcount}">
840 $cgi->hidden("-name" => "callback", "-value" => $form->{callback})
841 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
842 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
843 . $cgi->hidden('-name' => 'customer_discount', '-value' => [$form->{customer_discount}])
852 $main::lxdebug->leave_sub();
856 $main::lxdebug->enter_sub();
858 my $form = $main::form;
859 my %myconfig = %main::myconfig;
861 $main::auth->assert('invoice_edit');
863 &mark_as_paid_common(\%myconfig,"ar");
865 $main::lxdebug->leave_sub();
869 $main::lxdebug->enter_sub();
871 my $form = $main::form;
872 my %myconfig = %main::myconfig;
874 $main::auth->assert('invoice_edit');
876 my ($recursive_call) = shift;
878 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
880 $form->{print_and_post} = 0 if $form->{second_run};
881 my $taxincluded = "checked" if $form->{taxincluded};
884 &check_name("customer");
886 $form->{taxincluded} ||= $taxincluded;
888 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
889 $form->{exchangerate} = $form->{forex} if $form->{forex};
891 for my $i (1 .. $form->{paidaccounts}) {
892 next unless $form->{"paid_$i"};
893 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
894 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
895 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
898 my $i = $form->{rowcount};
899 my $exchangerate = $form->{exchangerate} || 1;
901 # if last row empty, check the form otherwise retrieve new item
902 if ( ($form->{"partnumber_$i"} eq "")
903 && ($form->{"description_$i"} eq "")
904 && ($form->{"partsgroup_$i"} eq "")) {
906 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
911 IS->retrieve_item(\%myconfig, \%$form);
913 my $rows = scalar @{ $form->{item_list} };
915 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
918 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
927 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
929 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
930 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
932 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
933 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
935 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
937 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
938 my $decimalplaces = max 2, length $1;
941 $form->{"sellprice_$i"} = $sellprice;
943 # if there is an exchange rate adjust sellprice
944 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
945 $form->{"sellprice_$i"} /= $exchangerate;
948 $form->{"listprice_$i"} /= $exchangerate;
950 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
951 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
952 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
953 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
955 $form->{creditremaining} -= $amount;
957 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
959 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
961 if ($main::lizenzen) {
962 if ($form->{"inventory_accno_$i"} ne "") {
963 $form->{"lizenzen_$i"} = qq|<option></option>|;
964 foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
965 $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
967 $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
971 # get pricegroups for parts
972 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
974 # build up html code for prices_$i
982 # ok, so this is a new part
983 # ask if it is a part or service item
985 if ( $form->{"partsgroup_$i"}
986 && ($form->{"partsnumber_$i"} eq "")
987 && ($form->{"description_$i"} eq "")) {
989 $form->{"discount_$i"} = "";
993 $form->{"id_$i"} = 0;
998 $main::lxdebug->leave_sub();
1002 $main::lxdebug->enter_sub();
1004 my $form = $main::form;
1005 my %myconfig = %main::myconfig;
1006 my $locale = $main::locale;
1008 $main::auth->assert('invoice_edit');
1012 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1013 for my $i (1 .. $form->{paidaccounts}) {
1014 if ($form->{"paid_$i"}) {
1015 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1017 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1019 $form->error($locale->text('Cannot post payment for a closed period!'))
1020 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1022 if ($form->{currency} ne $form->{defaultcurrency}) {
1023 $form->{"exchangerate_$i"} = $form->{exchangerate}
1024 if ($invdate == $datepaid);
1025 $form->isblank("exchangerate_$i",
1026 $locale->text('Exchangerate for payment missing!'));
1031 ($form->{AR}) = split /--/, $form->{AR};
1032 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1034 $form->redirect($locale->text('Payment posted!'))
1035 if (IS->post_payment(\%myconfig, \%$form));
1036 $form->error($locale->text('Cannot post payment!'));
1039 $main::lxdebug->leave_sub();
1043 $main::lxdebug->enter_sub();
1045 my $form = $main::form;
1046 my %myconfig = %main::myconfig;
1047 my $locale = $main::locale;
1049 $main::auth->assert('invoice_edit');
1051 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1052 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
1053 $form->isblank("customer", $locale->text('Customer missing!'));
1054 $form->error($locale->text('Cannot post invoice for a closed period!'))
1055 if ($form->date_closed($form->{"invdate"}, \%myconfig));
1057 $form->{invnumber} =~ s/^\s*//g;
1058 $form->{invnumber} =~ s/\s*$//g;
1060 # if oldcustomer ne customer redo form
1061 if (&check_name('customer')) {
1065 if ($form->{second_run}) {
1066 $form->{print_and_post} = 0;
1071 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1072 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
1074 $form->error($locale->text('Cannot post invoice for a closed period!'))
1075 if ($invdate <= $closedto);
1077 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1078 if ($form->{currency} ne $form->{defaultcurrency});
1080 for my $i (1 .. $form->{paidaccounts}) {
1081 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1082 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1084 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1086 $form->error($locale->text('Cannot post payment for a closed period!'))
1087 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1089 if ($form->{currency} ne $form->{defaultcurrency}) {
1090 $form->{"exchangerate_$i"} = $form->{exchangerate}
1091 if ($invdate == $datepaid);
1092 $form->isblank("exchangerate_$i",
1093 $locale->text('Exchangerate for payment missing!'));
1098 ($form->{AR}) = split /--/, $form->{AR};
1099 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1100 $form->{storno} ||= 0;
1102 $form->{label} = $locale->text('Invoice');
1104 $form->{id} = 0 if $form->{postasnew};
1106 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
1107 if ($form->{postasnew}) {
1108 if ($form->{type} eq "credit_note") {
1109 undef($form->{cnnumber});
1111 undef($form->{invnumber});
1116 $form->error($locale->text('Cannot post invoice!'))
1117 unless IS->post_invoice(\%myconfig, \%$form);
1118 remove_draft() if $form->{remove_draft};
1120 if(!exists $form->{addition}) {
1121 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1122 $form->{addition} = $print_post ? "PRINTED AND POSTED" :
1123 $form->{storno} ? "STORNO" :
1125 $form->save_history($form->dbconnect(\%myconfig));
1128 $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
1131 $main::lxdebug->leave_sub();
1134 sub print_and_post {
1135 $main::lxdebug->enter_sub();
1137 my $form = $main::form;
1139 $main::auth->assert('invoice_edit');
1141 my $old_form = new Form;
1143 $form->{print_and_post} = 1;
1147 $main::lxdebug->leave_sub();
1151 sub use_as_template {
1152 $main::lxdebug->enter_sub();
1154 my $form = $main::form;
1155 my %myconfig = %main::myconfig;
1157 $main::auth->assert('invoice_edit');
1159 map { delete $form->{$_} } qw(printed emailed queued invnumber invdate deliverydate id datepaid_1 source_1 memo_1 paid_1 exchangerate_1 AP_paid_1 storno);
1160 $form->{paidaccounts} = 1;
1161 $form->{rowcount}--;
1162 $form->{invdate} = $form->current_date(\%myconfig);
1165 $main::lxdebug->leave_sub();
1169 $main::lxdebug->enter_sub();
1171 my $form = $main::form;
1172 my %myconfig = %main::myconfig;
1173 my $locale = $main::locale;
1175 $main::auth->assert('invoice_edit');
1177 if ($form->{storno}) {
1178 $form->error($locale->text('Cannot storno storno invoice!'));
1181 if (IS->has_storno(\%myconfig, $form, "ar")) {
1182 $form->error($locale->text("Invoice has already been storno'd!"));
1185 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
1191 # Payments must not be recorded for the new storno invoice.
1192 $form->{paidaccounts} = 0;
1193 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
1195 $form->{storno_id} = $form->{id};
1196 $form->{storno} = 1;
1198 $form->{invnumber} = "Storno zu " . $form->{invnumber};
1199 $form->{rowcount}++;
1202 $main::lxdebug->leave_sub();
1206 $main::lxdebug->enter_sub();
1208 my $form = $main::form;
1210 $main::auth->assert('invoice_edit');
1212 $form->{preview} = 1;
1213 my $old_form = new Form;
1214 for (keys %$form) { $old_form->{$_} = $form->{$_} }
1216 &print_form($old_form);
1217 $main::lxdebug->leave_sub();
1222 $main::lxdebug->enter_sub();
1224 my $form = $main::form;
1225 my $locale = $main::locale;
1227 $main::auth->assert('invoice_edit');
1229 if ($form->{second_run}) {
1230 $form->{print_and_post} = 0;
1237 <form method="post" action="$form->{script}">
1240 # delete action variable
1241 map { delete $form->{$_} } qw(action header);
1243 foreach my $key (keys %$form) {
1244 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1245 $form->{$key} =~ s/\"/"/g;
1246 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1250 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
1253 . $locale->text('Are you sure you want to delete Invoice Number')
1254 . qq| $form->{invnumber}
1258 <input name="action" class="submit" type="submit" value="|
1259 . $locale->text('Yes') . qq|">
1263 $main::lxdebug->leave_sub();
1267 $main::lxdebug->enter_sub();
1269 my $form = $main::form;
1270 my %myconfig = %main::myconfig;
1271 my $locale = $main::locale;
1273 $main::auth->assert('invoice_edit');
1275 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1277 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1280 $form->{rowcount}--;
1281 $form->{shipto} = 1;
1284 $form->{title} = $locale->text('Add Credit Note');
1285 $form->{script} = 'is.pl';
1287 our $buysell = 'buy';
1290 # bo creates the id, reset it
1291 map { delete $form->{$_} }
1292 qw(id invnumber subject message cc bcc printed emailed queued);
1293 $form->{ $form->{vc} } =~ s/--.*//g;
1294 $form->{type} = "credit_note";
1297 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
1299 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1300 qw(creditlimit creditremaining);
1302 my $currency = $form->{currency};
1305 $form->{currency} = $currency;
1306 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1307 $form->{exchangerate} = $form->{forex} || '';
1309 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1316 $main::lxdebug->leave_sub();
1320 $main::lxdebug->enter_sub();
1322 my $form = $main::form;
1323 my %myconfig = %main::myconfig;
1324 my $locale = $main::locale;
1326 $main::auth->assert('invoice_edit');
1328 if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
1329 # saving the history
1330 if(!exists $form->{addition}) {
1331 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1332 $form->{addition} = "DELETED";
1333 $form->save_history($form->dbconnect(\%myconfig));
1335 # /saving the history
1336 $form->redirect($locale->text('Invoice deleted!'));
1338 $form->error($locale->text('Cannot delete invoice!'));
1340 $main::lxdebug->leave_sub();
1344 $main::lxdebug->enter_sub();
1346 my $form = $main::form;
1348 $main::auth->assert('invoice_edit');
1353 my $saved_form = save_form();
1357 restore_form($saved_form, 0, qw(id invnumber));
1362 $main::lxdebug->leave_sub();