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