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