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