Kosmetik: Perltidy-Lauf nach den Einstellungen in doc/programmierrichtlinien.txt...
[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 SL::CA;
36
37 1;
38
39 # end of main
40
41 # this is for our long dates
42 # $locale->text('January')
43 # $locale->text('February')
44 # $locale->text('March')
45 # $locale->text('April')
46 # $locale->text('May ')
47 # $locale->text('June')
48 # $locale->text('July')
49 # $locale->text('August')
50 # $locale->text('September')
51 # $locale->text('October')
52 # $locale->text('November')
53 # $locale->text('December')
54
55 # this is for our short month
56 # $locale->text('Jan')
57 # $locale->text('Feb')
58 # $locale->text('Mar')
59 # $locale->text('Apr')
60 # $locale->text('May')
61 # $locale->text('Jun')
62 # $locale->text('Jul')
63 # $locale->text('Aug')
64 # $locale->text('Sep')
65 # $locale->text('Oct')
66 # $locale->text('Nov')
67 # $locale->text('Dec')
68
69 sub chart_of_accounts {
70   $lxdebug->enter_sub();
71
72   CA->all_accounts(\%myconfig, \%$form);
73
74   @column_index = qw(accno gifi_accno description debit credit);
75
76   $column_header{accno} =
77     qq|<th class=listheading>| . $locale->text('Account') . qq|</th>\n|;
78   $column_header{gifi_accno} =
79     qq|<th class=listheading>| . $locale->text('GIFI') . qq|</th>\n|;
80   $column_header{description} =
81     qq|<th class=listheading>| . $locale->text('Description') . qq|</th>\n|;
82   $column_header{debit} =
83     qq|<th class=listheading>| . $locale->text('Debit') . qq|</th>\n|;
84   $column_header{credit} =
85     qq|<th class=listheading>| . $locale->text('Credit') . qq|</th>\n|;
86
87   $form->{title} = $locale->text('Chart of Accounts');
88
89   $colspan = $#column_index + 1;
90
91   $form->header;
92
93   print qq|
94 <body>
95
96 <table border=0 width=100%>
97   <tr><th class=listtop colspan=$colspan>$form->{title}</th></tr>
98   <tr height="5"></tr>
99   <tr class=listheading>|;
100
101   map { print $column_header{$_} } @column_index;
102
103   print qq|
104   </tr>
105 |;
106
107   foreach $ca (@{ $form->{CA} }) {
108
109     $description      = $form->escape($ca->{description});
110     $gifi_description = $form->escape($ca->{gifi_description});
111
112     $href =
113       qq|$form->{script}?path=$form->{path}&action=list&accno=$ca->{accno}&login=$form->{login}&password=$form->{password}&description=$description&gifi_accno=$ca->{gifi_accno}&gifi_description=$gifi_description|;
114
115     if ($ca->{charttype} eq "H") {
116       print qq|<tr class=listheading>|;
117       map { $column_data{$_} = "<th>$ca->{$_}</th>"; } qw(accno description);
118       $column_data{gifi_accno} = "<th>$ca->{gifi_accno}&nbsp;</th>";
119     } else {
120       $i++;
121       $i %= 2;
122       print qq|<tr class=listrow$i>|;
123       $column_data{accno}      = "<td><a href=$href>$ca->{accno}</a></td>";
124       $column_data{gifi_accno} =
125         "<td><a href=$href&accounttype=gifi>$ca->{gifi_accno}</a>&nbsp;</td>";
126       $column_data{description} = "<td>$ca->{description}</td>";
127     }
128
129     $column_data{debit} =
130         "<td align=right>"
131       . $form->format_amount(\%myconfig, $ca->{debit}, 2, "&nbsp;")
132       . "</td>\n";
133     $column_data{credit} =
134         "<td align=right>"
135       . $form->format_amount(\%myconfig, $ca->{credit}, 2, "&nbsp;")
136       . "</td>\n";
137
138     $totaldebit  += $ca->{debit};
139     $totalcredit += $ca->{credit};
140
141     map { print $column_data{$_} } @column_index;
142
143     print qq|
144 </tr>
145 |;
146   }
147
148   map { $column_data{$_} = "<td>&nbsp;</td>"; }
149     qw(accno gifi_accno description);
150
151   $column_data{debit} =
152     "<th align=right class=listtotal>"
153     . $form->format_amount(\%myconfig, $totaldebit, 2, 0) . "</th>";
154   $column_data{credit} =
155     "<th align=right class=listtotal>"
156     . $form->format_amount(\%myconfig, $totalcredit, 2, 0) . "</th>";
157
158   print "<tr class=listtotal>";
159
160   map { print $column_data{$_} } @column_index;
161
162   print qq|
163 </tr>
164 <tr>
165   <td colspan=$colspan><hr size=3 noshade></td>
166 </tr>
167 </table>
168
169 </body>
170 </html>
171 |;
172
173   $lxdebug->leave_sub();
174 }
175
176 sub list {
177   $lxdebug->enter_sub();
178
179   $form->{title} = $locale->text('List Transactions');
180   if ($form->{accounttype} eq 'gifi') {
181     $form->{title} .= " - " . $locale->text('GIFI') . " $form->{gifi_accno}";
182   } else {
183     $form->{title} .= " - " . $locale->text('Account') . " $form->{accno}";
184   }
185
186   # get departments
187   $form->all_departments(\%myconfig);
188   if (@{ $form->{all_departments} }) {
189     $form->{selectdepartment} = "<option>\n";
190
191     map {
192       $form->{selectdepartment} .=
193         "<option>$_->{description}--$_->{id}\n"
194     } (@{ $form->{all_departments} });
195   }
196
197   $department = qq|
198         <tr>
199           <th align=right nowrap>| . $locale->text('Department') . qq|</th>
200           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
201         </tr>
202 | if $form->{selectdepartment};
203
204   $form->header;
205
206   map { $form->{$_} =~ s/\"/&quot;/g; } qw(description gifi_description);
207
208   print qq|
209 <body>
210
211 <form method=post action=$form->{script}>
212
213 <input type=hidden name=accno value=$form->{accno}>
214 <input type=hidden name=description value="$form->{description}">
215 <input type=hidden name=sort value=transdate>
216 <input type=hidden name=eur value=$eur>
217 <input type=hidden name=accounttype value=$form->{accounttype}>
218 <input type=hidden name=gifi_accno value=$form->{gifi_accno}>
219 <input type=hidden name=gifi_description value="$form->{gifi_description}">
220
221 <table border=0 width=100%>
222   <tr><th class=listtop>$form->{title}</th></tr>
223   <tr height="5"></tr
224   <tr valign=top>
225     <td>
226       <table>
227         $department
228         <tr>
229           <th align=right>| . $locale->text('From') . qq|</th>
230           <td><input name=fromdate size=11 title="$myconfig{dateformat}"></td>
231           <th align=right>| . $locale->text('To') . qq|</th>
232           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
233         </tr>
234         <tr>
235           <th align=right>| . $locale->text('Include in Report') . qq|</th>
236           <td colspan=3>
237           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|
238     . $locale->text('Subtotal') . qq|</td>
239         </tr>
240       </table>
241     </td>
242   </tr>
243   <tr><td><hr size=3 noshade></td></tr>
244 </table>
245
246 <input type=hidden name=login value=$form->{login}>
247 <input type=hidden name=path value=$form->{path}>
248 <input type=hidden name=password value=$form->{password}>
249
250 <br><input class=submit type=submit name=action value="|
251     . $locale->text('List Transactions') . qq|">
252 </form>
253
254 </body>
255 </html>
256 |;
257
258   $lxdebug->leave_sub();
259 }
260
261 sub list_transactions {
262   $lxdebug->enter_sub();
263
264   CA->all_transactions(\%myconfig, \%$form);
265
266   $description      = $form->escape($form->{description});
267   $gifi_description = $form->escape($form->{gifi_description});
268   $department       = $form->escape($form->{department});
269   $projectnumber    = $form->escape($form->{projectnumber});
270   $title            = $form->escape($form->{title});
271
272   # construct href
273   $href =
274     "$form->{script}?path=$form->{path}&action=list_transactions&accno=$form->{accno}&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&description=$description&accounttype=$form->{accounttype}&gifi_accno=$form->{gifi_accno}&gifi_description=$gifi_description&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title";
275
276   $description      = $form->escape($form->{description},      1);
277   $gifi_description = $form->escape($form->{gifi_description}, 1);
278   $department       = $form->escape($form->{department},       1);
279   $projectnumber    = $form->escape($form->{projectnumber},    1);
280   $title            = $form->escape($form->{title},            1);
281
282   # construct callback
283   $callback =
284     "$form->{script}?path=$form->{path}&action=list_transactions&accno=$form->{accno}&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&description=$description&accounttype=$form->{accounttype}&gifi_accno=$form->{gifi_accno}&gifi_description=$gifi_description&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title";
285
286   # figure out which column comes first
287   $column_header{transdate} =
288       qq|<th><a class=listheading href=$href&sort=transdate>|
289     . $locale->text('Date')
290     . qq|</a></th>|;
291   $column_header{reference} =
292       qq|<th><a class=listheading href=$href&sort=reference>|
293     . $locale->text('Reference')
294     . qq|</a></th>|;
295   $column_header{description} =
296       qq|<th><a class=listheading href=$href&sort=description>|
297     . $locale->text('Description')
298     . qq|</a></th>|;
299   $column_header{debit}   = qq|<th>| . $locale->text('Debit') . qq|</th>|;
300   $column_header{credit}  = qq|<th>| . $locale->text('Credit') . qq|</th>|;
301   $column_header{balance} = qq|<th>| . $locale->text('Balance') . qq|</th>|;
302
303   @column_index =
304     $form->sort_columns(qw(transdate reference description debit credit));
305
306   if ($form->{accounttype} eq 'gifi') {
307     map { $form->{$_} = $form->{"gifi_$_"} } qw(accno description);
308   }
309   if ($form->{accno}) {
310     push @column_index, "balance";
311   }
312
313   $form->{title} =
314     ($form->{accounttype} eq 'gifi')
315     ? $locale->text('GIFI')
316     : $locale->text('Account');
317
318   $form->{title} .= " $form->{accno} - $form->{description}";
319
320   if ($form->{department}) {
321     ($department) = split /--/, $form->{department};
322     $options = $locale->text('Department') . " : $department<br>";
323   }
324   if ($form->{projectnumber}) {
325     $options .= $locale->text('Project Number');
326     $options .= " : $form->{projectnumber}<br>";
327   }
328
329   if ($form->{fromdate} || $form->{todate}) {
330     if ($form->{fromdate}) {
331       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
332     }
333     if ($form->{todate}) {
334       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
335     }
336
337     $form->{period} = "$fromdate - $todate";
338   } else {
339     $form->{period} =
340       $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
341   }
342
343   $options .= $form->{period};
344
345   $form->header;
346
347   print qq|
348 <body>
349
350 <table width=100%>
351   <tr>
352     <th class=listtop>$form->{title}</th>
353   </tr>
354   <tr height="5"></tr>
355   <tr>
356     <td>$options</td>
357   </tr>
358   <tr>
359     <td>
360       <table width=100%>
361        <tr class=listheading>
362 |;
363
364   map { print "$column_header{$_}\n" } @column_index;
365
366   print qq|
367        </tr>
368 |;
369
370   # add sort to callback
371   $callback = $form->escape($callback . "&sort=$form->{sort}");
372
373   if (@{ $form->{CA} }) {
374     $sameitem = $form->{CA}->[0]->{ $form->{sort} };
375   }
376
377   $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
378   if ($form->{accno} && $form->{balance}) {
379
380     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
381
382     $column_data{balance} =
383         "<td align=right>"
384       . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)
385       . "</td>";
386
387     $i++;
388     $i %= 2;
389     print qq|
390         <tr class=listrow$i>
391 |;
392     map { print $column_data{$_} } @column_index;
393     print qq|
394        </tr>
395 |;
396   }
397
398   foreach $ca (@{ $form->{CA} }) {
399
400     if ($form->{l_subtotal} eq 'Y') {
401       if ($sameitem ne $ca->{ $form->{sort} }) {
402         &ca_subtotal;
403       }
404     }
405
406     # construct link to source
407     $href =
408       "<a href=$ca->{module}.pl?path=$form->{path}&action=edit&id=$ca->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ca->{reference}</a>";
409
410     $column_data{debit} =
411       "<td align=right>"
412       . $form->format_amount(\%myconfig, $ca->{debit}, 2, "&nbsp;") . "</td>";
413     $column_data{credit} =
414       "<td align=right>"
415       . $form->format_amount(\%myconfig, $ca->{credit}, 2, "&nbsp;") . "</td>";
416
417     $form->{balance} += $ca->{amount};
418     $column_data{balance} =
419         "<td align=right>"
420       . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)
421       . "</td>";
422
423     $subtotaldebit  += $ca->{debit};
424     $subtotalcredit += $ca->{credit};
425
426     $totaldebit  += $ca->{debit};
427     $totalcredit += $ca->{credit};
428
429     $column_data{transdate}   = qq|<td>$ca->{transdate}</td>|;
430     $column_data{reference}   = qq|<td>$href</td>|;
431     $column_data{description} = qq|<td>$ca->{description}</td>|;
432
433     $i++;
434     $i %= 2;
435     print qq|
436         <tr class=listrow$i>
437 |;
438
439     map { print $column_data{$_} } @column_index;
440
441     print qq|
442         </tr>
443 |;
444
445   }
446
447   if ($form->{l_subtotal} eq 'Y') {
448     &ca_subtotal;
449   }
450
451   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
452
453   $column_data{debit} =
454     "<th align=right>"
455     . $form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;") . "</th>";
456   $column_data{credit} =
457     "<th align=right>"
458     . $form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;") . "</th>";
459   $column_data{balance} =
460     "<th align=right>"
461     . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0) . "</th>";
462
463   print qq|
464         <tr class=listtotal>
465 |;
466
467   map { print $column_data{$_} } @column_index;
468
469   print qq|
470         </tr>
471       </table>
472     </td>
473   </tr>
474   <tr>
475     <td><hr size=3 noshade></td>
476   </tr>
477 </table>
478
479 </body>
480 </html>
481 |;
482
483   $lxdebug->leave_sub();
484 }
485
486 sub ca_subtotal {
487   $lxdebug->enter_sub();
488
489   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
490
491   $column_data{debit} =
492     "<th align=right>"
493     . $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;") . "</th>";
494   $column_data{credit} =
495     "<th align=right>"
496     . $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;") . "</th>";
497
498   $subtotaldebit  = 0;
499   $subtotalcredit = 0;
500
501   $sameitem = $ca->{ $form->{sort} };
502
503   print qq|
504       <tr class=listsubtotal>
505 |;
506
507   map { print "$column_data{$_}\n" } @column_index;
508
509   print qq|
510       </tr>
511 |;
512
513   $lxdebug->leave_sub();
514 }