d8180a630201674dfb608338fb15ba1c1479e5f1
[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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # Batch printing
31 #
32 #======================================================================
33
34 use SL::BP;
35 use Data::Dumper;
36 use List::Util qw(first);
37
38 1;
39
40 require "bin/mozilla/common.pl";
41
42 use strict;
43
44 # end of main
45
46 sub assert_bp_access {
47   my %access_map = (
48     'invoice'           => 'invoice_edit',
49     'sales_order'       => 'sales_order_edit',
50     'sales_quotation'   => 'sales_quotation_edit',
51     'purchase_order'    => 'purchase_order_edit',
52     'packing_list'      => 'sales_delivery_order_edit|purchase_delivery_order_edit',
53     'request_quotation' => 'request_quotation_edit',
54     'check'             => 'cash',
55     'receipt'           => 'cash',
56   );
57
58   if ($::form->{type} && $access_map{$::form->{type}}) {
59     $::auth->assert($access_map{$::form->{type}});
60
61   } else {
62     $::auth->assert('DOES_NOT_EXIST');
63   }
64 }
65
66 sub search {
67   $::lxdebug->enter_sub;
68
69   assert_bp_access();
70
71   # setup customer/vendor selection
72   BP->get_vc(\%::myconfig, $::form);
73
74   my %label = (
75        invoice           => { title => $::locale->text('Sales Invoices'),  invnumber => 1, ordnumber => 1 },
76        sales_order       => { title => $::locale->text('Sales Orders'),    ordnumber => 1, },
77        purchase_order    => { title => $::locale->text('Purchase Orders'), ordnumber => 1, },
78        sales_quotation   => { title => $::locale->text('Quotations'),      quonumber => 1, },
79        request_quotation => { title => $::locale->text('RFQs'),            quonumber => 1, },
80        packing_list      => { title => $::locale->text('Delivery Orders'), donumber  => 1, ordnumber => 1 },
81        check             => { title => $::locale->text('Checks'),          chknumber => 1, },
82        receipt           => { title => $::locale->text('Receipts'),        rctnumber => 1, },
83   );
84
85   my $bp_accounts = $::form->{type} =~ /check|receipt/
86                  && BP->payment_accounts(\%::myconfig, $::form);
87
88   $::form->header;
89   print $::form->parse_html_template('bp/search', {
90     label         => \%label,
91     show_accounts => $bp_accounts,
92     account_sub   => sub { ("$_[0]{accno}--$_[0]{description}")x2 },
93     vc_keys       => sub { "$_[0]{name}--$_[0]{id}" },
94   });
95
96   $::lxdebug->leave_sub;
97 }
98
99 sub remove {
100   $::lxdebug->enter_sub;
101   assert_bp_access();
102
103   $::form->info($::locale->text('Removing marked entries from queue ...'));
104   $::form->{callback} .= "&header=1" if $::form->{callback};
105
106   $::form->redirect($::locale->text('Removed spoolfiles!'))
107     if BP->delete_spool(\%::myconfig, $::form);
108   $::form->error($::locale->text('Cannot remove files!'));
109
110   $::lxdebug->leave_sub;
111 }
112
113 sub print {
114   $::lxdebug->enter_sub;
115   assert_bp_access();
116
117   $::form->get_lists(printers => 'ALL_PRINTERS');
118   # use the command stored in the databse or fall back to $myconfig{printer}
119   my $selected_printer =  first { $_ } map ({ $_ ->{printer_command} }
120                                          grep { $_->{id} eq $::form->{printer} }
121                                            @{ $::form->{ALL_PRINTERS} }),
122                                        $::myconfig{printer};
123
124   if ($::form->{callback}) {
125     map { $::form->{callback} .= "&checked_$_=1" if $::form->{"checked_$_"} }
126       (1 .. $::form->{rowcount});
127     $::form->{callback} .= "&header=1";
128   }
129
130   for my $i (1 .. $::form->{rowcount}) {
131     if ($::form->{"checked_$i"}) {
132       $::form->info($::locale->text('Printing ... '));
133
134       if (BP->print_spool(\%::myconfig, $::form, "| $selected_printer")) {
135         print $::locale->text('done');
136         $::form->redirect($::locale->text('Marked entries printed!'));
137       }
138       ::end_of_request();
139     }
140   }
141
142   $::form->error('Nothing selected!');
143
144   $::lxdebug->leave_sub;
145 }
146
147 sub list_spool {
148   $::lxdebug->enter_sub;
149   assert_bp_access();
150
151   # parse old vc picker
152   $::form->{ $::form->{vc} } = $::form->unescape($::form->{ $::form->{vc} });
153   ($::form->{ $::form->{vc} }, $::form->{"$::form->{vc}_id"}) = split(/--/, $::form->{ $::form->{vc} });
154
155   BP->get_spoolfiles(\%::myconfig, $::form);
156
157   my @href_options = ('vc', 'type', 'title', $::form->{vc});
158
159   my %option_texts = (
160     customer      => sub { $::locale->text('Customer')         . " : $::form->{customer}" },
161     vendor        => sub { $::locale->text('Customer')         . " : $::form->{vendor}" },
162     account       => sub { $::locale->text('Account')          . " : $::form->{account}" },
163     invnumber     => sub { $::locale->text('Invoice Number')   . " : $::form->{invnumber}" },
164     ordnumber     => sub { $::locale->text('Order Number')     . " : $::form->{ordnumber}" },
165     quonumber     => sub { $::locale->text('Quotation Number') . " : $::form->{quonumber}" },
166     donumber      => sub { $::locale->text('Delivery Order Number') . " : $::form->{donumber}" },
167     transdatefrom => sub { $::locale->text('From') . " " . $::locale->date(\%::myconfig, $::form->{transdatefrom}, 1) },
168     transdateto   => sub { $::locale->text('To')   . " " . $::locale->date(\%::myconfig, $::form->{transdateto}, 1) },
169   );
170
171   my @options;
172   for my $key ($::form->{vc}, qw(account invnumber ordnumber quonumber donumber transdatefrom transdateto)) {
173     next unless $::form->{$key};
174     push @href_options, $key;
175     push @options, $option_texts{$key} ? $option_texts{$key}->() : '';
176   }
177
178   my $last_spoolfile;
179   for my $ref (@{ $::form->{SPOOL} }) {
180     $ref->{module}   = ($ref->{module} eq 'ar') ? "is" : "ir" if $ref->{invoice};
181     $ref->{new_file} = $last_spoolfile ne $ref->{spoolfile};
182   } continue {
183     $last_spoolfile = $ref->{spoolfile};
184   }
185
186   $::form->get_lists(printers => "ALL_PRINTERS");
187
188   $::form->header;
189   print $::form->parse_html_template('bp/list_spool', {
190      spool        => $::lx_office_conf{paths}->{spool},
191      href         => build_std_url('bp.pl', @href_options),
192      is_invoice   => scalar ($::form->{type} =~ /^invoice$/),
193      is_order     => scalar ($::form->{type} =~ /_order$/),
194      is_quotation => scalar ($::form->{type} =~ /_quotation$/),
195      options      => \@options,
196   });
197
198   $::lxdebug->leave_sub;
199 }
200
201 sub continue { call_sub($::form->{"nextsub"}); }
202