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 #======================================================================
 
  42 require "bin/mozilla/arap.pl";
 
  43 require "bin/mozilla/common.pl";
 
  45 our ($form, %myconfig, $lxdebug, $locale, $auth);
 
  52   $lxdebug->enter_sub();
 
  54   $auth->assert('cash');
 
  58   $form->{ARAP} = ($form->{type} eq 'receipt') ? "AR" : "AP";
 
  59   $form->{arap} = lc $form->{ARAP};
 
  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});
 
  67     CP->get_openvc(\%myconfig, \%$form);
 
  70   $form->{"select$form->{vc}"} = "";
 
  72   if ($form->{"all_$form->{vc}"}) {
 
  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}"} };
 
  79   CP->paymentaccounts(\%myconfig, \%$form);
 
  81   # Standard Konto für Umlaufvermögen
 
  82   my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
 
  84   $form->{selectaccount} = "";
 
  85   $form->{"select$form->{ARAP}"} = "";
 
  87   map { $form->{selectaccount} .= "<option>$_->{accno}--$_->{description}\n";
 
  88         $form->{account}        = "$_->{accno}--$_->{description}" if ($_->{accno} eq $accno_arap) } @{ $form->{PR}{"$form->{ARAP}_paid"} };
 
  90   # Braucht man das hier überhaupt? Erstmal auskommentieren .. jan 18.12.2010
 
  92   #    $form->{"select$form->{ARAP}"} .=
 
  93   #      "<option>$_->{accno}--$_->{description}\n"
 
  94   #  } @{ $form->{PR}{ $form->{ARAP} } };
 
  95   # ENDE LOESCHMICH in 2012
 
  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});
 
 107   $form->{defaultcurrency} = $form->{currency} = $form->{oldcurrency} =
 
 110   $form->{selectcurrency} = "";
 
 111   map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
 
 117   $lxdebug->leave_sub();
 
 123   $auth->assert('cash');
 
 125   my ($vc, $arap, $exchangerate);
 
 128   if ($form->{ $form->{vc} } eq "") {
 
 129     map { $form->{"addr$_"} = "" } (1 .. 4);
 
 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}/;
 
 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}}">|;
 
 142   $form->{openinvoices} = $form->{all_vc} ? "" : 1;
 
 144   # $locale->text('AR')
 
 145   # $locale->text('AP')
 
 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|')|;
 
 154   print $::form->parse_html_template('cp/form_header', {
 
 155     is_customer => $form->{vc}   eq 'customer',
 
 156     is_receipt  => $form->{type} eq 'receipt',
 
 166   $::lxdebug->enter_sub;
 
 167   $::auth->assert('cash');
 
 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);
 
 176   print $::form->parse_html_template('cp/invoices', {
 
 177     invoices => \@invoices,
 
 181   $::lxdebug->leave_sub;
 
 185   $::lxdebug->enter_sub;
 
 186   $::auth->assert('cash');
 
 188   print $::form->parse_html_template('cp/form_footer');
 
 190   $::lxdebug->leave_sub;
 
 194   $lxdebug->enter_sub();
 
 196   $auth->assert('cash');
 
 198   my ($new_name_selected) = @_;
 
 200   my ($buysell, $newvc, $updated, $exchangerate, $amount);
 
 202   if ($form->{vc} eq 'customer') {
 
 208   # if we switched to all_vc
 
 209   # funktioniert derzeit nicht 12.10.2010 jb
 
 210   if ($form->{all_vc} ne $form->{oldall_vc}) {
 
 212     $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
 
 214     $form->{"select$form->{vc}"} = "";
 
 216     if ($form->{all_vc}) {
 
 217       $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
 
 219       if ($form->{"all_$form->{vc}"}) {
 
 221           $form->{"select$form->{vc}"} .=
 
 222             "<option>$_->{name}--$_->{id}\n"
 
 223         } @{ $form->{"all_$form->{vc}"} };
 
 225     } else {  # ab hier wieder ausgeführter code (s.o.):
 
 226       CP->get_openvc(\%myconfig, \%$form);
 
 228       if ($form->{"all_$form->{vc}"}) {
 
 230           qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
 
 232           $form->{"select$form->{vc}"} .=
 
 233             "<option>$_->{name}--$_->{id}\n"
 
 234         } @{ $form->{"all_$form->{vc}"} };
 
 237       # if the name is not the same
 
 238       if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
 
 239         $form->{ $form->{vc} } = $newvc;
 
 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);
 
 249     # if you search for invoice '11' ar_transactions will also match invoices
 
 250     # 112, 211, ... due to the LIKE
 
 252     # so there is now an extra loop that tries to match the invoice number
 
 253     # exactly among all returned results, and then passes the customer_id instead of the name
 
 254     # because the name may not be unique
 
 256     my $found_exact_invnumber_match = 0;
 
 257     foreach my $i ( @{ $form->{AR} } ) {
 
 258       next unless $i->{invnumber} eq $form->{invnumber};
 
 259       # found exactly matching invnumber
 
 260       $form->{$form->{vc}} = $i->{name};
 
 261       $form->{customer_id} = $i->{customer_id};
 
 262       $found_exact_invnumber_match = 1;
 
 265     unless ( $found_exact_invnumber_match ) {
 
 266       # use first returned entry, may not be the correct one if invnumber doesn't match uniquely
 
 267       $form->{$form->{vc}} = $form->{AR}[0]{name};
 
 268       $form->{customer_id} = $form->{AR}[0]{customer_id};
 
 271     # s.o. nur für zahlungsausgang
 
 272     AP->ap_transactions(\%myconfig, \%$form);
 
 273     $form->{$form->{vc}} = $form->{AP}[0]{name};
 
 276   # get customer and invoices
 
 277   $updated = &check_name($form->{vc}) unless $form->{customer_id};
 
 279   if ( $form->{customer_id} ) {
 
 280     # we already know the exact customer_id, fill $form with customer data
 
 281     IS->get_customer(\%myconfig, \%$form);
 
 285   if ($new_name_selected || $updated) {
 
 286     # get open invoices from ar/ap using $form->{vc} and a.${vc}_id
 
 287     CP->get_openinvoices(\%myconfig, \%$form);
 
 288     ($newvc) = split /--/, $form->{ $form->{vc} };
 
 289     $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
 
 293   if ($form->{currency} ne $form->{oldcurrency}) {
 
 294     $form->{oldcurrency} = $form->{currency};
 
 296       CP->get_openinvoices(\%myconfig, \%$form);
 
 301   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{datepaid}, $buysell);
 
 302   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 304   $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
 
 307     $form->{rowcount} = 0;
 
 309     $form->{queued} = "";
 
 312     foreach my $ref (@{ $form->{PR} }) {
 
 314       $form->{"id_$i"}        = $ref->{id};
 
 315       $form->{"invnumber_$i"} = $ref->{invnumber};
 
 316       $form->{"transdate_$i"} = $ref->{transdate};
 
 317       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
 
 318       $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
 
 320         ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
 
 321       $form->{"checked_$i"} = "";
 
 322       $form->{"paid_$i"}    = "";
 
 327           $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
 
 331     $form->{rowcount} = $i;
 
 336   # Modified from $amount = $form->{amount} by J.Zach to update amount to total
 
 337   # payment amount in Zahlungsausgang
 
 339   for my $i (1 .. $form->{rowcount}) {
 
 343         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
 
 344     } qw(amount due paid);
 
 346     if ($form->{"checked_$i"}) {
 
 349       if (!$form->{"paid_$i"}) {
 
 350         $form->{"paid_$i"} = $form->{"due_$i"};
 
 353       # Modified by J.Zach, see abovev
 
 354       $amount += $form->{"paid_$i"};
 
 357       $form->{"paid_$i"} = "";
 
 362         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
 
 363     } qw(amount due paid);
 
 367   # Line added by J.Zach, see above
 
 368   $form->{amount}=$amount;
 
 374   $lxdebug->leave_sub();
 
 378   $lxdebug->enter_sub();
 
 380   $auth->assert('cash');
 
 384   if ($form->{currency} ne $form->{defaultcurrency}) {
 
 385     $form->error($locale->text('Exchangerate missing!'))
 
 386       unless $form->{exchangerate};
 
 389   # Beim Aktualisieren wird das Konto übernommen
 
 390   # und jetzt auch Beleg und Datum
 
 391   $form->{callback} = "cp.pl?action=payment&vc=$form->{vc}&type=$form->{type}&account=$form->{account}&$form->{currency}" .
 
 392                       "&datepaid=$form->{datepaid}&source=$form->{source}";
 
 394   my $msg1 = $::form->{type} eq 'receipt' ? $::locale->text("Receipt posted!") : $::locale->text("Payment posted!");
 
 395   my $msg2 = $::form->{type} eq 'receipt' ? $::locale->text("Cannot post Receipt!") : $::locale->text("Cannot post Payment!");
 
 397   # Die Nachrichten (Receipt posted!) werden nicht angezeigt.
 
 398   # Entweder wieder aktivieren oder komplett rausnehmen
 
 399   $form->redirect($msg1) if (CP->process_payment(\%::myconfig, $::form));
 
 402   $lxdebug->leave_sub();
 
 406   $lxdebug->enter_sub();
 
 408   $auth->assert('cash');
 
 410   my ($closedto, $datepaid, $amount);
 
 412   &check_name($form->{vc});
 
 414   if ($form->{currency} ne $form->{oldcurrency}) {
 
 418   $form->error($locale->text('Date missing!')) unless $form->{datepaid};
 
 419   my $selected_check = 1;
 
 420   for my $i (1 .. $form->{rowcount}) {
 
 421     if ($form->{"checked_$i"}) {
 
 422       if ($form->parse_amount(\%myconfig, $form->{"paid_$i"}, 2) <= 0) { # negativen Betrag eingegeben
 
 423           $form->error($locale->text('Amount has to be greater then zero! Wrong row number: ') . $i);
 
 425         undef($selected_check);
 
 426         # last; # ich muss doch über alle buchungen laufen, da ich noch
 
 427         # die freitext-eingabe der werte prüfen will
 
 430   $form->error($locale->text('No transaction selected!')) if $selected_check;
 
 432   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
 
 433   $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
 
 435   $form->error($locale->text('Cannot process payment for a closed period!'))
 
 436     if ($form->date_closed($form->{"datepaid"}, \%myconfig));
 
 438   $amount = $form->parse_amount(\%myconfig, $form->{amount});
 
 439   $form->{amount} = $amount;
 
 441   for my $i (1 .. $form->{rowcount}) {
 
 442     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 443       $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
 
 445       push(@{ $form->{paid} },      $form->{"paid_$i"});
 
 446       push(@{ $form->{due} },       $form->{"due_$i"});
 
 447       push(@{ $form->{invnumber} }, $form->{"invnumber_$i"});
 
 448       push(@{ $form->{invdate} },   $form->{"transdate_$i"});
 
 452   if ($form->round_amount($amount, 2) != 0) {
 
 453     push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
 
 454     push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
 
 455     push(@{ $form->{invnumber} },
 
 456          ($form->{ARAP} eq 'AR')
 
 457          ? $locale->text('Deposit')
 
 458          : $locale->text('Prepayment'));
 
 459     push(@{ $form->{invdate} }, $form->{datepaid});
 
 462   $lxdebug->leave_sub();