ea7e4308414717ff1ee72c20cb6bf9c9e82f2a29
[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 strict;
40 #use warnings;
41
42 require "bin/mozilla/arap.pl";
43 require "bin/mozilla/common.pl";
44
45 our ($form, %myconfig, $lxdebug, $locale, $auth);
46
47 1;
48
49 # end of main
50
51 sub payment {
52   $lxdebug->enter_sub();
53
54   $auth->assert('cash');
55
56   my (@curr);
57
58   $form->{ARAP} = ($form->{type} eq 'receipt') ? "AR" : "AP";
59   $form->{arap} = lc $form->{ARAP};
60
61   # setup customer/vendor selection for open invoices
62   if ($form->{all_vc}) {
63     # Dieser Zweig funktioniert derzeit NIE. Ggf. ganz raus oder
64     # alle offenen Zahlungen wieder korrekt anzeigen. jb 12.10.2010
65     $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
66   } else {
67     CP->get_openvc(\%myconfig, \%$form);
68   }
69
70   $form->{"select$form->{vc}"} = "";
71
72   if ($form->{"all_$form->{vc}"}) {
73     # s.o. jb 12.10.2010
74     $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
75     map { $form->{"select$form->{vc}"} .= "<option>$_->{name}--$_->{id}\n" }
76       @{ $form->{"all_$form->{vc}"} };
77   }
78
79   CP->paymentaccounts(\%myconfig, \%$form);
80
81   # Standard Konto für Umlaufvermögen
82   my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
83
84   $form->{selectaccount} = "";
85   $form->{"select$form->{ARAP}"} = "";
86
87   map { $form->{selectaccount} .= "<option>$_->{accno}--$_->{description}\n";
88         $form->{account}        = "$_->{accno}--$_->{description}" if ($_->{accno} eq $accno_arap) } @{ $form->{PR}{"$form->{ARAP}_paid"} };
89
90   # Braucht man das hier überhaupt? Erstmal auskommentieren .. jan 18.12.2010
91   #  map {
92   #    $form->{"select$form->{ARAP}"} .=
93   #      "<option>$_->{accno}--$_->{description}\n"
94   #  } @{ $form->{PR}{ $form->{ARAP} } };
95   # ENDE LOESCHMICH in 2012
96
97   # currencies
98   # oldcurrency ist zwar noch hier als fragment enthalten, wird aber bei
99   # der aktualisierung der form auch nicht mitübernommen. das konzept
100   # old_$FOO habe ich auch noch nicht verstanden ...
101   # Ok. Wenn currency übernommen werden, dann in callback-string über-
102   # geben und hier reinparsen, oder besser multibox oder html auslagern?
103   # Antwort: form->currency wird mit oldcurrency oder curr[0] überschrieben
104   # Wofür macht das Sinn?
105   @curr = split(/:/, $form->{currencies});
106   chomp $curr[0];
107   $form->{defaultcurrency} = $form->{currency} = $form->{oldcurrency} =
108     $curr[0];
109
110   $form->{selectcurrency} = "";
111   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
112
113
114   &form_header;
115   &form_footer;
116
117   $lxdebug->leave_sub();
118 }
119
120 sub form_header {
121   $lxdebug->enter_sub;
122
123   $auth->assert('cash');
124
125   my ($vc, $arap, $exchangerate);
126   my ($onload);
127
128   if ($form->{ $form->{vc} } eq "") {
129     map { $form->{"addr$_"} = "" } (1 .. 4);
130   }
131
132   for my $item ($form->{vc}, "account", "currency", $form->{ARAP}) {
133     $form->{"select$item"} =~ s/ selected//;
134     $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
135   }
136
137   $vc =
138     ($form->{"select$form->{vc}"})
139     ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>|
140     : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
141
142   $form->{openinvoices} = $form->{all_vc} ? "" : 1;
143
144   # $locale->text('AR')
145   # $locale->text('AP')
146
147   $form->header;
148
149   $arap = lc $form->{ARAP};
150   $onload = qq|focus()|;
151   $onload .= qq|;setupDateFormat('|. $myconfig{dateformat} .qq|', '|. $locale->text("Falsches Datumsformat!") .qq|')|;
152   $onload .= qq|;setupPoints('|. $myconfig{numberformat} .qq|', '|. $locale->text("wrongformat") .qq|')|;
153
154   print $::form->parse_html_template('cp/form_header', {
155     is_customer => $form->{vc}   eq 'customer',
156     is_receipt  => $form->{type} eq 'receipt',
157     onload      => $onload,
158     arap        => $arap,
159     vccontent   => $vc,
160   });
161
162   $lxdebug->leave_sub;
163 }
164
165 sub list_invoices {
166   $::lxdebug->enter_sub;
167   $::auth->assert('cash');
168
169   my @columns = qw(amount due paid invnumber id transdate checked);
170   my (@invoices, %total);
171   for my $i (1 .. $::form->{rowcount}) {
172     push @invoices, +{ map { $_ => $::form->{"$_\_$i"} } @columns };
173     $total{$_} += $invoices[-1]{$_} = $::form->parse_amount(\%::myconfig, $invoices[-1]{$_}) for qw(amount due paid);
174   }
175
176   print $::form->parse_html_template('cp/invoices', {
177     invoices => \@invoices,
178     totals   => \%total,
179   });
180
181   $::lxdebug->leave_sub;
182 }
183
184 sub form_footer {
185   $::lxdebug->enter_sub;
186   $::auth->assert('cash');
187
188   print $::form->parse_html_template('cp/form_footer');
189
190   $::lxdebug->leave_sub;
191 }
192
193 sub update {
194   $lxdebug->enter_sub();
195
196   $auth->assert('cash');
197
198   my ($new_name_selected) = @_;
199
200   my ($buysell, $newvc, $updated, $exchangerate, $amount);
201
202   if ($form->{vc} eq 'customer') {
203     $buysell = "buy";
204   } else {
205     $buysell = "sell";
206   }
207
208   # if we switched to all_vc
209   # funktioniert derzeit nicht 12.10.2010 jb
210   if ($form->{all_vc} ne $form->{oldall_vc}) {
211
212     $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
213
214     $form->{"select$form->{vc}"} = "";
215
216     if ($form->{all_vc}) {
217       $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
218
219       if ($form->{"all_$form->{vc}"}) {
220         map {
221           $form->{"select$form->{vc}"} .=
222             "<option>$_->{name}--$_->{id}\n"
223         } @{ $form->{"all_$form->{vc}"} };
224       }
225     } else {  # ab hier wieder ausgeführter code (s.o.):
226       CP->get_openvc(\%myconfig, \%$form);
227
228       if ($form->{"all_$form->{vc}"}) {
229         $newvc =
230           qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
231         map {
232           $form->{"select$form->{vc}"} .=
233             "<option>$_->{name}--$_->{id}\n"
234         } @{ $form->{"all_$form->{vc}"} };
235       }
236
237       # if the name is not the same
238       if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
239         $form->{ $form->{vc} } = $newvc;
240       }
241     }
242   }
243   # Falls Suche über Rechnungsnummer und kein Kundenname vorhanden
244   if ($form->{invnumber} && !($form->{$form->{vc}})){
245   $form->{open} ='Y'; # nur die offenen rechnungen
246   if ($form->{ARAP} eq 'AR'){
247     AR->ar_transactions(\%myconfig, \%$form);
248     # den ersten treffen nehmen und mit dem namen überschreiben
249     $form->{$form->{vc}} = $form->{AR}[0]{name};
250   } else {
251     # s.o. nur für zahlungsausgang
252     AP->ap_transactions(\%myconfig, \%$form);
253     $form->{$form->{vc}} = $form->{AP}[0]{name};
254     }
255   }
256   # get customer and invoices
257   $updated = &check_name($form->{vc});
258
259   if ($new_name_selected || $updated) {
260     CP->get_openinvoices(\%myconfig, \%$form);
261     ($newvc) = split /--/, $form->{ $form->{vc} };
262     $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
263     $updated = 1;
264   }
265
266   if ($form->{currency} ne $form->{oldcurrency}) {
267     $form->{oldcurrency} = $form->{currency};
268     if (!$updated) {
269       CP->get_openinvoices(\%myconfig, \%$form);
270       $updated = 1;
271     }
272   }
273
274   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{datepaid}, $buysell);
275   $form->{exchangerate} = $form->{forex} if $form->{forex};
276
277   $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
278
279   if ($updated) {
280     $form->{rowcount} = 0;
281
282     $form->{queued} = "";
283
284     my $i = 0;
285     foreach my $ref (@{ $form->{PR} }) {
286       $i++;
287       $form->{"id_$i"}        = $ref->{id};
288       $form->{"invnumber_$i"} = $ref->{invnumber};
289       $form->{"transdate_$i"} = $ref->{transdate};
290       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
291       $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
292       $form->{"due_$i"}    =
293         ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
294       $form->{"checked_$i"} = "";
295       $form->{"paid_$i"}    = "";
296
297       # need to format
298       map {
299         $form->{"${_}_$i"} =
300           $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
301       } qw(amount due);
302
303     }
304     $form->{rowcount} = $i;
305   }
306
307   # recalculate
308
309   # Modified from $amount = $form->{amount} by J.Zach to update amount to total
310   # payment amount in Zahlungsausgang
311   $amount = 0;
312   for my $i (1 .. $form->{rowcount}) {
313
314     map {
315       $form->{"${_}_$i"} =
316         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
317     } qw(amount due paid);
318
319     if ($form->{"checked_$i"}) {
320
321       # calculate paid_$i
322       if (!$form->{"paid_$i"}) {
323         $form->{"paid_$i"} = $form->{"due_$i"};
324       }
325
326       # Modified by J.Zach, see abovev
327       $amount += $form->{"paid_$i"};
328
329     } else {
330       $form->{"paid_$i"} = "";
331     }
332
333     map {
334       $form->{"${_}_$i"} =
335         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
336     } qw(amount due paid);
337
338   }
339
340   # Line added by J.Zach, see above
341   $form->{amount}=$amount;
342
343   &form_header;
344   &list_invoices;
345   &form_footer;
346
347   $lxdebug->leave_sub();
348 }
349
350 sub post {
351   $lxdebug->enter_sub();
352
353   $auth->assert('cash');
354
355   &check_form;
356
357   if ($form->{currency} ne $form->{defaultcurrency}) {
358     $form->error($locale->text('Exchangerate missing!'))
359       unless $form->{exchangerate};
360   }
361
362   # Beim Aktualisieren wird das Konto übernommen
363   # und jetzt auch Beleg und Datum
364   $form->{callback} = "cp.pl?action=payment&vc=$form->{vc}&type=$form->{type}&account=$form->{account}&$form->{currency}" .
365                       "&datepaid=$form->{datepaid}&source=$form->{source}";
366
367   my $msg1 = $::form->{type} eq 'receipt' ? $::locale->text("Receipt posted!") : $::locale->text("Payment posted!");
368   my $msg2 = $::form->{type} eq 'receipt' ? $::locale->text("Cannot post Receipt!") : $::locale->text("Cannot post Payment!");
369
370   # Die Nachrichten (Receipt posted!) werden nicht angezeigt.
371   # Entweder wieder aktivieren oder komplett rausnehmen
372   $form->redirect($msg1) if (CP->process_payment(\%::myconfig, $::form));
373   $form->error($msg2);
374
375   $lxdebug->leave_sub();
376 }
377
378 sub check_form {
379   $lxdebug->enter_sub();
380
381   $auth->assert('cash');
382
383   my ($closedto, $datepaid, $amount);
384
385   &check_name($form->{vc});
386
387   if ($form->{currency} ne $form->{oldcurrency}) {
388     &update;
389     ::end_of_request();
390   }
391   $form->error($locale->text('Date missing!')) unless $form->{datepaid};
392   my $selected_check = 1;
393   for my $i (1 .. $form->{rowcount}) {
394     if ($form->{"checked_$i"}) {
395       if ($form->parse_amount(\%myconfig, $form->{"paid_$i"}, 2) <= 0) { # negativen Betrag eingegeben
396           $form->error($locale->text('Amount has to be greater then zero! Wrong row number: ') . $i);
397       }
398         undef($selected_check);
399         # last; # ich muss doch über alle buchungen laufen, da ich noch
400         # die freitext-eingabe der werte prüfen will
401     }
402   }
403   $form->error($locale->text('No transaction selected!')) if $selected_check;
404
405   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
406   $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
407
408   $form->error($locale->text('Cannot process payment for a closed period!'))
409     if ($form->date_closed($form->{"datepaid"}, \%myconfig));
410
411   $amount = $form->parse_amount(\%myconfig, $form->{amount});
412   $form->{amount} = $amount;
413
414   for my $i (1 .. $form->{rowcount}) {
415     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
416       $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
417
418       push(@{ $form->{paid} },      $form->{"paid_$i"});
419       push(@{ $form->{due} },       $form->{"due_$i"});
420       push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
421       push(@{ $form->{invdate} },   $form->{"transdate_$i"});
422     }
423   }
424
425   if ($form->round_amount($amount, 2) != 0) {
426     push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
427     push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
428     push(@{ $form->{invnumber} },
429          ($form->{ARAP} eq 'AR')
430          ? $locale->text('Deposit')
431          : $locale->text('Prepayment'));
432     push(@{ $form->{invdate} }, $form->{datepaid});
433   }
434
435   $lxdebug->leave_sub();
436 }