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} .= "<option>$_->{description}--$_->{id}</option>\n"
182 # } @{ $form->{all_departments} };
185 $form->{employee} = "$form->{employee}--$form->{employee_id}";
188 $form->{forex} = $form->{exchangerate};
189 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
191 foreach my $key (keys %{ $form->{AR_links} }) {
192 foreach my $ref (@{ $form->{AR_links}{$key} }) {
193 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}</option>\n";
196 if ($key eq "AR_paid") {
197 next unless $form->{acc_trans}{$key};
198 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
199 $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
202 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount} * -1;
203 $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{transdate};
204 $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
205 $form->{"forex_$i"} = $form->{"exchangerate_$i"};
206 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
207 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
209 $form->{paidaccounts} = $i;
212 $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
216 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
218 $form->{AR} = $form->{AR_1} unless $form->{id};
220 $form->{locked} = ($form->datetonum($form->{invdate}, \%myconfig)
221 <= $form->datetonum($form->{closedto}, \%myconfig));
223 $main::lxdebug->leave_sub();
226 sub prepare_invoice {
227 $main::lxdebug->enter_sub();
229 my $form = $main::form;
230 my %myconfig = %main::myconfig;
232 $main::auth->assert('invoice_edit');
234 if ($form->{type} eq "credit_note") {
235 $form->{type} = "credit_note";
236 $form->{formname} = "credit_note";
238 $form->{type} = "invoice";
239 $form->{formname} = "invoice";
246 foreach my $ref (@{ $form->{invoice_details} }) {
249 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
251 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
252 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
254 my $decimalplaces = ($dec > 2) ? $dec : 2;
256 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
257 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
258 $dec_qty = length $dec_qty;
260 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
262 $form->{rowcount} = $i;
266 # get pricegroups for parts
267 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
268 set_pricegroup($_) for 1 .. $form->{rowcount};
270 $main::lxdebug->leave_sub();
274 $main::lxdebug->enter_sub();
276 my $form = $main::form;
277 my %myconfig = %main::myconfig;
278 my $locale = $main::locale;
279 my $cgi = $main::cgi;
281 $main::auth->assert('invoice_edit');
286 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
287 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
289 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
290 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
292 $payment = qq|<option value=""></option>|;
293 foreach my $item (@{ $form->{payment_terms} }) {
294 if ($form->{payment_id} eq $item->{id}) {
295 $payment .= qq|<option value="$item->{id}" selected>$item->{description}</option>|;
297 $payment .= qq|<option value="$item->{id}">$item->{description}</option>|;
301 my $set_duedate_url = "$form->{script}?action=set_duedate";
303 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
305 my @old_project_ids = ($form->{"globalproject_id"});
306 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
308 $form->get_lists("contacts" => "ALL_CONTACTS",
309 "shipto" => "ALL_SHIPTO",
310 "projects" => { "key" => "ALL_PROJECTS",
312 "old_id" => \@old_project_ids },
313 "employees" => "ALL_EMPLOYEES",
314 "salesmen" => "ALL_SALESMEN",
315 "taxzones" => "ALL_TAXZONES",
316 "currencies" => "ALL_CURRENCIES",
317 "customers" => "ALL_CUSTOMERS",
318 "price_factors" => "ALL_PRICE_FACTORS");
320 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
321 $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
322 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
323 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
326 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
327 $TMPL_VAR{vclimit} = $myconfig{vclimit};
328 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('customer', '', 0, 0)";
329 push @custom_hiddens, "customer_id";
330 push @custom_hiddens, "oldcustomer";
331 push @custom_hiddens, "selectcustomer";
333 # currencies and exchangerate
334 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
335 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
336 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
337 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
338 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
339 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
340 push @custom_hiddens, "forex";
341 push @custom_hiddens, "exchangerate" if $form->{forex};
343 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
344 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
346 $form->{fokus} = "invoice.customer";
348 my $follow_up_vc = $form->{customer};
349 $follow_up_vc =~ s/--\d*\s*$//;
350 $TMPL_VAR{customer_name} = $follow_up_vc;
352 # set option selected
353 foreach my $item (qw(AR)) {
354 $form->{"select$item"} =~ s/ selected//;
355 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
358 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
359 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
360 $TMPL_VAR{dateformat} = $myconfig{dateformat};
361 $TMPL_VAR{numberformat} = $myconfig{numberformat};
364 $TMPL_VAR{HIDDENS} = [qw(
365 id action type media format queued printed emailed title vc discount
366 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
367 max_dunning_level dunning_amount
368 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
369 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
370 convert_from_do_ids convert_from_oe_ids
372 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
374 $form->{jsscript} = 1;
377 print $form->parse_html_template("is/form_header", \%TMPL_VAR);
379 $main::lxdebug->leave_sub();
383 $main::lxdebug->enter_sub();
385 my $form = $main::form;
386 my %myconfig = %main::myconfig;
387 my $locale = $main::locale;
388 my $cgi = $main::cgi;
390 $main::auth->assert('invoice_edit');
392 $form->{invtotal} = $form->{invsubtotal};
394 my ($rows, $introws);
395 if (($rows = $form->numtextrows($form->{notes}, 26, 8)) < 2) { $rows = 2; }
396 if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) { $introws = 2; }
397 $form->{rows} = ($rows > $introws) ? $rows : $introws;
399 my ($tax, $subtotal);
400 $form->{taxaccounts_array} = [ split / /, $form->{taxaccounts} ];
402 foreach my $item (@{ $form->{taxaccounts_array} }) {
403 if ($form->{"${item}_base"}) {
404 if ($form->{taxincluded}) {
405 $form->{"${item}_total"} = $form->round_amount( ($form->{"${item}_base"} * $form->{"${item}_rate"}
406 / (1 + $form->{"${item}_rate"})), 2);
407 $form->{"${item}_netto"} = $form->round_amount( ($form->{"${item}_base"} - $form->{"${item}_total"}), 2);
409 $form->{"${item}_total"} = $form->round_amount( $form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
410 $form->{invtotal} += $form->{"${item}_total"};
415 $form->{oldinvtotal} = $form->{invtotal};
417 # unfortunately locales doesn't support extended syntax
419 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
420 if (@{ $follow_ups} ) {
421 $form->{follow_up_text} = $locale->text("There are #1 unfinished follow-ups of which #2 are due.",
422 scalar(@{ $follow_ups }),
423 sum map { $_->{due} * 1 } @{ $follow_ups });
431 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
432 $form->{paid_indices} = [ 1 .. $form->{paidaccounts} ];
434 for my $i (1 .. $form->{paidaccounts}) {
435 $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
436 $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
439 $totalpaid += $form->{"paid_$i"};
440 if ($form->{"paid_$i"}) {
441 $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
443 $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
444 $form->{"exchangerate_$i"} ||= "";
448 $form->{print_options} = print_options(inline => 1);
450 print $form->parse_html_template('is/form_footer');
451 # print $form->parse_html_template('is/_payments'); # parser
458 # <table width="100%">
459 # <tr valign="bottom">
463 # <th align="left">| . $locale->text('Notes') . qq|</th>
464 # <th align="left">| . $locale->text('Internal Notes') . qq|</th>
465 # <th align="right">| . $locale->text('Payment Terms') . qq|</th>
470 # <td><select name="payment_id" onChange="if (this.value) set_duedate(['payment_id__' + this.value, 'invdate__' + invdate.value],['duedate'])">$payment
479 # <th align=left>| . $locale->text('Ertrag') . qq|</th>
480 # <td>| . $form->format_amount(\%myconfig, $form->{marge_total}, 2, 0) . qq|</td>
483 # <th align=left>| . $locale->text('Ertrag prozentual') . qq|</th>
484 # <td>| . $form->format_amount(\%myconfig, $form->{marge_percent}, 2, 0) . qq| %</td>
486 # <input type=hidden name="marge_total" value="$form->{"marge_total"}">
487 # <input type=hidden name="marge_percent" value="$form->{"marge_percent"}">
496 # <th align="right">| . $locale->text('Total') . qq|</th>
497 # <td align="right">$form->{invtotal}</td>
507 # if ($main::webdav) {
510 # <td><hr size="3" noshade></td>
513 # <th class="listtop" align="left">Dokumente im Webdav-Repository</th>
515 # <table width="100%">
516 # <td align="left" width="30%"><b>Dateiname</b></td>
517 # <td align="left" width="70%"><b>Webdavlink</b></td>
519 # foreach my $file (@{ $form->{WEBDAV} }) {
520 # $webdav_list .= qq|
522 # <td align="left">$file->{name}</td>
523 # <td align="left"><a href="$file->{link}">$file->{type}</a></td>
527 # $webdav_list .= qq|
532 # print $webdav_list;
534 #if ($form->{type} eq "credit_note") {
538 # <table width="100%">
539 # <tr class="listheading">
540 # <th colspan="6" class="listheading">|
541 # . $locale->text('Payments') . qq|</th>
548 # <table width="100%">
549 # <tr class="listheading">
550 # <th colspan="6" class="listheading">|
551 # . $locale->text('Incoming Payments') . qq|</th>
557 # if ($form->{currency} eq $form->{defaultcurrency}) {
558 # @column_index = qw(datepaid source memo paid AR_paid);
560 # @column_index = qw(datepaid source memo paid exchangerate AR_paid);
564 # $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
565 # $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
566 # $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
567 # $column_data{AR_paid} = "<th>" . $locale->text('Account') . "</th>";
568 # $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
569 # $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
574 # map { print "$column_data{$_}\n" } @column_index;
582 # $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
583 # for my $i (1 .. $form->{paidaccounts}) {
588 # $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
589 # $form->{"selectAR_paid_$i"} =~
590 # s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
593 # $totalpaid += $form->{"paid_$i"};
594 # if ($form->{"paid_$i"}) {
595 # $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
597 # $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
599 # if ($form->{"exchangerate_$i"} == 0) {
600 # $form->{"exchangerate_$i"} = "";
602 # my $exchangerate = qq| |;
603 # if ($form->{currency} ne $form->{defaultcurrency}) {
604 # if ($form->{"forex_$i"}) {
605 # $exchangerate = qq|<input type="hidden" name="exchangerate_$i" value="$form->{"exchangerate_$i"}">$form->{"exchangerate_$i"}|;
607 # $exchangerate = qq|<input name="exchangerate_$i" size="10" value="$form->{"exchangerate_$i"}">|;
611 # $exchangerate .= qq|<input type="hidden" name="forex_$i" value="$form->{"forex_$i"}">|;
613 # $column_data{"paid_$i"} =
614 # qq|<td align="center"><input name="paid_$i" size="11" value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
615 # $column_data{"exchangerate_$i"} = qq|<td align="center">$exchangerate</td>|;
616 # $column_data{"AR_paid_$i"} =
617 # qq|<td align="center"><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
618 # $column_data{"datepaid_$i"} =
619 # 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)\">
620 # <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
621 # $column_data{"source_$i"} =
622 # qq|<td align=center><input name="source_$i" size="11" value="$form->{"source_$i"}"></td>|;
623 # $column_data{"memo_$i"} =
624 # qq|<td align="center"><input name="memo_$i" size="11" value="$form->{"memo_$i"}"></td>|;
626 # map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
629 # push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
632 # my $paid_missing = $form->{oldinvtotal} - $totalpaid;
638 # <td align="center">| . $locale->text('Total') . qq|</td>
639 # <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
644 # <td align="center">| . $locale->text('Missing amount') . qq|</td>
645 # <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
649 # map({ print($cgi->hidden("-name" => $_, "-value" => $form->{$_})); } qw(paidaccounts selectAR_paid oldinvtotal));
650 # print qq|<input type="hidden" name="oldtotalpaid" value="$totalpaid">
655 # <td><hr size="3" noshade></td>
669 # my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
670 # my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
673 # my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ar") && (($totalpaid == 0) || ($totalpaid eq ""));
676 # <input class="submit" type="submit" accesskey="u" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
677 # <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
678 # <input class="submit" type="submit" name="action" value="| . $locale->text('Print') . qq|">
679 # <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|"> |;
680 # print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Storno') . qq|"> | if ($show_storno);
681 # print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Post Payment') . qq|"> |;
682 # print qq|<input class="submit" type="submit" name="action" value="| . $locale->text('Use As Template') . qq|"> |;
683 # if ($form->{id} && !($form->{type} eq "credit_note")) {
684 # print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Credit Note') . qq|"> |;
686 # if ($form->{radier}) {
687 # print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Delete') . qq|"> |;
691 # if ($invdate > $closedto) {
692 # print qq| <input class="submit" type="submit" name="action" value="| . $locale->text('Order') . qq|"> |;
695 # print qq| <input type="button" class="submit" onclick="follow_up_window()" value="| . $locale->text('Follow-Up') . qq|">|;
698 # if ($invdate > $closedto) {
700 # <input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">
701 # <input class="submit" type="submit" name="action" value="| . $locale->text('Ship to') . qq|">
702 # <input class="submit" type="submit" name="action" value="| . $locale->text('Preview') . qq|">
703 # <input class="submit" type="submit" name="action" value="| . $locale->text('E-mail') . qq|">
704 # <input class="submit" type="submit" name="action" value="| . $locale->text('Print and Post') . qq|">
705 # <input class="submit" type="submit" name="action" value="| . $locale->text('Post') . qq|"> | .
706 # NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
710 # # button for saving history
711 # if($form->{id} ne "") {
713 # <input type="button" class="submit" onclick="set_history_window(|
715 # . qq|);" name="history" id="history" value="|
716 # . $locale->text('history')
719 # # /button for saving history
721 # # mark_as_paid button
722 # if($form->{id} ne "") {
723 # print qq|<input type="submit" class="submit" name="action" value="| . $locale->text('mark as paid') . qq|">|;
725 # # /mark_as_paid button
726 # print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
729 #<input type="hidden" name="rowcount" value="$form->{rowcount}">
731 #$cgi->hidden("-name" => "callback", "-value" => $form->{callback})
732 #. $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
733 #. $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
734 #. $cgi->hidden('-name' => 'customer_discount', '-value' => [$form->{customer_discount}])
743 $main::lxdebug->leave_sub();
747 $main::lxdebug->enter_sub();
749 my $form = $main::form;
750 my %myconfig = %main::myconfig;
752 $main::auth->assert('invoice_edit');
754 &mark_as_paid_common(\%myconfig,"ar");
756 $main::lxdebug->leave_sub();
760 $main::lxdebug->enter_sub();
762 my $form = $main::form;
763 my %myconfig = %main::myconfig;
765 $main::auth->assert('invoice_edit');
767 my ($recursive_call) = shift;
769 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining) unless $recursive_call;
771 $form->{print_and_post} = 0 if $form->{second_run};
772 my $taxincluded = "checked" if $form->{taxincluded};
775 &check_name("customer");
777 $form->{taxincluded} ||= $taxincluded;
779 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy');
780 $form->{exchangerate} = $form->{forex} if $form->{forex};
782 for my $i (1 .. $form->{paidaccounts}) {
783 next unless $form->{"paid_$i"};
784 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
785 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy');
786 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
789 my $i = $form->{rowcount};
790 my $exchangerate = $form->{exchangerate} || 1;
792 # if last row empty, check the form otherwise retrieve new item
793 if ( ($form->{"partnumber_$i"} eq "")
794 && ($form->{"description_$i"} eq "")
795 && ($form->{"partsgroup_$i"} eq "")) {
797 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
802 IS->retrieve_item(\%myconfig, \%$form);
804 my $rows = scalar @{ $form->{item_list} };
806 $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{customer_discount} * 100);
809 $form->{"qty_$i"} = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
818 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
820 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
821 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
823 $form->{payment_id} = $form->{"part_payment_id_$i"} if $form->{"part_payment_id_$i"} ne "";
824 $form->{"discount_$i"} = 0 if $form->{"not_discountable_$i"};
826 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
828 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
829 my $decimalplaces = max 2, length $1;
832 $form->{"sellprice_$i"} = $sellprice;
834 # if there is an exchange rate adjust sellprice
835 $form->{"sellprice_$i"} *= (1 - $form->{tradediscount});
836 $form->{"sellprice_$i"} /= $exchangerate;
839 $form->{"listprice_$i"} /= $exchangerate;
841 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
842 map { $form->{"${_}_base"} = 0 } split / /, $form->{taxaccounts};
843 map { $form->{"${_}_base"} += $amount } split / /, $form->{"taxaccounts_$i"};
844 map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
846 $form->{creditremaining} -= $amount;
848 map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
850 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
852 if ($main::lizenzen) {
853 if ($form->{"inventory_accno_$i"} ne "") {
854 $form->{"lizenzen_$i"} = qq|<option></option>|;
855 foreach my $item (@{ $form->{LIZENZEN}{ $form->{"id_$i"} } }) {
856 $form->{"lizenzen_$i"} .= qq|<option value="$item->{"id"}">$item->{"licensenumber"}</option>|;
858 $form->{"lizenzen_$i"} .= qq|<option value=-1>Neue Lizenz</option>|;
862 # get pricegroups for parts
863 IS->get_pricegroups_for_parts(\%myconfig, \%$form);
865 # build up html code for prices_$i
873 # ok, so this is a new part
874 # ask if it is a part or service item
876 if ( $form->{"partsgroup_$i"}
877 && ($form->{"partsnumber_$i"} eq "")
878 && ($form->{"description_$i"} eq "")) {
880 $form->{"discount_$i"} = "";
884 $form->{"id_$i"} = 0;
889 $main::lxdebug->leave_sub();
893 $main::lxdebug->enter_sub();
895 my $form = $main::form;
896 my %myconfig = %main::myconfig;
897 my $locale = $main::locale;
899 $main::auth->assert('invoice_edit');
903 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
904 for my $i (1 .. $form->{paidaccounts}) {
905 if ($form->{"paid_$i"}) {
906 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
908 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
910 $form->error($locale->text('Cannot post payment for a closed period!'))
911 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
913 if ($form->{currency} ne $form->{defaultcurrency}) {
914 $form->{"exchangerate_$i"} = $form->{exchangerate}
915 if ($invdate == $datepaid);
916 $form->isblank("exchangerate_$i",
917 $locale->text('Exchangerate for payment missing!'));
922 ($form->{AR}) = split /--/, $form->{AR};
923 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
925 $form->redirect($locale->text('Payment posted!'))
926 if (IS->post_payment(\%myconfig, \%$form));
927 $form->error($locale->text('Cannot post payment!'));
930 $main::lxdebug->leave_sub();
934 $main::lxdebug->enter_sub();
936 my $form = $main::form;
937 my %myconfig = %main::myconfig;
938 my $locale = $main::locale;
940 $main::auth->assert('invoice_edit');
942 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
943 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
944 $form->isblank("customer", $locale->text('Customer missing!'));
945 $form->error($locale->text('Cannot post invoice for a closed period!'))
946 if ($form->date_closed($form->{"invdate"}, \%myconfig));
948 $form->{invnumber} =~ s/^\s*//g;
949 $form->{invnumber} =~ s/\s*$//g;
951 # if oldcustomer ne customer redo form
952 if (&check_name('customer')) {
956 if ($form->{second_run}) {
957 $form->{print_and_post} = 0;
962 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
963 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
965 $form->error($locale->text('Cannot post invoice for a closed period!'))
966 if ($invdate <= $closedto);
968 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
969 if ($form->{currency} ne $form->{defaultcurrency});
971 for my $i (1 .. $form->{paidaccounts}) {
972 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
973 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
975 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
977 $form->error($locale->text('Cannot post payment for a closed period!'))
978 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
980 if ($form->{currency} ne $form->{defaultcurrency}) {
981 $form->{"exchangerate_$i"} = $form->{exchangerate}
982 if ($invdate == $datepaid);
983 $form->isblank("exchangerate_$i",
984 $locale->text('Exchangerate for payment missing!'));
989 ($form->{AR}) = split /--/, $form->{AR};
990 ($form->{AR_paid}) = split /--/, $form->{AR_paid};
991 $form->{storno} ||= 0;
993 $form->{label} = $locale->text('Invoice');
995 $form->{id} = 0 if $form->{postasnew};
997 # get new invnumber in sequence if no invnumber is given or if posasnew was requested
998 if ($form->{postasnew}) {
999 if ($form->{type} eq "credit_note") {
1000 undef($form->{cnnumber});
1002 undef($form->{invnumber});
1007 $form->error($locale->text('Cannot post invoice!'))
1008 unless IS->post_invoice(\%myconfig, \%$form);
1009 remove_draft() if $form->{remove_draft};
1011 if(!exists $form->{addition}) {
1012 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1013 $form->{addition} = $print_post ? "PRINTED AND POSTED" :
1014 $form->{storno} ? "STORNO" :
1016 $form->save_history($form->dbconnect(\%myconfig));
1019 $form->redirect( $form->{label} . " $form->{invnumber} " . $locale->text('posted!'))
1022 $main::lxdebug->leave_sub();
1025 sub print_and_post {
1026 $main::lxdebug->enter_sub();
1028 my $form = $main::form;
1030 $main::auth->assert('invoice_edit');
1032 my $old_form = new Form;
1034 $form->{print_and_post} = 1;
1038 $main::lxdebug->leave_sub();
1042 sub use_as_template {
1043 $main::lxdebug->enter_sub();
1045 my $form = $main::form;
1046 my %myconfig = %main::myconfig;
1048 $main::auth->assert('invoice_edit');
1050 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);
1051 $form->{paidaccounts} = 1;
1052 $form->{rowcount}--;
1053 $form->{invdate} = $form->current_date(\%myconfig);
1056 $main::lxdebug->leave_sub();
1060 $main::lxdebug->enter_sub();
1062 my $form = $main::form;
1063 my %myconfig = %main::myconfig;
1064 my $locale = $main::locale;
1066 $main::auth->assert('invoice_edit');
1068 if ($form->{storno}) {
1069 $form->error($locale->text('Cannot storno storno invoice!'));
1072 if (IS->has_storno(\%myconfig, $form, "ar")) {
1073 $form->error($locale->text("Invoice has already been storno'd!"));
1076 map({ my $key = $_; delete($form->{$key}) unless (grep({ $key eq $_ } qw(id login password stylesheet type))); } keys(%{ $form }));
1082 # Payments must not be recorded for the new storno invoice.
1083 $form->{paidaccounts} = 0;
1084 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
1086 $form->{storno_id} = $form->{id};
1087 $form->{storno} = 1;
1089 $form->{invnumber} = "Storno zu " . $form->{invnumber};
1090 $form->{rowcount}++;
1093 $main::lxdebug->leave_sub();
1097 $main::lxdebug->enter_sub();
1099 my $form = $main::form;
1101 $main::auth->assert('invoice_edit');
1103 $form->{preview} = 1;
1104 my $old_form = new Form;
1105 for (keys %$form) { $old_form->{$_} = $form->{$_} }
1107 &print_form($old_form);
1108 $main::lxdebug->leave_sub();
1113 $main::lxdebug->enter_sub();
1115 my $form = $main::form;
1116 my $locale = $main::locale;
1118 $main::auth->assert('invoice_edit');
1120 if ($form->{second_run}) {
1121 $form->{print_and_post} = 0;
1128 <form method="post" action="$form->{script}">
1131 # delete action variable
1132 map { delete $form->{$_} } qw(action header);
1134 foreach my $key (keys %$form) {
1135 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1136 $form->{$key} =~ s/\"/"/g;
1137 print qq|<input type="hidden" name="$key" value="$form->{$key}">\n|;
1141 <h2 class="confirm">| . $locale->text('Confirm!') . qq|</h2>
1144 . $locale->text('Are you sure you want to delete Invoice Number')
1145 . qq| $form->{invnumber}
1149 <input name="action" class="submit" type="submit" value="|
1150 . $locale->text('Yes') . qq|">
1154 $main::lxdebug->leave_sub();
1158 $main::lxdebug->enter_sub();
1160 my $form = $main::form;
1161 my %myconfig = %main::myconfig;
1162 my $locale = $main::locale;
1164 $main::auth->assert('invoice_edit');
1166 $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1168 $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1171 $form->{rowcount}--;
1172 $form->{shipto} = 1;
1175 $form->{title} = $locale->text('Add Credit Note');
1176 $form->{script} = 'is.pl';
1178 our $buysell = 'buy';
1181 # bo creates the id, reset it
1182 map { delete $form->{$_} }
1183 qw(id invnumber subject message cc bcc printed emailed queued);
1184 $form->{ $form->{vc} } =~ s/--.*//g;
1185 $form->{type} = "credit_note";
1188 map { $form->{"select$_"} = "" } ($form->{vc}, 'currency');
1190 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
1191 qw(creditlimit creditremaining);
1193 my $currency = $form->{currency};
1196 $form->{currency} = $currency;
1197 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{invdate}, $buysell);
1198 $form->{exchangerate} = $form->{forex} || '';
1200 $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1207 $main::lxdebug->leave_sub();
1211 $main::lxdebug->enter_sub();
1213 my $form = $main::form;
1214 my %myconfig = %main::myconfig;
1215 my $locale = $main::locale;
1217 $main::auth->assert('invoice_edit');
1219 if (IS->delete_invoice(\%myconfig, \%$form, $main::spool)) {
1220 # saving the history
1221 if(!exists $form->{addition}) {
1222 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1223 $form->{addition} = "DELETED";
1224 $form->save_history($form->dbconnect(\%myconfig));
1226 # /saving the history
1227 $form->redirect($locale->text('Invoice deleted!'));
1229 $form->error($locale->text('Cannot delete invoice!'));
1231 $main::lxdebug->leave_sub();
1235 $main::lxdebug->enter_sub();
1237 my $form = $main::form;
1239 $main::auth->assert('invoice_edit');
1244 my $saved_form = save_form();
1248 restore_form($saved_form, 0, qw(id invnumber));
1253 $main::lxdebug->leave_sub();