1 #=====================================================================
2 # SQL-Ledger Accounting
5 # Author: Dieter Simader
6 # Email: dsimader@sql-ledger.org
7 # Web: http://www.sql-ledger.org
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
25 # General ledger backend code
28 # DS. 2000-07-04 Created
29 # DS. 2001-06-12 Changed relations from accno to chart_id
31 #======================================================================
36 sub delete_transaction {
37 my ($self, $myconfig, $form) = @_;
40 my $dbh = $form->dbconnect_noauto($myconfig);
42 my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
43 $dbh->do($query) || $form->dberror($query);
45 $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
46 $dbh->do($query) || $form->dberror($query);
49 my $rc = $dbh->commit;
57 sub post_transaction {
58 my ($self, $myconfig, $form) = @_;
60 my ($debit, $credit) = (0, 0);
64 # check if debit and credit balances
65 for $i (1 .. $form->{rowcount}) {
66 if ($form->{"debit_$i"} && $form->{"credit_$i"}) {
70 $form->{"debit_$i"} = $form->parse_amount($myconfig, $form->{"debit_$i"});
71 $form->{"credit_$i"} = $form->parse_amount($myconfig, $form->{"credit_$i"});
73 $debit += $form->{"debit_$i"};
74 $credit += $form->{"credit_$i"};
77 $debit = $form->round_amount($debit, 2);
78 $credit = $form->round_amount($credit, 2);
80 if ($debit != $credit) {
84 if (($debit + $credit) == 0) {
88 # connect to database, turn off AutoCommit
89 my $dbh = $form->dbconnect_noauto($myconfig);
91 # post the transaction
92 # make up a unique handle and store in reference field
93 # then retrieve the record based on the unique handle to get the id
94 # replace the reference field with the actual variable
95 # add records to acc_trans
97 # if there is a $form->{id} replace the old transaction
98 # delete all acc_trans entries and add the new ones
101 map { $form->{$_} =~ s/'/''/g } qw(reference description);
107 # delete individual transactions
108 $query = qq|DELETE FROM acc_trans
109 WHERE trans_id = $form->{id}|;
110 $dbh->do($query) || $form->dberror($query);
114 $uid .= $form->{login};
116 $query = qq|INSERT INTO gl (reference, employee_id)
117 VALUES ('$uid', (SELECT id FROM employee
118 WHERE login = '$form->{login}'))|;
119 $dbh->do($query) || $form->dberror($query);
121 $query = qq|SELECT id FROM gl
122 WHERE reference = '$uid'|;
123 $sth = $dbh->prepare($query);
124 $sth->execute || $form->dberror($query);
126 ($form->{id}) = $sth->fetchrow_array;
131 $query = qq|UPDATE gl SET
132 reference = '$form->{reference}',
133 description = '$form->{description}',
134 notes = '$form->{notes}',
135 transdate = '$form->{transdate}'
136 WHERE id = $form->{id}|;
138 $dbh->do($query) || $form->dberror($query);
141 # insert acc_trans transactions
142 for $i (1 .. $form->{rowcount}) {
144 ($accno) = split(/--/, $form->{"accno_$i"});
147 if ($form->{"credit_$i"} != 0) {
148 $amount = $form->{"credit_$i"};
150 if ($form->{"debit_$i"} != 0) {
151 $amount = $form->{"debit_$i"} * -1;
155 # if there is an amount, add the record
157 $project_id = ($form->{"project_id_$i"}) ? $form->{"project_id_$i"} : 'NULL';
158 $query = qq|INSERT INTO acc_trans (trans_id, chart_id, amount, transdate,
161 ($form->{id}, (SELECT id
163 WHERE accno = '$accno'),
164 $amount, '$form->{transdate}', '$form->{reference}',
167 $dbh->do($query) || $form->dberror($query);
171 # commit and redirect
172 my $rc = $dbh->commit;
181 sub all_transactions {
182 my ($self, $myconfig, $form) = @_;
184 # connect to database
185 my $dbh = $form->dbconnect($myconfig);
189 my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
191 if ($form->{reference}) {
192 my $source = $form->like(lc $form->{reference});
193 $glwhere .= " AND lower(g.reference) LIKE '$source'";
194 $arwhere .= " AND lower(a.invnumber) LIKE '$source'";
195 $apwhere .= " AND lower(a.invnumber) LIKE '$source'";
197 if ($form->{source}) {
198 my $source = $form->like(lc $form->{source});
199 $glwhere .= " AND lower(ac.source) LIKE '$source'";
200 $arwhere .= " AND lower(ac.source) LIKE '$source'";
201 $apwhere .= " AND lower(ac.source) LIKE '$source'";
203 if ($form->{datefrom}) {
204 $glwhere .= " AND ac.transdate >= '$form->{datefrom}'";
205 $arwhere .= " AND ac.transdate >= '$form->{datefrom}'";
206 $apwhere .= " AND ac.transdate >= '$form->{datefrom}'";
208 if ($form->{dateto}) {
209 $glwhere .= " AND ac.transdate <= '$form->{dateto}'";
210 $arwhere .= " AND ac.transdate <= '$form->{dateto}'";
211 $apwhere .= " AND ac.transdate <= '$form->{dateto}'";
213 if ($form->{description}) {
214 my $description = $form->like(lc $form->{description});
215 $glwhere .= " AND lower(g.description) LIKE '$description'";
216 $arwhere .= " AND lower(ct.name) LIKE '$description'";
217 $apwhere .= " AND lower(ct.name) LIKE '$description'";
219 if ($form->{notes}) {
220 my $notes = $form->like(lc $form->{notes});
221 $glwhere .= " AND lower(g.notes) LIKE '$notes'";
222 $arwhere .= " AND lower(a.notes) LIKE '$notes'";
223 $apwhere .= " AND lower(a.notes) LIKE '$notes'";
225 if ($form->{accno}) {
226 $glwhere .= " AND c.accno = '$form->{accno}'";
227 $arwhere .= " AND c.accno = '$form->{accno}'";
228 $apwhere .= " AND c.accno = '$form->{accno}'";
230 if ($form->{gifi_accno}) {
231 $glwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
232 $arwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
233 $apwhere .= " AND c.gifi_accno = '$form->{gifi_accno}'";
235 if ($form->{category} ne 'X') {
236 $glwhere .= " AND c.category = '$form->{category}'";
237 $arwhere .= " AND c.category = '$form->{category}'";
238 $apwhere .= " AND c.category = '$form->{category}'";
241 if ($form->{accno}) {
242 # get category for account
243 $query = qq|SELECT category
245 WHERE accno = '$form->{accno}'|;
246 $sth = $dbh->prepare($query);
248 $sth->execute || $form->dberror($query);
249 ($form->{ml}) = $sth->fetchrow_array;
252 if ($form->{datefrom}) {
253 $query = qq|SELECT SUM(ac.amount)
254 FROM acc_trans ac, chart c
255 WHERE ac.chart_id = c.id
256 AND c.accno = '$form->{accno}'
257 AND ac.transdate < date '$form->{datefrom}'
259 $sth = $dbh->prepare($query);
260 $sth->execute || $form->dberror($query);
262 ($form->{balance}) = $sth->fetchrow_array;
267 if ($form->{gifi_accno}) {
268 # get category for account
269 $query = qq|SELECT category
271 WHERE gifi_accno = '$form->{gifi_accno}'|;
272 $sth = $dbh->prepare($query);
274 $sth->execute || $form->dberror($query);
275 ($form->{ml}) = $sth->fetchrow_array;
278 if ($form->{datefrom}) {
279 $query = qq|SELECT SUM(ac.amount)
280 FROM acc_trans ac, chart c
281 WHERE ac.chart_id = c.id
282 AND c.gifi_accno = '$form->{gifi_accno}'
283 AND ac.transdate < date '$form->{datefrom}'
285 $sth = $dbh->prepare($query);
286 $sth->execute || $form->dberror($query);
288 ($form->{balance}) = $sth->fetchrow_array;
293 my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE : q|'0'|;
295 my $sortorder = join ', ', $form->sort_columns(qw(transdate reference source description accno));
296 my %ordinal = ( transdate => 6,
300 map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
302 my $query = qq|SELECT g.id, 'gl' AS type, $false AS invoice, g.reference,
303 g.description, ac.transdate, ac.source,
304 ac.amount, c.accno, c.gifi_accno, g.notes
305 FROM gl g, acc_trans ac, chart c
307 AND ac.chart_id = c.id
308 AND g.id = ac.trans_id
310 SELECT a.id, 'ar' AS type, a.invoice, a.invnumber,
311 ct.name, ac.transdate, ac.source,
312 ac.amount, c.accno, c.gifi_accno, a.notes
313 FROM ar a, acc_trans ac, chart c, customer ct
315 AND ac.chart_id = c.id
316 AND a.customer_id = ct.id
317 AND a.id = ac.trans_id
319 SELECT a.id, 'ap' AS type, a.invoice, a.invnumber,
320 ct.name, ac.transdate, ac.source,
321 ac.amount, c.accno, c.gifi_accno, a.notes
322 FROM ap a, acc_trans ac, chart c, vendor ct
324 AND ac.chart_id = c.id
325 AND a.vendor_id = ct.id
326 AND a.id = ac.trans_id
327 ORDER BY $sortorder|;
328 my $sth = $dbh->prepare($query);
329 $sth->execute || $form->dberror($query);
331 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
334 if ($ref->{type} eq "gl") {
335 $ref->{module} = "gl";
339 if ($ref->{type} eq "ap") {
340 if ($ref->{invoice}) {
341 $ref->{module} = "ir";
343 $ref->{module} = "ap";
348 if ($ref->{type} eq "ar") {
349 if ($ref->{invoice}) {
350 $ref->{module} = "is";
352 $ref->{module} = "ar";
356 if ($ref->{amount} < 0) {
357 $ref->{debit} = $ref->{amount} * -1;
360 $ref->{credit} = $ref->{amount};
364 push @{ $form->{GL} }, $ref;
370 if ($form->{accno}) {
371 $query = qq|SELECT description FROM chart WHERE accno = '$form->{accno}'|;
372 $sth = $dbh->prepare($query);
373 $sth->execute || $form->dberror($query);
375 ($form->{account_description}) = $sth->fetchrow_array;
378 if ($form->{gifi_accno}) {
379 $query = qq|SELECT description FROM gifi WHERE accno = '$form->{gifi_accno}'|;
380 $sth = $dbh->prepare($query);
381 $sth->execute || $form->dberror($query);
383 ($form->{gifi_account_description}) = $sth->fetchrow_array;
393 my ($self, $myconfig, $form) = @_;
397 # connect to database
398 my $dbh = $form->dbconnect($myconfig);
401 $query = "SELECT closedto, revtrans
403 $sth = $dbh->prepare($query);
404 $sth->execute || $form->dberror($query);
406 ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
409 $query = "SELECT reference, description, notes, transdate
411 WHERE id = $form->{id}";
412 $sth = $dbh->prepare($query);
413 $sth->execute || $form->dberror($query);
415 ($form->{reference}, $form->{description}, $form->{notes}, $form->{transdate}) = $sth->fetchrow_array;
418 # retrieve individual rows
419 $query = "SELECT c.accno, a.amount, project_id,
420 (SELECT p.projectnumber FROM project p
421 WHERE a.project_id = p.id) AS projectnumber
422 FROM acc_trans a, chart c
423 WHERE a.chart_id = c.id
424 AND a.trans_id = $form->{id}
426 $sth = $dbh->prepare($query);
427 $sth->execute || $form->dberror($query);
429 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
430 push @{ $form->{GL} }, $ref;
433 $query = "SELECT current_date AS transdate, closedto, revtrans
435 $sth = $dbh->prepare($query);
436 $sth->execute || $form->dberror($query);
438 ($form->{transdate}, $form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
443 # get chart of accounts
444 $query = qq|SELECT accno,description
446 WHERE charttype = 'A'
448 $sth = $dbh->prepare($query);
449 $sth->execute || $form->dberror($query);
451 while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
452 push @{ $form->{chart} }, $ref;