Whitespace an den Zeilenenden entfernt.
[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')
239     . qq|</td>
240         </tr>
241       </table>
242     </td>
243   </tr>
244   <tr><td><hr size=3 noshade></td></tr>
245 </table>
246
247 <input type=hidden name=login value=$form->{login}>
248 <input type=hidden name=path value=$form->{path}>
249 <input type=hidden name=password value=$form->{password}>
250
251 <br><input class=submit type=submit name=action value="|
252     . $locale->text('List Transactions') . qq|">
253 </form>
254
255 </body>
256 </html>
257 |;
258
259   $lxdebug->leave_sub();
260 }
261
262 sub list_transactions {
263   $lxdebug->enter_sub();
264
265   CA->all_transactions(\%myconfig, \%$form);
266
267   $description      = $form->escape($form->{description});
268   $gifi_description = $form->escape($form->{gifi_description});
269   $department       = $form->escape($form->{department});
270   $projectnumber    = $form->escape($form->{projectnumber});
271   $title            = $form->escape($form->{title});
272
273   # construct href
274   $href =
275     "$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";
276
277   $description      = $form->escape($form->{description},      1);
278   $gifi_description = $form->escape($form->{gifi_description}, 1);
279   $department       = $form->escape($form->{department},       1);
280   $projectnumber    = $form->escape($form->{projectnumber},    1);
281   $title            = $form->escape($form->{title},            1);
282
283   # construct callback
284   $callback =
285     "$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";
286
287   # figure out which column comes first
288   $column_header{transdate} =
289       qq|<th><a class=listheading href=$href&sort=transdate>|
290     . $locale->text('Date')
291     . qq|</a></th>|;
292   $column_header{reference} =
293       qq|<th><a class=listheading href=$href&sort=reference>|
294     . $locale->text('Reference')
295     . qq|</a></th>|;
296   $column_header{description} =
297       qq|<th><a class=listheading href=$href&sort=description>|
298     . $locale->text('Description')
299     . qq|</a></th>|;
300   $column_header{debit}   = qq|<th>| . $locale->text('Debit') . qq|</th>|;
301   $column_header{credit}  = qq|<th>| . $locale->text('Credit') . qq|</th>|;
302   $column_header{balance} = qq|<th>| . $locale->text('Balance') . qq|</th>|;
303
304   @column_index =
305     $form->sort_columns(qw(transdate reference description debit credit));
306
307   if ($form->{accounttype} eq 'gifi') {
308     map { $form->{$_} = $form->{"gifi_$_"} } qw(accno description);
309   }
310   if ($form->{accno}) {
311     push @column_index, "balance";
312   }
313
314   $form->{title} =
315     ($form->{accounttype} eq 'gifi')
316     ? $locale->text('GIFI')
317     : $locale->text('Account');
318
319   $form->{title} .= " $form->{accno} - $form->{description}";
320
321   if ($form->{department}) {
322     ($department) = split /--/, $form->{department};
323     $options = $locale->text('Department') . " : $department<br>";
324   }
325   if ($form->{projectnumber}) {
326     $options .= $locale->text('Project Number');
327     $options .= " : $form->{projectnumber}<br>";
328   }
329
330   if ($form->{fromdate} || $form->{todate}) {
331     if ($form->{fromdate}) {
332       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
333     }
334     if ($form->{todate}) {
335       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
336     }
337
338     $form->{period} = "$fromdate - $todate";
339   } else {
340     $form->{period} =
341       $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
342   }
343
344   $options .= $form->{period};
345
346   $form->header;
347
348   print qq|
349 <body>
350
351 <table width=100%>
352   <tr>
353     <th class=listtop>$form->{title}</th>
354   </tr>
355   <tr height="5"></tr>
356   <tr>
357     <td>$options</td>
358   </tr>
359   <tr>
360     <td>
361       <table width=100%>
362        <tr class=listheading>
363 |;
364
365   map { print "$column_header{$_}\n" } @column_index;
366
367   print qq|
368        </tr>
369 |;
370
371   # add sort to callback
372   $callback = $form->escape($callback . "&sort=$form->{sort}");
373
374   if (@{ $form->{CA} }) {
375     $sameitem = $form->{CA}->[0]->{ $form->{sort} };
376   }
377
378   $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
379   if ($form->{accno} && $form->{balance}) {
380
381     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
382
383     $column_data{balance} =
384         "<td align=right>"
385       . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)
386       . "</td>";
387
388     $i++;
389     $i %= 2;
390     print qq|
391         <tr class=listrow$i>
392 |;
393     map { print $column_data{$_} } @column_index;
394     print qq|
395        </tr>
396 |;
397   }
398
399   foreach $ca (@{ $form->{CA} }) {
400
401     if ($form->{l_subtotal} eq 'Y') {
402       if ($sameitem ne $ca->{ $form->{sort} }) {
403         &ca_subtotal;
404       }
405     }
406
407     # construct link to source
408     $href =
409       "<a href=$ca->{module}.pl?path=$form->{path}&action=edit&id=$ca->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ca->{reference}</a>";
410
411     $column_data{debit} =
412       "<td align=right>"
413       . $form->format_amount(\%myconfig, $ca->{debit}, 2, "&nbsp;") . "</td>";
414     $column_data{credit} =
415       "<td align=right>"
416       . $form->format_amount(\%myconfig, $ca->{credit}, 2, "&nbsp;") . "</td>";
417
418     $form->{balance} += $ca->{amount};
419     $column_data{balance} =
420         "<td align=right>"
421       . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)
422       . "</td>";
423
424     $subtotaldebit  += $ca->{debit};
425     $subtotalcredit += $ca->{credit};
426
427     $totaldebit  += $ca->{debit};
428     $totalcredit += $ca->{credit};
429
430     $column_data{transdate}   = qq|<td>$ca->{transdate}</td>|;
431     $column_data{reference}   = qq|<td>$href</td>|;
432     $column_data{description} = qq|<td>$ca->{description}</td>|;
433
434     $i++;
435     $i %= 2;
436     print qq|
437         <tr class=listrow$i>
438 |;
439
440     map { print $column_data{$_} } @column_index;
441
442     print qq|
443         </tr>
444 |;
445
446   }
447
448   if ($form->{l_subtotal} eq 'Y') {
449     &ca_subtotal;
450   }
451
452   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
453
454   $column_data{debit} =
455     "<th align=right>"
456     . $form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;") . "</th>";
457   $column_data{credit} =
458     "<th align=right>"
459     . $form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;") . "</th>";
460   $column_data{balance} =
461     "<th align=right>"
462     . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0) . "</th>";
463
464   print qq|
465         <tr class=listtotal>
466 |;
467
468   map { print $column_data{$_} } @column_index;
469
470   print qq|
471         </tr>
472       </table>
473     </td>
474   </tr>
475   <tr>
476     <td><hr size=3 noshade></td>
477   </tr>
478 </table>
479
480 </body>
481 </html>
482 |;
483
484   $lxdebug->leave_sub();
485 }
486
487 sub ca_subtotal {
488   $lxdebug->enter_sub();
489
490   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
491
492   $column_data{debit} =
493     "<th align=right>"
494     . $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;") . "</th>";
495   $column_data{credit} =
496     "<th align=right>"
497     . $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;") . "</th>";
498
499   $subtotaldebit  = 0;
500   $subtotalcredit = 0;
501
502   $sameitem = $ca->{ $form->{sort} };
503
504   print qq|
505       <tr class=listsubtotal>
506 |;
507
508   map { print "$column_data{$_}\n" } @column_index;
509
510   print qq|
511       </tr>
512 |;
513
514   $lxdebug->leave_sub();
515 }