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