summaryrefslogtreecommitdiff
path: root/LedgerSMB/HR.pm
blob: 91c1a0608cd371377f7360f7fd7cf9270d2aa4b8 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. # http://www.ledgersmb.org/
  5. #
  6. # Copyright (C) 2006
  7. # This work contains copyrighted information from a number of sources all used
  8. # with permission.
  9. #
  10. # This file contains source code included with or based on SQL-Ledger which
  11. # is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
  12. # under the GNU General Public License version 2 or, at your option, any later
  13. # version. For a full list including contact information of contributors,
  14. # maintainers, and copyright holders, see the CONTRIBUTORS file.
  15. #
  16. # Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
  17. # Copyright (C) 2003
  18. #
  19. # Author: DWS Systems Inc.
  20. # Web: http://www.sql-ledger.org
  21. #
  22. # Contributors:
  23. #
  24. #======================================================================
  25. #
  26. # This file has NOT undergone whitespace cleanup.
  27. #
  28. #======================================================================
  29. #
  30. # backend code for human resources and payroll
  31. #
  32. #======================================================================
  33. package HR;
  34. sub get_employee {
  35. my ($self, $myconfig, $form) = @_;
  36. my $dbh = $form->dbconnect($myconfig);
  37. my $query;
  38. my $sth;
  39. my $ref;
  40. my $notid = "";
  41. if ($form->{id}) {
  42. $query = qq|SELECT e.* FROM employee e WHERE e.id = ?|;
  43. $sth = $dbh->prepare($query);
  44. $sth->execute($form->{id}) || $form->dberror($query);
  45. $ref = $sth->fetchrow_hashref(NAME_lc);
  46. # check if employee can be deleted, orphaned
  47. $form->{status} = "orphaned" unless $ref->{login};
  48. $ref->{employeelogin} = $ref->{login};
  49. delete $ref->{login};
  50. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  51. $sth->finish;
  52. # get manager
  53. $form->{managerid} *= 1;
  54. $query = qq|SELECT name FROM employee WHERE id = ?|;
  55. $sth = $dbh->prepare($query);
  56. $sth->execute($form->{managerid});
  57. ($form->{manager}) = $sth->fetchrow_array($query);
  58. $notid = qq|AND id != |.$dbh->quote($form->{id});
  59. } else {
  60. $query = qq|SELECT current_date|;
  61. ($form->{startdate}) = $dbh->selectrow_array($query);
  62. }
  63. # get managers
  64. $query = qq|
  65. SELECT id, name
  66. FROM employee
  67. WHERE sales = '1'
  68. AND role = 'manager'
  69. $notid
  70. ORDER BY 2|;
  71. $sth = $dbh->prepare($query);
  72. $sth->execute || $form->dberror($query);
  73. while ($ref = $sth->fetchrow_hashref(NAME_lc)) {
  74. push @{ $form->{all_manager} }, $ref;
  75. }
  76. $sth->finish;
  77. $dbh->commit;
  78. }
  79. sub save_employee {
  80. my ($self, $myconfig, $form) = @_;
  81. my $dbh = $form->{dbh};
  82. my $query;
  83. my $sth;
  84. if (! $form->{id}) {
  85. my $uid = localtime;
  86. $uid .= "$$";
  87. $query = qq|INSERT INTO employee (name) VALUES ('$uid')|;
  88. $dbh->do($query) || $form->dberror($query);
  89. $query = qq|SELECT id FROM employee WHERE name = '$uid'|;
  90. $sth = $dbh->prepare($query);
  91. $sth->execute || $form->dberror($query);
  92. ($form->{id}) = $sth->fetchrow_array;
  93. $sth->finish;
  94. }
  95. my ($null, $managerid) = split /--/, $form->{manager};
  96. $managerid *= 1;
  97. $form->{sales} *= 1;
  98. $form->{employeenumber} = $form->update_defaults(
  99. $myconfig, "employeenumber", $dbh)
  100. if ! $form->{employeenumber};
  101. $query = qq|
  102. UPDATE employee
  103. SET employeenumber = ?,
  104. name = ?,
  105. address1 = ?,
  106. address2 = ?,
  107. city = ?,
  108. state = ?,
  109. zipcode = ?,
  110. country = ?,
  111. workphone = ?,
  112. homephone = ?,
  113. startdate = ?,
  114. enddate = ?,
  115. notes = ?,
  116. role = ?,
  117. sales = ?,
  118. email = ?,
  119. ssn = ?,
  120. dob = ?,
  121. iban = ?,
  122. bic = ?,
  123. managerid = ?
  124. WHERE id = ?|;
  125. $sth = $dbh->prepare($query);
  126. $form->db_prepare_vars('startdate', 'enddate');
  127. $sth->execute(
  128. $form->{employeenumber}, $form->{name}, $form->{address1},
  129. $form->{address2}, $form->{city}, $form->{state},
  130. $form->{zipcode}, $form->{country}, $form->{workphone},
  131. $form->{homephone}, $form->{startdate}, $form->{enddate},
  132. $form->{notes}, $form->{role}, $form->{sales}, $form->{email},
  133. $form->{ssn}, $form->{dob}, $form->{iban}, $form->{bic},
  134. $managerid, $form->{id}
  135. ) || $form->dberror($query);
  136. $dbh->commit;
  137. }
  138. sub delete_employee {
  139. my ($self, $myconfig, $form) = @_;
  140. # connect to database
  141. my $dbh = $form->dbconnect_noauto($myconfig);
  142. # delete employee
  143. my $query = qq|
  144. DELETE FROM employee
  145. WHERE id = |.$dbh->quote($form->{id});
  146. $dbh->do($query) || $form->dberror($query);
  147. $dbh->commit;
  148. }
  149. sub employees {
  150. my ($self, $myconfig, $form) = @_;
  151. # connect to database
  152. my $dbh = $form->{dbh};
  153. my $where = "1 = 1";
  154. $form->{sort} = ($form->{sort}) ? $form->{sort} : "name";
  155. my @a = qw(name);
  156. my $sortorder = $form->sort_order(\@a);
  157. my $var;
  158. if ($form->{startdatefrom}) {
  159. $where .= " AND e.startdate >= ".
  160. $dbh->quote($form->{startdatefrom});
  161. }
  162. if ($form->{startdateto}) {
  163. $where .= " AND e.startddate <= ".
  164. $dbh->quote($form->{startdateto});
  165. }
  166. if ($form->{name} ne "") {
  167. $var = $dbh->quote($form->like(lc $form->{name}));
  168. $where .= " AND lower(e.name) LIKE $var";
  169. }
  170. if ($form->{notes} ne "") {
  171. $var = $dbh->quote($form->like(lc $form->{notes}));
  172. $where .= " AND lower(e.notes) LIKE $var";
  173. }
  174. if ($form->{sales} eq 'Y') {
  175. $where .= " AND e.sales = '1'";
  176. }
  177. if ($form->{status} eq 'orphaned') {
  178. $where .= qq| AND e.login IS NULL|;
  179. }
  180. if ($form->{status} eq 'active') {
  181. $where .= qq| AND e.enddate IS NULL|;
  182. }
  183. if ($form->{status} eq 'inactive') {
  184. $where .= qq| AND e.enddate <= current_date|;
  185. }
  186. my $query = qq|
  187. SELECT e.*, m.name AS manager
  188. FROM employee e
  189. LEFT JOIN employee m ON (m.id = e.managerid)
  190. WHERE $where
  191. ORDER BY $sortorder|;
  192. my $sth = $dbh->prepare($query);
  193. $sth->execute || $form->dberror($query);
  194. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  195. $ref->{address} = "";
  196. for (qw(address1 address2 city state zipcode country)) {
  197. $ref->{address} .= "$ref->{$_} ";
  198. }
  199. push @{ $form->{all_employee} }, $ref;
  200. }
  201. $sth->finish;
  202. $dbh->commit;
  203. }
  204. 1;