summaryrefslogtreecommitdiff
path: root/bin/bp.pl
blob: a366697ff4b1705b0be35cb58558777de6b037fe (plain)
  1. #=====================================================================
  2. # LedgerSMB Small Medium Business Accounting
  3. # http://www.ledgersmb.org/
  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. #
  22. # This program is free software; you can redistribute it and/or modify
  23. # it under the terms of the GNU General Public License as published by
  24. # the Free Software Foundation; either version 2 of the License, or
  25. # (at your option) any later version.
  26. #
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31. # You should have received a copy of the GNU General Public License
  32. # along with this program; if not, write to the Free Software
  33. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  34. #======================================================================
  35. #
  36. # Batch printing
  37. #
  38. #======================================================================
  39. use LedgerSMB::BP;
  40. use LedgerSMB::Template;
  41. 1;
  42. # end of main
  43. sub search {
  44. # $locale->text('Sales Invoices')
  45. # $locale->text('Packing Lists')
  46. # $locale->text('Pick Lists')
  47. # $locale->text('Sales Orders')
  48. # $locale->text('Work Orders')
  49. # $locale->text('Purchase Orders')
  50. # $locale->text('Bin Lists')
  51. # $locale->text('Quotations')
  52. # $locale->text('RFQs')
  53. # $locale->text('Time Cards')
  54. my %hiddens;
  55. # setup customer/vendor selection
  56. BP->get_vc( \%myconfig, \%$form );
  57. my %name;
  58. if ( ref $form->{"all_$form->{vc}"} eq 'ARRAY' ) {
  59. $name{type} = 'select';
  60. $name{data} = {name => $form->{vc}, options => [{text => '', value => ''}]};
  61. for ( @{ $form->{"all_$form->{vc}"} } ) {
  62. push @{$name{data}{options}}, {
  63. text => $_->{name},
  64. value => "$_->{name}--$_->{id}"
  65. };
  66. }
  67. } else {
  68. $name{type} = 'input';
  69. $name{data} = {name => $form->{vc}, size => 35};
  70. }
  71. # $locale->text('Customer')
  72. # $locale->text('Vendor')
  73. # $locale->text('Employee')
  74. my %label = (
  75. invoice => { title => 'Sales Invoices', name => 'Customer' },
  76. packing_list => { title => 'Packing Lists', name => 'Customer' },
  77. pick_list => { title => 'Pick Lists', name => 'Customer' },
  78. sales_order => { title => 'Sales Orders', name => 'Customer' },
  79. work_order => { title => 'Work Orders', name => 'Customer' },
  80. purchase_order => { title => 'Purchase Orders', name => 'Vendor' },
  81. bin_list => { title => 'Bin Lists', name => 'Vendor' },
  82. sales_quotation => { title => 'Quotations', name => 'Customer' },
  83. request_quotation => { title => 'RFQs', name => 'Vendor' },
  84. timecard => { title => 'Time Cards', name => 'Employee' },
  85. check => { title => 'Check', name => 'Vendor' },
  86. );
  87. $label{invoice}{invnumber} = {
  88. label => $locale->text('Invoice Number'),
  89. name => 'invnumber',
  90. };
  91. $label{invoice}{ordnumber} = {
  92. label => $locale->text('Order Number'),
  93. name => 'ordnumber',
  94. };
  95. $label{sales_quotation}{quonumber} = {
  96. label => $locale->text('Quotation Number'),
  97. name => 'quonumber',
  98. };
  99. $label{packing_list}{invnumber} = $label{invoice}{invnumber};
  100. $label{packing_list}{ordnumber} = $label{invoice}{ordnumber};
  101. $label{pick_list}{invnumber} = $label{invoice}{invnumber};
  102. $label{pick_list}{ordnumber} = $label{invoice}{ordnumber};
  103. $label{sales_order}{ordnumber} = $label{invoice}{ordnumber};
  104. $label{work_order}{ordnumber} = $label{invoice}{ordnumber};
  105. $label{purchase_order}{ordnumber} = $label{invoice}{ordnumber};
  106. $label{bin_list}{ordnumber} = $label{invoice}{ordnumber};
  107. $label{request_quotation}{quonumber} = $label{sales_quotation}{quonumber};
  108. # do one call to text
  109. $form->{title} = $locale->text("Print $label{$form->{type}}{title}");
  110. # accounting years
  111. if ( @{ $form->{all_years} } ) {
  112. # accounting years
  113. $form->{selectaccountingyear} = {
  114. name => 'year',
  115. options => [{text => '', value => ''}]
  116. };
  117. for ( @{ $form->{all_years} } ) {
  118. push @{$form->{selectaccountingyear}{options}}, {
  119. text => $_,
  120. value => $_
  121. };
  122. }
  123. $form->{selectaccountingmonth} = {
  124. name => 'month',
  125. options => [{text => '', value => ''}]
  126. };
  127. for ( sort keys %{ $form->{all_month} } ) {
  128. push @{$form->{selectaccountingmonth}{options}}, {
  129. text => $locale->text($form->{all_month}{$_}),
  130. value => $_
  131. };
  132. }
  133. }
  134. $hiddens{vc} = $form->{vc};
  135. $hiddens{type} = $form->{type};
  136. $hiddens{title} = $form->{title};
  137. $hiddens{sort} = 'transdate';
  138. $hiddens{nextsub} = 'list_spool';
  139. $hiddens{path} = $form->{path};
  140. $hiddens{login} = $form->{login};
  141. $hiddens{sessionid} = $form->{sessionid};
  142. my @buttons = ({
  143. name => 'action',
  144. value => 'list_spool',
  145. text => $locale->text('Continue'),
  146. });
  147. my $template = LedgerSMB::Template->new_UI(
  148. user => \%myconfig,
  149. locale => $locale,
  150. template => 'bp-search',
  151. );
  152. $template->render({
  153. form => $form,
  154. user => \%myconfig,
  155. label => \%label,
  156. name => \%name,
  157. hiddens => \%hiddens,
  158. buttons => \@buttons,
  159. });
  160. }
  161. sub remove {
  162. my $selected = 0;
  163. my %hiddens;
  164. for $i ( 1 .. $form->{rowcount} ) {
  165. if ( $form->{"checked_$i"} ) {
  166. $selected = 1;
  167. last;
  168. }
  169. }
  170. $form->error( $locale->text('Nothing selected!') ) unless $selected;
  171. $form->{title} = $locale->text('Confirm!');
  172. for (qw(action header)) { delete $form->{$_} }
  173. foreach my $key ( keys %$form ) {
  174. $hiddens{$key} = $form->{$key};
  175. }
  176. my $query = $locale->text(
  177. 'Are you sure you want to remove the marked entries from the queue?');
  178. my @buttons = ({
  179. name => 'action',
  180. value => 'remove_from_queue',
  181. text => $locale->text('Yes'),
  182. });
  183. my $template = LedgerSMB::Template->new_UI(
  184. user => \%myconfig,
  185. locale => $locale,
  186. template => 'form-confirmation',
  187. );
  188. $template->render({
  189. form => $form,
  190. query => $query,
  191. hiddens => \%hiddens,
  192. buttons => \@buttons,
  193. });
  194. }
  195. sub remove_from_queue {
  196. $form->info( $locale->text('Removing marked entries from queue ...') );
  197. $form->{callback} .= "&header=1" if $form->{callback};
  198. if ( BP->delete_spool( \%myconfig, \%$form, ${LedgerSMB::Sysconfig::spool} )
  199. )
  200. {
  201. $form->redirect( $locale->text('Removed spoolfiles!') );
  202. }
  203. else {
  204. $form->error( $locale->text('Cannot remove files!') );
  205. }
  206. }
  207. sub print {
  208. if ( $form->{callback} ) {
  209. for ( 1 .. $form->{rowcount} ) {
  210. $form->{callback} .= "&checked_$_=1" if $form->{"checked_$_"};
  211. }
  212. $form->{callback} .= "&header=1";
  213. }
  214. for $i ( 1 .. $form->{rowcount} ) {
  215. if ( $form->{"checked_$i"} ) {
  216. $form->{OUT} = "${LedgerSMB::Sysconfig::printer}{$form->{media}}";
  217. $form->{printmode} = '|-';
  218. $form->info( $locale->text('Printing') . " ..." );
  219. if (
  220. BP->print_spool(
  221. \%myconfig, \%$form, ${LedgerSMB::Sysconfig::spool}
  222. )
  223. )
  224. {
  225. print $locale->text('done');
  226. $form->redirect( $locale->text('Marked entries printed!') );
  227. }
  228. exit;
  229. }
  230. }
  231. $form->error('Nothing selected!');
  232. }
  233. sub list_spool {
  234. $form->{ $form->{vc} } = $form->unescape( $form->{ $form->{vc} } );
  235. ( $form->{ $form->{vc} }, $form->{"$form->{vc}_id"} ) =
  236. split( /--/, $form->{ $form->{vc} } );
  237. BP->get_spoolfiles( \%myconfig, \%$form );
  238. $title = $form->escape( $form->{title} );
  239. $href =
  240. "$form->{script}?action=list_spool&direction=$form->{direction}&oldsort=$form->{oldsort}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&vc=$form->{vc}&type=$form->{type}&title=$title";
  241. $form->sort_order();
  242. $title = $form->escape( $form->{title}, 1 );
  243. $callback =
  244. "$form->{script}?action=list_spool&direction=$form->{direction}&oldsort=$form->{oldsort}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&vc=$form->{vc}&type=$form->{type}&title=$title";
  245. if ( $form->{ $form->{vc} } ) {
  246. $callback .=
  247. "&$form->{vc}=" . $form->escape( $form->{ $form->{vc} }, 1 );
  248. $href .= "&$form->{vc}=" . $form->escape( $form->{ $form->{vc} } );
  249. $option =
  250. ( $form->{vc} eq 'customer' )
  251. ? $locale->text('Customer')
  252. : $locale->text('Vendor');
  253. $option .= " : $form->{$form->{vc}}";
  254. }
  255. if ( $form->{account} ) {
  256. $callback .= "&account=" . $form->escape( $form->{account}, 1 );
  257. $href .= "&account=" . $form->escape( $form->{account} );
  258. $option .= "\n<br>" if ($option);
  259. $option .= $locale->text('Account') . " : $form->{account}";
  260. }
  261. if ( $form->{invnumber} ) {
  262. $callback .= "&invnumber=" . $form->escape( $form->{invnumber}, 1 );
  263. $href .= "&invnumber=" . $form->escape( $form->{invnumber} );
  264. $option .= "\n<br>" if ($option);
  265. $option .= $locale->text('Invoice Number') . " : $form->{invnumber}";
  266. }
  267. if ( $form->{ordnumber} ) {
  268. $callback .= "&ordnumber=" . $form->escape( $form->{ordnumber}, 1 );
  269. $href .= "&ordnumber=" . $form->escape( $form->{ordnumber} );
  270. $option .= "\n<br>" if ($option);
  271. $option .= $locale->text('Order Number') . " : $form->{ordnumber}";
  272. }
  273. if ( $form->{quonumber} ) {
  274. $callback .= "&quonumber=" . $form->escape( $form->{quonumber}, 1 );
  275. $href .= "&quonumber=" . $form->escape( $form->{quonumber} );
  276. $option .= "\n<br>" if ($option);
  277. $option .= $locale->text('Quotation Number') . " : $form->{quonumber}";
  278. }
  279. if ( $form->{transdatefrom} ) {
  280. $callback .= "&transdatefrom=$form->{transdatefrom}";
  281. $href .= "&transdatefrom=$form->{transdatefrom}";
  282. $option .= "\n<br>" if ($option);
  283. $option .=
  284. $locale->text('From') . "&nbsp;"
  285. . $locale->date( \%myconfig, $form->{transdatefrom}, 1 );
  286. }
  287. if ( $form->{transdateto} ) {
  288. $callback .= "&transdateto=$form->{transdateto}";
  289. $href .= "&transdateto=$form->{transdateto}";
  290. $option .= "\n<br>" if ($option);
  291. $option .=
  292. $locale->text('To') . "&nbsp;"
  293. . $locale->date( \%myconfig, $form->{transdateto}, 1 );
  294. }
  295. $name = ucfirst $form->{vc};
  296. @columns = qw(transdate);
  297. if ( $form->{type} =~ /(invoice)/ ) {
  298. push @columns, "invnumber";
  299. }
  300. if ( $form->{type} =~ /(packing|pick)_list/ ) {
  301. push @columns, "invnumber";
  302. }
  303. if ( $form->{type} =~ /_(order|list)$/ ) {
  304. push @columns, "ordnumber";
  305. }
  306. if ( $form->{type} =~ /_quotation$/ ) {
  307. push @columns, "quonumber";
  308. }
  309. if ( $form->{type} eq 'timecard' ) {
  310. push @columns, "id";
  311. }
  312. push @columns, ( name, spoolfile );
  313. @column_index = $form->sort_columns(@columns);
  314. unshift @column_index, "checked";
  315. $column_header{checked} = "<th class=listheading>&nbsp;</th>";
  316. $column_header{transdate} =
  317. "<th><a class=listheading href=$href&sort=transdate>"
  318. . $locale->text('Date')
  319. . "</a></th>";
  320. $column_header{invnumber} =
  321. "<th><a class=listheading href=$href&sort=invnumber>"
  322. . $locale->text('Invoice')
  323. . "</a></th>";
  324. $column_header{ordnumber} =
  325. "<th><a class=listheading href=$href&sort=ordnumber>"
  326. . $locale->text('Order')
  327. . "</a></th>";
  328. $column_header{quonumber} =
  329. "<th><a class=listheading href=$href&sort=quonumber>"
  330. . $locale->text('Quotation')
  331. . "</a></th>";
  332. $column_header{name} =
  333. "<th><a class=listheading href=$href&sort=name>"
  334. . $locale->text($name)
  335. . "</a></th>";
  336. $column_header{id} =
  337. "<th><a class=listheading href=$href&sort=id>"
  338. . $locale->text('ID')
  339. . "</a></th>";
  340. $column_header{spoolfile} =
  341. "<th class=listheading>" . $locale->text('Spoolfile') . "</th>";
  342. $form->header;
  343. print qq|
  344. <body>
  345. <form method=post action=$form->{script}>
  346. <table width=100%>
  347. <tr>
  348. <th class=listtop>$form->{title}</th>
  349. </tr>
  350. <tr height="5"></tr>
  351. <tr>
  352. <td>$option</td>
  353. </tr>
  354. <tr>
  355. <td>
  356. <table width=100%>
  357. <tr class=listheading>
  358. |;
  359. for (@column_index) { print "\n$column_header{$_}" }
  360. print qq|
  361. </tr>
  362. |;
  363. # add sort and escape callback, this one we use for the add sub
  364. $form->{callback} = $callback .= "&sort=$form->{sort}";
  365. # escape callback for href
  366. $callback = $form->escape($callback);
  367. $i = 0;
  368. foreach $ref ( @{ $form->{SPOOL} } ) {
  369. $i++;
  370. $form->{"checked_$i"} = "checked" if $form->{"checked_$i"};
  371. # this is for audittrail
  372. $form->{module} = $ref->{module};
  373. if ( $ref->{invoice} ) {
  374. $ref->{module} = ( $ref->{module} eq 'ar' ) ? "is" : "ir";
  375. }
  376. $module = "$ref->{module}.pl";
  377. $column_data{transdate} = "<td>$ref->{transdate}&nbsp;</td>";
  378. if ( ${LedgerSMB::Sysconfig::spool} eq $ref->{spoolfile} ) {
  379. $column_data{checked} = qq|<td></td>|;
  380. }
  381. else {
  382. $column_data{checked} =
  383. qq|<td><input name=checked_$i type=checkbox class=checkbox $form->{"checked_$i"} $form->{"checked_$i"}></td>|;
  384. }
  385. for (qw(id invnumber ordnumber quonumber)) {
  386. $column_data{$_} = qq|<td>$ref->{$_}</td>|;
  387. }
  388. if ( $ref->{module} eq 'oe' ) {
  389. $column_data{invnumber} = qq|<td>&nbsp</td>|;
  390. $column_data{ordnumber} =
  391. qq|<td><a href=$module?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&callback=$callback>$ref->{ordnumber}</a></td>
  392. <input type=hidden name="reference_$i" value="$ref->{ordnumber}">|;
  393. $column_data{quonumber} =
  394. qq|<td><a href=$module?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&callback=$callback>$ref->{quonumber}</a></td>
  395. <input type=hidden name="reference_$i" value="$ref->{quonumber}">|;
  396. }
  397. elsif ( $ref->{module} eq 'jc' ) {
  398. $column_data{id} =
  399. qq|<td><a href=$module?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&callback=$callback>$ref->{id}</a></td>
  400. <input type=hidden name="reference_$i" value="$ref->{id}">|;
  401. }
  402. else {
  403. $column_data{invnumber} =
  404. qq|<td><a href=$module?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&callback=$callback>$ref->{invnumber}</a></td>
  405. <input type=hidden name="reference_$i" value="$ref->{invnumber}">|;
  406. }
  407. $column_data{name} = "<td>$ref->{name}</td>";
  408. $column_data{spoolfile} =
  409. qq|<td><a href=${LedgerSMB::Sysconfig::spool}/$ref->{spoolfile}>$ref->{spoolfile}</a></td>
  410. |;
  411. ${LedgerSMB::Sysconfig::spool} = $ref->{spoolfile};
  412. $j++;
  413. $j %= 2;
  414. print "
  415. <tr class=listrow$j>
  416. ";
  417. for (@column_index) { print "\n$column_data{$_}" }
  418. print qq|
  419. <input type=hidden name="id_$i" value=$ref->{id}>
  420. <input type=hidden name="spoolfile_$i" value=$ref->{spoolfile}>
  421. </tr>
  422. |;
  423. }
  424. print qq|
  425. <input type=hidden name=rowcount value=$i>
  426. </table>
  427. </td>
  428. </tr>
  429. <tr>
  430. <td><hr size=3 noshade></td>
  431. </tr>
  432. </table>
  433. <br>
  434. |;
  435. $form->hide_form(
  436. qw(callback title vc type sort module account path login sessionid));
  437. if ( %{LedgerSMB::Sysconfig::printer} && ${LedgerSMB::Sysconfig::latex} ) {
  438. foreach $key ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  439. print qq|
  440. <input name=media type=radio class=radio value="$key" |;
  441. print qq|checked| if $key eq $myconfig{printer};
  442. print qq|>$key|;
  443. }
  444. print qq|<p>\n|;
  445. # type=submit $locale->text('Select all')
  446. # type=submit $locale->text('Print')
  447. # type=submit $locale->text('Remove')
  448. %button = (
  449. 'select_all' =>
  450. { ndx => 2, key => 'A', value => $locale->text('Select all') },
  451. 'print' =>
  452. { ndx => 3, key => 'P', value => $locale->text('Print') },
  453. 'remove' =>
  454. { ndx => 4, key => 'R', value => $locale->text('Remove') },
  455. );
  456. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  457. {
  458. $form->print_button( \%button, $_ );
  459. }
  460. }
  461. if ( $form->{lynx} ) {
  462. require "bin/menu.pl";
  463. &menubar;
  464. }
  465. print qq|
  466. </form>
  467. </body>
  468. </html>
  469. |;
  470. }
  471. sub select_all {
  472. for ( 1 .. $form->{rowcount} ) { $form->{"checked_$_"} = 1 }
  473. &list_spool;
  474. }
  475. sub continue { &{ $form->{nextsub} } }