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., 51 Franklin Street, Fifth Floor, Boston,
29 #======================================================================
33 #======================================================================
44 require "bin/mozilla/arap.pl";
45 require "bin/mozilla/common.pl";
47 our ($form, %myconfig, $lxdebug, $locale, $auth);
54 $lxdebug->enter_sub();
56 $auth->assert('cash');
60 $form->{ARAP} = ($form->{type} eq 'receipt') ? "AR" : "AP";
61 $form->{arap} = lc $form->{ARAP};
63 # setup customer/vendor selection for open invoices
64 if ($form->{all_vc}) {
65 # Dieser Zweig funktioniert derzeit NIE. Ggf. ganz raus oder
66 # alle offenen Zahlungen wieder korrekt anzeigen. jb 12.10.2010
67 $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
69 CP->get_openvc(\%myconfig, \%$form);
71 # Auswahlliste für vc zusammenbauen
72 # Erweiterung für schliessende option und erweiterung um value
73 # für bugfix 1771 (doppelte Leerzeichen werden nicht 'gepostet')
74 $form->{"select$form->{vc}"} = "";
76 if ($form->{"all_$form->{vc}"}) {
77 $form->{"select$form->{vc}"} .= "<option value=\"\"></option>\n";
79 $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
80 # hotfix for 2450. TODO remove legacy code and use L
81 map { $form->{"select$form->{vc}"} .= "<option value=\"" . H($_->{name}) . "--$_->{id}\">" . H($_->{name}) . "--$_->{id}</option>\n" }
82 @{ $form->{"all_$form->{vc}"} };
84 CP->paymentaccounts(\%myconfig, \%$form);
86 # Standard Konto für Umlaufvermögen
87 my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
88 # Entsprechend präventiv die Auswahlliste für Kontonummer
89 # auch mit value= zusammenbauen (s.a. oben bugfix 1771)
90 # Wichtig: Auch das Template anpassen, damit hidden input korrekt die "
92 $form->{selectaccount} = "";
93 $form->{"select$form->{ARAP}"} = "";
95 map { $form->{selectaccount} .= "<option value=\"$_->{accno}--$_->{description}\">$_->{accno}--$_->{description}</option>\n";
96 $form->{account} = "$_->{accno}--$_->{description}" if ($_->{accno} eq $accno_arap) } @{ $form->{PR}{"$form->{ARAP}_paid"} };
99 # oldcurrency ist zwar noch hier als fragment enthalten, wird aber bei
100 # der aktualisierung der form auch nicht mitübernommen. das konzept
101 # old_$FOO habe ich auch noch nicht verstanden ...
102 # Ok. Wenn currency übernommen werden, dann in callback-string über-
103 # geben und hier reinparsen, oder besser multibox oder html auslagern?
104 # Antwort: form->currency wird mit oldcurrency oder curr[0] überschrieben
105 # Wofür macht das Sinn?
106 @curr = $form->get_all_currencies();
107 $form->{defaultcurrency} = $form->{currency} = $form->{oldcurrency} =
108 $form->get_default_currency(\%myconfig);
110 # Entsprechend präventiv die Auswahlliste für Währungen
111 # auch mit value= zusammenbauen (s.a. oben bugfix 1771)
112 $form->{selectcurrency} = "";
113 map { $form->{selectcurrency} .= "<option value=\"$_\">$_</option>\n" } @curr;
119 $lxdebug->leave_sub();
125 $auth->assert('cash');
127 my ($vc, $arap, $exchangerate);
129 if ($form->{ $form->{vc} } eq "") {
130 map { $form->{"addr$_"} = "" } (1 .. 4);
133 # sometimes it happens that values in customer arrive without the signs '--'
134 # but in order to select the right option field we need values with '--'
135 if ($form->{vc} eq "customer" && $form->{"all_$form->{vc}"}){
136 my ($customername) = split /--/, $form->{ $form->{vc} };
137 $form->{ $form->{vc} } = $customername . "--" . $form->{customer_id};
140 # geändert von <option>asdf--2929
142 # <option value="asdf--2929">asdf--2929</option>
143 # offen: $form->{ARAP} kann raus?
144 for my $item ($form->{vc}, "account", "currency", $form->{ARAP}) {
145 $form->{$item} = H($form->{$item});
146 $form->{"select$item"} =~ s/ selected//;
147 $form->{"select$item"} =~ s/option value="\Q$form->{$item}\E">\Q$form->{$item}\E/option selected value="$form->{$item}">$form->{$item}/;
151 ($form->{"select$form->{vc}"})
152 ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>|
153 : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
155 $form->{openinvoices} = $form->{all_vc} ? "" : 1;
157 # $locale->text('AR')
158 # $locale->text('AP')
162 $arap = lc $form->{ARAP};
164 print $::form->parse_html_template('cp/form_header', {
165 is_customer => $form->{vc} eq 'customer',
166 is_receipt => $form->{type} eq 'receipt',
175 $::lxdebug->enter_sub;
176 $::auth->assert('cash');
178 my @columns = qw(amount due paid invnumber id transdate checked);
179 my (@invoices, %total);
180 for my $i (1 .. $::form->{rowcount}) {
181 push @invoices, +{ map { $_ => $::form->{"$_\_$i"} } @columns };
182 $total{$_} += $invoices[-1]{$_} = $::form->parse_amount(\%::myconfig, $invoices[-1]{$_}) for qw(amount due paid);
185 print $::form->parse_html_template('cp/invoices', {
186 invoices => \@invoices,
190 $::lxdebug->leave_sub;
194 $::lxdebug->enter_sub;
195 $::auth->assert('cash');
197 print $::form->parse_html_template('cp/form_footer');
199 $::lxdebug->leave_sub;
203 $lxdebug->enter_sub();
205 $auth->assert('cash');
207 my ($new_name_selected) = @_;
209 my ($buysell, $newvc, $updated, $exchangerate, $amount);
211 if ($form->{vc} eq 'customer') {
217 # if we switched to all_vc
218 # funktioniert derzeit nicht 12.10.2010 jb
219 if ($form->{all_vc} ne $form->{oldall_vc}) {
221 $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
223 $form->{"select$form->{vc}"} = "";
225 if ($form->{all_vc}) {
226 $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
228 if ($form->{"all_$form->{vc}"}) {
230 $form->{"select$form->{vc}"} .=
231 "<option>$_->{name}--$_->{id}\n"
232 } @{ $form->{"all_$form->{vc}"} };
234 } else { # ab hier wieder ausgeführter code (s.o.):
235 CP->get_openvc(\%myconfig, \%$form);
237 if ($form->{"all_$form->{vc}"}) {
239 qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
241 $form->{"select$form->{vc}"} .=
242 "<option>$_->{name}--$_->{id}\n"
243 } @{ $form->{"all_$form->{vc}"} };
246 # if the name is not the same
247 if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
248 $form->{ $form->{vc} } = $newvc;
253 # search by customernumber
254 # the customernumber has to be correct otherwise nothing is found
255 if ($form->{vc} eq 'customer' and $form->{customernumber} and $form->{ARAP} eq 'AR') {
256 $form->{open} ='Y'; # only open invoices
257 # ar_transactions automatically searches by $form->{customer_id} or else
258 # $form->{customer} if available, and these variables will always be set
259 # so we have to empty these values first
260 $form->{customer_id} = '';
261 $form->{customer} = '';
262 AR->ar_transactions(\%myconfig, \%$form);
264 # Here we just take the first returned value even if the custumernumber
266 $form->{customer} = $form->{AR}[0]{name};
267 $form->{customer_id} = $form->{AR}[0]{customer_id};
270 # search by invoicenumber,
271 if ($form->{invnumber}) {
272 $form->{open} ='Y'; # only open invoices
273 if ($form->{ARAP} eq 'AR'){
274 # ar_transactions automatically searches by $form->{customer_id} or else
275 # $form->{customer} if available, and these variables will always be set
276 # so we have to empty these values first
277 $form->{customer_id} = '';
278 $form->{customer} = '';
279 AR->ar_transactions(\%myconfig, \%$form);
281 # if you search for invoice '11' ar_transactions will also match invoices
282 # 112, 211, ... due to the LIKE
284 # so there is now an extra loop that tries to match the invoice number
285 # exactly among all returned results, and then passes the customer_id instead of the name
286 # because the name may not be unique
288 my $found_exact_invnumber_match = 0;
289 foreach my $i ( @{ $form->{AR} } ) {
290 next unless $i->{invnumber} eq $form->{invnumber};
291 # found exactly matching invnumber
292 $form->{$form->{vc}} = $i->{name};
293 $form->{customer_id} = $i->{customer_id};
294 #$form->{"old${form->{vc}"} = $i->{customer_id};
295 $found_exact_invnumber_match = 1;
298 unless ( $found_exact_invnumber_match ) {
299 # use first returned entry, may not be the correct one if invnumber doesn't
301 $form->{$form->{vc}} = $form->{AR}[0]{name};
302 $form->{customer_id} = $form->{AR}[0]{customer_id};
305 # s.o. nur für zahlungsausgang
306 AP->ap_transactions(\%myconfig, \%$form);
307 $form->{$form->{vc}} = $form->{AP}[0]{name};
311 # determine customer/vendor
312 if ( $form->{customer_id} and ($form->{invnumber} or $form->{customernumber}) ) {
313 # we already know the exact customer_id, so fill $form with customer data
314 IS->get_customer(\%myconfig, \%$form);
317 # check_name is called with "customer" or "vendor" and otherwise uses contents of $form
318 # check_name also runs get_customer/get_vendor
319 $updated = &check_name($form->{vc});
322 if ($new_name_selected || $updated) {
323 # get open invoices from ar/ap using $form->{vc} and a.${vc}_id, i.e. customer_id
324 CP->get_openinvoices(\%myconfig, \%$form);
325 ($newvc) = split /--/, $form->{ $form->{vc} };
326 $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
330 if ($form->{currency} ne $form->{oldcurrency}) {
331 $form->{oldcurrency} = $form->{currency};
333 CP->get_openinvoices(\%myconfig, \%$form);
338 if (!$form->{forex}) { # read exchangerate from input field (not hidden)
339 $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
341 $form->{forex} = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{datepaid}, $buysell);
342 $form->{exchangerate} = $form->{forex} if $form->{forex};
344 $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
347 $form->{rowcount} = 0;
349 $form->{queued} = "";
352 foreach my $ref (@{ $form->{PR} }) {
354 $form->{"id_$i"} = $ref->{id};
355 $form->{"invnumber_$i"} = $ref->{invnumber};
356 $form->{"transdate_$i"} = $ref->{transdate};
357 $ref->{exchangerate} = 1 unless $ref->{exchangerate};
358 $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
360 ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
361 $form->{"checked_$i"} = "";
362 $form->{"paid_$i"} = "";
367 $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
371 $form->{rowcount} = $i;
376 # Modified from $amount = $form->{amount} by J.Zach to update amount to total
377 # payment amount in Zahlungsausgang
379 for my $i (1 .. $form->{rowcount}) {
383 $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
384 } qw(amount due paid);
386 if ($form->{"checked_$i"}) {
389 if (!$form->{"paid_$i"}) {
390 $form->{"paid_$i"} = $form->{"due_$i"};
393 # Modified by J.Zach, see abovev
394 $amount += $form->{"paid_$i"};
397 $form->{"paid_$i"} = "";
402 $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
403 } qw(amount due paid);
407 # Line added by J.Zach, see above
408 $form->{amount}=$amount;
414 $lxdebug->leave_sub();
418 $lxdebug->enter_sub();
420 $auth->assert('cash');
424 if ($form->{currency} ne $form->{defaultcurrency}) {
425 $form->error($locale->text('Exchangerate missing!'))
426 unless $form->{exchangerate};
429 # Beim Aktualisieren wird das Konto übernommen
430 # und jetzt auch Beleg und Datum
431 $form->{callback} = "cp.pl?action=payment&vc=$form->{vc}&type=$form->{type}&account=$form->{account}&$form->{currency}" .
432 "&datepaid=$form->{datepaid}&source=$form->{source}";
434 my $msg1 = $::form->{type} eq 'receipt' ? $::locale->text("Receipt posted!") : $::locale->text("Payment posted!");
435 my $msg2 = $::form->{type} eq 'receipt' ? $::locale->text("Cannot post Receipt!") : $::locale->text("Cannot post Payment!");
437 # Die Nachrichten (Receipt posted!) werden nicht angezeigt.
438 # Entweder wieder aktivieren oder komplett rausnehmen
439 $form->redirect($msg1) if (CP->process_payment(\%::myconfig, $::form));
442 $lxdebug->leave_sub();
446 $lxdebug->enter_sub();
448 $auth->assert('cash');
450 my ($closedto, $datepaid, $amount);
452 &check_name($form->{vc});
454 if ($form->{currency} ne $form->{oldcurrency}) {
456 $::dispatcher->end_request;
458 $form->error($locale->text('Date missing!')) unless $form->{datepaid};
459 my $selected_check = 1;
460 for my $i (1 .. $form->{rowcount}) {
461 next unless $form->{"checked_$i"};
462 if (abs($form->parse_amount(\%myconfig, $form->{"paid_$i"}, 2)) < 0.01) {
463 $form->error($locale->text('Row #1: amount has to be different from zero.', $i));
465 undef $selected_check;
467 $form->error($locale->text('No transaction selected!')) if $selected_check;
469 $closedto = $form->datetonum($form->{closedto}, \%myconfig);
470 $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
472 $form->error($locale->text('Cannot process payment for a closed period!'))
473 if ($form->date_closed($form->{"datepaid"}, \%myconfig));
475 $amount = $form->parse_amount(\%myconfig, $form->{amount});
476 $form->{amount} = $amount;
478 for my $i (1 .. $form->{rowcount}) {
479 if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
480 $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
482 push(@{ $form->{paid} ||= [] }, $form->{"paid_$i"});
483 push(@{ $form->{due} ||= [] }, $form->{"due_$i"});
484 push(@{ $form->{invnumber} ||= [] }, $form->{"invnumber_$i"});
485 push(@{ $form->{invdate} ||= [] }, $form->{"transdate_$i"});
489 if ($form->round_amount($amount, 2) != 0) {
490 push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
491 push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
492 push(@{ $form->{invnumber} },
493 ($form->{ARAP} eq 'AR')
494 ? $locale->text('Deposit')
495 : $locale->text('Prepayment'));
496 push(@{ $form->{invdate} }, $form->{datepaid});
499 $lxdebug->leave_sub();