This commit was generated by cvs2svn to compensate for changes in r3880,
[freeside.git] / sql-ledger / sql-ledger / bin / mozilla / gl.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 # Genereal Ledger
26 #
27 #======================================================================
28
29
30 use SL::GL;
31 use SL::PE;
32
33 require "$form->{path}/arap.pl";
34
35 1;
36 # end of main
37
38
39 # this is for our long dates
40 # $locale->text('January')
41 # $locale->text('February')
42 # $locale->text('March')
43 # $locale->text('April')
44 # $locale->text('May ')
45 # $locale->text('June')
46 # $locale->text('July')
47 # $locale->text('August')
48 # $locale->text('September')
49 # $locale->text('October')
50 # $locale->text('November')
51 # $locale->text('December')
52
53 # this is for our short month
54 # $locale->text('Jan')
55 # $locale->text('Feb')
56 # $locale->text('Mar')
57 # $locale->text('Apr')
58 # $locale->text('May')
59 # $locale->text('Jun')
60 # $locale->text('Jul')
61 # $locale->text('Aug')
62 # $locale->text('Sep')
63 # $locale->text('Oct')
64 # $locale->text('Nov')
65 # $locale->text('Dec')
66
67
68 sub add {
69
70   $form->{title} = "Add";
71   
72   $form->{callback} = "$form->{script}?action=add&transfer=$form->{transfer}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}" unless $form->{callback};
73
74   # we use this only to set a default date
75   GL->transaction(\%myconfig, \%$form);
76   
77   map { $form->{selectaccno} .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{all_accno} };
78
79   if ($form->{all_projects}) {
80     $form->{selectprojectnumber} = "<option>\n";
81     map { $form->{selectprojectnumber} .= qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n| } @{ $form->{all_projects} };
82   }
83
84   
85   $form->{rowcount} = ($form->{transfer}) ? 2 : 9;
86
87   # departments
88   $form->all_departments(\%myconfig);
89   if (@{ $form->{all_departments} }) {
90     $form->{selectdepartment} = "<option>\n";
91
92     map { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| } (@{ $form->{all_departments} });
93   }
94  
95   &display_form;
96   
97 }
98
99
100 sub edit {
101
102   GL->transaction(\%myconfig, \%$form);
103
104   map { $form->{selectaccno} .= "<option>$_->{accno}--$_->{description}\n" } @{ $form->{all_accno} };
105   
106   # projects
107   if ($form->{all_projects}) {
108     $form->{selectprojectnumber} = "<option>\n";
109     map { $form->{selectprojectnumber} .= qq|<option value="$_->{projectnumber}--$_->{id}">$_->{projectnumber}\n| } @{ $form->{all_projects} };
110   }
111
112   
113   # departments
114   $form->all_departments(\%myconfig);
115   if (@{ $form->{all_departments} }) {
116
117     $form->{department} = "$form->{department}--$form->{department_id}";
118
119     $form->{selectdepartment} = "<option>\n";
120
121     map { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| } (@{ $form->{all_departments} });
122   }
123  
124   $form->{locked} = ($form->{revtrans}) ? '1' : ($form->datetonum($form->{transdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
125
126   # readonly
127   $form->{readonly} = 1 if $myconfig{acs} =~ /General Ledger--Add Transaction/; 
128
129   $form->{title} = "Edit";
130   
131   &form_header;
132
133   $i = 1;
134   foreach $ref (@{ $form->{GL} }) {
135     $form->{"accno_$i"} = "$ref->{accno}--$ref->{description}";
136
137     $form->{"projectnumber_$i"} = "$ref->{projectnumber}--$ref->{project_id}";
138     $form->{"fx_transaction_$i"} = $ref->{fx_transaction};
139     
140     if ($ref->{amount} < 0) {
141       $form->{totaldebit} -= $ref->{amount};
142       $form->{"debit_$i"} = $ref->{amount} * -1;
143     } else {
144       $form->{totalcredit} += $ref->{amount};
145       $form->{"credit_$i"} = $ref->{amount};
146     }
147
148     $i++;
149   }
150
151   $form->{rowcount} = $i;
152   
153   &display_rows;
154
155   &form_footer;
156   
157 }
158
159
160
161 sub search {
162
163   $form->{title} = $locale->text('General Ledger')." ".$locale->text('Reports');
164   
165   $form->all_departments(\%myconfig);
166   # departments
167   if (@{ $form->{all_departments} }) {
168     $form->{selectdepartment} = "<option>\n";
169
170     map { $form->{selectdepartment} .= qq|<option value="$_->{description}--$_->{id}">$_->{description}\n| } (@{ $form->{all_departments} });
171  
172     $department = qq|
173         <tr>
174           <th align=right nowrap>|.$locale->text('Department').qq|</th>
175           <td colspan=3><select name=department>$form->{selectdepartment}</select></td>
176         </tr>
177 |;
178   }
179
180
181   # accounting years
182   $form->{selectaccountingyear} = "<option>\n";
183   map { $form->{selectaccountingyear} .= qq|<option>$_\n| } @{ $form->{all_years} };
184   $form->{selectaccountingmonth} = "<option>\n";
185   map { $form->{selectaccountingmonth} .= qq|<option value=$_>|.$locale->text($form->{all_month}{$_}).qq|\n| } sort keys %{ $form->{all_month} };
186
187   $selectfrom = qq|
188         <tr>
189         <th align=right>|.$locale->text('Period').qq|</th>
190         <td colspan=3>
191         <select name=month>$form->{selectaccountingmonth}</select>
192         <select name=year>$form->{selectaccountingyear}</select>
193         <input name=interval class=radio type=radio value=0 checked>|.$locale->text('Current').qq|
194         <input name=interval class=radio type=radio value=1>|.$locale->text('Month').qq|
195         <input name=interval class=radio type=radio value=3>|.$locale->text('Quarter').qq|
196         <input name=interval class=radio type=radio value=12>|.$locale->text('Year').qq|
197         </td>
198       </tr>
199 |;
200
201   
202   $form->header;
203
204   print qq|
205 <body>
206
207 <form method=post action=$form->{script}>
208
209 <input type=hidden name=sort value=transdate>
210
211 <table width=100%>
212   <tr>
213     <th class=listtop>$form->{title}</th>
214   </tr>
215   <tr height="5"></tr>
216   <tr>
217     <td>
218       <table>
219         <tr>
220           <th align=right>|.$locale->text('Reference').qq|</th>
221           <td><input name=reference size=20></td>
222           <th align=right>|.$locale->text('Source').qq|</th>
223           <td><input name=source size=20></td>
224         </tr>
225         $department
226         <tr>
227           <th align=right>|.$locale->text('Description').qq|</th>
228           <td colspan=3><input name=description size=40></td>
229         </tr>
230         <tr>
231           <th align=right>|.$locale->text('Notes').qq|</th>
232           <td colspan=3><input name=notes size=40></td>
233         </tr>
234         <tr>
235           <th align=right>|.$locale->text('From').qq|</th>
236           <td><input name=datefrom size=11 title="$myconfig{dateformat}"></td>
237           <th align=right>|.$locale->text('To').qq|</th>
238           <td><input name=dateto size=11 title="$myconfig{dateformat}"></td>
239         </tr>
240         $selectfrom
241         <tr>
242           <th align=right>|.$locale->text('Amount').qq| >=</th>
243           <td><input name=amountfrom size=11></td>
244           <th align=right>|.$locale->text('Amount').qq| <=</th>
245           <td><input name=amountto size=11></td>
246         </tr>
247         <tr>
248           <th align=right>|.$locale->text('Include in Report').qq|</th>
249           <td colspan=3>
250             <table>
251               <tr>
252                 <td>
253                   <input name="category" class=radio type=radio value=X checked>&nbsp;|.$locale->text('All').qq|
254                   <input name="category" class=radio type=radio value=A>&nbsp;|.$locale->text('Asset').qq|
255                   <input name="category" class=radio type=radio value=C>&nbsp;|.$locale->text('Contra').qq|
256                   <input name="category" class=radio type=radio value=L>&nbsp;|.$locale->text('Liability').qq|
257                   <input name="category" class=radio type=radio value=Q>&nbsp;|.$locale->text('Equity').qq|
258                   <input name="category" class=radio type=radio value=I>&nbsp;|.$locale->text('Income').qq|
259                   <input name="category" class=radio type=radio value=E>&nbsp;|.$locale->text('Expense').qq|
260                 </td>
261               </tr>
262               <tr>
263                 <table>
264                   <tr>
265                     <td align=right><input name="l_id" class=checkbox type=checkbox value=Y></td>
266                     <td>|.$locale->text('ID').qq|</td>
267                     <td align=right><input name="l_transdate" class=checkbox type=checkbox value=Y checked></td>
268                     <td>|.$locale->text('Date').qq|</td>
269                     <td align=right><input name="l_reference" class=checkbox type=checkbox value=Y checked></td>
270                     <td>|.$locale->text('Reference').qq|</td>
271                     <td align=right><input name="l_description" class=checkbox type=checkbox value=Y checked></td>
272                     <td>|.$locale->text('Description').qq|</td>
273                     <td align=right><input name="l_notes" class=checkbox type=checkbox value=Y></td>
274                     <td>|.$locale->text('Notes').qq|</td>
275                   </tr>
276                   <tr>
277                     <td align=right><input name="l_debit" class=checkbox type=checkbox value=Y checked></td>
278                     <td>|.$locale->text('Debit').qq|</td>
279                     <td align=right><input name="l_credit" class=checkbox type=checkbox value=Y checked></td>
280                     <td>|.$locale->text('Credit').qq|</td>
281                     <td align=right><input name="l_source" class=checkbox type=checkbox value=Y checked></td>
282                     <td>|.$locale->text('Source').qq|</td>
283                     <td align=right><input name="l_accno" class=checkbox type=checkbox value=Y checked></td>
284                     <td>|.$locale->text('Account').qq|</td>
285                     <td align=right><input name="l_gifi_accno" class=checkbox type=checkbox value=Y></td>
286                     <td>|.$locale->text('GIFI').qq|</td>
287                   </tr>
288                   <tr>
289                     <td align=right><input name="l_subtotal" class=checkbox type=checkbox value=Y></td>
290                     <td>|.$locale->text('Subtotal').qq|</td>
291                   </tr>
292                 </table>
293               </tr>
294             </table>
295         </tr>
296       </table>
297     </td>
298   </tr>
299   <tr>
300     <td><hr size=3 noshade></td>
301   </tr>
302 </table>
303
304 <input type=hidden name=nextsub value=generate_report>
305
306 <input type=hidden name=path value=$form->{path}>
307 <input type=hidden name=login value=$form->{login}>
308 <input type=hidden name=sessionid value=$form->{sessionid}>
309
310 <br>
311 <input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
312 </form>
313
314 </body>
315 </html>
316 |;
317 }
318
319
320 sub generate_report {
321
322   $form->{sort} = "transdate" unless $form->{sort};
323
324   GL->all_transactions(\%myconfig, \%$form);
325   
326   $href = "$form->{script}?action=generate_report&direction=$form->{direction}&oldsort=$form->{oldsort}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
327   
328   $form->sort_order();
329
330   $callback = "$form->{script}?action=generate_report&direction=$form->{direction}&oldsort=$form->{oldsort}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}";
331   
332   %acctype = ( 'A' => $locale->text('Asset'),
333                'C' => $locale->text('Contra'),
334                'L' => $locale->text('Liability'),
335                'Q' => $locale->text('Equity'),
336                'I' => $locale->text('Income'),
337                'E' => $locale->text('Expense'),
338              );
339   
340   $form->{title} = $locale->text('General Ledger');
341   
342   $ml = ($form->{ml} =~ /(A|E)/) ? -1 : 1;
343
344   unless ($form->{category} eq 'X') {
345     $form->{title} .= " : ".$locale->text($acctype{$form->{category}});
346   }
347   if ($form->{accno}) {
348     $href .= "&accno=".$form->escape($form->{accno});
349     $callback .= "&accno=".$form->escape($form->{accno},1);
350     $option = $locale->text('Account')." : $form->{accno} $form->{account_description}";
351   }
352   if ($form->{gifi_accno}) {
353     $href .= "&gifi_accno=".$form->escape($form->{gifi_accno});
354     $callback .= "&gifi_accno=".$form->escape($form->{gifi_accno},1);
355     $option .= "\n<br>" if $option;
356     $option .= $locale->text('GIFI')." : $form->{gifi_accno} $form->{gifi_account_description}";
357   }
358   if ($form->{source}) {
359     $href .= "&source=".$form->escape($form->{source});
360     $callback .= "&source=".$form->escape($form->{source},1);
361     $option .= "\n<br>" if $option;
362     $option .= $locale->text('Source')." : $form->{source}";
363   }
364   if ($form->{reference}) {
365     $href .= "&reference=".$form->escape($form->{reference});
366     $callback .= "&reference=".$form->escape($form->{reference},1);
367     $option .= "\n<br>" if $option;
368     $option .= $locale->text('Reference')." : $form->{reference}";
369   }
370   if ($form->{department}) {
371     $href .= "&department=".$form->escape($form->{department});
372     $callback .= "&department=".$form->escape($form->{department},1);
373     ($department) = split /--/, $form->{department};
374     $option .= "\n<br>" if $option;
375     $option .= $locale->text('Department')." : $department";
376   }
377
378   if ($form->{description}) {
379     $href .= "&description=".$form->escape($form->{description});
380     $callback .= "&description=".$form->escape($form->{description},1);
381     $option .= "\n<br>" if $option;
382     $option .= $locale->text('Description')." : $form->{description}";
383   }
384   if ($form->{notes}) {
385     $href .= "&notes=".$form->escape($form->{notes});
386     $callback .= "&notes=".$form->escape($form->{notes},1);
387     $option .= "\n<br>" if $option;
388     $option .= $locale->text('Notes')." : $form->{notes}";
389   }
390    
391   if ($form->{datefrom}) {
392     $href .= "&datefrom=$form->{datefrom}";
393     $callback .= "&datefrom=$form->{datefrom}";
394     $option .= "\n<br>" if $option;
395     $option .= $locale->text('From')." ".$locale->date(\%myconfig, $form->{datefrom}, 1);
396   }
397   if ($form->{dateto}) {
398     $href .= "&dateto=$form->{dateto}";
399     $callback .= "&dateto=$form->{dateto}";
400     if ($form->{datefrom}) {
401       $option .= " ";
402     } else {
403       $option .= "\n<br>" if $option;
404     }
405     $option .= $locale->text('To')." ".$locale->date(\%myconfig, $form->{dateto}, 1);
406   }
407   
408   if ($form->{amountfrom}) {
409     $href .= "&amountfrom=$form->{amountfrom}";
410     $callback .= "&amountfrom=$form->{amountfrom}";
411     $option .= "\n<br>" if $option;
412     $option .= $locale->text('Amount')." >= ".$form->format_amount(\%myconfig, $form->{amountfrom}, 2);
413   }
414   if ($form->{amountto}) {
415     $href .= "&amountto=$form->{amountto}";
416     $callback .= "&amountto=$form->{amountto}";
417     if ($form->{amountfrom}) {
418       $option .= " <= ";
419     } else {
420       $option .= "\n<br>" if $option;
421       $option .= $locale->text('Amount')." <= ";
422     }
423     $option .= $form->format_amount(\%myconfig, $form->{amountto}, 2);
424   }
425
426
427   @columns = $form->sort_columns(qw(transdate id reference description notes source debit credit accno gifi_accno));
428
429   if ($form->{link} =~ /_paid/) {
430     @columns = $form->sort_columns(qw(transdate id reference description notes source cleared debit credit accno gifi_accno));
431     $form->{l_cleared} = "Y";
432   }
433
434   if ($form->{accno} || $form->{gifi_accno}) {
435     @columns = grep !/(accno|gifi_accno)/, @columns;
436     push @columns, "balance";
437     $form->{l_balance} = "Y";
438   }
439   
440   
441   foreach $item (@columns) {
442     if ($form->{"l_$item"} eq "Y") {
443       push @column_index, $item;
444
445       # add column to href and callback
446       $callback .= "&l_$item=Y";
447       $href .= "&l_$item=Y";
448     }
449   }
450   
451   if ($form->{l_subtotal} eq 'Y') {
452     $callback .= "&l_subtotal=Y";
453     $href .= "&l_subtotal=Y";
454   }
455
456   $callback .= "&category=$form->{category}";
457   $href .= "&category=$form->{category}";
458
459   $column_header{id} = "<th><a class=listheading href=$href&sort=id>".$locale->text('ID')."</a></th>";
460   $column_header{transdate} = "<th><a class=listheading href=$href&sort=transdate>".$locale->text('Date')."</a></th>";
461   $column_header{reference} = "<th><a class=listheading href=$href&sort=reference>".$locale->text('Reference')."</a></th>";
462   $column_header{source} = "<th><a class=listheading href=$href&sort=source>".$locale->text('Source')."</a></th>";
463   $column_header{description} = "<th><a class=listheading href=$href&sort=description>".$locale->text('Description')."</a></th>";
464   $column_header{notes} = "<th class=listheading>".$locale->text('Notes')."</th>";
465   $column_header{debit} = "<th class=listheading>".$locale->text('Debit')."</th>";
466   $column_header{credit} = "<th class=listheading>".$locale->text('Credit')."</th>";
467   $column_header{accno} = "<th><a class=listheading href=$href&sort=accno>".$locale->text('Account')."</a></th>";
468   $column_header{gifi_accno} = "<th><a class=listheading href=$href&sort=gifi_accno>".$locale->text('GIFI')."</a></th>";
469   $column_header{balance} = "<th>".$locale->text('Balance')."</th>";
470   $column_header{cleared} = qq|<th>|.$locale->text('R').qq|</th>|;
471  
472   $form->header;
473
474   print qq|
475 <body>
476
477 <table width=100%>
478   <tr>
479     <th class=listtop>$form->{title}</th>
480   </tr>
481   <tr height="5"></tr>
482   <tr>
483     <td>$option</td>
484   </tr>
485   <tr>
486     <td>
487       <table width=100%>
488         <tr class=listheading>
489 |;
490
491 map { print "$column_header{$_}\n" } @column_index;
492
493 print "
494         </tr>
495 ";
496   
497   # add sort to callback
498   $form->{callback} = "$callback&sort=$form->{sort}";
499   $callback = $form->escape($form->{callback});
500   
501   # initial item for subtotals
502   if (@{ $form->{GL} }) {
503     $sameitem = $form->{GL}->[0]->{$form->{sort}};
504   }
505   
506   if (($form->{accno} || $form->{gifi_accno}) && $form->{balance}) {
507
508     map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
509     $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</td>";
510     
511     $i++; $i %= 2;
512     print qq|
513         <tr class=listrow$i>
514 |;
515     map { print "$column_data{$_}\n" } @column_index;
516     
517     print qq|
518         </tr>
519 |;
520   }
521
522   # reverse href
523   $direction = ($form->{direction} eq 'ASC') ? "ASC" : "DESC";
524   $form->sort_order();
525   $href =~ s/direction=$form->{direction}/direction=$direction/;
526     
527   foreach $ref (@{ $form->{GL} }) {
528
529     # if item ne sort print subtotal
530     if ($form->{l_subtotal} eq 'Y') {
531       if ($sameitem ne $ref->{$form->{sort}}) {
532         &gl_subtotal;
533       }
534     }
535     
536     $form->{balance} += $ref->{amount};
537     
538     $subtotaldebit += $ref->{debit};
539     $subtotalcredit += $ref->{credit};
540     
541     $totaldebit += $ref->{debit};
542     $totalcredit += $ref->{credit};
543
544     $ref->{debit} = $form->format_amount(\%myconfig, $ref->{debit}, 2, "&nbsp;");
545     $ref->{credit} = $form->format_amount(\%myconfig, $ref->{credit}, 2, "&nbsp;");
546     
547     $column_data{id} = "<td>$ref->{id}</td>";
548     $column_data{transdate} = "<td>$ref->{transdate}</td>";
549     $column_data{reference} = "<td><a href=$ref->{module}.pl?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&sessionid=$form->{sessionid}&callback=$callback>$ref->{reference}</td>";
550     $column_data{description} = "<td>$ref->{description}&nbsp;</td>";
551     $column_data{source} = "<td>$ref->{source}&nbsp;</td>";
552     $column_data{notes} = "<td>$ref->{notes}&nbsp;</td>";
553     $column_data{debit} = "<td align=right>$ref->{debit}</td>";
554     $column_data{credit} = "<td align=right>$ref->{credit}</td>";
555     $column_data{accno} = "<td><a href=$href&accno=$ref->{accno}&callback=$callback>$ref->{accno}</a></td>";
556     $column_data{gifi_accno} = "<td><a href=$href&gifi_accno=$ref->{gifi_accno}&callback=$callback>$ref->{gifi_accno}</a>&nbsp;</td>";
557     $column_data{balance} = "<td align=right>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</td>";
558     $column_data{cleared} = ($ref->{cleared}) ? "<td>*</td>" : "<td>&nbsp;</td>";
559
560     $i++; $i %= 2;
561     print "
562         <tr class=listrow$i>";
563     map { print "$column_data{$_}\n" } @column_index;
564     print "</tr>";
565     
566   }
567
568
569   &gl_subtotal if ($form->{l_subtotal} eq 'Y');
570
571
572   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
573   
574   $column_data{debit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totaldebit, 2, "&nbsp;")."</th>";
575   $column_data{credit} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $totalcredit, 2, "&nbsp;")."</th>";
576   $column_data{balance} = "<th align=right class=listtotal>".$form->format_amount(\%myconfig, $form->{balance} * $ml, 2, 0)."</th>";
577   
578   print qq|
579         <tr class=listtotal>
580 |;
581
582   map { print "$column_data{$_}\n" } @column_index;
583
584   $i = 1;
585   if ($myconfig{acs} !~ /GL--GL/) {
586     $button{'GL--Add Transaction'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('GL Transaction').qq|"> |;
587     $button{'GL--Add Transaction'}{order} = $i++;
588   }
589   if ($myconfig{acs} !~ /AR--AR/) {
590     $button{'AR--Add Transaction'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('AR Transaction').qq|"> |;
591     $button{'AR--Add Transaction'}{order} = $i++;
592     $button{'AR--Sales Invoice'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Sales Invoice ').qq|"> |;
593     $button{'AR--Sales Invoice'}{order} = $i++;
594   }
595   if ($myconfig{acs} !~ /AP--AP/) {
596     $button{'AP--Add Transaction'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('AP Transaction').qq|"> |;
597     $button{'AP--Add Transaction'}{order} = $i++;
598     $button{'AP--Vendor Invoice'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Vendor Invoice ').qq|"> |;
599     $button{'AP--Vendor Invoice'}{order} = $i++;
600   }
601
602   foreach $item (split /;/, $myconfig{acs}) {
603     delete $button{$item};
604   }
605   
606   print qq|
607         </tr>
608       </table>
609     </td>
610   </tr>
611   <tr>
612     <td><hr size=3 noshade></td>
613   </tr>
614 </table>
615
616 <br>
617
618 <form method=post action=$form->{script}>
619
620 <input name=callback type=hidden value="$form->{callback}">
621
622 <input type=hidden name=path value=$form->{path}>
623 <input type=hidden name=login value=$form->{login}>
624 <input type=hidden name=sessionid value=$form->{sessionid}>
625 |;
626
627   foreach $item (sort { $a->{order} <=> $b->{order} } %button) {
628     print $item->{code};
629   }
630
631   if ($form->{menubar}) {
632     require "$form->{path}/menu.pl";
633     &menubar;
634   }
635
636   print qq|
637
638 </form>
639
640 </body>
641 </html>
642 |;
643
644 }
645
646
647 sub gl_subtotal {
648       
649   $subtotaldebit = $form->format_amount(\%myconfig, $subtotaldebit, 2, "&nbsp;");
650   $subtotalcredit = $form->format_amount(\%myconfig, $subtotalcredit, 2, "&nbsp;");
651   
652   map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
653
654   $column_data{debit} = "<th align=right class=listsubtotal>$subtotaldebit</td>";
655   $column_data{credit} = "<th align=right class=listsubtotal>$subtotalcredit</td>";
656
657   
658   print "<tr class=listsubtotal>";
659   map { print "$column_data{$_}\n" } @column_index;
660   print "</tr>";
661
662   $subtotaldebit = 0;
663   $subtotalcredit = 0;
664
665   $sameitem = $ref->{$form->{sort}};
666
667 }
668
669
670 sub update {
671
672   @a = ();
673   $count = 0;
674   @flds = qw(accno debit credit projectnumber);
675
676   for $i (1 .. $form->{rowcount}) {
677     unless (($form->{"debit_$i"} eq "") && ($form->{"credit_$i"} eq "")) {
678       # take accno apart
679       ($form->{"accno_$i"}) = split(/--/, $form->{"accno_$i"});
680       map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(debit credit);
681       
682       push @a, {};
683       $j = $#a;
684       
685       map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
686       $count++;
687     }
688   }
689
690
691   for $i (1 .. $count) {
692     $j = $i - 1;
693     map { $form->{"${_}_$i"} = $a[$j]->{$_} } @flds;
694   }
695
696   for $i ($count + 1 .. $form->{rowcount}) {
697     map { delete $form->{"${_}_$i"} } @flds;
698   }
699
700   $form->{rowcount} = $count + 1;
701
702   &display_form;
703   
704 }
705
706
707 sub display_form {
708
709   &form_header;
710   &display_rows;
711   &form_footer;
712
713 }
714
715
716 sub display_rows {
717
718   $form->{selectprojectnumber} = $form->unescape($form->{selectprojectnumber});
719   
720   $form->{totaldebit} = 0;
721   $form->{totalcredit} = 0;
722
723   for $i (1 .. $form->{rowcount}) {
724  
725     $form->{totaldebit} += $form->{"debit_$i"};
726     $form->{totalcredit} += $form->{"credit_$i"};
727
728     map { $form->{"${_}_$i"} = ($form->{"${_}_$i"}) ? $form->format_amount(\%myconfig, $form->{"${_}_$i"}, 2) : "" } qw(debit credit);
729
730     $selectaccno = $form->{selectaccno};
731     $selectaccno =~ s/option>\Q$form->{"accno_$i"}\E/option selected>$form->{"accno_$i"}/;
732
733     if ($form->{selectprojectnumber}) {
734       $selectprojectnumber = $form->{selectprojectnumber};
735       $selectprojectnumber =~ s/(<option value="$form->{"projectnumber_$i"}")/$1 selected/;
736       
737       $project = qq|
738   <td><select name="projectnumber_$i">$selectprojectnumber</select></td>|;
739     }
740     
741   
742     if ($form->{transfer}) {
743       $form->{"fx_transaction_$i"} = ($form->{"fx_transaction_$i"}) ? "checked" : "";
744       $fx_transaction = qq|
745   <td><input name="fx_transaction_$i" class=checkbox type=checkbox value=1 $form->{"fx_transaction_$i"}></td>
746 |;
747     } else {
748       $fx_transaction = qq|
749     <input type=hidden name="fx_transaction_$i" value=$form->{"fx_transaction_$i"}>
750 |;
751     }
752     
753
754     print qq|<tr>
755   <td><select name="accno_$i">$selectaccno</select></td>
756   $fx_transaction
757   <td><input name="debit_$i" size=12 value=$form->{"debit_$i"}></td>
758   <td><input name="credit_$i" size=12 value=$form->{"credit_$i"}></td>
759   $project
760 </tr>
761
762 |;
763   }
764
765
766   print qq|
767 <input type=hidden name=rowcount value=$form->{rowcount}>
768 <input type=hidden name=selectaccno value="$form->{selectaccno}">
769 <input type=hidden name=selectprojectnumber value="|.$form->escape($form->{selectprojectnumber},1).qq|">|;
770
771 }
772
773
774 sub form_header {
775
776   $title = $form->{title};
777   if ($form->{transfer}) {
778     $form->{title} = $locale->text("$title Cash Transfer Transaction");
779   } else {
780     $form->{title} = $locale->text("$title General Ledger Transaction");
781   }
782     
783 # $locale->text('Add Cash Transfer Transaction')
784 # $locale->text('Edit Cash Transfer Transaction')
785 # $locale->text('Add General Ledger Transaction')
786 # $locale->text('Edit General Ledger Transaction')
787   
788
789   $form->{selectdepartment} = $form->unescape($form->{selectdepartment});
790   $form->{selectdepartment} =~ s/ selected//;
791   $form->{selectdepartment} =~ s/(<option value="\Q$form->{department}\E")/$1 selected/;
792
793   map { $form->{$_} = $form->quote($form->{$_}) } qw(reference description notes);
794
795   if (($rows = $form->numtextrows($form->{description}, 50)) > 1) {
796     $description = qq|<textarea name=description rows=$rows cols=50 wrap=soft>$form->{description}</textarea>|;
797   } else {
798     $description = qq|<input name=description size=50 value="$form->{description}">|;
799   }
800   
801   if (($rows = $form->numtextrows($form->{notes}, 50)) > 1) {
802     $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
803   } else {
804     $notes = qq|<input name=notes size=50 value="$form->{notes}">|;
805   }
806   
807   $department = qq|
808         <tr>
809           <th align=right nowrap>|.$locale->text('Department').qq|</th>
810           <td><select name=department>$form->{selectdepartment}</select></td>
811           <input type=hidden name=selectdepartment value="|.$form->escape($form->{selectdepartment},1).qq|">
812         </tr>
813 | if $form->{selectdepartment};
814
815   $project = qq| 
816           <th class=listheading>|.$locale->text('Project').qq|</th>
817 | if $form->{selectprojectnumber};
818
819   if ($form->{transfer}) {
820     $fx_transaction = qq|
821           <th class=listheading>|.$locale->text('FX').qq|</th>
822 |;
823   }
824   
825   $form->header;
826
827   print qq|
828 <body>
829
830 <form method=post action=$form->{script}>
831
832 <input name=id type=hidden value=$form->{id}>
833
834 <input type=hidden name=transfer value=$form->{transfer}>
835
836 <input type=hidden name=selectaccno value="$form->{selectaccno}">
837
838 <input type=hidden name=closedto value=$form->{closedto}>
839 <input type=hidden name=locked value=$form->{locked}>
840 <input type=hidden name=title value="$title">
841
842 <table width=100%>
843   <tr>
844     <th class=listtop>$form->{title}</th>
845   </tr>
846   <tr height="5"></tr>
847   <tr>
848     <td>
849       <table width=100%>
850         <tr>
851           <th align=right>|.$locale->text('Reference').qq|</th>
852           <td><input name=reference size=20 value="$form->{reference}"></td>
853           <th align=right>|.$locale->text('Date').qq|</th>
854           <td><input name=transdate size=11 title="$myconfig{dateformat}" value=$form->{transdate}></td>
855         </tr>
856         $department
857         <tr>
858           <th align=right>|.$locale->text('Description').qq|</th>
859           <td colspan=3>$description</td>
860         </tr>
861         <tr>
862           <th align=right>|.$locale->text('Notes').qq|</th>
863           <td colspan=3>$notes</td>
864         </tr>
865       </table>
866     </td>
867   </tr>
868   <tr>
869     <td>
870       <table width=100%>
871         <tr class=listheading>
872           <th class=listheading>|.$locale->text('Account').qq|</th>
873           $fx_transaction
874           <th class=listheading>|.$locale->text('Debit').qq|</th>
875           <th class=listheading>|.$locale->text('Credit').qq|</th>
876           $project
877         </tr>
878 |;
879
880 }
881
882
883 sub form_footer {
884
885   ($dec) = ($form->{totaldebit} =~ /\.(\d+)/);
886   $dec = length $dec;
887   $decimalplaces = ($dec > 2) ? $dec : 2;
888   
889   map { $form->{$_} = $form->format_amount(\%myconfig, $form->{$_}, $decimalplaces, "&nbsp;") } qw(totaldebit totalcredit);
890
891
892   $project = qq|
893           <th>&nbsp;</th>
894 | if $form->{selectprojectnumber};
895
896   if ($form->{transfer}) {
897     $fx_transaction = qq|
898           <th>&nbsp;</th>
899 |;
900   }
901     
902   print qq|
903         <tr class=listtotal>
904           <th>&nbsp;</th>
905           $fx_transaction
906           <th class=listtotal align=right>$form->{totaldebit}</th>
907           <th class=listtotal align=right>$form->{totalcredit}</th>
908           $project
909         </tr>
910       </table>
911     </td>
912   </tr>
913   <tr>
914     <td><hr size=3 noshade></td>
915   </tr>
916 </table>
917
918 <input type=hidden name=path value=$form->{path}>
919 <input type=hidden name=login value=$form->{login}>
920 <input type=hidden name=sessionid value=$form->{sessionid}>
921
922 <input name=callback type=hidden value="$form->{callback}">
923
924 <br>
925 |;
926
927   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
928   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
929   
930   if (! $form->{readonly}) {
931     
932     if ($form->{id}) {
933       print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
934 |;
935
936       if (!$form->{locked}) {
937         print qq|
938         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">
939         <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
940 |;
941       }
942
943       print qq|
944         <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
945 |;
946       
947     } else {
948       if ($transdate > $closedto) {
949         print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
950         <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
951       }
952     }
953   }
954
955   if ($form->{menubar}) {
956     require "$form->{path}/menu.pl";
957     &menubar;
958   }
959   
960   print "
961   </form>
962
963 </body>
964 </html>
965 ";
966   
967 }
968
969
970 sub delete {
971
972   $form->header;
973
974   print qq|
975 <body>
976
977 <form method=post action=$form->{script}>
978 |;
979
980   $form->hide_form();
981   
982   print qq|
983 <h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
984
985 <h4>|.$locale->text('Are you sure you want to delete Transaction').qq| $form->{reference}</h4>
986
987 <input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
988 </form>
989 |;
990
991 }
992
993
994 sub yes {
995
996   $form->redirect($locale->text('Transaction deleted!')) if (GL->delete_transaction(\%myconfig, \%$form));
997   $form->error($locale->text('Cannot delete transaction!'));
998   
999 }
1000
1001
1002 sub post {
1003
1004   # check if there is something in reference and date
1005   $form->isblank("reference", $locale->text('Reference missing!'));
1006   $form->isblank("transdate", $locale->text('Transaction Date missing!'));
1007
1008   $transdate = $form->datetonum($form->{transdate}, \%myconfig);
1009   $closedto = $form->datetonum($form->{closedto}, \%myconfig);
1010
1011   
1012   $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $closedto);
1013
1014   # add up debits and credits
1015   if (!$form->{adjustment}) {
1016     for $i (1 .. $form->{rowcount}) {
1017       $debit += $form->parse_amount(\%myconfig, $form->{"debit_$i"});
1018       $credit += $form->parse_amount(\%myconfig, $form->{"credit_$i"});
1019     }
1020     
1021     if ($form->round_amount($debit, 2) != $form->round_amount($credit, 2)) {
1022       &post_adjustment;
1023       exit;
1024     }
1025   }
1026
1027   $form->redirect($locale->text('Transaction posted!')) if GL->post_transaction(\%myconfig, \%$form);
1028   $form->error($locale->text('Cannot post transaction!'));
1029   
1030 }
1031
1032
1033 sub post_as_new {
1034
1035   $form->{id} = 0;
1036   &post;
1037
1038 }
1039
1040
1041 sub post_adjustment {
1042
1043   $form->header;
1044
1045   print qq|
1046 <body>
1047
1048 <form method=post action=$form->{script}>
1049
1050 <input type=hidden name=adjustment value=1>
1051 |;
1052
1053   $form->hide_form();
1054   
1055   print qq|
1056 <h2 class=confirm>|.$locale->text('Warning!').qq|</h2>
1057
1058 <h4>|.$locale->text('Out of balance transaction!').qq|</h4>
1059
1060 <input name=action class=submit type=submit value="|.$locale->text('Post').qq|">
1061 </form>
1062 |;
1063
1064 }
1065
1066