c0e325e4f41073b0d0d45b3d9ec0b0c6469b45eb
[kivitendo-erp.git] / bin / mozilla / acctranscorrections.pl
1 use SL::AccTransCorrections;
2 use SL::Form;
3 use SL::Locale::String qw(t8);
4 use SL::User;
5 use Data::Dumper;
6 use YAML;
7
8 require "bin/mozilla/common.pl";
9
10 use strict;
11
12 sub analyze_filter {
13   $main::lxdebug->enter_sub();
14
15   my $form     = $main::form;
16   my $locale   = $main::locale;
17
18   setup_analyze_filter_action_bar();
19
20   $form->{title}    = $locale->text('General ledger corrections');
21   $form->header();
22   print $form->parse_html_template('acctranscorrections/analyze_filter');
23
24   $main::lxdebug->leave_sub();
25 }
26
27 sub analyze {
28   $main::lxdebug->enter_sub();
29
30   my $form     = $main::form;
31   my $locale   = $main::locale;
32
33   $form->{title} = $locale->text('General ledger corrections');
34
35   delete @{ $form }{qw(transdate_from transdate_to)} if ($form->{scope} eq 'full');
36
37   my $callback = 'acctranscorrections.pl?action=analyze';
38   map { $callback .= "&${_}=" . $form->escape($form->{$_}) } grep { $form->{$_} } qw(transdate_from transdate_to);
39   $callback = $form->escape($callback);
40
41   my $analyzer = AccTransCorrections->new();
42
43   my %params   = map { $_ => $form->{$_} } qw(transdate_from transdate_to);
44   my @problems = $analyzer->analyze(%params, 'callback' => $callback);
45
46   if (!scalar @problems) {
47     $form->show_generic_information($locale->text('No problems were recognized.'));
48
49     $main::lxdebug->leave_sub();
50     return;
51   }
52
53   setup_analyze_action_bar();
54
55   $form->header();
56   print $form->parse_html_template('acctranscorrections/analyze_overview',
57                                    { 'PROBLEMS' => \@problems,
58                                      'callback' => $callback,
59                                    });
60
61   $main::lxdebug->leave_sub();
62 }
63
64 sub assistant {
65   $main::lxdebug->enter_sub();
66
67   my $form     = $main::form;
68   my $locale   = $main::locale;
69
70   $form->{title} = $locale->text('Assistant for general ledger corrections');
71
72   $form->isblank('trans_id', $locale->text('Transaction ID missing.'));
73
74   my $analyzer  = AccTransCorrections->new();
75   my ($problem) = $analyzer->analyze('trans_id' => $form->{trans_id}, 'full_analysis' => 1);
76
77   if (!$problem) {
78     my $module =
79         $form->{trans_module} eq 'ar' ? $locale->text('AR Transaction')
80       : $form->{trans_module} eq 'ap' ? $locale->text('AP Transaction')
81       :                                 $locale->text('General Ledger Transaction');
82
83     $form->show_generic_information($locale->text('The assistant could not find anything wrong with #1. Maybe the problem has been solved in the meantime.',
84                                                   "$module $form->{trans_reference}"));
85
86     $main::lxdebug->leave_sub();
87     return;
88   }
89
90   if ($problem->{type} eq 'wrong_taxkeys') {
91     assistant_for_wrong_taxkeys($problem);
92
93   } elsif ($problem->{type} eq 'wrong_taxes') {
94     assistant_for_wrong_taxkeys($problem);
95 #     assistant_for_wrong_taxes($problem);
96
97   } else {
98     $form->show_generic_error($locale->text('Unknown problem type.'));
99   }
100
101   $main::lxdebug->leave_sub();
102 }
103
104 sub assistant_for_ap_ar_wrong_taxkeys {
105   $main::lxdebug->enter_sub();
106
107   my $form     = $main::form;
108   my $locale   = $main::locale;
109
110   $form->{title} = $locale->text('Assistant for general ledger corrections');
111
112   $form->header();
113   print $form->parse_html_template('acctranscorrections/assistant_for_ap_ar_wrong_taxkeys');
114
115   $main::lxdebug->leave_sub();
116 }
117
118 sub fix_ap_ar_wrong_taxkeys {
119   $main::lxdebug->enter_sub();
120
121   my $form     = $main::form;
122   my $locale   = $main::locale;
123
124   my $analyzer = AccTransCorrections->new();
125   $analyzer->fix_ap_ar_wrong_taxkeys();
126
127   $form->{title} = $locale->text('Assistant for general ledger corrections');
128   $form->header();
129   print $form->parse_html_template('acctranscorrections/fix_ap_ar_wrong_taxkeys');
130
131   $main::lxdebug->leave_sub();
132 }
133
134 sub assistant_for_invoice_inventory_with_taxkeys {
135   $main::lxdebug->enter_sub();
136
137   my $form     = $main::form;
138   my $locale   = $main::locale;
139
140   $form->{title} = $locale->text('Assistant for general ledger corrections');
141
142   $form->header();
143   print $form->parse_html_template('acctranscorrections/assistant_for_invoice_inventory_with_taxkeys');
144
145   $main::lxdebug->leave_sub();
146 }
147
148 sub fix_invoice_inventory_with_taxkeys {
149   $main::lxdebug->enter_sub();
150
151   my $form     = $main::form;
152   my $locale   = $main::locale;
153
154   my $analyzer  = AccTransCorrections->new();
155   $analyzer->fix_invoice_inventory_with_taxkeys();
156
157   $form->{title} = $locale->text('Assistant for general ledger corrections');
158   $form->header();
159   print $form->parse_html_template('acctranscorrections/fix_invoice_inventory_with_taxkeys');
160
161   $main::lxdebug->leave_sub();
162 }
163
164 sub assistant_for_wrong_taxes {
165   $main::lxdebug->enter_sub();
166
167   my $form     = $main::form;
168   my $locale   = $main::locale;
169
170   my $problem = shift;
171
172   $form->{title} = $locale->text('Assistant for general ledger corrections');
173
174   $form->header();
175   print $form->parse_html_template('acctranscorrections/assistant_for_wrong_taxes', { 'problem' => $problem, });
176
177   $main::lxdebug->leave_sub();
178 }
179
180 sub assistant_for_wrong_taxkeys {
181   $main::lxdebug->enter_sub();
182
183   my $form     = $main::form;
184   my $locale   = $main::locale;
185
186   my $problem = shift;
187
188   $form->{title} = $locale->text('Assistant for general ledger corrections');
189
190   $form->header();
191   print $form->parse_html_template('acctranscorrections/assistant_for_wrong_taxkeys', { 'problem' => $problem, });
192
193   $main::lxdebug->leave_sub();
194 }
195
196 sub fix_wrong_taxkeys {
197   $main::lxdebug->enter_sub();
198
199   my $form     = $main::form;
200   my $locale   = $main::locale;
201
202   my $fixes = ref $form->{fixes} eq 'ARRAY' ? $form->{fixes} : [];
203
204   my $analyzer  = AccTransCorrections->new();
205   $analyzer->fix_wrong_taxkeys('fixes' => $fixes);
206
207   $form->{title} = $locale->text('Assistant for general ledger corrections');
208   $form->header();
209   print $form->parse_html_template('acctranscorrections/fix_wrong_taxkeys');
210
211   $main::lxdebug->leave_sub();
212 }
213
214 sub delete_transaction {
215   $main::lxdebug->enter_sub();
216
217   my $form     = $main::form;
218   my $locale   = $main::locale;
219
220   $form->{title} = $locale->text('Delete transaction');
221   $form->header();
222
223   if (!$form->{confirmation}) {
224     print $form->parse_html_template('acctranscorrections/delete_transaction_confirmation');
225   } else {
226     my $analyzer  = AccTransCorrections->new();
227     $analyzer->delete_transaction('trans_id' => $form->{trans_id});
228
229     print $form->parse_html_template('acctranscorrections/delete_transaction');
230   }
231
232   $main::lxdebug->leave_sub();
233 }
234
235 sub redirect {
236   $main::lxdebug->enter_sub();
237
238   my $form     = $main::form;
239
240   $form->redirect('Missing callbcak');
241
242   $main::lxdebug->leave_sub();
243 }
244
245 sub dispatcher {
246   my $form     = $main::form;
247   my $locale   = $main::locale;
248
249   foreach my $action (qw(fix_wrong_taxkeys delete_transaction)) {
250     if ($form->{"action_${action}"}) {
251       call_sub($action);
252       return;
253     }
254   }
255
256   $form->error($locale->text('No action defined.'));
257 }
258
259 sub setup_analyze_filter_action_bar {
260   my %params = @_;
261
262   for my $bar ($::request->layout->get('actionbar')) {
263     $bar->add(
264       action => [
265         t8('Analyze'),
266         submit    => [ '#form' ],
267         accesskey => 'enter',
268       ],
269     );
270   }
271 }
272
273 sub setup_analyze_action_bar {
274   my %params = @_;
275
276   for my $bar ($::request->layout->get('actionbar')) {
277     $bar->add(
278       action => [
279         t8('Back'),
280         call => [ 'kivi.history_back' ],
281       ],
282     );
283   }
284 }
285
286 1;