File: CA.pm

package info (click to toggle)
sql-ledger 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 35,524 kB
  • ctags: 4,556
  • sloc: perl: 64,519; sql: 28,330; sh: 34; makefile: 21
file content (392 lines) | stat: -rw-r--r-- 9,792 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#=====================================================================
# SQL-Ledger ERP
# Copyright (C) 2006
#
#  Author: DWS Systems Inc.
#     Web: http://www.sql-ledger.com
#
#======================================================================
#
# chart of accounts
#
#======================================================================


package CA;


sub all_accounts {
  my ($self, $myconfig, $form) = @_;

  my $amount = ();
  # connect to database
  my $dbh = $form->dbconnect($myconfig);

  my $ref;
  
  my %defaults = $form->get_defaults($dbh, \@{['precision', 'company']});
  for (keys %defaults) { $form->{$_} = $defaults{$_} }
 
  my $query = qq|SELECT c.accno,
                 SUM(ac.amount) AS amount
                 FROM chart c
		 JOIN acc_trans ac ON (ac.chart_id = c.id)
		 WHERE ac.approved = '1'
		 GROUP BY c.accno|;
  my $sth = $dbh->prepare($query);
  $sth->execute || $form->dberror($query);

  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
    $amount{$ref->{accno}} = $ref->{amount}
  }
  $sth->finish;
 
  $query = qq|SELECT accno, description
              FROM gifi|;
  $sth = $dbh->prepare($query);
  $sth->execute || $form->dberror($query);

  my $gifi = ();
  while (my ($accno, $description) = $sth->fetchrow_array) {
    $gifi{$accno} = $description;
  }
  $sth->finish;

  $query = qq|SELECT c.id, c.accno, c.description, c.charttype, c.gifi_accno,
              c.category, c.link, c.contra,
	      l.description AS translation
              FROM chart c
	      LEFT JOIN translation l ON (l.trans_id = c.id AND l.language_code = '$myconfig->{countrycode}')
	      ORDER BY c.accno|;
  $sth = $dbh->prepare($query);
  $sth->execute || $form->dberror($query);
 
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
    $ref->{amount} = $amount{$ref->{accno}};
    $ref->{gifi_description} = $gifi{$ref->{gifi_accno}};
    if ($ref->{amount} < 0) {
      $ref->{debit} = $ref->{amount} * -1;
    } else {
      $ref->{credit} = $ref->{amount};
    }
    $ref->{description} = $ref->{translation} if $ref->{translation};
    push @{ $form->{CA} }, $ref;
  }

  $sth->finish;
  $dbh->disconnect;

}


sub all_transactions {
  my ($self, $myconfig, $form) = @_;

  # connect to database
  my $dbh = $form->dbconnect($myconfig);


  my %defaults = $form->get_defaults($dbh, \@{['precision', 'company']});
  for (keys %defaults) { $form->{$_} = $defaults{$_} }
    
  # get chart_id
  my $query = qq|SELECT id FROM chart
                 WHERE accno = '$form->{accno}'|;
  if ($form->{accounttype} eq 'gifi') {
    $query = qq|SELECT id FROM chart
                WHERE gifi_accno = '$form->{gifi_accno}'|;
  }
  my $sth = $dbh->prepare($query);
  $sth->execute || $form->dberror($query);

  my @id = ();
  while (my ($id) = $sth->fetchrow_array) {
    push @id, $id;
  }
  $sth->finish;

  my $fromdate_where;
  my $todate_where;

  ($form->{fromdate}, $form->{todate}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  
  if ($form->{fromdate}) {
    $fromdate_where = qq|
                 AND ac.transdate >= '$form->{fromdate}'
		|;
  }
  if ($form->{todate}) {
    $todate_where = qq|
                 AND ac.transdate <= '$form->{todate}'
		|;
  }
  

  my $false = ($myconfig->{dbdriver} =~ /Pg/) ? FALSE : q|'0'|;
  
  my $null;
  my $department_id;
  my $dpt_where;
  my $dpt_join;
  my $union;
  
  ($null, $department_id) = split /--/, $form->{department};
  
  if ($department_id) {
    $dpt_join = qq|
                   JOIN department t ON (t.id = a.department_id)
		  |;
    $dpt_where = qq|
		   AND t.id = $department_id
		  |;
  }

  my $project;
  my $project_id;
  if ($form->{projectnumber}) {
    ($null, $project_id) = split /--/, $form->{projectnumber};
    $project = qq|
                 AND ac.project_id = $project_id
		 |;
  }

  if ($form->{accno} || $form->{gifi_accno}) {
    # get category for account
    $query = qq|SELECT c.description, c.category, c.link, c.contra,
                l.description AS translation
                FROM chart c
		LEFT JOIN translation l ON (l.trans_id = c.id AND l.language_code = '$myconfig->{countrycode}')
		WHERE c.accno = '$form->{accno}'|;
    if ($form->{accounttype} eq 'gifi') {
      $query = qq|SELECT description, category, link, contra
                FROM chart
		WHERE gifi_accno = '$form->{gifi_accno}'
		AND charttype = 'A'|;
    }

    ($form->{description}, $form->{category}, $form->{link}, $form->{contra}, $form->{translation}) = $dbh->selectrow_array($query);

    $form->{description} = $form->{translation} if $form->{translation};
    
    if ($form->{fromdate}) {

      if ($department_id) {
	
	$query = ""; 
	$union = "";

	for (qw(ar ap gl)) {
	  
	  if ($form->{accounttype} eq 'gifi') {
	    $query = qq|
	                $union
			SELECT SUM(ac.amount)
			FROM acc_trans ac
			JOIN $_ a ON (a.id = ac.trans_id)
			JOIN chart c ON (ac.chart_id = c.id)
			WHERE c.gifi_accno = '$form->{gifi_accno}'
			AND ac.approved = '1'
			AND ac.transdate < '$form->{fromdate}'
			AND a.department_id = $department_id
			$project
			|;
		      
	  } else {

	    $query = qq|
			$union
			SELECT SUM(ac.amount)
			FROM acc_trans ac
			JOIN $_ a ON (a.id = ac.trans_id)
			JOIN chart c ON (ac.chart_id = c.id)
			WHERE c.accno = '$form->{accno}'
			AND ac.approved = '1'
			AND ac.transdate < '$form->{fromdate}'
			AND a.department_id = $department_id
			$project
			|;
	  }

	}
	
      } else {
	
	if ($form->{accounttype} eq 'gifi') {
	  $query = qq|SELECT SUM(ac.amount)
		    FROM acc_trans ac
		    JOIN chart c ON (ac.chart_id = c.id)
		    WHERE c.gifi_accno = '$form->{gifi_accno}'
		    AND ac.approved = '1'
		    AND ac.transdate < '$form->{fromdate}'
		    $project
		    |;
	} else {
	  $query = qq|SELECT SUM(ac.amount)
		      FROM acc_trans ac
		      JOIN chart c ON (ac.chart_id = c.id)
		      WHERE c.accno = '$form->{accno}'
		      AND ac.approved = '1'
		      AND ac.transdate < '$form->{fromdate}'
		      $project
		      |;
	}
      }
	
      ($form->{balance}) = $dbh->selectrow_array($query);
      
    }
  }

  $query = "";
  my $union = "";

  foreach my $id (@id) {
    
    # get all transactions
    $query .= qq|$union
                 SELECT a.id, a.reference, a.description, ac.transdate,
	         $false AS invoice, ac.amount, 'gl' as module, ac.cleared,
		 ac.source,
		 '' AS till, ac.chart_id, '0' AS vc_id
		 FROM gl a
		 JOIN acc_trans ac ON (ac.trans_id = a.id)
		 $dpt_join
		 WHERE ac.chart_id = $id
		 AND ac.approved = '1'
		 $fromdate_where
		 $todate_where
		 $dpt_where
		 $project
      
             UNION ALL
      
                 SELECT a.id, a.invnumber, c.name, ac.transdate,
	         a.invoice, ac.amount, 'ar' as module, ac.cleared,
		 ac.source,
		 a.till, ac.chart_id, c.id AS vc_id
		 FROM ar a
		 JOIN acc_trans ac ON (ac.trans_id = a.id)
		 JOIN customer c ON (a.customer_id = c.id)
		 $dpt_join
		 WHERE ac.chart_id = $id
		 AND ac.approved = '1'
		 $fromdate_where
		 $todate_where
		 $dpt_where
		 $project
      
             UNION ALL
      
                 SELECT a.id, a.invnumber, v.name, ac.transdate,
	         a.invoice, ac.amount, 'ap' as module, ac.cleared,
		 ac.source,
		 a.till, ac.chart_id, v.id AS vc_id
		 FROM ap a
		 JOIN acc_trans ac ON (ac.trans_id = a.id)
		 JOIN vendor v ON (a.vendor_id = v.id)
		 $dpt_join
		 WHERE ac.chart_id = $id
		 AND ac.approved = '1'
		 $fromdate_where
		 $todate_where
		 $dpt_where
		 $project
		 |;

    $union = qq|
             UNION ALL
                 |;
  }

  my @sf = qw(transdate reference description);
  my %ordinal = $form->ordinal_order($dbh, $query);
  $query .= qq| ORDER BY | .$form->sort_order(\@sf, \%ordinal);

  $sth = $dbh->prepare($query);
  $sth->execute || $form->dberror($query);

  $query = qq|SELECT c.id, c.accno FROM chart c
              JOIN acc_trans ac ON (ac.chart_id = c.id)
              WHERE ac.amount >= 0
	      AND (c.link = 'AR' OR c.link = 'AP')
	      AND ac.approved = '1'
	      AND ac.trans_id = ?|;
  my $dr = $dbh->prepare($query) || $form->dberror($query);
  
  $query = qq|SELECT c.id, c.accno FROM chart c
              JOIN acc_trans ac ON (ac.chart_id = c.id)
              WHERE ac.amount < 0
	      AND (c.link = 'AR' OR c.link = 'AP')
	      AND ac.approved = '1'
	      AND ac.trans_id = ?|;
  my $cr = $dbh->prepare($query) || $form->dberror($query);
  
  my $accno;
  my $chart_id;
  my %accno;
  
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
    
    # gl
    if ($ref->{module} eq "gl") {
      $ref->{module} = "gl";
      $ref->{vc_id} = 0;
      $ref->{db} = "";
    }

    # ap
    if ($ref->{module} eq "ap") {
      $ref->{module} = ($ref->{invoice}) ? 'ir' : 'ap';
      $ref->{module} = 'ps' if $ref->{till};
      $ref->{db} = "vendor";
    }

    # ar
    if ($ref->{module} eq "ar") {
      $ref->{module} = ($ref->{invoice}) ? 'is' : 'ar';
      $ref->{module} = 'ps' if $ref->{till};
      $ref->{db} = "customer";
    }

    if ($ref->{amount}) {
      %accno = ();

      if ($ref->{amount} < 0) {
	$ref->{debit} = $ref->{amount} * -1;
	$ref->{credit} = 0;
	$dr->execute($ref->{id});
	$ref->{accno} = ();
	while (($chart_id, $accno) = $dr->fetchrow_array) {
	  $accno{$accno} = 1 if $chart_id ne $ref->{chart_id};
	}
	$dr->finish;
	
	for (sort keys %accno) { push @{ $ref->{accno} }, "$_ " }

      } else {
	$ref->{credit} = $ref->{amount};
	$ref->{debit} = 0;
	
	$cr->execute($ref->{id});
	$ref->{accno} = ();
	while (($chart_id, $accno) = $cr->fetchrow_array) {
	  $accno{$accno} = 1 if $chart_id ne $ref->{chart_id};
	}
	$cr->finish;

	for (keys %accno) { push @{ $ref->{accno} }, "$_ " }

      }

      push @{ $form->{CA} }, $ref;
    }
    
  }
 
  $sth->finish;
  $dbh->disconnect;

}

1;