This commit was generated by cvs2svn to compensate for changes in r4407,
[freeside.git] / sql-ledger / SL / HR.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 # backend code for human resources and payroll
26 #
27 #======================================================================
28
29 package HR;
30
31
32 sub get_employee {
33   my ($self, $myconfig, $form) = @_;
34
35   my $dbh = $form->dbconnect($myconfig);
36
37   my $query;
38   my $sth;
39   my $ref;
40   my $notid = "";
41
42   if ($form->{id}) {
43     $query = qq|SELECT e.*
44                 FROM employee e
45                 WHERE e.id = $form->{id}|;
46     $sth = $dbh->prepare($query);
47     $sth->execute || $form->dberror($query);
48   
49     $ref = $sth->fetchrow_hashref(NAME_lc);
50   
51     # check if employee can be deleted, orphaned
52     $form->{status} = "orphaned" unless $ref->{login};
53
54 $form->{status} = 'orphaned';   # leave orphaned for now until payroll is done
55
56     $ref->{employeelogin} = $ref->{login};
57     delete $ref->{login};
58     map { $form->{$_} = $ref->{$_} } keys %$ref;
59
60     $sth->finish;
61
62     # get manager
63     $form->{managerid} *= 1;
64     $query = qq|SELECT name
65                 FROM employee
66                 WHERE id = $form->{managerid}|;
67     ($form->{manager}) = $dbh->selectrow_array($query);
68     
69                 
70 ######### disabled for now
71 if ($form->{deductions}) {
72     # get allowances
73     $query = qq|SELECT d.id, d.description, da.before, da.after, da.rate
74                 FROM employeededuction da
75                 JOIN deduction d ON (da.deduction_id = d.id)
76                 WHERE da.employee_id = $form->{id}|;
77     $sth = $dbh->prepare($query);
78     $sth->execute || $form->dberror($query);
79
80     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
81       $ref->{rate} *= 100;
82       push @{ $form->{all_employeededuction} }, $ref;
83     }
84     $sth->finish;
85 }    
86
87     $notid = qq|AND id != $form->{id}|;
88     
89   }
90
91   
92   # get managers
93   $query = qq|SELECT id, name
94               FROM employee
95               WHERE sales = '1'
96               AND role = 'manager'
97               $notid
98               ORDER BY 2|;
99   $sth = $dbh->prepare($query);
100   $sth->execute || $form->dberror($query);
101
102   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
103     push @{ $form->{all_manager} }, $ref;
104   }
105   $sth->finish;
106
107
108   # get deductions
109 if ($form->{deductions}) {  
110   $query = qq|SELECT id, description
111               FROM deduction
112               ORDER BY 2|;
113   $sth = $dbh->prepare($query);
114   $sth->execute || $form->dberror($query);
115
116   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
117     push @{ $form->{all_deduction} }, $ref;
118   }
119   $sth->finish;
120 }
121
122   $dbh->disconnect;
123
124 }
125
126
127
128 sub save_employee {
129   my ($self, $myconfig, $form) = @_;
130
131   # connect to database
132   my $dbh = $form->dbconnect_noauto($myconfig);
133   my $query;
134   my $sth;
135
136   if (! $form->{id}) {
137     my $uid = time;
138     $uid .= $form->{login};
139
140     $query = qq|INSERT INTO employee (name)
141                 VALUES ('$uid')|;
142     $dbh->do($query) || $form->dberror($query);
143     
144     $query = qq|SELECT id FROM employee
145                 WHERE name = '$uid'|;
146     $sth = $dbh->prepare($query);
147     $sth->execute || $form->dberror($query);
148
149     ($form->{id}) = $sth->fetchrow_array;
150     $sth->finish;
151   }
152
153   my ($null, $managerid) = split /--/, $form->{manager};
154   $managerid *= 1;
155   $form->{sales} *= 1;
156
157   $form->{employeenumber} = $form->update_defaults($myconfig, "employeenumber", $dbh) if ! $form->{employeenumber};
158
159   $query = qq|UPDATE employee SET
160               employeenumber = |.$dbh->quote($form->{employeenumber}).qq|,
161               name = |.$dbh->quote($form->{name}).qq|,
162               address1 = |.$dbh->quote($form->{address1}).qq|,
163               address2 = |.$dbh->quote($form->{address2}).qq|,
164               city = |.$dbh->quote($form->{city}).qq|,
165               state = |.$dbh->quote($form->{state}).qq|,
166               zipcode = |.$dbh->quote($form->{zipcode}).qq|,
167               country = |.$dbh->quote($form->{country}).qq|,
168               workphone = '$form->{workphone}',
169               homephone = '$form->{homephone}',
170               startdate = |.$form->dbquote($form->{startdate}, SQL_DATE).qq|,
171               enddate = |.$form->dbquote($form->{enddate}, SQL_DATE).qq|,
172               notes = |.$dbh->quote($form->{notes}).qq|,
173               role = '$form->{role}',
174               sales = '$form->{sales}',
175               email = |.$dbh->quote($form->{email}).qq|,
176               ssn = '$form->{ssn}',
177               dob = |.$form->dbquote($form->{dob}, SQL_DATE).qq|,
178               iban = '$form->{iban}',
179               bic = '$form->{bic}',
180               managerid = $managerid
181               WHERE id = $form->{id}|;
182   $dbh->do($query) || $form->dberror($query);
183
184 # for now
185 if ($form->{selectdeduction}) {       
186   # insert deduction and allowances for payroll
187   $query = qq|DELETE FROM employeededuction
188               WHERE employee_id = $form->{id}|;
189   $dbh->do($query) || $form->dberror($query);
190
191   $query = qq|INSERT INTO employeededuction (employee_id, deduction_id,
192               before, after, rate) VALUES ($form->{id},?,?,?,?)|;
193   my $sth = $dbh->prepare($query) || $form->dberror($query);
194
195   for ($i = 1; $i <= $form->{deduction_rows}; $i++) {
196     map { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(before after);
197     ($null, $deduction_id) = split /--/, $form->{"deduction_$i"};
198     if ($deduction_id) {
199       $sth->execute($deduction_id, $form->{"before_$i"}, $form->{"after_$i"}, $form->{"rate_$i"} / 100) || $form->dberror($query);
200     }
201   }
202   $sth->finish;
203 }
204
205   $dbh->commit;
206   $dbh->disconnect;
207
208 }
209
210
211 sub delete_employee {
212   my ($self, $myconfig, $form) = @_;
213
214   # connect to database
215   my $dbh = $form->dbconnect_noauto($myconfig);
216
217   # delete employee
218   my $query = qq|DELETE FROM $form->{db}
219                  WHERE id = $form->{id}|;
220   $dbh->do($query) || $form->dberror($query);
221
222   $dbh->commit;
223   $dbh->disconnect;
224
225 }
226
227
228 sub employees {
229   my ($self, $myconfig, $form) = @_;
230
231   # connect to database
232   my $dbh = $form->dbconnect($myconfig);
233
234   my $where = "1 = 1";
235   $form->{sort} = ($form->{sort}) ? $form->{sort} : "name";
236   my @a = qw(name);
237   my $sortorder = $form->sort_order(\@a);
238   
239   my $var;
240   
241   if ($form->{startdate}) {
242     $where .= " AND e.startdate >= '$startdate'";
243   }
244   if ($form->{enddate}) {
245     $where .= " AND e.enddate >= '$enddate'";
246   }
247   if ($form->{name}) {
248     $var = $form->like(lc $form->{name});
249     $where .= " AND lower(e.name) LIKE '$var'";
250   }
251   if ($form->{notes}) {
252     $var = $form->like(lc $form->{notes});
253     $where .= " AND lower(e.notes) LIKE '$var'";
254   }
255   if ($form->{status} eq 'sales') {
256     $where .= " AND e.sales = '1'";
257   }
258   if ($form->{status} eq 'orphaned') {
259     $where .= qq| AND e.login IS NULL|;
260   }
261
262   my $query = qq|SELECT e.*, m.name AS manager
263                  FROM employee e
264                  LEFT JOIN employee m ON (m.id = e.managerid)
265                  WHERE $where
266                  ORDER BY $sortorder|;
267
268   my $sth = $dbh->prepare($query);
269   $sth->execute || $form->dberror($query);
270
271   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
272     $ref->{address} = "";
273     map { $ref->{address} .= "$ref->{$_} "; } qw(address1 address2 city state zipcode country);
274     push @{ $form->{all_employee} }, $ref;
275   }
276
277   $sth->finish;
278   $dbh->disconnect;
279
280 }
281
282
283 sub get_deduction {
284   my ($self, $myconfig, $form) = @_;
285
286   my $dbh = $form->dbconnect($myconfig);
287   my $query;
288   my $sth;
289   my $ref;
290   my $item;
291   my $i;
292   
293   if ($form->{id}) {
294     $query = qq|SELECT d.*,
295                  c1.accno AS ap_accno,
296                  c1.description AS ap_description,
297                  c2.accno AS expense_accno,
298                  c2.description AS expense_description
299                  FROM deduction d
300                  LEFT JOIN chart c1 ON (c1.id = d.ap_accno_id)
301                  LEFT JOIN chart c2 ON (c2.id = d.expense_accno_id)
302                  WHERE d.id = $form->{id}|;
303     $sth = $dbh->prepare($query);
304     $sth->execute || $form->dberror($query);
305   
306     $ref = $sth->fetchrow_hashref(NAME_lc);
307     map { $form->{$_} = $ref->{$_} } keys %$ref;
308
309     $sth->finish;
310   
311     # check if orphaned
312 $form->{status} = 'orphaned';     # for now
313   
314
315     # get the rates
316     $query = qq|SELECT rate, amount, above, below
317                 FROM deductionrate
318                 WHERE trans_id = $form->{id}
319                 ORDER BY rate, amount|;
320     $sth = $dbh->prepare($query);
321     $sth->execute || $form->dberror($query);
322
323     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
324       push @{ $form->{deductionrate} }, $ref;
325     }
326     $sth->finish;
327                 
328     # get all for deductionbase
329     $query = qq|SELECT d.description, d.id, db.maximum
330                 FROM deductionbase db
331                 JOIN deduction d ON (d.id = db.deduction_id)
332                 WHERE db.trans_id = $form->{id}|;
333     $sth = $dbh->prepare($query);
334     $sth->execute || $form->dberror($query);
335
336     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
337       push @{ $form->{deductionbase} }, $ref;
338     }
339     $sth->finish;
340            
341     # get all for deductionafter
342     $query = qq|SELECT d.description, d.id
343                 FROM deductionafter da
344                 JOIN deduction d ON (d.id = da.deduction_id)
345                 WHERE da.trans_id = $form->{id}|;
346     $sth = $dbh->prepare($query);
347     $sth->execute || $form->dberror($query);
348
349     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
350       push @{ $form->{deductionafter} }, $ref;
351     }
352     $sth->finish;
353     
354     # build selection list for base and after
355     $query = qq|SELECT id, description
356                 FROM deduction
357                 WHERE id != $form->{id}
358                 ORDER BY 2|;
359            
360   } else {
361     # build selection list for base and after
362     $query = qq|SELECT id, description
363                 FROM deduction
364                 ORDER BY 2|;
365   }
366
367   $sth = $dbh->prepare($query);
368   $sth->execute || $form->dberror($query);
369
370   while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
371     push @{ $form->{all_deduction} }, $ref;
372   }
373   $sth->finish;
374
375       
376   my %category = ( ap           => 'L',
377                    expense      => 'E' );
378   
379   foreach $item (keys %category) {
380     $query = qq|SELECT accno, description
381                 FROM chart
382                 WHERE charttype = 'A'
383                 AND category = '$category{$item}'
384                 ORDER BY accno|;
385     $sth = $dbh->prepare($query);
386     $sth->execute || $form->dberror($query);
387
388     while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
389       push @{ $form->{"${item}_accounts"} }, $ref;
390     }
391     $sth->finish;
392   }
393
394    
395   $dbh->disconnect;
396
397 }
398
399
400 sub deductions {
401   my ($self, $myconfig, $form) = @_;
402
403   my $dbh = $form->dbconnect($myconfig);
404   
405   my $query = qq|SELECT d.id, d.description, d.employeepays, d.employerpays,
406                  c1.accno AS ap_accno, c2.accno AS expense_accno,
407                  dr.rate, dr.amount, dr.above, dr.below
408                  FROM deduction d
409                  JOIN deductionrate dr ON (dr.trans_id = d.id)
410                  LEFT JOIN chart c1 ON (d.ap_accno_id = c1.id)
411                  LEFT JOIN chart c2 ON (d.expense_accno_id = c2.id)
412                  ORDER BY 2, 7, 8|;
413   my $sth = $dbh->prepare($query);
414   $sth->execute || $form->dberror($query);
415   
416   while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
417     push @{ $form->{all_deduction} }, $ref;
418   }
419   
420   $sth->finish;
421   $dbh->disconnect;
422
423 }
424
425
426 sub save_deduction {
427   my ($self, $myconfig, $form) = @_;
428
429   # connect to database
430   my $dbh = $form->dbconnect_noauto($myconfig);
431
432   ($form->{ap_accno}) = split /--/, $form->{ap_accno};
433   ($form->{expense_accno}) = split /--/, $form->{expense_accno};
434   
435   my $null;
436   my $deduction_id;
437   my $query;
438   my $sth;
439
440   if (! $form->{id}) {
441     my $uid = time;
442     $uid .= $form->{login};
443
444     $query = qq|INSERT INTO deduction (description)
445                 VALUES ('$uid')|;
446     $dbh->do($query) || $form->dberror($query);
447     
448     $query = qq|SELECT id FROM deduction
449                 WHERE description = '$uid'|;
450     $sth = $dbh->prepare($query);
451     $sth->execute || $form->dberror($query);
452
453     ($form->{id}) = $sth->fetchrow_array;
454     $sth->finish;
455   }
456
457   
458   map { $form->{$_} = $form->parse_amount($myconfig, $form->{$_}) } qw(employeepays employerpays);
459   
460   $query = qq|UPDATE deduction SET
461               description = |.$dbh->quote($form->{description}).qq|,
462               ap_accno_id =
463                    (SELECT id FROM chart
464                     WHERE accno = '$form->{ap_accno}'),
465               expense_accno_id =
466                    (SELECT id FROM chart
467                     WHERE accno = '$form->{expense_accno}'),
468               employerpays = '$form->{employerpays}',
469               employeepays = '$form->{employeepays}',
470               fromage = |.$form->dbquote($form->{fromage}, SQL_INT).qq|,
471               toage = |.$form->dbquote($form->{toage}, SQL_INT).qq|
472               WHERE id = $form->{id}|;
473   $dbh->do($query) || $form->dberror($query);
474
475
476   $query = qq|DELETE FROM deductionrate
477               WHERE trans_id = $form->{id}|;
478   $dbh->do($query) || $form->dberror($query);
479   
480   $query = qq|INSERT INTO deductionrate
481               (trans_id, rate, amount, above, below) VALUES (?,?,?,?,?)|;
482   $sth = $dbh->prepare($query) || $form->dberror($query);
483  
484   for ($i = 1; $i <= $form->{rate_rows}; $i++) {
485     map { $form->{"${_}_$i"} = $form->parse_amount($myconfig, $form->{"${_}_$i"}) } qw(rate amount above below);
486     $form->{"rate_$i"} /= 100;
487
488     if ($form->{"rate_$i"} || $form->{"amount_$i"}) {
489       $sth->execute($form->{id}, $form->{"rate_$i"}, $form->{"amount_$i"}, $form->{"above_$i"}, $form->{"below_$i"}) || $form->dberror($query);
490     }
491   }
492   $sth->finish;
493
494
495   $query = qq|DELETE FROM deductionbase
496               WHERE trans_id = $form->{id}|;
497   $dbh->do($query) || $form->dberror($query);
498  
499   $query = qq|INSERT INTO deductionbase
500               (trans_id, deduction_id, maximum) VALUES (?,?,?)|;
501   $sth = $dbh->prepare($query) || $form->dberror($query);
502  
503   for ($i = 1; $i <= $form->{base_rows}; $i++) {
504     ($null, $deduction_id) = split /--/, $form->{"base_$i"};
505     $form->{"maximum_$i"} = $form->parse_amount($myconfig, $form->{"maximum_$i"});
506     if ($deduction_id) {
507       $sth->execute($form->{id}, $deduction_id, $form->{"maximum_$i"}) || $form->dberror($query);
508     }
509   }
510   $sth->finish;
511
512
513   $query = qq|DELETE FROM deductionafter
514               WHERE trans_id = $form->{id}|;
515   $dbh->do($query) || $form->dberror($query);
516  
517   $query = qq|INSERT INTO deductionafter
518               (trans_id, deduction_id) VALUES (?,?)|;
519   $sth = $dbh->prepare($query) || $form->dberror($query);
520  
521   for ($i = 1; $i <= $form->{after_rows}; $i++) {
522     ($null, $deduction_id) = split /--/, $form->{"after_$i"};
523     if ($deduction_id) {
524       $sth->execute($form->{id}, $deduction_id) || $form->dberror($query);
525     }
526   }
527   $sth->finish;
528  
529   $dbh->commit;
530   $dbh->disconnect;
531
532 }
533
534
535 sub delete_deduction {
536   my ($self, $myconfig, $form) = @_;
537
538   # connect to database
539   my $dbh = $form->dbconnect_noauto($myconfig);
540
541   # delete deduction
542   my $query = qq|DELETE FROM $form->{db}
543                  WHERE id = $form->{id}|;
544   $dbh->do($query) || $form->dberror($query);
545
546   foreach $item (qw(rate base after)) {
547     $query = qq|DELETE FROM deduction$item
548                 WHERE trans_id = $form->{id}|;
549     $dbh->do($query) || $form->dberror($query);
550   }
551
552   $dbh->commit;
553   $dbh->disconnect;
554
555 }
556
557 1;
558