1 #=====================================================================
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
7 #=====================================================================
8 # SQL-Ledger Accounting
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 #======================================================================
32 #======================================================================
34 use POSIX qw(strftime);
35 use List::Util qw(sum);
42 use SL::ReportGenerator;
44 require "bin/mozilla/arap.pl";
45 require "bin/mozilla/common.pl";
46 require "bin/mozilla/drafts.pl";
47 require "bin/mozilla/reportgenerator.pl";
55 # this is for our long dates
56 # $locale->text('January')
57 # $locale->text('February')
58 # $locale->text('March')
59 # $locale->text('April')
60 # $locale->text('May ')
61 # $locale->text('June')
62 # $locale->text('July')
63 # $locale->text('August')
64 # $locale->text('September')
65 # $locale->text('October')
66 # $locale->text('November')
67 # $locale->text('December')
69 # this is for our short month
70 # $locale->text('Jan')
71 # $locale->text('Feb')
72 # $locale->text('Mar')
73 # $locale->text('Apr')
74 # $locale->text('May')
75 # $locale->text('Jun')
76 # $locale->text('Jul')
77 # $locale->text('Aug')
78 # $locale->text('Sep')
79 # $locale->text('Oct')
80 # $locale->text('Nov')
81 # $locale->text('Dec')
84 $main::lxdebug->enter_sub();
86 my $form = $main::form;
87 my %myconfig = %main::myconfig;
89 $main::auth->assert('general_ledger');
91 return $main::lxdebug->leave_sub() if (load_draft_maybe());
93 $form->{title} = "Add";
95 $form->{callback} = "ap.pl?action=add" unless $form->{callback};
97 AP->get_transdate(\%myconfig, $form);
98 $form->{initial_transdate} = $form->{transdate};
100 $form->{transdate} = $form->{initial_transdate};
103 $main::lxdebug->leave_sub();
107 $main::lxdebug->enter_sub();
109 my $form = $main::form;
111 $main::auth->assert('general_ledger');
113 $form->{title} = "Edit";
118 $main::lxdebug->leave_sub();
122 $main::lxdebug->enter_sub();
124 my $form = $main::form;
126 $main::auth->assert('general_ledger');
131 $main::lxdebug->leave_sub();
135 $main::lxdebug->enter_sub();
137 my $form = $main::form;
138 my %myconfig = %main::myconfig;
140 $main::auth->assert('general_ledger');
142 $form->create_links("AP", \%myconfig, "vendor");
143 my $taxincluded = $form->{taxincluded};
144 my $duedate = $form->{duedate};
146 IR->get_vendor(\%myconfig, \%$form);
147 $form->{taxincluded} = $taxincluded;
148 $form->{duedate} = $duedate if $duedate;
149 $form->{oldvendor} = "$form->{vendor}--$form->{vendor_id}";
150 $form->{rowcount} = 1;
152 # build the popup menus
153 $form->{taxincluded} = ($form->{id}) ? $form->{taxincluded} : "checked";
156 $form->{notes} = $form->{intnotes} unless $form->{notes};
159 my @curr = split(/:/, $form->{currencies});
161 $form->{defaultcurrency} = $curr[0];
163 map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
166 if (@{ $form->{all_vendor} || [] }) {
167 $form->{vendor} = qq|$form->{vendor}--$form->{vendor_id}|;
168 map { $form->{selectvendor} .= "<option>$_->{name}--$_->{id}\n" }
169 (@{ $form->{all_vendor} });
173 if (@{ $form->{all_departments} || [] }) {
174 $form->{selectdepartment} = "<option>\n";
175 $form->{department} = "$form->{department}--$form->{department_id}";
178 $form->{selectdepartment} .=
179 "<option>$_->{description}--$_->{id}\n"
180 } (@{ $form->{all_departments} || [] });
183 $form->{employee} = "$form->{employee}--$form->{employee_id}";
185 AP->setup_form($form);
188 ($form->datetonum($form->{transdate}, \%myconfig) <=
189 $form->datetonum($form->{closedto}, \%myconfig));
191 $main::lxdebug->leave_sub();
195 $main::lxdebug->enter_sub();
197 my $form = $main::form;
198 my %myconfig = %main::myconfig;
199 my $locale = $main::locale;
200 my $cgi = $main::cgi;
202 $main::auth->assert('general_ledger');
204 my $title = $form->{title};
205 $form->{title} = $locale->text("$title Accounts Payables Transaction");
207 $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
209 # type=submit $locale->text('Add Accounts Payables Transaction')
210 # type=submit $locale->text('Edit Accounts Payables Transaction')
212 $form->{javascript} = qq|<script type="text/javascript">
214 function setTaxkey(accno, row) {
215 var taxkey = accno.options[accno.selectedIndex].value;
216 var reg = /--([0-9]*)/;
217 var found = reg.exec(taxkey);
218 var index = found[1];
219 index = parseInt(index);
220 var tax = 'taxchart_' + row;
221 for (var i = 0; i < document.getElementById(tax).options.length; ++i) {
222 var reg2 = new RegExp("^"+ index, "");
223 if (reg2.exec(document.getElementById(tax).options[i].value)) {
224 document.getElementById(tax).options[i].selected = true;
231 # show history button
232 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_history.js"></script>|;
233 #/show hhistory button
235 # set option selected
236 foreach my $item (qw(vendor currency department)) {
237 $form->{"select$item"} =~ s/ selected//;
238 $form->{"select$item"} =~
239 s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
241 my $readonly = ($form->{id}) ? "readonly" : "";
243 $form->{radier} = ($form->current_date(\%myconfig) eq $form->{gldate}) ? 1 : 0;
244 $readonly = ($form->{radier}) ? "" : $readonly;
246 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
247 $form->{exchangerate} = $form->{forex} if $form->{forex};
251 $form->{exchangerate} =
252 $form->format_amount(\%myconfig, $form->{exchangerate});
253 if ($form->{exchangerate} == 0) {
254 $form->{exchangerate} = "";
256 $form->{creditlimit} =
257 $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
258 $form->{creditremaining} =
259 $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
261 my $exchangerate = qq|
262 <input type=hidden name=forex value=$form->{forex}>
264 if ($form->{currency} ne $form->{defaultcurrency}) {
265 if ($form->{forex}) {
268 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
269 <td><input type=hidden name=exchangerate value=$form->{exchangerate}>$form->{exchangerate}</td>
274 <th align=right>| . $locale->text('Exchangerate') . qq|</th>
275 <td><input name=exchangerate size=10 value=$form->{exchangerate}></td>
280 my $taxincluded = "";
284 <td align=right><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}></td>
285 <th align=left nowrap>|
286 . $locale->text('Tax Included') . qq|</th>
291 if (($rows = $form->numtextrows($form->{notes}, 50)) < 2) {
295 qq|<textarea name=notes rows=$rows cols=50 wrap=soft $readonly>$form->{notes}</textarea>|;
299 <th align="right" nowrap>| . $locale->text('Department') . qq|</th>
300 <td colspan=3><select name=department>$form->{selectdepartment}</select>
301 <input type=hidden name=selectdepartment value="$form->{selectdepartment}">
304 | if $form->{selectdepartment};
306 my $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
309 ($form->{selectvendor})
310 ? qq|<select name="vendor" onchange="document.getElementById('update_button').click();">$form->{selectvendor} </select>|
311 : qq|<input name=vendor value="$form->{vendor}" size=35>|;
313 my @old_project_ids = ();
314 map({ push(@old_project_ids, $form->{"project_id_$_"})
315 if ($form->{"project_id_$_"}); } (1..$form->{"rowcount"}));
317 $form->get_lists("projects" => { "key" => "ALL_PROJECTS",
319 "old_id" => \@old_project_ids },
320 "charts" => { "key" => "ALL_CHARTS",
321 "transdate" => $form->{transdate} },
322 "taxcharts" => { "key" => "ALL_TAXCHARTS",
323 "module" => "AP" },);
325 map({ $_->{link_split} = [ split(/:/, $_->{link}) ]; }
326 @{ $form->{ALL_CHARTS} });
328 my %project_labels = ();
329 my @project_values = ("");
330 foreach my $item (@{ $form->{"ALL_PROJECTS"} }) {
331 push(@project_values, $item->{"id"});
332 $project_labels{$item->{"id"}} = $item->{"projectnumber"};
335 my (%AP_amount_labels, @AP_amount_values);
336 my (%AP_labels, @AP_values);
337 my (%AP_paid_labels, @AP_paid_values);
341 foreach my $item (@{ $form->{ALL_CHARTS} }) {
342 if (grep({ $_ eq "AP_amount" } @{ $item->{link_split} })) {
343 $taxchart_init = $item->{tax_id} if ($taxchart_init eq "");
344 my $key = "$item->{accno}--$item->{tax_id}";
345 push(@AP_amount_values, $key);
346 $AP_amount_labels{$key} =
347 "$item->{accno}--$item->{description}";
349 } elsif (grep({ $_ eq "AP" } @{ $item->{link_split} })) {
350 push(@AP_values, $item->{accno});
351 $AP_labels{$item->{accno}} = "$item->{accno}--$item->{description}";
353 } elsif (grep({ $_ eq "AP_paid" } @{ $item->{link_split} })) {
354 push(@AP_paid_values, $item->{accno});
355 $AP_paid_labels{$item->{accno}} =
356 "$item->{accno}--$item->{description}";
359 $charts{$item->{accno}} = $item;
362 my %taxchart_labels = ();
363 my @taxchart_values = ();
365 foreach my $item (@{ $form->{ALL_TAXCHARTS} }) {
366 my $key = "$item->{id}--$item->{rate}";
367 $taxchart_init = $key if ($taxchart_init eq $item->{id});
368 push(@taxchart_values, $key);
369 $taxchart_labels{$key} =
370 "$item->{taxdescription} " . ($item->{rate} * 100) . ' %';
371 $taxcharts{$item->{id}} = $item;
374 # use JavaScript Calendar or not
375 $form->{jsscript} = 1;
377 my ($button1, $button2);
378 if ($form->{jsscript}) {
380 # with JavaScript Calendar
382 <td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\" $readonly></td>
383 <td><input type=button name=transdate id="trigger1" value=|
384 . $locale->text('button') . qq|></td>
387 <td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\" $readonly></td>
388 <td><input type=button name=duedate id="trigger2" value=|
389 . $locale->text('button') . qq|></td></td>
394 Form->write_trigger(\%myconfig, "2", "transdate", "BL", "trigger1",
395 "duedate", "BL", "trigger2");
398 # without JavaScript Calendar
400 qq|<td><input name=transdate id=transdate size=11 title="$myconfig{dateformat}" value="$form->{transdate}" onBlur=\"check_right_date_format(this)\" $readonly></td>|;
402 qq|<td><input name=duedate id=duedate size=11 title="$myconfig{dateformat}" value="$form->{duedate}" onBlur=\"check_right_date_format(this)\" $readonly></td>|;
405 my $follow_up_vc = $form->{vendor};
406 $follow_up_vc =~ s/--.*?//;
407 my $follow_up_trans_info = "$form->{invnumber} ($follow_up_vc)";
409 $form->{javascript} .= qq|<script type="text/javascript" src="js/common.js"></script>|;
410 $form->{javascript} .= qq|<script type="text/javascript" src="js/show_vc_details.js"></script>|;
411 $form->{javascript} .= qq|<script type="text/javascript" src="js/follow_up.js"></script>|;
414 my $onload = qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
415 $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
417 <body onLoad="$onload">
419 <form method=post action=$form->{script}>
421 <input type=hidden name=id value=$form->{id}>
422 <input type=hidden name=sort value=$form->{sort}>
423 <input type=hidden name=closedto value=$form->{closedto}>
424 <input type=hidden name=locked value=$form->{locked}>
425 <input type=hidden name=title value="$title">
427 <input type="hidden" name="follow_up_trans_id_1" value="| . H($form->{id}) . qq|">
428 <input type="hidden" name="follow_up_trans_type_1" value="ap_transaction">
429 <input type="hidden" name="follow_up_trans_info_1" value="| . H($follow_up_trans_info) . qq|">
430 <input type="hidden" name="follow_up_rowcount" value="1">
432 | . ($form->{saved_message} ? qq|<p>$form->{saved_message}</p>| : "") . qq|
436 <th class=listtop>$form->{title}</th>
446 <th align=right nowrap>| . $locale->text('Vendor') . qq|</th>
447 <td colspan=3>$vendor <input type="button" value="D" onclick="show_vc_details('vendor')"></td>
448 <input type=hidden name=selectvendor value="| . H($form->{selectvendor}) . qq|">
449 <input type=hidden name=oldvendor value="| . H($form->{oldvendor}) . qq|">
450 <input type=hidden name=vendor_id value="| . H($form->{vendor_id}) . qq|">
451 <input type=hidden name=terms value="| . H($form->{terms}) . qq|">
458 <th align=left nowrap>| . $locale->text('Credit Limit') . qq|</th>
459 <td>$form->{creditlimit}</td>
460 <th align=left nowrap>| . $locale->text('Remaining') . qq|</th>
461 <td class="plus$n">$form->{creditremaining}</td>
462 <input type=hidden name=creditlimit value=$form->{creditlimit}>
463 <input type=hidden name=creditremaining value=$form->{creditremaining}>
468 <th align=right nowrap>| . $locale->text('Currency') . qq|</th>
469 <td><select name=currency>$form->{selectcurrency}</select></td>
470 <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
471 <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
472 <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
473 <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
483 <th align=right nowrap>| . $locale->text('Invoice Number') . qq|</th>
484 <td><input name=invnumber size=11 value="$form->{invnumber}" $readonly></td>
487 <th align=right nowrap>| . $locale->text('Order Number') . qq|</th>
488 <td><input name=ordnumber size=11 value="$form->{ordnumber}" $readonly></td>
491 <th align=right nowrap>| . $locale->text('Invoice Date') . qq|</th>
495 <th align=right nowrap>| . $locale->text('Due Date') . qq|</th>
508 <input type=hidden name=rowcount value=$form->{rowcount}>
512 <tr class=listheading>
513 <th class=listheading style="width:15%">| . $locale->text('Account') . qq|</th>
514 <th class=listheading style="width:10%">| . $locale->text('Amount') . qq|</th>
515 <th class=listheading style="width:10%">| . $locale->text('Tax') . qq|</th>
516 <th class=listheading style="width:10%">| . $locale->text('Taxkey') . qq|</th>
517 <th class=listheading style="width:10%">| . $locale->text('Project') . qq|</th>
521 my $amount = $locale->text('Amount');
522 my $project = $locale->text('Project');
524 for my $i (1 .. $form->{rowcount}) {
527 $form->{"amount_$i"} =
528 $form->format_amount(\%myconfig, $form->{"amount_$i"}, 2);
529 $form->{"tax_$i"} = $form->format_amount(\%myconfig, $form->{"tax_$i"}, 2);
531 my $selected_accno_full;
532 my ($accno_row) = split(/--/, $form->{"AP_amount_$i"});
533 my $item = $charts{$accno_row};
534 $selected_accno_full = "$item->{accno}--$item->{tax_id}";
536 my $selected_taxchart = $form->{"taxchart_$i"};
537 my ($selected_accno, $selected_tax_id) = split(/--/, $selected_accno_full);
538 my ($previous_accno, $previous_tax_id) = split(/--/, $form->{"previous_AP_amount_$i"});
540 if ($previous_accno &&
541 ($previous_accno eq $selected_accno) &&
542 ($previous_tax_id ne $selected_tax_id)) {
543 my $item = $taxcharts{$selected_tax_id};
544 $selected_taxchart = "$item->{id}--$item->{rate}";
547 $selected_taxchart = $taxchart_init unless ($form->{"taxchart_$i"});
549 my $selectAP_amount =
550 NTI($cgi->popup_menu('-name' => "AP_amount_$i",
551 '-id' => "AP_amount_$i",
552 '-style' => 'width:400px',
553 '-onChange' => "setTaxkey(this, $i)",
554 '-values' => \@AP_amount_values,
555 '-labels' => \%AP_amount_labels,
556 '-default' => $selected_accno_full))
557 . $cgi->hidden('-name' => "previous_AP_amount_$i",
558 '-default' => $selected_accno_full);
561 NTI($cgi->popup_menu('-name' => "taxchart_$i",
562 '-id' => "taxchart_$i",
563 '-style' => 'width:200px',
564 '-values' => \@taxchart_values,
565 '-labels' => \%taxchart_labels,
566 '-default' => $selected_taxchart))
570 NTI($cgi->popup_menu('-name' => "project_id_$i",
571 '-values' => \@project_values,
572 '-labels' => \%project_labels,
573 '-default' => $form->{"project_id_$i"} ));
577 <td>$selectAP_amount</td>
578 <td><input name="amount_$i" size=10 value=$form->{"amount_$i"}></td>
579 <td><input type="hidden" name="tax_$i" value="$form->{"tax_$i"}">$form->{"tax_$i"}</td>
581 <td>$projectnumber</td>
589 ($form->{taxincluded})
590 ? $locale->text('Tax Included')
591 : $locale->text('Tax');
593 $form->{invtotal_unformatted} = $form->{invtotal};
594 $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2);
597 NTI($cgi->popup_menu('-name' => "APselected", '-id' => "APselected",
598 '-style' => 'width:400px',
599 '-values' => \@AP_values, '-labels' => \%AP_labels,
600 '-default' => $form->{APselected}));
608 <td>${APselected}</td>
609 <th align=left>$form->{invtotal}</th>
611 <input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
612 <input type=hidden name=oldtotalpaid value=$form->{oldtotalpaid}>
614 <input type=hidden name=taxaccounts value="$form->{taxaccounts}">
627 <th align=left width=1%>| . $locale->text('Notes') . qq|</th>
628 <td align=left>$notes</td>
636 <tr class=listheading>
637 <th class=listheading colspan=7>| . $locale->text('Payments') . qq|</th>
642 if ($form->{currency} eq $form->{defaultcurrency}) {
643 @column_index = qw(datepaid source memo paid AP_paid paid_project_id);
645 @column_index = qw(datepaid source memo paid exchangerate AP_paid paid_project_id);
649 $column_data{datepaid} = "<th>" . $locale->text('Date') . "</th>";
650 $column_data{paid} = "<th>" . $locale->text('Amount') . "</th>";
651 $column_data{exchangerate} = "<th>" . $locale->text('Exch') . "</th>";
652 $column_data{AP_paid} = "<th>" . $locale->text('Account') . "</th>";
653 $column_data{source} = "<th>" . $locale->text('Source') . "</th>";
654 $column_data{memo} = "<th>" . $locale->text('Memo') . "</th>";
655 $column_data{paid_project_id} = "<th>" . $locale->text('Project Number') . "</th>";
660 map { print "$column_data{$_}\n" } @column_index;
668 $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
669 for my $i (1 .. $form->{paidaccounts}) {
675 NTI($cgi->popup_menu('-name' => "AP_paid_$i",
676 '-id' => "AP_paid_$i",
677 '-values' => \@AP_paid_values,
678 '-labels' => \%AP_paid_labels,
679 '-default' => $form->{"AP_paid_$i"}));
681 $totalpaid += $form->{"paid_$i"};
684 if ($form->{"paid_$i"}) {
686 $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
688 $form->{"exchangerate_$i"} =
689 $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
690 if ($form->{"exchangerate_$i"} == 0) {
691 $form->{"exchangerate_$i"} = "";
694 $exchangerate = qq| |;
695 if ($form->{currency} ne $form->{defaultcurrency}) {
696 if ($form->{"forex_$i"}) {
698 qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
701 qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
706 <input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
709 $column_data{"paid_$i"} =
710 qq|<td align=center><input name="paid_$i" size=11 value="$form->{"paid_$i"}" onBlur=\"check_right_number_format(this)\"></td>|;
711 $column_data{"AP_paid_$i"} =
712 qq|<td align=center>${selectAP_paid}</td>|;
713 $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
714 $column_data{"datepaid_$i"} =
715 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)\">
716 <input type="button" name="datepaid_$i" id="trigger_datepaid_$i" value="| . $locale->text('Details (one letter abbreviation)') . qq|"></td>|;
717 $column_data{"source_$i"} =
718 qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
719 $column_data{"memo_$i"} =
720 qq|<td align=center><input name="memo_$i" size=11 value="$form->{"memo_$i"}"></td>|;
721 $column_data{"paid_project_id_$i"} =
723 . NTI($cgi->popup_menu('-name' => "paid_project_id_$i",
724 '-values' => \@project_values,
725 '-labels' => \%project_labels,
726 '-default' => $form->{"paid_project_id_$i"} ))
729 map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
734 push(@triggers, "datepaid_$i", "BL", "trigger_datepaid_$i");
737 my $paid_missing = $form->{invtotal_unformatted} - $totalpaid;
743 <td align="center">| . $locale->text('Total') . qq|</td>
744 <td align="center">| . H($form->format_amount(\%myconfig, $totalpaid, 2)) . qq|</td>
749 <td align="center">| . $locale->text('Missing amount') . qq|</td>
750 <td align="center">| . H($form->format_amount(\%myconfig, $paid_missing, 2)) . qq|</td>
752 | . $form->write_trigger(\%myconfig, scalar(@triggers) / 3, @triggers) .
754 <input type=hidden name=paidaccounts value=$form->{paidaccounts}>
760 <td><hr size=3 noshade></td>
765 $main::lxdebug->leave_sub();
769 $main::lxdebug->enter_sub();
771 my $form = $main::form;
772 my %myconfig = %main::myconfig;
773 my $locale = $main::locale;
774 my $cgi = $main::cgi;
776 $main::auth->assert('general_ledger');
778 my $follow_ups_block;
780 my $follow_ups = FU->follow_ups('trans_id' => $form->{id});
782 if (@{ $follow_ups} ) {
783 my $num_due = sum map { $_->{due} * 1 } @{ $follow_ups };
784 $follow_ups_block = qq|<p>| . $locale->text("There are #1 unfinished follow-ups of which #2 are due.", scalar @{ $follow_ups }, $num_due) . qq|</p>|;
792 <input name=callback type=hidden value="$form->{callback}">
793 <input name="gldate" type="hidden" value="| . Q($form->{gldate}) . qq|">
795 . $cgi->hidden('-name' => 'draft_id', '-default' => [$form->{draft_id}])
796 . $cgi->hidden('-name' => 'draft_description', '-default' => [$form->{draft_description}])
802 if (!$form->{id} && $form->{draft_id}) {
803 print(NTI($cgi->checkbox('-name' => 'remove_draft', '-id' => 'remove_draft',
804 '-value' => 1, '-checked' => $form->{remove_draft},
806 qq| <label for="remove_draft">| .
807 $locale->text("Remove draft when posting") .
811 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
812 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
814 print qq|<input class="submit" type="submit" name="action" id="update_button" value="| . $locale->text('Update') . qq|">|;
817 if ($form->{radier}) {
818 print qq| <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|">
819 <input class=submit type=submit name=action value="| . $locale->text('Delete') . qq|">
822 # ToDO: - insert a global check for stornos, so that a storno is only possible a limited time after saving it
824 print qq| <input class=submit type=submit name=action value="| . $locale->text('Storno') . qq|"> |
825 if ($form->{id} && !IS->has_storno(\%myconfig, $form, 'ap') && !IS->is_storno(\%myconfig, $form, 'ap', $form->{id}) && (($total_paid == 0) || ($total_paid eq "")));
827 print qq| <input class=submit type=submit name=action value="| . $locale->text('Post Payment') . qq|">
828 <input class=submit type=submit name=action value="| . $locale->text('Use As Template') . qq|">
829 <input type="button" class="submit" onclick="follow_up_window()" value="| . $locale->text('Follow-Up') . qq|">
831 } elsif (($transdate > $closedto) && !$form->{id}) {
833 <input class=submit type=submit name=action value="| . $locale->text('Post') . qq|"> | .
834 NTI($cgi->submit('-name' => 'action', '-value' => $locale->text('Save draft'), '-class' => 'submit'));
836 # button for saving history
837 if($form->{id} ne "") {
838 print qq| <input type="button" class="submit" onclick="set_history_window($form->{id});" name="history" id="history" value="| . $locale->text('history') . qq|"> |;
840 # /button for saving history
841 # mark_as_paid button
842 if($form->{id} ne "") {
843 print qq| <input type="submit" class="submit" name="action" value="| . $locale->text('mark as paid') . qq|"> |;
845 # /mark_as_paid button
853 $main::lxdebug->leave_sub();
857 $main::lxdebug->enter_sub();
859 my $form = $main::form;
860 my %myconfig = %main::myconfig;
862 $main::auth->assert('general_ledger');
864 &mark_as_paid_common(\%myconfig,"ap");
866 $main::lxdebug->leave_sub();
870 $main::lxdebug->enter_sub();
872 my $form = $main::form;
873 my %myconfig = %main::myconfig;
875 $main::auth->assert('general_ledger');
879 $form->{invtotal} = 0;
881 delete @{ $form }{ grep { m/^tax_\d+$/ } keys %{ $form } };
883 map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) }
884 qw(exchangerate creditlimit creditremaining);
886 my @flds = qw(amount AP_amount projectnumber oldprojectnumber project_id taxchart);
888 my (@a, $j, $totaltax);
889 for my $i (1 .. $form->{rowcount}) {
890 $form->{"amount_$i"} =
891 $form->parse_amount(\%myconfig, $form->{"amount_$i"});
892 $form->{"tax_$i"} = $form->parse_amount(\%myconfig, $form->{"tax_$i"});
893 if ($form->{"amount_$i"}) {
896 my ($taxkey, $rate) = split(/--/, $form->{"taxchart_$i"});
898 if ($form->{taxincluded}) {
899 $form->{"tax_$i"} = $form->{"amount_$i"} / ($rate + 1) * $rate;
901 $form->{"tax_$i"} = $form->{"amount_$i"} * $rate;
904 $form->{"tax_$i"} = 0;
906 $form->{"tax_$i"} = $form->round_amount($form->{"tax_$i"}, 2);
908 $totaltax += $form->{"tax_$i"};
909 map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
913 $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
915 map { $form->{invtotal} += $form->{"amount_$_"} } (1 .. $form->{rowcount});
917 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{transdate}, 'sell');
918 $form->{exchangerate} = $form->{forex} if $form->{forex};
920 $form->{invdate} = $form->{transdate};
921 my %saved_variables = map +( $_ => $form->{$_} ), qw(AP AP_amount_1 taxchart_1);
923 my $vendor_changed = &check_name("vendor");
925 $form->{AP} = $saved_variables{AP};
926 if ($saved_variables{AP_amount_1} =~ m/.--./) {
927 map { $form->{$_} = $saved_variables{$_} } qw(AP_amount_1 taxchart_1);
929 delete $form->{taxchart_1};
932 $form->{rowcount} = $count + 1;
935 ($form->{taxincluded}) ? $form->{invtotal} : $form->{invtotal} + $totaltax;
938 for my $i (1 .. $form->{paidaccounts}) {
939 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
942 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
943 } qw(paid exchangerate);
945 $totalpaid += $form->{"paid_$i"};
947 $form->{"forex_$i"} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'sell');
948 $form->{"exchangerate_$i"} = $form->{"forex_$i"} if $form->{"forex_$i"};
952 $form->{creditremaining} -=
953 ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} -
954 $form->{oldinvtotal});
955 $form->{oldinvtotal} = $form->{invtotal};
956 $form->{oldtotalpaid} = $totalpaid;
959 $form->{notes} = $form->{intnotes} if $vendor_changed;
963 $main::lxdebug->leave_sub();
968 $main::lxdebug->enter_sub();
970 my $form = $main::form;
971 my %myconfig = %main::myconfig;
972 my $locale = $main::locale;
974 $main::auth->assert('general_ledger');
976 $form->{defaultcurrency} = $form->get_default_currency(\%myconfig);
979 for my $i (1 .. $form->{paidaccounts}) {
980 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
981 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
983 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
985 $form->error($locale->text('Cannot post payment for a closed period!'))
986 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
988 if ($form->{currency} ne $form->{defaultcurrency}) {
989 $form->{"exchangerate_$i"} = $form->{exchangerate}
990 if ($invdate == $datepaid);
991 $form->isblank("exchangerate_$i",
992 $locale->text('Exchangerate for payment missing!'));
997 ($form->{AP}) = split /--/, $form->{AP};
998 ($form->{AP_paid}) = split /--/, $form->{AP_paid};
999 $form->redirect($locale->text('Payment posted!'))
1000 if (AP->post_payment(\%myconfig, \%$form));
1001 $form->error($locale->text('Cannot post payment!'));
1004 $main::lxdebug->leave_sub();
1009 $main::lxdebug->enter_sub();
1011 my $form = $main::form;
1012 my %myconfig = %main::myconfig;
1013 my $locale = $main::locale;
1015 $main::auth->assert('general_ledger');
1017 # check if there is a vendor, invoice and due date
1018 $form->isblank("transdate", $locale->text("Invoice Date missing!"));
1019 $form->isblank("duedate", $locale->text("Due Date missing!"));
1020 $form->isblank("vendor", $locale->text('Vendor missing!'));
1022 my $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1023 my $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1024 $form->error($locale->text('Cannot post transaction for a closed period!')) if ($form->date_closed($form->{"transdate"}, \%myconfig));
1026 my $zero_amount_posting = 1;
1027 for my $i (1 .. $form->{rowcount}) {
1028 if ($form->parse_amount(\%myconfig, $form->{"amount_$i"})) {
1029 $zero_amount_posting = 0;
1034 $form->error($locale->text('Zero amount posting!')) if $zero_amount_posting;
1036 $form->isblank("exchangerate", $locale->text('Exchangerate missing!'))
1037 if ($form->{currency} ne $form->{defaultcurrency});
1038 delete($form->{AP});
1040 for my $i (1 .. $form->{paidaccounts}) {
1041 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
1042 my $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
1044 $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1046 $form->error($locale->text('Cannot post payment for a closed period!'))
1047 if ($form->date_closed($form->{"datepaid_$i"}, \%myconfig));
1049 if ($form->{currency} ne $form->{defaultcurrency}) {
1050 $form->{"exchangerate_$i"} = $form->{exchangerate}
1051 if ($transdate == $datepaid);
1052 $form->isblank("exchangerate_$i",
1053 $locale->text('Exchangerate for payment missing!'));
1059 # if old vendor ne vendor redo form
1060 my ($vendor) = split /--/, $form->{vendor};
1061 if ($form->{oldvendor} ne "$vendor--$form->{vendor_id}") {
1065 my ($debitaccno, $debittaxkey) = split /--/, $form->{AP_amountselected};
1066 my ($taxkey, $NULL) = split /--/, $form->{taxchartselected};
1067 my ($payablesaccno, $payablestaxkey) = split /--/, $form->{APselected};
1068 # $form->{AP_amount_1} = $debitaccno;
1069 $form->{AP_payables} = $payablesaccno;
1070 $form->{taxkey} = $taxkey;
1071 $form->{storno} = 0;
1073 $form->{id} = 0 if $form->{postasnew};
1075 if (AP->post_transaction(\%myconfig, \%$form)) {
1076 # saving the history
1077 if(!exists $form->{addition} && $form->{id} ne "") {
1078 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1079 $form->{addition} = "POSTED";
1080 $form->save_history($form->dbconnect(\%myconfig));
1082 # /saving the history
1083 remove_draft() if $form->{remove_draft};
1084 # Dieser Text wird niemals ausgegeben: Probleme beim redirect?
1085 $form->redirect($locale->text('Transaction posted!'));
1087 $form->error($locale->text('Cannot post transaction!'));
1090 $main::lxdebug->leave_sub();
1094 $main::lxdebug->enter_sub();
1096 my $form = $main::form;
1097 my %myconfig = %main::myconfig;
1099 $main::auth->assert('general_ledger');
1101 $form->{postasnew} = 1;
1102 # saving the history
1103 if(!exists $form->{addition} && $form->{id} ne "") {
1104 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1105 $form->{addition} = "POSTED AS NEW";
1106 $form->save_history($form->dbconnect(\%myconfig));
1108 # /saving the history
1111 $main::lxdebug->leave_sub();
1114 sub use_as_template {
1115 $main::lxdebug->enter_sub();
1117 my $form = $main::form;
1118 my %myconfig = %main::myconfig;
1120 $main::auth->assert('general_ledger');
1122 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);
1123 $form->{paidaccounts} = 1;
1124 $form->{rowcount}--;
1125 $form->{invdate} = $form->current_date(\%myconfig);
1128 $main::lxdebug->leave_sub();
1132 $main::lxdebug->enter_sub();
1134 my $form = $main::form;
1135 my $locale = $main::locale;
1137 $main::auth->assert('general_ledger');
1139 $form->{title} = $locale->text('Confirm!');
1143 delete $form->{header};
1148 <form method=post action=$form->{script}>
1151 foreach my $key (keys %$form) {
1152 next if (($key eq 'login') || ($key eq 'password') || ('' ne ref $form->{$key}));
1153 $form->{$key} =~ s/\"/"/g;
1154 print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
1158 <h2 class=confirm>$form->{title}</h2>
1161 . $locale->text('Are you sure you want to delete Transaction')
1162 . qq| $form->{invnumber}</h4>
1164 <input name=action class=submit type=submit value="|
1165 . $locale->text('Yes') . qq|">
1172 $main::lxdebug->leave_sub();
1176 $main::lxdebug->enter_sub();
1178 my $form = $main::form;
1179 my %myconfig = %main::myconfig;
1180 my $locale = $main::locale;
1182 $main::auth->assert('general_ledger');
1184 if (AP->delete_transaction(\%myconfig, \%$form, $main::spool)) {
1185 # saving the history
1186 if(!exists $form->{addition}) {
1187 $form->{snumbers} = qq|invnumber_| . $form->{invnumber};
1188 $form->{addition} = "DELETED";
1189 $form->save_history($form->dbconnect(\%myconfig));
1191 # /saving the history
1192 $form->redirect($locale->text('Transaction deleted!'));
1194 $form->error($locale->text('Cannot delete transaction!'));
1196 $main::lxdebug->leave_sub();
1200 $main::lxdebug->enter_sub();
1202 $main::auth->assert('general_ledger | invoice_edit');
1204 my $form = $main::form;
1205 my %myconfig = %main::myconfig;
1206 my $locale = $main::locale;
1208 # setup customer selection
1209 $form->all_vc(\%myconfig, "vendor", "AP");
1211 $form->{title} = $locale->text('AP Transactions');
1212 $form->{fokus} = "search.vendor";
1213 $form->{jsscript} = 1;
1215 $form->get_lists("projects" => { "key" => "ALL_PROJECTS", "all" => 1 },
1216 "departments" => "ALL_DEPARTMENTS",
1217 "vendors" => "ALL_VC");
1219 # constants and subs for template
1220 $form->{jsscript} = 1;
1221 $form->{vc_keys} = sub { "$_[0]->{name}--$_[0]->{id}" };
1224 print $form->parse_html_template('ap/search', { %myconfig });
1226 $main::lxdebug->leave_sub();
1229 sub create_subtotal_row {
1230 $main::lxdebug->enter_sub();
1232 my ($totals, $columns, $column_alignment, $subtotal_columns, $class) = @_;
1234 my $form = $main::form;
1235 my %myconfig = %main::myconfig;
1237 my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
1239 map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } @{ $subtotal_columns };
1241 $row->{tax}->{data} = $form->format_amount(\%myconfig, $totals->{amount} - $totals->{netamount}, 2);
1243 map { $totals->{$_} = 0 } @{ $subtotal_columns };
1245 $main::lxdebug->leave_sub();
1250 sub ap_transactions {
1251 $main::lxdebug->enter_sub();
1253 my $form = $main::form;
1254 my %myconfig = %main::myconfig;
1255 my $locale = $main::locale;
1257 $main::auth->assert('general_ledger | vendor_invoice_edit');
1259 ($form->{vendor}, $form->{vendor_id}) = split(/--/, $form->{vendor});
1261 report_generator_set_default_sort('transdate', 1);
1263 AP->ap_transactions(\%myconfig, \%$form);
1265 $form->{title} = $locale->text('AP Transactions');
1267 my $report = SL::ReportGenerator->new(\%myconfig, $form);
1270 qw(transdate id type invnumber ordnumber name netamount tax amount paid datepaid
1271 due duedate transaction_description notes employee globalprojectnumber
1272 vendornumber country ustid taxzone payment_terms charts);
1274 my @hidden_variables = map { "l_${_}" } @columns;
1275 push @hidden_variables, "l_subtotal", qw(open closed vendor invnumber ordnumber transaction_description notes project_id transdatefrom transdateto);
1277 my $href = build_std_url('action=ap_transactions', grep { $form->{$_} } @hidden_variables);
1280 'transdate' => { 'text' => $locale->text('Date'), },
1281 'id' => { 'text' => $locale->text('ID'), },
1282 'type' => { 'text' => $locale->text('Type'), },
1283 'invnumber' => { 'text' => $locale->text('Invoice'), },
1284 'ordnumber' => { 'text' => $locale->text('Order'), },
1285 'name' => { 'text' => $locale->text('Vendor'), },
1286 'netamount' => { 'text' => $locale->text('Amount'), },
1287 'tax' => { 'text' => $locale->text('Tax'), },
1288 'amount' => { 'text' => $locale->text('Total'), },
1289 'paid' => { 'text' => $locale->text('Paid'), },
1290 'datepaid' => { 'text' => $locale->text('Date Paid'), },
1291 'due' => { 'text' => $locale->text('Amount Due'), },
1292 'duedate' => { 'text' => $locale->text('Due Date'), },
1293 'transaction_description' => { 'text' => $locale->text('Transaction description'), },
1294 'notes' => { 'text' => $locale->text('Notes'), },
1295 'employee' => { 'text' => $locale->text('Salesperson'), },
1296 'globalprojectnumber' => { 'text' => $locale->text('Project Number'), },
1297 'vendornumber' => { 'text' => $locale->text('Vendor Number'), },
1298 'country' => { 'text' => $locale->text('Country'), },
1299 'ustid' => { 'text' => $locale->text('USt-IdNr.'), },
1300 'taxzone' => { 'text' => $locale->text('Steuersatz'), },
1301 'payment_terms' => { 'text' => $locale->text('Payment Terms'), },
1302 'charts' => { 'text' => $locale->text('Buchungskonto'), },
1305 foreach my $name (qw(id transdate duedate invnumber ordnumber name datepaid employee shippingpoint shipvia transaction_description)) {
1306 my $sortdir = $form->{sort} eq $name ? 1 - $form->{sortdir} : $form->{sortdir};
1307 $column_defs{$name}->{link} = $href . "&sort=$name&sortdir=$sortdir";
1310 my %column_alignment = map { $_ => 'right' } qw(netamount tax amount paid due);
1312 $form->{"l_type"} = "Y";
1313 map { $column_defs{$_}->{visible} = $form->{"l_${_}"} ? 1 : 0 } @columns;
1315 $report->set_columns(%column_defs);
1316 $report->set_column_order(@columns);
1318 $report->set_export_options('ap_transactions', @hidden_variables, qw(sort sortdir));
1320 $report->set_sort_indicator($form->{sort}, $form->{sortdir});
1323 push @options, $locale->text('Vendor') . " : $form->{vendor}" if ($form->{vendor});
1324 push @options, $locale->text('Department') . " : " . (split /--/, $form->{department})[0] if ($form->{department});
1325 push @options, $locale->text('Invoice Number') . " : $form->{invnumber}" if ($form->{invnumber});
1326 push @options, $locale->text('Order Number') . " : $form->{ordnumber}" if ($form->{ordnumber});
1327 push @options, $locale->text('Notes') . " : $form->{notes}" if ($form->{notes});
1328 push @options, $locale->text('Transaction description') . " : $form->{transaction_description}" if ($form->{transaction_description});
1329 push @options, $locale->text('From') . " " . $locale->date(\%myconfig, $form->{transdatefrom}, 1) if ($form->{transdatefrom});
1330 push @options, $locale->text('Bis') . " " . $locale->date(\%myconfig, $form->{transdateto}, 1) if ($form->{transdateto});
1331 push @options, $locale->text('Open') if ($form->{open});
1332 push @options, $locale->text('Closed') if ($form->{closed});
1334 $report->set_options('top_info_text' => join("\n", @options),
1335 'raw_bottom_info_text' => $form->parse_html_template('ap/ap_transactions_bottom'),
1336 'output_format' => 'HTML',
1337 'title' => $form->{title},
1338 'attachment_basename' => $locale->text('vendor_invoice_list') . strftime('_%Y%m%d', localtime time),
1340 $report->set_options_from_form();
1342 # add sort and escape callback, this one we use for the add sub
1343 $form->{callback} = $href .= "&sort=$form->{sort}";
1345 # escape callback for href
1346 my $callback = $form->escape($href);
1348 my @subtotal_columns = qw(netamount amount paid due);
1350 my %totals = map { $_ => 0 } @subtotal_columns;
1351 my %subtotals = map { $_ => 0 } @subtotal_columns;
1355 foreach my $ap (@{ $form->{AP} }) {
1356 $ap->{tax} = $ap->{amount} - $ap->{netamount};
1357 $ap->{due} = $ap->{amount} - $ap->{paid};
1359 map { $subtotals{$_} += $ap->{$_};
1360 $totals{$_} += $ap->{$_} } @subtotal_columns;
1362 map { $ap->{$_} = $form->format_amount(\%myconfig, $ap->{$_}, 2) } qw(netamount tax amount paid due);
1364 my $is_storno = $ap->{storno} && $ap->{storno_id};
1365 my $has_storno = $ap->{storno} && !$ap->{storno_id};
1367 if ($ap->{invoice}) {
1369 $has_storno ? $locale->text("Invoice with Storno (abbreviation)")
1370 : $is_storno ? $locale->text("Storno (one letter abbreviation)")
1371 : $locale->text("Invoice (one letter abbreviation)");
1374 $has_storno ? $locale->text("AP Transaction with Storno (abbreviation)")
1375 : $is_storno ? $locale->text("AP Transaction Storno (one letter abbreviation)")
1376 : $locale->text("AP Transaction (abbreviation)");
1381 foreach my $column (@columns) {
1383 'data' => $ap->{$column},
1384 'align' => $column_alignment{$column},
1388 $row->{invnumber}->{link} = build_std_url("script=" . ($ap->{invoice} ? 'ir.pl' : 'ap.pl'), 'action=edit')
1389 . "&id=" . E($ap->{id}) . "&callback=${callback}";
1391 my $row_set = [ $row ];
1393 if (($form->{l_subtotal} eq 'Y')
1394 && (($idx == (scalar @{ $form->{AP} } - 1))
1395 || ($ap->{ $form->{sort} } ne $form->{AP}->[$idx + 1]->{ $form->{sort} }))) {
1396 push @{ $row_set }, create_subtotal_row(\%subtotals, \@columns, \%column_alignment, \@subtotal_columns, 'listsubtotal');
1399 $report->add_data($row_set);
1404 $report->add_separator();
1405 $report->add_data(create_subtotal_row(\%totals, \@columns, \%column_alignment, \@subtotal_columns, 'listtotal'));
1407 $report->generate_with_headers();
1409 $main::lxdebug->leave_sub();
1413 $main::lxdebug->enter_sub();
1415 my $form = $main::form;
1416 my %myconfig = %main::myconfig;
1417 my $locale = $main::locale;
1419 $main::auth->assert('general_ledger');
1421 if (IS->has_storno(\%myconfig, $form, 'ap')) {
1422 $form->{title} = $locale->text("Cancel Accounts Payables Transaction");
1423 $form->error($locale->text("Transaction has already been cancelled!"));
1426 AP->storno($form, \%myconfig, $form->{id});
1428 # saving the history
1429 if(!exists $form->{addition} && $form->{id} ne "") {
1430 $form->{snumbers} = "ordnumber_$form->{ordnumber}";
1431 $form->{addition} = "STORNO";
1432 $form->save_history($form->dbconnect(\%myconfig));
1434 # /saving the history
1436 $form->redirect(sprintf $locale->text("Transaction %d cancelled."), $form->{storno_id});
1438 $main::lxdebug->leave_sub();