This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / old / sql-ledger / SL / GL.pm
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2001
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors:
10 #
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.
15 #
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 #======================================================================
24 #
25 # General ledger backend code
26 #
27 # CHANGE LOG:
28 #   DS. 2000-07-04  Created
29 #   DS. 2001-06-12  Changed relations from accno to chart_id
30 #
31 #======================================================================
32
33 package GL;
34
35
36 sub delete_transaction {
37   my ($self, $myconfig, $form) = @_;
38   
39   # connect to database
40   my $dbh = $form->dbconnect_noauto($myconfig);
41
42   my $query = qq|DELETE FROM gl WHERE id = $form->{id}|;
43   $dbh->do($query) || $form->dberror($query);
44
45   $query = qq|DELETE FROM acc_trans WHERE trans_id = $form->{id}|;
46   $dbh->do($query) || $form->dberror($query);
47
48   # commit and redirect
49   my $rc = $dbh->commit;
50   $dbh->disconnect;
51   
52   $rc;
53   
54 }
55
56
57 sub post_transaction {
58   my ($self, $myconfig, $form) = @_;
59   
60   my ($debit, $credit) = (0, 0);
61   my $project_id;
62
63   my $i;
64   # check if debit and credit balances
65   for $i (1 .. $form->{rowcount}) {
66     if ($form->{"debit_$i"} && $form->{"credit_$i"}) {
67       return -1;
68     }
69
70     $form->{"debit_$i"} = $form->parse_amount($myconfig, $form->{"debit_$i"});
71     $form->{"credit_$i"} = $form->parse_amount($myconfig, $form->{"credit_$i"});
72
73     $debit += $form->{"debit_$i"};
74     $credit += $form->{"credit_$i"};
75   }
76
77   $debit = $form->round_amount($debit, 2);
78   $credit = $form->round_amount($credit, 2);
79   
80   if ($debit != $credit) {
81     return -2;
82   }
83
84   if (($debit + $credit) == 0) {
85     return -3;
86   }
87   
88   # connect to database, turn off AutoCommit
89   my $dbh = $form->dbconnect_noauto($myconfig);
90
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
96
97   # if there is a $form->{id} replace the old transaction
98   # delete all acc_trans entries and add the new ones
99
100   # escape '
101   map { $form->{$_} =~ s/'/''/g } qw(reference description);
102
103
104   my ($query, $sth);
105   
106   if ($form->{id}) {
107     # delete individual transactions
108     $query = qq|DELETE FROM acc_trans 
109                 WHERE trans_id = $form->{id}|;
110     $dbh->do($query) || $form->dberror($query);
111     
112   } else {
113     my $uid = time;
114     $uid .= $form->{login};
115
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);
120     
121     $query = qq|SELECT id FROM gl
122                 WHERE reference = '$uid'|;
123     $sth = $dbh->prepare($query);
124     $sth->execute || $form->dberror($query);
125
126     ($form->{id}) = $sth->fetchrow_array;
127     $sth->finish;
128
129   }
130   
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}|;
137            
138   $dbh->do($query) || $form->dberror($query);
139
140  
141   # insert acc_trans transactions
142   for $i (1 .. $form->{rowcount}) {
143     # extract accno
144     ($accno) = split(/--/, $form->{"accno_$i"});
145     my $amount = 0;
146     
147     if ($form->{"credit_$i"} != 0) {
148       $amount = $form->{"credit_$i"};
149     }
150     if ($form->{"debit_$i"} != 0) {
151       $amount = $form->{"debit_$i"} * -1;
152     }
153
154
155     # if there is an amount, add the record
156     if ($amount != 0) {
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,
159                   source, project_id)
160                   VALUES
161                   ($form->{id}, (SELECT id
162                                  FROM chart
163                                  WHERE accno = '$accno'),
164                    $amount, '$form->{transdate}', '$form->{reference}',
165                   $project_id)|;
166     
167       $dbh->do($query) || $form->dberror($query);
168     }
169   }
170
171   # commit and redirect
172   my $rc = $dbh->commit;
173   $dbh->disconnect;
174
175   $rc;
176
177 }
178
179
180
181 sub all_transactions {
182   my ($self, $myconfig, $form) = @_;
183
184   # connect to database
185   my $dbh = $form->dbconnect($myconfig);
186   my $query;
187   my $sth;
188
189   my ($glwhere, $arwhere, $apwhere) = ("1 = 1", "1 = 1", "1 = 1");
190   
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'";
196   }
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'";
202   }
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}'";
207   }
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}'";
212   }
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'";
218   }
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'";
224   }
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}'";
229   }
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}'";
234   }
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}'";
239   }
240
241   if ($form->{accno}) {
242     # get category for account
243     $query = qq|SELECT category
244                 FROM chart
245                 WHERE accno = '$form->{accno}'|;
246     $sth = $dbh->prepare($query); 
247
248     $sth->execute || $form->dberror($query); 
249     ($form->{ml}) = $sth->fetchrow_array; 
250     $sth->finish; 
251     
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}'
258                   |;
259       $sth = $dbh->prepare($query);
260       $sth->execute || $form->dberror($query);
261
262       ($form->{balance}) = $sth->fetchrow_array;
263       $sth->finish;
264     }
265   }
266   
267   if ($form->{gifi_accno}) {
268     # get category for account
269     $query = qq|SELECT category
270                 FROM chart
271                 WHERE gifi_accno = '$form->{gifi_accno}'|;
272     $sth = $dbh->prepare($query); 
273
274     $sth->execute || $form->dberror($query); 
275     ($form->{ml}) = $sth->fetchrow_array; 
276     $sth->finish; 
277    
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}'
284                   |;
285       $sth = $dbh->prepare($query);
286       $sth->execute || $form->dberror($query);
287
288       ($form->{balance}) = $sth->fetchrow_array;
289       $sth->finish;
290     }
291   }
292
293   my $false = ($myconfig->{dbdriver} eq 'Pg') ? FALSE : q|'0'|;
294
295   my $sortorder = join ', ', $form->sort_columns(qw(transdate reference source description accno));
296   my %ordinal = ( transdate => 6,
297                   reference => 4,
298                   source => 7,
299                   description => 5 );
300   map { $sortorder =~ s/$_/$ordinal{$_}/ } keys %ordinal;
301   
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
306                  WHERE $glwhere
307                  AND ac.chart_id = c.id
308                  AND g.id = ac.trans_id
309         UNION ALL
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
314                  WHERE $arwhere
315                  AND ac.chart_id = c.id
316                  AND a.customer_id = ct.id
317                  AND a.id = ac.trans_id
318         UNION ALL
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
323                  WHERE $apwhere
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);
330
331   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
332
333     # gl
334     if ($ref->{type} eq "gl") {
335       $ref->{module} = "gl";
336     }
337
338     # ap
339     if ($ref->{type} eq "ap") {
340       if ($ref->{invoice}) {
341         $ref->{module} = "ir";
342       } else {
343         $ref->{module} = "ap";
344       }
345     }
346
347     # ar
348     if ($ref->{type} eq "ar") {
349       if ($ref->{invoice}) {
350         $ref->{module} = "is";
351       } else {
352         $ref->{module} = "ar";
353       }
354     }
355
356     if ($ref->{amount} < 0) {
357       $ref->{debit} = $ref->{amount} * -1;
358       $ref->{credit} = 0;
359     } else {
360       $ref->{credit} = $ref->{amount};
361       $ref->{debit} = 0;
362     }
363
364     push @{ $form->{GL} }, $ref;
365     
366   }
367
368   $sth->finish;
369
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);
374
375     ($form->{account_description}) = $sth->fetchrow_array;
376     $sth->finish;
377   }
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);
382
383     ($form->{gifi_account_description}) = $sth->fetchrow_array;
384     $sth->finish;
385   }
386  
387   $dbh->disconnect;
388
389 }
390
391
392 sub transaction {
393   my ($self, $myconfig, $form) = @_;
394   
395   my ($query, $sth);
396   
397   # connect to database
398   my $dbh = $form->dbconnect($myconfig);
399
400   if ($form->{id}) {
401     $query = "SELECT closedto, revtrans
402               FROM defaults";
403     $sth = $dbh->prepare($query);
404     $sth->execute || $form->dberror($query);
405
406     ($form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
407     $sth->finish;
408
409     $query = "SELECT reference, description, notes, transdate
410               FROM gl
411               WHERE id = $form->{id}";
412     $sth = $dbh->prepare($query);
413     $sth->execute || $form->dberror($query);
414
415     ($form->{reference}, $form->{description}, $form->{notes}, $form->{transdate}) = $sth->fetchrow_array;
416     $sth->finish;
417   
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}
425               ORDER BY accno";
426     $sth = $dbh->prepare($query);
427     $sth->execute || $form->dberror($query);
428     
429     while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
430       push @{ $form->{GL} }, $ref;
431     }
432   } else {
433     $query = "SELECT current_date AS transdate, closedto, revtrans
434               FROM defaults";
435     $sth = $dbh->prepare($query);
436     $sth->execute || $form->dberror($query);
437
438     ($form->{transdate}, $form->{closedto}, $form->{revtrans}) = $sth->fetchrow_array;
439   }
440
441   $sth->finish;
442
443   # get chart of accounts
444   $query = qq|SELECT accno,description
445               FROM chart
446               WHERE charttype = 'A'
447               ORDER by accno|;
448   $sth = $dbh->prepare($query);
449   $sth->execute || $form->dberror($query);
450   $form->{chart} = "";
451   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
452     push @{ $form->{chart} }, $ref;
453   }
454   $sth->finish;
455   
456   $dbh->disconnect;
457
458 }
459
460
461 1;
462