epic-s6ts
[kivitendo-erp.git] / bin / mozilla / bp.pl
1 #=====================================================================
2 # LX-Office ERP
3 # Copyright (C) 2004
4 # Based on SQL-Ledger Version 2.1.9
5 # Web http://www.lx-office.org
6 #
7 #=====================================================================
8 # SQL-Ledger Accounting
9 # Copyright (c) 2003
10 #
11 #  Author: Dieter Simader
12 #   Email: dsimader@sql-ledger.org
13 #     Web: http://www.sql-ledger.org
14 #
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to the Free Software
27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
28 # MA 02110-1335, USA.
29 #======================================================================
30 #
31 # Batch printing
32 #
33 #======================================================================
34
35 use SL::BP;
36 use SL::Locale::String qw(t8);
37 use Data::Dumper;
38 use List::Util qw(first);
39
40 1;
41
42 require "bin/mozilla/common.pl";
43
44 use strict;
45
46 # end of main
47
48 sub assert_bp_access {
49   my %access_map = (
50     'invoice'           => 'invoice_edit',
51     'sales_order'       => 'sales_order_edit',
52     'sales_quotation'   => 'sales_quotation_edit',
53     'purchase_order'    => 'purchase_order_edit',
54     'packing_list'      => 'sales_delivery_order_edit|purchase_delivery_order_edit',
55     'request_quotation' => 'request_quotation_edit',
56     'check'             => 'cash',
57     'receipt'           => 'cash',
58   );
59
60   if ($::form->{type} && $access_map{$::form->{type}}) {
61     $::auth->assert($access_map{$::form->{type}});
62
63   } else {
64     $::auth->assert('DOES_NOT_EXIST');
65   }
66 }
67
68 sub search {
69   $::lxdebug->enter_sub;
70
71   assert_bp_access();
72
73   my %label = (
74        invoice           => { title => $::locale->text('Sales Invoices'),  invnumber => 1, ordnumber => 1 },
75        sales_order       => { title => $::locale->text('Sales Orders'),    ordnumber => 1, },
76        purchase_order    => { title => $::locale->text('Purchase Orders'), ordnumber => 1, },
77        sales_quotation   => { title => $::locale->text('Quotations'),      quonumber => 1, },
78        request_quotation => { title => $::locale->text('RFQs'),            quonumber => 1, },
79        packing_list      => { title => $::locale->text('Delivery Orders'), donumber  => 1, ordnumber => 1 },
80        check             => { title => $::locale->text('Checks'),          chknumber => 1, },
81        receipt           => { title => $::locale->text('Receipts'),        rctnumber => 1, },
82   );
83
84   my $bp_accounts = $::form->{type} =~ /check|receipt/
85                  && BP->payment_accounts(\%::myconfig, $::form);
86
87   setup_bp_search_action_bar();
88
89   $::form->header;
90   print $::form->parse_html_template('bp/search', {
91     label         => \%label,
92     show_accounts => $bp_accounts,
93     account_sub   => sub { ("$_[0]{accno}--$_[0]{description}")x2 },
94     vc_keys       => sub { "$_[0]{name}--$_[0]{id}" },
95   });
96
97   $::lxdebug->leave_sub;
98 }
99
100 sub remove {
101   $::lxdebug->enter_sub;
102   assert_bp_access();
103
104   $::form->info($::locale->text('Removing marked entries from queue ...'));
105   $::form->{callback} .= "&header=1" if $::form->{callback};
106
107   $::form->redirect($::locale->text('Removed spoolfiles!'))
108     if BP->delete_spool(\%::myconfig, $::form);
109   $::form->error($::locale->text('Cannot remove files!'));
110
111   $::lxdebug->leave_sub;
112 }
113
114 sub print {
115   $::lxdebug->enter_sub;
116   assert_bp_access();
117
118   $::form->get_lists(printers => 'ALL_PRINTERS');
119   # use the command stored in the databse or fall back to $myconfig{printer}
120   my $selected_printer =  first { $_ } map ({ $_ ->{printer_command} }
121                                          grep { $_->{id} eq $::form->{printer} }
122                                            @{ $::form->{ALL_PRINTERS} }),
123                                        $::myconfig{printer};
124
125   if ($::form->{callback}) {
126     map { $::form->{callback} .= "&checked_$_=1" if $::form->{"checked_$_"} }
127       (1 .. $::form->{rowcount});
128     $::form->{callback} .= "&header=1";
129   }
130
131   for my $i (1 .. $::form->{rowcount}) {
132     if ($::form->{"checked_$i"}) {
133       $::form->info($::locale->text('Printing ... '));
134
135       if (BP->print_spool(\%::myconfig, $::form, "| $selected_printer")) {
136         print $::locale->text('done');
137         $::form->redirect($::locale->text('Marked entries printed!'));
138       }
139       $::dispatcher->end_request;
140     }
141   }
142
143   $::form->error('Nothing selected!');
144
145   $::lxdebug->leave_sub;
146 }
147
148 sub list_spool {
149   $::lxdebug->enter_sub;
150   assert_bp_access();
151
152   # parse old vc picker
153   $::form->{ $::form->{vc} } = $::form->unescape($::form->{ $::form->{vc} });
154   ($::form->{ $::form->{vc} }, $::form->{"$::form->{vc}_id"}) = split(/--/, $::form->{ $::form->{vc} });
155
156   BP->get_spoolfiles(\%::myconfig, $::form);
157
158   my @href_options = ('vc', 'type', 'title', $::form->{vc});
159
160   my %option_texts = (
161     customer      => sub { $::locale->text('Customer')         . " : $::form->{customer}" },
162     vendor        => sub { $::locale->text('Customer')         . " : $::form->{vendor}" },
163     account       => sub { $::locale->text('Account')          . " : $::form->{account}" },
164     invnumber     => sub { $::locale->text('Invoice Number')   . " : $::form->{invnumber}" },
165     ordnumber     => sub { $::locale->text('Order Number')     . " : $::form->{ordnumber}" },
166     quonumber     => sub { $::locale->text('Quotation Number') . " : $::form->{quonumber}" },
167     donumber      => sub { $::locale->text('Delivery Order Number') . " : $::form->{donumber}" },
168     transdatefrom => sub { $::locale->text('From') . " " . $::locale->date(\%::myconfig, $::form->{transdatefrom}, 1) },
169     transdateto   => sub { $::locale->text('To')   . " " . $::locale->date(\%::myconfig, $::form->{transdateto}, 1) },
170   );
171
172   my @options;
173   for my $key ($::form->{vc}, qw(account invnumber ordnumber quonumber donumber transdatefrom transdateto)) {
174     next unless $::form->{$key};
175     push @href_options, $key;
176     push @options, $option_texts{$key} ? $option_texts{$key}->() : '';
177   }
178
179   my $last_spoolfile;
180   for my $ref (@{ $::form->{SPOOL} }) {
181     $ref->{module}   = ($ref->{module} eq 'ar') ? "is" : "ir" if $ref->{invoice};
182     $ref->{new_file} = $last_spoolfile ne $ref->{spoolfile};
183   } continue {
184     $last_spoolfile = $ref->{spoolfile};
185   }
186
187   $::form->get_lists(printers => "ALL_PRINTERS");
188
189   setup_bp_list_spool_action_bar();
190
191   $::form->header;
192   print $::form->parse_html_template('bp/list_spool', {
193      href         => build_std_url('bp.pl', @href_options),
194      is_invoice   => scalar ($::form->{type} =~ /^invoice$/),
195      is_order     => scalar ($::form->{type} =~ /_order$/),
196      is_quotation => scalar ($::form->{type} =~ /_quotation$/),
197      options      => \@options,
198   });
199
200   $::lxdebug->leave_sub;
201 }
202
203 sub setup_bp_search_action_bar {
204   my %params = @_;
205
206   for my $bar ($::request->layout->get('actionbar')) {
207     $bar->add(
208       action => [
209         t8('Show'),
210         submit    => [ '#form', { action => "list_spool" } ],
211         accesskey => 'enter',
212       ],
213     );
214   }
215 }
216
217 sub setup_bp_list_spool_action_bar {
218   my %params = @_;
219
220   for my $bar ($::request->layout->get('actionbar')) {
221     $bar->add(
222       action => [
223         t8('Remove'),
224         submit  => [ '#form', { action => "remove" } ],
225         checks  => [ [ 'kivi.check_if_entries_selected', '.check_all' ] ],
226         confirm => t8('Are you sure you want to remove the marked entries from the queue?'),
227       ],
228       action => [
229         t8('Print'),
230         submit => [ '#form', { action => "print" } ],
231         checks => [ [ 'kivi.check_if_entries_selected', '.check_all' ] ],
232       ],
233     );
234   }
235 }