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