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};
150 $form->backup_vars(qw(payment_id language_id taxzone_id salesman_id taxincluded cp_id intnotes id shipto_id));
152 IS->get_customer(\%myconfig, \%$form);
154 #quote all_customer Bug 133
155 foreach my $ref (@{ $form->{all_customer} }) {
156 $ref->{name} = $form->quote($ref->{name});
159 $form->restore_vars(qw(id));
161 IS->retrieve_invoice(\%myconfig, \%$form);
162 $form->restore_vars(qw(payment_id language_id taxzone_id intnotes cp_id shipto_id));
163 $form->restore_vars(qw(taxincluded)) if $form->{id};
164 $form->restore_vars(qw(salesman_id)) if $main::editing;
166 # build vendor/customer drop down comatibility... don't ask
167 if (@{ $form->{"all_customer"} }) {
168 $form->{"selectcustomer"} = 1;
169 $form->{customer} = qq|$form->{customer}--$form->{"customer_id"}|;
172 $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
173 $form->{selectcustomer} = 1;
176 if ($form->{all_departments}) {
177 $form->{selectdepartment} = "<option>\n";
178 $form->{department} = "$form->{department}--$form->{department_id}";
181 $form->{selectdepartment} .=
182 "<option>$_->{description}--$_->{id}</option>\n"
183 } (@{ $form->{all_departments} });
186 $form->{employee} = "$form->{employee}--$form->{employee_id}";
189 $form->{forex} = $form->{exchangerate};
190 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
192 foreach my $key (keys %{ $form->{AR_links} }) {
193 foreach my $ref (@{ $form->{AR_links}{$key} }) {
194 $form->{"select$key"} .=
195 "<option>$ref->{accno}--$ref->{description}</option>\n";
198 if ($key eq "AR_paid") {
199 next unless $form->{acc_trans}{$key};
200 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
201 $form->{"AR_paid_$i"} =
202 "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
205 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
206 $form->{"datepaid_$i"} =
207 $form->{acc_trans}{$key}->[$i - 1]->{transdate};
208 $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
209 $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
210 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
211 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
213 $form->{paidaccounts} = $i;
217 "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
222 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
224 $form->{AR} = $form->{AR_1} unless $form->{id};
227 ($form->datetonum($form->{invdate}, \%myconfig) <=
228 $form->datetonum($form->{closedto}, \%myconfig));
230 $main::lxdebug->leave_sub();
233 sub prepare_invoice {
234 $main::lxdebug->enter_sub();
236 my $form = $main::form;
237 my %myconfig = %main::myconfig;
239 $main::auth->assert('invoice_edit');
241 if ($form->{type} eq "credit_note") {
242 $form->{type} = "credit_note";
243 $form->{formname} = "credit_note";
245 $form->{type} = "invoice";
246 $form->{formname} = "invoice";
253 foreach my $ref (@{ $form->{invoice_details} }) {
256 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
258 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
259 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
261 my $decimalplaces = ($dec > 2) ? $dec : 2;
263 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
264 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
265 $dec_qty = length $dec_qty;
267 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
269 $form->{rowcount} = $i;
273 # get pricegroups for parts
274 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
275 set_pricegroup($_) for 1 .. $form->{rowcount};
277 $main::lxdebug->leave_sub();
281 $main::lxdebug->enter_sub();
283 my $form = $main::form;
284 my %myconfig = %main::myconfig;
285 my $locale = $main::locale;
286 my $cgi = $main::cgi;
288 $main::auth->assert('invoice_edit');
293 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
294 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
296 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
297 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
299 $payment = qq|<option value=""></option>|;
300 foreach my $item (@{ $form->{payment_terms} }) {
301 if ($form->{payment_id} eq $item->{id}) {
302 $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
304 $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
308 my $set_duedate_url = "$form->{script}?action=set_duedate";
310 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
312 my @old_project_ids = ($form->{"globalproject_id"});
313 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
315 $form->get_lists("contacts" => "ALL_CONTACTS",
316 "shipto" => "ALL_SHIPTO",
317 "projects" => { "key" => "ALL_PROJECTS",
319 "old_id" => \@old_project_ids },
320 "employees" => "ALL_EMPLOYEES",
321 "salesmen" => "ALL_SALESMEN",
322 "taxzones" => "ALL_TAXZONES",
323 "currencies" => "ALL_CURRENCIES",
324 "customers" => "ALL_CUSTOMERS",
325 "price_factors" => "ALL_PRICE_FACTORS");
327 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
328 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
329 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
330 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
333 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
334 $TMPL_VAR{vclimit} = $myconfig{vclimit};
335 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
336 push @custom_hiddens, "customer_id";
337 push @custom_hiddens, "oldcustomer";
338 push @custom_hiddens, "selectcustomer";
340 # currencies and exchangerate
341 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
342 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
343 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
344 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
345 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
346 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
347 push @custom_hiddens, "forex";
348 push @custom_hiddens, "exchangerate" if $form->{forex};
350 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
351 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
353 $form->{fokus} = "invoice.customer";
355 my $follow_up_vc = $form->{customer};
356 $follow_up_vc =~ s/--\d*\s*$//;
357 $TMPL_VAR{customer_name} = $follow_up_vc;
359 # set option selected
360 foreach my $item (qw(AR)) {
361 $form->{"select$item"} =~ s/ selected//;
362 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
365 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
366 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
367 $TMPL_VAR{dateformat} = $myconfig{dateformat};
368 $TMPL_VAR{numberformat} = $myconfig{numberformat};
371 $TMPL_VAR{HIDDENS} = [qw(
372 id action type media format queued printed emailed title vc discount
373 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
374 max_dunning_level dunning_amount
375 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
376 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
377 convert_from_do_ids convert_from_oe_ids
379 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
381 $form->{jsscript} = 1;
384 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
386 $main::lxdebug->leave_sub();
390 $main::lxdebug->enter_sub();
392 my $form = $main::form;
393 my %myconfig = %main::myconfig;
394 my $locale = $main::locale;
395 my $cgi = $main::cgi;
397 $main::auth->assert('invoice_edit');
399 $form->{invtotal} = $form->{invsubtotal};
401 my ($rows, $introws);
402 if (($rows = $form->numtextrows($form->{notes}, 26, 8)) < 2) {
405 if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
408 $rows = ($rows > $introws) ? $rows : $introws;
409 my $notes = qq|<textarea name="notes" rows="$rows" cols="26" wrap="soft">$form->{notes}</textarea>|;
410 my $intnotes = qq|<textarea name="intnotes" rows="$rows" cols="35" wrap="soft">$form->{intnotes}</textarea>|;
412 $form->{taxincluded} = ($form->{taxincluded} ? "checked" : "");
414 my $taxincluded = "";
415 if ($form->{taxaccounts}) {
416 $taxincluded = qq| <input name="taxincluded" class="checkbox" type="checkbox" $form->{taxincluded}> <b>| . $locale->text('Tax Included') . qq|</b><br><br>|;
419 my ($tax, $subtotal);
420 if (!$form->{taxincluded}) {
422 foreach my $item (split / /, $form->{taxaccounts}) {
423 if ($form->{"${item}_base"}) {
424 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
425 $form->{invtotal} += $form->{"${item}_total"};
426 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
430 <th align="right">$form->{"${item}_description"} | . $form->{"${item}_rate"} * 100 .qq|%</th>
431 <td align="right">$form->{"${item}_total"}</td>
437 $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
441 <th align="right">| . $locale->text('Subtotal') . qq|</th>
442 <td align="right">$form->{invsubtotal}</td>
448 if ($form->{taxincluded}) {
449 foreach my $item (split / /, $form->{taxaccounts}) {
450 if ($form->{"${item}_base"}) {
451 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"} / (1 + $form->{"${item}_rate"})), 2);
452 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
453 $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
454 $form->{"${item}_netto"} = $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
458 <th align="right">Enthaltene $form->{"${item}_description"} |
459 . $form->{"${item}_rate"} * 100 .qq|%</th>
460 <td align="right">$form->{"${item}_total"}</td>
463 <th align="right">Nettobetrag</th>
464 <td align="right">$form->{"${item}_netto"}</td>
472 $form->{oldinvtotal} = $form->{invtotal};
473 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
475 my $follow_ups_block;
477 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
479 if (@{ $follow_ups} ) {
480 my $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
481 $follow_ups_block = qq|
483 <td colspan="2">| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</td>
497 <th align="left">| . $locale->text('Notes') . qq|</th>
498 <th align="left">| . $locale->text('Internal Notes') . qq|</th>
499 <th align="right">| . $locale->text('Payment Terms') . qq|</th>
504 <td><select name="payment_id" onChange="if (this.value) set_duedate(['payment_id__' + this.value, 'invdate__' + invdate.value],['duedate'])">$payment
513 <th align=left>| . $locale->text('Ertrag') . qq|</th>
514 <td>| . $form->format_amount(\%myconfig, $form->{marge_total}, 2, 0) . qq|</td>
517 <th align=left>| . $locale->text('Ertrag prozentual') . qq|</th>
518 <td>| . $form->format_amount(\%myconfig, $form->{marge_percent}, 2, 0) . qq| %</td>
520 <input type=hidden name="marge_total" value="$form->{"marge_total"}">
521 <input type=hidden name="marge_percent" value="$form->{"marge_percent"}">
530 <th align="right">| . $locale->text('Total') . qq|</th>
531 <td align="right">$form->{invtotal}</td>
544 <td><hr size="3" noshade></td>
547 <th class="listtop" align="left">Dokumente im Webdav-Repository</th>
550 <td align="left" width="30%"><b>Dateiname</b></td>
551 <td align="left" width="70%"><b>Webdavlink</b></td>
553 foreach my $file (@{ $form->{WEBDAV} }) {
556 <td align="left">$file->{name}</td>
557 <td align="left"><a href="$file->{link}">$file->{type}</a></td>
568 if ($form->{type} eq "credit_note") {
573 <tr class="listheading">
574 <th colspan="6" class="listheading">|
575 . $locale->text('Payments') . qq|</th>
583 <tr class="listheading">
584 <th colspan="6" class="listheading">|
585 . $locale->text('Incoming Payments') . qq|</th>
591 if ($form->{currency} eq $form->{defaultcurrency}) {
592 @column_index = qw(datepaid source memo paid AR_paid);
594 @column_index = qw(datepaid source memo paid exchangerate AR_paid);
598 $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
599 $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
600 $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
601 $column_data{AR_paid} = "<th>" . $locale->text('Account') . "</th>";
602 $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
603 $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
608 map { print "$column_data{$_}\n" } @column_index;
616 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
617 for my $i (1 .. $form->{paidaccounts}) {
622 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
623 $form->{"selectAR_paid_$i"} =~
624 s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
627 $totalpaid += $form->{"paid_$i"};
628 if ($form->{"paid_$i"}) {
629 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
631 $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
633 if ($form->{"exchangerate_$i"} == 0) {
634 $form->{"exchangerate_$i"} = "";
636 my $exchangerate = qq| |;
637 if ($form->{currency} ne $form->{defaultcurrency}) {
638 if ($form->{"forex_$i"}) {
639 $exchangerate = qq|<input type="hidden" name="exchangerate_$i" value="$form->{"exchangerate_$i"}">$form->{"exchangerate_$i"}|;
641 $exchangerate = qq|<input name="exchangerate_$i" size="10" value="$form->{"exchangerate_$i"}">|;
645 $exchangerate .= qq|<input type="hidden" name="forex_$i" value="$form->{"forex_$i"}">|;
647 $column_data{"paid_$i"} =
648 qq|<td align="center"><input name="paid_$i" size="11" value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
649 $column_data{"exchangerate_$i"} = qq|<td align="center">$exchangerate</td>|;
650 $column_data{"AR_paid_$i"} =
651 qq|<td align="center"><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
652 $column_data{"datepaid_$i"} =
653 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)\">
654 <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
655 $column_data{"source_$i"} =
656 qq|<td align=center><input name="source_$i" size="11" value="$form->{"source_$i"}"></td>|;
657 $column_data{"memo_$i"} =
658 qq|<td align="center"><input name="memo_$i" size="11" value="$form->{"memo_$i"}"></td>|;
660 map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
663 push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
666 my $paid_missing = $form->{oldinvtotal} - $totalpaid;
672 <td align="center">| . $locale->text('Total') . qq|</td>
673 <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
678 <td align="center">| . $locale->text('Missing amount') . qq|</td>
679 <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
683 map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } qw(paidaccounts selectAR_paid oldinvtotal));
684 print qq|<input type="hidden" name="oldtotalpaid" value="$totalpaid">
689 <td><hr size="3" noshade></td>
703 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
704 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
707 my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && (($totalpaid == 0) || ($totalpaid eq ""));
710 <input class="submit" type="submit" accesskey="u" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
711 <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
712 <input class="submit" type="submit" name="action" value="| . $locale->text('Print') . qq|">
713 <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|"> |;
714 print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Storno') . qq|"> | if ($show_storno);
715 print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Post Payment') . qq|"> |;
716 print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Use As Template') . qq|"> |;
717 if ($form->{id} && !($form->{type} eq "credit_note")) {
718 print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Credit Note') . qq|"> |;
720 if ($form->{radier}) {
721 print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Delete') . qq|"> |;
725 if ($invdate > $closedto) {
726 print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Order') . qq|"> |;
729 print qq| <input type="button" class="submit" onclick="follow_up_window()" value="| . $locale->text('Follow-Up') . qq|">|;
732 if ($invdate > $closedto) {
734 <input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
735 <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
736 <input class="submit" type="submit" name="action" value="| . $locale->text('Preview') . qq|">
737 <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|">
738 <input class="submit" type="submit" name="action" value="| . $locale->text('Print and Post') . qq|">
739 <input class="submit" type="submit" name="action" value="| . $locale->text('Post') . qq|"> | .
740 NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
744 # button for saving history
745 if($form->{id} ne "") {
747 <input type="button" class="submit" onclick="set_history_window(|
749 . qq|);" name="history" id="history" value="|
750 . $locale->text('history')
753 # /button for saving history
755 # mark_as_paid button
756 if($form->{id} ne "") {
757 print qq|<input type="submit" class="submit" name="action" value="| . $locale->text('mark as paid') . qq|">|;
759 # /mark_as_paid button
760 print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
763 <input type="hidden" name="rowcount" value="$form->{rowcount}">
765 $cgi->hidden("-name" => "callback", "-value" => $form->{callback})
766 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
767 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
768 . $cgi->hidden('-name' => 'customer_discount', '-value' => [$form->{customer_discount}])
777 $main::lxdebug->leave_sub();
781 $main::lxdebug->enter_sub();
783 my $form = $main::form;
784 my %myconfig = %main::myconfig;
786 $main::auth->assert('invoice_edit');
788 &mark_as_paid_common(\%myconfig,"ar");
790 $main::lxdebug->leave_sub();
794 $main::lxdebug->enter_sub();
796 my $form = $main::form;
797 my %myconfig = %main::myconfig;
799 $main::auth->assert('invoice_edit');
801 my ($recursive_call) = shift;
803 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
805 $form->{print_and_post} = 0 if $form->{second_run};
806 my $taxincluded = "checked" if $form->{taxincluded};
809 &check_name("customer");
811 $form->{taxincluded} ||= $taxincluded;
813 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
814 $form->{exchangerate} = $form->{forex} if $form->{forex};
816 for my $i (1 .. $form->{paidaccounts}) {
817 next unless $form->{"paid_$i"};
818 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
819 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
820 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
823 my $i = $form->{rowcount};
824 my $exchangerate = $form->{exchangerate} || 1;
826 # if last row empty, check the form otherwise retrieve new item
827 if ( ($form->{"partnumber_$i"} eq "")
828 && ($form->{"description_$i"} eq "")
829 && ($form->{"partsgroup_$i"} eq "")) {
831 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
836 IS->retrieve_item(\%myconfig, \%$form);
838 my $rows = scalar @{ $form->{item_list} };
840 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
843 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
852 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
854 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
855 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
857 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
858 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
860 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
862 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
863 my $decimalplaces = max 2, length $1;
866 $form->{"sellprice_$i"} = $sellprice;
868 # if there is an exchange rate adjust sellprice
869 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
870 $form->{"sellprice_$i"} /= $exchangerate;
873 $form->{"listprice_$i"} /= $exchangerate;
875 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
876 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
877 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
878 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
880 $form->{creditremaining} -= $amount;
882 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
884 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
886 if ($main::lizenzen) {
887 if ($form->{"inventory_accno_$i"} ne "") {
888 $form->{"lizenzen_$i"} = qq|<option></option>|;
889 foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
890 $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
892 $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
896 # get pricegroups for parts
897 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
899 # build up html code for prices_$i
907 # ok, so this is a new part
908 # ask if it is a part or service item
910 if ( $form->{"partsgroup_$i"}
911 && ($form->{"partsnumber_$i"} eq "")
912 && ($form->{"description_$i"} eq "")) {
914 $form->{"discount_$i"} = "";
918 $form->{"id_$i"} = 0;
923 $main::lxdebug->leave_sub();
927 $main::lxdebug->enter_sub();
929 my $form = $main::form;
930 my %myconfig = %main::myconfig;
931 my $locale = $main::locale;
933 $main::auth->assert('invoice_edit');
937 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
938 for my $i (1 .. $form->{paidaccounts}) {
939 if ($form->{"paid_$i"}) {
940 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
942 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
944 $form->error($locale->text('Cannot post payment for a closed period!'))
945 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
947 if ($form->{currency} ne $form->{defaultcurrency}) {
948 $form->{"exchangerate_$i"} = $form->{exchangerate}
949 if ($invdate == $datepaid);
950 $form->isblank("exchangerate_$i",
951 $locale->text('Exchangerate for payment missing!'));
956 ($form->{AR}) = split /--/, $form->{AR};
957 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
959 $form->redirect($locale->text('Payment posted!'))
960 if (IS->post_payment(\%myconfig, \%$form));
961 $form->error($locale->text('Cannot post payment!'));
964 $main::lxdebug->leave_sub();
968 $main::lxdebug->enter_sub();
970 my $form = $main::form;
971 my %myconfig = %main::myconfig;
972 my $locale = $main::locale;
974 $main::auth->assert('invoice_edit');
976 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
977 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
978 $form->isblank("customer", $locale->text('Customer missing!'));
979 $form->error($locale->text('Cannot post invoice for a closed period!'))
980 if ($form->date_closed($form->{"invdate"}, \%myconfig));
982 $form->{invnumber} =~ s/^\s*//g;
983 $form->{invnumber} =~ s/\s*$//g;
985 # if oldcustomer ne customer redo form
986 if (&check_name('customer')) {
990 if ($form->{second_run}) {
991 $form->{print_and_post} = 0;
996 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
997 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
999 $form->error($locale->text('Cannot post invoice for a closed period!'))
1000 if ($invdate <= $closedto);
1002 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1003 if ($form->{currency} ne $form->{defaultcurrency});
1005 for my $i (1 .. $form->{paidaccounts}) {
1006 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1007 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1009 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1011 $form->error($locale->text('Cannot post payment for a closed period!'))
1012 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1014 if ($form->{currency} ne $form->{defaultcurrency}) {
1015 $form->{"exchangerate_$i"} = $form->{exchangerate}
1016 if ($invdate == $datepaid);
1017 $form->isblank("exchangerate_$i",
1018 $locale->text('Exchangerate for payment missing!'));
1023 ($form->{AR}) = split /--/, $form->{AR};
1024 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
1025 $form->{storno} ||= 0;
1027 $form->{label} = $locale->text('Invoice');
1029 $form->{id} = 0 if $form->{postasnew};
1031 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
1032 if ($form->{postasnew}) {
1033 if ($form->{type} eq "credit_note") {
1034 undef($form->{cnnumber});
1036 undef($form->{invnumber});
1041 $form->error($locale->text('Cannot post invoice!'))
1042 unless IS->post_invoice(\%myconfig, \%$form);
1043 remove_draft() if $form->{remove_draft};
1045 if(!exists $form->{addition}) {
1046 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1047 $form->{addition} = $print_post ? "PRINTED AND POSTED" :
1048 $form->{storno} ? "STORNO" :
1050 $form->save_history($form->dbconnect(\%myconfig));
1053 $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
1056 $main::lxdebug->leave_sub();
1059 sub print_and_post {
1060 $main::lxdebug->enter_sub();
1062 my $form = $main::form;
1064 $main::auth->assert('invoice_edit');
1066 my $old_form = new Form;
1068 $form->{print_and_post} = 1;
1072 $main::lxdebug->leave_sub();
1076 sub use_as_template {
1077 $main::lxdebug->enter_sub();
1079 my $form = $main::form;
1080 my %myconfig = %main::myconfig;
1082 $main::auth->assert('invoice_edit');
1084 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);
1085 $form->{paidaccounts} = 1;
1086 $form->{rowcount}--;
1087 $form->{invdate} = $form->current_date(\%myconfig);
1090 $main::lxdebug->leave_sub();
1094 $main::lxdebug->enter_sub();
1096 my $form = $main::form;
1097 my %myconfig = %main::myconfig;
1098 my $locale = $main::locale;
1100 $main::auth->assert('invoice_edit');
1102 if ($form->{storno}) {
1103 $form->error($locale->text('Cannot storno storno invoice!'));
1106 if (IS->has_storno(\%myconfig, $form, "ar")) {
1107 $form->error($locale->text("Invoice has already been storno'd!"));
1110 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
1116 # Payments must not be recorded for the new storno invoice.
1117 $form->{paidaccounts} = 0;
1118 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
1120 $form->{storno_id} = $form->{id};
1121 $form->{storno} = 1;
1123 $form->{invnumber} = "Storno zu " . $form->{invnumber};
1124 $form->{rowcount}++;
1127 $main::lxdebug->leave_sub();
1131 $main::lxdebug->enter_sub();
1133 my $form = $main::form;
1135 $main::auth->assert('invoice_edit');
1137 $form->{preview} = 1;
1138 my $old_form = new Form;
1139 for (keys %$form) { $old_form->{$_} = $form->{$_} }
1141 &print_form($old_form);
1142 $main::lxdebug->leave_sub();
1147 $main::lxdebug->enter_sub();
1149 my $form = $main::form;
1150 my $locale = $main::locale;
1152 $main::auth->assert('invoice_edit');
1154 if ($form->{second_run}) {
1155 $form->{print_and_post} = 0;
1162 <form method="post" action="$form->{script}">
1165 # delete action variable
1166 map { delete $form->{$_} } qw(action header);
1168 foreach my $key (keys %$form) {
1169 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1170 $form->{$key} =~ s/\"/"/g;
1171 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1175 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
1178 . $locale->text('Are you sure you want to delete Invoice Number')
1179 . qq| $form->{invnumber}
1183 <input name="action" class="submit" type="submit" value="|
1184 . $locale->text('Yes') . qq|">
1188 $main::lxdebug->leave_sub();
1192 $main::lxdebug->enter_sub();
1194 my $form = $main::form;
1195 my %myconfig = %main::myconfig;
1196 my $locale = $main::locale;
1198 $main::auth->assert('invoice_edit');
1200 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1202 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1205 $form->{rowcount}--;
1206 $form->{shipto} = 1;
1209 $form->{title} = $locale->text('Add Credit Note');
1210 $form->{script} = 'is.pl';
1212 our $buysell = 'buy';
1215 # bo creates the id, reset it
1216 map { delete $form->{$_} }
1217 qw(id invnumber subject message cc bcc printed emailed queued);
1218 $form->{ $form->{vc} } =~ s/--.*//g;
1219 $form->{type} = "credit_note";
1222 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
1224 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1225 qw(creditlimit creditremaining);
1227 my $currency = $form->{currency};
1230 $form->{currency} = $currency;
1231 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1232 $form->{exchangerate} = $form->{forex} || '';
1234 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1241 $main::lxdebug->leave_sub();
1245 $main::lxdebug->enter_sub();
1247 my $form = $main::form;
1248 my %myconfig = %main::myconfig;
1249 my $locale = $main::locale;
1251 $main::auth->assert('invoice_edit');
1253 if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
1254 # saving the history
1255 if(!exists $form->{addition}) {
1256 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1257 $form->{addition} = "DELETED";
1258 $form->save_history($form->dbconnect(\%myconfig));
1260 # /saving the history
1261 $form->redirect($locale->text('Invoice deleted!'));
1263 $form->error($locale->text('Cannot delete invoice!'));
1265 $main::lxdebug->leave_sub();
1269 $main::lxdebug->enter_sub();
1271 my $form = $main::form;
1273 $main::auth->assert('invoice_edit');
1278 my $saved_form = save_form();
1282 restore_form($saved_form, 0, qw(id invnumber));
1287 $main::lxdebug->leave_sub();