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 # build vendor/customer drop down comatibility... don't ask
198 if (@{ $form->{"all_customer"} }) {
199 $form->{"selectcustomer"} = 1;
200 $form->{customer} = qq|$form->{customer}--$form->{"customer_id"}|;
203 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
204 $form->{selectcustomer} = 1;
207 if ($form->{all_departments}) {
208 $form->{selectdepartment} = "<option>\n";
209 $form->{department} = "$form->{department}--$form->{department_id}";
212 $form->{selectdepartment} .=
213 "<option>$_->{description}--$_->{id}</option>\n"
214 } (@{ $form->{all_departments} });
217 $form->{employee} = "$form->{employee}--$form->{employee_id}";
220 $form->{forex} = $form->{exchangerate};
221 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
223 foreach my $key (keys %{ $form->{AR_links} }) {
224 foreach my $ref (@{ $form->{AR_links}{$key} }) {
225 $form->{"select$key"} .=
226 "<option>$ref->{accno}--$ref->{description}</option>\n";
229 if ($key eq "AR_paid") {
230 next unless $form->{acc_trans}{$key};
231 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
232 $form->{"AR_paid_$i"} =
233 "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
236 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
237 $form->{"datepaid_$i"} =
238 $form->{acc_trans}{$key}->[$i - 1]->{transdate};
239 $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
240 $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
241 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
242 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
244 $form->{paidaccounts} = $i;
248 "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
253 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
255 $form->{AR} = $form->{AR_1} unless $form->{id};
258 ($form->datetonum($form->{invdate}, \%myconfig) <=
259 $form->datetonum($form->{closedto}, \%myconfig));
261 $main::lxdebug->leave_sub();
264 sub prepare_invoice {
265 $main::lxdebug->enter_sub();
267 my $form = $main::form;
268 my %myconfig = %main::myconfig;
270 $main::auth->assert('invoice_edit');
272 if ($form->{type} eq "credit_note") {
273 $form->{type} = "credit_note";
274 $form->{formname} = "credit_note";
276 $form->{type} = "invoice";
277 $form->{formname} = "invoice";
284 foreach my $ref (@{ $form->{invoice_details} }) {
287 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
289 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
290 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
292 my $decimalplaces = ($dec > 2) ? $dec : 2;
294 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
295 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
296 $dec_qty = length $dec_qty;
298 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
300 $form->{rowcount} = $i;
304 # get pricegroups for parts
305 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
306 set_pricegroup($_) for 1 .. $form->{rowcount};
308 $main::lxdebug->leave_sub();
312 $main::lxdebug->enter_sub();
314 my $form = $main::form;
315 my %myconfig = %main::myconfig;
316 my $locale = $main::locale;
317 my $cgi = $main::cgi;
319 $main::auth->assert('invoice_edit');
324 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
325 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
327 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
328 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
330 $payment = qq|<option value=""></option>|;
331 foreach my $item (@{ $form->{payment_terms} }) {
332 if ($form->{payment_id} eq $item->{id}) {
333 $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
335 $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
339 my $set_duedate_url = "$form->{script}?action=set_duedate";
341 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
343 my @old_project_ids = ($form->{"globalproject_id"});
344 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
346 $form->get_lists("contacts" => "ALL_CONTACTS",
347 "shipto" => "ALL_SHIPTO",
348 "projects" => { "key" => "ALL_PROJECTS",
350 "old_id" => \@old_project_ids },
351 "employees" => "ALL_EMPLOYEES",
352 "salesmen" => "ALL_SALESMEN",
353 "taxzones" => "ALL_TAXZONES",
354 "currencies" => "ALL_CURRENCIES",
355 "customers" => "ALL_CUSTOMERS",
356 "price_factors" => "ALL_PRICE_FACTORS");
358 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
359 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
360 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
361 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
364 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
365 $TMPL_VAR{vclimit} = $myconfig{vclimit};
366 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
367 push @custom_hiddens, "customer_id";
368 push @custom_hiddens, "oldcustomer";
369 push @custom_hiddens, "selectcustomer";
371 # currencies and exchangerate
372 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
373 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
374 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
375 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
376 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
377 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
378 push @custom_hiddens, "forex";
379 push @custom_hiddens, "exchangerate" if $form->{forex};
381 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
382 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
384 $form->{fokus} = "invoice.customer";
386 my $follow_up_vc = $form->{customer};
387 $follow_up_vc =~ s/--\d*\s*$//;
388 $TMPL_VAR{customer_name} = $follow_up_vc;
390 # set option selected
391 foreach my $item (qw(AR)) {
392 $form->{"select$item"} =~ s/ selected//;
393 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
396 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
397 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
398 $TMPL_VAR{dateformat} = $myconfig{dateformat};
399 $TMPL_VAR{numberformat} = $myconfig{numberformat};
402 $TMPL_VAR{HIDDENS} = [qw(
403 id action type media format queued printed emailed title vc discount
404 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
405 max_dunning_level dunning_amount
406 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
407 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
408 convert_from_do_ids convert_from_oe_ids
410 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
412 $form->{jsscript} = 1;
415 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
417 $main::lxdebug->leave_sub();
421 $main::lxdebug->enter_sub();
423 my $form = $main::form;
424 my %myconfig = %main::myconfig;
425 my $locale = $main::locale;
426 my $cgi = $main::cgi;
428 $main::auth->assert('invoice_edit');
430 $form->{invtotal} = $form->{invsubtotal};
432 my ($rows, $introws);
433 if (($rows = $form->numtextrows($form->{notes}, 26, 8)) < 2) {
436 if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
439 $rows = ($rows > $introws) ? $rows : $introws;
440 my $notes = qq|<textarea name="notes" rows="$rows" cols="26" wrap="soft">$form->{notes}</textarea>|;
441 my $intnotes = qq|<textarea name="intnotes" rows="$rows" cols="35" wrap="soft">$form->{intnotes}</textarea>|;
443 $form->{taxincluded} = ($form->{taxincluded} ? "checked" : "");
445 my $taxincluded = "";
446 if ($form->{taxaccounts}) {
447 $taxincluded = qq| <input name="taxincluded" class="checkbox" type="checkbox" $form->{taxincluded}> <b>| . $locale->text('Tax Included') . qq|</b><br><br>|;
450 my ($tax, $subtotal);
451 if (!$form->{taxincluded}) {
453 foreach my $item (split / /, $form->{taxaccounts}) {
454 if ($form->{"${item}_base"}) {
455 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
456 $form->{invtotal} += $form->{"${item}_total"};
457 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
461 <th align="right">$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
462 <td align="right">$form->{"${item}_total"}</td>
468 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
472 <th align="right">| . $locale->text('Subtotal') . qq|</th>
473 <td align="right">$form->{invsubtotal}</td>
479 if ($form->{taxincluded}) {
480 foreach my $item (split / /, $form->{taxaccounts}) {
481 if ($form->{"${item}_base"}) {
482 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
483 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
484 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
485 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
489 <th align="right">Enthaltene $form->{"${item}_description"} |
490 . $form->{"${item}_rate"} * 100 .qq|%</th>
491 <td align="right">$form->{"${item}_total"}</td>
494 <th align="right">Nettobetrag</th>
495 <td align="right">$form->{"${item}_netto"}</td>
503 $form->{oldinvtotal} = $form->{invtotal};
504 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
506 my $follow_ups_block;
508 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
510 if (@{ $follow_ups} ) {
511 my $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
512 $follow_ups_block = qq|
514 <td colspan="2">| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</td>
528 <th align="left">| . $locale->text('Notes') . qq|</th>
529 <th align="left">| . $locale->text('Internal Notes') . qq|</th>
530 <th align="right">| . $locale->text('Payment Terms') . qq|</th>
535 <td><select name="payment_id" onChange="if (this.value) set_duedate(['payment_id__' + this.value, 'invdate__' + invdate.value],['duedate'])">$payment
544 <th align=left>| . $locale->text('Ertrag') . qq|</th>
545 <td>| . $form->format_amount(\%myconfig, $form->{marge_total}, 2, 0) . qq|</td>
548 <th align=left>| . $locale->text('Ertrag prozentual') . qq|</th>
549 <td>| . $form->format_amount(\%myconfig, $form->{marge_percent}, 2, 0) . qq| %</td>
551 <input type=hidden name="marge_total" value="$form->{"marge_total"}">
552 <input type=hidden name="marge_percent" value="$form->{"marge_percent"}">
561 <th align="right">| . $locale->text('Total') . qq|</th>
562 <td align="right">$form->{invtotal}</td>
575 <td><hr size="3" noshade></td>
578 <th class="listtop" align="left">Dokumente im Webdav-Repository</th>
581 <td align="left" width="30%"><b>Dateiname</b></td>
582 <td align="left" width="70%"><b>Webdavlink</b></td>
584 foreach my $file (@{ $form->{WEBDAV} }) {
587 <td align="left">$file->{name}</td>
588 <td align="left"><a href="$file->{link}">$file->{type}</a></td>
599 if ($form->{type} eq "credit_note") {
604 <tr class="listheading">
605 <th colspan="6" class="listheading">|
606 . $locale->text('Payments') . qq|</th>
614 <tr class="listheading">
615 <th colspan="6" class="listheading">|
616 . $locale->text('Incoming Payments') . qq|</th>
622 if ($form->{currency} eq $form->{defaultcurrency}) {
623 @column_index = qw(datepaid source memo paid AR_paid);
625 @column_index = qw(datepaid source memo paid exchangerate AR_paid);
629 $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
630 $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
631 $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
632 $column_data{AR_paid} = "<th>" . $locale->text('Account') . "</th>";
633 $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
634 $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
639 map { print "$column_data{$_}\n" } @column_index;
647 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
648 for my $i (1 .. $form->{paidaccounts}) {
653 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
654 $form->{"selectAR_paid_$i"} =~
655 s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
658 $totalpaid += $form->{"paid_$i"};
659 if ($form->{"paid_$i"}) {
660 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
662 $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
664 if ($form->{"exchangerate_$i"} == 0) {
665 $form->{"exchangerate_$i"} = "";
667 my $exchangerate = qq| |;
668 if ($form->{currency} ne $form->{defaultcurrency}) {
669 if ($form->{"forex_$i"}) {
670 $exchangerate = qq|<input type="hidden" name="exchangerate_$i" value="$form->{"exchangerate_$i"}">$form->{"exchangerate_$i"}|;
672 $exchangerate = qq|<input name="exchangerate_$i" size="10" value="$form->{"exchangerate_$i"}">|;
676 $exchangerate .= qq|<input type="hidden" name="forex_$i" value="$form->{"forex_$i"}">|;
678 $column_data{"paid_$i"} =
679 qq|<td align="center"><input name="paid_$i" size="11" value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
680 $column_data{"exchangerate_$i"} = qq|<td align="center">$exchangerate</td>|;
681 $column_data{"AR_paid_$i"} =
682 qq|<td align="center"><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
683 $column_data{"datepaid_$i"} =
684 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)\">
685 <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
686 $column_data{"source_$i"} =
687 qq|<td align=center><input name="source_$i" size="11" value="$form->{"source_$i"}"></td>|;
688 $column_data{"memo_$i"} =
689 qq|<td align="center"><input name="memo_$i" size="11" value="$form->{"memo_$i"}"></td>|;
691 map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
694 push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
697 my $paid_missing = $form->{oldinvtotal} - $totalpaid;
703 <td align="center">| . $locale->text('Total') . qq|</td>
704 <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
709 <td align="center">| . $locale->text('Missing amount') . qq|</td>
710 <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
714 map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } qw(paidaccounts selectAR_paid oldinvtotal));
715 print qq|<input type="hidden" name="oldtotalpaid" value="$totalpaid">
720 <td><hr size="3" noshade></td>
734 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
735 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
738 my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && (($totalpaid == 0) || ($totalpaid eq ""));
741 <input class="submit" type="submit" accesskey="u" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
742 <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
743 <input class="submit" type="submit" name="action" value="| . $locale->text('Print') . qq|">
744 <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|"> |;
745 print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Storno') . qq|"> | if ($show_storno);
746 print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Post Payment') . qq|"> |;
747 print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Use As Template') . qq|"> |;
748 if ($form->{id} && !($form->{type} eq "credit_note")) {
749 print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Credit Note') . qq|"> |;
751 if ($form->{radier}) {
752 print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Delete') . qq|"> |;
756 if ($invdate > $closedto) {
757 print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Order') . qq|"> |;
760 print qq| <input type="button" class="submit" onclick="follow_up_window()" value="| . $locale->text('Follow-Up') . qq|">|;
763 if ($invdate > $closedto) {
765 <input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
766 <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
767 <input class="submit" type="submit" name="action" value="| . $locale->text('Preview') . qq|">
768 <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|">
769 <input class="submit" type="submit" name="action" value="| . $locale->text('Print and Post') . qq|">
770 <input class="submit" type="submit" name="action" value="| . $locale->text('Post') . qq|"> | .
771 NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
775 # button for saving history
776 if($form->{id} ne "") {
778 <input type="button" class="submit" onclick="set_history_window(|
780 . qq|);" name="history" id="history" value="|
781 . $locale->text('history')
784 # /button for saving history
786 # mark_as_paid button
787 if($form->{id} ne "") {
788 print qq|<input type="submit" class="submit" name="action" value="| . $locale->text('mark as paid') . qq|">|;
790 # /mark_as_paid button
791 print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
794 <input type="hidden" name="rowcount" value="$form->{rowcount}">
796 $cgi->hidden("-name" => "callback", "-value" => $form->{callback})
797 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
798 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
799 . $cgi->hidden('-name' => 'customer_discount', '-value' => [$form->{customer_discount}])
808 $main::lxdebug->leave_sub();
812 $main::lxdebug->enter_sub();
814 my $form = $main::form;
815 my %myconfig = %main::myconfig;
817 $main::auth->assert('invoice_edit');
819 &mark_as_paid_common(\%myconfig,"ar");
821 $main::lxdebug->leave_sub();
825 $main::lxdebug->enter_sub();
827 my $form = $main::form;
828 my %myconfig = %main::myconfig;
830 $main::auth->assert('invoice_edit');
832 my ($recursive_call) = shift;
834 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
836 $form->{print_and_post} = 0 if $form->{second_run};
837 my $taxincluded = "checked" if $form->{taxincluded};
840 &check_name("customer");
842 $form->{taxincluded} ||= $taxincluded;
844 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
845 $form->{exchangerate} = $form->{forex} if $form->{forex};
847 for my $i (1 .. $form->{paidaccounts}) {
848 next unless $form->{"paid_$i"};
849 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
850 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
851 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
854 my $i = $form->{rowcount};
855 my $exchangerate = $form->{exchangerate} || 1;
857 # if last row empty, check the form otherwise retrieve new item
858 if ( ($form->{"partnumber_$i"} eq "")
859 && ($form->{"description_$i"} eq "")
860 && ($form->{"partsgroup_$i"} eq "")) {
862 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
867 IS->retrieve_item(\%myconfig, \%$form);
869 my $rows = scalar @{ $form->{item_list} };
871 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
874 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
883 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
885 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
886 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
888 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
889 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
891 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
893 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
894 my $decimalplaces = max 2, length $1;
897 $form->{"sellprice_$i"} = $sellprice;
899 # if there is an exchange rate adjust sellprice
900 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
901 $form->{"sellprice_$i"} /= $exchangerate;
904 $form->{"listprice_$i"} /= $exchangerate;
906 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
907 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
908 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
909 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
911 $form->{creditremaining} -= $amount;
913 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
915 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
917 if ($main::lizenzen) {
918 if ($form->{"inventory_accno_$i"} ne "") {
919 $form->{"lizenzen_$i"} = qq|<option></option>|;
920 foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
921 $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
923 $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
927 # get pricegroups for parts
928 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
930 # build up html code for prices_$i
938 # ok, so this is a new part
939 # ask if it is a part or service item
941 if ( $form->{"partsgroup_$i"}
942 && ($form->{"partsnumber_$i"} eq "")
943 && ($form->{"description_$i"} eq "")) {
945 $form->{"discount_$i"} = "";
949 $form->{"id_$i"} = 0;
954 $main::lxdebug->leave_sub();
958 $main::lxdebug->enter_sub();
960 my $form = $main::form;
961 my %myconfig = %main::myconfig;
962 my $locale = $main::locale;
964 $main::auth->assert('invoice_edit');
968 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
969 for my $i (1 .. $form->{paidaccounts}) {
970 if ($form->{"paid_$i"}) {
971 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
973 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
975 $form->error($locale->text('Cannot post payment for a closed period!'))
976 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
978 if ($form->{currency} ne $form->{defaultcurrency}) {
979 $form->{"exchangerate_$i"} = $form->{exchangerate}
980 if ($invdate == $datepaid);
981 $form->isblank("exchangerate_$i",
982 $locale->text('Exchangerate for payment missing!'));
987 ($form->{AR}) = split /--/, $form->{AR};
988 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
990 $form->redirect($locale->text('Payment posted!'))
991 if (IS->post_payment(\%myconfig, \%$form));
992 $form->error($locale->text('Cannot post payment!'));
995 $main::lxdebug->leave_sub();
999 $main::lxdebug->enter_sub();
1001 my $form = $main::form;
1002 my %myconfig = %main::myconfig;
1003 my $locale = $main::locale;
1005 $main::auth->assert('invoice_edit');
1007 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1008 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
1009 $form->isblank("customer", $locale->text('Customer missing!'));
1010 $form->error($locale->text('Cannot post invoice for a closed period!'))
1011 if ($form->date_closed($form->{"invdate"}, \%myconfig));
1013 $form->{invnumber} =~ s/^\s*//g;
1014 $form->{invnumber} =~ s/\s*$//g;
1016 # if oldcustomer ne customer redo form
1017 if (&check_name('customer')) {
1021 if ($form->{second_run}) {
1022 $form->{print_and_post} = 0;
1027 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1028 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
1030 $form->error($locale->text('Cannot post invoice for a closed period!'))
1031 if ($invdate <= $closedto);
1033 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1034 if ($form->{currency} ne $form->{defaultcurrency});
1036 for my $i (1 .. $form->{paidaccounts}) {
1037 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1038 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1040 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1042 $form->error($locale->text('Cannot post payment for a closed period!'))
1043 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1045 if ($form->{currency} ne $form->{defaultcurrency}) {
1046 $form->{"exchangerate_$i"} = $form->{exchangerate}
1047 if ($invdate == $datepaid);
1048 $form->isblank("exchangerate_$i",
1049 $locale->text('Exchangerate for payment missing!'));
1054 ($form->{AR}) = split /--/, $form->{AR};
1055 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1056 $form->{storno} ||= 0;
1058 $form->{label} = $locale->text('Invoice');
1060 $form->{id} = 0 if $form->{postasnew};
1062 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
1063 if ($form->{postasnew}) {
1064 if ($form->{type} eq "credit_note") {
1065 undef($form->{cnnumber});
1067 undef($form->{invnumber});
1072 $form->error($locale->text('Cannot post invoice!'))
1073 unless IS->post_invoice(\%myconfig, \%$form);
1074 remove_draft() if $form->{remove_draft};
1076 if(!exists $form->{addition}) {
1077 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1078 $form->{addition} = $print_post ? "PRINTED AND POSTED" :
1079 $form->{storno} ? "STORNO" :
1081 $form->save_history($form->dbconnect(\%myconfig));
1084 $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
1087 $main::lxdebug->leave_sub();
1090 sub print_and_post {
1091 $main::lxdebug->enter_sub();
1093 my $form = $main::form;
1095 $main::auth->assert('invoice_edit');
1097 my $old_form = new Form;
1099 $form->{print_and_post} = 1;
1103 $main::lxdebug->leave_sub();
1107 sub use_as_template {
1108 $main::lxdebug->enter_sub();
1110 my $form = $main::form;
1111 my %myconfig = %main::myconfig;
1113 $main::auth->assert('invoice_edit');
1115 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);
1116 $form->{paidaccounts} = 1;
1117 $form->{rowcount}--;
1118 $form->{invdate} = $form->current_date(\%myconfig);
1121 $main::lxdebug->leave_sub();
1125 $main::lxdebug->enter_sub();
1127 my $form = $main::form;
1128 my %myconfig = %main::myconfig;
1129 my $locale = $main::locale;
1131 $main::auth->assert('invoice_edit');
1133 if ($form->{storno}) {
1134 $form->error($locale->text('Cannot storno storno invoice!'));
1137 if (IS->has_storno(\%myconfig, $form, "ar")) {
1138 $form->error($locale->text("Invoice has already been storno'd!"));
1141 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
1147 # Payments must not be recorded for the new storno invoice.
1148 $form->{paidaccounts} = 0;
1149 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
1151 $form->{storno_id} = $form->{id};
1152 $form->{storno} = 1;
1154 $form->{invnumber} = "Storno zu " . $form->{invnumber};
1155 $form->{rowcount}++;
1158 $main::lxdebug->leave_sub();
1162 $main::lxdebug->enter_sub();
1164 my $form = $main::form;
1166 $main::auth->assert('invoice_edit');
1168 $form->{preview} = 1;
1169 my $old_form = new Form;
1170 for (keys %$form) { $old_form->{$_} = $form->{$_} }
1172 &print_form($old_form);
1173 $main::lxdebug->leave_sub();
1178 $main::lxdebug->enter_sub();
1180 my $form = $main::form;
1181 my $locale = $main::locale;
1183 $main::auth->assert('invoice_edit');
1185 if ($form->{second_run}) {
1186 $form->{print_and_post} = 0;
1193 <form method="post" action="$form->{script}">
1196 # delete action variable
1197 map { delete $form->{$_} } qw(action header);
1199 foreach my $key (keys %$form) {
1200 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1201 $form->{$key} =~ s/\"/"/g;
1202 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1206 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
1209 . $locale->text('Are you sure you want to delete Invoice Number')
1210 . qq| $form->{invnumber}
1214 <input name="action" class="submit" type="submit" value="|
1215 . $locale->text('Yes') . qq|">
1219 $main::lxdebug->leave_sub();
1223 $main::lxdebug->enter_sub();
1225 my $form = $main::form;
1226 my %myconfig = %main::myconfig;
1227 my $locale = $main::locale;
1229 $main::auth->assert('invoice_edit');
1231 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1233 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1236 $form->{rowcount}--;
1237 $form->{shipto} = 1;
1240 $form->{title} = $locale->text('Add Credit Note');
1241 $form->{script} = 'is.pl';
1243 our $buysell = 'buy';
1246 # bo creates the id, reset it
1247 map { delete $form->{$_} }
1248 qw(id invnumber subject message cc bcc printed emailed queued);
1249 $form->{ $form->{vc} } =~ s/--.*//g;
1250 $form->{type} = "credit_note";
1253 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
1255 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1256 qw(creditlimit creditremaining);
1258 my $currency = $form->{currency};
1261 $form->{currency} = $currency;
1262 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1263 $form->{exchangerate} = $form->{forex} || '';
1265 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1272 $main::lxdebug->leave_sub();
1276 $main::lxdebug->enter_sub();
1278 my $form = $main::form;
1279 my %myconfig = %main::myconfig;
1280 my $locale = $main::locale;
1282 $main::auth->assert('invoice_edit');
1284 if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
1285 # saving the history
1286 if(!exists $form->{addition}) {
1287 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1288 $form->{addition} = "DELETED";
1289 $form->save_history($form->dbconnect(\%myconfig));
1291 # /saving the history
1292 $form->redirect($locale->text('Invoice deleted!'));
1294 $form->error($locale->text('Cannot delete invoice!'));
1296 $main::lxdebug->leave_sub();
1300 $main::lxdebug->enter_sub();
1302 my $form = $main::form;
1304 $main::auth->assert('invoice_edit');
1309 my $saved_form = save_form();
1313 restore_form($saved_form, 0, qw(id invnumber));
1318 $main::lxdebug->leave_sub();