more info in error message for unknown export type, fix test for sqlradius_withdomain...
[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 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #======================================================================
23 #
24 # module for Chart of Accounts, Income Statement and Balance Sheet
25 # search and edit transactions posted by the GL, AR and AP
26
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=listheading>|.$locale->text('Account').qq|</th>\n|;
71   $column_header{gifi_accno} = qq|<th class=listheading>|.$locale->text('GIFI').qq|</th>\n|;
72   $column_header{description} = qq|<th class=listheading>|.$locale->text('Description').qq|</th>\n|;
73   $column_header{debit} = qq|<th class=listheading>|.$locale->text('Debit').qq|</th>\n|;
74   $column_header{credit} = qq|<th class=listheading>|.$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 width=100%>
87   <tr><th class=listtop colspan=$colspan>$form->{title}</font></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}&password=$form->{password}&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;</font></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 class=listtotal align=right>".$form->format_amount(\%myconfig, $totaldebit, 2, 0)."</font></th>";
133   $column_data{credit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalcredit, 2, 0)."</font></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   $form->header;
163   
164   map { $form->{$_} =~ s/"/&quot;/g; } qw(description gifi_description);
165  
166   print qq|
167 <body>
168
169 <form method=post action=$form->{script}>
170
171 <input type=hidden name=accno value=$form->{accno}>
172 <input type=hidden name=description value="$form->{description}">
173 <input type=hidden name=sort value=transdate>
174
175 <input type=hidden name=accounttype value=$form->{accounttype}>
176 <input type=hidden name=gifi_accno value=$form->{gifi_accno}>
177 <input type=hidden name=gifi_description value="$form->{gifi_description}">
178
179 <table border=0 width=100%>
180   <tr><th class=listtop>$form->{title}</font></th></tr>
181   <tr height="5"></tr
182   <tr valign=top>
183     <td>
184       <table>
185         <tr>
186           <th align=right>|.$locale->text('From').qq|</th>
187           <td><input name=fromdate size=11 title="$myconfig{dateformat}"></td>
188           <th align=right>|.$locale->text('to').qq|</th>
189           <td><input name=todate size=11 title="$myconfig{dateformat}"></td>
190         </tr>
191         <tr>
192           <th align=right>|.$locale->text('Include in Report').qq|</th>
193           <td colspan=3>
194           <input name=l_subtotal class=checkbox type=checkbox value=Y>&nbsp;|.$locale->text('Subtotal').qq|</td>
195         </tr>
196       </table>
197     </td>
198   </tr>
199   <tr><td><hr size=3 noshade></td></tr>
200 </table>
201
202 <input type=hidden name=login value=$form->{login}>
203 <input type=hidden name=path value=$form->{path}>
204 <input type=hidden name=password value=$form->{password}>
205
206 <br><input class=submit type=submit name=action value="|.$locale->text('List Transactions').qq|">
207 </form>
208
209 </body>
210 </html>
211 |;
212
213 }
214
215
216 sub list_transactions {
217
218   CA->all_transactions(\%myconfig, \%$form);
219
220   $description = $form->escape($form->{description});
221   $gifi_description = $form->escape($form->{gifi_description});
222
223   # construct href
224   $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}";
225
226   # construct callback
227   $callback = "rp.pl?path=$form->{path}&action=generate_trial_balance&login=$form->{login}&password=$form->{password}&fromdate=$form->{fromdate}&todate=$form->{todate}&l_heading=$form->{l_heading}&l_subtotal=$form->{l_subtotal}&accounttype=$form->{accounttype}";
228
229   # figure out which column comes first
230   $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>|.$locale->text('Date').qq|</a></th>|;
231   $column_header{reference} = qq|<th><a class=listheading href=$href&sort=reference>|.$locale->text('Reference').qq|</a></th>|;
232   $column_header{description} = qq|<th><a class=listheading href=$href&sort=description>|.$locale->text('Description').qq|</a></th>|;
233   $column_header{debit} = qq|<th class=listheading>|.$locale->text('Debit').qq|</th>|;
234   $column_header{credit} = qq|<th class=listheading>|.$locale->text('Credit').qq|</th>|;
235   $column_header{balance} = qq|<th class=listheading>|.$locale->text('Balance').qq|</th>|;
236
237   @column_index = $form->sort_columns(qw(transdate reference description debit credit));
238   
239   if ($form->{accounttype} eq 'gifi') {
240     map { $form->{$_} = $form->{"gifi_$_"} } qw(accno description);
241   }
242   if ($form->{accno}) {
243     push @column_index, "balance";
244   }
245     
246   $form->{title} = ($form->{accounttype} eq 'gifi') ? $locale->text('GIFI') : $locale->text('Account');
247   
248   $form->{title} .= " $form->{accno} - $form->{description}";
249  
250  
251   if ($form->{fromdate} || $form->{todate}) {
252     if ($form->{fromdate}) {
253       $fromdate = $locale->date(\%myconfig, $form->{fromdate}, 1);
254     }
255     if ($form->{todate}) {
256       $todate = $locale->date(\%myconfig, $form->{todate}, 1);
257     }
258     
259     $form->{period} = "$fromdate - $todate";
260   } else {
261     $form->{period} = $locale->date(\%myconfig, $form->current_date(\%myconfig),1);
262   }
263
264   
265   $form->header;
266
267   print qq|
268 <body>
269
270 <table width=100%>
271   <tr>
272     <th class=listtop>$form->{title}</font></th>
273   </tr>
274   <tr height="5"></tr>
275   <tr>
276     <td>$form->{period}</td>
277   </tr>
278   <tr>
279     <td>
280       <table width=100%>
281        <tr class=listheading>
282 |;
283
284 map { print "$column_header{$_}\n" } @column_index;
285
286 print qq|
287        </tr>
288 |;
289
290   # add sort to callback
291   $callback = $form->escape($callback . "&sort=$form->{sort}");
292
293   if (@{ $form->{CA} }) {
294     $sameitem = $form->{CA}->[0]->{$form->{sort}};
295   }
296
297   $ml = ($form->{category} =~ /(A|E)/) ? -1 : 1;
298   if ($form->{accno} && $form->{balance}) {
299     
300     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
301
302     $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</td>";
303     
304     $i++; $i %= 2;
305     print qq|
306         <tr class=listrow$i>
307 |;
308     map { print $column_data{$_} } @column_index;
309     print qq|
310        </tr>
311 |;
312   }
313     
314   foreach $ca (@{ $form->{CA} }) {
315
316     if ($form->{l_subtotal} eq 'Y') {
317       if ($sameitem ne $ca->{$form->{sort}}) {
318         &ca_subtotal;
319       }
320     }
321     
322     # construct link to source
323     $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>";
324
325     
326     $column_data{debit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{debit}, 2, "&nbsp;")."</td>";
327     $column_data{credit} = "<td align=right>".$form->format_amount(\%myconfig, $ca->{credit}, 2, "&nbsp;")."</td>";
328     
329     $form->{balance} += $ca->{amount};
330     $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</td>";
331
332     $subtotaldebit += $ca->{debit};
333     $subtotalcredit += $ca->{credit};
334     
335     $totaldebit += $ca->{debit};
336     $totalcredit += $ca->{credit};
337     
338     $column_data{transdate} = qq|<td>$ca->{transdate}</td>|;
339     $column_data{reference} = qq|<td>$href</td>|;
340     $column_data{description} = qq|<td>$ca->{description}</td>|;
341   
342     $i++; $i %= 2;
343     print qq|
344         <tr class=listrow$i>
345 |;
346
347     map { print $column_data{$_} } @column_index;
348
349     print qq|
350         </tr>
351 |;
352
353   }
354  
355
356   if ($form->{l_subtotal} eq 'Y') {
357     &ca_subtotal;
358   }
359  
360
361   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
362   
363   $column_data{debit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;")."</font></th>";
364   $column_data{credit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;")."</font></th>";
365   $column_data{balance} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</font></th>";
366
367   print qq|
368         <tr class=listtotal>
369 |;
370
371   map { print $column_data{$_} } @column_index;
372   
373   print qq|
374         </tr>
375       </table>
376     </td>
377   </tr>
378   <tr>
379     <td><hr size=3 noshade></td>
380   </tr>
381 </table>
382
383 </body>
384 </html>
385 |;
386   
387 }
388
389
390 sub ca_subtotal {
391
392   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
393   
394   $column_data{debit} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;") . "</font></th>";
395   $column_data{credit} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;") . "</font></th>";
396        
397   $subtotaldebit = 0;
398   $subtotalcredit = 0;
399
400   $sameitem = $ca->{$form->{sort}};
401
402   print qq|
403       <tr class=listsubtotal>
404 |;
405
406   map { print "$column_data{$_}\n" } @column_index;
407
408   print qq|
409       </tr>
410 |;
411
412 }
413