summaryrefslogtreecommitdiff
path: root/bin/bp.pl
blob: 7d4be653aafd3705efc83e72904d665f5c1cacd2 (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. ##SC: XXX adjust later once printing hooked up to templates
  217. $form->{OUT} = "${LedgerSMB::Sysconfig::printer}{$form->{media}}";
  218. $form->{printmode} = '|-';
  219. $form->info( $locale->text('Printing') . " ..." );
  220. if (
  221. BP->print_spool(
  222. \%myconfig, \%$form, ${LedgerSMB::Sysconfig::spool}
  223. )
  224. )
  225. {
  226. print $locale->text('done');
  227. $form->redirect( $locale->text('Marked entries printed!') );
  228. }
  229. exit;
  230. }
  231. }
  232. $form->error('Nothing selected!');
  233. }
  234. sub list_spool {
  235. my %hiddens;
  236. my @buttons;
  237. $form->{ $form->{vc} } = $form->unescape( $form->{ $form->{vc} } );
  238. ( $form->{ $form->{vc} }, $form->{"$form->{vc}_id"} ) =
  239. split( /--/, $form->{ $form->{vc} } );
  240. BP->get_spoolfiles( \%myconfig, \%$form );
  241. my $title = $form->escape( $form->{title} );
  242. my $href =
  243. "$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";
  244. $form->sort_order();
  245. $title = $form->escape( $form->{title}, 1 );
  246. my $callback =
  247. "$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";
  248. my @options;
  249. if ( $form->{ $form->{vc} } ) {
  250. $callback .=
  251. "&$form->{vc}=" . $form->escape( $form->{ $form->{vc} }, 1 );
  252. $href .= "&$form->{vc}=" . $form->escape( $form->{ $form->{vc} } );
  253. push @options,
  254. ( $form->{vc} eq 'customer' )
  255. ? $locale->text('Customer: [_1]', $form->{$form->{vc}})
  256. : $locale->text('Vendor: [_1]', $form->{$form->{vc}});
  257. }
  258. if ( $form->{account} ) {
  259. $callback .= "&account=" . $form->escape( $form->{account}, 1 );
  260. $href .= "&account=" . $form->escape( $form->{account} );
  261. push @options, $locale->text('Account: [_1]', $form->{account});
  262. }
  263. if ( $form->{invnumber} ) {
  264. $callback .= "&invnumber=" . $form->escape( $form->{invnumber}, 1 );
  265. $href .= "&invnumber=" . $form->escape( $form->{invnumber} );
  266. push @options, $locale->text('Invoice Number: [_1]', $form->{invnumber});
  267. }
  268. if ( $form->{ordnumber} ) {
  269. $callback .= "&ordnumber=" . $form->escape( $form->{ordnumber}, 1 );
  270. $href .= "&ordnumber=" . $form->escape( $form->{ordnumber} );
  271. push @options, $locale->text('Order Number: [_1]', $form->{ordnumber});
  272. }
  273. if ( $form->{quonumber} ) {
  274. $callback .= "&quonumber=" . $form->escape( $form->{quonumber}, 1 );
  275. $href .= "&quonumber=" . $form->escape( $form->{quonumber} );
  276. push @options, $locale->text('Quotation Number: [_1]', $form->{quonumber});
  277. }
  278. if ( $form->{transdatefrom} ) {
  279. $callback .= "&transdatefrom=$form->{transdatefrom}";
  280. $href .= "&transdatefrom=$form->{transdatefrom}";
  281. push @options, $locale->text('From [_1]',
  282. $locale->date( \%myconfig, $form->{transdatefrom}, 1 ));
  283. }
  284. if ( $form->{transdateto} ) {
  285. $callback .= "&transdateto=$form->{transdateto}";
  286. $href .= "&transdateto=$form->{transdateto}";
  287. push @options, $locale->text('To [_1]',
  288. $locale->date( \%myconfig, $form->{transdateto}, 1 ));
  289. }
  290. my $name = ucfirst $form->{vc};
  291. my @columns = qw(transdate);
  292. if ( $form->{type} =~ /(invoice)/ ) {
  293. push @columns, "invnumber";
  294. }
  295. if ( $form->{type} =~ /(packing|pick)_list/ ) {
  296. push @columns, "invnumber";
  297. }
  298. if ( $form->{type} =~ /_(order|list)$/ ) {
  299. push @columns, "ordnumber";
  300. }
  301. if ( $form->{type} =~ /_quotation$/ ) {
  302. push @columns, "quonumber";
  303. }
  304. if ( $form->{type} eq 'timecard' ) {
  305. push @columns, "id";
  306. }
  307. push @columns, ( name, spoolfile );
  308. my @column_index = $form->sort_columns(@columns);
  309. unshift @column_index, "checked";
  310. my %column_header;
  311. $column_header{checked} = ' ';
  312. $column_header{transdate} = {
  313. href => "$href&sort=transdate",
  314. text => $locale->text('Date'),
  315. };
  316. $column_header{invnumber} = {
  317. href => "$href&sort=invnumber",
  318. text => $locale->text('Invoice'),
  319. };
  320. $column_header{ordnumber} = {
  321. href => "$href&sort=ordnumber",
  322. text => $locale->text('Order'),
  323. };
  324. $column_header{quonumber} = {
  325. href => "$href&sort=quonumber",
  326. text => $locale->text('Quotation'),
  327. };
  328. $column_header{name} = {
  329. href => "$href&sort=name",
  330. text => $locale->text($name),
  331. };
  332. $column_header{id} = {
  333. href => "$href&sort=id",
  334. text => $locale->text('ID'),
  335. };
  336. $column_header{spoolfile} = $locale->text('Spoolfile');
  337. # add sort and escape callback, this one we use for the add sub
  338. $form->{callback} = $callback .= "&sort=$form->{sort}";
  339. # escape callback for href
  340. $callback = $form->escape($callback);
  341. my $i = 0;
  342. my @rows;
  343. foreach my $ref ( @{ $form->{SPOOL} } ) {
  344. my %column_data;
  345. $i++;
  346. $form->{"checked_$i"} = "checked" if $form->{"checked_$i"};
  347. # this is for audittrail
  348. $form->{module} = $ref->{module};
  349. if ( $ref->{invoice} ) {
  350. $ref->{module} = ( $ref->{module} eq 'ar' ) ? "is" : "ir";
  351. }
  352. $module = "$ref->{module}.pl";
  353. $column_data{transdate} = $ref->{transdate};
  354. if ( ${LedgerSMB::Sysconfig::spool} eq $ref->{spoolfile} ) {
  355. $column_data{checked} = '';
  356. }
  357. else {
  358. $column_data{checked} = {input => {
  359. name => "checked_$i",
  360. type => 'checkbox',
  361. $form->{"checked_$i"} => $form->{"checked_$i"},
  362. }};
  363. }
  364. for (qw(id invnumber ordnumber quonumber)) {
  365. $column_data{$_} = $ref->{$_};
  366. }
  367. if ( $ref->{module} eq 'oe' ) {
  368. $hiddens{"reference_$i"} = $ref->{ordnumber};
  369. $column_data{invnumber} = ' ';
  370. $column_data{ordnumber} = {
  371. href => "$module?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&callback=$callback",
  372. text => $ref->{ordnumber},
  373. };
  374. $hiddens{"reference_$i"} = $ref->{quonumber} unless $ref->{ordnumber};
  375. $column_data{quonumber} = {
  376. href => "$module?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&callback=$callback",
  377. text => $ref->{quonumber},
  378. };
  379. }
  380. elsif ( $ref->{module} eq 'jc' ) {
  381. $hiddens{"reference_$i"} = $ref->{id};
  382. $column_data{id} = {
  383. href => "$module?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&callback=$callback",
  384. text => $ref->{id},
  385. };
  386. }
  387. else {
  388. $hiddens{"reference_$i"} = $ref->{invnumber};
  389. $column_data{invnumber} = {
  390. href => "$module?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&type=$form->{type}&callback=$callback",
  391. text => $ref->{invnumber},
  392. };
  393. }
  394. $column_data{name} = $ref->{name};
  395. $column_data{spoolfile} = {
  396. href => "${LedgerSMB::Sysconfig::spool}/$ref->{spoolfile}",
  397. text => $ref->{spoolfile},
  398. };
  399. ${LedgerSMB::Sysconfig::spool} = $ref->{spoolfile};
  400. $j++;
  401. $j %= 2;
  402. $column_data{i} = $j;
  403. $hiddens{"id_$i"} = $ref->{id};
  404. $hiddens{"spoolfile_$i"} = $ref->{spoolfile};
  405. push @rows, \%column_data;
  406. }
  407. $hiddens{rowcount} = $i;
  408. $hiddens{$_} = $form->{$_} foreach
  409. qw(callback title vc type sort module account path login sessionid);
  410. my @printers;
  411. if ( %{LedgerSMB::Sysconfig::printer} && ${LedgerSMB::Sysconfig::latex} ) {
  412. foreach my $key ( sort keys %{LedgerSMB::Sysconfig::printer} ) {
  413. push @printers, {
  414. type => 'radio',
  415. name => 'media',
  416. value => $key,
  417. label => $key,
  418. };
  419. $printers[$#printers]{checked} = 'checked' if $key eq $myconfig{printer};
  420. }
  421. # type=submit $locale->text('Select all')
  422. # type=submit $locale->text('Print')
  423. # type=submit $locale->text('Remove')
  424. %button = (
  425. 'select_all' =>
  426. { ndx => 2, key => 'A', value => $locale->text('Select all') },
  427. 'print' =>
  428. { ndx => 3, key => 'P', value => $locale->text('Print') },
  429. 'remove' =>
  430. { ndx => 4, key => 'R', value => $locale->text('Remove') },
  431. );
  432. for ( sort { $button{$a}->{ndx} <=> $button{$b}->{ndx} } keys %button )
  433. {
  434. push @buttons, {
  435. name => 'action',
  436. value => $_,
  437. accesskey => $button{$_}{key},
  438. title => "$button{$_}{value} [Alt-$button{$_}{key}]",
  439. text => $button{$_}{value},
  440. };
  441. }
  442. }
  443. ##SC: Temporary removal
  444. ## if ( $form->{lynx} ) {
  445. ## require "bin/menu.pl";
  446. ## &menubar;
  447. ## }
  448. my $template = LedgerSMB::Template->new_UI(
  449. user => \%myconfig,
  450. locale => $locale,
  451. template => 'bp-list-spool',
  452. );
  453. $template->render({
  454. form => $form,
  455. user => \%myconfig,
  456. hiddens => \%hiddens,
  457. buttons => \@buttons,
  458. options => \@options,
  459. rows => \@rows,
  460. columns => \@column_index,
  461. heading => \%column_header,
  462. printers => \@printers,
  463. });
  464. }
  465. sub select_all {
  466. for ( 1 .. $form->{rowcount} ) { $form->{"checked_$_"} = 1 }
  467. &list_spool;
  468. }
  469. sub continue { &{ $form->{nextsub} } }