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 #======================================================================
 
  40   $main::lxdebug->enter_sub();
 
  42   my ($type, $countrycode) = @_;
 
  47     if (-f "locale/$countrycode/Num2text") {
 
  48       require "locale/$countrycode/Num2text";
 
  56   $main::lxdebug->leave_sub();
 
  62   $main::lxdebug->enter_sub();
 
  64   my ($self, $myconfig, $form) = @_;
 
  67   my $dbh = $form->dbconnect($myconfig);
 
  69   my $query = qq|SELECT c.accno, c.description, c.link
 
  71                  WHERE c.link LIKE '%$form->{ARAP}%'
 
  73   my $sth = $dbh->prepare($query);
 
  74   $sth->execute || $form->dberror($query);
 
  76   $form->{PR}{ $form->{ARAP} } = ();
 
  77   $form->{PR}{"$form->{ARAP}_paid"} = ();
 
  79   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
 
  80     foreach my $item (split /:/, $ref->{link}) {
 
  81       if ($item eq $form->{ARAP}) {
 
  82         push @{ $form->{PR}{ $form->{ARAP} } }, $ref;
 
  84       if ($item eq "$form->{ARAP}_paid") {
 
  85         push @{ $form->{PR}{"$form->{ARAP}_paid"} }, $ref;
 
  91   # get currencies and closedto
 
  92   $query = qq|SELECT curr, closedto
 
  94   $sth = $dbh->prepare($query);
 
  95   $sth->execute || $form->dberror($query);
 
  97   ($form->{currencies}, $form->{closedto}) = $sth->fetchrow_array;
 
 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 $query = qq|SELECT count(*)
 
 114                  FROM $form->{vc} ct, $arap a
 
 115                  WHERE a.$form->{vc}_id = ct.id
 
 116                  AND a.amount != a.paid|;
 
 117   my $sth = $dbh->prepare($query);
 
 118   $sth->execute || $form->dberror($query);
 
 119   my ($count) = $sth->fetchrow_array;
 
 124   # build selection list
 
 125   if ($count < $myconfig->{vclimit}) {
 
 126     $query = qq|SELECT DISTINCT ct.id, ct.name
 
 127                 FROM $form->{vc} ct, $arap a
 
 128                 WHERE a.$form->{vc}_id = ct.id
 
 129                 AND a.amount != a.paid
 
 131     $sth = $dbh->prepare($query);
 
 132     $sth->execute || $form->dberror($query);
 
 134     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 135       push @{ $form->{"all_$form->{vc}"} }, $ref;
 
 142   if ($form->{ARAP} eq 'AR') {
 
 143     $query = qq|SELECT d.id, d.description
 
 148     $query = qq|SELECT d.id, d.description
 
 152   $sth = $dbh->prepare($query);
 
 153   $sth->execute || $form->dberror($query);
 
 155   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 156     push @{ $form->{all_departments} }, $ref;
 
 162   $main::lxdebug->leave_sub();
 
 165 sub get_openinvoices {
 
 166   $main::lxdebug->enter_sub();
 
 168   my ($self, $myconfig, $form) = @_;
 
 170   # connect to database
 
 171   my $dbh = $form->dbconnect($myconfig);
 
 173   my $where = qq|WHERE a.$form->{vc}_id = $form->{"$form->{vc}_id"}
 
 174                  AND a.curr = '$form->{currency}'
 
 175                  AND NOT a.amount = paid|;
 
 178   if ($form->{vc} eq 'customer') {
 
 185     qq|SELECT a.id, a.invnumber, a.transdate, a.amount, a.paid, a.curr
 
 189   my $sth = $dbh->prepare($query);
 
 190   $sth->execute || $form->dberror($query);
 
 192   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
 
 194     # if this is a foreign currency transaction get exchangerate
 
 195     $ref->{exchangerate} =
 
 196       $form->get_exchangerate($dbh, $ref->{curr}, $ref->{transdate}, $buysell)
 
 197       if ($form->{currency} ne $form->{defaultcurrency});
 
 198     push @{ $form->{PR} }, $ref;
 
 204   $main::lxdebug->leave_sub();
 
 207 sub process_payment {
 
 208   $main::lxdebug->enter_sub();
 
 210   my ($self, $myconfig, $form) = @_;
 
 212   # connect to database, turn AutoCommit off
 
 213   my $dbh = $form->dbconnect_noauto($myconfig);
 
 215   my ($paymentaccno) = split /--/, $form->{account};
 
 217   # if currency ne defaultcurrency update exchangerate
 
 218   if ($form->{currency} ne $form->{defaultcurrency}) {
 
 219     $form->{exchangerate} =
 
 220       $form->parse_amount($myconfig, $form->{exchangerate});
 
 222     if ($form->{vc} eq 'customer') {
 
 223       $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid},
 
 224                                  $form->{exchangerate}, 0);
 
 226       $form->update_exchangerate($dbh, $form->{currency}, $form->{datepaid}, 0,
 
 227                                  $form->{exchangerate});
 
 230     $form->{exchangerate} = 1;
 
 233   my $query = qq|SELECT fxgain_accno_id, fxloss_accno_id
 
 235   my $sth = $dbh->prepare($query);
 
 236   $sth->execute || $form->dberror($query);
 
 238   my ($fxgain_accno_id, $fxloss_accno_id) = $sth->fetchrow_array;
 
 243   if ($form->{vc} eq 'customer') {
 
 252   if ($form->{ARAP} eq 'AR') {
 
 256                 OR c.link LIKE 'AR:%')
 
 262                 OR c.link LIKE '%:AP'
 
 263                 OR c.link LIKE '%:AP:%')
 
 267   $paymentamount = $form->{amount};
 
 269   #  $paymentamount = $form->{amount};
 
 271   ($null, $form->{department_id}) = split /--/, $form->{department};
 
 272   $form->{department_id} *= 1;
 
 274   # query to retrieve paid amount
 
 275   $query = qq|SELECT a.paid FROM ar a
 
 278   my $pth = $dbh->prepare($query) || $form->dberror($query);
 
 280   # go through line by line
 
 281   for my $i (1 .. $form->{rowcount}) {
 
 283     $form->{"paid_$i"} = $form->parse_amount($myconfig, $form->{"paid_$i"});
 
 284     $form->{"due_$i"}  = $form->parse_amount($myconfig, $form->{"due_$i"});
 
 286     if ($form->{"checked_$i"} && $form->{"paid_$i"}) {
 
 288         (($paymentamount * 1000) - ($form->{"paid_$i"} * 1000)) / 1000;
 
 290       # get exchangerate for original
 
 291       $query = qq|SELECT $buysell
 
 293                   JOIN $form->{arap} a ON (a.transdate = e.transdate)
 
 294                   WHERE e.curr = '$form->{currency}'
 
 295                   AND a.id = $form->{"id_$i"}|;
 
 296       $sth = $dbh->prepare($query);
 
 297       $sth->execute || $form->dberror($query);
 
 299       my ($exchangerate) = $sth->fetchrow_array;
 
 302       $exchangerate = 1 unless $exchangerate;
 
 304       $query = qq|SELECT c.id
 
 306                   JOIN acc_trans a ON (a.chart_id = c.id)
 
 308                   AND a.trans_id = $form->{"id_$i"}|;
 
 309       $sth = $dbh->prepare($query);
 
 310       $sth->execute || $form->dberror($query);
 
 312       my ($id) = $sth->fetchrow_array;
 
 315       $amount = $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
 
 318       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
 
 320                   VALUES ($form->{"id_$i"}, $id, '$form->{datepaid}',
 
 322       $dbh->do($query) || $form->dberror($query);
 
 325       $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
 
 326                   amount, source, memo)
 
 327                   VALUES ($form->{"id_$i"},
 
 328                          (SELECT c.id FROM chart c
 
 329                           WHERE c.accno = '$paymentaccno'),
 
 330                   '$form->{datepaid}', $form->{"paid_$i"} * $ml * -1,
 
 331                   '$form->{source}', '$form->{memo}')|;
 
 332       $dbh->do($query) || $form->dberror($query);
 
 334       # add exchangerate difference if currency ne defaultcurrency
 
 336         $form->round_amount($form->{"paid_$i"} * ($form->{exchangerate} - 1),
 
 340         # exchangerate difference
 
 341         $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
 
 342                     amount, cleared, fx_transaction)
 
 343                     VALUES ($form->{"id_$i"},
 
 344                            (SELECT c.id FROM chart c
 
 345                             WHERE c.accno = '$paymentaccno'),
 
 346                   '$form->{datepaid}', $amount * $ml * -1, '0', '1')|;
 
 347         $dbh->do($query) || $form->dberror($query);
 
 353                   $form->{"paid_$i"} * ($exchangerate - $form->{exchangerate}),
 
 356           my $accno_id = ($amount < 0) ? $fxgain_accno_id : $fxloss_accno_id;
 
 357           $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
 
 358                       amount, cleared, fx_transaction)
 
 359                       VALUES ($form->{"id_$i"}, $accno_id,
 
 360                       '$form->{datepaid}', $amount * $ml * -1, '0', '1')|;
 
 361           $dbh->do($query) || $form->dberror($query);
 
 366         $form->round_amount($form->{"paid_$i"} * $exchangerate, 2);
 
 368       $pth->execute($form->{"id_$i"}) || $form->dberror;
 
 369       ($amount) = $pth->fetchrow_array;
 
 372       $amount += $form->{"paid_$i"};
 
 374       # update AR/AP transaction
 
 375       $query = qq|UPDATE $form->{arap} set
 
 377                   datepaid = '$form->{datepaid}'
 
 378                   WHERE id = $form->{"id_$i"}|;
 
 379       $dbh->do($query) || $form->dberror($query);
 
 383   # record a AR/AP with a payment
 
 384   if ($form->round_amount($paymentamount, 2) > 0) {
 
 385     $form->{invnumber} = "";
 
 386     OP::overpayment("", $myconfig, $form, $dbh, $paymentamount, $ml, 1);
 
 389   if ($form->round_amount($paymentamount, 2) < 0) {
 
 393   if ($form->round_amount($paymentamount, 2) == 0) {
 
 399   $main::lxdebug->leave_sub();