summaryrefslogtreecommitdiff
path: root/sql-ledger/SL/BP.pm
blob: d85077db244e9dfd738c3efa0d7b641f711871b6 (plain)
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
#=====================================================================
# SQL-Ledger Accounting
# Copyright (C) 2003
#
#  Author: Dieter Simader
#   Email: dsimader@sql-ledger.org
#     Web: http://www.sql-ledger.org
#
#  Contributors:
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#======================================================================
#
# Batch printing module backend routines
#
#======================================================================

package BP;


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

  # connect to database
  my $dbh = $form->dbconnect($myconfig);
  
  my %arap = ( invoice => ['ar'],
               packing_list => ['oe', 'ar'],
	       sales_order => ['oe'],
	       work_order => ['oe'],
	       pick_list => ['oe', 'ar'],
	       purchase_order => ['oe'],
	       bin_list => ['oe'],
	       sales_quotation => ['oe'],
	       request_quotation => ['oe'],
	       check => ['ap'],
	       receipt => ['ar']
	     );
  
  my $query = "";
  my $sth;
  my $n;
  my $count;
  my $item;
  
  foreach $item (@{ $arap{$form->{type}} }) {
    $query = qq|
              SELECT count(*)
	      FROM (SELECT DISTINCT vc.id
		    FROM $form->{vc} vc, $item a, status s
		    WHERE a.$form->{vc}_id = vc.id
		    AND s.trans_id = a.id
		    AND s.formname = '$form->{type}'
		    AND s.spoolfile IS NOT NULL) AS total|;
    ($n) = $dbh->selectrow_array($query);
    $count += $n;
  }


  # build selection list
  my $union = "";
  $query = "";
  if ($count < $myconfig->{vclimit}) {
    foreach $item (@{ $arap{$form->{type}} }) {
      $query .= qq|
                  $union
		  SELECT DISTINCT vc.id, vc.name
		  FROM $form->{vc} vc, $item a, status s
		  WHERE a.$form->{vc}_id = vc.id
		  AND s.trans_id = a.id
		  AND s.formname = '$form->{type}'
		  AND s.spoolfile IS NOT NULL|;
      $union = "UNION";
    }
    
    $sth = $dbh->prepare($query);
    $sth->execute || $form->dberror($query);

    while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
      push @{ $form->{"all_$form->{vc}"} }, $ref;
    }
    $sth->finish;
  }

  $form->all_years($dbh, $myconfig);

  $dbh->disconnect;
 
}
		 
  

sub payment_accounts {
  my ($self, $myconfig, $form) = @_;
  
  # connect to database
  my $dbh = $form->dbconnect($myconfig);

  my $query = qq|SELECT DISTINCT c.accno, c.description
                 FROM status s, chart c
		 WHERE s.chart_id = c.id
		 AND s.formname = '$form->{type}'|;
  my $sth = $dbh->prepare($query);
  $sth->execute || $form->dberror($query);

  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
    push @{ $form->{accounts} }, $ref;
  }
  
  $sth->finish;
  $dbh->disconnect;
  
}

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

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

  my $query;
  my $invnumber = "invnumber";
  my $item;
  
  my %arap = ( invoice => ['ar'],
               packing_list => ['oe', 'ar'],
	       sales_order => ['oe'],
	       work_order => ['oe'],
	       pick_list => ['oe', 'ar'],
	       purchase_order => ['oe'],
	       bin_list => ['oe'],
	       sales_quotation => ['oe'],
	       request_quotation => ['oe'],
	       check => ['ap'],
	       receipt => ['ar']
	     );
  

  if ($form->{type} eq 'check' || $form->{type} eq 'receipt') {
    
    my ($accno) = split /--/, $form->{account};
    
    $query = qq|SELECT a.id, vc.name, a.invnumber, ac.transdate, s.spoolfile,
                a.invoice, '$arap{$form->{type}}[0]' AS module
                FROM acc_trans ac
		JOIN chart c ON (c.id = ac.chart_id)
		JOIN $arap{$form->{type}}[0] a ON (a.id = ac.trans_id)
		JOIN status s ON (s.trans_id = a.id)
		JOIN $form->{vc} vc ON (vc.id = a.$form->{vc}_id)
		WHERE s.formname = '$form->{type}'
		AND c.accno = '$accno'
		AND NOT ac.fx_transaction|;

      if ($form->{"$form->{vc}_id"}) {
	$query .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
      } else {
	if ($form->{$form->{vc}}) {
	  $item = $form->like(lc $form->{$form->{vc}});
	  $query .= " AND lower(vc.name) LIKE '$item'";
	}
      }
      if ($form->{invnumber}) {
	$item = $form->like(lc $form->{invnumber});
	$query .= " AND lower(a.invnumber) LIKE '$item'";
      }
      if ($form->{ordnumber}) {
	$item = $form->like(lc $form->{ordnumber});
	$query .= " AND lower(a.ordnumber) LIKE '$item'";
      }
      if ($form->{quonumber}) {
	$item = $form->like(lc $form->{quonumber});
	$query .= " AND lower(a.quonumber) LIKE '$item'";
      }

      $query .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
      $query .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};


  } else {
    
    foreach $item (@{ $arap{$form->{type}} }) {
      
      $invoice = "a.invoice";
      $invnumber = "invnumber";
      
      if ($item eq 'oe') {
	$invnumber = "ordnumber";
	$invoice = "'0'"; 
      }
      
      $query .= qq|
                $union
		SELECT a.id, vc.name, a.$invnumber AS invnumber, a.transdate,
                a.ordnumber, a.quonumber, $invoice AS invoice,
		'$item' AS module, s.spoolfile
		FROM $item a, $form->{vc} vc, status s
		WHERE s.trans_id = a.id
		AND s.spoolfile IS NOT NULL
		AND s.formname = '$form->{type}'
		AND a.$form->{vc}_id = vc.id|;

      if ($form->{"$form->{vc}_id"}) {
	$query .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
      } else {
	if ($form->{$form->{vc}}) {
	  $item = $form->like(lc $form->{$form->{vc}});
	  $query .= " AND lower(vc.name) LIKE '$item'";
	}
      }
      if ($form->{invnumber}) {
	$item = $form->like(lc $form->{invnumber});
	$query .= " AND lower(a.invnumber) LIKE '$item'";
      }
      if ($form->{ordnumber}) {
	$item = $form->like(lc $form->{ordnumber});
	$query .= " AND lower(a.ordnumber) LIKE '$item'";
      }
      if ($form->{quonumber}) {
	$item = $form->like(lc $form->{quonumber});
	$query .= " AND lower(a.quonumber) LIKE '$item'";
      }

      $query .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
      $query .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};

      $union = "UNION";

    }
  }

  my %ordinal = ( 'name' => 2,
                  'invnumber' => 3,
                  'transdate' => 4,
		  'ordnumber' => 5,
		  'quonumber' => 6
		);
  my @a = (transdate, $invnumber, name);
  my $sortorder = $form->sort_order(\@a, \%ordinal);
 
  $query .= " ORDER by $sortorder";

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

  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
    push @{ $form->{SPOOL} }, $ref;
  }
  
  $sth->finish;
  $dbh->disconnect;

}


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

  # connect to database, turn AutoCommit off
  my $dbh = $form->dbconnect_noauto($myconfig);

  my $query;
  my %audittrail;
  
  if ($form->{type} =~ /(check|receipt)/) {
    $query = qq|DELETE FROM status
                WHERE spoolfile = ?|;
  } else {
    $query = qq|UPDATE status SET
                 spoolfile = NULL,
		 printed = '1'
                 WHERE spoolfile = ?|;
  }
  my $sth = $dbh->prepare($query) || $form->dberror($query);
  
  
  foreach my $i (1 .. $form->{rowcount}) {
    if ($form->{"checked_$i"}) {
      $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
      $sth->finish;
      
      %audittrail = ( tablename  => $form->{module},
                      reference  => $form->{"reference_$i"},
		      formname   => $form->{type},
		      action     => 'dequeued',
		      id         => $form->{"id_$i"} );
 
      $form->audittrail($dbh, "", \%audittrail);
    }
  }
    
  # commit
  my $rc = $dbh->commit;
  $dbh->disconnect;

  if ($rc) {
    foreach my $i (1 .. $form->{rowcount}) {
      $_ = qq|$spool/$form->{"spoolfile_$i"}|;
      if ($form->{"checked_$i"}) {
	unlink;
      }
    }
  }

  $rc;
  
}


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

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

  my %audittrail;
  
  my $query = qq|UPDATE status SET
		 printed = '1'
                 WHERE formname = '$form->{type}'
		 AND spoolfile = ?|;
  my $sth = $dbh->prepare($query) || $form->dberror($query);
  
  foreach my $i (1 .. $form->{rowcount}) {
    if ($form->{"checked_$i"}) {
      open(OUT, $form->{OUT}) or $form->error("$form->{OUT} : $!");
      
      $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
      
      # send file to printer
      open(IN, $spoolfile) or $form->error("$spoolfile : $!");

      while (<IN>) {
	print OUT $_;
      }
      close(IN);
      close(OUT);

      $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
      $sth->finish;
      
      %audittrail = ( tablename  => $form->{module},
                      reference  => $form->{"reference_$i"},
		      formname   => $form->{type},
		      action     => 'printed',
		      id         => $form->{"id_$i"} );
 
      $form->audittrail($dbh, "", \%audittrail);
      
      $dbh->commit;
    }
  }

  $dbh->disconnect;

}


1;