This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / sql-ledger / SL / PE.pm
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2003
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 # Project module
26 # also used for partsgroups
27 #
28 #======================================================================
29
30 package PE;
31
32
33 sub projects {
34   my ($self, $myconfig, $form) = @_;
35   
36   # connect to database
37   my $dbh = $form->dbconnect($myconfig);
38
39   $form->{sort} = "projectnumber" unless $form->{sort};
40   my @a = ($form->{sort});
41   my %ordinal = ( projectnumber => 2,
42                   description   => 3 );
43   my $sortorder = $form->sort_order(\@a, \%ordinal);
44
45   my $query = qq|SELECT id, projectnumber, description
46                  FROM project
47                  WHERE 1 = 1|;
48
49   if ($form->{projectnumber}) {
50     my $projectnumber = $form->like(lc $form->{projectnumber});
51     $query .= " AND lower(projectnumber) LIKE '$projectnumber'";
52   }
53   if ($form->{projectdescription}) {
54     my $description = $form->like(lc $form->{projectdescription});
55     $query .= " AND lower(description) LIKE '$description'";
56   }
57   if ($form->{status} eq 'orphaned') {
58     $query .= " AND id NOT IN (SELECT p.id
59                                FROM project p, acc_trans a
60                                WHERE p.id = a.project_id)
61                 AND id NOT IN (SELECT p.id
62                                FROM project p, invoice i
63                                WHERE p.id = i.project_id)
64                 AND id NOT IN (SELECT p.id
65                                FROM project p, orderitems o
66                                WHERE p.id = o.project_id)";
67   }
68
69   $query .= qq|
70                  ORDER BY $sortorder|;
71
72   $sth = $dbh->prepare($query);
73   $sth->execute || $form->dberror($query);
74
75   my $i = 0;
76   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
77     push @{ $form->{project_list} }, $ref;
78     $i++;
79   }
80
81   $sth->finish;
82   $dbh->disconnect;
83   
84   $i;
85
86 }
87
88
89 sub get_project {
90   my ($self, $myconfig, $form) = @_;
91
92   # connect to database
93   my $dbh = $form->dbconnect($myconfig);
94   
95   my $query = qq|SELECT *
96                  FROM project
97                  WHERE id = $form->{id}|;
98   my $sth = $dbh->prepare($query);
99   $sth->execute || $form->dberror($query);
100
101   my $ref = $sth->fetchrow_hashref(NAME_lc);
102   
103   map { $form->{$_} = $ref->{$_} } keys %$ref;
104
105   $sth->finish;
106
107   # check if it is orphaned
108   $query = qq|SELECT count(*)
109               FROM acc_trans
110               WHERE project_id = $form->{id}
111            UNION
112               SELECT count(*)
113               FROM invoice
114               WHERE project_id = $form->{id}
115            UNION
116               SELECT count(*)
117               FROM orderitems
118               WHERE project_id = $form->{id}
119              |;
120   $sth = $dbh->prepare($query);
121   $sth->execute || $form->dberror($query);
122
123   while (my ($count) = $sth->fetchrow_array) {
124     $form->{orphaned} += $count;
125   }
126   $sth->finish;
127   $form->{orphaned} = !$form->{orphaned};
128   
129   $dbh->disconnect;
130
131 }
132
133
134 sub save_project {
135   my ($self, $myconfig, $form) = @_;
136   
137   # connect to database
138   my $dbh = $form->dbconnect($myconfig);
139   
140   if ($form->{id}) {
141     $query = qq|UPDATE project SET
142                 projectnumber = |.$dbh->quote($form->{projectnumber}).qq|,
143                 description = |.$dbh->quote($form->{description}).qq|
144                 WHERE id = $form->{id}|;
145   } else {
146     $query = qq|INSERT INTO project
147                 (projectnumber, description)
148                 VALUES (|
149                 .$dbh->quote($form->{projectnumber}).qq|, |
150                 .$dbh->quote($form->{description}).qq|)|;
151   }
152   $dbh->do($query) || $form->dberror($query);
153   
154   $dbh->disconnect;
155
156 }
157
158
159 sub partsgroups {
160   my ($self, $myconfig, $form) = @_;
161   
162   my $var;
163   
164   # connect to database
165   my $dbh = $form->dbconnect($myconfig);
166
167   $form->{sort} = "partsgroup" unless $form->{partsgroup};
168   my @a = (partsgroup);
169   my $sortorder = $form->sort_order(\@a);
170
171   my $query = qq|SELECT g.*
172                  FROM partsgroup g|;
173
174   my $where = "1 = 1";
175   
176   if ($form->{partsgroup}) {
177     $var = $form->like(lc $form->{partsgroup});
178     $where .= " AND lower(partsgroup) LIKE '$var'";
179   }
180   $query .= qq|
181                WHERE $where
182                ORDER BY $sortorder|;
183   
184   if ($form->{status} eq 'orphaned') {
185     $query = qq|SELECT g.*
186                 FROM partsgroup g
187                 LEFT JOIN parts p ON (p.partsgroup_id = g.id)
188                 WHERE $where
189                 EXCEPT
190                 SELECT g.*
191                 FROM partsgroup g
192                 JOIN parts p ON (p.partsgroup_id = g.id)
193                 WHERE $where
194                 ORDER BY $sortorder|;
195   }
196
197   $sth = $dbh->prepare($query);
198   $sth->execute || $form->dberror($query);
199
200   my $i = 0;
201   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
202     push @{ $form->{item_list} }, $ref;
203     $i++;
204   }
205
206   $sth->finish;
207   $dbh->disconnect;
208   
209   $i;
210
211 }
212
213
214 sub save_partsgroup {
215   my ($self, $myconfig, $form) = @_;
216   
217   # connect to database
218   my $dbh = $form->dbconnect($myconfig);
219   
220   if ($form->{id}) {
221     $query = qq|UPDATE partsgroup SET
222                 partsgroup = |.$dbh->quote($form->{partsgroup}).qq|
223                 WHERE id = $form->{id}|;
224   } else {
225     $query = qq|INSERT INTO partsgroup
226                 (partsgroup)
227                 VALUES (|.$dbh->quote($form->{partsgroup}).qq|)|;
228   }
229   $dbh->do($query) || $form->dberror($query);
230   
231   $dbh->disconnect;
232
233 }
234
235
236 sub get_partsgroup {
237   my ($self, $myconfig, $form) = @_;
238
239   # connect to database
240   my $dbh = $form->dbconnect($myconfig);
241   
242   my $query = qq|SELECT *
243                  FROM partsgroup
244                  WHERE id = $form->{id}|;
245   my $sth = $dbh->prepare($query);
246   $sth->execute || $form->dberror($query);
247
248   my $ref = $sth->fetchrow_hashref(NAME_lc);
249  
250   map { $form->{$_} = $ref->{$_} } keys %$ref;
251
252   $sth->finish;
253
254   # check if it is orphaned
255   $query = qq|SELECT count(*)
256               FROM parts
257               WHERE partsgroup_id = $form->{id}|;
258   $sth = $dbh->prepare($query);
259   $sth->execute || $form->dberror($query);
260
261   ($form->{orphaned}) = $sth->fetchrow_array;
262   $form->{orphaned} = !$form->{orphaned};
263        
264   $sth->finish;
265   
266   $dbh->disconnect;
267
268 }
269
270
271 sub pricegroups {
272   my ($self, $myconfig, $form) = @_;
273   
274   my $var;
275   
276   # connect to database
277   my $dbh = $form->dbconnect($myconfig);
278
279   $form->{sort} = "pricegroup" unless $form->{sort};
280   my @a = (pricegroup);
281   my $sortorder = $form->sort_order(\@a);
282
283   my $query = qq|SELECT g.*
284                  FROM pricegroup g|;
285
286   my $where = "1 = 1";
287   
288   if ($form->{pricegroup}) {
289     $var = $form->like(lc $form->{pricegroup});
290     $where .= " AND lower(pricegroup) LIKE '$var'";
291   }
292   $query .= qq|
293                WHERE $where
294                ORDER BY $sortorder|;
295   
296   if ($form->{status} eq 'orphaned') {
297     $query = qq|SELECT g.*
298                 FROM pricegroup g
299                 WHERE $where
300                 AND g.id NOT IN (SELECT DISTINCT pricegroup_id
301                                  FROM partscustomer)
302                 ORDER BY $sortorder|;
303   }
304
305   $sth = $dbh->prepare($query);
306   $sth->execute || $form->dberror($query);
307
308   my $i = 0;
309   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
310     push @{ $form->{item_list} }, $ref;
311     $i++;
312   }
313
314   $sth->finish;
315   $dbh->disconnect;
316   
317   $i;
318
319 }
320
321
322 sub save_pricegroup {
323   my ($self, $myconfig, $form) = @_;
324   
325   # connect to database
326   my $dbh = $form->dbconnect($myconfig);
327   
328   if ($form->{id}) {
329     $query = qq|UPDATE pricegroup SET
330                 pricegroup = |.$dbh->quote($form->{pricegroup}).qq|
331                 WHERE id = $form->{id}|;
332   } else {
333     $query = qq|INSERT INTO pricegroup
334                 (pricegroup)
335                 VALUES (|.$dbh->quote($form->{pricegroup}).qq|)|;
336   }
337   $dbh->do($query) || $form->dberror($query);
338   
339   $dbh->disconnect;
340
341 }
342
343
344 sub get_pricegroup {
345   my ($self, $myconfig, $form) = @_;
346
347   # connect to database
348   my $dbh = $form->dbconnect($myconfig);
349   
350   my $query = qq|SELECT *
351                  FROM pricegroup
352                  WHERE id = $form->{id}|;
353   my $sth = $dbh->prepare($query);
354   $sth->execute || $form->dberror($query);
355
356   my $ref = $sth->fetchrow_hashref(NAME_lc);
357  
358   map { $form->{$_} = $ref->{$_} } keys %$ref;
359
360   $sth->finish;
361
362   # check if it is orphaned
363   $query = qq|SELECT count(*)
364               FROM partscustomer
365               WHERE pricegroup_id = $form->{id}|;
366   $sth = $dbh->prepare($query);
367   $sth->execute || $form->dberror($query);
368
369   ($form->{orphaned}) = $sth->fetchrow_array;
370   $form->{orphaned} = !$form->{orphaned};
371        
372   $sth->finish;
373   
374   $dbh->disconnect;
375
376 }
377
378
379 sub delete_tuple {
380   my ($self, $myconfig, $form) = @_;
381   
382   # connect to database
383   my $dbh = $form->dbconnect_noauto($myconfig);
384   
385   $query = qq|DELETE FROM $form->{type}
386               WHERE id = $form->{id}|;
387   $dbh->do($query) || $form->dberror($query);
388   
389   if ($form->{type} !~ /pricegroup/) {
390     $query = qq|DELETE FROM translation
391                 WHERE trans_id = $form->{id}|;
392     $dbh->do($query) || $form->dberror($query);
393   }
394  
395   $dbh->commit;
396   $dbh->disconnect;
397
398 }
399
400
401 sub description_translations {
402   my ($self, $myconfig, $form) = @_;
403
404   my $where = "1 = 1\n";
405   my $var;
406   my $ref;
407   
408   map { $where .= "AND lower(p.$_) LIKE '".$form->like(lc $form->{$_})."'\n" if $form->{$_} } qw(partnumber description);
409   
410   $where .= " AND p.obsolete = '0'";
411   $where .= " AND p.id = $form->{id}" if $form->{id};
412
413   # connect to database
414   my $dbh = $form->dbconnect($myconfig);
415
416   my %ordinal = ( 'partnumber' => 2,
417                   'description' => 3
418                 );
419   
420   my @a = qw(partnumber description);
421   my $sortorder = $form->sort_order(\@a, \%ordinal);
422
423   my $query = qq|SELECT l.description AS language, t.description AS translation,
424                  l.code
425                  FROM translation t
426                  JOIN language l ON (l.code = t.language_code)
427                  WHERE trans_id = ?
428                  ORDER BY 1|;
429   my $tth = $dbh->prepare($query);
430   
431   $query = qq|SELECT p.id, p.partnumber, p.description
432               FROM parts p
433               WHERE $where
434               ORDER BY $sortorder|;
435
436   my $sth = $dbh->prepare($query);
437   $sth->execute || $form->dberror($query);
438
439   my $tra;
440   
441   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
442     push @{ $form->{translations} }, $ref;
443
444     # get translations for description
445     $tth->execute($ref->{id}) || $form->dberror;
446
447     while ($tra = $tth->fetchrow_hashref(NAME_lc)) {
448       $form->{trans_id} = $ref->{id};
449       $tra->{id} = $ref->{id};
450       push @{ $form->{translations} }, $tra;
451     }
452
453   }
454   $sth->finish;
455
456   &get_language("", $dbh, $form) if $form->{id};
457
458   $dbh->disconnect;
459
460 }
461
462
463 sub partsgroup_translations {
464   my ($self, $myconfig, $form) = @_;
465
466   my $where = "1 = 1\n";
467   my $ref;
468   
469   if ($form->{description}) {
470     $where .= "AND lower(p.partsgroup) LIKE '".$form->like(lc $form->{description})."'";
471   }
472   $where .= " AND p.id = $form->{id}" if $form->{id};
473   
474   # connect to database
475   my $dbh = $form->dbconnect($myconfig);
476
477   my $query = qq|SELECT l.description AS language, t.description AS translation,
478                  l.code
479                  FROM translation t
480                  JOIN language l ON (l.code = t.language_code)
481                  WHERE trans_id = ?
482                  ORDER BY 1|;
483   my $tth = $dbh->prepare($query);
484   
485   $form->sort_order();
486   
487   $query = qq|SELECT p.id, p.partsgroup AS description
488               FROM partsgroup p
489               WHERE $where
490               ORDER BY 2 $form->{direction}|;
491
492   my $sth = $dbh->prepare($query);
493   $sth->execute || $form->dberror($query);
494
495   my $tra;
496   
497   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
498     push @{ $form->{translations} }, $ref;
499
500     # get translations for partsgroup
501     $tth->execute($ref->{id}) || $form->dberror;
502
503     while ($tra = $tth->fetchrow_hashref(NAME_lc)) {
504       $form->{trans_id} = $ref->{id};
505       push @{ $form->{translations} }, $tra;
506     }
507
508   }
509   $sth->finish;
510
511   &get_language("", $dbh, $form) if $form->{id};
512
513   $dbh->disconnect;
514
515 }
516
517
518 sub project_translations {
519   my ($self, $myconfig, $form) = @_;
520
521   my $where = "1 = 1\n";
522   my $var;
523   my $ref;
524   
525   map { $where .= "AND lower(p.$_) LIKE '".$form->like(lc $form->{$_})."'\n" if $form->{$_} } qw(projectnumber description);
526   
527   $where .= " AND p.id = $form->{id}" if $form->{id};
528
529   # connect to database
530   my $dbh = $form->dbconnect($myconfig);
531
532   my %ordinal = ( 'projectnumber' => 2,
533                   'description' => 3
534                 );
535   
536   my @a = qw(projectnumber description);
537   my $sortorder = $form->sort_order(\@a, \%ordinal);
538
539   my $query = qq|SELECT l.description AS language, t.description AS translation,
540                  l.code
541                  FROM translation t
542                  JOIN language l ON (l.code = t.language_code)
543                  WHERE trans_id = ?
544                  ORDER BY 1|;
545   my $tth = $dbh->prepare($query);
546   
547   $query = qq|SELECT p.id, p.projectnumber, p.description
548               FROM project p
549               WHERE $where
550               ORDER BY $sortorder|;
551
552   my $sth = $dbh->prepare($query);
553   $sth->execute || $form->dberror($query);
554
555   my $tra;
556   
557   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
558     push @{ $form->{translations} }, $ref;
559
560     # get translations for description
561     $tth->execute($ref->{id}) || $form->dberror;
562
563     while ($tra = $tth->fetchrow_hashref(NAME_lc)) {
564       $form->{trans_id} = $ref->{id};
565       $tra->{id} = $ref->{id};
566       push @{ $form->{translations} }, $tra;
567     }
568
569   }
570   $sth->finish;
571
572   &get_language("", $dbh, $form) if $form->{id};
573
574   $dbh->disconnect;
575
576 }
577
578
579 sub get_language {
580   my ($self, $dbh, $form) = @_;
581   
582   # get language
583   my $query = qq|SELECT *
584                  FROM language
585                  ORDER BY 2|;
586   my $sth = $dbh->prepare($query);
587   $sth->execute || $form->dberror($query);
588
589   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
590     push @{ $form->{all_language} }, $ref;
591   }
592   $sth->finish;
593
594 }
595
596
597 sub save_translation {
598   my ($self, $myconfig, $form) = @_;
599
600   # connect to database
601   my $dbh = $form->dbconnect_noauto($myconfig);
602
603   my $query = qq|DELETE FROM translation
604                  WHERE trans_id = $form->{id}|;
605   $dbh->do($query) || $form->dberror($query);
606
607   $query = qq|INSERT INTO translation (trans_id, language_code, description)
608               VALUES ($form->{id}, ?, ?)|;
609   my $sth = $dbh->prepare($query) || $form->dberror($query);
610
611   foreach my $i (1 .. $form->{translation_rows}) {
612     if ($form->{"language_code_$i"}) {
613       $sth->execute($form->{"language_code_$i"}, $form->{"translation_$i"});
614       $sth->finish;
615     }
616   }
617   $dbh->commit;
618   $dbh->disconnect;
619
620 }
621
622
623 sub delete_translation {
624   my ($self, $myconfig, $form) = @_;
625   
626   # connect to database
627   my $dbh = $form->dbconnect($myconfig);
628   
629   $query = qq|DELETE FROM translation
630               WHERE trans_id = $form->{id}|;
631   $dbh->do($query) || $form->dberror($query);
632   
633   $dbh->disconnect;
634
635 }
636
637
638 1;
639