Zahlungseingang: Feld für Kunden/Lieferanten die Umschaltfunktionalität wieder herges...
[kivitendo-erp.git] / bin / mozilla / cp.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 2002
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
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.
20 #
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 #======================================================================
29 #
30 # Payment module
31 #
32 #======================================================================
33
34 use SL::CP;
35 use SL::IS;
36 use SL::IR;
37 use SL::AR;
38 use SL::AP;
39 use Data::Dumper;
40 use strict;
41 #use warnings;
42
43 require "bin/mozilla/arap.pl";
44 require "bin/mozilla/common.pl";
45
46 our ($form, %myconfig, $lxdebug, $locale, $auth);
47
48 1;
49
50 # end of main
51
52 sub payment {
53   $lxdebug->enter_sub();
54
55   $auth->assert('cash');
56
57   my (@curr);
58
59   $form->{ARAP} = ($form->{type} eq 'receipt') ? "AR" : "AP";
60   $form->{arap} = lc $form->{ARAP};
61
62   # setup customer/vendor selection for open invoices
63   if ($form->{all_vc}) {
64     # Dieser Zweig funktioniert derzeit NIE. Ggf. ganz raus oder
65     # alle offenen Zahlungen wieder korrekt anzeigen. jb 12.10.2010
66     $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
67   } else {
68     CP->get_openvc(\%myconfig, \%$form);
69   }
70   # Auswahlliste für vc zusammenbauen
71   # Erweiterung für schliessende option und erweiterung um value
72   # für bugfix 1771 (doppelte Leerzeichen werden nicht 'gepostet')
73   $form->{"select$form->{vc}"} = "";
74
75   if ($form->{"all_$form->{vc}"}) {
76         $form->{"select$form->{vc}"} .= "<option value=\"\"></option>\n";
77     # s.o. jb 12.10.2010
78     $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
79     map { $form->{"select$form->{vc}"} .= "<option value=\"$_->{name}--$_->{id}\">$_->{name}--$_->{id}</option>\n" }
80       @{ $form->{"all_$form->{vc}"} };
81   }
82   CP->paymentaccounts(\%myconfig, \%$form);
83
84   # Standard Konto für Umlaufvermögen
85   my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
86   # Entsprechend präventiv die Auswahlliste für Kontonummer
87   # auch mit value= zusammenbauen (s.a. oben bugfix 1771)
88   # Wichtig: Auch das Template anpassen, damit hidden input korrekt die "
89   # escaped.
90   $form->{selectaccount} = "";
91   $form->{"select$form->{ARAP}"} = "";
92
93   map { $form->{selectaccount} .= "<option value=\"$_->{accno}--$_->{description}\">$_->{accno}--$_->{description}</option>\n";
94         $form->{account}        = "$_->{accno}--$_->{description}" if ($_->{accno} eq $accno_arap) } @{ $form->{PR}{"$form->{ARAP}_paid"} };
95
96   # Braucht man das hier überhaupt? Erstmal auskommentieren .. jan 18.12.2010
97   #  map {
98   #    $form->{"select$form->{ARAP}"} .=
99   #      "<option>$_->{accno}--$_->{description}\n"
100   #  } @{ $form->{PR}{ $form->{ARAP} } };
101   # ENDE LOESCHMICH in 2012
102
103   # currencies
104   # oldcurrency ist zwar noch hier als fragment enthalten, wird aber bei
105   # der aktualisierung der form auch nicht mitübernommen. das konzept
106   # old_$FOO habe ich auch noch nicht verstanden ...
107   # Ok. Wenn currency übernommen werden, dann in callback-string über-
108   # geben und hier reinparsen, oder besser multibox oder html auslagern?
109   # Antwort: form->currency wird mit oldcurrency oder curr[0] überschrieben
110   # Wofür macht das Sinn?
111   @curr = $form->get_all_currencies();
112   $form->{defaultcurrency} = $form->{currency} = $form->{oldcurrency} =
113     $form->get_default_currency(\%myconfig);
114
115   # Entsprechend präventiv die Auswahlliste für Währungen
116   # auch mit value= zusammenbauen (s.a. oben bugfix 1771)
117   $form->{selectcurrency} = "";
118   map { $form->{selectcurrency} .= "<option value=\"$_\">$_</option>\n" } @curr;
119
120
121   &form_header;
122   &form_footer;
123
124   $lxdebug->leave_sub();
125 }
126
127 sub form_header {
128   $lxdebug->enter_sub;
129
130   $auth->assert('cash');
131
132   my ($vc, $arap, $exchangerate);
133
134   if ($form->{ $form->{vc} } eq "") {
135     map { $form->{"addr$_"} = "" } (1 .. 4);
136   }
137
138   # sometimes it happens that values in customer arrive without the signs '--'
139   # but in order to select the right option field we need values with '--'
140   if ($form->{vc} eq "customer"){
141     my ($customername) = split /--/, $form->{ $form->{vc} };
142     $form->{ $form->{vc} } = $customername . "--" . $form->{customer_id};
143   }
144   # bugfix 1771
145   # geändert von <option>asdf--2929
146   # nach:
147   #              <option value="asdf--2929">asdf--2929</option>
148   # offen: $form->{ARAP} kann raus?
149   for my $item ($form->{vc}, "account", "currency", $form->{ARAP}) {
150     $form->{"select$item"} =~ s/ selected//;
151     $form->{"select$item"} =~ s/option value="\Q$form->{$item}\E">\Q$form->{$item}\E/option selected value="$form->{$item}">$form->{$item}/;
152   }
153
154   $vc =
155     ($form->{"select$form->{vc}"})
156     ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>|
157     : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
158
159   $form->{openinvoices} = $form->{all_vc} ? "" : 1;
160
161   # $locale->text('AR')
162   # $locale->text('AP')
163
164   $form->header;
165
166   $arap = lc $form->{ARAP};
167
168   print $::form->parse_html_template('cp/form_header', {
169     is_customer => $form->{vc}   eq 'customer',
170     is_receipt  => $form->{type} eq 'receipt',
171     arap        => $arap,
172     vccontent   => $vc,
173   });
174
175   $lxdebug->leave_sub;
176 }
177
178 sub list_invoices {
179   $::lxdebug->enter_sub;
180   $::auth->assert('cash');
181
182   my @columns = qw(amount due paid invnumber id transdate checked);
183   my (@invoices, %total);
184   for my $i (1 .. $::form->{rowcount}) {
185     push @invoices, +{ map { $_ => $::form->{"$_\_$i"} } @columns };
186     $total{$_} += $invoices[-1]{$_} = $::form->parse_amount(\%::myconfig, $invoices[-1]{$_}) for qw(amount due paid);
187   }
188
189   print $::form->parse_html_template('cp/invoices', {
190     invoices => \@invoices,
191     totals   => \%total,
192   });
193
194   $::lxdebug->leave_sub;
195 }
196
197 sub form_footer {
198   $::lxdebug->enter_sub;
199   $::auth->assert('cash');
200
201   print $::form->parse_html_template('cp/form_footer');
202
203   $::lxdebug->leave_sub;
204 }
205
206 sub update {
207   $lxdebug->enter_sub();
208
209   $auth->assert('cash');
210
211   my ($new_name_selected) = @_;
212
213   my ($buysell, $newvc, $updated, $exchangerate, $amount);
214
215   if ($form->{vc} eq 'customer') {
216     $buysell = "buy";
217   } else {
218     $buysell = "sell";
219   }
220
221   # if we switched to all_vc
222   # funktioniert derzeit nicht 12.10.2010 jb
223   if ($form->{all_vc} ne $form->{oldall_vc}) {
224
225     $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
226
227     $form->{"select$form->{vc}"} = "";
228
229     if ($form->{all_vc}) {
230       $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
231
232       if ($form->{"all_$form->{vc}"}) {
233         map {
234           $form->{"select$form->{vc}"} .=
235             "<option>$_->{name}--$_->{id}\n"
236         } @{ $form->{"all_$form->{vc}"} };
237       }
238     } else {  # ab hier wieder ausgeführter code (s.o.):
239       CP->get_openvc(\%myconfig, \%$form);
240
241       if ($form->{"all_$form->{vc}"}) {
242         $newvc =
243           qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
244         map {
245           $form->{"select$form->{vc}"} .=
246             "<option>$_->{name}--$_->{id}\n"
247         } @{ $form->{"all_$form->{vc}"} };
248       }
249
250       # if the name is not the same
251       if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
252         $form->{ $form->{vc} } = $newvc;
253       }
254     }
255   }
256
257   # search by customernumber
258   # the customernumber has to be correct otherwise nothing is found
259   if ($form->{vc} eq 'customer' and $form->{customernumber} and $form->{ARAP} eq 'AR') {
260     $form->{open} ='Y'; # only open invoices
261     # ar_transactions automatically searches by $form->{customer_id} or else
262     # $form->{customer} if available, and these variables will always be set
263     # so we have to empty these values first
264     $form->{customer_id} = '';
265     $form->{customer} = '';
266     AR->ar_transactions(\%myconfig, \%$form);
267
268     # Here we just take the first returned value even if the custumernumber
269     # may not be unique
270     $form->{customer} = $form->{AR}[0]{name};
271     $form->{customer_id} = $form->{AR}[0]{customer_id};
272   }
273
274   # search by invoicenumber,
275   if ($form->{invnumber}) {
276     $form->{open} ='Y'; # only open invoices
277     if ($form->{ARAP} eq 'AR'){
278       # ar_transactions automatically searches by $form->{customer_id} or else
279       # $form->{customer} if available, and these variables will always be set
280       # so we have to empty these values first
281       $form->{customer_id} = '';
282       $form->{customer} = '';
283       AR->ar_transactions(\%myconfig, \%$form);
284
285       # if you search for invoice '11' ar_transactions will also match invoices
286       # 112, 211, ... due to the LIKE
287
288       # so there is now an extra loop that tries to match the invoice number
289       # exactly among all returned results, and then passes the customer_id instead of the name
290       # because the name may not be unique
291
292       my $found_exact_invnumber_match = 0;
293       foreach my $i ( @{ $form->{AR} } ) {
294         next unless $i->{invnumber} eq $form->{invnumber};
295         # found exactly matching invnumber
296         $form->{$form->{vc}} = $i->{name};
297         $form->{customer_id} = $i->{customer_id};
298         #$form->{"old${form->{vc}"} = $i->{customer_id};
299         $found_exact_invnumber_match = 1;
300       };
301
302       unless ( $found_exact_invnumber_match ) {
303         # use first returned entry, may not be the correct one if invnumber doesn't
304         # match uniquely
305         $form->{$form->{vc}} = $form->{AR}[0]{name};
306         $form->{customer_id} = $form->{AR}[0]{customer_id};
307       };
308     } else {
309       # s.o. nur für zahlungsausgang
310       AP->ap_transactions(\%myconfig, \%$form);
311       $form->{$form->{vc}} = $form->{AP}[0]{name};
312     }
313   }
314
315   # determine customer/vendor
316   if ( $form->{customer_id} and ($form->{invnumber} or $form->{customernumber}) ) {
317     # we already know the exact customer_id, so fill $form with customer data
318     IS->get_customer(\%myconfig, \%$form);
319     $updated = 1;
320   } else {
321     # check_name is called with "customer" or "vendor" and otherwise uses contents of $form
322     # check_name also runs get_customer/get_vendor
323     $updated = &check_name($form->{vc});
324   };
325
326   # if ($new_name_selected || $updated) {
327     # get open invoices from ar/ap using $form->{vc} and a.${vc}_id, i.e. customer_id
328     CP->get_openinvoices(\%myconfig, \%$form);
329     ($newvc) = split /--/, $form->{ $form->{vc} };
330     $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
331     $updated = 1;
332   # }
333
334   if ($form->{currency} ne $form->{oldcurrency}) {
335     $form->{oldcurrency} = $form->{currency};
336     if (!$updated) {
337       CP->get_openinvoices(\%myconfig, \%$form);
338       $updated = 1;
339     }
340   }
341
342   if (!$form->{forex}) {        # read exchangerate from input field (not hidden)
343     $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
344   }
345   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{datepaid}, $buysell);
346   $form->{exchangerate} = $form->{forex} if $form->{forex};
347
348   $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
349
350   if ($updated) {
351     $form->{rowcount} = 0;
352
353     $form->{queued} = "";
354
355     my $i = 0;
356     foreach my $ref (@{ $form->{PR} }) {
357       $i++;
358       $form->{"id_$i"}        = $ref->{id};
359       $form->{"invnumber_$i"} = $ref->{invnumber};
360       $form->{"transdate_$i"} = $ref->{transdate};
361       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
362       $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
363       $form->{"due_$i"}    =
364         ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
365       $form->{"checked_$i"} = "";
366       $form->{"paid_$i"}    = "";
367
368       # need to format
369       map {
370         $form->{"${_}_$i"} =
371           $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
372       } qw(amount due);
373
374     }
375     $form->{rowcount} = $i;
376   }
377
378   # recalculate
379
380   # Modified from $amount = $form->{amount} by J.Zach to update amount to total
381   # payment amount in Zahlungsausgang
382   $amount = 0;
383   for my $i (1 .. $form->{rowcount}) {
384
385     map {
386       $form->{"${_}_$i"} =
387         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
388     } qw(amount due paid);
389
390     if ($form->{"checked_$i"}) {
391
392       # calculate paid_$i
393       if (!$form->{"paid_$i"}) {
394         $form->{"paid_$i"} = $form->{"due_$i"};
395       }
396
397       # Modified by J.Zach, see abovev
398       $amount += $form->{"paid_$i"};
399
400     } else {
401       $form->{"paid_$i"} = "";
402     }
403
404     map {
405       $form->{"${_}_$i"} =
406         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
407     } qw(amount due paid);
408
409   }
410
411   # Line added by J.Zach, see above
412   $form->{amount}=$amount;
413
414   &form_header;
415   &list_invoices;
416   &form_footer;
417
418   $lxdebug->leave_sub();
419 }
420
421 sub post {
422   $lxdebug->enter_sub();
423
424   $auth->assert('cash');
425
426   &check_form;
427
428   if ($form->{currency} ne $form->{defaultcurrency}) {
429     $form->error($locale->text('Exchangerate missing!'))
430       unless $form->{exchangerate};
431   }
432
433   # Beim Aktualisieren wird das Konto übernommen
434   # und jetzt auch Beleg und Datum
435   $form->{callback} = "cp.pl?action=payment&vc=$form->{vc}&type=$form->{type}&account=$form->{account}&$form->{currency}" .
436                       "&datepaid=$form->{datepaid}&source=$form->{source}";
437
438   my $msg1 = $::form->{type} eq 'receipt' ? $::locale->text("Receipt posted!") : $::locale->text("Payment posted!");
439   my $msg2 = $::form->{type} eq 'receipt' ? $::locale->text("Cannot post Receipt!") : $::locale->text("Cannot post Payment!");
440
441   # Die Nachrichten (Receipt posted!) werden nicht angezeigt.
442   # Entweder wieder aktivieren oder komplett rausnehmen
443   $form->redirect($msg1) if (CP->process_payment(\%::myconfig, $::form));
444   $form->error($msg2);
445
446   $lxdebug->leave_sub();
447 }
448
449 sub check_form {
450   $lxdebug->enter_sub();
451
452   $auth->assert('cash');
453
454   my ($closedto, $datepaid, $amount);
455
456   &check_name($form->{vc});
457
458   if ($form->{currency} ne $form->{oldcurrency}) {
459     &update;
460     ::end_of_request();
461   }
462   $form->error($locale->text('Date missing!')) unless $form->{datepaid};
463   my $selected_check = 1;
464   for my $i (1 .. $form->{rowcount}) {
465     next unless $form->{"checked_$i"};
466     if (abs($form->parse_amount(\%myconfig, $form->{"paid_$i"}, 2)) < 0.01) {
467       $form->error($locale->text('Row #1: amount has to be different from zero.', $i));
468     }
469     undef $selected_check;
470   }
471   $form->error($locale->text('No transaction selected!')) if $selected_check;
472
473   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
474   $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
475
476   $form->error($locale->text('Cannot process payment for a closed period!'))
477     if ($form->date_closed($form->{"datepaid"}, \%myconfig));
478
479   $amount = $form->parse_amount(\%myconfig, $form->{amount});
480   $form->{amount} = $amount;
481
482   for my $i (1 .. $form->{rowcount}) {
483     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
484       $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
485
486       push(@{ $form->{paid}      ||= [] }, $form->{"paid_$i"});
487       push(@{ $form->{due}       ||= [] }, $form->{"due_$i"});
488       push(@{ $form->{invnumber} ||= [] }, $form->{"invnumber_$i"});
489       push(@{ $form->{invdate}   ||= [] }, $form->{"transdate_$i"});
490     }
491   }
492
493   if ($form->round_amount($amount, 2) != 0) {
494     push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
495     push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
496     push(@{ $form->{invnumber} },
497          ($form->{ARAP} eq 'AR')
498          ? $locale->text('Deposit')
499          : $locale->text('Prepayment'));
500     push(@{ $form->{invdate} }, $form->{datepaid});
501   }
502
503   $lxdebug->leave_sub();
504 }