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 #======================================================================
 
  43 require "bin/mozilla/arap.pl";
 
  44 require "bin/mozilla/common.pl";
 
  46 our ($form, %myconfig, $lxdebug, $locale, $auth);
 
  53   $lxdebug->enter_sub();
 
  55   $auth->assert('cash');
 
  59   $form->{ARAP} = ($form->{type} eq 'receipt') ? "AR" : "AP";
 
  60   $form->{arap} = lc $form->{ARAP};
 
  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});
 
  68     CP->get_openvc(\%myconfig, \%$form);
 
  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}"} = "";
 
  75   if ($form->{"all_$form->{vc}"}) {
 
  76     $form->{"select$form->{vc}"} .= "<option value=\"\"></option>\n";
 
  78     $form->{"$form->{vc}_id"} = $form->{"all_$form->{vc}"}->[0]->{id};
 
  79     # hotfix for 2450. TODO remove legacy code and use L
 
  80     map { $form->{"select$form->{vc}"} .= "<option value=\"" . H($_->{name}) . "--$_->{id}\">" . H($_->{name}) . "--$_->{id}</option>\n" }
 
  81       @{ $form->{"all_$form->{vc}"} };
 
  83   CP->paymentaccounts(\%myconfig, \%$form);
 
  85   # Standard Konto für Umlaufvermögen
 
  86   my $accno_arap = IS->get_standard_accno_current_assets(\%myconfig, \%$form);
 
  87   # Entsprechend präventiv die Auswahlliste für Kontonummer
 
  88   # auch mit value= zusammenbauen (s.a. oben bugfix 1771)
 
  89   # Wichtig: Auch das Template anpassen, damit hidden input korrekt die "
 
  91   $form->{selectaccount} = "";
 
  92   $form->{"select$form->{ARAP}"} = "";
 
  94   map { $form->{selectaccount} .= "<option value=\"$_->{accno}--$_->{description}\">$_->{accno}--$_->{description}</option>\n";
 
  95         $form->{account}        = "$_->{accno}--$_->{description}" if ($_->{accno} eq $accno_arap) } @{ $form->{PR}{"$form->{ARAP}_paid"} };
 
  97   # Braucht man das hier überhaupt? Erstmal auskommentieren .. jan 18.12.2010
 
  99   #    $form->{"select$form->{ARAP}"} .=
 
 100   #      "<option>$_->{accno}--$_->{description}\n"
 
 101   #  } @{ $form->{PR}{ $form->{ARAP} } };
 
 102   # ENDE LOESCHMICH in 2012
 
 105   # oldcurrency ist zwar noch hier als fragment enthalten, wird aber bei
 
 106   # der aktualisierung der form auch nicht mitübernommen. das konzept
 
 107   # old_$FOO habe ich auch noch nicht verstanden ...
 
 108   # Ok. Wenn currency übernommen werden, dann in callback-string über-
 
 109   # geben und hier reinparsen, oder besser multibox oder html auslagern?
 
 110   # Antwort: form->currency wird mit oldcurrency oder curr[0] überschrieben
 
 111   # Wofür macht das Sinn?
 
 112   @curr = $form->get_all_currencies();
 
 113   $form->{defaultcurrency} = $form->{currency} = $form->{oldcurrency} =
 
 114     $form->get_default_currency(\%myconfig);
 
 116   # Entsprechend präventiv die Auswahlliste für Währungen
 
 117   # auch mit value= zusammenbauen (s.a. oben bugfix 1771)
 
 118   $form->{selectcurrency} = "";
 
 119   map { $form->{selectcurrency} .= "<option value=\"$_\">$_</option>\n" } @curr;
 
 125   $lxdebug->leave_sub();
 
 131   $auth->assert('cash');
 
 133   my ($vc, $arap, $exchangerate);
 
 135   if ($form->{ $form->{vc} } eq "") {
 
 136     map { $form->{"addr$_"} = "" } (1 .. 4);
 
 139   # sometimes it happens that values in customer arrive without the signs '--'
 
 140   # but in order to select the right option field we need values with '--'
 
 141   if ($form->{vc} eq "customer" && $form->{"all_$form->{vc}"}){
 
 142     my ($customername) = split /--/, $form->{ $form->{vc} };
 
 143     $form->{ $form->{vc} } = $customername . "--" . $form->{customer_id};
 
 146   # geändert von <option>asdf--2929
 
 148   #              <option value="asdf--2929">asdf--2929</option>
 
 149   # offen: $form->{ARAP} kann raus?
 
 150   for my $item ($form->{vc}, "account", "currency", $form->{ARAP}) {
 
 151     $form->{$item} = H($form->{$item});
 
 152     $form->{"select$item"} =~ s/ selected//;
 
 153     $form->{"select$item"} =~ s/option value="\Q$form->{$item}\E">\Q$form->{$item}\E/option selected value="$form->{$item}">$form->{$item}/;
 
 157     ($form->{"select$form->{vc}"})
 
 158     ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}\n</select>|
 
 159     : qq|<input name=$form->{vc} size=35 value="$form->{$form->{vc}}">|;
 
 161   $form->{openinvoices} = $form->{all_vc} ? "" : 1;
 
 163   # $locale->text('AR')
 
 164   # $locale->text('AP')
 
 168   $arap = lc $form->{ARAP};
 
 170   print $::form->parse_html_template('cp/form_header', {
 
 171     is_customer => $form->{vc}   eq 'customer',
 
 172     is_receipt  => $form->{type} eq 'receipt',
 
 181   $::lxdebug->enter_sub;
 
 182   $::auth->assert('cash');
 
 184   my @columns = qw(amount due paid invnumber id transdate checked);
 
 185   my (@invoices, %total);
 
 186   for my $i (1 .. $::form->{rowcount}) {
 
 187     push @invoices, +{ map { $_ => $::form->{"$_\_$i"} } @columns };
 
 188     $total{$_} += $invoices[-1]{$_} = $::form->parse_amount(\%::myconfig, $invoices[-1]{$_}) for qw(amount due paid);
 
 191   print $::form->parse_html_template('cp/invoices', {
 
 192     invoices => \@invoices,
 
 196   $::lxdebug->leave_sub;
 
 200   $::lxdebug->enter_sub;
 
 201   $::auth->assert('cash');
 
 203   print $::form->parse_html_template('cp/form_footer');
 
 205   $::lxdebug->leave_sub;
 
 209   $lxdebug->enter_sub();
 
 211   $auth->assert('cash');
 
 213   my ($new_name_selected) = @_;
 
 215   my ($buysell, $newvc, $updated, $exchangerate, $amount);
 
 217   if ($form->{vc} eq 'customer') {
 
 223   # if we switched to all_vc
 
 224   # funktioniert derzeit nicht 12.10.2010 jb
 
 225   if ($form->{all_vc} ne $form->{oldall_vc}) {
 
 227     $form->{openinvoices} = ($form->{all_vc}) ? 0 : 1;
 
 229     $form->{"select$form->{vc}"} = "";
 
 231     if ($form->{all_vc}) {
 
 232       $form->all_vc(\%myconfig, $form->{vc}, $form->{ARAP});
 
 234       if ($form->{"all_$form->{vc}"}) {
 
 236           $form->{"select$form->{vc}"} .=
 
 237             "<option>$_->{name}--$_->{id}\n"
 
 238         } @{ $form->{"all_$form->{vc}"} };
 
 240     } else {  # ab hier wieder ausgeführter code (s.o.):
 
 241       CP->get_openvc(\%myconfig, \%$form);
 
 243       if ($form->{"all_$form->{vc}"}) {
 
 245           qq|$form->{"all_$form->{vc}"}[0]->{name}--$form->{"all_$form->{vc}"}[0]->{id}|;
 
 247           $form->{"select$form->{vc}"} .=
 
 248             "<option>$_->{name}--$_->{id}\n"
 
 249         } @{ $form->{"all_$form->{vc}"} };
 
 252       # if the name is not the same
 
 253       if ($form->{"select$form->{vc}"} !~ /$form->{$form->{vc}}/) {
 
 254         $form->{ $form->{vc} } = $newvc;
 
 259   # search by customernumber
 
 260   # the customernumber has to be correct otherwise nothing is found
 
 261   if ($form->{vc} eq 'customer' and $form->{customernumber} and $form->{ARAP} eq 'AR') {
 
 262     $form->{open} ='Y'; # only open invoices
 
 263     # ar_transactions automatically searches by $form->{customer_id} or else
 
 264     # $form->{customer} if available, and these variables will always be set
 
 265     # so we have to empty these values first
 
 266     $form->{customer_id} = '';
 
 267     $form->{customer} = '';
 
 268     AR->ar_transactions(\%myconfig, \%$form);
 
 270     # Here we just take the first returned value even if the custumernumber
 
 272     $form->{customer} = $form->{AR}[0]{name};
 
 273     $form->{customer_id} = $form->{AR}[0]{customer_id};
 
 276   # search by invoicenumber,
 
 277   if ($form->{invnumber}) {
 
 278     $form->{open} ='Y'; # only open invoices
 
 279     if ($form->{ARAP} eq 'AR'){
 
 280       # ar_transactions automatically searches by $form->{customer_id} or else
 
 281       # $form->{customer} if available, and these variables will always be set
 
 282       # so we have to empty these values first
 
 283       $form->{customer_id} = '';
 
 284       $form->{customer} = '';
 
 285       AR->ar_transactions(\%myconfig, \%$form);
 
 287       # if you search for invoice '11' ar_transactions will also match invoices
 
 288       # 112, 211, ... due to the LIKE
 
 290       # so there is now an extra loop that tries to match the invoice number
 
 291       # exactly among all returned results, and then passes the customer_id instead of the name
 
 292       # because the name may not be unique
 
 294       my $found_exact_invnumber_match = 0;
 
 295       foreach my $i ( @{ $form->{AR} } ) {
 
 296         next unless $i->{invnumber} eq $form->{invnumber};
 
 297         # found exactly matching invnumber
 
 298         $form->{$form->{vc}} = $i->{name};
 
 299         $form->{customer_id} = $i->{customer_id};
 
 300         #$form->{"old${form->{vc}"} = $i->{customer_id};
 
 301         $found_exact_invnumber_match = 1;
 
 304       unless ( $found_exact_invnumber_match ) {
 
 305         # use first returned entry, may not be the correct one if invnumber doesn't
 
 307         $form->{$form->{vc}} = $form->{AR}[0]{name};
 
 308         $form->{customer_id} = $form->{AR}[0]{customer_id};
 
 311       # s.o. nur für zahlungsausgang
 
 312       AP->ap_transactions(\%myconfig, \%$form);
 
 313       $form->{$form->{vc}} = $form->{AP}[0]{name};
 
 317   # determine customer/vendor
 
 318   if ( $form->{customer_id} and ($form->{invnumber} or $form->{customernumber}) ) {
 
 319     # we already know the exact customer_id, so fill $form with customer data
 
 320     IS->get_customer(\%myconfig, \%$form);
 
 323     # check_name is called with "customer" or "vendor" and otherwise uses contents of $form
 
 324     # check_name also runs get_customer/get_vendor
 
 325     $updated = &check_name($form->{vc});
 
 328   if ($new_name_selected || $updated) {
 
 329     # get open invoices from ar/ap using $form->{vc} and a.${vc}_id, i.e. customer_id
 
 330     CP->get_openinvoices(\%myconfig, \%$form);
 
 331     ($newvc) = split /--/, $form->{ $form->{vc} };
 
 332     $form->{"old$form->{vc}"} = qq|$newvc--$form->{"$form->{vc}_id"}|;
 
 336   if ($form->{currency} ne $form->{oldcurrency}) {
 
 337     $form->{oldcurrency} = $form->{currency};
 
 339       CP->get_openinvoices(\%myconfig, \%$form);
 
 344   if (!$form->{forex}) {        # read exchangerate from input field (not hidden)
 
 345     $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
 
 347   $form->{forex}        = $form->check_exchangerate( \%myconfig, $form->{currency}, $form->{datepaid}, $buysell);
 
 348   $form->{exchangerate} = $form->{forex} if $form->{forex};
 
 350   $amount = $form->{amount} = $form->parse_amount(\%myconfig, $form->{amount});
 
 353     $form->{rowcount} = 0;
 
 355     $form->{queued} = "";
 
 358     foreach my $ref (@{ $form->{PR} }) {
 
 360       $form->{"id_$i"}        = $ref->{id};
 
 361       $form->{"invnumber_$i"} = $ref->{invnumber};
 
 362       $form->{"transdate_$i"} = $ref->{transdate};
 
 363       $ref->{exchangerate} = 1 unless $ref->{exchangerate};
 
 364       $form->{"amount_$i"} = $ref->{amount} / $ref->{exchangerate};
 
 366         ($ref->{amount} - $ref->{paid}) / $ref->{exchangerate};
 
 367       $form->{"checked_$i"} = "";
 
 368       $form->{"paid_$i"}    = "";
 
 373           $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
 
 377     $form->{rowcount} = $i;
 
 382   # Modified from $amount = $form->{amount} by J.Zach to update amount to total
 
 383   # payment amount in Zahlungsausgang
 
 385   for my $i (1 .. $form->{rowcount}) {
 
 389         $form->parse_amount(\%myconfig, $form->{"${_}_$i"})
 
 390     } qw(amount due paid);
 
 392     if ($form->{"checked_$i"}) {
 
 395       if (!$form->{"paid_$i"}) {
 
 396         $form->{"paid_$i"} = $form->{"due_$i"};
 
 399       # Modified by J.Zach, see abovev
 
 400       $amount += $form->{"paid_$i"};
 
 403       $form->{"paid_$i"} = "";
 
 408         $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2)
 
 409     } qw(amount due paid);
 
 413   # Line added by J.Zach, see above
 
 414   $form->{amount}=$amount;
 
 420   $lxdebug->leave_sub();
 
 424   $lxdebug->enter_sub();
 
 426   $auth->assert('cash');
 
 430   if ($form->{currency} ne $form->{defaultcurrency}) {
 
 431     $form->error($locale->text('Exchangerate missing!'))
 
 432       unless $form->{exchangerate};
 
 435   # Beim Aktualisieren wird das Konto übernommen
 
 436   # und jetzt auch Beleg und Datum
 
 437   $form->{callback} = "cp.pl?action=payment&vc=$form->{vc}&type=$form->{type}&account=$form->{account}&$form->{currency}" .
 
 438                       "&datepaid=$form->{datepaid}&source=$form->{source}";
 
 440   my $msg1 = $::form->{type} eq 'receipt' ? $::locale->text("Receipt posted!") : $::locale->text("Payment posted!");
 
 441   my $msg2 = $::form->{type} eq 'receipt' ? $::locale->text("Cannot post Receipt!") : $::locale->text("Cannot post Payment!");
 
 443   # Die Nachrichten (Receipt posted!) werden nicht angezeigt.
 
 444   # Entweder wieder aktivieren oder komplett rausnehmen
 
 445   $form->redirect($msg1) if (CP->process_payment(\%::myconfig, $::form));
 
 448   $lxdebug->leave_sub();
 
 452   $lxdebug->enter_sub();
 
 454   $auth->assert('cash');
 
 456   my ($closedto, $datepaid, $amount);
 
 458   &check_name($form->{vc});
 
 460   if ($form->{currency} ne $form->{oldcurrency}) {
 
 464   $form->error($locale->text('Date missing!')) unless $form->{datepaid};
 
 465   my $selected_check = 1;
 
 466   for my $i (1 .. $form->{rowcount}) {
 
 467     next unless $form->{"checked_$i"};
 
 468     if (abs($form->parse_amount(\%myconfig, $form->{"paid_$i"}, 2)) < 0.01) {
 
 469       $form->error($locale->text('Row #1: amount has to be different from zero.', $i));
 
 471     undef $selected_check;
 
 473   $form->error($locale->text('No transaction selected!')) if $selected_check;
 
 475   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
 
 476   $datepaid = $form->datetonum($form->{datepaid}, \%myconfig);
 
 478   $form->error($locale->text('Cannot process payment for a closed period!'))
 
 479     if ($form->date_closed($form->{"datepaid"}, \%myconfig));
 
 481   $amount = $form->parse_amount(\%myconfig, $form->{amount});
 
 482   $form->{amount} = $amount;
 
 484   for my $i (1 .. $form->{rowcount}) {
 
 485     if ($form->parse_amount(\%myconfig, $form->{"paid_$i"})) {
 
 486       $amount -= $form->parse_amount(\%myconfig, $form->{"paid_$i"});
 
 488       push(@{ $form->{paid}      ||= [] }, $form->{"paid_$i"});
 
 489       push(@{ $form->{due}       ||= [] }, $form->{"due_$i"});
 
 490       push(@{ $form->{invnumber} ||= [] }, $form->{"invnumber_$i"});
 
 491       push(@{ $form->{invdate}   ||= [] }, $form->{"transdate_$i"});
 
 495   if ($form->round_amount($amount, 2) != 0) {
 
 496     push(@{ $form->{paid} }, $form->format_amount(\%myconfig, $amount, 2));
 
 497     push(@{ $form->{due} }, $form->format_amount(\%myconfig, 0, "0"));
 
 498     push(@{ $form->{invnumber} },
 
 499          ($form->{ARAP} eq 'AR')
 
 500          ? $locale->text('Deposit')
 
 501          : $locale->text('Prepayment'));
 
 502     push(@{ $form->{invdate} }, $form->{datepaid});
 
 505   $lxdebug->leave_sub();