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');
101 $form->{webdav} = $main::webdav;
102 $form->{jsscript} = 1;
104 $form->create_links("AP", \%myconfig, "vendor");
106 #quote all_vendor Bug 133
107 foreach my $ref (@{ $form->{all_vendor} }) {
108 $ref->{name} = $form->quote($ref->{name});
111 if ($form->{all_vendor}) {
112 unless ($form->{vendor_id}) {
113 $form->{vendor_id} = $form->{all_vendor}->[0]->{id};
117 my ($payment_id, $language_id, $taxzone_id);
118 if ($form->{payment_id}) {
119 $payment_id = $form->{payment_id};
121 if ($form->{language_id}) {
122 $language_id = $form->{language_id};
124 if ($form->{taxzone_id}) {
125 $taxzone_id = $form->{taxzone_id};
128 my $cp_id = $form->{cp_id};
129 IR->get_vendor(\%myconfig, \%$form);
130 IR->retrieve_invoice(\%myconfig, \%$form);
131 $form->{cp_id} = $cp_id;
134 $form->{payment_id} = $payment_id;
137 $form->{language_id} = $language_id;
140 $form->{taxzone_id} = $taxzone_id;
143 my @curr = split(/:/, $form->{currencies}); #seems to be missing
144 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
146 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
148 # build vendor/customer drop down comatibility... don't ask
149 if (@{ $form->{"all_vendor"} || [] }) {
150 $form->{"selectvendor"} = 1;
151 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
155 if ($form->{all_departments}) {
156 $form->{selectdepartment} = "<option>\n";
157 $form->{department} = "$form->{department}--$form->{department_id}";
160 $form->{selectdepartment} .=
161 "<option>$_->{description}--$_->{id}\n"
162 } (@{ $form->{all_departments} || [] });
166 $form->{forex} = $form->{exchangerate};
167 my $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
169 foreach my $key (keys %{ $form->{AP_links} }) {
171 foreach my $ref (@{ $form->{AP_links}{$key} }) {
172 $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
175 next unless $form->{acc_trans}{$key};
177 if ($key eq "AP_paid") {
178 for my $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
179 $form->{"AP_paid_$i"} =
180 "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
183 $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{amount};
184 $form->{"datepaid_$i"} =
185 $form->{acc_trans}{$key}->[$i - 1]->{transdate};
186 $form->{"forex_$i"} = $form->{"exchangerate_$i"} =
187 $form->{acc_trans}{$key}->[$i - 1]->{exchangerate};
188 $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{source};
189 $form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i - 1]->{memo};
191 $form->{paidaccounts} = $i;
195 "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
200 $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
202 $form->{AP} = $form->{AP_1} unless $form->{id};
205 ($form->datetonum($form->{invdate}, \%myconfig) <=
206 $form->datetonum($form->{closedto}, \%myconfig));
208 $main::lxdebug->leave_sub();
211 sub prepare_invoice {
212 $main::lxdebug->enter_sub();
214 my $form = $main::form;
215 my %myconfig = %main::myconfig;
217 $main::auth->assert('vendor_invoice_edit');
221 map { $form->{$_} =~ s/\"/"/g } qw(invnumber ordnumber quonumber);
224 foreach my $ref (@{ $form->{invoice_details} }) {
226 map { $form->{"${_}_$i"} = $ref->{$_} } keys %{$ref};
228 my ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
230 my $decimalplaces = ($dec > 2) ? $dec : 2;
232 $form->{"sellprice_$i"} =
233 $form->format_amount(\%myconfig, $form->{"sellprice_$i"},
236 (my $dec_qty) = ($form->{"qty_$i"} =~ /\.(\d+)/);
237 $dec_qty = length $dec_qty;
240 $form->format_amount(\%myconfig, ($form->{"qty_$i"} * -1), $dec_qty);
242 $form->{rowcount} = $i;
246 $main::lxdebug->leave_sub();
250 $main::lxdebug->enter_sub();
252 my $form = $main::form;
253 my %myconfig = %main::myconfig;
254 my $locale = $main::locale;
255 my $cgi = $main::cgi;
257 $main::auth->assert('vendor_invoice_edit');
259 push @{ $form->{AJAX} }, CGI::Ajax->new('set_duedate_vendor' => "$form->{script}?action=set_duedate_vendor");
261 # set option selected
262 foreach my $item (qw(AP vendor currency department)) {
263 $form->{"select$item"} =~ s/ selected//;
264 $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
267 $form->{employee_id} = $form->{old_employee_id} if $form->{old_employee_id};
268 $form->{salesman_id} = $form->{old_salesman_id} if $form->{old_salesman_id};
269 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
270 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
271 $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
272 $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
273 $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
275 my $exchangerate = "";
276 if ($form->{currency} ne $form->{defaultcurrency}) {
277 if ($form->{forex}) {
278 $exchangerate .= qq| <th align=right nowrap>| . $locale->text('Exchangerate') . qq|</th>
279 <td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>\n|;
281 $exchangerate .= qq| <th align=right nowrap>| . $locale->text('Exchangerate') . qq|</th>
282 <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>\n|;
285 $exchangerate .= qq| <input type=hidden name=forex value=$form->{forex}>\n|;
287 my @old_project_ids = ($form->{"globalproject_id"});
288 map { push @old_project_ids, $form->{"project_id_$_"} if $form->{"project_id_$_"}; } 1..$form->{"rowcount"};
290 $form->get_lists("contacts" => "ALL_CONTACTS",
291 "projects" => { "key" => "ALL_PROJECTS",
293 "old_id" => \@old_project_ids },
294 "taxzones" => "ALL_TAXZONES",
295 "employees" => "ALL_SALESMEN",
296 "currencies" => "ALL_CURRENCIES",
297 "vendors" => "ALL_VENDORS",
298 "price_factors" => "ALL_PRICE_FACTORS");
301 my @values = (undef);
302 foreach my $item (@{ $form->{"ALL_CONTACTS"} }) {
303 push(@values, $item->{"cp_id"});
304 $labels{$item->{"cp_id"}} = $item->{"cp_name"} . ($item->{"cp_abteilung"} ? " ($item->{cp_abteilung})" : "");
308 if (scalar @values > 1) {
311 <th align="right">| . $locale->text('Contact Person') . qq|</th>
312 <td>| . NTI($cgi->popup_menu('-name' => 'cp_id', '-values' => \@values, '-style' => 'width: 250px',
313 '-labels' => \%labels, '-default' => $form->{"cp_id"})) . qq|
320 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
321 push(@values, $item->{"id"});
322 $labels{$item->{"id"}} = $item->{"projectnumber"};
324 my $globalprojectnumber = NTI($cgi->popup_menu('-name' => 'globalproject_id', '-values' => \@values, '-labels' => \%labels,
325 '-default' => $form->{"globalproject_id"}));
330 foreach my $item (@{ $form->{"ALL_CURRENCIES"} }) {
331 push(@values, $item);
332 $labels{$item} = $item;
335 $form->{currency} = $form->{defaultcurrency} unless $form->{currency};
337 if (scalar @values) {
340 <th align="right">| . $locale->text('Currency') . qq|</th>
341 <td>| . NTI($cgi->popup_menu('-name' => 'currency', '-default' => $form->{"currency"},
342 '-values' => \@values, '-labels' => \%labels)) . qq|
349 foreach my $item (@{ $form->{"ALL_SALESMEN"} }) {
350 push(@values, $item->{"id"});
351 $labels{$item->{"id"}} = $item->{"name"};
355 <th align="right">| . $locale->text('Employee') . qq|</th>
356 <td>| . NTI($cgi->popup_menu('-name' => 'employee_id', '-default' => $form->{"employee_id"},
357 '-values' => \@values, '-labels' => \%labels)) . qq|
363 foreach my $item (@{ $form->{"ALL_VENDORS"} }) {
364 push(@values, $item->{name}.qq|--|.$item->{"id"});
365 $labels{$item->{name}.qq|--|.$item->{"id"}} = $item->{"name"};
368 $form->{selectvendor} = ($myconfig{vclimit} > scalar(@values));
371 <th align="right">| . $locale->text('Vendor') . qq|</th>
373 (($myconfig{vclimit} <= scalar(@values))
374 ? qq|<input type="text" value="| . H($form->{vendor}) . qq|" name="vendor">|
375 : (NTI($cgi->popup_menu('-name' => 'vendor', '-default' => $form->{oldvendor},
376 '-onChange' => 'document.getElementById(\'update_button\').click();',
377 '-values' => \@values, '-labels' => \%labels, '-style' => 'width: 250px')))) . qq|
378 <input type="button" value="| . $locale->text('Details (one letter abbreviation)') . qq|" onclick="show_vc_details('vendor')">
383 foreach my $item (@{ $form->{"ALL_TAXZONES"} }) {
384 push(@values, $item->{"id"});
385 $labels{$item->{"id"}} = $item->{"description"};
389 if (!$form->{"id"}) {
392 <th align="right">| . $locale->text('Steuersatz') . qq|</th>
393 <td>| . NTI($cgi->popup_menu('-name' => 'taxzone_id', '-default' => $form->{"taxzone_id"},
394 '-values' => \@values, '-labels' => \%labels, '-style' => 'width: 250px')) . qq|
400 <th align="right">| . $locale->text('Steuersatz') . qq|</th>
402 <input type="hidden" name="taxzone_id" value="| . H($form->{"taxzone_id"}) . qq|">
403 | . H($labels{$form->{"taxzone_id"}}) . qq|
410 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
411 <td colspan="3"><select name="department" style="width: 250px">$form->{selectdepartment}</select>
412 <input type="hidden" name="selectdepartment" value="$form->{selectdepartment}">
414 </tr>\n| if $form->{selectdepartment};
416 my $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
418 # use JavaScript Calendar or not
419 $form->{jsscript} = 1;
424 <input name=invdate id=invdate size=11 title="$myconfig{dateformat}" value="$form->{invdate}" onBlur=\"check_right_date_format(this)\"
425 onChange="if (this.value) set_duedate_vendor(['invdate__' + this.value, 'old_duedate__' + document.getElementsByName('duedate')[0].value, 'vendor_id__' + document.getElementsByName('vendor_id')[0].value],['duedate'])">
426 <input type=button name=invdate id="trigger1" value="?">
429 #, 'old_duedate__'' + document.getElementsByName('duedate')[0].value, 'vendor_id__' + document.getElementsByName('vendor_id')[0].value],['duedate'])">
431 <td width="13" nowrap>
432 <input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\">
433 <input type=button name=duedate id="trigger2" value=| . $locale->text('button') . qq|>
438 Form->write_trigger(\%myconfig, "2",
439 "invdate", "BL", "trigger1",
440 "duedate", "BL", "trigger2");
442 my $follow_up_vc = $form->{vendor};
443 $follow_up_vc =~ s/--\d*\s*$//;
444 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
446 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_form_details.js"></script>|;
447 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
448 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
449 $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
451 $jsscript .= $form->write_trigger(\%myconfig, 2, "orddate", "BL", "trigger_orddate", "quodate", "BL", "trigger_quodate");
454 my $onload = qq|focus()|;
455 $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
456 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
458 <body onLoad="$onload">
459 <script type="text/javascript" src="js/common.js"></script>
460 <form method="post" action="ir.pl" name="Form">
463 $form->hide_form(qw(id title vc type level creditlimit creditremaining closedto locked shippted storno storno_id
464 max_dunning_level dunning_amount vendor_id oldvendor selectvendor taxaccounts
465 fxgain_accno fxloss_accno taxpart taxservice cursor_fokus
466 convert_from_oe_ids convert_from_do_ids),
467 map { $_.'_rate', $_.'_description' } split / /, $form->{taxaccounts} );
469 print qq|<p>$form->{saved_message}</p>| if $form->{saved_message};
473 <div class="listtop" width="100%">$form->{title}</div>
482 <td align="right">| . $locale->text('Credit Limit') . qq|</td>
483 <td>$form->{creditlimit}; | . $locale->text('Remaining') . qq| <span class="plus$n">$form->{creditremaining}</span></td>
486 <th align="right">| . $locale->text('Record in') . qq|</th>
487 <td colspan="3"><select name="AP" style="width: 250px">$form->{selectAP}</select></td>
488 <input type="hidden" name="selectAP" value="$form->{selectAP}">
502 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
503 <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
506 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
510 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
514 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
515 <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
516 <input type=hidden name=quonumber value="$form->{quonumber}">
519 <th align="right" nowrap>| . $locale->text('Order Date') . qq|</th>
520 <td><input name="orddate" id="orddate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{orddate}) . qq|" onBlur=\"check_right_date_format(this)\">
521 <input type="button" name="b_orddate" id="trigger_orddate" value="?"></td>
524 <th align="right" nowrap>| . $locale->text('Quotation Date') . qq|</th>
525 <td><input name="quodate" id="quodate" size="11" title="$myconfig{dateformat}" value="| . Q($form->{quodate}) . qq|" onBlur=\"check_right_date_format(this)\">
526 <input type="button" name="b_quodate" id="trigger_quodate" value="?"></td>
529 <th align="right" nowrap>| . $locale->text('Project Number') . qq|</th>
530 <td>$globalprojectnumber</td>
541 <input type=hidden name=webdav value=$main::webdav>
544 foreach my $item (split / /, $form->{taxaccounts}) {
546 <input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
547 <input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
551 $main::lxdebug->leave_sub();
555 $main::lxdebug->enter_sub();
557 my $form = $main::form;
558 my %myconfig = %main::myconfig;
559 my $locale = $main::locale;
560 my $cgi = $main::cgi;
562 $main::auth->assert('vendor_invoice_edit');
564 $form->{invtotal} = $form->{invsubtotal};
566 my ($rows, $introws);
567 if (($rows = $form->numtextrows($form->{notes}, 25, 8)) < 2) {
570 if (($introws = $form->numtextrows($form->{intnotes}, 35, 8)) < 2) {
573 $rows = ($rows > $introws) ? $rows : $introws;
575 qq|<textarea name=notes rows=$rows cols=25 wrap=soft>$form->{notes}</textarea>|;
577 qq|<textarea name=intnotes rows=$rows cols=35 wrap=soft>$form->{intnotes}</textarea>|;
579 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
581 my $taxincluded = "";
582 if ($form->{taxaccounts}) {
584 <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|
585 . $locale->text('Tax Included') . qq|</b>
589 my ($tax, $subtotal);
590 if (!$form->{taxincluded}) {
592 foreach my $item (split / /, $form->{taxaccounts}) {
593 if ($form->{"${item}_base"}) {
594 $form->{invtotal} += $form->{"${item}_total"} =
596 $form->{"${item}_base"} * $form->{"${item}_rate"},
598 $form->{"${item}_total"} =
599 $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
603 <th align=right>$form->{"${item}_description"} |
604 . $form->{"${item}_rate"} * 100 .qq|%</th>
605 <td align=right>$form->{"${item}_total"}</td>
611 $form->{invsubtotal} =
612 $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
616 <th align=right>| . $locale->text('Subtotal') . qq|</th>
617 <td align=right>$form->{invsubtotal}</td>
623 if ($form->{taxincluded}) {
624 foreach my $item (split / /, $form->{taxaccounts}) {
625 if ($form->{"${item}_base"}) {
626 $form->{"${item}_total"} =
628 ($form->{"${item}_base"} * $form->{"${item}_rate"} /
629 (1 + $form->{"${item}_rate"})
632 $form->{"${item}_base"} =
633 $form->round_amount($form->{"${item}_base"}, 2);
634 $form->{"${item}_netto"} =
636 ($form->{"${item}_base"} - $form->{"${item}_total"}),
638 $form->{"${item}_netto"} =
639 $form->format_amount(\%myconfig, $form->{"${item}_netto"}, 2);
640 $form->{"${item}_total"} =
641 $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
645 <th align=right>Enthaltene $form->{"${item}_description"} |
646 . $form->{"${item}_rate"} * 100 .qq|%</th>
647 <td align=right>$form->{"${item}_total"}</td>
650 <th align=right>Nettobetrag</th>
651 <td align=right>$form->{"${item}_netto"}</td>
659 $form->{oldinvtotal} = $form->{invtotal};
661 $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
663 my $follow_ups_block;
665 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
667 if (@{ $follow_ups} ) {
668 my $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
669 $follow_ups_block = qq|
671 <td colspan="2">| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</td>
680 <td colspan=$colspan>
681 <table cellspacing="0">
686 <th align=left>| . $locale->text('Notes') . qq|</th>
687 <th align=left>| . $locale->text('Internal Notes') . qq|</th>
696 <td colspan=2 align=right width=100%>
703 <th align=right>| . $locale->text('Total') . qq|</th>
704 <td align=right>$form->{invtotal}</td>
717 <td><hr size=3 noshade></td>
720 <th class=listtop align=left>Dokumente im Webdav-Repository</th>
723 <td align=left width=30%><b>Dateiname</b></td>
724 <td align=left width=70%><b>Webdavlink</b></td>
726 foreach my $file (@{ $form->{WEBDAV} }) {
729 <td align="left">$file->{name}</td>
730 <td align="left"><a href="$file->{link}">$file->{type}</a></td>
743 <td colspan=$colspan>
746 <th colspan=6 class=listheading>| . $locale->text('Payments') . qq|</th>
751 if ($form->{currency} eq $form->{defaultcurrency}) {
752 @column_index = qw(datepaid source memo paid AP_paid);
754 @column_index = qw(datepaid source memo paid exchangerate AP_paid);
758 $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
759 $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
760 $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
761 $column_data{AP_paid} = "<th>" . $locale->text('Account') . "</th>";
762 $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
763 $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
768 map { print "$column_data{$_}\n" } @column_index;
776 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
777 for my $i (1 .. $form->{paidaccounts}) {
783 $form->{"selectAP_paid_$i"} = $form->{selectAP_paid};
784 $form->{"selectAP_paid_$i"} =~
785 s/option>\Q$form->{"AP_paid_$i"}\E/option selected>$form->{"AP_paid_$i"}/;
787 $totalpaid += $form->{"paid_$i"};
790 if ($form->{"paid_$i"}) {
792 $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
794 $form->{"exchangerate_$i"} =
795 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
797 my $exchangerate = qq| |;
798 if ($form->{currency} ne $form->{defaultcurrency}) {
799 if ($form->{"forex_$i"}) {
801 qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
804 qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
808 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
811 $column_data{"paid_$i"} =
812 qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
813 $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
814 $column_data{"AP_paid_$i"} =
815 qq|<td align=center><select name="AP_paid_$i">$form->{"selectAP_paid_$i"}</select></td>|;
816 $column_data{"datepaid_$i"} =
817 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)\">
818 <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="?"></td>|;
819 $column_data{"source_$i"} =
820 qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
821 $column_data{"memo_$i"} =
822 qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
824 map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
829 push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
832 my $paid_missing = $form->{oldinvtotal} - $totalpaid;
838 <td align="center">| . $locale->text('Total') . qq|</td>
839 <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
844 <td align="center">| . $locale->text('Missing amount') . qq|</td>
845 <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
848 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
849 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
850 <input type=hidden name=selectAP_paid value="$form->{selectAP_paid}">
855 <td><hr size=3 noshade></td>
861 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
862 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
864 print qq|<input class=submit type=submit name=action id=update_button value="|
865 . $locale->text('Update') . qq|">
869 my $show_storno = !$form->{storno} && !IS->has_storno(\%myconfig, $form, "ap") && (($totalpaid == 0) || ($totalpaid eq ""));
871 print qq|<input class=submit type=submit name=action value="|
872 . $locale->text('Post Payment') . qq|">
874 print qq|<input class=submit type=submit name=action value="|
875 . $locale->text('Storno') . qq|">
877 if ($form->{radier}) {
879 <input class=submit type=submit name=action value="|
880 . $locale->text('Delete') . qq|">
883 print qq|<input class=submit type=submit name=action value="|
884 . $locale->text('Use As Template') . qq|">
885 <input type="button" class="submit" onclick="follow_up_window()" value="|
886 . $locale->text('Follow-Up')
892 if (!$form->{id} && ($invdate > $closedto)) {
893 print qq| <input class=submit type=submit name=action value="|
894 . $locale->text('Post') . qq|"> | .
895 NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'),
896 '-class' => 'submit'));
899 print $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers);
900 $form->hide_form(qw(rowcount callback draft_id draft_description vendor_discount));
902 # button for saving history
903 if($form->{id} ne "") {
905 <input type="button" class="submit" onclick="set_history_window(|
907 . qq|);" name="history" id="history" value="|
908 . $locale->text('history')
911 # /button for saving history
912 # mark_as_paid button
913 if($form->{id} ne "") {
914 print qq| <input type="submit" class="submit" name="action" value="|
915 . $locale->text('mark as paid') . qq|">|;
917 # /mark_as_paid button
923 $main::lxdebug->leave_sub();
927 $main::lxdebug->enter_sub();
929 my $form = $main::form;
930 my %myconfig = %main::myconfig;
932 $main::auth->assert('vendor_invoice_edit');
934 &mark_as_paid_common(\%myconfig,"ap");
936 $main::lxdebug->leave_sub();
940 $main::lxdebug->enter_sub();
942 my $form = $main::form;
943 my %myconfig = %main::myconfig;
945 $main::auth->assert('vendor_invoice_edit');
947 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
949 &check_name('vendor');
951 $form->{forex} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'sell');
952 $form->{exchangerate} = $form->{forex} if $form->{forex};
954 for my $i (1 .. $form->{paidaccounts}) {
955 next unless $form->{"paid_$i"};
956 map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
957 $form->{"forex_$i"} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
958 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
961 my $i = $form->{rowcount};
962 my $exchangerate = ($form->{exchangerate} * 1) || 1;
964 if ( ($form->{"partnumber_$i"} eq "")
965 && ($form->{"description_$i"} eq "")
966 && ($form->{"partsgroup_$i"} eq "")) {
967 $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
972 IR->retrieve_item(\%myconfig, \%$form);
974 my $rows = scalar @{ $form->{item_list} };
977 $form->{"qty_$i"} = 1 unless ($form->{"qty_$i"});
986 # override sellprice if there is one entered
987 my $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
989 # ergaenzung fuer bug 736 Lieferanten-Rabatt auch in Einkaufsrechnungen vorbelegen jb
990 $form->{"discount_$i"} = $form->format_amount(\%myconfig,
991 $form->{vendor_discount} * 100 );
992 map { $form->{item_list}[$i]{$_} =~ s/\"/"/g } qw(partnumber description unit);
993 map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
995 $form->{"marge_price_factor_$i"} = $form->{item_list}->[0]->{price_factor};
997 ($sellprice || $form->{"sellprice_$i"}) =~ /\.(\d+)/;
998 my $dec_qty = length $1;
999 my $decimalplaces = max 2, $dec_qty;
1002 $form->{"sellprice_$i"} = $sellprice;
1004 # if there is an exchange rate adjust sellprice
1005 $form->{"sellprice_$i"} /= $exchangerate;
1008 my $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
1009 $form->{creditremaining} -= $amount;
1010 $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
1011 $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"}, $dec_qty);
1018 # ok, so this is a new part
1019 # ask if it is a part or service item
1021 if ( $form->{"partsgroup_$i"}
1022 && ($form->{"partsnumber_$i"} eq "")
1023 && ($form->{"description_$i"} eq "")) {
1024 $form->{rowcount}--;
1025 $form->{"discount_$i"} = "";
1029 $form->{"id_$i"} = 0;
1034 $main::lxdebug->leave_sub();
1038 $main::lxdebug->enter_sub();
1040 my $form = $main::form;
1041 my %myconfig = %main::myconfig;
1042 my $locale = $main::locale;
1044 $main::auth->assert('vendor_invoice_edit');
1046 if ($form->{storno}) {
1047 $form->error($locale->text('Cannot storno storno invoice!'));
1050 if (IS->has_storno(\%myconfig, $form, "ap")) {
1051 $form->error($locale->text("Invoice has already been storno'd!"));
1054 my $employee_id = $form->{employee_id};
1059 # Payments must not be recorded for the new storno invoice.
1060 $form->{paidaccounts} = 0;
1061 map { my $key = $_; delete $form->{$key} if grep { $key =~ /^$_/ } qw(datepaid_ source_ memo_ paid_ exchangerate_ AR_paid_) } keys %{ $form };
1063 # saving the history
1064 if(!exists $form->{addition} && $form->{id} ne "") {
1065 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1066 $form->{addition} = "CANCELED";
1067 $form->save_history($form->dbconnect(\%myconfig));
1069 # /saving the history
1071 $form->{storno_id} = $form->{id};
1072 $form->{storno} = 1;
1074 $form->{invnumber} = "Storno zu " . $form->{invnumber};
1075 $form->{rowcount}++;
1076 $form->{employee_id} = $employee_id;
1078 $main::lxdebug->leave_sub();
1082 sub use_as_template {
1083 $main::lxdebug->enter_sub();
1085 my $form = $main::form;
1086 my %myconfig = %main::myconfig;
1088 $main::auth->assert('vendor_invoice_edit');
1090 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);
1091 $form->{paidaccounts} = 1;
1092 $form->{rowcount}--;
1093 $form->{invdate} = $form->current_date(\%myconfig);
1096 $main::lxdebug->leave_sub();
1100 $main::lxdebug->enter_sub();
1102 my $form = $main::form;
1103 my %myconfig = %main::myconfig;
1104 my $locale = $main::locale;
1106 $main::auth->assert('vendor_invoice_edit');
1108 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1109 for my $i (1 .. $form->{paidaccounts}) {
1110 if ($form->{"paid_$i"}) {
1111 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1113 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1115 $form->error($locale->text('Cannot post payment for a closed period!'))
1116 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1118 if ($form->{currency} ne $form->{defaultcurrency}) {
1119 # $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid); # invdate isn't set here
1120 $form->isblank("exchangerate_$i", $locale->text('Exchangerate for payment missing!'));
1125 ($form->{AP}) = split /--/, $form->{AP};
1126 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1127 if (IR->post_payment(\%myconfig, \%$form)){
1128 if (!exists $form->{addition} && $form->{id} ne "") {
1129 # saving the history
1130 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1131 $form->{addition} = "PAYMENT POSTED";
1132 $form->{what_done} = $form->{currency} . qq| | . $form->{paid} . qq| | . $locale->text("POSTED");
1133 $form->save_history($form->dbconnect(\%myconfig));
1134 # /saving the history
1137 $form->redirect($locale->text('Payment posted!'));
1140 $form->error($locale->text('Cannot post payment!'));
1142 $main::lxdebug->leave_sub();
1146 my $form = $main::form;
1148 my @dates = sort { $b->[1] cmp $a->[1] }
1149 map { [ $_, $main::locale->reformat_date(\%main::myconfig, $_, 'yyyy-mm-dd') ] }
1151 map { $form->{"datepaid_${_}"} }
1152 (1..$form->{rowcount});
1154 return @dates ? $dates[0]->[0] : undef;
1159 $main::lxdebug->enter_sub();
1161 my $form = $main::form;
1162 my %myconfig = %main::myconfig;
1163 my $locale = $main::locale;
1165 $main::auth->assert('vendor_invoice_edit');
1167 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
1169 $form->isblank("invdate", $locale->text('Invoice Date missing!'));
1170 $form->isblank("vendor", $locale->text('Vendor missing!'));
1171 $form->isblank("invnumber", $locale->text('Invnumber missing!'));
1173 $form->{invnumber} =~ s/^\s*//g;
1174 $form->{invnumber} =~ s/\s*$//g;
1176 # if the vendor changed get new values
1177 if (&check_name('vendor')) {
1184 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1185 my $invdate = $form->datetonum($form->{invdate}, \%myconfig);
1186 my $max_datepaid = _max_datepaid();
1188 $form->error($locale->text('Cannot post invoice for a closed period!')) if $max_datepaid && $form->date_closed($max_datepaid, \%myconfig);
1190 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1191 if ($form->{currency} ne $form->{defaultcurrency});
1194 for $i (1 .. $form->{paidaccounts}) {
1195 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1196 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1198 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1200 $form->error($locale->text('Cannot post payment for a closed period!'))
1201 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1203 if ($form->{currency} ne $form->{defaultcurrency}) {
1204 $form->{"exchangerate_$i"} = $form->{exchangerate}
1205 if ($invdate == $datepaid);
1206 $form->isblank("exchangerate_$i",
1207 $locale->text('Exchangerate for payment missing!'));
1212 ($form->{AP}) = split /--/, $form->{AP};
1213 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
1214 $form->{storno} ||= 0;
1216 $form->{id} = 0 if $form->{postasnew};
1220 if (IR->post_invoice(\%myconfig, \%$form)){
1221 # saving the history
1222 if(!exists $form->{addition} && $form->{id} ne "") {
1223 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1224 $form->{addition} = "POSTED";
1225 #$form->{what_done} = $locale->text("Rechnungsnummer") . qq| | . $form->{invnumber};
1226 $form->save_history($form->dbconnect(\%myconfig));
1228 # /saving the history
1229 remove_draft() if $form->{remove_draft};
1230 $form->redirect( $locale->text('Invoice')
1231 . " $form->{invnumber} "
1232 . $locale->text('posted!'));
1234 $form->error($locale->text('Cannot post invoice!'));
1236 $main::lxdebug->leave_sub();
1240 $main::lxdebug->enter_sub();
1242 my $form = $main::form;
1243 my $locale = $main::locale;
1245 $main::auth->assert('vendor_invoice_edit');
1251 <form method=post action=$form->{script}>
1254 # delete action variable
1255 map { delete $form->{$_} } qw(action header);
1257 foreach my $key (keys %$form) {
1258 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1259 $form->{$key} =~ s/\"/"/g;
1260 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1264 <h2 class=confirm>| . $locale->text('Confirm!') . qq|</h2>
1267 . $locale->text('Are you sure you want to delete Invoice Number')
1268 . qq| $form->{invnumber}</h4>
1270 <input name=action class=submit type=submit value="|
1271 . $locale->text('Yes') . qq|">
1275 $main::lxdebug->leave_sub();
1279 $main::lxdebug->enter_sub();
1281 my $form = $main::form;
1282 my %myconfig = %main::myconfig;
1283 my $locale = $main::locale;
1285 $main::auth->assert('vendor_invoice_edit');
1287 if (IR->delete_invoice(\%myconfig, \%$form)) {
1288 # saving the history
1289 if(!exists $form->{addition}) {
1290 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1291 $form->{addition} = "DELETED";
1292 $form->save_history($form->dbconnect(\%myconfig));
1294 # /saving the history
1295 $form->redirect($locale->text('Invoice deleted!'));
1297 $form->error($locale->text('Cannot delete invoice!'));
1299 $main::lxdebug->leave_sub();
1302 sub set_duedate_vendor {
1303 $main::lxdebug->enter_sub();
1305 my $form = $main::form;
1307 print $form->ajax_response_header(), IR->get_duedate('vendor_id' => $form->{vendor_id},
1308 'invdate' => $form->{invdate},
1309 'default' => $form->{old_duedate});
1311 $main::lxdebug->leave_sub();