/man/man8/

E.pm?h=master' type='application/atom+xml'/>
summaryrefslogtreecommitdiff
path: root/LedgerSMB/PE.pm
blob: d49c1cc340c73a87bee80a402220347587e7ea60 (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 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. my $dbh = $form->{dbh};
  38. $form->{sort} = "projectnumber" unless $form->{sort};
  39. my @a = ( $form->{sort} );
  40. my %ordinal = (
  41. 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|
  50. SELECT pr.*, c.name
  51. FROM project pr
  52. LEFT JOIN customer c ON (c.id = pr.customer_id)|;
  53. if ( $form->{type} eq 'job' ) {
  54. $where .= qq| AND pr.id NOT IN (SELECT DISTINCT id
  55. FROM parts
  56. WHERE project_id > 0)|;
  57. }
  58. my $var;
  59. if ( $form->{projectnumber} ne "" ) {
  60. $var = $dbh->quote( $form->like( lc $form->{projectnumber} ) );
  61. $where .= " AND lower(pr.projectnumber) LIKE $var";
  62. }
  63. if ( $form->{description} ne "" ) {
  64. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  65. $where .= " AND lower(pr.description) LIKE $var";
  66. }
  67. ( $form->{startdatefrom}, $form->{startdateto} ) =
  68. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  69. if $form->{year} && $form->{month};
  70. if ( $form->{startdatefrom} ) {
  71. $where .=
  72. " AND (pr.startdate IS NULL OR pr.startdate >= "
  73. . $dbh->quote( $form->{startdatefrom} ) . ")";
  74. }
  75. if ( $form->{startdateto} ) {
  76. $where .=
  77. " AND (pr.startdate IS NULL OR pr.startdate <= "
  78. . $dbh->quote( $form->{startdateto} ) . ")";
  79. }
  80. if ( $form->{status} eq 'orphaned' ) {
  81. $where .= qq| AND pr.id NOT IN (SELECT DISTINCT project_id
  82. FROM acc_trans
  83. WHERE project_id > 0
  84. UNION
  85. SELECT DISTINCT project_id
  86. FROM invoice
  87. WHERE project_id > 0
  88. UNION
  89. SELECT DISTINCT project_id
  90. FROM orderitems
  91. WHERE project_id > 0
  92. UNION
  93. SELECT DISTINCT project_id
  94. FROM jcitems
  95. WHERE project_id > 0)
  96. |;
  97. }
  98. if ( $form->{status} eq 'active' ) {
  99. $where .= qq|
  100. AND (pr.enddate IS NULL
  101. OR pr.enddate >= current_date)|;
  102. }
  103. if ( $form->{status} eq 'inactive' ) {
  104. $where .= qq| AND pr.enddate <= current_date|;
  105. }
  106. $query .= qq|
  107. $where
  108. ORDER BY $sortorder|;
  109. $sth = $dbh->prepare($query);
  110. $sth->execute || $form->dberror($query);
  111. my $i = 0;
  112. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  113. push @{ $form->{all_project} }, $ref;
  114. $i++;
  115. }
  116. $sth->finish;
  117. $dbh->commit;
  118. $i;
  119. }
  120. sub get_project {
  121. my ( $self, $myconfig, $form ) = @_;
  122. my $dbh = $form->{dbh};
  123. my $query;
  124. my $sth;
  125. my $ref;
  126. my $where;
  127. if ( $form->{id} ) {
  128. $query = qq|
  129. SELECT pr.*, c.name AS customer
  130. FROM project pr
  131. LEFT JOIN customer c ON (c.id = pr.customer_id)
  132. WHERE pr.id = ?|;
  133. $sth = $dbh->prepare($query);
  134. $sth->execute( $form->{id} ) || $form->dberror($query);
  135. $ref = $sth->fetchrow_hashref(NAME_lc);
  136. for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
  137. $sth->finish;
  138. # check if it is orphaned
  139. $query = qq|
  140. SELECT count(*)
  141. FROM acc_trans
  142. WHERE project_id = ?
  143. UNION
  144. SELECT count(*)
  145. FROM invoice
  146. WHERE project_id = ?
  147. UNION
  148. SELECT count(*)
  149. FROM orderitems
  150. WHERE project_id = ?
  151. UNION
  152. SELECT count(*)
  153. FROM jcitems
  154. WHERE project_id = ?|;
  155. $sth = $dbh->prepare($query);
  156. $sth->execute( $form->{id}, $form->{id}, $form->{id}, $form->{id} )
  157. || $form->dberror($query);
  158. my $count;
  159. while ( ($count) = $sth->fetchrow_array ) {
  160. $form->{orphaned} += $count;
  161. }
  162. $sth->finish;
  163. $form->{orphaned} = !$form->{orphaned};
  164. }
  165. PE->get_customer( $myconfig, $form, $dbh );
  166. $form->run_custom_queries( 'project', 'SELECT' );
  167. $dbh->commit;
  168. }
  169. sub save_project {
  170. my ( $self, $myconfig, $form ) = @_;
  171. my $dbh = $form->{dbh};
  172. $form->{customer_id} ||= undef;
  173. $form->{projectnumber} =
  174. $form->update_defaults( $myconfig, "projectnumber", $dbh )
  175. unless $form->{projectnumber};
  176. my $enddate;
  177. my $startdate;
  178. $enddate = $form->{enddate} if $form->{enddate};
  179. $startdate = $form->{startdate} if $form->{startdate};
  180. if ( $form->{id} ) {
  181. $query = qq|
  182. UPDATE project
  183. SET projectnumber = ?,
  184. description = ?,
  185. startdate = ?,
  186. enddate = ?,
  187. customer_id = ?
  188. WHERE id = | . $dbh->quote( $form->{id} );
  189. }
  190. else {
  191. $query = qq|
  192. INSERT INTO project (projectnumber, description,
  193. startdate, enddate, customer_id)
  194. VALUES (?, ?, ?, ?, ?)|;
  195. }
  196. $sth = $dbh->prepare($query);
  197. $sth->execute( $form->{projectnumber},
  198. $form->{description}, $startdate, $enddate, $form->{customer_id} )
  199. || $form->dberror($query);
  200. if (!$form->{id}){
  201. $query = "SELECT currval('id')";
  202. ($form->{id}) = $dbh->selectrow_array($query) || $form->dberror($query);
  203. }
  204. $form->run_custom_queries( 'project', 'UPDATE' );
  205. $dbh->commit;
  206. }
  207. sub list_stock {
  208. my ( $self, $myconfig, $form ) = @_;
  209. my $dbh = $form->{dbh};
  210. my $var;
  211. my $where = "1 = 1";
  212. if ( $form->{status} eq 'active' ) {
  213. $where = qq|
  214. (pr.enddate IS NULL OR pr.enddate >= current_date)
  215. AND pr.completed < pr.production|;
  216. }
  217. if ( $form->{status} eq 'inactive' ) {
  218. $where = qq|pr.completed = pr.production|;
  219. }
  220. if ( $form->{projectnumber} ) {
  221. $var = $dbh->quote( $form->like( lc $form->{projectnumber} ) );
  222. $where .= " AND lower(pr.projectnumber) LIKE $var";
  223. }
  224. if ( $form->{description} ) {
  225. $var = $dbh->quote( $form->like( lc $form->{description} ) );
  226. $where .= " AND lower(pr.description) LIKE $var";
  227. }
  228. $form->{sort} = "projectnumber" unless $form->{sort};
  229. my @a = ( $form->{sort} );
  230. my %ordinal = ( projectnumber => 2, description => 3 );
  231. my $sortorder = $form->sort_order( \@a, \%ordinal );
  232. my $query = qq|
  233. SELECT pr.*, p.partnumber
  234. FROM project pr
  235. JOIN parts p ON (p.id = pr.parts_id)
  236. WHERE $where
  237. ORDER BY $sortorder|;
  238. $sth = $dbh->prepare($query);
  239. $sth->execute || $form->dberror($query);
  240. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  241. push @{ $form->{all_project} }, $ref;
  242. }