/doc/plugins/htmlbalance/

/>
summaryrefslogtreecommitdiff
path: root/LedgerSMB/PE.pm
blob: 604e62e7677465406a1769c107429fa26106330a (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. # Project module
  31. # also used for partsgroups
  32. #
  33. #======================================================================
  34. package PE;
  35. sub projects {
  36. my ($self, $myconfig, $form) = @_;
  37. # connect to database
  38. my $dbh = $form->dbconnect($myconfig);
  39. $form->{sort} = "projectnumber" unless $form->{sort};
  40. my @a = ($form->{sort});
  41. my %ordinal = ( projectnumber => 2,
  42. description => 3,
  43. startdate => 4,
  44. enddate => 5,
  45. );
  46. my $sortorder = $form->sort_order(\@a, \%ordinal);
  47. my $query;
  48. my $where = "WHERE 1=1";
  49. $query = qq|SELECT pr.*, c.name
  50. FROM project pr
  51. LEFT JOIN customer c ON (c.id = pr.customer_id)|;
  52. if ($form->{type} eq 'job') {
  53. $where .= qq| AND pr.id NOT IN (SELECT DISTINCT id
  54. FROM parts
  55. WHERE project_id > 0)|;
  56. }
  57. my $var;
  58. if ($form->{projectnumber} ne "") {
  59. $var = $form->like(lc $form->{projectnumber});
  60. $where .= " AND lower(pr.projectnumber) LIKE '$var'";
  61. }
  62. if ($form->{description} ne "") {
  63. $var = $form->like(lc $form->{description});
  64. $where .= " AND lower(pr.description) LIKE '$var'";
  65. }
  66. ($form->{startdatefrom}, $form->{startdateto}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  67. if ($form->{startdatefrom}) {
  68. $where .= " AND (pr.startdate IS NULL OR pr.startdate >= '$form->{startdatefrom}')";
  69. }
  70. if ($form->{startdateto}) {
  71. $where .= " AND (pr.startdate IS NULL OR pr.startdate <= '$form->{startdateto}')";
  72. }
  73. if ($form->{status} eq 'orphaned') {
  74. $where .= qq| AND pr.id NOT IN (SELECT DISTINCT project_id
  75. FROM acc_trans
  76. WHERE project_id > 0
  77. UNION
  78. SELECT DISTINCT project_id
  79. FROM invoice
  80. WHERE project_id > 0
  81. UNION
  82. SELECT DISTINCT project_id
  83. FROM orderitems
  84. WHERE project_id > 0
  85. UNION
  86. SELECT DISTINCT project_id
  87. FROM jcitems
  88. WHERE project_id > 0)
  89. |;
  90. }
  91. if ($form->{status} eq 'active') {
  92. $where .= qq| AND (pr.enddate IS NULL OR pr.enddate >= current_date)|;
  93. }
  94. if ($form->{status} eq 'inactive') {
  95. $where .= qq| AND pr.enddate <= current_date|;
  96. }
  97. $query .= qq|
  98. $where
  99. ORDER BY $sortorder|;
  100. $sth = $dbh->prepare($query);
  101. $sth->execute || $form->dberror($query);
  102. my $i = 0;
  103. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  104. push @{ $form->{all_project} }, $ref;
  105. $i++;
  106. }
  107. $sth->finish;
  108. $dbh->disconnect;
  109. $i;
  110. }
  111. sub get_project {
  112. my ($self, $myconfig, $form) = @_;
  113. # connect to database
  114. my $dbh = $form->dbconnect($myconfig);
  115. my $query;
  116. my $sth;
  117. my $ref;
  118. my $where;
  119. if ($form->{id}) {
  120. $where = "WHERE pr.id = $form->{id}" if $form->{id};
  121. $query = qq|SELECT pr.*,
  122. c.name AS customer
  123. FROM project pr
  124. LEFT JOIN customer c ON (c.id = pr.customer_id)
  125. $where|;
  126. $sth = $dbh->prepare($query);
  127. $sth->execute || $form->dberror($query);
  128. $ref = $sth->fetchrow_hashref(NAME_lc);
  129. for (keys %$ref) { $form->{$_} = $ref->{$_} }
  130. $sth->finish;
  131. # check if it is orphaned
  132. $query = qq|SELECT count(*)
  133. FROM acc_trans
  134. WHERE project_id = $form->{id}
  135. UNION
  136. SELECT count(*)
  137. FROM invoice
  138. WHERE project_id = $form->{id}
  139. UNION
  140. SELECT count(*)
  141. FROM orderitems
  142. WHERE project_id = $form->{id}
  143. UNION
  144. SELECT count(*)
  145. FROM jcitems
  146. WHERE project_id = $form->{id}
  147. |;
  148. $sth = $dbh->prepare($query);
  149. $sth->execute || $form->dberror($query);
  150. my $count;
  151. while (($count) = $sth->fetchrow_array) {
  152. $form->{orphaned} += $count;
  153. }
  154. $sth->finish;
  155. $form->{orphaned} = !$form->{orphaned};
  156. }
  157. PE->get_customer($myconfig, $form, $dbh);
  158. $form->run_custom_queries('project', 'SELECT');
  159. $dbh->disconnect;
  160. }
  161. sub save_project {
  162. my ($self, $myconfig, $form) = @_;
  163. # connect to database
  164. my $dbh = $form->dbconnect($myconfig);
  165. $form->{customer_id} ||= 'NULL';
  166. $form->{projectnumber} = $form->update_defaults($myconfig, "projectnumber", $dbh) unless $form->{projectnumber};
  167. if ($form->{id}) {
  168. $query = qq|UPDATE project SET
  169. projectnumber = |.$dbh->quote($form->{projectnumber}).qq|,
  170. description = |.$dbh->quote($form->{description}).qq|,
  171. startdate = |.$form->dbquote($form->{startdate}, SQL_DATE).qq|,
  172. enddate = |.$form->dbquote($form->{enddate}, SQL_DATE).qq|,
  173. customer_id = $form->{customer_id}
  174. WHERE id = $form->{id}|;
  175. } else {
  176. $query = qq|INSERT INTO project
  177. (projectnumber, description, startdate, enddate, customer_id)
  178. VALUES (|
  179. .$dbh->quote($form->{projectnumber}).qq|, |
  180. .$dbh->quote($form->{description}).qq|, |
  181. .$form->dbquote($form->{startdate}, SQL_DATE).qq|, |
  182. .$form->dbquote($form->{enddate}, SQL_DATE).qq|,
  183. $form->{customer_id}
  184. )|;
  185. }
  186. $dbh->do($query) || $form->dberror($query);
  187. $form->run_custom_queries('project', 'UPDATE');
  188. $dbh->disconnect;
  189. }
  190. sub list_stock {
  191. my ($self, $myconfig, $form) = @_;
  192. # connect to database
  193. my $dbh = $form->dbconnect($myconfig);
  194. my $var;
  195. my $where = "1 = 1";
  196. if ($form->{status} eq 'active') {
  197. $where = qq|(pr.enddate IS NULL
  198. OR pr.enddate >= current_date)
  199. AND pr.completed < pr.production|;
  200. }
  201. if ($form->{status} eq 'inactive') {
  202. $where = qq|pr.completed = pr.production|;
  203. }
  204. if ($form->{projectnumber}) {
  205. $var = $form->like(lc $form->{projectnumber});
  206. $where .= " AND lower(pr.projectnumber) LIKE '$var'";
  207. }
  208. if ($form->{description}) {
  209. $var = $form->like(lc $form->{description});
  210. $where .= " AND lower(pr.description) LIKE '$var'";
  211. }
  212. $form->{sort} = "projectnumber" unless $form->{sort};
  213. my @a = ($form->{sort});
  214. my %ordinal = ( projectnumber => 2,
  215. description => 3
  216. );
  217. my $sortorder = $form->sort_order(\@a, \%ordinal);
  218. my $query = qq|SELECT pr.*, p.partnumber
  219. FROM project pr
  220. JOIN parts p ON (p.id = pr.parts_id)
  221. WHERE $where
  222. ORDER BY $sortorder|;
  223. $sth = $dbh->prepare($query);
  224. $sth->execute || $form->dberror($query);
  225. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  226. push @{ $form->{all_project} }, $ref;
  227. }
  228. $sth->finish;
  229. $query = qq|SELECT current_date FROM defaults|;
  230. ($form->{stockingdate}) = $dbh->selectrow_array($query) if !$form->{stockingdate};
  231. $dbh->disconnect;
  232. }
  233. sub jobs {
  234. my ($self, $myconfig, $form) = @_;
  235. # connect to database
  236. my $dbh = $form->dbconnect($myconfig);
  237. $form->{sort} = "projectnumber" unless $form->{sort};
  238. my @a = ($form->{sort});
  239. my %ordinal = ( projectnumber => 2,
  240. description => 3,
  241. startdate => 4,
  242. );
  243. my $sortorder = $form->sort_order(\@a, \%ordinal);
  244. my $query = qq|SELECT pr.*, p.partnumber, p.onhand, c.name
  245. FROM project pr
  246. JOIN parts p ON (p.id = pr.parts_id)
  247. LEFT JOIN customer c ON (c.id = pr.customer_id)
  248. WHERE 1=1|;
  249. if ($form->{projectnumber} ne "") {
  250. $var = $form->like(lc $form->{projectnumber});
  251. $query .= " AND lower(pr.projectnumber) LIKE '$var'";
  252. }
  253. if ($form->{description} ne "") {
  254. $var = $form->like(lc $form->{description});
  255. $query .= " AND lower(pr.description) LIKE '$var'";
  256. }
  257. ($form->{startdatefrom}, $form->{startdateto}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  258. if ($form->{startdatefrom}) {
  259. $query .= " AND pr.startdate >= '$form->{startdatefrom}'";
  260. }
  261. if ($form->{startdateto}) {
  262. $query .= " AND pr.startdate <= '$form->{startdateto}'";
  263. }
  264. if ($form->{status} eq 'active') {
  265. $query .= qq| AND NOT pr.production = pr.completed|;
  266. }
  267. if ($form->{status} eq 'inactive') {
  268. $query .= qq| AND pr.production = pr.completed|;
  269. }
  270. if ($form->{status} eq 'orphaned') {
  271. $query .= qq| AND pr.completed = 0
  272. AND (pr.id NOT IN SELECT DISTINCT project_id
  273. FROM invoice
  274. WHERE project_id > 0)
  275. UNION
  276. SELECT DISTINCT project_id
  277. FROM orderitems
  278. WHERE project_id > 0
  279. SELECT DISTINCT project_id
  280. FROM jcitems
  281. WHERE project_id > 0
  282. )|;
  283. }
  284. $query .= qq|
  285. ORDER BY $sortorder|;
  286. $sth = $dbh->prepare($query);
  287. $sth->execute || $form->dberror($query);
  288. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  289. push @{ $form->{all_project} }, $ref;
  290. }
  291. $sth->finish;
  292. $dbh->disconnect;
  293. }
  294. sub get_job {
  295. my ($self, $myconfig, $form) = @_;
  296. # connect to database
  297. my $dbh = $form->dbconnect($myconfig);
  298. my $query;
  299. my $sth;
  300. my $ref;
  301. if ($form->{id}) {
  302. $query = qq|SELECT weightunit
  303. FROM defaults|;
  304. ($form->{weightunit}) = $dbh->selectrow_array($query);
  305. $query = qq|SELECT pr.*,
  306. p.partnumber, p.description AS partdescription, p.unit, p.listprice,
  307. p.sellprice, p.priceupdate, p.weight, p.notes, p.bin,
  308. p.partsgroup_id,
  309. ch.accno AS income_accno, ch.description AS income_description,
  310. pr.customer_id, c.name AS customer,
  311. pg.partsgroup
  312. FROM project pr
  313. LEFT JOIN parts p ON (p.id = pr.parts_id)
  314. LEFT JOIN chart ch ON (ch.id = p.income_accno_id)
  315. LEFT JOIN customer c ON (c.id = pr.customer_id)
  316. LEFT JOIN partsgroup pg ON (pg.id = p.partsgroup_id)
  317. WHERE pr.id = $form->{id}|;
  318. } else {
  319. $query = qq|SELECT weightunit, current_date AS startdate FROM defaults|;
  320. }
  321. $sth = $dbh->prepare($query);
  322. $sth->execute || $form->dberror($query);
  323. $ref = $sth->fetchrow_hashref(NAME_lc);
  324. for (keys %$ref) { $form->{$_} = $ref->{$_} }