add some time-worked reporting
[freeside.git] / sql-ledger / bin / mozilla / ca.pl
1 #=====================================================================
2 # SQL-Ledger Accounting
3 # Copyright (C) 2001
4 #
5 #  Author: Dieter Simader
6 #   Email: dsimader@sql-ledger.org
7 #     Web: http://www.sql-ledger.org
8 #
9 #  Contributors:
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #======================================================================
24 #
25 # module for Chart of Accounts, Income Statement and Balance Sheet
26 # search and edit transactions posted by the GL, AR and AP
27
28 #======================================================================
29
30 use SL::CA;
31
32 1;
33 # end of main
34
35 # this is for our long dates
36 # $locale->text('January')
37 # $locale->text('February')
38 # $locale->text('March')
39 # $locale->text('April')
40 # $locale->text('May ')
41 # $locale->text('June')
42 # $locale->text('July')
43 # $locale->text('August')
44 # $locale->text('September')
45 # $locale->text('October')
46 # $locale->text('November')
47 # $locale->text('December')
48
49 # this is for our short month
50 # $locale->text('Jan')
51 # $locale->text('Feb')
52 # $locale->text('Mar')
53 # $locale->text('Apr')
54 # $locale->text('May')
55 # $locale->text('Jun')
56 # $locale->text('Jul')
57 # $locale->text('Aug')
58 # $locale->text('Sep')
59 # $locale->text('Oct')
60 # $locale->text('Nov')
61 # $locale->text('Dec')
62
63
64 sub chart_of_accounts {
65
66   CA->all_accounts(\%myconfig, \%$form);
67
68   @column_index = qw(accno gifi_accno description debit credit);
69
70   $column_header{accno} = qq|<th class=listtop>|.$locale->text('Account').qq|</th>\n|;
71   $column_header{gifi_accno} = qq|<th class=listtop>|.$locale->text('GIFI').qq|</th>\n|;
72   $column_header{description} = qq|<th class=listtop>|.$locale->text('Description').qq|</th>\n|;
73   $column_header{debit} = qq|<th class=listtop>|.$locale->text('Debit').qq|</th>\n|;
74   $column_header{credit} = qq|<th class=listtop>|.$locale->text('Credit').qq|</th>\n|;
75   
76
77   $form->{title} = $locale->text('Chart of Accounts');
78
79   $colspan = $#column_index + 1;
80   
81   $form->header;
82
83   print qq|
84 <body>
85   
86 <table border=0 width=100%>
87   <tr><th class=listtop colspan=$colspan>$form->{title}</th></tr>
88   <tr height="5"></tr>
89   <tr class=listheading>|;
90
91   map { print $column_header{$_} } @column_index;
92
93   print qq|
94   </tr>
95 |;
96
97   
98   foreach $ca (@{ $form->{CA} }) {
99
100     $description = $form->escape($ca->{description});
101     $gifi_description = $form->escape($ca->{gifi_description});
102     
103     $href = qq|$form->{script}?path=$form->{path}&action=list&accno=$ca->{accno}&login=$form->{login}&sessionid=$form->{sessionid}&description=$description&gifi_accno=$ca->{gifi_accno}&gifi_description=$gifi_description|;
104     
105     if ($ca->{charttype} eq "H") {
106       print qq|<tr class=listheading>|;
107       map { $column_data{$_} = "<th class=listheading>$ca->{$_}</th>"; } qw(accno description);
108       $column_data{gifi_accno} = "<th class=listheading>$ca->{gifi_accno}&nbsp;</th>";
109     } else {
110       $i++; $i %= 2;
111       print qq|<tr class=listrow$i>|;
112       $column_data{accno} = "<td><a href=$href>$ca->{accno}</a></td>";
113       $column_data{gifi_accno} = "<td><a href=$href&accounttype=gifi>$ca->{gifi_accno}</a>&nbsp;</td>";
114       $column_data{description} = "<td>$ca->{description}</td>";
115     }
116       
117     $column_data{debit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{debit}, 2, "&nbsp;")."</td>\n";
118     $column_data{credit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{credit}, 2, "&nbsp;")."</td>\n";
119     
120     $totaldebit += $ca->{debit};
121     $totalcredit += $ca->{credit};
122
123     map { print $column_data{$_} } @column_index;
124
125     print qq|
126 </tr>
127 |;
128   }
129
130   map { $column_data{$_} = "<td>&nbsp;</td>"; } qw(accno gifi_accno description);
131
132   $column_data{debit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totaldebit, 2, 0)."</th>";
133   $column_data{credit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totalcredit, 2, 0)."</th>";
134   
135   print "<tr class=listtotal>";
136
137   map { print $column_data{$_} } @column_index;
138
139   print qq|
140 </tr>
141 <tr>
142   <td colspan=$colspan><hr size=3 noshade></td>
143 </tr>
144 </table>
145
146 </body>
147 </html>
148 |;
149
150 }
151
152
153 sub list {
154
155   $form->{title} = $locale->text('List Transactions');
156   if ($form->{accounttype} eq 'gifi') {
157     $form->{title} .= " - ".$locale->text('GIFI')." $form->{gifi_accno}";
158   } else {
159     $form->{title} .= " - ".$locale->text('Account')." $form->{accno}";
160   }
161
162   # get departments
163   $form->all_departments(\%myconfig);
164   if (@{ $form->{all_departments} }) {
165     $form->{selectdepartment} = "<option>\n";
166
167     map { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| } (@{ $form->{all_departments} });
168   }
169
170   $department = qq|
171         <tr>
172           <th align=right nowrap>|.$locale->text('Department').qq|</th>
173           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
174         </tr>
175 | if $form->{selectdepartment};
176
177   # accounting years
178   $form->{selectaccountingyear} = "<option>\n";
179   map { $form->{selectaccountingyear} .= qq|<option>$_\n| } @{ $form->{all_years} };
180   $form->{selectaccountingmonth} = "<option>\n";
181   map { $form->{selectaccountingmonth} .= qq|<option value=$_>|.$locale->text($form->{all_month}{$_}).qq|\n| } sort keys %{ $form->{all_month} };
182
183   $selectfrom = qq|
184         <tr>
185         <th align=right>|.$locale->text('Period').qq|</th>
186         <td colspan=3>
187         <select name=month>$form->{selectaccountingmonth}</select>
188         <select name=year>$form->{selectaccountingyear}</select>
189         <input name=interval class=radio type=radio value=0 checked>|.$locale->text('Current').qq|
190         <input name=interval class=radio type=radio value=1>|.$locale->text('Month').qq|
191         <input name=interval class=radio type=radio value=3>|.$locale->text('Quarter').qq|
192         <input name=interval class=radio type=radio value=12>|.$locale->text('Year').qq|
193         </td>
194       </tr>
195 |;
196
197
198   $form->header;
199   
200   map { $form->{$_} = $form->quote($form->{$_}) } qw(description gifi_description);
201  
202   print qq|
203 <body>
204
205 <form method=post action=$form->{script}>
206
207 <input type=hidden name=accno value=$form->{accno}>
208 <input type=hidden name=description value="$form->{description}">
209 <input type=hidden name=sort value=transdate>
210
211 <input type=hidden name=accounttype value=$form->{accounttype}>
212 <input type=hidden name=gifi_accno value=$form->{gifi_accno}>
213 <input type=hidden name=gifi_description value="$form->{gifi_description}">
214
215 <table border=0 width=100%>
216   <tr><th class=listtop>$form->{title}</th></tr>
217   <tr height="5"></tr
218   <tr valign=top>
219     <td>
220       <table>
221         $department
222         <tr>
223           <th align=right>|.$locale->text('From').qq|</th>
224           <td><input name=fromdate size=11 title="$myconfig{dateformat}"></td>
225           <th align=right>|.$locale->text('To').qq|</th>
226           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
227         </tr>
228         $selectfrom
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;|.$locale->text('Subtotal').qq|</td>
233         </tr>
234       </table>
235     </td>
236   </tr>
237   <tr><td><hr size=3 noshade></td></tr>
238 </table>
239
240 <input type=hidden name=login value=$form->{login}>
241 <input type=hidden name=path value=$form->{path}>
242 <input type=hidden name=sessionid value=$form->{sessionid}>
243
244 <br><input class=submit type=submit name=action value="|.$locale->text('List Transactions').qq|">
245 </form>
246
247 </body>
248 </html>
249 |;
250
251 }
252
253
254 sub list_transactions {
255
256   CA->all_transactions(\%myconfig, \%$form);
257   
258   $description = $form->escape($form->{description});
259   $gifi_description = $form->escape($form->{gifi_description});
260   $department = $form->escape($form->{department});
261   $projectnumber = $form->escape($form->{projectnumber});
262   $title = $form->escape($form->{title});
263
264   # construct href
265   $href = "$form->{script}?path=$form->{path}&direction=$form->{direction}&oldsort=$form->{oldsort}&action=list_transactions&accno=$form->{accno}&login=$form->{login}&sessionid=$form->{sessionid}&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&title=$title";
266
267   $form->sort_order();
268   
269   $description = $form->escape($form->{description},1);
270   $gifi_description = $form->escape($form->{gifi_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 = "$form->{script}?path=$form->{path}&direction=$form->{direction}&oldsort=$form->{oldsort}&action=list_transactions&accno=$form->{accno}&login=$form->{login}&sessionid=$form->{sessionid}&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&title=$title";
277
278   # figure out which column comes first
279   $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>|.$locale->text('Date').qq|</a></th>|;
280   $column_header{reference} = qq|<th><a class=listheading href=$href&sort=reference>|.$locale->text('Reference').qq|</a></th>|;
281   $column_header{description} = qq|<th><a class=listheading href=$href&sort=description>|.$locale->text('Description').qq|</a></th>|;
282   $column_header{cleared} = qq|<th class=listheading>|.$locale->text('R').qq|</th>|;
283   $column_header{debit} = qq|<th class=listheading>|.$locale->text('Debit').qq|</th>|;
284   $column_header{credit} = qq|<th class=listheading>|.$locale->text('Credit').qq|</th>|;
285   $column_header{balance} = qq|<th class=listheading>|.$locale->text('Balance').qq|</th>|;
286
287   @column_index = $form->sort_columns(qw(transdate reference description debit credit));
288
289   if ($form->{link} =~ /_paid/) {
290     @column_index = $form->sort_columns(qw(transdate reference description cleared debit credit));
291   }
292   
293   if ($form->{accounttype} eq 'gifi') {
294     map { $form->{$_} = $form->{"gifi_$_"} } qw(accno description);
295   }
296   if ($form->{accno}) {
297     push @column_index, "balance";
298   }
299     
300   $form->{title} = ($form->{accounttype} eq 'gifi') ? $locale->text('GIFI') : $locale->text('Account');
301   
302   $form->{title} .= " $form->{accno} - $form->{description}";
303
304   if ($form->{department}) {
305     ($department) = split /--/, $form->{department};
306     $options = $locale->text('Department')." : $department<br>";
307   }
308   if ($form->{projectnumber}) {
309     ($projectnumber) = split /--/, $form->{projectnumber};
310     $options .= $locale->text('Project Number')." : $projectnumber<br>";
311   }
312
313   if ($form->{fromdate} || $form->{todate}) {
314     if ($form->{fromdate}) {
315       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
316     }
317     if ($form->{todate}) {
318       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
319     }
320     
321     $form->{period} = "$fromdate - $todate";
322   } else {
323     $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig),1);
324   }
325
326   $options .= $form->{period};
327   
328   $form->header;
329
330   print qq|
331 <body>
332
333 <table width=100%>
334   <tr>
335     <th class=listtop>$form->{title}</th>
336   </tr>
337   <tr height="5"></tr>
338   <tr>
339     <td>$options</td>
340   </tr>
341   <tr>
342     <td>
343       <table width=100%>
344        <tr class=listheading>
345 |;
346
347 map { print "$column_header{$_}\n" } @column_index;
348
349 print qq|
350        </tr>
351 |;
352
353   # add sort to callback
354   $callback = $form->escape($callback . "&sort=$form->{sort}");
355
356   if (@{ $form->{CA} }) {
357     $sameitem = $form->{CA}->[0]->{$form->{sort}};
358   }
359
360   $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
361   if ($form->{accno} && $form->{balance}) {
362     
363     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
364
365     $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</td>";
366     
367     $i++; $i %= 2;
368     print qq|
369         <tr class=listrow$i>
370 |;
371     map { print $column_data{$_} } @column_index;
372     print qq|
373        </tr>
374 |;
375   }
376     
377   foreach $ca (@{ $form->{CA} }) {
378
379     if ($form->{l_subtotal} eq 'Y') {
380       if ($sameitem ne $ca->{$form->{sort}}) {
381         &ca_subtotal;
382       }
383     }
384     
385     # construct link to source
386     $href = "<a href=$ca->{module}.pl?path=$form->{path}&action=edit&id=$ca->{id}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ca->{reference}</a>";
387
388     
389     $column_data{debit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{debit}, 2, "&nbsp;")."</td>";
390     $column_data{credit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{credit}, 2, "&nbsp;")."</td>";
391     
392     $form->{balance} += $ca->{amount};
393     $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</td>";
394
395     $subtotaldebit += $ca->{debit};
396     $subtotalcredit += $ca->{credit};
397     
398     $totaldebit += $ca->{debit};
399     $totalcredit += $ca->{credit};
400     
401     $column_data{transdate} = qq|<td>$ca->{transdate}</td>|;
402     $column_data{reference} = qq|<td>$href</td>|;
403     $column_data{description} = qq|<td>$ca->{description}&nbsp;</td>|;
404     
405     $column_data{cleared} = ($ca->{cleared}) ? qq|<td>*</td>| : qq|<td>&nbsp;</td>|;
406   
407     $i++; $i %= 2;
408     print qq|
409         <tr class=listrow$i>
410 |;
411
412     map { print $column_data{$_} } @column_index;
413
414     print qq|
415         </tr>
416 |;
417
418   }
419  
420
421   if ($form->{l_subtotal} eq 'Y') {
422     &ca_subtotal;
423   }
424  
425
426   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
427   
428   $column_data{debit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;")."</th>";
429   $column_data{credit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;")."</th>";
430   $column_data{balance} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</th>";
431
432   print qq|
433         <tr class=listtotal>
434 |;
435
436   map { print $column_data{$_} } @column_index;
437   
438   print qq|
439         </tr>
440       </table>
441     </td>
442   </tr>
443   <tr>
444     <td><hr size=3 noshade></td>
445   </tr>
446 </table>
447
448 </body>
449 </html>
450 |;
451   
452 }
453
454
455 sub ca_subtotal {
456
457   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
458   
459   $column_data{debit} = "<th align=right class=listsubtotal>".$form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;") . "</th>";
460   $column_data{credit} = "<th align=right class=listsubtotal>".$form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;") . "</th>";
461        
462   $subtotaldebit = 0;
463   $subtotalcredit = 0;
464
465   $sameitem = $ca->{$form->{sort}};
466
467   print qq|
468       <tr class=listsubtotal>
469 |;
470
471   map { print "$column_data{$_}\n" } @column_index;
472
473   print qq|
474       </tr>
475 |;
476
477 }
478