summaryrefslogtreecommitdiff
path: root/LedgerSMB/BP.pm
blob: c1feadc2060cba6717b2d63194ad0cfceb201580 (plain)
  1. #=====================================================================
  2. # LedgerSMB
  3. # Small Medium Business Accounting software
  4. #
  5. # See COPYRIGHT file for copyright information
  6. #======================================================================
  7. #
  8. # This file has undergone whitespace cleanup.
  9. #
  10. #======================================================================
  11. #
  12. # Batch printing module backend routines
  13. #
  14. #======================================================================
  15. package BP;
  16. sub get_vc {
  17. my ($self, $myconfig, $form) = @_;
  18. # connect to database
  19. my $dbh = $form->dbconnect($myconfig);
  20. my %arap = ( invoice => ['ar'],
  21. packing_list => ['oe', 'ar'],
  22. sales_order => ['oe'],
  23. work_order => ['oe'],
  24. pick_list => ['oe', 'ar'],
  25. purchase_order => ['oe'],
  26. bin_list => ['oe'],
  27. sales_quotation => ['oe'],
  28. request_quotation => ['oe'],
  29. timecard => ['jcitems'],);
  30. my $query = "";
  31. my $sth;
  32. my $n;
  33. my $count;
  34. my $item;
  35. foreach $item (@{ $arap{$form->{type}} }) {
  36. $query = qq|SELECT count(*)
  37. FROM (SELECT DISTINCT vc.id
  38. FROM $form->{vc} vc, $item a, status s
  39. WHERE a.$form->{vc}_id = vc.id
  40. AND s.trans_id = a.id
  41. AND s.formname = '$form->{type}'
  42. AND s.spoolfile IS NOT NULL) AS total|;
  43. ($n) = $dbh->selectrow_array($query);
  44. $count += $n;
  45. }
  46. # build selection list
  47. my $union = "";
  48. $query = "";
  49. if ($count < $myconfig->{vclimit}) {
  50. foreach $item (@{ $arap{$form->{type}} }) {
  51. $query .= qq| $union
  52. SELECT DISTINCT vc.id, vc.name
  53. FROM $item a
  54. JOIN $form->{vc} vc ON (a.$form->{vc}_id = vc.id)
  55. JOIN status s ON (s.trans_id = a.id)
  56. WHERE s.formname = '$form->{type}'
  57. AND s.spoolfile IS NOT NULL|;
  58. $union = "UNION";
  59. }
  60. $sth = $dbh->prepare($query);
  61. $sth->execute || $form->dberror($query);
  62. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  63. push @{ $form->{"all_$form->{vc}"} }, $ref;
  64. }
  65. $sth->finish;
  66. }
  67. $form->all_years($myconfig, $dbh);
  68. $dbh->disconnect;
  69. }
  70. sub get_spoolfiles {
  71. my ($self, $myconfig, $form) = @_;
  72. # connect to database
  73. my $dbh = $form->dbconnect($myconfig);
  74. my $query;
  75. my $invnumber = "invnumber";
  76. my $item;
  77. my %arap = ( invoice => ['ar'],
  78. packing_list => ['oe', 'ar'],
  79. sales_order => ['oe'],
  80. work_order => ['oe'],
  81. pick_list => ['oe', 'ar'],
  82. purchase_order => ['oe'],
  83. bin_list => ['oe'],
  84. sales_quotation => ['oe'],
  85. request_quotation => ['oe'],
  86. timecard => ['jc'],);
  87. ($form->{transdatefrom}, $form->{transdateto}) = $form->from_to($form->{year}, $form->{month}, $form->{interval}) if $form->{year} && $form->{month};
  88. if ($form->{type} eq 'timecard') {
  89. my $dateformat = $myconfig->{dateformat};
  90. $dateformat =~ s/yy/yyyy/;
  91. $dateformat =~ s/yyyyyy/yyyy/;
  92. $invnumber = 'id';
  93. $query = qq|SELECT j.id, e.name, j.id AS invnumber,
  94. to_char(j.checkedin, '$dateformat') AS transdate,
  95. '' AS ordnumber, '' AS quonumber, '0' AS invoice,
  96. '$arap{$form->{type}}[0]' AS module, s.spoolfile
  97. FROM jcitems j
  98. JOIN employee e ON (e.id = j.employee_id)
  99. JOIN status s ON (s.trans_id = j.id)
  100. WHERE s.formname = '$form->{type}'
  101. AND s.spoolfile IS NOT NULL|;
  102. if ($form->{"$form->{vc}_id"}) {
  103. $query .= qq| AND j.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  104. } else {
  105. if ($form->{$form->{vc}}) {
  106. $item = $form->like(lc $form->{$form->{vc}});
  107. $query .= " AND lower(e.name) LIKE '$item'";
  108. }
  109. }
  110. $query .= " AND j.checkedin >= '$form->{transdatefrom}'" if $form->{transdatefrom};
  111. $query .= " AND j.checkedin <= '$form->{transdateto}'" if $form->{transdateto};
  112. } else {
  113. foreach $item (@{ $arap{$form->{type}} }) {
  114. $invoice = "a.invoice";
  115. $invnumber = "invnumber";
  116. if ($item eq 'oe') {
  117. $invnumber = "ordnumber";
  118. $invoice = "'0'";
  119. }
  120. $query .= qq| $union
  121. SELECT a.id, vc.name, a.$invnumber AS invnumber, a.transdate,
  122. a.ordnumber, a.quonumber, $invoice AS invoice,
  123. '$item' AS module, s.spoolfile
  124. FROM $item a, $form->{vc} vc, status s
  125. WHERE s.trans_id = a.id
  126. AND s.spoolfile IS NOT NULL
  127. AND s.formname = '$form->{type}'
  128. AND a.$form->{vc}_id = vc.id|;
  129. if ($form->{"$form->{vc}_id"}) {
  130. $query .= qq| AND a.$form->{vc}_id = $form->{"$form->{vc}_id"}|;
  131. } else {
  132. if ($form->{$form->{vc}} ne "") {
  133. $item = $form->like(lc $form->{$form->{vc}});
  134. $query .= " AND lower(vc.name) LIKE '$item'";
  135. }
  136. }
  137. if ($form->{invnumber} ne "") {
  138. $item = $form->like(lc $form->{invnumber});
  139. $query .= " AND lower(a.invnumber) LIKE '$item'";
  140. }
  141. if ($form->{ordnumber} ne "") {
  142. $item = $form->like(lc $form->{ordnumber});
  143. $query .= " AND lower(a.ordnumber) LIKE '$item'";
  144. }
  145. if ($form->{quonumber} ne "") {
  146. $item = $form->like(lc $form->{quonumber});
  147. $query .= " AND lower(a.quonumber) LIKE '$item'";
  148. }
  149. $query .= " AND a.transdate >= '$form->{transdatefrom}'" if $form->{transdatefrom};
  150. $query .= " AND a.transdate <= '$form->{transdateto}'" if $form->{transdateto};
  151. $union = "UNION";
  152. }
  153. }
  154. my %ordinal = ( 'name' => 2,
  155. 'invnumber' => 3,
  156. 'transdate' => 4,
  157. 'ordnumber' => 5,
  158. 'quonumber' => 6,);
  159. my @a = ();
  160. push @a, ("transdate", "$invnumber", "name");
  161. my $sortorder = $form->sort_order(\@a, \%ordinal);
  162. $query .= " ORDER by $sortorder";
  163. my $sth = $dbh->prepare($query);
  164. $sth->execute || $form->dberror($query);
  165. while (my $ref = $sth->fetchrow_hashref(NAME_lc)) {
  166. push @{ $form->{SPOOL} }, $ref;
  167. }
  168. $sth->finish;
  169. $dbh->disconnect;
  170. }
  171. sub delete_spool {
  172. my ($self, $myconfig, $form, $spool) = @_;
  173. # connect to database, turn AutoCommit off
  174. my $dbh = $form->dbconnect_noauto($myconfig);
  175. my $query;
  176. my %audittrail;
  177. $query = qq|UPDATE status
  178. SET spoolfile = NULL
  179. WHERE spoolfile = ?|;
  180. my $sth = $dbh->prepare($query) || $form->dberror($query);
  181. foreach my $i (1 .. $form->{rowcount}) {
  182. if ($form->{"checked_$i"}) {
  183. $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
  184. $sth->finish;
  185. %audittrail = ( tablename => $form->{module},
  186. reference => $form->{"reference_$i"},
  187. formname => $form->{type},
  188. action => 'dequeued',
  189. id => $form->{"id_$i"} );
  190. $form->audittrail($dbh, "", \%audittrail);
  191. }
  192. }
  193. # commit
  194. my $rc = $dbh->commit;
  195. $dbh->disconnect;
  196. if ($rc) {
  197. foreach my $i (1 .. $form->{rowcount}) {
  198. $_ = qq|$spool/$form->{"spoolfile_$i"}|;
  199. if ($form->{"checked_$i"}) {
  200. unlink;
  201. }
  202. }
  203. }
  204. $rc;
  205. }
  206. sub print_spool {
  207. my ($self, $myconfig, $form, $spool) = @_;
  208. # connect to database
  209. my $dbh = $form->dbconnect_noauto($myconfig);
  210. my %audittrail;
  211. my $query = qq|UPDATE status
  212. SET printed = '1'
  213. WHERE spoolfile = ?|;
  214. my $sth = $dbh->prepare($query) || $form->dberror($query);
  215. foreach my $i (1 .. $form->{rowcount}) {
  216. if ($form->{"checked_$i"}) {
  217. open(OUT, $form->{OUT}) or $form->error("$form->{OUT} : $!");
  218. binmode(OUT);
  219. $spoolfile = qq|$spool/$form->{"spoolfile_$i"}|;
  220. # send file to printer
  221. open(IN, $spoolfile) or $form->error("$spoolfile : $!");
  222. binmode(IN);
  223. while (<IN>) {
  224. print OUT $_;
  225. }
  226. close(IN);
  227. close(OUT);
  228. $sth->execute($form->{"spoolfile_$i"}) || $form->dberror($query);
  229. $sth->finish;
  230. %audittrail = ( tablename => $form->{module},
  231. reference => $form->{"reference_$i"},
  232. formname => $form->{type},
  233. action => 'printed',
  234. id => $form->{"id_$i"} );
  235. $form->audittrail($dbh, "", \%audittrail);
  236. $dbh->commit;
  237. }
  238. }
  239. $dbh->disconnect;
  240. }
  241. 1;