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 received 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/common.pl";
44 require "bin/mozilla/drafts.pl";
53 $main::lxdebug->enter_sub();
55 my $form = $main::form;
56 my $locale = $main::locale;
58 $main::auth->assert('vendor_invoice_edit');
60 return $main::lxdebug->leave_sub() if (load_draft_maybe());
62 $form->{title} = $locale->text('Add Vendor Invoice');
68 $main::lxdebug->leave_sub();
72 $main::lxdebug->enter_sub();
74 my $form = $main::form;
75 my $locale = $main::locale;
77 $main::auth->assert('vendor_invoice_edit');
80 $form->{javascript} = qq|<script type=text/javascript src=js/show_history.js></script>|;
81 #/show hhistory button
83 $form->{title} = $locale->text('Edit Vendor Invoice');
89 $main::lxdebug->leave_sub();
93 $main::lxdebug->enter_sub();
95 my $form = $main::form;
96 my %myconfig = %main::myconfig;
98 $main::auth->assert('vendor_invoice_edit');
100 $form->{vc} = 'vendor';
103 $form->{webdav} = $main::webdav;
104 $form->{jsscript} = 1;
106 $form->create_links("AP", \%myconfig, "vendor");
108 #quote all_vendor Bug 133
109 foreach my $ref (@{ $form->{all_vendor} }) {
110 $ref->{name} = $form->quote($ref->{name});
113 if ($form->{all_vendor}) {
114 unless ($form->{vendor_id}) {
115 $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
119 my ($payment_id, $language_id, $taxzone_id);
120 if ($form->{payment_id}) {
121 $payment_id = $form->{payment_id};
123 if ($form->{language_id}) {
124 $language_id = $form->{language_id};
126 if ($form->{taxzone_id}) {
127 $taxzone_id = $form->{taxzone_id};
130 my $cp_id = $form->{cp_id};
131 IR->get_vendor(\%myconfig, \%$form);
132 IR->retrieve_invoice(\%myconfig, \%$form);
133 $form->{cp_id} = $cp_id;
136 $form->{payment_id} = $payment_id;
139 $form->{language_id} = $language_id;
142 $form->{taxzone_id} = $taxzone_id;
145 my @curr = split(/:/, $form->{currencies}); #seems to be missing
146 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
148 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
150 # build vendor/customer drop down comatibility... don't ask
151 if (@{ $form->{"all_vendor"} || [] }) {
152 $form->{"selectvendor"} = 1;
153 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
157 if ($form->{all_departments}) {
158 $form->{selectdepartment} = "<option>\n";
159 $form->{department} = "$form->{department}--$form->{department_id}";
162 $form->{selectdepartment} .=
163 "<option>$_->{description}--$_->{id}\n"
164 } (@{ $form->{all_departments} || [] });
168 $form->{forex} = $form->{exchangerate};
169 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
171 foreach my $key (keys %{ $form->{AP_links} }) {
173 foreach my $ref (@{ $form->{AP_links}{$key} }) {
174 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
177 next unless $form->{acc_trans}{$key};
179 if ($key eq "AP_paid") {
180 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
181 $form->{"AP_paid_$i"} =
182 "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
185 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
186 $form->{"datepaid_$i"} =
187 $form->{acc_trans}{$key}->[$i - 1]->{transdate};
188 $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
189 $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
190 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
191 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
193 $form->{paidaccounts} = $i;
197 "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
202 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
204 $form->{AP} = $form->{AP_1} unless $form->{id};
207 ($form->datetonum($form->{invdate}, \%myconfig) <=
208 $form->datetonum($form->{closedto}, \%myconfig));
210 $main::lxdebug->leave_sub();
213 sub prepare_invoice {
214 $main::lxdebug->enter_sub();
216 my $form = $main::form;
217 my %myconfig = %main::myconfig;
219 $main::auth->assert('vendor_invoice_edit');
223 map { $form->{$_} =~ s/\"/"/g } qw(invnumber ordnumber quonumber);
226 foreach my $ref (@{ $form->{invoice_details} }) {
228 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
230 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
232 my $decimalplaces = ($dec > 2) ? $dec : 2;
234 $form->{"sellprice_$i"} =
235 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
238 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
239 $dec_qty = length $dec_qty;
242 $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
244 $form->{rowcount} = $i;
248 $main::lxdebug->leave_sub();
252 $main::lxdebug->enter_sub();
254 my $form = $main::form;
255 my %myconfig = %main::myconfig;
256 my $locale = $main::locale;
257 my $cgi = $main::cgi;
259 $main::auth->assert('invoice_edit');
264 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
265 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
267 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
268 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
270 my $set_duedate_url = "$form->{script}?action=set_duedate";
272 push @ { $form->{AJAX} }, new CGI::Ajax( 'set_duedate' => $set_duedate_url );
274 my @old_project_ids = ($form->{"globalproject_id"});
275 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
277 $form->get_lists("contacts" => "ALL_CONTACTS",
278 "shipto" => "ALL_SHIPTO",
279 "projects" => { "key" => "ALL_PROJECTS",
281 "old_id" => \@old_project_ids },
282 "employees" => "ALL_EMPLOYEES",
283 "taxzones" => "ALL_TAXZONES",
284 "currencies" => "ALL_CURRENCIES",
285 "vendors" => "ALL_VENDORS",
286 "price_factors" => "ALL_PRICE_FACTORS");
288 $TMPL_VAR{sales_employee_labels} = sub { $_[0]->{name} || $_[0]->{login} };
289 # $TMPL_VAR{shipto_labels} = sub { join "; ", grep { $_ } map { $_[0]->{"shipto${_}" } } qw(name department_1 street city) };
290 $TMPL_VAR{contact_labels} = sub { join(', ', $_[0]->{"cp_name"}, $_[0]->{"cp_givenname"}) . ($_[0]->{cp_abteilung} ? " ($_[0]->{cp_abteilung})" : "") };
291 $TMPL_VAR{department_labels} = sub { "$_[0]->{description}--$_[0]->{id}" };
294 $TMPL_VAR{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
295 $TMPL_VAR{vclimit} = $myconfig{vclimit};
296 $TMPL_VAR{vc_select} = "customer_or_vendor_selection_window('vendor', '', 1, 0)";
297 push @custom_hiddens, "vendor_id";
298 push @custom_hiddens, "oldvendor";
299 push @custom_hiddens, "selectvendor";
301 # currencies and exchangerate
302 my @values = map { $_ } @{ $form->{ALL_CURRENCIES} };
303 my %labels = map { $_ => $_ } @{ $form->{ALL_CURRENCIES} };
304 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
305 $TMPL_VAR{show_exchangerate} = $form->{currency} ne $form->{defaultcurrency};
306 $TMPL_VAR{currencies} = NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
307 '-values' => \@values, '-labels' => \%labels)) if scalar @values;
308 push @custom_hiddens, "forex";
309 push @custom_hiddens, "exchangerate" if $form->{forex};
311 $TMPL_VAR{creditwarning} = ($form->{creditlimit} != 0) && ($form->{creditremaining} < 0) && !$form->{update};
312 $TMPL_VAR{is_credit_remaining_negativ} = $form->{creditremaining} =~ /-/;
314 $form->{fokus} = "invoice.vendor";
316 my $follow_up_vc = $form->{vendor};
317 $follow_up_vc =~ s/--\d*\s*$//;
318 $TMPL_VAR{vendor_name} = $follow_up_vc;
320 # set option selected
321 foreach my $item (qw(AP)) {
322 $form->{"select$item"} =~ s/ selected//;
323 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
326 $TMPL_VAR{is_type_credit_note} = $form->{type} eq "credit_note";
327 $TMPL_VAR{is_format_html} = $form->{format} eq 'html';
328 $TMPL_VAR{dateformat} = $myconfig{dateformat};
329 $TMPL_VAR{numberformat} = $myconfig{numberformat};
332 $TMPL_VAR{HIDDENS} = [qw(
333 id action type media format queued printed emailed title vc discount
334 title creditlimit creditremaining tradediscount business closedto locked shipped storno storno_id
335 max_dunning_level dunning_amount
336 shiptoname shiptostreet shiptozipcode shiptocity shiptocountry shiptocontact shiptophone shiptofax
337 shiptoemail shiptodepartment_1 shiptodepartment_2 message email subject cc bcc taxaccounts cursor_fokus
338 convert_from_do_ids convert_from_oe_ids
340 map { $_.'_rate', $_.'_description', $_.'_taxnumber' } split / /, $form->{taxaccounts}];
342 $form->{jsscript} = 1;
345 print $form->parse_html_template("ir/form_header", \%TMPL_VAR);
347 $main::lxdebug->leave_sub();
351 $main::lxdebug->enter_sub();
353 my $form = $main::form;
354 my %myconfig = %main::myconfig;
355 my $locale = $main::locale;
356 my $cgi = $main::cgi;
358 $main::auth->assert('vendor_invoice_edit');
360 $form->{invtotal} = $form->{invsubtotal};
362 my ($rows, $introws);
363 if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
366 if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
369 $rows = ($rows > $introws) ? $rows : $introws;
371 qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
373 qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
375 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
377 my $taxincluded = "";
378 if ($form->{taxaccounts}) {
380 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
381 . $locale->text('Tax Included') . qq|</b>
385 my ($tax, $subtotal);
386 if (!$form->{taxincluded}) {
388 foreach my $item (split / /, $form->{taxaccounts}) {
389 if ($form->{"${item}_base"}) {
390 $form->{invtotal} += $form->{"${item}_total"} =
392 $form->{"${item}_base"} * $form->{"${item}_rate"},
394 $form->{"${item}_total"} =
395 $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
399 <th align=right>$form->{"${item}_description"} |
400 . $form->{"${item}_rate"} * 100 .qq|%</th>
401 <td align=right>$form->{"${item}_total"}</td>
407 $form->{invsubtotal} =
408 $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
412 <th align=right>| . $locale->text('Subtotal') . qq|</th>
413 <td align=right>$form->{invsubtotal}</td>
419 if ($form->{taxincluded}) {
420 foreach my $item (split / /, $form->{taxaccounts}) {
421 if ($form->{"${item}_base"}) {
422 $form->{"${item}_total"} =
424 ($form->{"${item}_base"} * $form->{"${item}_rate"} /
425 (1 + $form->{"${item}_rate"})
428 $form->{"${item}_base"} =
429 $form->round_amount($form->{"${item}_base"}, 2);
430 $form->{"${item}_netto"} =
432 ($form->{"${item}_base"} - $form->{"${item}_total"}),
434 $form->{"${item}_netto"} =
435 $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
436 $form->{"${item}_total"} =
437 $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
441 <th align=right>Enthaltene $form->{"${item}_description"} |
442 . $form->{"${item}_rate"} * 100 .qq|%</th>
443 <td align=right>$form->{"${item}_total"}</td>
446 <th align=right>Nettobetrag</th>
447 <td align=right>$form->{"${item}_netto"}</td>
455 $form->{oldinvtotal} = $form->{invtotal};
457 $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
459 my $follow_ups_block;
461 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
463 if (@{ $follow_ups} ) {
464 my $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
465 $follow_ups_block = qq|
467 <td colspan="2">| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</td>
476 <td colspan=$colspan>
477 <table cellspacing="0">
482 <th align=left>| . $locale->text('Notes') . qq|</th>
483 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
492 <td colspan=2 align=right width=100%>
499 <th align=right>| . $locale->text('Total') . qq|</th>
500 <td align=right>$form->{invtotal}</td>
513 <td><hr size=3 noshade></td>
516 <th class=listtop align=left>Dokumente im Webdav-Repository</th>
519 <td align=left width=30%><b>Dateiname</b></td>
520 <td align=left width=70%><b>Webdavlink</b></td>
522 foreach my $file (@{ $form->{WEBDAV} }) {
525 <td align="left">$file->{name}</td>
526 <td align="left"><a href="$file->{link}">$file->{type}</a></td>
539 <td colspan=$colspan>
542 <th colspan=6 class=listheading>| . $locale->text('Payments') . qq|</th>
547 if ($form->{currency} eq $form->{defaultcurrency}) {
548 @column_index = qw(datepaid source memo paid AP_paid);
550 @column_index = qw(datepaid source memo paid exchangerate AP_paid);
554 $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
555 $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
556 $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
557 $column_data{AP_paid} = "<th>" . $locale->text('Account') . "</th>";
558 $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
559 $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
564 map { print "$column_data{$_}\n" } @column_index;
572 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
573 for my $i (1 .. $form->{paidaccounts}) {
579 $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
580 $form->{"selectAP_paid_$i"} =~
581 s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
583 $totalpaid += $form->{"paid_$i"};
586 if ($form->{"paid_$i"}) {
588 $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
590 $form->{"exchangerate_$i"} =
591 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
593 my $exchangerate = qq| |;
594 if ($form->{currency} ne $form->{defaultcurrency}) {
595 if ($form->{"forex_$i"}) {
597 qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
600 qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
604 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
607 $column_data{"paid_$i"} =
608 qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
609 $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
610 $column_data{"AP_paid_$i"} =
611 qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
612 $column_data{"datepaid_$i"} =
613 qq|<td align=center><input name="datepaid_$i" id="datepaid_$i" size=11 title="$myconfig{dateformat}" value="$form->{"datepaid_$i"}" onBlur=\"check_right_date_format(this)\">
614 <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
615 $column_data{"source_$i"} =
616 qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
617 $column_data{"memo_$i"} =
618 qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
620 map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
625 push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
628 my $paid_missing = $form->{oldinvtotal} - $totalpaid;
634 <td align="center">| . $locale->text('Total') . qq|</td>
635 <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
640 <td align="center">| . $locale->text('Missing amount') . qq|</td>
641 <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
644 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
645 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
646 <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
651 <td><hr size=3 noshade></td>
657 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
658 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
660 print qq|<input class=submit type=submit name=action id=update_button value="|
661 . $locale->text('Update') . qq|">
665 my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && (($totalpaid == 0) || ($totalpaid eq ""));
667 print qq|<input class=submit type=submit name=action value="|
668 . $locale->text('Post Payment') . qq|">
670 print qq|<input class=submit type=submit name=action value="|
671 . $locale->text('Storno') . qq|">
673 if ($form->{radier}) {
675 <input class=submit type=submit name=action value="|
676 . $locale->text('Delete') . qq|">
679 print qq|<input class=submit type=submit name=action value="|
680 . $locale->text('Use As Template') . qq|">
681 <input type="button" class="submit" onclick="follow_up_window()" value="|
682 . $locale->text('Follow-Up')
688 if (!$form->{id} && ($invdate > $closedto)) {
689 print qq| <input class=submit type=submit name=action value="|
690 . $locale->text('Post') . qq|"> | .
691 NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
692 '-class' => 'submit'));
695 print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers);
696 $form->hide_form(qw(rowcount callback draft_id draft_description vendor_discount));
698 # button for saving history
699 if($form->{id} ne "") {
701 <input type="button" class="submit" onclick="set_history_window(|
703 . qq|);" name="history" id="history" value="|
704 . $locale->text('history')
707 # /button for saving history
708 # mark_as_paid button
709 if($form->{id} ne "") {
710 print qq| <input type="submit" class="submit" name="action" value="|
711 . $locale->text('mark as paid') . qq|">|;
713 # /mark_as_paid button
719 $main::lxdebug->leave_sub();
723 $main::lxdebug->enter_sub();
725 my $form = $main::form;
726 my %myconfig = %main::myconfig;
728 $main::auth->assert('vendor_invoice_edit');
730 &mark_as_paid_common(\%myconfig,"ap");
732 $main::lxdebug->leave_sub();
736 $main::lxdebug->enter_sub();
738 my $form = $main::form;
739 my %myconfig = %main::myconfig;
741 $main::auth->assert('vendor_invoice_edit');
743 # map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
745 &check_name('vendor');
747 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
748 $form->{exchangerate} = $form->{forex} if $form->{forex};
750 for my $i (1 .. $form->{paidaccounts}) {
751 next unless $form->{"paid_$i"};
752 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
753 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
754 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
757 my $i = $form->{rowcount};
758 my $exchangerate = ($form->{exchangerate} * 1) || 1;
760 if ( ($form->{"partnumber_$i"} eq "")
761 && ($form->{"description_$i"} eq "")
762 && ($form->{"partsgroup_$i"} eq "")) {
763 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
768 IR->retrieve_item(\%myconfig, \%$form);
770 my $rows = scalar @{ $form->{item_list} };
773 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
782 # override sellprice if there is one entered
783 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
785 # ergaenzung fuer bug 736 Lieferanten-Rabatt auch in Einkaufsrechnungen vorbelegen jb
786 $form->{"discount_$i"} = $form->format_amount(\%myconfig,
787 $form->{vendor_discount} * 100 );
788 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
789 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
791 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
793 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
794 my $dec_qty = length $1;
795 my $decimalplaces = max 2, $dec_qty;
798 $form->{"sellprice_$i"} = $sellprice;
800 # if there is an exchange rate adjust sellprice
801 $form->{"sellprice_$i"} /= $exchangerate;
804 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
805 $form->{creditremaining} -= $amount;
806 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
807 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
814 # ok, so this is a new part
815 # ask if it is a part or service item
817 if ( $form->{"partsgroup_$i"}
818 && ($form->{"partsnumber_$i"} eq "")
819 && ($form->{"description_$i"} eq "")) {
821 $form->{"discount_$i"} = "";
825 $form->{"id_$i"} = 0;
830 $main::lxdebug->leave_sub();
834 $main::lxdebug->enter_sub();
836 my $form = $main::form;
837 my %myconfig = %main::myconfig;
838 my $locale = $main::locale;
840 $main::auth->assert('vendor_invoice_edit');
842 if ($form->{storno}) {
843 $form->error($locale->text('Cannot storno storno invoice!'));
846 if (IS->has_storno(\%myconfig, $form, "ap")) {
847 $form->error($locale->text("Invoice has already been storno'd!"));
850 my $employee_id = $form->{employee_id};
855 # Payments must not be recorded for the new storno invoice.
856 $form->{paidaccounts} = 0;
857 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
860 if(!exists $form->{addition} && $form->{id} ne "") {
861 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
862 $form->{addition} = "CANCELED";
863 $form->save_history($form->dbconnect(\%myconfig));
865 # /saving the history
867 $form->{storno_id} = $form->{id};
870 $form->{invnumber} = "Storno zu " . $form->{invnumber};
872 $form->{employee_id} = $employee_id;
874 $main::lxdebug->leave_sub();
878 sub use_as_template {
879 $main::lxdebug->enter_sub();
881 my $form = $main::form;
882 my %myconfig = %main::myconfig;
884 $main::auth->assert('vendor_invoice_edit');
886 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);
887 $form->{paidaccounts} = 1;
889 $form->{invdate} = $form->current_date(\%myconfig);
892 $main::lxdebug->leave_sub();
896 $main::lxdebug->enter_sub();
898 my $form = $main::form;
899 my %myconfig = %main::myconfig;
900 my $locale = $main::locale;
902 $main::auth->assert('vendor_invoice_edit');
904 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
905 for my $i (1 .. $form->{paidaccounts}) {
906 if ($form->{"paid_$i"}) {
907 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
909 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
911 $form->error($locale->text('Cannot post payment for a closed period!'))
912 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
914 if ($form->{currency} ne $form->{defaultcurrency}) {
915 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
916 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
921 ($form->{AP}) = split /--/, $form->{AP};
922 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
923 if (IR->post_payment(\%myconfig, \%$form)){
924 if (!exists $form->{addition} && $form->{id} ne "") {
926 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
927 $form->{addition} = "PAYMENT POSTED";
928 $form->{what_done} = $form->{currency} . qq| | . $form->{paid} . qq| | . $locale->text("POSTED");
929 $form->save_history($form->dbconnect(\%myconfig));
930 # /saving the history
933 $form->redirect($locale->text('Payment posted!'));
936 $form->error($locale->text('Cannot post payment!'));
938 $main::lxdebug->leave_sub();
942 my $form = $main::form;
944 my @dates = sort { $b->[1] cmp $a->[1] }
945 map { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
947 map { $form->{"datepaid_${_}"} }
948 (1..$form->{rowcount});
950 return @dates ? $dates[0]->[0] : undef;
955 $main::lxdebug->enter_sub();
957 my $form = $main::form;
958 my %myconfig = %main::myconfig;
959 my $locale = $main::locale;
961 $main::auth->assert('vendor_invoice_edit');
963 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
965 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
966 $form->isblank("vendor", $locale->text('Vendor missing!'));
967 $form->isblank("invnumber", $locale->text('Invnumber missing!'));
969 $form->{invnumber} =~ s/^\s*//g;
970 $form->{invnumber} =~ s/\s*$//g;
972 # if the vendor changed get new values
973 if (&check_name('vendor')) {
980 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
981 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
982 my $max_datepaid = _max_datepaid();
984 $form->error($locale->text('Cannot post invoice for a closed period!')) if $max_datepaid && $form->date_closed($max_datepaid, \%myconfig);
986 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
987 if ($form->{currency} ne $form->{defaultcurrency});
990 for $i (1 .. $form->{paidaccounts}) {
991 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
992 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
994 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
996 $form->error($locale->text('Cannot post payment for a closed period!'))
997 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
999 if ($form->{currency} ne $form->{defaultcurrency}) {
1000 $form->{"exchangerate_$i"} = $form->{exchangerate}
1001 if ($invdate == $datepaid);
1002 $form->isblank("exchangerate_$i",
1003 $locale->text('Exchangerate for payment missing!'));
1008 ($form->{AP}) = split /--/, $form->{AP};
1009 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1010 $form->{storno} ||= 0;
1012 $form->{id} = 0 if $form->{postasnew};
1016 if (IR->post_invoice(\%myconfig, \%$form)){
1017 # saving the history
1018 if(!exists $form->{addition} && $form->{id} ne "") {
1019 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1020 $form->{addition} = "POSTED";
1021 #$form->{what_done} = $locale->text("Rechnungsnummer") . qq| | . $form->{invnumber};
1022 $form->save_history($form->dbconnect(\%myconfig));
1024 # /saving the history
1025 remove_draft() if $form->{remove_draft};
1026 $form->redirect( $locale->text('Invoice')
1027 . " $form->{invnumber} "
1028 . $locale->text('posted!'));
1030 $form->error($locale->text('Cannot post invoice!'));
1032 $main::lxdebug->leave_sub();
1036 $main::lxdebug->enter_sub();
1038 my $form = $main::form;
1039 my $locale = $main::locale;
1041 $main::auth->assert('vendor_invoice_edit');
1047 <form method=post action=$form->{script}>
1050 # delete action variable
1051 map { delete $form->{$_} } qw(action header);
1053 foreach my $key (keys %$form) {
1054 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1055 $form->{$key} =~ s/\"/"/g;
1056 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1060 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1063 . $locale->text('Are you sure you want to delete Invoice Number')
1064 . qq| $form->{invnumber}</h4>
1066 <input name=action class=submit type=submit value="|
1067 . $locale->text('Yes') . qq|">
1071 $main::lxdebug->leave_sub();
1075 $main::lxdebug->enter_sub();
1077 my $form = $main::form;
1078 my %myconfig = %main::myconfig;
1079 my $locale = $main::locale;
1081 $main::auth->assert('vendor_invoice_edit');
1083 if (IR->delete_invoice(\%myconfig, \%$form)) {
1084 # saving the history
1085 if(!exists $form->{addition}) {
1086 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1087 $form->{addition} = "DELETED";
1088 $form->save_history($form->dbconnect(\%myconfig));
1090 # /saving the history
1091 $form->redirect($locale->text('Invoice deleted!'));
1093 $form->error($locale->text('Cannot delete invoice!'));
1095 $main::lxdebug->leave_sub();
1098 sub set_duedate_vendor {
1099 $main::lxdebug->enter_sub();
1101 my $form = $main::form;
1103 print $form->ajax_response_header(), IR->get_duedate('vendor_id' => $form->{vendor_id},
1104 'invdate' => $form->{invdate},
1105 'default' => $form->{old_duedate});
1107 $main::lxdebug->leave_sub();