Umstellung der Benutzerverwaltung von Dateien im Verzeichnis "users" auf die Verwendu...
[kivitendo-erp.git] / bin / mozilla / ca.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) 2001
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., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #======================================================================
29 #
30 # module for Chart of Accounts, Income Statement and Balance Sheet
31 # search and edit transactions posted by the GL, AR and AP
32 #
33 #======================================================================
34
35 use POSIX qw(strftime);
36
37 use SL::CA;
38 use SL::ReportGenerator;
39
40 require "bin/mozilla/reportgenerator.pl";
41
42 1;
43
44 # end of main
45
46 # this is for our long dates
47 # $locale->text('January')
48 # $locale->text('February')
49 # $locale->text('March')
50 # $locale->text('April')
51 # $locale->text('May ')
52 # $locale->text('June')
53 # $locale->text('July')
54 # $locale->text('August')
55 # $locale->text('September')
56 # $locale->text('October')
57 # $locale->text('November')
58 # $locale->text('December')
59
60 # this is for our short month
61 # $locale->text('Jan')
62 # $locale->text('Feb')
63 # $locale->text('Mar')
64 # $locale->text('Apr')
65 # $locale->text('May')
66 # $locale->text('Jun')
67 # $locale->text('Jul')
68 # $locale->text('Aug')
69 # $locale->text('Sep')
70 # $locale->text('Oct')
71 # $locale->text('Nov')
72 # $locale->text('Dec')
73
74 sub chart_of_accounts {
75   $lxdebug->enter_sub();
76
77   $auth->assert('report');
78
79   $form->{title} = $locale->text('Chart of Accounts');
80
81   CA->all_accounts(\%myconfig, \%$form);
82
83   my @columns     = qw(accno description debit credit);
84   my %column_defs = (
85     'accno'       => { 'text' => $locale->text('Account'), },
86     'description' => { 'text' => $locale->text('Description'), },
87     'debit'       => { 'text' => $locale->text('Debit'), },
88     'credit'      => { 'text' => $locale->text('Credit'), },
89   );
90
91   my $report = SL::ReportGenerator->new(\%myconfig, $form);
92
93   $report->set_options('output_format'         => 'HTML',
94                        'title'                 => $form->{title},
95                        'attachment_basename'   => $locale->text('chart_of_accounts') . strftime('_%Y%m%d', localtime time),
96                        'std_column_visibility' => 1,
97     );
98   $report->set_options_from_form();
99
100   $report->set_columns(%column_defs);
101   $report->set_column_order(@columns);
102
103   $report->set_export_options('chart_of_accounts');
104
105   $report->set_sort_indicator($form->{sort}, 1);
106
107   my %totals = ('debit' => 0, 'credit' => 0);
108
109   foreach my $ca (@{ $form->{CA} }) {
110     my $row = { };
111
112     foreach (qw(debit credit)) {
113       $totals{$_} += $ca->{$_} * 1;
114       $ca->{$_}    = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_});
115     }
116
117     map { $row->{$_} = { 'data' => $ca->{$_} } } @columns;
118
119     map { $row->{$_}->{align} = 'right'       } qw(debit credit);
120     map { $row->{$_}->{class} = 'listheading' } @columns if ($ca->{charttype} eq "H");
121
122     $row->{accno}->{link} = build_std_url('action=list', 'accno=' . E($ca->{accno}), 'description=' . E($ca->{description}));
123
124     $report->add_data($row);
125   }
126
127   my $row = { map { $_ => { 'class' => 'listtotal', 'align' => 'right' } } @columns };
128   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals{$_}, 2) } qw(debit credit);
129
130   $report->add_separator();
131   $report->add_data($row);
132
133   $report->generate_with_headers();
134
135   $lxdebug->leave_sub();
136 }
137
138 sub list {
139   $lxdebug->enter_sub();
140
141   $auth->assert('report');
142
143   $form->{title} = $locale->text('List Transactions');
144   $form->{title} .= " - " . $locale->text('Account') . " $form->{accno}";
145
146   # get departments
147   $form->all_departments(\%myconfig);
148   if (@{ $form->{all_departments} }) {
149     $form->{selectdepartment} = "<option>\n";
150
151     map {
152       $form->{selectdepartment} .=
153         "<option>$_->{description}--$_->{id}\n"
154     } (@{ $form->{all_departments} });
155   }
156
157   $department = qq|
158         <tr>
159           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
160           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
161         </tr>
162 | if $form->{selectdepartment};
163
164   $form->header;
165
166   $form->{description} =~ s/\"/&quot;/g;
167
168   print qq|
169 <body>
170
171 <form method=post action=$form->{script}>
172
173 <input type=hidden name=accno value=$form->{accno}>
174 <input type=hidden name=description value="$form->{description}">
175 <input type=hidden name=sort value=transdate>
176 <input type=hidden name=eur value=$eur>
177 <input type=hidden name=accounttype value=$form->{accounttype}>
178
179 <table border=0 width=100%>
180   <tr><th class=listtop>$form->{title}</th></tr>
181   <tr height="5"></tr
182   <tr valign=top>
183     <td>
184       <table>
185         $department
186         <tr>
187           <th align=right>| . $locale->text('From') . qq|</th>
188           <td><input name=fromdate size=11 title="$myconfig{dateformat}"></td>
189           <th align=right>| . $locale->text('To') . qq|</th>
190           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
191         </tr>
192         <tr>
193           <th align=right>| . $locale->text('Include in Report') . qq|</th>
194           <td colspan=3>
195           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
196     . $locale->text('Subtotal') . qq|</td>
197         </tr>
198       </table>
199     </td>
200   </tr>
201   <tr><td><hr size=3 noshade></td></tr>
202 </table>
203
204 <br><input class=submit type=submit name=action value="|
205     . $locale->text('List Transactions') . qq|">
206 </form>
207
208 </body>
209 </html>
210 |;
211
212   $lxdebug->leave_sub();
213 }
214
215 sub list_transactions {
216   $lxdebug->enter_sub();
217
218   $auth->assert('report');
219
220   $form->{title} = $locale->text('Account') . " $form->{accno} - $form->{description}";
221
222   CA->all_transactions(\%myconfig, \%$form);
223
224   my @options;
225   if ($form->{department}) {
226     my ($department) = split /--/, $form->{department};
227     push @options, $locale->text('Department') . " : $department";
228   }
229   if ($form->{projectnumber}) {
230     push @options, $locale->text('Project Number') . " : $form->{projectnumber}<br>";
231   }
232
233   my $period;
234   if ($form->{fromdate} || $form->{todate}) {
235     my ($fromdate, $todate);
236
237     if ($form->{fromdate}) {
238       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
239     }
240     if ($form->{todate}) {
241       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
242     }
243
244     $period = "$fromdate - $todate";
245
246   } else {
247     $period = $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
248   }
249
250   push @options, $period;
251
252   my @columns     = qw(transdate reference description debit credit balance);
253   my %column_defs = (
254     'transdate'   => { 'text' => $locale->text('Date'), },
255     'reference'   => { 'text' => $locale->text('Reference'), },
256     'description' => { 'text' => $locale->text('Description'), },
257     'debit'       => { 'text' => $locale->text('Debit'), },
258     'credit'      => { 'text' => $locale->text('Credit'), },
259     'balance'     => { 'text' => $locale->text('Balance'), },
260   );
261   my %column_alignment = map { $_ => 'right' } qw(debit credit balance);
262
263   my @hidden_variables = qw(accno fromdate todate description accounttype l_heading l_subtotal department projectnumber project_id sort);
264
265   my $link = build_std_url('action=list_transactions', grep { $form->{$_} } @hidden_variables);
266   map { $column_defs{$_}->{link} = $link . "&sort=$_" } qw(transdate reference description);
267
268   $form->{callback} = $link . '&sort=' . E($form->{sort});
269
270   my $report = SL::ReportGenerator->new(\%myconfig, $form);
271
272   $report->set_options('top_info_text'         => join("\n", @options),
273                        'output_format'         => 'HTML',
274                        'title'                 => $form->{title},
275                        'attachment_basename'   => $locale->text('list_of_transactions') . strftime('_%Y%m%d', localtime time),
276                        'std_column_visibility' => 1,
277     );
278   $report->set_options_from_form();
279
280   $report->set_columns(%column_defs);
281   $report->set_column_order(@columns);
282
283   $report->set_export_options('list_transactions', @hidden_variables);
284
285   $report->set_sort_indicator($form->{sort}, 1);
286
287   $column_defs->{balance}->{visible} = $form->{accno} ? 1 : 0;
288
289   my $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
290
291   if ($form->{accno} && $form->{balance}) {
292     my $row = {
293       'balance' => {
294         'data'  => $form->format_amount(\%myconfig, $form->{balance} * $ml, 2),
295         'align' => 'right',
296       },
297     };
298
299     $report->add_data($row);
300   }
301
302   my $idx       = 0;
303   my %totals    = ( 'debit' => 0, 'credit' => 0 );
304   my %subtotals = ( 'debit' => 0, 'credit' => 0 );
305   my ($previous_index, $row_set);
306
307   foreach my $ca (@{ $form->{CA} }) {
308     $form->{balance} += $ca->{amount};
309
310     foreach (qw(debit credit)) {
311       $subtotals{$_} += $ca->{$_};
312       $totals{$_}    += $ca->{$_};
313       $ca->{$_}       = $form->format_amount(\%myconfig, $ca->{$_}, 2) if ($ca->{$_} != 0);
314     }
315
316     $ca->{balance} = $form->format_amount(\%myconfig, $form->{balance} * $ml, 2);
317
318     my $row = { };
319
320     foreach (@columns) {
321       $row->{$_} = {
322         'data'  => $ca->{$_},
323         'align' => $column_alignment{$_},
324       };
325     }
326
327     if ($ca->{index} ne $previous_index) {
328       $report->add_data($row_set) if ($row_set);
329
330       $row_set         = [ ];
331       $previous_index  = $ca->{index};
332
333       $row->{reference}->{link} = build_std_url("script=$ca->{module}.pl", 'action=edit', 'id=' . E($ca->{id}), 'callback');
334
335     } else {
336       map { $row->{$_}->{data} = '' } qw(reference description);
337       $row->{transdate}->{data} = '' if ($form->{sort} eq 'transdate');
338     }
339
340     push @{ $row_set }, $row;
341
342     if (($form->{l_subtotal} eq 'Y')
343         && (($idx == scalar @{ $form->{CA} } - 1)
344             || ($ca->{$form->{sort}} ne $form->{CA}->[$idx + 1]->{$form->{sort}}))) {
345       $report->add_data(create_subtotal_row(\%subtotals, \@columns, \%column_alignment, 'listsubtotal'));
346     }
347
348     $idx++;
349   }
350
351   $report->add_data($row_set) if ($row_set);
352
353   $report->add_separator();
354
355   my $row = create_subtotal_row(\%totals, \@columns, \%column_alignment, 'listtotal');
356   $row->{balance}->{data} = $form->format_amount(\%myconfig, $form->{balance} * $ml, 2);
357   $report->add_data($row);
358
359   $report->generate_with_headers();
360
361   $lxdebug->leave_sub();
362 }
363
364 sub create_subtotal_row {
365   $lxdebug->enter_sub();
366
367   my ($totals, $columns, $column_alignment, $class) = @_;
368
369   my $row = { map { $_ => { 'data' => '', 'class' => $class, 'align' => $column_alignment->{$_}, } } @{ $columns } };
370
371   map { $row->{$_}->{data} = $form->format_amount(\%myconfig, $totals->{$_}, 2) } qw(credit debit);
372
373   map { $totals->{$_} = 0 } qw(debit credit);
374
375   $lxdebug->leave_sub();
376
377   return $row;
378 }