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
 
  17 # This program is free software; you can redistribute it and/or modify
 
  18 # it under the terms of the GNU General Public License as published by
 
  19 # the Free Software Foundation; either version 2 of the License, or
 
  20 # (at your option) any later version.
 
  22 # This program is distributed in the hope that it will be useful,
 
  23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  25 # GNU General Public License for more details.
 
  26 # You should have received a copy of the GNU General Public License
 
  27 # along with this program; if not, write to the Free Software
 
  28 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
  29 #======================================================================
 
  31 # Check and receipt printing payment module backend routines
 
  32 # Number to text conversion routines are in
 
  33 # locale/{countrycode}/Num2text
 
  35 #======================================================================
 
  43   $main::lxdebug->enter_sub();
 
  45   my ($type, $countrycode) = @_;
 
  50     if (-f "locale/$countrycode/Num2text") {
 
  51       require "locale/$countrycode/Num2text";
 
  59   $main::lxdebug->leave_sub();
 
  65   $main::lxdebug->enter_sub();
 
  67   my ($self, $myconfig, $form) = @_;
 
  70   my $dbh = $form->dbconnect($myconfig);
 
  72   my $ARAP = $form->{ARAP} eq "AR" ? "AR" : "AP";
 
  75     qq|SELECT accno, description, link | .
 
  77     qq|WHERE link LIKE ? |.
 
  79   my $sth = prepare_execute_query($form, $dbh, $query, like($ARAP));
 
  81   $form->{PR}{ $form->{ARAP} } = ();
 
  82   $form->{PR}{"$form->{ARAP}_paid"} = ();
 
  84   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
  85     foreach my $item (split(/:/, $ref->{link})) {
 
  86       if ($item eq $form->{ARAP}) {
 
  87         push(@{ $form->{PR}{ $form->{ARAP} } }, $ref);
 
  89       if ($item eq "$form->{ARAP}_paid") {
 
  90         push(@{ $form->{PR}{"$form->{ARAP}_paid"} }, $ref);
 
  97   $query = qq|SELECT closedto FROM defaults|;
 
  98   ($form->{closedto}) = selectrow_query($form, $dbh, $query);
 
 102   $main::lxdebug->leave_sub();
 
 106   $main::lxdebug->enter_sub();
 
 108   my ($self, $myconfig, $form) = @_;
 
 110   my $dbh = $form->dbconnect($myconfig);
 
 112   my $arap = ($form->{vc} eq 'customer') ? 'ar' : 'ap';
 
 113   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
 
 115     qq|SELECT count(*) | .
 
 116     qq|FROM $vc ct, $arap a | .
 
 117     qq|WHERE (a.${vc}_id = ct.id) AND (a.amount != a.paid)|;
 
 118   my ($count) = selectrow_query($form, $dbh, $query);
 
 120   # build selection list
 
 121   if ($count < $myconfig->{vclimit}) {
 
 123       qq|SELECT DISTINCT ct.id, ct.name | .
 
 124       qq|FROM $vc ct, $arap a | .
 
 125       qq|WHERE (a.${vc}_id = ct.id) AND (a.amount != a.paid) | .
 
 126       qq|ORDER BY ct.name|;
 
 127     $form->{"all_$form->{vc}"} = selectall_hashref_query($form, $dbh, $query);
 
 130   # aufruf für all_deparments rausgenommen, da die abteilungen nur
 
 131   # beim buchen der belege (rechnung, fibu) geändert werden und danach
 
 132   # NICHT mehr überschrieben werden
 
 135   $main::lxdebug->leave_sub();
 
 138 sub get_openinvoices {
 
 139   $main::lxdebug->enter_sub();
 
 141   my ($self, $myconfig, $form) = @_;
 
 143   # connect to database
 
 144   my $dbh = $form->dbconnect($myconfig);
 
 146   my $vc = $form->{vc} eq "customer" ? "customer" : "vendor";
 
 148   my $buysell = $form->{vc} eq 'customer' ? "buy" : "sell";
 
 149   my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
 
 151   my @values = (conv_i($form->{"${vc}_id"}), "$form->{currency}");
 
 152   my $whereinvoice = '';
 
 153   if ($::form->{invnumber}) {
 
 154     $whereinvoice = ' AND a.invnumber LIKE ? ';
 
 155     push @values, $::form->{invnumber};
 
 159      qq|SELECT a.id, a.invnumber, a.transdate, a.amount, a.paid, cu.name AS curr | .
 
 161      qq|LEFT JOIN currencies cu ON (cu.id=a.currency_id)| .
 
 162      qq|WHERE (a.${vc}_id = ?) AND cu.name = ? AND NOT (a.amount = a.paid)| .
 
 166   my $sth = prepare_execute_query($form, $dbh, $query, @values);
 
 169   while (my $ref = $sth->fetchrow_hashref("NAME_lc")) {
 
 171     # if this is a foreign currency transaction get exchangerate
 
 172     $ref->{exchangerate} =
 
 173       $form->get_exchangerate($dbh, $ref->{curr}, $ref->{transdate}, $buysell)
 
 174       if ($form->{currency} ne $form->{defaultcurrency});
 
 175     push @{ $form->{PR} }, $ref;
 
 184       AND ((SELECT cu.name FROM currencies cu WHERE cu.id=${arap}.currency_id) <> ?)
 
 187   ($form->{openinvoices_other_currencies}) = selectfirst_array_query($form, $dbh, $query, conv_i($form->{"${vc}_id"}), "$form->{currency}");
 
 191   $main::lxdebug->leave_sub();
 
 194 sub process_payment {
 
 195   $main::lxdebug->enter_sub();
 
 197   my ($self, $myconfig, $form) = @_;
 
 200   # connect to database, turn AutoCommit off
 
 201   my $dbh = $form->dbconnect_noauto($myconfig);
 
 203   my ($paymentaccno) = split /--/, $form->{account};
 
 205   # if currency ne defaultcurrency update exchangerate
 
 206   if ($form->{currency} ne $form->{defaultcurrency}) {
 
 207     $form->{exchangerate} =
 
 208       $form->parse_amount($myconfig, $form->{exchangerate});
 
 210     if ($form->{vc} eq 'customer') {
 
 211       $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, $form->{exchangerate}, 0);
 
 213       $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, 0, $form->{exchangerate});
 
 216     $form->{exchangerate} = 1;
 
 219   my $query = qq|SELECT fxgain_accno_id, fxloss_accno_id FROM defaults|;
 
 220   my ($fxgain_accno_id, $fxloss_accno_id) = selectrow_query($form, $dbh, $query);
 
 222   my $buysell = $form->{vc} eq "customer" ? "buy" : "sell";
 
 223   my $arap = $form->{arap} eq "ar" ? "ar" : "ap";
 
 228   if ($form->{ARAP} eq 'AR') {
 
 230     $where = qq| ((c.link = 'AR') OR (c.link LIKE 'AR:%')) |;
 
 234       qq| ((c.link = 'AP') OR | .
 
 235       qq|  (c.link LIKE '%:AP') OR | .
 
 236       qq|  (c.link LIKE '%:AP:%')) |;
 
 240   # query to retrieve paid amount
 
 242     qq|SELECT a.paid FROM ar a | .
 
 243     qq|WHERE a.id = ? | .
 
 245   my $pth = prepare_query($form, $dbh, $query);
 
 247   # go through line by line
 
 248   for my $i (1 .. $form->{rowcount}) {
 
 250     $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
 
 251     $form->{"due_$i"}  = $form->parse_amount($myconfig, $form->{"due_$i"});
 
 253     if ($form->{"checked_$i"} && $form->{"paid_$i"}) {
 
 255       # get exchangerate for original
 
 257         qq|SELECT $buysell | .
 
 258         qq|FROM exchangerate e | .
 
 259         qq|JOIN ${arap} a ON (a.transdate = e.transdate) | .
 
 260         qq|WHERE (e.currency_id = (SELECT id FROM currencies WHERE name = ?)) AND (a.id = ?)|;
 
 262         selectrow_query($form, $dbh, $query,
 
 263                         $form->{currency}, $form->{"id_$i"});
 
 265       $exchangerate = 1 unless $exchangerate;
 
 270         qq|JOIN acc_trans a ON (a.chart_id = c.id) | .
 
 272         qq|AND (a.trans_id = ?)|;
 
 273       my ($id) = selectrow_query($form, $dbh, $query, $form->{"id_$i"});
 
 275       $amount = $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
 
 279         qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, chart_link, taxkey, tax_id) | .
 
 280         qq|VALUES (?, ?, ?, ?, (SELECT link FROM chart WHERE id=?), 0, (SELECT id FROM tax WHERE taxkey=0 LIMIT 1))|;
 
 281       do_query($form, $dbh, $query, $form->{"id_$i"}, $id,
 
 282                conv_date($form->{datepaid}), $amount * $ml, $id);
 
 286         qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
 
 287         qq|                       source, memo, chart_link, taxkey, tax_id) | .
 
 288         qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, (SELECT link FROM chart WHERE accno=?), 0, (SELECT id FROM tax WHERE taxkey=0 LIMIT 1))|;
 
 289       my @values = (conv_i($form->{"id_$i"}), $paymentaccno,
 
 290                     conv_date($form->{datepaid}),
 
 291                     $form->{"paid_$i"} * $ml * -1, $form->{source},
 
 292                     $form->{memo}, $paymentaccno);
 
 293       do_query($form, $dbh, $query, @values);
 
 295       # add exchangerate difference if currency ne defaultcurrency
 
 296       $amount = $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - 1),
 
 300         # exchangerate difference
 
 302           qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount, | .
 
 303           qq|                       cleared, fx_transaction, chart_link, taxkey, tax_id) | .
 
 304           qq|VALUES (?, (SELECT id FROM chart WHERE accno = ?), ?, ?, ?, ?, (SELECT link FROM chart WHERE accno = ?), 0, (SELECT id FROM tax WHERE taxkey=0 LIMIT 1))|;
 
 305         @values = (conv_i($form->{"id_$i"}), $paymentaccno,
 
 306                    conv_date($form->{datepaid}), ($amount * $ml * -1), '0',
 
 308         do_query($form, $dbh, $query, @values);
 
 313           $form->round_amount($form->{"paid_$i"} *
 
 314                               ($exchangerate - $form->{exchangerate}), 2);
 
 316           my $accno_id = ($amount < 0) ? $fxgain_accno_id : $fxloss_accno_id;
 
 318             qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, | .
 
 319             qq|                       amount, cleared, fx_transaction, chart_link, taxkey, tax_id) | .
 
 320             qq|VALUES (?, ?, ?, ?, ?, ?, (SELECT link FROM chart WHERE id=?), 0, (SELECT id FROM tax WHERE taxkey=0 LIMIT 1))|;
 
 321           @values = (conv_i($form->{"id_$i"}), $accno_id,
 
 322                      conv_date($form->{datepaid}), $amount * $ml * -1, '0',
 
 324           do_query($form, $dbh, $query, @values);
 
 329         $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
 
 330       $pth->execute($form->{"id_$i"}) || $form->dberror;
 
 331       ($amount) = $pth->fetchrow_array;
 
 334       $amount += $form->{"paid_$i"};
 
 338       if ($form->{arap} eq 'ap') {
 
 339         $paid = "paid = paid + $amount";
 
 341         $paid = "paid = $amount";
 
 344       # update AR/AP transaction
 
 345       $query = qq|UPDATE $arap SET $paid, datepaid = ? WHERE id = ?|;
 
 346       @values = (conv_date($form->{datepaid}), conv_i($form->{"id_$i"}));
 
 347       do_query($form, $dbh, $query, @values);
 
 349       $form->{id} = $form->{"id_$i"};
 
 350       if(!exists $form->{addition}) {
 
 351         $form->{snumbers}  = qq|invnumber_| . $form->{"invnumber_$i"};
 
 352         $form->{what_done} = "invoice";
 
 353         $form->{addition}  = "PAYMENT POSTED";
 
 356       # /saving the history
 
 360   # Hier wurden negativen Zahlungseingänge abgefangen
 
 361   # da Zahlungsein- und ausgänge immer positiv sind
 
 362   # Besser: in Oberfläche schon prüfen erledigt jb 10.2010
 
 367   $main::lxdebug->leave_sub();