so Search.tsf and Search.rdf work
[freeside.git] / sql-ledger / SL / CT.pm
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2000
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 create_links {
33   my ($self, $myconfig, $form) = @_;
34
35   my $dbh = $form->dbconnect($myconfig);
36   my $query;
37   my $sth;
38   my $ref;
39
40   if ($form->{id}) {
41     $query = qq|SELECT ct.*, b.description AS business, s.*,
42                 e.name AS employee, g.pricegroup AS pricegroup,
43                 l.description AS language, ct.curr
44                 FROM $form->{db} ct
45                 LEFT JOIN business b ON (ct.business_id = b.id)
46                 LEFT JOIN shipto s ON (ct.id = s.trans_id)
47                 LEFT JOIN employee e ON (ct.employee_id = e.id)
48                 LEFT JOIN pricegroup g ON (g.id = ct.pricegroup_id)
49                 LEFT JOIN language l ON (l.code = ct.language_code)
50                 WHERE ct.id = $form->{id}|;
51     $sth = $dbh->prepare($query);
52     $sth->execute || $form->dberror($query);
53   
54     $ref = $sth->fetchrow_hashref(NAME_lc);
55   
56     map { $form->{$_} = $ref->{$_} } keys %$ref;
57
58     $sth->finish;
59
60     # check if it is orphaned
61     my $arap = ($form->{db} eq 'customer') ? "ar" : "ap";
62     $query = qq|SELECT a.id
63               FROM $arap a
64               JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
65               WHERE ct.id = $form->{id}
66             UNION
67               SELECT a.id
68               FROM oe a
69               JOIN $form->{db} ct ON (a.$form->{db}_id = ct.id)
70               WHERE ct.id = $form->{id}|;
71     $sth = $dbh->prepare($query);
72     $sth->execute || $form->dberror($query);
73   
74     unless ($sth->fetchrow_array) {
75       $form->{status} = "orphaned";
76     }
77     $sth->finish;
78
79
80     # get taxes for customer/vendor
81     $query = qq|SELECT c.accno
82                 FROM chart c
83                 JOIN $form->{db}tax t ON (t.chart_id = c.id)
84                 WHERE t.$form->{db}_id = $form->{id}|;
85     $sth = $dbh->prepare($query);
86     $sth->execute || $form->dberror($query);
87
88     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
89       $form->{tax}{$ref->{accno}}{taxable} = 1;
90     }
91     $sth->finish;
92
93   } else {
94
95     ($form->{employee}, $form->{employee_id}) = $form->get_employee($dbh);
96
97     $query = qq|SELECT current_date FROM defaults|;
98     ($form->{startdate}) = $dbh->selectrow_array($query);
99
100   }
101
102   # get tax labels
103   $query = qq|SELECT c.accno, c.description
104               FROM chart c
105               JOIN tax t ON (t.chart_id = c.id)
106               WHERE c.link LIKE '%CT_tax%'
107               ORDER BY c.accno|;
108   $sth = $dbh->prepare($query);
109   $sth->execute || $form->dberror($query);
110
111   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
112     $form->{taxaccounts} .= "$ref->{accno} ";
113     $form->{tax}{$ref->{accno}}{description} = $ref->{description};
114   }
115   $sth->finish;
116   chop $form->{taxaccounts};
117
118     
119   # get business types
120   $query = qq|SELECT *
121               FROM business
122               ORDER BY 2|;
123   $sth = $dbh->prepare($query);
124   $sth->execute || $form->dberror($query);
125   
126   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
127     push @{ $form->{all_business} }, $ref;
128   }
129   $sth->finish;
130   
131   # this is for the salesperson
132   $query = qq|SELECT id, name
133               FROM employee
134               WHERE sales = '1'
135               ORDER BY 2|;
136   $sth = $dbh->prepare($query);
137   $sth->execute || $form->dberror($query);
138   
139   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
140     push @{ $form->{all_employee} }, $ref;
141   }
142   $sth->finish;
143
144   # get language
145   $query = qq|SELECT *
146               FROM language
147               ORDER BY 2|;
148   $sth = $dbh->prepare($query);
149   $sth->execute || $form->dberror($query);
150   
151   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
152     push @{ $form->{all_language} }, $ref;
153   }
154   $sth->finish;
155  
156   # get pricegroups
157   $query = qq|SELECT *
158               FROM pricegroup
159               ORDER BY 2|;
160   $sth = $dbh->prepare($query);
161   $sth->execute || $form->dberror($query);
162   
163   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
164     push @{ $form->{all_pricegroup} }, $ref;
165   }
166   $sth->finish;
167   
168   # get currencies
169   $query = qq|SELECT curr AS currencies
170               FROM defaults|;
171   $sth = $dbh->prepare($query);
172   $sth->execute || $form->dberror($query);
173   
174   ($form->{currencies}) = $sth->fetchrow_array;
175   $sth->finish;
176
177   $dbh->disconnect;
178
179 }
180
181
182 sub save_customer {
183   my ($self, $myconfig, $form) = @_;
184
185   # connect to database
186   my $dbh = $form->dbconnect_noauto($myconfig);
187   my $query;
188   my $sth;
189   my $null;
190   
191   # remove double spaces
192   $form->{name} =~ s/  / /g;
193   # remove double minus and minus at the end
194   $form->{name} =~ s/--+/-/g;
195   $form->{name} =~ s/-+$//;
196   
197   # assign value discount, terms, creditlimit
198   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
199   $form->{discount} /= 100;
200   $form->{terms} *= 1;
201   $form->{taxincluded} *= 1;
202   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
203   
204
205   if ($form->{id}) {
206     $query = qq|DELETE FROM customertax
207                 WHERE customer_id = $form->{id}|;
208     $dbh->do($query) || $form->dberror($query);
209
210     $query = qq|DELETE FROM shipto
211                 WHERE trans_id = $form->{id}|;
212     $dbh->do($query) || $form->dberror($query);
213
214     # retrieve enddate
215     if ($form->{type} && $form->{enddate}) {
216       my $now;
217       $query = qq|SELECT enddate, current_date AS now FROM customer|;
218       ($form->{enddate}, $now) = $dbh->selectrow_array($query);
219       $form->{enddate} = $now if $form->{enddate} lt $now;
220     }
221
222   } else {
223     my $uid = time;
224     $uid .= $form->{login};
225
226     $query = qq|INSERT INTO customer (name)
227                 VALUES ('$uid')|;
228     $dbh->do($query) || $form->dberror($query);
229     
230     $query = qq|SELECT id FROM customer
231                 WHERE name = '$uid'|;
232     $sth = $dbh->prepare($query);
233     $sth->execute || $form->dberror($query);
234
235     ($form->{id}) = $sth->fetchrow_array;
236     $sth->finish;
237
238   }
239
240   my $employee_id;
241   ($null, $employee_id) = split /--/, $form->{employee};
242   $employee_id *= 1;
243   
244   my $pricegroup_id;
245   ($null, $pricegroup_id) = split /--/, $form->{pricegroup};
246   $pricegroup_id *= 1;
247
248   my $business_id;
249   ($null, $business_id) = split /--/, $form->{business};
250   $business_id *= 1;
251
252   my $language_code;
253   ($null, $language_code) = split /--/, $form->{language};
254
255   $form->{customernumber} = $form->update_defaults($myconfig, "customernumber", $dbh) if ! $form->{customernumber};
256   
257   $query = qq|UPDATE customer SET
258               customernumber = |.$dbh->quote($form->{customernumber}).qq|,
259               name = |.$dbh->quote($form->{name}).qq|,
260               address1 = |.$dbh->quote($form->{address1}).qq|,
261               address2 = |.$dbh->quote($form->{address2}).qq|,
262               city = |.$dbh->quote($form->{city}).qq|,
263               state = |.$dbh->quote($form->{state}).qq|,
264               zipcode = |.$dbh->quote($form->{zipcode}).qq|,
265               country = |.$dbh->quote($form->{country}).qq|,
266               contact = |.$dbh->quote($form->{contact}).qq|,
267               phone = '$form->{phone}',
268               fax = '$form->{fax}',
269               email = '$form->{email}',
270               cc = '$form->{cc}',
271               bcc = '$form->{bcc}',
272               notes = |.$dbh->quote($form->{notes}).qq|,
273               discount = $form->{discount},
274               creditlimit = $form->{creditlimit},
275               terms = $form->{terms},
276               taxincluded = '$form->{taxincluded}',
277               business_id = $business_id,
278               taxnumber = |.$dbh->quote($form->{taxnumber}).qq|,
279               sic_code = '$form->{sic}',
280               iban = '$form->{iban}',
281               bic = '$form->{bic}',
282               employee_id = $employee_id,
283               pricegroup_id = $pricegroup_id,
284               language_code = '$language_code',
285               curr = '$form->{curr}',
286               startdate = |.$form->dbquote($form->{startdate}, SQL_DATE).qq|,
287               enddate = |.$form->dbquote($form->{enddate}, SQL_DATE).qq|
288               WHERE id = $form->{id}|;
289   $dbh->do($query) || $form->dberror($query);
290
291   # save taxes
292   foreach $item (split / /, $form->{taxaccounts}) {
293     if ($form->{"tax_$item"}) {
294       $query = qq|INSERT INTO customertax (customer_id, chart_id)
295                   VALUES ($form->{id}, (SELECT id
296                                         FROM chart
297                                         WHERE accno = '$item'))|;
298       $dbh->do($query) || $form->dberror($query);
299     }
300   }
301   
302   # add shipto
303   $form->add_shipto($dbh, $form->{id});
304
305   $dbh->commit;
306   $dbh->disconnect;
307
308 }
309
310
311 sub save_vendor {
312   my ($self, $myconfig, $form) = @_;
313
314   # connect to database
315   my $dbh = $form->dbconnect_noauto($myconfig);
316
317   my $query;
318   my $sth;
319   my $null;
320
321   # remove double spaces
322   $form->{name} =~ s/  / /g;
323   # remove double minus and minus at the end
324   $form->{name} =~ s/--+/-/g;
325   $form->{name} =~ s/-+$//;
326
327   $form->{discount} = $form->parse_amount($myconfig, $form->{discount});
328   $form->{discount} /= 100;
329   $form->{terms} *= 1;
330   $form->{taxincluded} *= 1;
331   $form->{creditlimit} = $form->parse_amount($myconfig, $form->{creditlimit});
332  
333   
334   if ($form->{id}) {
335     $query = qq|DELETE FROM vendortax
336                 WHERE vendor_id = $form->{id}|;
337     $dbh->do($query) || $form->dberror($query);
338
339     $query = qq|DELETE FROM shipto
340                 WHERE trans_id = $form->{id}|;
341     $dbh->do($query) || $form->dberror($query);
342   } else {
343     my $uid = time;
344     $uid .= $form->{login};
345     
346     $query = qq|INSERT INTO vendor (name)
347                 VALUES ('$uid')|;
348     $dbh->do($query) || $form->dberror($query);
349    
350     $query = qq|SELECT id FROM vendor
351                 WHERE name = '$uid'|;
352     $sth = $dbh->prepare($query);
353     $sth->execute || $form->dberror($query);
354
355     ($form->{id}) = $sth->fetchrow_array;
356     $sth->finish;
357
358   }
359    
360   my $employee_id;
361   ($null, $employee_id) = split /--/, $form->{employee};
362   $employee_id *= 1;
363   
364   my $pricegroup_id;
365   ($null, $pricegroup_id) = split /--/, $form->{pricegroup};
366   $pricegroup_id *= 1;
367
368   my $business_id;
369   ($null, $business_id) = split /--/, $form->{business};
370   $business_id *= 1;
371
372   my $language_code;
373   ($null, $language_code) = split /--/, $form->{language};
374  
375   $form->{vendornumber} = $form->update_defaults($myconfig, "vendornumber", $dbh) if ! $form->{vendornumber};
376   
377   $query = qq|UPDATE vendor SET
378               vendornumber = |.$dbh->quote($form->{vendornumber}).qq|,
379               name = |.$dbh->quote($form->{name}).qq|,
380               address1 = |.$dbh->quote($form->{address1}).qq|,
381               address2 = |.$dbh->quote($form->{address2}).qq|,
382               city = |.$dbh->quote($form->{city}).qq|,
383               state = |.$dbh->quote($form->{state}).qq|,
384               zipcode = |.$dbh->quote($form->{zipcode}).qq|,
385               country = |.$dbh->quote($form->{country}).qq|,
386               contact = |.$dbh->quote($form->{contact}).qq|,
387               phone = '$form->{phone}',
388               fax = '$form->{fax}',
389               email = '$form->{email}',
390               cc = '$form->{cc}',
391               bcc = '$form->{bcc}',
392               notes = |.$dbh->quote($form->{notes}).qq|,
393               terms = $form->{terms},
394               discount = $form->{discount},
395               creditlimit = $form->{creditlimit},
396               taxincluded = '$form->{taxincluded}',
397               gifi_accno = '$form->{gifi_accno}',
398               business_id = $business_id,
399               taxnumber = |.$dbh->quote($form->{taxnumber}).qq|,
400               sic_code = '$form->{sic}',
401               iban = '$form->{iban}',
402               bic = '$form->{bic}',
403               employee_id = $employee_id,
404               language_code = '$language_code',
405               pricegroup_id = $pricegroup_id,
406               curr = '$form->{curr}',
407               startdate = |.$form->dbquote($form->{startdate}, SQL_DATE).qq|,
408               enddate = |.$form->dbquote($form->{enddate}, SQL_DATE).qq|
409               WHERE id = $form->{id}|;
410   $dbh->do($query) || $form->dberror($query);
411
412   # save taxes
413   foreach $item (split / /, $form->{taxaccounts}) {
414     if ($form->{"tax_$item"}) {
415       $query = qq|INSERT INTO vendortax (vendor_id, chart_id)
416                   VALUES ($form->{id}, (SELECT id
417                                         FROM chart
418                                         WHERE accno = '$item'))|;
419       $dbh->do($query) || $form->dberror($query);
420     }
421   }
422
423   # add shipto
424   $form->add_shipto($dbh, $form->{id});
425
426   $dbh->commit;
427   $dbh->disconnect;
428
429 }
430
431
432
433 sub delete {
434   my ($self, $myconfig, $form) = @_;
435
436   # connect to database
437   my $dbh = $form->dbconnect($myconfig);
438
439   # delete customer/vendor
440   my $query = qq|DELETE FROM $form->{db}
441                  WHERE id = $form->{id}|;
442   $dbh->do($query) || $form->dberror($query);
443
444   $dbh->disconnect;
445
446 }
447
448
449 sub search {
450   my ($self, $myconfig, $form) = @_;
451
452   # connect to database
453   my $dbh = $form->dbconnect($myconfig);
454
455   my $where = "1 = 1";
456   $form->{sort} = ($form->{sort}) ? $form->{sort} : "name";
457   my @a = qw(name);
458   my $sortorder = $form->sort_order(\@a);
459
460   my $var;
461   my $item;
462
463   @a = ("$form->{db}number");
464   push @a, qw(name contact city state zipcode country notes email);
465   
466   foreach $item (@a) {
467     if ($form->{$item}) {
468       $var = $form->like(lc $form->{$item});
469       $where .= " AND lower(ct.$item) LIKE '$var'";
470     }
471   }
472   if ($form->{address}) {
473     $var = $form->like(lc $form->{address});
474     $where .= " AND (lower(ct.address1) LIKE '$var' OR lower(ct.address2) LIKE '$var')";
475   }
476
477   if ($form->{status} eq 'orphaned') {
478     $where .= qq| AND ct.id NOT IN (SELECT o.$form->{db}_id
479                                     FROM oe o, $form->{db} cv
480                                     WHERE cv.id = o.$form->{db}_id)|;
481     if ($form->{db} eq 'customer') {
482       $where .= qq| AND ct.id NOT IN (SELECT a.customer_id
483                                       FROM ar a, customer cv
484                                       WHERE cv.id = a.customer_id)|;
485     }
486     if ($form->{db} eq 'vendor') {
487       $where .= qq| AND ct.id NOT IN (SELECT a.vendor_id
488                                       FROM ap a, vendor cv
489                                       WHERE cv.id = a.vendor_id)|;
490     }
491     $form->{l_invnumber} = $form->{l_ordnumber} = $form->{l_quonumber} = "";
492   }
493   
494
495   my $query = qq|SELECT ct.*, b.description AS business,
496                  e.name AS employee, g.pricegroup, l.description AS language,
497                  m.name AS manager
498                  FROM $form->{db} ct
499               LEFT JOIN business b ON (ct.business_id = b.id)
500               LEFT JOIN employee e ON (ct.employee_id = e.id)
501               LEFT JOIN employee m ON (m.id = e.managerid)
502               LEFT JOIN pricegroup g ON (ct.pricegroup_id = g.id)
503               LEFT JOIN language l ON (l.code = ct.language_code)
504                  WHERE $where|;
505
506   # redo for invoices, orders and quotations
507   if ($form->{l_transnumber} || $form->{l_invnumber} || $form->{l_ordnumber} || $form->{l_quonumber}) {
508
509     my ($ar, $union, $module);
510     $query = "";
511     my $transwhere;
512     my $openarap = "";
513     my $openoe = "";
514     
515     if ($form->{open} || $form->{closed}) {
516       unless ($form->{open} && $form->{closed}) {
517         $openarap = " AND a.amount != a.paid" if $form->{open};
518         $openarap = " AND a.amount = a.paid" if $form->{closed};
519         $openoe = " AND o.closed = '0'" if $form->{open};
520         $openoe = " AND o.closed = '1'" if $form->{closed};
521       }
522     }
523       
524     if ($form->{l_transnumber}) {
525       $ar = ($form->{db} eq 'customer') ? 'ar' : 'ap';
526       $module = $ar;
527
528       $transwhere = "";
529       $transwhere .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
530       $transwhere .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
531       
532    
533       $query = qq|SELECT ct.*, b.description AS business,
534                   a.invnumber, a.ordnumber, a.quonumber, a.id AS invid,
535                   '$ar' AS module, 'invoice' AS formtype,
536                   (a.amount = a.paid) AS closed, a.amount, a.netamount
537                   FROM $form->{db} ct
538                 JOIN $ar a ON (a.$form->{db}_id = ct.id)
539                 LEFT JOIN business b ON (ct.business_id = b.id)
540                   WHERE $where
541                   AND a.invoice = '0'
542                   $transwhere
543                   $openarap
544                   |;
545   
546       $union = qq|
547               UNION|;
548       
549     }
550
551     if ($form->{l_invnumber}) {
552       $ar = ($form->{db} eq 'customer') ? 'ar' : 'ap';
553       $module = ($ar eq 'ar') ? 'is' : 'ir';
554
555       $transwhere = "";
556       $transwhere .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
557       $transwhere .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
558     
559       $query .= qq|$union
560                   SELECT ct.*, b.description AS business,
561                   a.invnumber, a.ordnumber, a.quonumber, a.id AS invid,
562                   '$module' AS module, 'invoice' AS formtype,
563                   (a.amount = a.paid) AS closed, a.amount, a.netamount
564                   FROM $form->{db} ct
565                 JOIN $ar a ON (a.$form->{db}_id = ct.id)
566                 LEFT JOIN business b ON (ct.business_id = b.id)
567                   WHERE $where
568                   AND a.invoice = '1'
569                   $transwhere
570                   $openarap
571                   |;
572   
573       $union = qq|
574               UNION|;
575       
576     }
577     
578     if ($form->{l_ordnumber}) {
579       
580       $transwhere = "";
581       $transwhere .= " AND o.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
582       $transwhere .= " AND o.transdate <= '$form->{transdateto}'" if $form->{transdateto};
583       $query .= qq|$union
584                   SELECT ct.*, b.description AS business,
585                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
586                   'oe' AS module, 'order' AS formtype,
587                   o.closed, o.amount, o.netamount
588                   FROM $form->{db} ct
589                 JOIN oe o ON (o.$form->{db}_id = ct.id)
590                 LEFT JOIN business b ON (ct.business_id = b.id)
591                   WHERE $where
592                   AND o.quotation = '0'
593                   $transwhere
594                   $openoe
595                   |;
596   
597       $union = qq|
598               UNION|;
599
600     }
601
602     if ($form->{l_quonumber}) {
603
604       $transwhere = "";
605       $transwhere .= " AND o.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
606       $transwhere .= " AND o.transdate <= '$form->{transdateto}'" if $form->{transdateto};
607       $query .= qq|$union
608                   SELECT ct.*, b.description AS business,
609                   ' ' AS invnumber, o.ordnumber, o.quonumber, o.id AS invid,
610                   'oe' AS module, 'quotation' AS formtype,
611                   o.closed, o.amount, o.netamount
612                   FROM $form->{db} ct
613                 JOIN oe o ON (o.$form->{db}_id = ct.id)
614                 LEFT JOIN business b ON (ct.business_id = b.id)
615                   WHERE $where
616                   AND o.quotation = '1'
617                   $transwhere
618                   $openoe
619                   |;
620
621     }
622
623       $sortorder .= ", invid";
624   }
625
626   $query .= qq|
627                  ORDER BY $sortorder|;
628                  
629   my $sth = $dbh->prepare($query);
630   $sth->execute || $form->dberror($query);
631
632   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
633     $ref->{address} = "";
634     map { $ref->{address} .= "$ref->{$_} "; } qw(address1 address2 city state zipcode country);
635     push @{ $form->{CT} }, $ref;
636   }
637
638   $sth->finish;
639   $dbh->disconnect;
640
641 }
642
643
644 sub get_history {
645   my ($self, $myconfig, $form) = @_;
646   
647   # connect to database
648   my $dbh = $form->dbconnect($myconfig);
649
650   my $query;
651   my $where = "1 = 1";
652   $form->{sort} = "partnumber" unless $form->{sort};
653   my $sortorder = $form->{sort};
654   my %ordinal = ();
655   my $var;
656   my $table;
657
658   # setup ASC or DESC
659   $form->sort_order();
660   
661   if ($form->{"$form->{db}number"}) {
662     $var = $form->like(lc $form->{"$form->{db}number"});
663     $where .= " AND lower(ct.$form->{db}number) LIKE '$var'";
664   }
665   if ($form->{name}) {
666     $var = $form->like(lc $form->{name});
667     $where .= " AND lower(ct.name) LIKE '$var'";
668   }
669   if ($form->{address}) {
670     $var = $form->like(lc $form->{address});
671     $where .= " AND lower(ct.address1) LIKE '$var'";
672   }
673   if ($form->{city}) {
674     $var = $form->like(lc $form->{city});
675     $where .= " AND lower(ct.city) LIKE '$var'";
676   }
677   if ($form->{state}) {
678     $var = $form->like(lc $form->{state});
679     $where .= " AND lower(ct.state) LIKE '$var'";
680   }
681   if ($form->{zipcode}) {
682     $var = $form->like(lc $form->{zipcode});
683     $where .= " AND lower(ct.zipcode) LIKE '$var'";
684   }
685   if ($form->{country}) {
686     $var = $form->like(lc $form->{country});
687     $where .= " AND lower(ct.country) LIKE '$var'";
688   }
689   if ($form->{contact}) {
690     $var = $form->like(lc $form->{contact});
691     $where .= " AND lower(ct.contact) LIKE '$var'";
692   }
693   if ($form->{notes}) {
694     $var = $form->like(lc $form->{notes});
695     $where .= " AND lower(ct.notes) LIKE '$var'";
696   }
697   if ($form->{email}) {
698     $var = $form->like(lc $form->{email});
699     $where .= " AND lower(ct.email) LIKE '$var'";
700   }
701
702   $where .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
703   $where .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
704
705   if ($form->{open} || $form->{closed}) {
706     unless ($form->{open} && $form->{closed}) {
707       if ($form->{type} eq 'invoice') {
708         $where .= " AND a.amount != a.paid" if $form->{open};
709         $where .= " AND a.amount = a.paid" if $form->{closed};
710       } else {
711         $where .= " AND a.closed = '0'" if $form->{open};
712         $where .= " AND a.closed = '1'" if $form->{closed};
713       }
714     }
715   }
716   
717   my $invnumber = 'invnumber';
718   my $deldate = 'deliverydate';
719   my $buysell;
720   
721   if ($form->{db} eq 'customer') {
722     $buysell = "buy";
723     if ($form->{type} eq 'invoice') {
724       $where .= qq| AND a.invoice = '1' AND i.assemblyitem = '0'|;
725       $table = 'ar';
726     } else {
727       $table = 'oe';
728       if ($form->{type} eq 'order') {
729         $invnumber = 'ordnumber';
730         $where .= qq| AND a.quotation = '0'|;
731       } else {
732         $invnumber = 'quonumber';
733         $where .= qq| AND a.quotation = '1'|;
734       }
735       $deldate = 'reqdate';
736     }
737   }
738   if ($form->{db} eq 'vendor') {
739     $buysell = "sell";
740     if ($form->{type} eq 'invoice') {
741       $where .= qq| AND a.invoice = '1' AND i.assemblyitem = '0'|;
742       $table = 'ap';
743     } else {
744       $table = 'oe';
745       if ($form->{type} eq 'order') {
746         $invnumber = 'ordnumber';
747         $where .= qq| AND a.quotation = '0'|;
748       } else {
749         $invnumber = 'quonumber';
750         $where .= qq| AND a.quotation = '1'|;
751       } 
752       $deldate = 'reqdate';
753     }
754   }
755  
756   my $invjoin = qq|
757                  JOIN invoice i ON (i.trans_id = a.id)|;
758
759   if ($form->{type} eq 'order') {
760     $invjoin = qq|
761                  JOIN orderitems i ON (i.trans_id = a.id)|;
762   }
763   if ($form->{type} eq 'quotation') {
764     $invjoin = qq|
765                  JOIN orderitems i ON (i.trans_id = a.id)|;
766     $where .= qq| AND a.quotation = '1'|;
767   }
768
769
770   if ($form->{history} eq 'summary') {
771     $query = qq|SELECT curr FROM defaults|;
772     my ($curr) = $dbh->selectrow_array($query);
773     $curr =~ s/:.*//;
774     
775     %ordinal = ( partnumber     => 8,
776                  description    => 9
777                );
778     $sortorder = "2 $form->{direction}, 1, $ordinal{$sortorder} $form->{direction}";
779     
780     $query = qq|SELECT ct.id AS ctid, ct.name, ct.address1,
781                 ct.address2, ct.city, ct.state,
782                 p.id AS pid, p.partnumber, i.description, p.unit,
783                 sum(i.qty) AS qty, sum(i.sellprice) AS sellprice,
784                 '$curr' AS curr,
785                 ct.zipcode, ct.country
786                 FROM $form->{db} ct
787                 JOIN $table a ON (a.$form->{db}_id = ct.id)
788                 $invjoin
789                 JOIN parts p ON (p.id = i.parts_id)
790                 WHERE $where
791                 GROUP BY ct.id, ct.name, ct.address1, ct.address2, ct.city,
792                 ct.state, ct.zipcode, ct.country,
793                 p.id, p.partnumber, i.description, p.unit
794                 ORDER BY $sortorder|;
795   } else {
796     %ordinal = ( partnumber     => 9,
797                  description    => 12,
798                  "$deldate"     => 16,
799                  serialnumber   => 17,
800                  projectnumber  => 18
801                 );
802  
803     $sortorder = "2 $form->{direction}, 1, 11, $ordinal{$sortorder} $form->{direction}";
804     
805     $query = qq|SELECT ct.id AS ctid, ct.name, ct.address1,
806                 ct.address2, ct.city, ct.state,
807                 p.id AS pid, p.partnumber, a.id AS invid,
808                 a.$invnumber, a.curr, i.description,
809                 i.qty, i.sellprice, i.discount,
810                 i.$deldate, i.serialnumber, pr.projectnumber,
811                 e.name AS employee, ct.zipcode, ct.country, i.unit|;
812     $query .= qq|, i.fxsellprice| if $form->{type} eq 'invoice';
813
814     if ($form->{type} ne 'invoice') {
815       if ($form->{l_curr}) {
816         $query .= qq|, (SELECT $buysell FROM exchangerate ex
817                         WHERE a.curr = ex.curr
818                         AND a.transdate = ex.transdate) AS exchangerate|;
819       }
820     }
821         
822     $query .= qq|
823                 FROM $form->{db} ct
824                 JOIN $table a ON (a.$form->{db}_id = ct.id)
825                 $invjoin
826                 JOIN parts p ON (p.id = i.parts_id)
827                 LEFT JOIN project pr ON (pr.id = i.project_id)
828                 LEFT JOIN employee e ON (e.id = a.employee_id)
829                 WHERE $where
830                 ORDER BY $sortorder|;
831   }
832
833
834   my $sth = $dbh->prepare($query);
835   $sth->execute || $form->dberror($query);
836
837   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
838     $ref->{address} = "";
839     $ref->{exchangerate} = 1 unless $ref->{exchangerate};
840     map { $ref->{address} .= "$ref->{$_} "; } qw(address1 address2 city state zipcode country);
841     $ref->{id} = $ref->{ctid};
842     push @{ $form->{CT} }, $ref;
843   }
844
845   $sth->finish;
846   $dbh->disconnect;
847
848 }
849
850
851 sub pricelist {
852   my ($self, $myconfig, $form) = @_;
853   
854   # connect to database
855   my $dbh = $form->dbconnect($myconfig);
856
857   my $query;
858
859   if ($form->{db} eq 'customer') {
860     $query = qq|SELECT p.id, p.partnumber, p.description,
861                 p.sellprice, pg.partsgroup, p.partsgroup_id,
862                 m.pricebreak, m.sellprice,
863                 m.validfrom, m.validto, m.curr
864                 FROM partscustomer m
865                 JOIN parts p ON (p.id = m.parts_id)
866                 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
867                 WHERE m.customer_id = $form->{id}
868                 ORDER BY partnumber|;
869   }
870   if ($form->{db} eq 'vendor') {
871     $query = qq|SELECT p.id, p.partnumber AS sku, p.description,
872                 pg.partsgroup, p.partsgroup_id,
873                 m.partnumber, m.leadtime, m.lastcost, m.curr
874                 FROM partsvendor m
875                 JOIN parts p ON (p.id = m.parts_id)
876                 LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
877                 WHERE m.vendor_id = $form->{id}
878                 ORDER BY p.partnumber|;
879   }
880
881   my $sth;
882   my $ref;
883
884   if ($form->{id}) {
885     $sth = $dbh->prepare($query);
886     $sth->execute || $form->dberror($query);
887
888     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
889       push @{ $form->{all_partspricelist} }, $ref;
890     }
891     $sth->finish;
892   }
893
894   $query = qq|SELECT curr FROM defaults|;
895   ($form->{currencies}) = $dbh->selectrow_array($query);
896
897   $query = qq|SELECT id, partsgroup FROM partsgroup
898               ORDER BY partsgroup|;
899
900   $sth = $dbh->prepare($query);
901   $sth->execute || $self->dberror($query);
902
903   $form->{all_partsgroup} = ();
904   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
905     push @{ $form->{all_partsgroup} }, $ref;
906   }
907   $sth->finish;
908   
909   $dbh->disconnect;
910
911 }
912
913
914 sub save_pricelist {
915   my ($self, $myconfig, $form) = @_;
916
917   my $dbh = $form->dbconnect_noauto($myconfig);
918   
919   my $query = qq|DELETE FROM parts$form->{db}
920                  WHERE $form->{db}_id = $form->{id}|;
921   $dbh->do($query) || $form->dberror($query);
922
923   
924   foreach $i (1 .. $form->{rowcount}) {
925
926     if ($form->{"id_$i"}) {
927
928       if ($form->{db} eq 'customer') {
929         map { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(pricebreak sellprice);
930         
931         $query = qq|INSERT INTO parts$form->{db} (parts_id, customer_id,
932                     pricebreak, sellprice, validfrom, validto, curr)
933                     VALUES ($form->{"id_$i"}, $form->{id},
934                     $form->{"pricebreak_$i"}, $form->{"sellprice_$i"},|
935                     .$form->dbquote($form->{"validfrom_$i"}, SQL_DATE) .qq|,|
936                     .$form->dbquote($form->{"validto_$i"}, SQL_DATE) .qq|,
937                     '$form->{"curr_$i"}')|;
938       } else {
939         map { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(leadtime lastcost);
940         
941         $query = qq|INSERT INTO parts$form->{db} (parts_id, vendor_id,
942                     partnumber, lastcost, leadtime, curr)
943                     VALUES ($form->{"id_$i"}, $form->{id},
944                     '$form->{"partnumber_$i"}', $form->{"lastcost_$i"},
945                     $form->{"leadtime_$i"}, '$form->{"curr_$i"}')|;
946
947       }
948       $dbh->do($query) || $form->dberror($query);
949     }
950
951   }
952
953   $_ = $dbh->commit;
954   $dbh->disconnect;
955
956 }
957
958
959
960 sub retrieve_item {
961   my ($self, $myconfig, $form) = @_;
962
963   # connect to database
964   my $dbh = $form->dbconnect($myconfig);
965
966   my $i = $form->{rowcount};
967   my $var;
968   my $null;
969
970   my $where = "WHERE p.obsolete = '0' AND p.income_accno_id > 0";
971
972   if ($form->{"partnumber_$i"}) {
973     $var = $form->like(lc $form->{"partnumber_$i"});
974     $where .= " AND lower(p.partnumber) LIKE '$var'";
975   }
976   if ($form->{"description_$i"}) {
977     $var = $form->like(lc $form->{"description_$i"});
978     $where .= " AND lower(p.description) LIKE '$var'";
979   }
980
981   if ($form->{"partsgroup_$i"}) {
982     ($null, $var) = split /--/, $form->{"partsgroup_$i"};
983     $where .= qq| AND p.partsgroup_id = $var|;
984   }
985   
986   
987   my $query = qq|SELECT p.id, p.partnumber, p.description, p.sellprice,
988                  p.lastcost, p.unit, pg.partsgroup, p.partsgroup_id
989                  FROM parts p
990                  LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
991                  $where
992                  |;
993   my $sth = $dbh->prepare($query);
994   $sth->execute || $form->dberror($query);
995
996   my $ref;
997   $form->{item_list} = ();
998   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
999     push @{ $form->{item_list} }, $ref;
1000   }
1001   $sth->finish;
1002   $dbh->disconnect;
1003
1004 }
1005
1006
1007 1;
1008