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