Subversion Repositories sql-ledger-old

Rev

Rev 114 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 finnarne 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
# backend code for customers and vendors
26
#
27
#======================================================================
28
 
29
package CT;
30
 
31
 
32
sub get_tuple {
33
  my ($self, $myconfig, $form) = @_;
34
 
35
  my $dbh = $form->dbconnect($myconfig);
36
  my $query = qq|SELECT *
37
                 FROM $form->{db}
38
                 WHERE id = $form->{id}|;
39
  my $sth = $dbh->prepare($query);
40
  $sth->execute || $form->dberror($query);
41
 
42
  my $ref = $sth->fetchrow_hashref(NAME_lc);
43
 
44
  map { $form->{$_} = $ref->{$_} } keys %$ref;
45
 
46
  $sth->finish;
47
 
48
 
49
  # get ship to
50
  $query = qq|SELECT *
51
              FROM shipto
52
              WHERE trans_id = $form->{id}|;
53
  $sth = $dbh->prepare($query);
54
  $sth->execute || $form->dberror($query);
55
 
56
  $ref = $sth->fetchrow_hashref(NAME_lc);
57
 
58
  map { $form->{$_} = $ref->{$_} } keys %$ref;
59
 
60
  $sth->finish;
61
 
62
 
63
  # get tax labels
64
  $query = qq|SELECT accno, description
65
              FROM chart, tax
66
              WHERE link LIKE '%CT_tax%'
67
              AND chart.id = tax.chart_id
68
              ORDER BY accno|;
69
  $sth = $dbh->prepare($query);
70
  $sth->execute || $form->dberror($query);
71
 
72
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
73
    $form->{taxaccounts} .= "$ref->{accno} ";
74
    $form->{tax}{$ref->{accno}}{description} = $ref->{description};
75
  }
76
  $sth->finish;
77
  chop $form->{taxaccounts};
78
 
79
  # get taxes for customer/vendor
80
  $query = qq|SELECT chart_id, accno
81
              FROM $form->{db}tax, chart
82
              WHERE chart_id = chart.id
83
              AND $form->{db}_id = $form->{id}|;
84
  $sth = $dbh->prepare($query);
85
  $sth->execute || $form->dberror($query);
86
 
87
  while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
88
    $form->{tax}{$ref->{accno}}{taxable} = 1;
89
  }
90
  $sth->finish;
91
 
92
 
93
  $dbh->disconnect;
94
 
95
}
96
 
97
 
98
sub taxaccounts {
99
  my ($self, $myconfig, $form) = @_;
100
 
101
  my $dbh = $form->dbconnect($myconfig);
102
 
103
  # get tax labels
104
  my $query = qq|SELECT accno, description
105
                 FROM chart, tax
106
                 WHERE link LIKE '%CT_tax%'
107
                 AND chart.id = tax.chart_id
108
                 ORDER BY accno|;
109
  $sth = $dbh->prepare($query);
110
  $sth->execute || $form->dberror($query);
111
 
112
  while (my $taxref = $sth->fetchrow_hashref(NAME_lc)) {
113
    $form->{taxaccounts} .= "$taxref->{accno} ";
114
    $form->{tax}{$taxref->{accno}}{description} = $taxref->{description};
115
  }
116
  $sth->finish;
117
  chop $form->{taxaccounts};
118
 
119
  $dbh->disconnect;
120
 
121
}
122
 
123
 
124
sub delete_customer {
125
  my ($self, $myconfig, $form) = @_;
126
 
127
  # connect to database, turn AutoCommit off
128
  my $dbh = $form->dbconnect_noauto($myconfig);
129
 
130
  my $query = qq|SELECT id FROM ar
131
                 WHERE customer_id = $form->{id}
132
                 UNION
133
                 SELECT id FROM oe
134
                 WHERE customer_id = $form->{id}|;
135
  my $sth = $dbh->prepare($query) || $form->dberror($query);
136
  $sth->execute;
137
 
138
  my ($rc) = $sth->fetchrow_array;
139
  $sth->finish;
140
 
141
  if ($rc) {
142
    $dbh->disconnect;
143
    $rc = -1;
144
  } else {
145
 
146
    # delete customer
147
    $query = qq|DELETE FROM customer
148
                WHERE id = $form->{id}|;
149
    $dbh->do($query) || $form->dberror($query);
150
 
151
    $query = qq|DELETE FROM shipto
152
                WHERE trans_id = $form->{id}|;
153
    $dbh->do($query) || $form->dberror($query);
154
 
155
    $query = qq|DELETE FROM customertax
156
                WHERE customer_id = $form->{id}|;
157
    $dbh->do($query) || $form->dberror($query);
158
 
159
    # commit and redirect
160
    $rc = $dbh->commit;
161
    $dbh->disconnect;
162
 
163
  }
164
 
165
  $rc;
166
 
167
}
168
 
169
 
170
sub save_customer {
171
  my ($self, $myconfig, $form) = @_;
172
 
173
  # connect to database
114 finnarne 174
  my $dbh = $form->dbconnect_noauto($myconfig);
2 finnarne 175
 
176
  # escape '
177
  map { $form->{$_} =~ s/'/''/g } qw(customernumber name addr1 addr2 addr3 addr4 contact notes);
178
 
179
  # assign value discount, terms, creditlimit
180
  $form->{discount} /= 100;
181
  $form->{terms} *= 1;
182
  $form->{taxincluded} *= 1;
183
  $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
184
 
185
  my ($query, $sth);
186
 
187
  if ($form->{id}) {
188
    $query = qq|DELETE FROM customertax
189
                WHERE customer_id = $form->{id}|;
190
    $dbh->do($query) || $form->dberror($query);
191
 
192
    $query = qq|DELETE FROM shipto
193
                WHERE trans_id = $form->{id}|;
194
    $dbh->do($query) || $form->dberror($query);
195
  } else {
196
    my $uid = time;
197
    $uid .= $form->{login};
198
 
199
    $query = qq|INSERT INTO customer (name)
200
                VALUES ('$uid')|;
201
    $dbh->do($query) || $form->dberror($query);
202
 
203
    $query = qq|SELECT id FROM customer
204
                WHERE name = '$uid'|;
205
    $sth = $dbh->prepare($query);
206
    $sth->execute || $form->dberror($query);
207
 
208
    ($form->{id}) = $sth->fetchrow_array;
209
    $sth->finish;
210
 
211
  }
114 finnarne 212
 
2 finnarne 213
  $query = qq|UPDATE customer SET
214
              customernumber = '$form->{customernumber}',
215
              name = '$form->{name}',
216
              addr1 = '$form->{addr1}',
217
              addr2 = '$form->{addr2}',
218
              addr3 = '$form->{addr3}',
219
              addr4 = '$form->{addr4}',
220
              contact = '$form->{contact}',
221
              phone = '$form->{phone}',
222
              fax = '$form->{fax}',
223
              email = '$form->{email}',
224
              cc = '$form->{cc}',
225
              bcc = '$form->{bcc}',
226
              notes = '$form->{notes}',
227
              discount = $form->{discount},
228
              creditlimit = $form->{creditlimit},
229
              terms = $form->{terms},
230
              taxincluded = '$form->{taxincluded}'
231
              WHERE id = $form->{id}|;
232
  $dbh->do($query) || $form->dberror($query);
233
 
234
  # save taxes
235
  foreach $item (split / /, $form->{taxaccounts}) {
236
    if ($form->{"tax_$item"}) {
237
      $query = qq|INSERT INTO customertax (customer_id, chart_id)
238
                  VALUES ($form->{id}, (SELECT id
239
                                        FROM chart
240
                                        WHERE accno = '$item'))|;
241
      $dbh->do($query) || $form->dberror($query);
242
    }
243
  }
244
 
245
  # add shipto
246
  $form->add_shipto($dbh, $form->{id});
247
 
114 finnarne 248
  $dbh->commit;
2 finnarne 249
  $dbh->disconnect;
250
 
251
}
252
 
253
 
254
sub save_vendor {
255
  my ($self, $myconfig, $form) = @_;
256
 
257
  # connect to database
114 finnarne 258
  my $dbh = $form->dbconnect_noauto($myconfig);
2 finnarne 259
 
260
  # escape '
261
  map { $form->{$_} =~ s/'/''/g } qw(vendornumber name addr1 addr2 addr3 addr4 contact notes);
262
 
263
  $form->{terms} *= 1;
264
  $form->{taxincluded} *= 1;
265
 
266
  my $query;
267
 
268
  if ($form->{id}) {
269
    $query = qq|DELETE FROM vendortax
270
                WHERE vendor_id = $form->{id}|;
271
    $dbh->do($query) || $form->dberror($query);
272
 
273
    $query = qq|DELETE FROM shipto
274
                WHERE trans_id = $form->{id}|;
275
    $dbh->do($query) || $form->dberror($query);
276
  } else {
277
    my $uid = time;
278
    $uid .= $form->{login};
279
 
280
    $query = qq|INSERT INTO vendor (name)
281
                VALUES ('$uid')|;
282
    $dbh->do($query) || $form->dberror($query);
283
 
284
    $query = qq|SELECT id FROM vendor
285
                WHERE name = '$uid'|;
286
    $sth = $dbh->prepare($query);
287
    $sth->execute || $form->dberror($query);
288
 
289
    ($form->{id}) = $sth->fetchrow_array;
290
    $sth->finish;
291
 
292
  }
293
 
294
 
295
  $query = qq|UPDATE vendor SET
296
              vendornumber = '$form->{vendornumber}',
297
              name = '$form->{name}',
298
              addr1 = '$form->{addr1}',
299
              addr2 = '$form->{addr2}',
300
              addr3 = '$form->{addr3}',
301
              addr4 = '$form->{addr4}',
302
              contact = '$form->{contact}',
303
              phone = '$form->{phone}',
304
              fax = '$form->{fax}',
305
              email = '$form->{email}',
306
              cc = '$form->{cc}',
307
              bcc = '$form->{bcc}',
308
              notes = '$form->{notes}',
309
              terms = $form->{terms},
310
              taxincluded = '$form->{taxincluded}'
311
              WHERE id = $form->{id}|;
312
  $dbh->do($query) || $form->dberror($query);
313
 
314
  # save taxes
315
  foreach $item (split / /, $form->{taxaccounts}) {
316
    if ($form->{"tax_$item"}) {
317
      $query = qq|INSERT INTO vendortax (vendor_id, chart_id)
318
                  VALUES ($form->{id}, (SELECT id
319
                                        FROM chart
320
                                        WHERE accno = '$item'))|;
321
      $dbh->do($query) || $form->dberror($query);
322
    }
323
  }
324
 
325
  # add shipto
326
  $form->add_shipto($dbh, $form->{id});
327
 
114 finnarne 328
  $dbh->commit;
2 finnarne 329
  $dbh->disconnect;
330
 
331
}
332
 
333
 
334
 
335
sub delete_vendor {
336
  my ($self, $myconfig, $form) = @_;
337
 
338
  # connect to database, turn AutoCommit off
339
  my $dbh = $form->dbconnect_noauto($myconfig);
340
 
341
  # check if there are any transactions on file
342
  my $query = qq|SELECT id FROM ap
343
                 WHERE vendor_id = $form->{id}
344
                 UNION
345
                 SELECT id FROM oe
346
                 WHERE vendor_id = $form->{id}|;
347
  my $sth = $dbh->prepare($query) || $form->dberror($query);
348
  $sth->execute;
349
 
350
  my ($rc) = $sth->fetchrow_array;
351
  $sth->finish;
352
 
353
  if ($rc) {
354
    $dbh->disconnect;
355
    $rc = -1;
356
  } else {
357
 
358
    # delete vendor
359
    $query = qq|DELETE FROM vendor
360
                WHERE id = $form->{id}|;
361
    $dbh->do($query) || $form->dberror($query);
362
 
363
    $query = qq|DELETE FROM shipto
364
                WHERE trans_id = $form->{id}|;
365
    $dbh->do($query) || $form->dberror($query);
366
 
367
    $query = qq|DELETE FROM vendortax
368
                WHERE vendor_id = $form->{id}|;
369
    $dbh->do($query) || $form->dberror($query);
370
 
371
    # commit and redirect
372
    $rc = $dbh->commit;
373
    $dbh->disconnect;
374
 
375
  }
376
 
377
  $rc;
378
 
379
}
380
 
381
 
382
sub search {
383
  my ($self, $myconfig, $form) = @_;
384
 
385
  # connect to database
386
  my $dbh = $form->dbconnect($myconfig);
387
 
388
  my $where = "1 = 1";
389
  $form->{sort} = "name" unless ($form->{sort});
390
 
391
  if ($form->{"$form->{db}number"}) {
392
    my $companynumber = $form->like(lc $form->{"$form->{db}number"});
393
    $where .= " AND lower($form->{db}number) LIKE '$companynumber'";
394
  }
395
  if ($form->{name}) {
396
    my $name = $form->like(lc $form->{name});
397
    $where .= " AND lower(name) LIKE '$name'";
398
  }
399
  if ($form->{contact}) {
400
    my $contact = $form->like(lc $form->{contact});
401
    $where .= " AND lower(contact) LIKE '$contact'";
402
  }
403
  if ($form->{email}) {
404
    my $email = $form->like(lc $form->{email});
405
    $where .= " AND lower(email) LIKE '$email'";
406
  }
407
 
408
  if ($form->{status} eq 'orphaned') {
409
    $where .= qq| AND id NOT IN (SELECT o.$form->{db}_id
410
                                 FROM oe o, $form->{db} ct
411
                                 WHERE ct.id = o.$form->{db}_id)|;
412
    if ($form->{db} eq 'customer') {
413
      $where .= qq| AND id NOT IN (SELECT a.customer_id
414
                                   FROM ar a, customer ct
415
                                   WHERE ct.id = a.customer_id)|;
416
    }
417
    if ($form->{db} eq 'vendor') {
418
      $where .= qq| AND id NOT IN (SELECT a.vendor_id
419
                                   FROM ap a, vendor ct
420
                                   WHERE ct.id = a.vendor_id)|;
421
    }
422
  }
423
 
424
  my $query = qq~SELECT id, name, $form->{db}number,
425
                 addr1 || ' ' || addr2 || ' ' || addr3 || ' ' || addr4 AS address,
426
                 contact, phone, fax, email, cc, terms
427
                 FROM $form->{db}
428
                 WHERE $where
429
                 ORDER BY $form->{sort}~;
430
 
431
  my $sth = $dbh->prepare($query);
432
  $sth->execute || $form->dberror($query);
433
 
434
 
435
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
436
    push @{ $form->{CT} }, $ref;
437
  }
438
 
439
  $sth->finish;
440
  $dbh->disconnect;
441
 
442
}
443
 
444
 
114 finnarne 445
sub get_history {
446
  my ($self, $myconfig, $form) = @_;
447
 
448
  # connect to database
449
  my $dbh = $form->dbconnect($myconfig);
450
 
451
  my $query;
452
  my $where = "1 = 1";
453
  my $sortorder = ($form->{sort}) ? $form->{sort} : "partnumber";
454
  my %ordinal = ();
455
  my $var;
456
  my $table;
457
 
458
  if ($form->{"$form->{db}number"}) {
459
    $var = $form->like(lc $form->{"$form->{db}number"});
460
    $where .= " AND lower(ct.$form->{db}number) LIKE '$var'";
461
  }
462
  if ($form->{name}) {
463
    $var = $form->like(lc $form->{name});
464
    $where .= " AND lower(ct.name) LIKE '$var'";
465
  }
466
  if ($form->{contact}) {
467
    $var = $form->like(lc $form->{contact});
468
    $where .= " AND lower(ct.contact) LIKE '$var'";
469
  }
470
  if ($form->{email}) {
471
    $var = $form->like(lc $form->{email});
472
    $where .= " AND lower(ct.email) LIKE '$var'";
473
  }
474
 
475
  $where .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
476
  $where .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
477
 
478
  my $invnumber = 'invnumber';
479
  my $deldate = 'deliverydate';
480
 
481
  if ($form->{db} eq 'customer') {
482
    if ($form->{type} eq 'invoice') {
483
      $where .= qq| AND a.invoice = '1' AND i.assemblyitem = '0'|;
484
      $table = 'ar';
485
    } else {
486
      $table = 'oe';
487
      $invnumber = 'ordnumber';
488
      $deldate = 'reqdate';
489
    }
490
  }
491
  if ($form->{db} eq 'vendor') {
492
    if ($form->{type} eq 'invoice') {
493
      $where .= qq| AND invoice = '1' AND i.assemblyitem = '0'|;
494
      $table = 'ap';
495
    } else {
496
      $table = 'oe';
497
      $invnumber = 'ordnumber';
498
      $deldate = 'reqdate';
499
    }
500
  }
501
 
502
  my $invjoin = qq|
503
                 JOIN invoice i ON (i.trans_id = a.id)|;
504
 
505
  if ($form->{type} eq 'order') {
506
    $invjoin = qq|
507
                 JOIN orderitems i ON (i.trans_id = a.id)|;
508
  }
509
 
510
  if ($form->{history} eq 'summary') {
511
    %ordinal = ( partnumber     => 8,
512
                 description    => 9
513
               );
514
    $sortorder = "2, 1, $ordinal{$sortorder}";
515
 
516
    $query = qq|SELECT ct.id AS ctid, ct.name, ct.addr1,
517
                ct.addr2, ct.addr3, ct.addr4,
518
                p.id AS pid, p.partnumber, i.description, p.unit,
519
                sum(i.qty) AS qty, sum(i.sellprice) AS sellprice|;
520
    $query .= qq|, sum(i.fxsellprice) AS fxsellprice| if $form->{type} eq 'invoice';
521
    $query .= qq|
522
                FROM $form->{db} ct
523
                JOIN $table a ON (a.$form->{db}_id = ct.id)
524
                $invjoin
525
                JOIN parts p ON (p.id = i.parts_id)
526
                WHERE $where
527
                GROUP BY ct.id, ct.name, ct.addr1, ct.addr2, ct.addr3,
528
                ct.addr4, p.id, p.partnumber, i.description, p.unit
529
                ORDER BY $sortorder|;
530
  } else {
531
    %ordinal = ( partnumber     => 9,
532
                 description    => 12,
533
                 "$deldate"     => 16,
534
                 projectnumber  => 17
535
                );
536
 
537
    $sortorder = "2, 1, 11, $ordinal{$sortorder}";
538
    $query = qq|SELECT ct.id AS ctid, ct.name, ct.addr1,
539
                ct.addr2, ct.addr3, ct.addr4,
540
                p.id AS pid, p.partnumber, a.id AS invid,
541
                a.$invnumber, a.curr, i.description,
542
                i.qty, i.sellprice, i.discount,
543
                i.$deldate, pr.projectnumber, i.unit|;
544
    $query .= qq|, i.fxsellprice| if $form->{type} eq 'invoice';
545
    $query .= qq|
546
                FROM $form->{db} ct
547
                JOIN $table a ON (a.$form->{db}_id = ct.id)
548
                $invjoin
549
                JOIN parts p ON (p.id = i.parts_id)
550
                LEFT JOIN project pr ON (pr.id = i.project_id)
551
                WHERE $where
552
                ORDER BY $sortorder|;
553
  }
554
 
555
 
556
  my $sth = $dbh->prepare($query);
557
  $sth->execute || $form->dberror($query);
558
 
559
  while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
560
    $ref->{address} = "";
561
    map { $ref->{address} .= "$ref->{$_} "; } qw(addr1 addr2 addr3 addr4);
562
    $ref->{id} = $ref->{ctid};
563
    push @{ $form->{CT} }, $ref;
564
  }
565
 
566
  $sth->finish;
567
  $dbh->disconnect;
568
 
569
}
570
 
2 finnarne 571
1;
572