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