9e3d4ecf2f7acc44094094d40d7b0dd23f277a74
[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}?path=$form->{path}&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=path value=$form->{path}>
243 <input type=hidden name=password value=$form->{password}>
244
245 <br><input class=submit type=submit name=action value="|
246     . $locale->text('List Transactions') . qq|">
247 </form>
248
249 </body>
250 </html>
251 |;
252
253   $lxdebug->leave_sub();
254 }
255
256 sub list_transactions {
257   $lxdebug->enter_sub();
258
259   CA->all_transactions(\%myconfig, \%$form);
260
261   $description      = $form->escape($form->{description});
262   $department       = $form->escape($form->{department});
263   $projectnumber    = $form->escape($form->{projectnumber});
264   $title            = $form->escape($form->{title});
265
266   # construct href
267   $href =
268     "$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}&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title";
269
270   $description      = $form->escape($form->{description},      1);
271   $department       = $form->escape($form->{department},       1);
272   $projectnumber    = $form->escape($form->{projectnumber},    1);
273   $title            = $form->escape($form->{title},            1);
274
275   # construct callback
276   $callback =
277     "$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}&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&department=$department&projectnumber=$projectnumber&project_id=$form->{project_id}&title=$title";
278
279   # figure out which column comes first
280   $column_header{transdate} =
281       qq|<th><a class=listheading href=$href&sort=transdate>|
282     . $locale->text('Date')
283     . qq|</a></th>|;
284   $column_header{reference} =
285       qq|<th><a class=listheading href=$href&sort=reference>|
286     . $locale->text('Reference')
287     . qq|</a></th>|;
288   $column_header{description} =
289       qq|<th><a class=listheading href=$href&sort=description>|
290     . $locale->text('Description')
291     . qq|</a></th>|;
292   $column_header{debit}   = qq|<th>| . $locale->text('Debit') . qq|</th>|;
293   $column_header{credit}  = qq|<th>| . $locale->text('Credit') . qq|</th>|;
294   $column_header{balance} = qq|<th>| . $locale->text('Balance') . qq|</th>|;
295
296   @column_index =
297     $form->sort_columns(qw(transdate reference description debit credit));
298
299   if ($form->{accno}) {
300     push @column_index, "balance";
301   }
302
303   $form->{title} = $locale->text('Account') .
304     " $form->{accno} - $form->{description}";
305
306   if ($form->{department}) {
307     ($department) = split /--/, $form->{department};
308     $options = $locale->text('Department') . " : $department<br>";
309   }
310   if ($form->{projectnumber}) {
311     $options .= $locale->text('Project Number');
312     $options .= " : $form->{projectnumber}<br>";
313   }
314
315   if ($form->{fromdate} || $form->{todate}) {
316     if ($form->{fromdate}) {
317       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
318     }
319     if ($form->{todate}) {
320       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
321     }
322
323     $form->{period} = "$fromdate - $todate";
324   } else {
325     $form->{period} =
326       $locale->date(\%myconfig, $form->current_date(\%myconfig), 1);
327   }
328
329   $options .= $form->{period};
330
331   $form->header;
332
333   print qq|
334 <body>
335
336 <table width=100%>
337   <tr>
338     <th class=listtop>$form->{title}</th>
339   </tr>
340   <tr height="5"></tr>
341   <tr>
342     <td>$options</td>
343   </tr>
344   <tr>
345     <td>
346       <table width=100%>
347        <tr class=listheading>
348 |;
349
350   map { print "$column_header{$_}\n" } @column_index;
351
352   print qq|
353        </tr>
354 |;
355
356   # add sort to callback
357   $callback = $form->escape($callback . "&sort=$form->{sort}");
358
359   if (@{ $form->{CA} }) {
360     $sameitem = $form->{CA}->[0]->{ $form->{sort} };
361   }
362
363   $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
364   if ($form->{accno} && $form->{balance}) {
365
366     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
367
368     $column_data{balance} =
369         "<td align=right>"
370       . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)
371       . "</td>";
372
373     $i++;
374     $i %= 2;
375     print qq|
376         <tr class=listrow$i>
377 |;
378     map { print $column_data{$_} } @column_index;
379     print qq|
380        </tr>
381 |;
382   }
383
384   foreach $ca (@{ $form->{CA} }) {
385
386     if ($form->{l_subtotal} eq 'Y') {
387       if ($sameitem ne $ca->{ $form->{sort} }) {
388         &ca_subtotal;
389       }
390     }
391
392     # construct link to source
393     $href =
394       "<a href=$ca->{module}.pl?path=$form->{path}&action=edit&id=$ca->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$ca->{reference}</a>";
395     my $debit = ($ca->{debit} != 0) ? $form->format_amount(\%myconfig, $ca->{debit}, 2, "&nbsp;") : "&nbsp;";
396     $column_data{debit} =
397       "<td align=right>$debit</td>";
398     my $credit = ($ca->{credit} != 0) ? $form->format_amount(\%myconfig, $ca->{credit}, 2, "&nbsp;") : "&nbsp;";
399     $column_data{credit} =
400       "<td align=right>$credit</td>";
401
402     $form->{balance} += $ca->{amount};
403     $column_data{balance} =
404         "<td align=right>"
405       . $form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)
406       . "</td>";
407
408     $subtotaldebit  += $ca->{debit};
409     $subtotalcredit += $ca->{credit};
410
411     $totaldebit  += $ca->{debit};
412     $totalcredit += $ca->{credit};
413
414     $column_data{transdate}   = qq|<td>$ca->{transdate}</td>|;
415     $column_data{reference}   = qq|<td>$href</td>|;
416     $column_data{description} = qq|<td>$ca->{description}</td>|;
417
418     $i++;
419     $i %= 2;
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 }