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