Alle Dateien durch Perltidy laufen lassen. Die verwendeten Optionen sind am Ende...
[kivitendo-erp.git] / SL / OP.pm
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) 2003
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #  Contributors:
16 #
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.
21 #
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 #======================================================================
30 #
31 # Overpayment function
32 # used in AR, AP, IS, IR, OE, CP
33 #======================================================================
34
35 package OP;
36
37 sub overpayment {
38   $main::lxdebug->enter_sub();
39
40   my ($self, $myconfig, $form, $dbh, $amount, $ml) = @_;
41
42   my $fxamount = $form->round_amount($amount * $form->{exchangerate}, 2);
43   my ($paymentaccno) = split /--/, $form->{account};
44
45   my $vc_id = "$form->{vc}_id";
46
47   my $uid = time;
48   $uid .= $form->{login};
49
50   # add AR/AP header transaction with a payment
51   $query = qq|INSERT INTO $form->{arap} (invnumber, employee_id)
52               VALUES ('$uid', (SELECT e.id FROM employee e
53                              WHERE e.login = '$form->{login}'))|;
54   $dbh->do($query) || $form->dberror($query);
55
56   $query = qq|SELECT a.id FROM $form->{arap} a
57             WHERE a.invnumber = '$uid'|;
58   $sth = $dbh->prepare($query);
59   $sth->execute || $form->dberror($query);
60
61   ($uid) = $sth->fetchrow_array;
62   $sth->finish;
63
64   my $invnumber = ($form->{invnumber}) ? $form->{invnumber} : $uid;
65   $query = qq|UPDATE $form->{arap} set
66               invnumber = '$invnumber',
67               $vc_id = $form->{"$form->{vc}_id"},
68               transdate = '$form->{datepaid}',
69               datepaid = '$form->{datepaid}',
70               duedate = '$form->{datepaid}',
71               netamount = 0,
72               amount = 0,
73               paid = $fxamount,
74               curr = '$form->{currency}',
75               department_id = $form->{department_id}
76               WHERE id = $uid|;
77   $dbh->do($query) || $form->dberror($query);
78
79   # add AR/AP
80   ($accno) = split /--/, $form->{ $form->{ARAP} };
81
82   $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate, amount)
83               VALUES ($uid, (SELECT c.id FROM chart c
84                              WHERE c.accno = '$accno'),
85               '$form->{datepaid}', $fxamount * $ml)|;
86   $dbh->do($query) || $form->dberror($query);
87
88   # add payment
89   $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
90               amount, source, memo)
91               VALUES ($uid, (SELECT c.id FROM chart c
92                              WHERE c.accno = '$paymentaccno'),
93                 '$form->{datepaid}', $amount * $ml * -1,
94                 '$form->{source}', '$form->{memo}')|;
95   $dbh->do($query) || $form->dberror($query);
96
97   # add exchangerate difference
98   if ($fxamount != $amount) {
99     $query = qq|INSERT INTO acc_trans (trans_id, chart_id, transdate,
100                 amount, cleared, fx_transaction)
101                 VALUES ($uid, (SELECT c.id FROM chart c
102                                WHERE c.accno = '$paymentaccno'),
103               '$form->{datepaid}', ($fxamount - $amount) * $ml * -1,
104               '1', '1')|;
105     $dbh->do($query) || $form->dberror($query);
106   }
107
108   $main::lxdebug->leave_sub();
109 }
110
111 1;
112