summaryrefslogtreecommitdiff
path: root/LedgerSMB/BP.pm
blob: ae75d7ff7f809a982a08bc3ff347d1a492073656 (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. # This file has undergone whitespace cleanup.
  24. #
  25. #======================================================================
  26. #
  27. # Batch printing module backend routines
  28. #
  29. #======================================================================
  30. package BP;
  31. use LedgerSMB::Sysconfig;
  32. sub get_vc {
  33. my ( $self, $myconfig, $form ) = @_;
  34. # connect to database
  35. my $dbh = $form->{dbh};
  36. my %arap = (
  37. invoice => ['ar'],
  38. packing_list => [ 'oe', 'ar' ],
  39. sales_order => ['oe'],
  40. work_order => ['oe'],
  41. pick_list => [ 'oe', 'ar' ],
  42. purchase_order => ['oe'],
  43. bin_list => ['oe'],
  44. sales_quotation => ['oe'],
  45. request_quotation => ['oe'],
  46. timecard => ['jcitems'],
  47. check => ['ap'],
  48. );
  49. my $query = "";
  50. my $sth;
  51. my $n;
  52. my $count;
  53. my $item;
  54. my $sth;
  55. $item = $form->{dbh}->quote($item);
  56. foreach $item ( @{ $arap{ $form->{type} } } ) {
  57. $query = qq|
  58. SELECT count(*)
  59. FROM (SELECT DISTINCT vc.id
  60. FROM $form->{vc} vc, $item a, status s
  61. WHERE a.entity_id = vc.entity_id
  62. AND s.trans_id = a.id
  63. AND s.formname = ?
  64. AND s.spoolfile IS NOT NULL) AS total|;
  65. $sth = $dbh->prepare($query);
  66. $sth->execute( $form->{type} );
  67. ($n) = $sth->fetchrow_array;
  68. $count += $n;
  69. }
  70. # build selection list
  71. my $union = "";
  72. $query = "";
  73. my @queryargs = ();
  74. if ( $count < $myconfig->{vclimit} ) {
  75. foreach $item ( @{ $arap{ $form->{type} } } ) {
  76. $query .= qq|
  77. $union
  78. SELECT DISTINCT vc.id, vc.name
  79. FROM $item a
  80. JOIN $form->{vc} vc
  81. USING (entity_id)
  82. JOIN status s ON (s.trans_id = a.id)
  83. WHERE s.formname = ?
  84. AND s.spoolfile IS NOT NULL|;
  85. $union = "UNION";
  86. push @queryargs, $form->{type};
  87. }
  88. $sth = $dbh->prepare($query);
  89. $sth->execute(@queryargs) || $form->dberror($query);
  90. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  91. push @{ $form->{"all_$form->{vc}"} }, $ref;
  92. }
  93. $sth->finish;
  94. }
  95. $dbh->{commit};
  96. $form->all_years( $myconfig, $dbh );
  97. }
  98. sub get_spoolfiles {
  99. my ( $self, $myconfig, $form ) = @_;
  100. # connect to database
  101. my $dbh = $form->{dbh};
  102. my $query;
  103. my $invnumber = "invnumber";
  104. my $item;
  105. my %arap = (
  106. invoice => ['ar'],
  107. packing_list => [ 'oe', 'ar' ],
  108. sales_order => ['oe'],
  109. work_order => ['oe'],
  110. pick_list => [ 'oe', 'ar' ],
  111. purchase_order => ['oe'],
  112. bin_list => ['oe'],
  113. sales_quotation => ['oe'],
  114. request_quotation => ['oe'],
  115. timecard => ['jc'],
  116. check => ['ap'],
  117. );
  118. ( $form->{transdatefrom}, $form->{transdateto} ) =
  119. $form->from_to( $form->{year}, $form->{month}, $form->{interval} )
  120. if $form->{year} && $form->{month};
  121. my @queryargs;
  122. if ( $form->{type} eq 'timecard' ) {
  123. my $dateformat = $myconfig->{dateformat};
  124. $dateformat =~ s/yy/yyyy/;
  125. $dateformat =~ s/yyyyyy/yyyy/;
  126. $invnumber = 'id';
  127. $query = qq|
  128. SELECT j.id, e.name, j.id AS invnumber,
  129. to_char(j.checkedin, ?) AS transdate,
  130. '' AS ordnumber, '' AS quonumber, '0' AS invoice,
  131. '$arap{$form->{type}}[0]' AS module, s.spoolfile
  132. FROM jcitems j
  133. JOIN employees e ON (e.id = j.employee_id)
  134. JOIN status s ON (s.trans_id = j.id)
  135. WHERE s.formname = ?
  136. AND s.spoolfile IS NOT NULL|;
  137. @queryargs = ( $dateformat, $form->{type} );
  138. if ( $form->{"$form->{vc}_id"} ) {
  139. $query .= qq| AND j.$form->{vc}_id = ?|;
  140. push( @queryargs, $form->{"$form->{vc}_id"} );
  141. }
  142. else {
  143. if ( $form->{ $form->{vc} } ) {
  144. $item = $form->like( lc $form->{ $form->{vc} } );
  145. $query .= " AND lower(e.name) LIKE ?";
  146. push( @queryargs, $item );
  147. }
  148. }
  149. if ( $form->{transdatefrom} ) {
  150. $query .= " AND j.checkedin >= ?";
  151. push( @queryargs, $form->{transdatefrom} );
  152. }
  153. if ( $form->{transdateto} ) {
  154. $query .= " AND j.checkedin <= ?";
  155. push( @queryargs, $form->{transdateto} );
  156. }
  157. }
  158. else {
  159. @queryargs = ();
  160. foreach $item ( @{ $arap{ $form->{type} } } ) {
  161. $invoice = "a.invoice";
  162. $invnumber = "invnumber";
  163. if ( $item eq 'oe' ) {
  164. $invnumber = "ordnumber";
  165. $invoice = "'0'";
  166. }
  167. $query .= qq|
  168. $union
  169. SELECT a.id, c.legal_name AS name, a.$invnumber AS invnumber, a.transdate,
  170. a.ordnumber, a.quonumber, $invoice AS invoice,
  171. '$item' AS module, s.spoolfile
  172. FROM $item a, $form->{vc} vc, status s, company c
  173. WHERE s.trans_id = a.id
  174. AND s.spoolfile IS NOT NULL
  175. AND s.formname = ?
  176. AND c.entity_id = vc.entity_id
  177. AND a.entity_id = vc.entity_id|;
  178. push( @queryargs, $form->{type} );
  179. if ( $form->{"$form->{vc}_id"} ) {
  180. $query .= qq| AND a.entity_id = $form->{"entity_id"}|;
  181. }
  182. else {
  183. if ( $form->{ $form->{vc} } ne "" ) {
  184. $item = $form->like( lc $form->{ $form->{vc} } );
  185. $query .= " AND lower(c.legal_name) LIKE ?";
  186. push @queryargs, $item;
  187. }
  188. }
  189. if ( $form->{invnumber} ne "" ) {
  190. $item = $form->like( lc $form->{invnumber} );
  191. $query .= " AND lower(a.invnumber) LIKE ?";
  192. push @queryargs, $item;
  193. }
  194. if ( $form->{ordnumber} ne "" ) {
  195. $item = $form->like( lc $form->{ordnumber} );
  196. $query .= " AND lower(a.ordnumber) LIKE ?";
  197. push @queryargs, $item;
  198. }
  199. if ( $form->{quonumber} ne "" ) {
  200. $item = $form->like( lc $form->{quonumber} );
  201. $query .= " AND lower(a.quonumber) LIKE ?";
  202. push @queryargs, $item;
  203. }
  204. if ( $form->{transdatefrom} ) {
  205. $query .= " AND a.transdate >= ?";
  206. push @queryargs, $form->{transdatefrom};
  207. }
  208. if ( $form->{transdateto} ) {
  209. $query .= " AND a.transdate <= ?";
  210. push @queryargs, $form->{transdateto};
  211. }
  212. $union = "UNION";
  213. }
  214. }
  215. my %ordinal = (
  216. 'name' => 2,
  217. 'invnumber' => 3,
  218. 'transdate' => 4,
  219. 'ordnumber' => 5,
  220. 'quonumber' => 6,
  221. );
  222. my @a = ();
  223. push @a, ( "transdate", "$invnumber", "name" );
  224. my $sortorder = $form->sort_order( \@a, \%ordinal );
  225. $query .= " ORDER by $sortorder";
  226. my $sth = $dbh->prepare($query);
  227. $sth->execute(@queryargs) || $form->dberror($query);
  228. while ( my $ref = $sth->fetchrow_hashref(NAME_lc) ) {
  229. push @{ $form->{SPOOL} }, $ref;
  230. }
  231. $sth->finish;
  232. $dbh->commit;
  233. }
  234. sub delete_spool {
  235. my ( $self, $myconfig, $form ) = @_;
  236. # connect to database, turn AutoCommit off
  237. my $dbh = $form->{dbh};
  238. my $query;
  239. my %audittrail;
  240. $query = qq|
  241. UPDATE status
  242. SET spoolfile = NULL
  243. WHERE spoolfile = ?|;
  244. my $sth = $dbh->prepare($query) || $form->dberror($query);
  245. foreach my $i ( 1 .. $form->{rowcount} ) {
  246. if ( $form->{"checked_$i"} ) {
  247. $sth->execute( $form->{"spoolfile_$i"} ) || $form->dberror($query);
  248. $sth->finish;
  249. %audittrail = (
  250. tablename => $form->{module},
  251. reference => $form->{"reference_$i"},
  252. formname => $form->{type},
  253. action => 'dequeued',
  254. id => $form->{"id_$i"}
  255. );
  256. $form->audittrail( $dbh, "", \%audittrail );
  257. }
  258. }
  259. # commit
  260. my $rc = $dbh->commit;
  261. if ($rc) {
  262. foreach my $i ( 1 .. $form->{rowcount} ) {
  263. $_ = qq|${LedgerSMB::Sysconfig::spool}/$form->{"spoolfile_$i"}|;
  264. if ( $form->{"checked_$i"} ) {
  265. unlink;
  266. }
  267. }
  268. }
  269. $rc;
  270. }
  271. sub print_spool {
  272. my ( $self, $myconfig, $form ) = @_;
  273. ##SC: XXX May need to be changed after hooking up printing to templates
  274. # connect to database
  275. my $dbh = $form->{dbh};
  276. my %audittrail;
  277. my $query = qq|UPDATE status
  278. SET printed = '1'
  279. WHERE spoolfile = ?|;
  280. my $sth = $dbh->prepare($query) || $form->dberror($query);
  281. foreach my $i ( 1 .. $form->{rowcount} ) {
  282. if ( $form->{"checked_$i"} ) {
  283. open( OUT, '>', $form->{OUT} ) or $form->error("$form->{OUT} : $!");
  284. binmode(OUT);
  285. $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
  286. # send file to printer
  287. open( IN, '<', $spoolfile ) or $form->error("$spoolfile : $!");
  288. binmode(IN);
  289. while (<IN>) {
  290. print OUT $_;
  291. }
  292. close(IN);
  293. close(OUT);
  294. $sth->execute( $form->{"spoolfile_$i"} ) || $form->dberror($query);
  295. $sth->finish;
  296. %audittrail = (
  297. tablename => $form->{module},
  298. reference => $form->{"reference_$i"},
  299. formname => $form->{type},
  300. action => 'printed',
  301. id => $form->{"id_$i"}
  302. );
  303. $form->audittrail( $dbh, "", \%audittrail );
  304. $dbh->commit;
  305. }
  306. }
  307. }
  308. 1;