1#=====================================================================
2# SQL-Ledger
3# Copyright (c) DWS Systems Inc.
4#
5#  Author: DWS Systems Inc.
6#     Web: http://www.sql-ledger.com
7#
8#======================================================================
9#
10# AR / AP
11#
12#======================================================================
13
14
15# any custom scripts for this one
16if (-f "$form->{path}/custom/aa.pl") {
17      eval { require "$form->{path}/custom/aa.pl"; };
18}
19if (-f "$form->{path}/custom/$form->{login}/aa.pl") {
20      eval { require "$form->{path}/custom/$form->{login}/aa.pl"; };
21}
22
23use SL::VR;
24
251;
26# end of main
27
28
29# this is for our long dates
30# $locale->text('January')
31# $locale->text('February')
32# $locale->text('March')
33# $locale->text('April')
34# $locale->text('May ')
35# $locale->text('June')
36# $locale->text('July')
37# $locale->text('August')
38# $locale->text('September')
39# $locale->text('October')
40# $locale->text('November')
41# $locale->text('December')
42
43# this is for our short month
44# $locale->text('Jan')
45# $locale->text('Feb')
46# $locale->text('Mar')
47# $locale->text('Apr')
48# $locale->text('May')
49# $locale->text('Jun')
50# $locale->text('Jul')
51# $locale->text('Aug')
52# $locale->text('Sep')
53# $locale->text('Oct')
54# $locale->text('Nov')
55# $locale->text('Dec')
56
57# $locale->text('Add AR Transaction')
58# $locale->text('Edit AR Transaction')
59# $locale->text('Add AP Transaction')
60# $locale->text('Edit AP Transaction')
61# $locale->text('Add AP Voucher')
62# $locale->text('Edit AP Voucher')
63
64# $locale->text('Add Credit Note')
65# $locale->text('Edit Credit Note')
66# $locale->text('Add Debit Note')
67# $locale->text('Edit Debit Note')
68
69
70sub add {
71
72  &create_links;
73
74  %title = ( transaction => "$form->{ARAP} Transaction",
75             credit_note => 'Credit Note',
76	     debit_note => 'Debit Note' );
77
78  $arap = lc $form->{ARAP};
79
80  if ($form->{batch}) {
81    $title = "Add $form->{ARAP} Voucher";
82    $form->{title} = $locale->text($title);
83    $form->helpref("${arap}_voucher", $myconfig{countrycode});
84    if ($form->{batchdescription}) {
85      $form->{title} .= " / $form->{batchdescription}";
86    }
87  } else {
88    $title = "Add $title{$form->{type}}";
89    $form->{title} = $locale->text($title);
90    $form->helpref("${arap}_$form->{type}", $myconfig{countrycode});
91  }
92
93  $form->{callback} = "$form->{script}?action=add&type=$form->{type}&path=$form->{path}&login=$form->{login}" unless $form->{callback};
94
95  $form->{focus} = "amount_1";
96  &display_form;
97
98}
99
100
101sub edit {
102
103  &create_links;
104
105  %title = ( transaction => "$form->{ARAP} Transaction",
106             credit_note => 'Credit Note',
107	     debit_note => 'Debit Note' );
108
109  if ($form->{batch}) {
110    $title = "Edit $form->{ARAP} Voucher";
111    $form->{title} = $locale->text($title);
112    if ($form->{batchdescription}) {
113      $form->{title} .= " / $form->{batchdescription}";
114    }
115  } else {
116    $title = "Edit $title{$form->{type}}";
117    $form->{title} = $locale->text($title);
118  }
119
120  $arap = lc $form->{ARAP};
121
122  $form->helpref("${arap}_$form->{type}", $myconfig{countrycode});
123
124  &display_form;
125
126}
127
128
129sub display_form {
130
131  &form_header;
132  &form_footer;
133
134}
135
136
137sub create_links {
138
139  $readonly = $form->{readonly};
140  $form->create_links($form->{ARAP}, \%myconfig, $form->{vc});
141  $form->{readonly} ||= $readonly;
142
143  for (qw(duedate taxincluded terms cashdiscount discountterms payment_accno payment_method language_code exchangerate)) { $temp{$_} = $form->{$_} }
144  $temp{$form->{ARAP}} = $form->{$form->{ARAP}};
145
146  if (exists $form->{oldinvtotal} && $form->{oldinvtotal} < 0) {
147    $form->{type} = ($form->{vc} eq 'customer') ? 'credit_note' : 'debit_note';
148    for (qw(invtotal totalpaid)) { $form->{"old$_"} *= -1 }
149  }
150
151  $form->{type} ||= "transaction";
152  $form->{formname} ||= $form->{type};
153  $form->{format} ||= $myconfig{outputformat};
154
155  $form->{selectprinter} = "";
156  for (@{ $form->{all_printer} }) { $form->{selectprinter} .= "$_->{printer}\n" }
157  chomp $form->{selectprinter};
158
159  if ($myconfig{printer}) {
160    $form->{format} ||= "postscript";
161  } else {
162    $form->{format} ||= "pdf";
163  }
164  $form->{media} ||= $myconfig{printer};
165
166
167# $locale->text('Transaction')
168# $locale->text('Credit Note')
169# $locale->text('Debit Note')
170
171  %selectform = ( transaction => 'Transaction',
172                  credit_note => 'Credit Note',
173		  debit_note => 'Debit Note' );
174
175  $form->{selectformname} = qq|$form->{type}--|.$locale->text($selectform{$form->{type}});
176
177  if ($latex) {
178    if (!$form->{batch}) {
179      if ($form->{ARAP} eq 'AR') {
180	if ($form->{type} eq 'credit_note') {
181	  $form->{selectformname} .= qq|\ncheck--|.$locale->text('Check');
182	} else {
183	  $form->{selectformname} .= qq|\nreceipt--|.$locale->text('Receipt');
184	}
185      } else {
186	if ($form->{type} eq 'debit_note') {
187	  $form->{selectformname} .= qq|\nreceipt--|.$locale->text('Receipt');
188	} else {
189	  $form->{selectformname} .= qq|\ncheck--|.$locale->text('Check');
190	}
191      }
192    }
193  }
194
195  if (!$form->{batch}) {
196    if ($form->{ARAP} eq 'AR') {
197      if ($form->{type} eq 'transaction') {
198	$form->{selectformname} .= qq|\nremittance_voucher--|.$locale->text('Remittance Voucher') if $form->{remittancevoucher};
199      }
200    }
201  }
202
203  # currencies
204  @curr = split /:/, $form->{currencies};
205  $form->{defaultcurrency} = $curr[0];
206  chomp $form->{defaultcurrency};
207
208  for (@curr) { $form->{selectcurrency} .= "$_\n" }
209
210  AA->get_name(\%myconfig, \%$form);
211
212  $form->{currency} =~ s/ //g;
213  $form->{duedate} = $temp{duedate} if $temp{duedate};
214
215  if ($form->{id}) {
216    for (keys %temp) { $form->{$_} = $temp{$_} };
217  }
218
219  $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
220  $form->{"old$form->{vc}number"} = $form->{"$form->{vc}number"};
221  for (qw(transdate duedate currency)) { $form->{"old$_"} = $form->{$_} }
222
223  # customers/vendors
224  $form->{"select$form->{vc}"} = "";
225  if (@{ $form->{"all_$form->{vc}"} }) {
226    $form->{$form->{vc}} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
227    for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq|$_->{name}--$_->{id}\n| }
228  }
229
230  # departments
231  $form->{department} = "$form->{department}--$form->{department_id}" if $form->{department_id};
232  &rebuild_departments;
233
234  $form->{employee} = "$form->{employee}--$form->{employee_id}";
235  # sales staff
236  if (@{ $form->{all_employee} }) {
237    $form->{selectemployee} = "\n";
238    for (@{ $form->{all_employee} }) { $form->{selectemployee} .= qq|$_->{name}--$_->{id}\n| }
239  }
240
241  # projects
242  if (@{ $form->{all_project} }) {
243    $form->{selectprojectnumber} = "\n";
244    for (@{ $form->{all_project} }) { $form->{selectprojectnumber} .= qq|$_->{projectnumber}--$_->{id}\n| }
245  }
246
247  if (@{ $form->{all_language} }) {
248    $form->{selectlanguage} = "\n";
249    for (@{ $form->{all_language} }) { $form->{selectlanguage} .= qq|$_->{code}--$_->{description}\n| }
250  }
251
252  $form->{roundchange} = "=$form->{roundchange}";
253
254  # paymentmethod
255  if (@{ $form->{all_paymentmethod} }) {
256    $form->{selectpaymentmethod} = "\n";
257    $form->{paymentmethod} = "$form->{paymentmethod}--$form->{paymentmethod_id}" if $form->{paymentmethod_id};
258    for (@{ $form->{all_paymentmethod} }) {
259      $form->{selectpaymentmethod} .= qq|$_->{description}--$_->{id}\n|;
260      if ($_->{roundchange}) {
261	$form->{roundchange} .= ";$_->{description}--$_->{id}=$_->{roundchange}"
262      }
263    }
264  }
265
266  # references
267  &all_references;
268
269  $form->{"select$form->{vc}"} = $form->escape($form->{"select$form->{vc}"},1);
270  for (qw(formname currency department employee projectnumber language paymentmethod printer)) { $form->{"select$_"} = $form->escape($form->{"select$_"},1) }
271
272  $form->{roundchange} = $form->escape($form->{roundchange},1);
273
274  $netamount = 0;
275  $tax = 0;
276  $taxrate = 0;
277  $ml = ($form->{ARAP} eq 'AR') ? 1 : -1;
278  $ml *= -1 if $form->{type} =~ /_note/;
279
280  foreach $key (keys %{ $form->{"$form->{ARAP}_links"} }) {
281
282    $form->{"select$key"} = "";
283    foreach $ref (@{ $form->{"$form->{ARAP}_links"}{$key} }) {
284      if ($key eq "$form->{ARAP}_tax") {
285
286        $desc_taxrate = $form->{"${item}_rate"} * 100;
287
288	$form->{"select$form->{ARAP}_tax_$ref->{accno}"} = $form->escape("$ref->{accno}--$ref->{description} $desc_taxrate%",1);
289	next;
290      }
291      $form->{"select$key"} .= "$ref->{accno}--$ref->{description}\n";
292    }
293    $form->{"select$key"} = $form->escape($form->{"select$key"},1);
294
295    # if there is a value we have an old entry
296    for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
297
298      if ($key eq "$form->{ARAP}_paid") {
299	$form->{"$form->{ARAP}_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
300	$form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1 * $ml;
301	$form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
302	$form->{"olddatepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
303	$form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
304	$form->{"memo_$i"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
305
306	$form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
307	$form->{"cleared_$i"} = $form->{acc_trans}{$key}->[$i-1]->{cleared};
308	$form->{"vr_id_$i"} = $form->{acc_trans}{$key}->[$i-1]->{vr_id};
309
310	$form->{"paymentmethod_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{paymentmethod}--$form->{acc_trans}{$key}->[$i-1]->{paymentmethod_id}";
311
312	$form->{paidaccounts}++;
313
314      } elsif ($key eq "$form->{ARAP}_discount") {
315
316	$form->{"$form->{ARAP}_discount_paid"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
317	$form->{"discount_paid"} = $form->{acc_trans}{$key}->[0]->{amount} * -1 * $ml;
318	$form->{"discount_datepaid"} = $form->{acc_trans}{$key}->[0]->{transdate};
319	$form->{"olddiscount_datepaid"} = $form->{acc_trans}{$key}->[0]->{transdate};
320	$form->{"discount_source"} = $form->{acc_trans}{$key}->[0]->{source};
321	$form->{"discount_memo"} = $form->{acc_trans}{$key}->[0]->{memo};
322
323	$form->{"discount_exchangerate"} = $form->{acc_trans}{$key}->[0]->{exchangerate};
324	$form->{"discount_cleared"} = $form->{acc_trans}{$key}->[0]->{cleared};
325	$form->{"discount_paymentmethod"} = "$form->{acc_trans}{$key}->[0]->{paymentmethod_id}--$form->{acc_trans}{$key}->[0]->{paymentmethod}";
326
327      } else {
328
329	$akey = $key;
330	$akey =~ s/$form->{ARAP}_//;
331
332        if ($key eq "$form->{ARAP}_tax") {
333	  if (! $form->{acc_trans}{$key}->[$i-1]->{id}) {
334	    $form->{"${key}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
335
336	    $amount = $form->{acc_trans}{$key}->[$i-1]->{amount} * $ml;
337	    $form->{"${akey}_$form->{acc_trans}{$key}->[$i-1]->{accno}"} += $amount;
338	  }
339
340	} else {
341	  $form->{"${akey}_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * $ml;
342
343	  if ($akey eq 'amount') {
344	    $form->{"description_$i"} = $form->{acc_trans}{$key}->[$i-1]->{memo};
345	    $form->{rowcount}++;
346	    $netamount += $form->{"${akey}_$i"};
347
348            $form->{"projectnumber_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{projectnumber}--$form->{acc_trans}{$key}->[$i-1]->{project_id}" if $form->{acc_trans}{$key}->[$i-1]->{project_id};
349	  }
350	  $form->{"${key}_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
351	}
352      }
353    }
354  }
355
356  if ($form->{paidaccounts}) {
357    $i = $form->{paidaccounts} + 1;
358  } else {
359    $i = $form->{paidaccounts} = 1;
360  }
361
362  $form->{"$form->{ARAP}_paid_$i"} = $form->{payment_accno} if $form->{payment_accno};
363  $form->{"paymentmethod_$i"} = $form->{payment_method} if $form->{payment_method};
364
365
366  $tax = $form->{oldinvtotal} - $netamount;
367  @taxaccounts = split / /, $form->{taxaccounts};
368
369  if ($form->{taxincluded}) {
370    $diff = 0;
371    # add tax to individual amounts
372    for $i (1 .. $form->{rowcount}) {
373      if ($netamount) {
374	$amount = $form->{"amount_$i"} * (1 + $tax / $netamount);
375	$form->{"amount_$i"} = $form->round_amount($amount, $form->{precision});
376      }
377    }
378  }
379
380  if ($form->{type} =~ /_note/) {
381    $form->{"select$form->{ARAP}_discount"} = "";
382  }
383
384  $form->{invtotal} = $netamount + $tax;
385
386
387  if ($form->{id}) {
388
389    $cdt = ($form->{cdt}) ? ($netamount - $form->{discount_paid}) : $netamount;
390
391    for (@taxaccounts) {
392      $tax = $form->round_amount($cdt * $form->{"${_}_rate"}, $form->{precision});
393      if ($tax) {
394	if ($form->{"tax_$_"} == $tax) {
395	  $form->{"calctax_$_"} = 1;
396	}
397      }
398    }
399
400    for (qw(printed emailed)) {
401      for $formname (split / /, $form->{$_}) {
402	$form->{"${formname}_$_"} = 1;
403      }
404    }
405
406  } else {
407    for (@taxaccounts) { $form->{"calctax_$_"} = 1 }
408  }
409
410  for (qw(payment discount)) { $form->{"${_}_accno"} = $form->escape($form->{"${_}_accno"},1) }
411  $form->{payment_method} = $form->escape($form->{payment_method}, 1);
412
413  $form->{"cd_available"} = $form->{invtotal} * $form->{cashdiscount};
414  $form->{cashdiscount} *= 100;
415
416  if ($form->{id} || !$form->{rowcount}) {
417    $i = $form->{rowcount}++;
418    $form->{"$form->{ARAP}_amount_$form->{rowcount}"} = $form->{"$form->{ARAP}_amount_$i"};
419  }
420
421  $form->{$form->{ARAP}} ||= $form->{"$form->{ARAP}_1"};
422  $form->{rowcount} = 1 unless $form->{"$form->{ARAP}_amount_1"};
423
424  $form->{locked} = ($form->{revtrans}) ? '1' : ($form->datetonum(\%myconfig, $form->{transdate}) <= $form->{closedto});
425
426  # readonly
427  if (! $form->{readonly}) {
428    if ($form->{batch}) {
429      $form->{readonly} = 1 if $myconfig{acs} =~ /Vouchers--Payable Batch/ || $form->{approved};
430    } else {
431      $form->{readonly} = 1 if $myconfig{acs} =~ /$form->{ARAP}--(Add Transaction| Note)/;
432    }
433  }
434
435}
436
437
438sub form_header {
439
440  # format amounts
441  $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
442
443  if ($form->{defaultcurrency}) {
444    $exchangerate = qq|<tr>|;
445    $exchangerate .= qq|
446                <th align=right nowrap>|.$locale->text('Currency').qq|</th>
447		<td>
448		  <table>
449		    <tr>
450
451		<td><select name=currency onChange="javascript:document.main.submit()">|
452		.$form->select_option($form->{selectcurrency}, $form->{currency})
453		.qq|</select></td>|;
454
455    if ($form->{currency} ne $form->{defaultcurrency}) {
456      $fdm = $form->dayofmonth($myconfig{dateformat}, $form->{transdate}, 'fdm');
457      $ldm = $form->dayofmonth($myconfig{dateformat}, $form->{transdate});
458
459      $exchangerate .= qq|
460      <th align=right nowrap>|.$locale->text('Exchange Rate').qq| <font color=red>*</font></th>
461      <td><input name=exchangerate class="inputright" size=10 value=$form->{exchangerate}>
462          <a href=am.pl?action=list_exchangerates&transdatefrom=$fdm&transdateto=$ldm&currency=$form->{currency}&login=$form->{login}&path=$form->{path} target=_blank>?</a></td>|;
463    }
464    $exchangerate .= qq|</tr></table></td></tr>|;
465  }
466
467  if (($rows = $form->numtextrows($form->{notes}, 50) - 1) < 2) {
468    $rows = 2;
469  }
470  $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
471
472  if (($rows = $form->numtextrows($form->{intnotes}, 50) - 1) < 2) {
473    $rows = 2;
474  }
475  $intnotes = qq|<textarea name=intnotes rows=$rows cols=50 wrap=soft>$form->{intnotes}</textarea>|;
476
477  $department = qq|
478	      <tr>
479		<th align="right" nowrap>|.$locale->text('Department').qq|</th>
480		<td><select name=department onChange="javascript:document.main.submit()">|
481		.$form->select_option($form->{selectdepartment}, $form->{department}, 1)
482		.qq|
483		</select>
484		</td>
485	      </tr>
486| if $form->{selectdepartment};
487
488
489  $n = ($form->{creditremaining} < 0) ? "0" : "1";
490
491  if ($form->{vc} eq 'customer') {
492    $vclabel = $locale->text('Customer');
493    $vcnumber = $locale->text('Customer Number');
494    $lock = 'lock_sinumber';
495  } else {
496    $vclabel = $locale->text('Vendor');
497    $vcnumber = $locale->text('Vendor Number');
498  }
499
500  if ($form->{id} && $form->{$lock}) {
501    $invnumber = qq|
502	      <tr>
503		<th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
504		<td>|.$form->quote($form->{invnumber}).qq|</td>
505	      </tr>|.$form->hide_form("invnumber", "$lock");
506  } else {
507    $invnumber = qq|
508	      <tr>
509		<th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
510		<td><input name=invnumber size=20 value="|.$form->quote($form->{invnumber}).qq|"></td>
511	      </tr>|;
512
513  }
514
515  $vcref = qq|<a href=ct.pl?action=edit&db=$form->{vc}&id=$form->{"$form->{vc}_id"}&login=$form->{login}&path=$form->{path} target=_blank>?</a>|;
516
517  $vc = qq|<input type=hidden name=action value="Update">
518	      <tr>
519		<th align=right nowrap>$vclabel <font color=red>*</font></th>
520|;
521
522  if ($form->{"select$form->{vc}"}) {
523    $vc .= qq|
524                <td><select name="$form->{vc}" onChange="javascript:document.main.submit()">|.$form->select_option($form->{"select$form->{vc}"}, $form->{$form->{vc}}, 1).qq|</select>
525		$vcref
526                </td>
527              </tr>
528	      <tr>
529		<th align=right nowrap>$vcnumber</th>
530		<td>$form->{"$form->{vc}number"}</td>
531	      </tr>
532| . $form->hide_form("$form->{vc}number");
533  } else {
534    $vc .= qq|
535                <td><input name="$form->{vc}" value="$form->{$form->{vc}}" size=35>
536	        $vcref
537	        </td>
538	      </tr>
539	      <tr>
540		<th align=right nowrap>$vcnumber</th>
541		<td><input name="$form->{vc}number" value="$form->{"$form->{vc}number"}" size=35></td>
542	      </tr>
543|;
544  }
545
546  $employee = $form->hide_form(qw(employee));
547
548  if ($form->{selectemployee}) {
549    $label = ($form->{ARAP} eq 'AR') ? $locale->text('Salesperson') : $locale->text('Employee');
550
551    $employee = qq|
552	      <tr>
553		<th align=right nowrap>$label</th>
554		<td><select name=employee>|
555		.$form->select_option($form->{selectemployee}, $form->{employee}, 1)
556		.qq|
557		</select>
558		</td>
559	      </tr>
560|;
561  }
562
563  for (qw(terms discountterms)) { $form->{$_} = "" if ! $form->{$_} }
564
565  $focus = ($form->{focus}) ? $form->{focus} : "amount_$form->{rowcount}";
566
567  if ($form->{"select$form->{ARAP}_discount"}) {
568    $terms = qq|
569 	      <tr>
570		<th align="right" nowrap>|.$locale->text('Terms').qq|</th>
571		<th align=left nowrap>
572		<input name=cashdiscount class="inputright" size=3 value=|.$form->format_amount(\%myconfig, $form->{cashdiscount}).qq|> /
573		<input name=discountterms class="inputright" size=3 value=$form->{discountterms}> |.$locale->text('Net').qq|
574		<input name=terms class="inputright" size=3 value=$form->{terms}> |.$locale->text('days').qq|
575		</th>
576	      </tr>
577|;
578  } else {
579    $terms = qq|
580 	      <tr>
581		<th align="right" nowrap>|.$locale->text('Terms').qq|</th>
582		<th align=left nowrap>
583		|.$locale->text('Net').qq|
584		<input name=terms class="inputright" size=3 value=$form->{terms}> |.$locale->text('days').qq|
585		</th>
586	      </tr>
587|;
588  }
589
590  if ($form->{batch} && ! $form->{approved}) {
591    $transdate = qq|
592		<td>$form->{transdate}</td>
593		<input type=hidden name=transdate value=$form->{transdate}>
594|;
595  } else {
596    $transdate = qq|
597		<td nowrap><input name=transdate size=11 class=date title="$myconfig{dateformat}" value="$form->{transdate}" />|.&js_calendar("main", "transdate").qq|</td>|;
598  }
599
600  if ($form->{vc} eq 'vendor') {
601    $dcn = qq|
602              <tr>
603	        <th align=right nowrap>|.$locale->text('DCN').qq|</th>
604		<td><input name=dcn size=60 value="|.$form->quote($form->{dcn}).qq|"></td>
605	      </tr>
606|;
607  } else {
608    $dcn = qq|
609              <tr valign=top>
610	        <th align=right nowrap>|.$locale->text('DCN').qq|</th>
611		<td>$form->{dcn}</td>
612	      </tr>
613| .$form->hide_form('dcn');
614  }
615
616  $reference_documents = &references;
617
618  if (($rows = $form->numtextrows($form->{description}, 60, 5)) > 1) {
619    $description = qq|<textarea name="description" rows=$rows cols=60 wrap=soft>$form->{description}</textarea>|;
620  } else {
621    $description = qq|<input name=description size=60 value="|.$form->quote($form->{description}).qq|">|;
622  }
623  $description = qq|
624              <tr valign=top>
625	        <th align=right nowrap>|.$locale->text('Description').qq|</th>
626		<td>$description</td>
627              </tr>
628|;
629
630  $checked{onhold} = ($form->{onhold}) ? "checked" : "";
631
632  $title = $form->quote($form->{title});
633
634  $form->{title} .= " / $form->{company}";
635
636  $form->header;
637
638  &calendar;
639
640print qq|
641
642<body onload="document.main.${focus}.focus()" />
643
644<form method="post" name="main" action="$form->{script}">
645
646<input type=hidden name=title value="$title">
647|;
648
649  $form->hide_form(qw(id type printed emailed sort closedto locked oldtransdate oldduedate oldcurrency audittrail recurring checktax creditlimit creditremaining defaultcurrency rowcount oldterms batch batchid batchnumber batchdescription cdt precision remittancevoucher reference_rows referenceurl olddepartment company));
650  $form->hide_form("select$form->{vc}");
651  $form->hide_form(map { "select$_" } qw(formname currency department employee projectnumber language paymentmethod printer));
652  $form->hide_form("old$form->{vc}", "$form->{vc}_id", "old$form->{vc}number");
653  $form->hide_form(map { "select$_" } ("$form->{ARAP}_amount", "$form->{ARAP}", "$form->{ARAP}_paid", "$form->{ARAP}_discount"));
654
655  $printed = $form->{printed};
656  $printed =~ s/\s??$form->{formname}\s??//;
657  for $formname (split / /, $printed) {
658    $form->hide_form("${formname}_printed");
659  }
660
661  print qq|
662
663<table width=100%>
664  <tr class=listtop>
665    <th class=listtop>$form->{helpref}$form->{title}</a></th>
666  </tr>
667  <tr height="5"></tr>
668  <tr valign=top>
669    <td>
670      <table width=100%>
671        <tr valign=top>
672	  <td>
673	    <table>
674	      $vc
675	      <tr>
676	        <th align=right nowrap>|.$locale->text('Address').qq|</th>
677		<td>$form->{address1} $form->{address2} $form->{city} $form->{state} $form->{zipcode} $form->{country}</td>
678	      </tr>
679	      <tr>
680		<th align=right nowrap>|.$locale->text('Credit Limit').qq|</th>
681		<td>
682		  <table>
683		    <tr>
684		      <td>|.$form->format_amount(\%myconfig, $form->{creditlimit}, $form->{precision}, "0").qq|</td>
685		      <td width=10></td>
686		      <th align=right nowrap>|.$locale->text('Remaining').qq|</th>
687		      <td class="plus$n">|.$form->format_amount(\%myconfig, $form->{creditremaining}, $form->{precision}, "0").qq|</td>
688		    </tr>
689		  </table>
690		</td>
691	      </tr>
692	      $exchangerate
693              <tr>
694	        <td>&nbsp;</td>
695	      </tr>
696	    </table>
697	  </td>
698	  <td align=right>
699	    <table>
700	      $department
701	      $employee
702              $invnumber
703	      <tr>
704		<th align=right nowrap>|.$locale->text('Order Number').qq|</th>
705		<td><input name=ordnumber size=20 value="|.$form->quote($form->{ordnumber}).qq|"></td>
706	      </tr>
707	      <tr>
708		<th align=right nowrap>|.$locale->text('Invoice Date').qq| <font color=red>*</font></th>
709		$transdate
710	      </tr>
711	      <tr>
712		<th align=right nowrap>|.$locale->text('Due Date').qq|</th>
713		<td nowrap><input name=duedate size=11 class=date title="$myconfig{dateformat}" value="$form->{duedate}" />|.&js_calendar("main", "duedate").qq|</td>
714	      </tr>
715	      <tr>
716		<th align=right nowrap>|.$locale->text('PO Number').qq|</th>
717		<td><input name=ponumber size=20 value="|.$form->quote($form->{ponumber}).qq|"></td>
718	      </tr>
719	      $terms
720	    </table>
721	  </td>
722	</tr>
723	<tr>
724	  <td>
725	    <table>
726	      <tr>
727	        <td colspan=2>
728		  $reference_documents
729		</td>
730	      </tr>
731	      $dcn
732	      $description
733	    </table>
734	  </td>
735	</tr>
736      </table>
737    </td>
738  </tr>
739  <tr>
740    <td>
741      <table>
742|;
743
744    if ($form->{selectprojectnumber}) {
745      $project = qq|
746	  <th>|.$locale->text('Project').qq|</th>
747|;
748    }
749
750     print qq|
751	<tr>
752	  <th>|.$locale->text('Amount').qq|</th>
753	  <th></th>
754	  <th>|.$locale->text('Account').qq|</th>
755	  <th>|.$locale->text('Description').qq|</th>
756	  $project
757	</tr>
758|;
759
760  $form->{subtotal} = 0;
761
762  for $i (1 .. $form->{rowcount}) {
763
764    if ($form->{selectprojectnumber}) {
765      $project = qq|
766	  <td align=right><select name="projectnumber_$i">|
767          .$form->select_option($form->{selectprojectnumber}, $form->{"projectnumber_$i"}, 1)
768	  .qq|</select></td>
769|;
770    }
771
772    if (($rows = $form->numtextrows($form->{"description_$i"}, 40)) > 1) {
773      $description = qq|<td><textarea name="description_$i" rows=$rows cols=40>$form->{"description_$i"}</textarea></td>|;
774    } else {
775      $description = qq|<td><input name="description_$i" size=40 value="|.$form->quote($form->{"description_$i"}).qq|"></td>|;
776    }
777
778    $form->{subtotal} += $form->{"amount_$i"};
779
780    print qq|
781	<tr valign=top>
782	  <td><input name="amount_$i" class="inputright" size=11 value="|
783	  .$form->format_amount(\%myconfig, $form->{"amount_$i"}, $form->{precision})
784	  .qq|" accesskey="$i"></td>
785	  <td></td>
786	  <td><select name="$form->{ARAP}_amount_$i">|
787	  .$form->select_option($form->{"select$form->{ARAP}_amount"}, $form->{"$form->{ARAP}_amount_$i"})
788	  .qq|</select></td>
789	  $description
790	  $project
791	</tr>
792|;
793  }
794
795  foreach $item (split / /, $form->{taxaccounts}) {
796
797    $form->{"calctax_$item"} = ($form->{"calctax_$item"}) ? "checked" : "";
798
799    $form->{"tax_$item"} = $form->format_amount(\%myconfig, $form->{"tax_$item"}, $form->{precision}, 0);
800
801    print qq|
802        <tr>
803	  <td><input name="tax_$item" class="inputright" size=11 value=$form->{"tax_$item"}></td>
804	  <td align=right><input name="calctax_$item" class=checkbox type=checkbox value=1 $form->{"calctax_$item"}></td>
805	  <td><select name="$form->{ARAP}_tax_$item">|.$form->select_option($form->{"select$form->{ARAP}_tax_$item"}).qq|</select></td>
806	</tr>
807|;
808
809    $form->hide_form(map { "${item}_$_" } qw(rate description taxnumber));
810    $form->hide_form("select$form->{ARAP}_tax_$item");
811  }
812
813
814  if (!$form->{"$form->{ARAP}_discount_paid"}) {
815    $form->{"$form->{ARAP}_discount_paid"} = $form->unescape($form->{discount_accno});
816  }
817
818  if ($form->{currency} eq $form->{defaultcurrency}) {
819    @column_index = qw(datepaid source memo paid);
820  } else {
821    @column_index = qw(datepaid source memo paid exchangerate);
822  }
823  push @column_index, "paymentmethod" if $form->{selectpaymentmethod};
824  push @column_index, "ARAP_paid";
825
826  $column_data{datepaid} = "<th nowrap>".$locale->text('Date')."</th>";
827  $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
828  $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
829  $column_data{ARAP_paid} = "<th>".$locale->text('Account')."</th>";
830  $column_data{source} = "<th>".$locale->text('Source')."</th>";
831  $column_data{memo} = "<th>".$locale->text('Memo')."</th>";
832  $column_data{paymentmethod} = "<th>".$locale->text('Method')."</th>";
833
834
835  $total = "";
836  $cashdiscount = "";
837  $payments = "";
838
839  $totalpaid = 0;
840
841  if ($form->{cashdiscount}) {
842    $discountavailable = qq|
843  <tr>
844    <td><b>|.$locale->text('Cash Discount').qq|:</b> |.$form->format_amount(\%myconfig, $form->{cd_available}, $form->{precision}).qq|</td>
845  </tr>
846|;
847
848    $cashdiscount = qq|
849  <tr class=listheading>
850    <th class=listheading>|.$locale->text('Cash Discount').qq|</th>
851  </tr>
852
853  <tr>
854    <td>
855      <table width=100%>
856        <tr>
857|;
858
859    for (@column_index) { $cashdiscount .= qq|$column_data{$_}\n| }
860
861
862    $totalpaid = $form->{"discount_paid"};
863
864    $cashdiscount .= qq|
865        </tr>
866|;
867
868    $exchangerate = qq|&nbsp;|;
869    if ($form->{currency} ne $form->{defaultcurrency}) {
870      $form->{discount_exchangerate} = $form->format_amount(\%myconfig, $form->{discount_exchangerate});
871      $exchangerate = qq|<input name="discount_exchangerate" class="inputright" size=10 value=$form->{"discount_exchangerate"}>|.$form->hide_form(qw(olddiscount_datepaid));
872    }
873
874    $column_data{paid} = qq|<td align=center><input name="discount_paid" class="inputright" size=11 value=|.$form->format_amount(\%myconfig, $form->{"discount_paid"}, $form->{precision}).qq|></td>|;
875    $column_data{ARAP_paid} = qq|<td align=center><select name="$form->{ARAP}_discount_paid">|.$form->select_option($form->{"select$form->{ARAP}_discount"}, $form->{"$form->{ARAP}_discount_paid"}).qq|</select></td>|;
876    $column_data{datepaid} = qq|<td align=center nowrap><input name="discount_datepaid" size=11 class=date title="$myconfig{dateformat}" value="$form->{"discount_datepaid"}">|.&js_calendar("main", "discount_datepaid").qq|</td>|;
877    $column_data{exchangerate} = qq|<td align=center>$exchangerate</td>|;
878    $column_data{source} = qq|<td align=center><input name="discount_source" size=11 value="|.$form->quote($form->{"discount_source"}).qq|"></td>|;
879    $column_data{memo} = qq|<td align=center><input name="discount_memo" size=11 value="|.$form->quote($form->{"discount_memo"}).qq|"></td>|;
880
881    if ($form->{selectpaymentmethod}) {
882      $column_data{paymentmethod} = qq|<td align=center><select name="discount_paymentmethod">|.$form->select_option($form->{"selectpaymentmethod"}, $form->{discount_paymentmethod}, 1).qq|</select></td>|;
883    }
884
885    $cashdiscount .= qq|
886        <tr>
887|;
888
889    for (@column_index) { $cashdiscount .= qq|$column_data{$_}\n| }
890
891    $cashdiscount .= qq|
892        </tr>
893|;
894
895    $cashdiscount .= $form->hide_form(map { "discount_$_" } qw(cleared));
896
897    $payments = qq|
898  <tr class=listheading>
899    <th class=listheading colspan=7>|.$locale->text('Payments').qq|</th>
900  </tr>
901|;
902
903  } else {
904    $payments = qq|
905  <tr class=listheading>
906    <th class=listheading>|.$locale->text('Payments').qq|</th>
907  </tr>
908
909  <tr>
910    <td>
911      <table width=100%>
912        <tr>
913|;
914
915  for (@column_index) { $payments .= qq|$column_data{$_}\n| }
916
917  $payments .= qq|
918        </tr>
919|;
920  }
921
922  if ($form->{batch}) {
923    $cashdiscount = "";
924    $payments = "";
925    $form->{paidaccounts} = 0;
926  }
927
928  $cd_tax = 0;
929  if ($form->{discount_paid} && $form->{cdt}) {
930    $cdtp = $form->{discount_paid} / $form->{subtotal} if $form->{subtotal};
931    for (split / /, $form->{taxaccounts}) {
932      $cd_tax += $form->round_amount($form->{"tax_$_"} * $cdtp, $form->{precision});
933    }
934  }
935
936
937  $form->{subtotal} = $form->format_amount(\%myconfig, $form->{subtotal} - $form->{discount_paid}, $form->{precision});
938  $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, $form->{precision});
939
940  $form->hide_form(qw(oldinvtotal oldtotalpaid taxaccounts));
941
942  $taxincluded = "";
943  $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
944
945  if ($form->{taxaccounts}) {
946    $taxincluded = qq|
947	      <tr>
948		<td><input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded} onChange="javascript:document.main.submit()"><b> |.$locale->text('Tax Included').qq|</b></td>
949	      </tr>
950|;
951  }
952
953  print qq|
954        <tr>
955	  <th align=right>$form->{invtotal}</th>
956	  <td></td>
957	  <td><select name="$form->{ARAP}">|
958	  .$form->select_option($form->{"select$form->{ARAP}"}, $form->{$form->{ARAP}})
959	  .qq|</select></td>
960        </tr>
961      </table>
962    </td>
963  </tr>
964  <tr>
965    <td>
966      <table width=100%>
967	$taxincluded
968	<tr>
969	  <td width=50%><b>|.$locale->text('Notes').qq|</b><br>
970	  $notes</td>
971	  <td width=50%><b>|.$locale->text('Internal Notes').qq|</b><br>
972	  $intnotes</td>
973	</tr>
974      </table>
975    </td>
976  </tr>
977  $discountavailable
978  $cashdiscount
979  $payments
980|;
981
982  $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
983  $form->{"$form->{ARAP}_paid_$form->{paidaccounts}"} = $form->unescape($form->{payment_accno});
984  $form->{"paymentmethod_$form->{paidaccounts}"} = $form->unescape($form->{payment_method});
985
986  $roundto = 0;
987  if ($form->{roundchange}) {
988    %roundchange = split /[=;]/, $form->unescape($form->{roundchange});
989    $roundto = $roundchange{''};
990  }
991
992  $totalpaid = 0;
993
994  for $i (1 .. $form->{paidaccounts}) {
995
996    print qq|
997        <tr>
998|;
999
1000    $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
1001
1002    $exchangerate = qq|&nbsp;|;
1003    if ($form->{currency} ne $form->{defaultcurrency}) {
1004      $exchangerate = qq|<input name="exchangerate_$i" class="inputright" size=10 value=$form->{"exchangerate_$i"}>|.$form->hide_form("olddatepaid_$i");
1005    }
1006
1007    $form->hide_form(map { "${_}_$i" } qw(vr_id cleared));
1008
1009    $totalpaid += $form->{"paid_$i"};
1010
1011    $column_data{paid} = qq|<td align=center><input name="paid_$i" class="inputright" size=11 value=|.$form->format_amount(\%myconfig, $form->{"paid_$i"}, $form->{precision}).qq|></td>|;
1012    $column_data{ARAP_paid} = qq|<td align=center><select name="$form->{ARAP}_paid_$i">|.$form->select_option($form->{"select$form->{ARAP}_paid"}, $form->{"$form->{ARAP}_paid_$i"}).qq|</select></td>|;
1013    $column_data{exchangerate} = qq|<td align=center>$exchangerate</td>|;
1014    $column_data{datepaid} = qq|<td align=center nowrap><input name="datepaid_$i" size=11 class=date title="$myconfig{dateformat}" value="$form->{"datepaid_$i"}" />|.&js_calendar("main", "datepaid_$i").qq|</td>|;
1015    $column_data{source} = qq|<td align=center><input name="source_$i" size=11 value="|.$form->quote($form->{"source_$i"}).qq|"></td>|;
1016    $column_data{memo} = qq|<td align=center><input name="memo_$i" size=11 value="|.$form->quote($form->{"memo_$i"}).qq|"></td>|;
1017
1018    if ($form->{selectpaymentmethod}) {
1019      if ($form->{"paymentmethod_$i"}) {
1020	if ($form->{"paid_$i"}) {
1021	  $roundto = $roundchange{$form->{"paymentmethod_$i"}};
1022	}
1023      }
1024
1025      $column_data{paymentmethod} = qq|<td align=center><select name="paymentmethod_$i">|.$form->select_option($form->{"selectpaymentmethod"}, $form->{"paymentmethod_$i"}, 1).qq|</select></td>|;
1026    }
1027
1028    for (@column_index) { print qq|$column_data{$_}\n| }
1029
1030    print "
1031        </tr>
1032";
1033  }
1034
1035  $totalpaid = $form->round_amount($totalpaid, $form->{precision});
1036
1037  if ($totalpaid == 0) {
1038    $roundto = $roundchange{$form->{"paymentmethod_$form->{paidaccounts}"}};
1039  }
1040
1041  if ($roundto > 0.01) {
1042    $outstanding = $form->round_amount($form->{oldinvtotal} / $roundto, 0) * $roundto;
1043    $outstanding -= $totalpaid;
1044    $outstanding = $form->round_amount($outstanding / $roundto, 0) * $roundto;
1045  } else {
1046    $outstanding = $form->round_amount($form->{oldinvtotal} - $totalpaid, $form->{precision});
1047  }
1048
1049  if ($outstanding) {
1050    # print total
1051    if ($outstanding > 0) {
1052      print qq|
1053	  <tr>
1054            <td colspan=4><b>|.$locale->text('Outstanding').":</b> ".$form->format_amount(\%myconfig, $outstanding, $form->{precision}).qq|</td>
1055	  </tr>
1056|;
1057    } else {
1058      print qq|
1059	  <tr>
1060            <td colspan=4><b>|.$locale->text('Overpaid').":</b> ".$form->format_amount(\%myconfig, $outstanding * -1, $form->{precision}).qq|</td>
1061	  </tr>
1062|;
1063    }
1064  }
1065
1066  $form->hide_form(qw(address1 address2 city state zipcode country paidaccounts payment_accno discount_accno payment_method roundchange cashovershort));
1067
1068  print qq|
1069      </table>
1070    </td>
1071  </tr>
1072  <tr>
1073    <td><hr size=3 noshade></td>
1074  </tr>
1075</table>
1076|;
1077
1078}
1079
1080
1081sub form_footer {
1082
1083  $form->hide_form(qw(helpref callback path login));
1084
1085  $transdate = $form->datetonum(\%myconfig, $form->{transdate});
1086
1087  if ($form->{readonly}) {
1088
1089    &islocked;
1090
1091  } else {
1092
1093    &print_options;
1094
1095    print "<br>";
1096
1097    %button = ('Update' => { ndx => 1, key => 'U', value => $locale->text('Update') },
1098	       'Preview' => { ndx => 3, key => 'V', value => $locale->text('Preview') },
1099               'Print' => { ndx => 4, key => 'P', value => $locale->text('Print') },
1100	       'Post' => { ndx => 5, key => 'O', value => $locale->text('Post') },
1101	       'Print and Post' => { ndx => 6, key => 'R', value => $locale->text('Print and Post') },
1102	       'Post as new' => { ndx => 7, key => 'N', value => $locale->text('Post as new') },
1103	       'Print and Post as new' => { ndx => 8, key => 'W', value => $locale->text('Print and Post as new') },
1104	       'Schedule' => { ndx => 9, key => 'H', value => $locale->text('Schedule') },
1105               'New Number' => { ndx => 10, key => 'M', value => $locale->text('New Number') },
1106	       'Delete' => { ndx => 11, key => 'D', value => $locale->text('Delete') },
1107	      );
1108
1109    delete $button{'Schedule'} if $form->{batch};
1110
1111    if ($form->{id}) {
1112
1113      if ($form->{locked} || $transdate <= $form->{closedto}) {
1114	for ("Post", "Print and Post", "Delete") { delete $button{$_} }
1115      }
1116
1117    } else {
1118
1119      for ("Post as new", "Print and Post as new", "Delete") { delete $button{$_} }
1120
1121      if ($transdate <= $form->{closedto}) {
1122	for ("Post", "Print and Post") { delete $button{$_} }
1123      }
1124    }
1125
1126    if (!$latex) {
1127      for ("Preview", "Print and Post", "Print and Post as new") { delete $button{$_} }
1128    }
1129
1130    $form->print_button(\%button);
1131
1132  }
1133
1134  if ($form->{menubar}) {
1135    require "$form->{path}/menu.pl";
1136    &menubar;
1137  }
1138
1139  print qq|
1140</form>
1141
1142</body>
1143</html>
1144|;
1145
1146}
1147
1148
1149sub update {
1150  my $display = shift;
1151
1152  if (!$display) {
1153    $form->{invtotal} = 0;
1154
1155    $form->{exchangerate} = $form->parse_amount(\%myconfig, $form->{exchangerate});
1156
1157    @flds = ("amount", "$form->{ARAP}_amount", "projectnumber", "description");
1158    $count = 0;
1159    @f = ();
1160    for $i (1 .. $form->{rowcount}) {
1161      $form->{"amount_$i"} = $form->parse_amount(\%myconfig, $form->{"amount_$i"});
1162      if ($form->{"amount_$i"}) {
1163	push @f, {};
1164	$j = $#f;
1165
1166	for (@flds) { $f[$j]->{$_} = $form->{"${_}_$i"} }
1167	$count++;
1168      }
1169    }
1170
1171    $form->redo_rows(\@flds, \@f, $count, $form->{rowcount});
1172    $form->{rowcount} = $count + 1;
1173
1174    $form->{"$form->{ARAP}_amount_$form->{rowcount}"} = $form->{"$form->{ARAP}_amount_$count"};
1175
1176    for (1 .. $form->{rowcount}) { $form->{invtotal} += $form->{"amount_$_"} }
1177
1178    if ($form->{transdate} ne $form->{oldtransdate} || $form->{currency} ne $form->{oldcurrency}) {
1179      $form->{exchangerate} = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{transdate});
1180    }
1181
1182    $form->{cashdiscount} = $form->parse_amount(\%myconfig, $form->{cashdiscount});
1183    $form->{discount_paid} = $form->parse_amount(\%myconfig, $form->{discount_paid});
1184
1185    &rebuild_departments if $form->{department} ne $form->{olddepartment};
1186
1187    if ($newname = &check_name($form->{vc})) {
1188      &rebuild_vc($form->{vc}, $form->{ARAP}, $form->{transdate});
1189    }
1190
1191    if ($form->{oldterms} != $form->{terms}) {
1192      $form->{duedate} = $form->add_date(\%myconfig, $form->{transdate}, $form->{terms}, 'days');
1193      $newterms = 1;
1194      $form->{oldterms} = $form->{terms};
1195      $form->{oldduedate} = $form->{duedate};
1196    }
1197
1198    if ($form->{duedate} ne $form->{oldduedate}) {
1199      $form->{terms} = $form->datediff(\%myconfig, $form->{transdate}, $form->{duedate});
1200      $newterms = 1;
1201      $form->{oldterms} = $form->{terms};
1202      $form->{oldduedate} = $form->{duedate};
1203    }
1204
1205    if ($form->{transdate} ne $form->{oldtransdate}) {
1206      $form->{duedate} = $form->add_date(\%myconfig, $form->{transdate}, $form->{terms}, 'days') if ! $newterms;
1207      $form->{oldtransdate} = $form->{transdate};
1208      $newproj = &rebuild_vc($form->{vc}, $form->{ARAP}, $form->{transdate}) if ! $newname;
1209      if (! $newproj) {
1210	$form->all_projects(\%myconfig, undef, $form->{transdate});
1211	$form->{selectprojectnumber} = "";
1212	if (@{ $form->{all_project} }) {
1213	  $form->{selectprojectnumber} = "\n";
1214	  for (@{ $form->{all_project} }) { $form->{selectprojectnumber} .= qq|$_->{projectnumber}--$_->{id}\n| }
1215	  $form->{selectprojectnumber} = $form->escape($form->{selectprojectnumber},1);
1216	}
1217      }
1218
1219      if (@{ $form->{all_employee} }) {
1220	$form->{selectemployee} = "\n";
1221	for (@{ $form->{all_employee} }) { $form->{selectemployee} .= qq|$_->{name}--$_->{id}\n| }
1222	$form->{selectemployee} = $form->escape($form->{selectemployee},1);
1223      }
1224    }
1225  }
1226
1227  # recalculate taxes
1228  @taxaccounts = split / /, $form->{taxaccounts};
1229
1230  for (@taxaccounts) { $form->{"tax_$_"} = $form->parse_amount(\%myconfig, $form->{"tax_$_"}) }
1231
1232  if ($form->{taxincluded}) {
1233
1234    $ml = 1;
1235
1236    for (0 .. 1) {
1237      $taxrate = 0;
1238      $diff = 0;
1239
1240      for (@taxaccounts) {
1241	if (($form->{"${_}_rate"} * $ml) > 0) {
1242	  if ($form->{"calctax_$_"}) {
1243	    $taxrate += $form->{"${_}_rate"};
1244	  } else {
1245	    if ($form->{checktax}) {
1246	      if ($form->{"tax_$_"}) {
1247		$taxrate += $form->{"${_}_rate"};
1248	      }
1249	    }
1250	  }
1251	}
1252      }
1253
1254      $taxrate *= $ml;
1255
1256      foreach $item (@taxaccounts) {
1257        $desc_taxrate = $form->{"${item}_rate"} * 100;
1258        $form->{"select$form->{ARAP}_tax_$item"} = qq|$item--$form->{"${item}_description"} $desc_taxrate%|;
1259        $form->{"calctax_$item"} = 1 if $form->{calctax};
1260
1261	if (($form->{"${item}_rate"} * $ml) > 0) {
1262	  if ($taxrate) {
1263	    $x = ($form->{cdt}) ? ($form->{invtotal} - $form->{discount_paid}) : $form->{invtotal};
1264	    $x *= $form->{"${item}_rate"} / (1 + $taxrate);
1265	    $y = $form->round_amount($x, $form->{precision});
1266	    $tax = $form->round_amount($x - $diff, $form->{precision});
1267	    $diff = $y - ($x - $diff);
1268	  }
1269	  $form->{"tax_$item"} = $tax if $form->{"calctax_$item"};
1270
1271	  $totaltax += $form->{"tax_$item"};
1272	}
1273      }
1274      $ml *= -1;
1275    }
1276    $totaltax += $form->round_amount($diff, $form->{precision});
1277
1278    $form->{checktax} = 1;
1279
1280  } else {
1281    foreach $item (@taxaccounts) {
1282      $form->{"calctax_$item"} = 1 if $form->{calctax};
1283
1284      if ($form->{"calctax_$item"}) {
1285	$x = ($form->{cdt}) ? $form->{invtotal} - $form->{discount_paid} : $form->{invtotal};
1286	$form->{"tax_$item"} = $form->round_amount($x * $form->{"${item}_rate"}, $form->{precision});
1287      }
1288      $desc_taxrate = $form->{"${item}_rate"} * 100;
1289
1290      $form->{"select$form->{ARAP}_tax_$item"} = qq|$item--$form->{"${item}_description"} $desc_taxrate%|;
1291      $totaltax += $form->{"tax_$item"};
1292    }
1293  }
1294
1295
1296  if ($form->{taxincluded}) {
1297    $netamount = $form->{invtotal} - $totaltax;
1298  } else {
1299    $netamount = $form->{invtotal};
1300    $form->{invtotal} += $totaltax;
1301  }
1302
1303  # redo payment discount
1304  $form->{cd_available} = $form->{invtotal} * $form->{cashdiscount} / 100;
1305
1306  if ($form->{discount_paid}) {
1307    if ($form->{discount_datepaid} ne $form->{olddiscount_datepaid} || $form->{currency} ne $form->{oldcurrency}) {
1308      if ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{discount_datepaid})) {
1309	$form->{discount_exchangerate} = $exchangerate;
1310      }
1311    }
1312    $form->{olddiscount_datepaid} = $form->{discount_datepaid};
1313  }
1314
1315  $form->{oldcurrency} = $form->{currency};
1316
1317  $totalpaid = $form->{discount_paid};
1318
1319  $j = 1;
1320  for $i (1 .. $form->{paidaccounts}) {
1321    if ($form->{"paid_$i"}) {
1322      for (qw(olddatepaid datepaid source memo cleared paymentmethod)) { $form->{"${_}_$j"} = $form->{"${_}_$i"} }
1323      for (qw(paid exchangerate)) { $form->{"${_}_$j"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) }
1324
1325      $totalpaid += $form->{"paid_$j"};
1326
1327      if ($form->{"datepaid_$j"} ne $form->{"olddatepaid_$j"} || $form->{currency} ne $form->{oldcurrency}) {
1328	if ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$j"})) {
1329	  $form->{"exchangerate_$j"} = $exchangerate;
1330	}
1331      }
1332
1333      $form->{"olddatepaid_$j"} = $form->{"datepaid_$j"};
1334
1335      if ($j++ != $i) {
1336	for (qw(olddatepaid datepaid source memo paid exchangerate cleared)) { delete $form->{"${_}_$i"} }
1337      }
1338    } else {
1339      for (qw(olddatepaid datepaid source memo paid exchangerate cleared)) { delete $form->{"${_}_$i"} }
1340    }
1341  }
1342
1343  $form->{payment_accno} = $form->escape($form->{"$form->{ARAP}_paid_$form->{paidaccounts}"},1);
1344  $form->{payment_method} = $form->escape($form->{"paymentmethod_$form->{paidaccounts}"},1);
1345
1346  $form->{paidaccounts} = $j;
1347
1348  $ml = ($form->{type} =~ /_note/) ? -1 : 1;
1349  $form->{creditremaining} -= ($form->{invtotal} - $totalpaid + $form->{oldtotalpaid} - $form->{oldinvtotal}) * $ml;
1350  $form->{oldinvtotal} = $form->{invtotal};
1351  $form->{oldtotalpaid} = $totalpaid;
1352
1353  $form->{printed} =~ s/\s??$form->{formname}\s??//;
1354  $form->{printed} .= " $form->{formname}" if $form->{"$form->{formname}_printed"};
1355  $form->{printed} =~ s/^ //;
1356
1357  &display_form;
1358
1359}
1360
1361
1362sub post {
1363
1364  $label = ($form->{vc} eq 'customer') ? $locale->text('Customer missing!') : $locale->text('Vendor missing!');
1365
1366  # check if there is an invoice number, invoice and due date
1367  $form->isblank("transdate", $locale->text('Invoice Date missing!'));
1368  $form->isblank($form->{vc}, $label);
1369
1370  $transdate = $form->datetonum(\%myconfig, $form->{transdate});
1371
1372  $form->error($locale->text('Cannot post transaction for a closed period!')) if ($transdate <= $form->{closedto});
1373
1374  $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
1375
1376  $roundto = 0;
1377
1378  if ($form->{roundchange}) {
1379    %roundchange = split /[=;]/, $form->unescape($form->{roundchange});
1380    $roundto = $roundchange{''};
1381  }
1382
1383  $paid = 0;
1384  for $i (1 .. $form->{paidaccounts}) {
1385    if ($form->{"paid_$i"}) {
1386      $paid += $form->parse_amount(\%myconfig, $form->{"paid_$i"});
1387
1388      $datepaid = $form->datetonum(\%myconfig, $form->{"datepaid_$i"});
1389
1390      $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
1391
1392      $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $form->{closedto});
1393
1394      if ($form->{currency} ne $form->{defaultcurrency}) {
1395	$form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
1396	$form->isblank("exchangerate_$i", $locale->text('Exchange rate for payment missing!'));
1397      }
1398
1399      if ($form->{selectpaymentmethod}) {
1400	$roundto = $roundchange{$form->{"paymentmethod_$i"}};
1401      }
1402    }
1403  }
1404
1405  $ARAP_paid = $form->{"$form->{ARAP}_paid_$form->{paidaccounts}"};
1406  $paymentmethod = $form->{"paymentmethod_$form->{paidaccounts}"};
1407
1408  # if oldname ne name redo form
1409  ($name) = split /--/, $form->{$form->{vc}};
1410  if ($form->{"old$form->{vc}"} ne qq|$name--$form->{"$form->{vc}_id"}|) {
1411    &update;
1412    exit;
1413  }
1414
1415  if (! $form->{repost}) {
1416    if ($form->{id} && ! $form->{batch}) {
1417      &repost;
1418      exit;
1419    }
1420  }
1421
1422  # add discount to payments
1423  if ($form->{discount_paid}) {
1424    $form->{paidaccounts}++ if $form->{"paid_$form->{paidaccounts}"};
1425    $i = $form->{paidaccounts};
1426
1427    for (qw(paid datepaid source memo exchangerate cleared)) { $form->{"${_}_$i"} = $form->{"discount_$_"} }
1428    $form->{discount_index} = $i;
1429    $form->{"$form->{ARAP}_paid_$i"} = $form->{"$form->{ARAP}_discount_paid"};
1430    $form->{"paymentmethod_$i"} = $form->{discount_paymentmethod};
1431
1432    if ($form->{"paid_$i"}) {
1433      $paid += $form->parse_amount(\%myconfig, $form->{"paid_$i"});
1434
1435      $datepaid = $form->datetonum(\%myconfig, $form->{"datepaid_$i"});
1436      $expired = $form->datetonum(\%myconfig, $form->add_date(\%myconfig, $form->{transdate}, $form->{discountterms}, 'days'));
1437
1438      $form->isblank("datepaid_$i", $locale->text('Cash Discount date missing!'));
1439
1440      $form->error($locale->text('Cannot post cash discount for a closed period!')) if ($datepaid <= $form->{closedto});
1441
1442      $form->error($locale->text('Date for cash discount past due!')) if ($datepaid > $expired);
1443
1444      $form->error($locale->text('Cash discount exceeds available discount!')) if $form->parse_amount(\%myconfig, $form->{"paid_$i"}) > ($form->{oldinvtotal} * $form->{cashdiscount});
1445
1446      if ($form->{currency} ne $form->{defaultcurrency}) {
1447	$form->{"exchangerate_$i"} = $form->{exchangerate} if ($transdate == $datepaid);
1448	$form->isblank("exchangerate_$i", $locale->text('Exchange rate for cash discount missing!'));
1449      }
1450    }
1451  }
1452
1453  if ($roundto > 0.01) {
1454    $total = $form->round_amount($form->{oldinvtotal} / $roundto, 0) * $roundto;
1455    $cashover = $form->round_amount($paid - $total - ($paid - $form->{oldinvtotal}), $form->{precision});
1456
1457    if ($cashover) {
1458      if ($form->round_amount($paid, $form->{precision}) == $form->round_amount($total, $form->{precision})) {
1459	$i = ++$form->{paidaccounts};
1460	$form->{"paid_$i"} = $form->format_amount(\%myconfig, $cashover, $form->{precision});
1461	$form->{"datepaid_$i"} = $datepaid;
1462	$form->{"$form->{ARAP}_paid_$i"} = $form->{cashovershort};
1463      }
1464    }
1465  }
1466
1467  $i = ++$form->{paidaccounts};
1468  $form->{"$form->{ARAP}_paid_$i"} = $ARAP_paid;
1469  $form->{"paymentmethod_$i"} = $paymentmethod;
1470
1471  $form->{printed} =~ s/\s??$form->{formname}\s??//;
1472  if ($form->{"$form->{formname}_printed"}) {
1473    $form->{printed} .= " $form->{formname}";
1474  }
1475  $form->{printed} =~ s/^ //;
1476
1477  $form->{userspath} = $userspath;
1478
1479  if ($form->{batch}) {
1480    $rc = VR->post_transaction(\%myconfig, \%$form);
1481  } else {
1482    $rc = AA->post_transaction(\%myconfig, \%$form);
1483  }
1484
1485  if ($form->{callback}) {
1486    $form->{callback} =~ s/(batch|batchid|batchdescription)=.*?&//g;
1487    $form->{callback} .= "&batch=$form->{batch}&batchid=$form->{batchid}&transdate=$form->{transdate}&batchdescription=".$form->escape($form->{batchdescription},1);
1488  }
1489
1490  if ($rc) {
1491    $form->redirect($locale->text('Transaction posted!'));
1492  } else {
1493    $form->error($locale->text('Cannot post transaction!'));
1494  }
1495
1496}
1497
1498
1499sub delete {
1500
1501  $form->{title} = $locale->text('Confirm!');
1502
1503  $form->header;
1504
1505  print qq|
1506<body>
1507
1508<form method=post action=$form->{script}>
1509|;
1510
1511  $form->{action} = "yes";
1512  $form->hide_form;
1513
1514  print qq|
1515<h2 class=confirm>$form->{title}</h2>
1516
1517<h4>|.$locale->text('Are you sure you want to delete Transaction').qq| $form->{invnumber}</h4>
1518
1519<input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
1520</form>
1521
1522</body>
1523</html>
1524|;
1525
1526}
1527
1528
1529
1530sub yes {
1531
1532  if (AA->delete_transaction(\%myconfig, \%$form)) {
1533    $form->redirect($locale->text('Transaction deleted!'));
1534  } else {
1535    $form->error($locale->text('Cannot delete transaction!'));
1536  }
1537
1538}
1539
1540
1541sub search {
1542
1543  $form->create_links($form->{ARAP}, \%myconfig, $form->{vc});
1544
1545  $form->{"select$form->{ARAP}"} = "\n";
1546  for (@{ $form->{"$form->{ARAP}_links"}{$form->{ARAP}} }) { $form->{"select$form->{ARAP}"} .= "$_->{accno}--$_->{description}\n" }
1547
1548  $vclabel = $locale->text('Customer');
1549  $vcnumber = $locale->text('Customer Number');
1550  $vctaxnumber = $locale->text('Taxnumber');
1551  $l_name = qq|<input name="l_name" class=checkbox type=checkbox value=Y checked> $vclabel|;
1552  $l_customernumber = qq|<input name="l_customernumber" class=checkbox type=checkbox value=Y> $vcnumber|;
1553  $l_till = qq|<input name="l_till" class=checkbox type=checkbox value=Y> |.$locale->text('Till');
1554
1555  if ($form->{vc} eq 'vendor') {
1556    $vclabel = $locale->text('Vendor');
1557    $vcnumber = $locale->text('Vendor Number');
1558    $vctaxnumber = $locale->text('SSN');
1559    $l_till = "";
1560    $l_customernumber = "";
1561    $l_name = qq|<input name="l_name" class=checkbox type=checkbox value=Y checked> $vclabel|;
1562    $l_vendornumber = qq|<input name="l_vendornumber" class=checkbox type=checkbox value=Y> $vcnumber|;
1563  }
1564
1565  if (@{ $form->{"all_$form->{vc}"} }) {
1566    $form->{"select$form->{vc}"} = "\n";
1567    for (@{ $form->{"all_$form->{vc}"} }) { $form->{"select$form->{vc}"} .= qq|$_->{name}--$_->{id}\n| }
1568    $vc = qq|
1569              <tr>
1570	        <th align=right nowrap>$vclabel</th>
1571	        <td colspan=3><select name="$form->{vc}">|
1572		.$form->select_option($form->{"select$form->{vc}"}, undef, 1)
1573		.qq|</select>
1574	        </td>
1575	      </tr>
1576|;
1577  } else {
1578    $vc = qq|
1579              <tr>
1580	        <th align=right nowrap>$vclabel</th>
1581	        <td colspan=3><input name=$form->{vc} size=35>
1582		</td>
1583	      </tr>
1584	      <tr>
1585	        <th align=right nowrap>$vcnumber</th>
1586		<td colspan=3><input name="$form->{vc}number" size=35>
1587		</td>
1588	      </tr>
1589|;
1590  }
1591
1592  # departments
1593  if (@{ $form->{all_department} }) {
1594    $form->{selectdepartment} = "\n";
1595
1596    for (@{ $form->{all_department} }) { $form->{selectdepartment} .= qq|$_->{description}--$_->{id}\n| }
1597
1598    $l_department = qq|<input name="l_department" class=checkbox type=checkbox value=Y> |.$locale->text('Department');
1599
1600    $department = qq|
1601        <tr>
1602	  <th align=right nowrap>|.$locale->text('Department').qq|</th>
1603	  <td><select name=department>|
1604	  .$form->select_option($form->{selectdepartment}, undef, 1)
1605	  .qq|</select></td>
1606	</tr>
1607|;
1608  }
1609
1610  if (@{ $form->{all_warehouse} }) {
1611    $form->{selectwarehouse} = "\n";
1612    $form->{warehouse} = qq|$form->{warehouse}--$form->{warehouse_id}|;
1613
1614    for (@{ $form->{all_warehouse} }) { $form->{selectwarehouse} .= qq|$_->{description}--$_->{id}\n| }
1615
1616    $warehouse = qq|
1617            <tr>
1618	      <th align=right>|.$locale->text('Warehouse').qq|</th>
1619	      <td><select name=warehouse>|
1620	      .$form->select_option($form->{selectwarehouse}, undef, 1)
1621	      .qq|</select>
1622	      </td>
1623	      <input type=hidden name=selectwarehouse value="|
1624	      .$form->escape($form->{selectwarehouse},1).qq|">
1625	    </tr>
1626|;
1627
1628    $l_warehouse = qq|<input name="l_warehouse" class=checkbox type=checkbox value=Y> |.$locale->text('Warehouse');
1629
1630  }
1631
1632
1633  if (@{ $form->{all_employee} }) {
1634    $form->{selectemployee} = "\n";
1635    for (@{ $form->{all_employee} }) { $form->{selectemployee} .= qq|$_->{name}--$_->{id}\n| }
1636
1637    $employeelabel = ($form->{ARAP} eq 'AR') ? $locale->text('Salesperson') : $locale->text('Employee');
1638
1639    $employee = qq|
1640        <tr>
1641	  <th align=right nowrap>$employeelabel</th>
1642	  <td><select name=employee>|
1643	  .$form->select_option($form->{selectemployee}, undef, 1)
1644	  .qq|</select></td>
1645	</tr>
1646|;
1647
1648    $l_employee = qq|<input name="l_employee" class=checkbox type=checkbox value=Y> $employeelabel|;
1649
1650  }
1651
1652  if ($form->{ARAP} eq 'AR') {
1653    $form->{title} = $locale->text('AR Transactions');
1654    $form->helpref("ar_transactions", $myconfig{countrycode});
1655  } else {
1656    $form->{title} = $locale->text('AP Transactions');
1657    $form->helpref("ap_transactions", $myconfig{countrycode});
1658  }
1659
1660  $invnumber = qq|
1661	<tr>
1662	  <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
1663	  <td><input name=invnumber size=20></td>
1664	</tr>
1665	<tr>
1666	  <th align=right nowrap>|.$locale->text('Description').qq|</th>
1667	  <td><input name=description size=40></td>
1668	</tr>
1669	<tr>
1670	  <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
1671	  <td><input name=ordnumber size=20></td>
1672	</tr>
1673	<tr>
1674	  <th align=right nowrap>|.$locale->text('PO Number').qq|</th>
1675	  <td><input name=ponumber size=20></td>
1676	</tr>
1677	<tr>
1678	  <th align=right nowrap>|.$locale->text('Source').qq|</th>
1679	  <td><input name=source size=40></td>
1680	</tr>
1681	<tr>
1682	  <th align=right nowrap>|.$locale->text('Line Item').qq|</th>
1683	  <td><input name=memo size=40></td>
1684	</tr>
1685	<tr>
1686	  <th align=right nowrap>|.$locale->text('Notes').qq|</th>
1687	  <td><input name=notes size=40></td>
1688	</tr>
1689|;
1690
1691  $openclosed = qq|
1692	      <tr>
1693		<td nowrap><input name=open class=checkbox type=checkbox value=Y checked> |.$locale->text('Open').qq|</td>
1694		<td nowrap><input name=closed class=checkbox type=checkbox value=Y> |.$locale->text('Closed').qq|</td>
1695		<td nowrap><input name=onhold class=checkbox type=checkbox value=Y> |.$locale->text('On Hold').qq|</td>
1696		<td nowrap><input name=paidlate class=checkbox type=checkbox value=Y> |.$locale->text('Paid Late').qq|</td>
1697		<td nowrap><input name=paidearly class=checkbox type=checkbox value=Y> |.$locale->text('Paid Early').qq|</td>
1698	      </tr>
1699|;
1700
1701  $summary = qq|
1702              <tr>
1703		<td><input name=summary type=radio class=radio value=1 checked> |.$locale->text('Summary').qq|</td>
1704		<td><input name=summary type=radio class=radio value=0> |.$locale->text('Detail').qq|
1705		</td>
1706	      </tr>
1707|;
1708
1709
1710  if ($form->{outstanding}) {
1711    if ($form->{ARAP} eq 'AR') {
1712      $form->{title} = $locale->text('AR Outstanding');
1713      $form->helpref("ar_outstanding", $myconfig{countrycode});
1714    } else {
1715      $form->{title} = $locale->text('AP Outstanding');
1716      $form->helpref("ap_outstanding", $myconfig{countrycode});
1717    }
1718
1719    $invnumber = "";
1720    $openclosed = "";
1721    $summary = "";
1722
1723  }
1724
1725  if (@{ $form->{all_years} }) {
1726    # accounting years
1727    $selectaccountingyear = "\n";
1728    for (@{ $form->{all_years} }) { $selectaccountingyear .= qq|$_\n| }
1729    $selectaccountingmonth = "\n";
1730    for (sort keys %{ $form->{all_month} }) { $selectaccountingmonth .= qq|$_--|.$locale->text($form->{all_month}{$_}).qq|\n| }
1731
1732    $selectfrom = qq|
1733      <tr>
1734	<th align=right>|.$locale->text('Period').qq|</th>
1735	<td>
1736	<select name=month>|.$form->select_option($selectaccountingmonth, undef, 1, 1).qq|</select>
1737	<select name=year>|.$form->select_option($selectaccountingyear, undef, 1).qq|</select>
1738	<input name=interval class=radio type=radio value=0 checked>&nbsp;|.$locale->text('Current').qq|
1739	<input name=interval class=radio type=radio value=1>&nbsp;|.$locale->text('Month').qq|
1740	<input name=interval class=radio type=radio value=3>&nbsp;|.$locale->text('Quarter').qq|
1741	<input name=interval class=radio type=radio value=12>&nbsp;|.$locale->text('Year').qq|
1742	</td>
1743      </tr>
1744|;
1745  }
1746
1747
1748  @f = ();
1749  push @f, qq|<input name="l_runningnumber" class=checkbox type=checkbox value=Y> |.$locale->text('No.');
1750  push @f, qq|<input name="l_id" class=checkbox type=checkbox value=Y> |.$locale->text('ID');
1751  push @f, qq|<input name="l_invnumber" class=checkbox type=checkbox value=Y checked> |.$locale->text('Invoice Number');
1752  push @f, qq|<input name="l_ordnumber" class=checkbox type=checkbox value=Y> |.$locale->text('Order Number');
1753  push @f, qq|<input name="l_description" class=checkbox type=checkbox value=Y checked> |.$locale->text('Description');
1754  push @f, qq|<input name="l_ponumber" class=checkbox type=checkbox value=Y> |.$locale->text('PO Number');
1755  push @f, qq|<input name="l_transdate" class=checkbox type=checkbox value=Y checked> |.$locale->text('Invoice Date');
1756  push @f, $l_name;
1757  push @f, $l_customernumber if $l_customernumber;
1758  push @f, $l_vendornumber if $l_vendornumber;
1759  push @f, qq|<input name="l_taxnumber" class=checkbox type=checkbox value=Y>$vctaxnumber|;
1760  push @f, qq|<input name="l_address" class=checkbox type=checkbox value=Y> |.$locale->text('Address');
1761  push @f, qq|<input name="l_city" class=checkbox type=checkbox value=Y> |.$locale->text('City');
1762  push @f, qq|<input name="l_zipcode" class=checkbox type=checkbox value=Y> |.$locale->text('Zipcode');
1763  push @f, qq|<input name="l_country" class=checkbox type=checkbox value=Y> |.$locale->text('Country');
1764  push @f, $l_employee if $l_employee;
1765  push @f, $l_department if $l_department;
1766  push @f, qq|<input name="l_netamount" class=checkbox type=checkbox value=Y> |.$locale->text('Amount');
1767  push @f, qq|<input name="l_tax" class=checkbox type=checkbox value=Y> |.$locale->text('Tax');
1768  push @f, qq|<input name="l_amount" class=checkbox type=checkbox value=Y checked> |.$locale->text('Total');
1769  push @f, qq|<input name="l_curr" class=checkbox type=checkbox value=Y> |.$locale->text('Currency');
1770  push @f, qq|<input name="l_datepaid" class=checkbox type=checkbox value=Y> |.$locale->text('Date Paid');
1771  push @f, qq|<input name="l_paymentdiff" class=checkbox type=checkbox value=Y> |.$locale->text('Payment Difference');
1772  push @f, qq|<input name="l_paid" class=checkbox type=checkbox value=Y checked> |.$locale->text('Paid');
1773  push @f, qq|<input name="l_paymentaccount" class=checkbox type=checkbox value=Y> |.$locale->text('Payment Account');
1774  push @f, qq|<input name="l_paymentmethod" class=checkbox type=checkbox value=Y> |.$locale->text('Payment Method');
1775  push @f, qq|<input name="l_duedate" class=checkbox type=checkbox value=Y> |.$locale->text('Due Date');
1776  push @f, qq|<input name="l_due" class=checkbox type=checkbox value=Y> |.$locale->text('Due');
1777  push @f, qq|<input name="l_memo" class=checkbox type=checkbox value=Y> |.$locale->text('Line Item');
1778  push @f, qq|<input name="l_notes" class=checkbox type=checkbox value=Y> |.$locale->text('Notes');
1779  push @f, $l_till if $l_till;
1780  push @f, $l_warehouse if $l_warehouse;
1781  push @f, qq|<input name="l_shippingpoint" class=checkbox type=checkbox value=Y> |.$locale->text('Shipping Point');
1782  push @f, qq|<input name="l_shipvia" class=checkbox type=checkbox value=Y> |.$locale->text('Ship via');
1783  push @f, qq|<input name="l_waybill" class=checkbox type=checkbox value=Y> |.$locale->text('Waybill');
1784  push @f, qq|<input name="l_dcn" class=checkbox type=checkbox value=Y> |.$locale->text('DCN');
1785
1786
1787  $form->header;
1788
1789  &calendar;
1790
1791  print qq|
1792<body>
1793
1794<form method=post name="main" action="$form->{script}">
1795
1796<table width=100%>
1797  <tr><th class=listtop>$form->{helpref}$form->{title}</a></th></tr>
1798  <tr height="5"></tr>
1799  <tr>
1800    <td>
1801      <table>
1802        <tr valign=top>
1803	  <td>
1804	    <table>
1805	      <tr>
1806		<th align=right>|.$locale->text('Account').qq|</th>
1807		<td colspan=3><select name=$form->{ARAP}>|
1808		.$form->select_option($form->{"select$form->{ARAP}"})
1809		.qq|</select></td>
1810	      </tr>
1811	      $vc
1812	      $invnumber
1813	      <tr>
1814		<th align=right nowrap>|.$locale->text('From').qq|</th>
1815		<td colspan=3 nowrap><input name=transdatefrom size=11 class=date title="$myconfig{dateformat}"> <b>|.&js_calendar("main", "transdatefrom").$locale->text('To').qq|</b> <input name=transdateto size=11 class=date title="$myconfig{dateformat}">|.&js_calendar("main", "transdateto").qq|</td>
1816	      </tr>
1817	      $selectfrom
1818	    </table>
1819	  </td>
1820
1821	  <td>
1822	    <table>
1823	      $employee
1824	      $department
1825	      $warehouse
1826	      <tr>
1827		<th align=right>|.$locale->text('Shipping Point').qq|</th>
1828		<td colspan=3><input name=shippingpoint size=40></td>
1829	      </tr>
1830	      <tr>
1831		<th align=right>|.$locale->text('Ship via').qq|</th>
1832		<td colspan=3><input name=shipvia size=40></td>
1833	      </tr>
1834	      <tr>
1835		<th align=right>|.$locale->text('Waybill').qq|</th>
1836		<td colspan=3><input name=waybill size=40></td>
1837	      </tr>
1838	    </table>
1839	  </td>
1840	</tr>
1841      </table>
1842    </td>
1843  </tr>
1844  <tr>
1845    <td>
1846      <table>
1847	<tr>
1848	  <th align=right nowrap>|.$locale->text('Include in Report').qq|</th>
1849	  <td>
1850	    <table width=100%>
1851	      $openclosed
1852	      $summary
1853|;
1854
1855  $form->{sort} = "transdate";
1856  $form->{title} = $title;
1857
1858  $form->hide_form(qw(title outstanding sort helpref));
1859
1860
1861  while (@f) {
1862    print qq|<tr>\n|;
1863    for (1 .. 5) {
1864      print qq|<td nowrap>|. shift @f;
1865      print qq|</td>\n|;
1866    }
1867    print qq|</tr>\n|;
1868  }
1869
1870  print qq|
1871	      <tr>
1872		<td nowrap><input name="l_subtotal" class=checkbox type=checkbox value=Y> |.$locale->text('Subtotal').qq|</td>
1873	      </tr>
1874	    </table>
1875	  </td>
1876	</tr>
1877      </table>
1878    </td>
1879  </tr>
1880  <tr>
1881    <td><hr size=3 noshade></td>
1882  </tr>
1883</table>
1884
1885<br>
1886<input type=hidden name=action value=continue>
1887<input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">|;
1888
1889  $form->hide_form(qw(nextsub path login));
1890
1891  print qq|
1892</form>
1893|;
1894
1895  if ($form->{menubar}) {
1896    require "$form->{path}/menu.pl";
1897    &menubar;
1898  }
1899
1900  print qq|
1901
1902</body>
1903</html>
1904|;
1905
1906}
1907
1908
1909sub transactions {
1910
1911  if ($form->{$form->{vc}}) {
1912    ($form->{$form->{vc}}, $form->{"$form->{vc}_id"}) = split(/--/, $form->{$form->{vc}});
1913  }
1914
1915  AA->transactions(\%myconfig, \%$form);
1916
1917  $href = "$form->{script}?action=transactions";
1918  for (qw(direction oldsort till outstanding path login summary)) { $href .= qq|&$_=$form->{$_}| }
1919  $href .= "&title=".$form->escape($form->{title});
1920  $href .= "&helpref=".$form->escape($form->{helpref});
1921
1922  $form->sort_order();
1923
1924  $callback = "$form->{script}?action=transactions";
1925  for (qw(direction oldsort till outstanding path login summary)) { $callback .= qq|&$_=$form->{$_}| }
1926  $callback .= "&title=".$form->escape($form->{title},1);
1927  $callback .= "&helpref=".$form->escape($form->{helpref},1);
1928
1929  if ($form->{$form->{ARAP}}) {
1930    $callback .= "&$form->{ARAP}=".$form->escape($form->{$form->{ARAP}},1);
1931    $href .= "&$form->{ARAP}=".$form->escape($form->{$form->{ARAP}});
1932    $form->{$form->{ARAP}} =~ s/--/ /;
1933    $option = $locale->text('Account')." : $form->{$form->{ARAP}}";
1934  }
1935
1936  if ($form->{$form->{vc}}) {
1937    $callback .= "&$form->{vc}=".$form->escape($form->{$form->{vc}},1).qq|--$form->{"$form->{vc}_id"}|;
1938    $href .= "&$form->{vc}=".$form->escape($form->{$form->{vc}}).qq|--$form->{"$form->{vc}_id"}|;
1939    $option .= "\n<br>" if ($option);
1940    $name = ($form->{vc} eq 'customer') ? $locale->text('Customer') : $locale->text('Vendor');
1941    $option .= "$name : $form->{$form->{vc}}";
1942  }
1943  if ($form->{"$form->{vc}number"}) {
1944    $callback .= "&$form->{vc}number=".$form->escape($form->{"$form->{vc}number"},1);
1945    $href .= "&$form->{vc}number=".$form->escape($form->{"$form->{vc}number"});
1946    $option .= "\n<br>" if ($option);
1947    $name = ($form->{vc} eq 'customer') ? $locale->text('Customer Number') : $locale->text('Vendor Number');
1948    $option .= qq|$name : $form->{"$form->{vc}number"}|;
1949  }
1950
1951  if ($form->{department}) {
1952    $callback .= "&department=".$form->escape($form->{department},1);
1953    $href .= "&department=".$form->escape($form->{department});
1954    ($department) = split /--/, $form->{department};
1955    $option .= "\n<br>" if ($option);
1956    $option .= $locale->text('Department')." : $department";
1957  }
1958  if ($form->{employee}) {
1959    $callback .= "&employee=".$form->escape($form->{employee},1);
1960    $href .= "&employee=".$form->escape($form->{employee});
1961    ($employee) = split /--/, $form->{employee};
1962    $option .= "\n<br>" if ($option);
1963    if ($form->{ARAP} eq 'AR') {
1964      $option .= $locale->text('Salesperson');
1965    } else {
1966      $option .= $locale->text('Employee');
1967    }
1968    $option .= " : $employee";
1969  }
1970
1971  if ($form->{invnumber}) {
1972    $callback .= "&invnumber=".$form->escape($form->{invnumber},1);
1973    $href .= "&invnumber=".$form->escape($form->{invnumber});
1974    $option .= "\n<br>" if ($option);
1975    $option .= $locale->text('Invoice Number')." : $form->{invnumber}";
1976  }
1977  if ($form->{description}) {
1978    $callback .= "&description=".$form->escape($form->{description},1);
1979    $href .= "&description=".$form->escape($form->{description});
1980    $option .= "\n<br>" if ($option);
1981    $option .= $locale->text('Description')." : $form->{description}";
1982  }
1983  if ($form->{ordnumber}) {
1984    $callback .= "&ordnumber=".$form->escape($form->{ordnumber},1);
1985    $href .= "&ordnumber=".$form->escape($form->{ordnumber});
1986    $option .= "\n<br>" if ($option);
1987    $option .= $locale->text('Order Number')." : $form->{ordnumber}";
1988  }
1989  if ($form->{ponumber}) {
1990    $callback .= "&ponumber=".$form->escape($form->{ponumber},1);
1991    $href .= "&ponumber=".$form->escape($form->{ponumber});
1992    $option .= "\n<br>" if ($option);
1993    $option .= $locale->text('PO Number')." : $form->{ponumber}";
1994  }
1995  if ($form->{notes}) {
1996    $callback .= "&notes=".$form->escape($form->{notes},1);
1997    $href .= "&notes=".$form->escape($form->{notes});
1998    $option .= "\n<br>" if $option;
1999    $option .= $locale->text('Notes')." : $form->{notes}";
2000  }
2001  if ($form->{warehouse}) {
2002    $callback .= "&warehouse=".$form->escape($form->{warehouse},1);
2003    $href .= "&warehouse=".$form->escape($form->{warehouse});
2004    ($warehouse) = split /--/, $form->{warehouse};
2005    $option .= "\n<br>" if ($option);
2006    $option .= $locale->text('Warehouse')." : $warehouse";
2007    delete $form->{l_warehouse};
2008  }
2009  if ($form->{shippingpoint}) {
2010    $callback .= "&shippingpoint=".$form->escape($form->{shippingpoint},1);
2011    $href .= "&shippingpoint=".$form->escape($form->{shippingpoint});
2012    $option .= "\n<br>" if ($option);
2013    $option .= $locale->text('Shipping Point')." : $form->{shippingpoint}";
2014  }
2015  if ($form->{shipvia}) {
2016    $callback .= "&shipvia=".$form->escape($form->{shipvia},1);
2017    $href .= "&shipvia=".$form->escape($form->{shipvia});
2018    $option .= "\n<br>" if ($option);
2019    $option .= $locale->text('Ship via')." : $form->{shipvia}";
2020  }
2021  if ($form->{waybill}) {
2022    $callback .= "&waybill=".$form->escape($form->{waybill},1);
2023    $href .= "&waybill=".$form->escape($form->{waybill});
2024    $option .= "\n<br>" if ($option);
2025    $option .= $locale->text('Waybill')." : $form->{waybill}";
2026  }
2027  if ($form->{memo}) {
2028    $callback .= "&memo=".$form->escape($form->{memo},1);
2029    $href .= "&memo=".$form->escape($form->{memo});
2030    $option .= "\n<br>" if $option;
2031    $option .= $locale->text('Line Item')." : $form->{memo}";
2032  }
2033  if ($form->{transdatefrom}) {
2034    $callback .= "&transdatefrom=$form->{transdatefrom}";
2035    $href .= "&transdatefrom=$form->{transdatefrom}";
2036    $option .= "\n<br>" if ($option);
2037    $option .= $locale->text('From')."&nbsp;".$locale->date(\%myconfig, $form->{transdatefrom}, 1);
2038  }
2039  if ($form->{transdateto}) {
2040    $callback .= "&transdateto=$form->{transdateto}";
2041    $href .= "&transdateto=$form->{transdateto}";
2042    $option .= "\n<br>" if ($option);
2043    $option .= $locale->text('To')."&nbsp;".$locale->date(\%myconfig, $form->{transdateto}, 1);
2044  }
2045  if ($form->{open}) {
2046    $callback .= "&open=$form->{open}";
2047    $href .= "&open=$form->{open}";
2048    $option .= "\n<br>" if ($option);
2049    $option .= $locale->text('Open');
2050  }
2051  if ($form->{closed}) {
2052    $callback .= "&closed=$form->{closed}";
2053    $href .= "&closed=$form->{closed}";
2054    $option .= "\n<br>" if ($option);
2055    $option .= $locale->text('Closed');
2056  }
2057  if ($form->{onhold}) {
2058    $callback .= "&onhold=$form->{onhold}";
2059    $href .= "&onhold=$form->{onhold}";
2060    $option .= "\n<br>" if ($option);
2061    $option .= $locale->text('On Hold');
2062  }
2063  if ($form->{paidlate}) {
2064    $callback .= "&paidlate=$form->{paidlate}";
2065    $href .= "&paidlate=$form->{paidlate}";
2066    $option .= "\n<br>" if ($option);
2067    $option .= $locale->text('Paid Late');
2068  }
2069  if ($form->{paidearly}) {
2070    $callback .= "&paidearly=$form->{paidearly}";
2071    $href .= "&paidearly=$form->{paidearly}";
2072    $option .= "\n<br>" if ($option);
2073    $option .= $locale->text('Paid Early');
2074  }
2075
2076
2077  @columns = $form->sort_columns(qw(transdate id invnumber ordnumber ponumber description name customernumber vendornumber taxnumber address city zipcode country netamount tax amount paid paymentaccount paymentmethod due curr datepaid duedate memo notes till employee warehouse shippingpoint shipvia waybill dcn paymentdiff department));
2078  unshift @columns, "runningnumber";
2079
2080  @column_index = qw(delete);
2081  @curr = split /:/, $form->{currencies};
2082
2083  foreach $item (@columns) {
2084    if ($form->{"l_$item"} eq "Y") {
2085      push @column_index, $item;
2086
2087      if ($form->{l_curr} && $item =~ /(amount|tax|paid|due)/) {
2088	for (@curr) {
2089	  push @column_index, "${_}_$item";
2090	}
2091      }
2092
2093      # add column to href and callback
2094      $callback .= "&l_$item=Y";
2095      $href .= "&l_$item=Y";
2096    }
2097  }
2098
2099  if (!$form->{summary}) {
2100    @f = grep !/memo/, @column_index;
2101    @column_index = (@f, (qw(source debit credit accno memo projectnumber)));
2102  }
2103
2104  if ($form->{l_subtotal} eq 'Y') {
2105    $callback .= "&l_subtotal=Y";
2106    $href .= "&l_subtotal=Y";
2107  }
2108
2109  if ($form->{vc} eq 'customer') {
2110    $employee = $locale->text('Salesperson');
2111    $name = $locale->text('Customer');
2112    $namenumber = $locale->text('Customer Number');
2113    $namefld = "customernumber";
2114    $vcnumber = $locale->text('Taxnumber');
2115  } else {
2116    $employee = $locale->text('Employee');
2117    $name = $locale->text('Vendor');
2118    $namenumber = $locale->text('Vendor Number');
2119    $namefld = "vendornumber";
2120    $vcnumber = $locale->text('SSN');
2121  }
2122
2123  $form->{allbox} = ($form->{allbox}) ? "checked" : "";
2124  $action = ($form->{deselect}) ? "deselect_all" : "select_all";
2125  $column_data{delete} = qq|<th class=listheading width=1%><input name="allbox" type=checkbox class=checkbox value="1" $form->{allbox} onChange="CheckAll(); javascript:main.submit()"><input type=hidden name=action value="$action"></th>|;
2126  $column_data{runningnumber} = qq|<th class=listheading>&nbsp;</th>|;
2127  $column_data{id} = "<th><a class=listheading href=$href&sort=id>".$locale->text('ID')."</a></th>";
2128  $column_data{transdate} = "<th><a class=listheading href=$href&sort=transdate>".$locale->text('Date')."</a></th>";
2129  $column_data{duedate} = "<th><a class=listheading href=$href&sort=duedate>".$locale->text('Due Date')."</a></th>";
2130  $column_data{invnumber} = "<th><a class=listheading href=$href&sort=invnumber>".$locale->text('Invoice')."</a></th>";
2131  $column_data{ordnumber} = "<th><a class=listheading href=$href&sort=ordnumber>".$locale->text('Order')."</a></th>";
2132  $column_data{ponumber} = "<th><a class=listheading href=$href&sort=ponumber>".$locale->text('PO Number')."</a></th>";
2133  $column_data{name} = "<th><a class=listheading href=$href&sort=name>$name</a></th>";
2134  $column_data{$namefld} = "<th><a class=listheading href=$href&sort=$namefld>$namenumber</a></th>";
2135  $column_data{taxnumber} = "<th><a class=listheading href=$href&sort=taxnumber>$vcnumber</th>";
2136  $column_data{address} = "<th class=listheading>" . $locale->text('Address') . "</th>";
2137  $column_data{city} = "<th><a class=listheading href=$href&sort=city>" . $locale->text('City') . "</th>";
2138  $column_data{zipcode} = "<th><a class=listheading href=$href&sort=zipcode>" . $locale->text('Zipcode') . "</th>";
2139  $column_data{country} = "<th><a class=listheading href=$href&sort=country>" . $locale->text('Country') . "</th>";
2140  $column_data{netamount} = "<th class=listheading>" . $locale->text('Amount') . "</th>";
2141  $column_data{tax} = "<th class=listheading>" . $locale->text('Tax') . "</th>";
2142  $column_data{amount} = "<th class=listheading>" . $locale->text('Total') . "</th>";
2143  $column_data{paid} = "<th class=listheading>" . $locale->text('Paid') . "</th>";
2144  $column_data{paymentaccount} = "<th><a class=listheading href=$href&sort=paymentaccount>" . $locale->text('Payment Account') . "</a></th>";
2145  $column_data{paymentmethod} = "<th><a class=listheading href=$href&sort=paymentmethod>" . $locale->text('Payment Method') . "</a></th>";
2146  $column_data{datepaid} = "<th><a class=listheading href=$href&sort=datepaid>" . $locale->text('Date Paid') . "</a></th>";
2147  $column_data{due} = "<th class=listheading>" . $locale->text('Due') . "</th>";
2148  $column_data{notes} = "<th class=listheading>".$locale->text('Notes')."</th>";
2149  $column_data{employee} = "<th><a class=listheading href=$href&sort=employee>$employee</a></th>";
2150  $column_data{till} = "<th><a class=listheading href=$href&sort=till>".$locale->text('Till')."</a></th>";
2151
2152  $column_data{warehouse} = qq|<th><a class=listheading href=$href&sort=warehouse>|.$locale->text('Warehouse').qq|</a></th>|;
2153
2154  $column_data{shippingpoint} = "<th><a class=listheading href=$href&sort=shippingpoint>" . $locale->text('Shipping Point') . "</a></th>";
2155  $column_data{shipvia} = "<th><a class=listheading href=$href&sort=shipvia>" . $locale->text('Ship via') . "</a></th>";
2156  $column_data{waybill} = "<th><a class=listheading href=$href&sort=waybill>" . $locale->text('Waybill') . "</a></th>";
2157  $column_data{dcn} = "<th><a class=listheading href=$href&sort=dcn>" . $locale->text('DCN') . "</a></th>";
2158  $column_data{paymentdiff} = "<th><a class=listheading href=$href&sort=paymentdiff>" . $locale->text('+/-') . "</a></th>";
2159
2160  $column_data{curr} = "<th><a class=listheading href=$href&sort=curr>" . $locale->text('Curr') . "</a></th>";
2161
2162  $column_data{department} = "<th><a class=listheading href=$href&sort=department>" . $locale->text('Department') . "</a></th>";
2163
2164  $column_data{accno} = "<th><a class=listheading href=$href&sort=accno>" . $locale->text('Account') . "</a></th>";
2165  $column_data{source} = "<th><a class=listheading href=$href&sort=source>" . $locale->text('Source') . "</a></th>";
2166  $column_data{debit} = "<th class=listheading>" . $locale->text('Debit') . "</th>";
2167  $column_data{credit} = "<th class=listheading>" . $locale->text('Credit') . "</th>";
2168  $column_data{projectnumber} = "<th><a class=listheading href=$href&sort=projectnumber>" . $locale->text('Project') . "</a></th>";
2169  $column_data{description} = "<th><a class=listheading href=$href&sort=description>" . $locale->text('Description') . "</a></th>";
2170  $column_data{memo} = "<th class=listheading>" . $locale->text('Line Item') . "</th>";
2171
2172  for $item (@curr) {
2173    for (qw(amount tax netamount paid due)) { $column_data{"${item}_$_"} = "<th>$item</th>" }
2174  }
2175
2176  $form->{title} = ($form->{outstanding}) ? $locale->text("$form->{ARAP} Outstanding") : $locale->text("$form->{ARAP} Transactions");
2177
2178  $form->{title} .= " / $form->{company}";
2179
2180  $form->header;
2181
2182  &check_all(qw(allbox id_));
2183
2184  print qq|
2185<body>
2186
2187<form method="post" name="main" action="$form->{script}" />
2188
2189<table width=100%>
2190  <tr>
2191    <th class=listtop>$form->{helpref}$form->{title}</a></th>
2192  </tr>
2193  <tr height="5"></tr>
2194  <tr>
2195    <td>$option</td>
2196  </tr>
2197  <tr>
2198    <td>
2199      <table width=100%>
2200	<tr class=listheading>
2201|;
2202
2203  for (@column_index) { print "\n$column_data{$_}" }
2204
2205  print qq|
2206	</tr>
2207|;
2208
2209
2210  # add sort and escape callback, this one we use for the add sub
2211  $form->{callback} = $callback .= "&sort=$form->{sort}";
2212
2213  # escape callback for href
2214  $callback = $form->escape($callback);
2215
2216  if (@{ $form->{transactions} }) {
2217    $sameitem = $form->{transactions}->[0]->{$form->{sort}};
2218  }
2219
2220  $i = 0;
2221  foreach $ref (@{ $form->{transactions} }) {
2222
2223    $i++;
2224    $checked{$i} = ($form->{"checked_$i"}) ? "checked" : "";
2225
2226    if ($form->{l_subtotal} eq 'Y') {
2227      if ($sameitem ne $ref->{$form->{sort}}) {
2228        &subtotal;
2229        $sameitem = $ref->{$form->{sort}};
2230      }
2231    }
2232
2233    if ($form->{l_curr}) {
2234      for $item (@curr) {
2235	for (qw(tax netamount amount paid due)) { $column_data{"${item}_$_"} = "<td>&nbsp;</td>" }
2236      }
2237
2238      if ($form->{defaultcurrency} ne $ref->{curr}) {
2239        for (qw(netamount amount paid)) { $ref->{"$ref->{curr}_$_"} = $ref->{$_}/$ref->{exchangerate} }
2240
2241        for (qw(netamount amount paid)) { $column_data{"$ref->{curr}_$_"} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{"$ref->{curr}_$_"}, $form->{precision}, "&nbsp;")."</td>" }
2242
2243        $column_data{"$ref->{curr}_tax"} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{"$ref->{curr}_amount"} - $ref->{"$ref->{curr}_netamount"}, $form->{precision}, "&nbsp;")."</td>";
2244        $column_data{"$ref->{curr}_due"} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{"$ref->{curr}_amount"} - $ref->{"$ref->{curr}_paid"}, $form->{precision}, "&nbsp;")."</td>";
2245
2246        $subtotal{$ref->{curr}}{netamount} += $ref->{"$ref->{curr}_netamount"};
2247        $subtotal{$ref->{curr}}{amount} += $ref->{"$ref->{curr}_amount"};
2248        $subtotal{$ref->{curr}}{paid} += $ref->{"$ref->{curr}_paid"};
2249
2250        $total{$ref->{curr}}{netamount} += $ref->{"$ref->{curr}_netamount"};
2251        $total{$ref->{curr}}{amount} += $ref->{"$ref->{curr}_amount"};
2252        $total{$ref->{curr}}{paid} += $ref->{"$ref->{curr}_paid"};
2253      }
2254
2255    }
2256
2257    $column_data{runningnumber} = "<td align=right>$i</td>";
2258
2259    if ($ref->{paid}) {
2260      $column_data{delete} = qq|<td>&nbsp;</td>|;
2261    } else {
2262      $column_data{delete} = qq|<td><input name="id_$i" class=checkbox type=checkbox value=$ref->{id} $checked{$i}></td>
2263      <input name="reference_$i" type="hidden" value="$ref->{invnumber}">
2264|;
2265    }
2266
2267    for (qw(netamount amount paid debit credit)) { $column_data{$_} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{$_}, $form->{precision}, "&nbsp;")."</td>" }
2268
2269    $column_data{tax} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{amount} - $ref->{netamount}, $form->{precision}, "&nbsp;")."</td>";
2270    $column_data{due} = "<td align=right>".$form->format_amount(\%myconfig, $ref->{amount} - $ref->{paid}, $form->{precision}, "&nbsp;")."</td>";
2271
2272
2273    $subtotalnetamount += $ref->{netamount};
2274    $subtotalamount += $ref->{amount};
2275    $subtotalpaid += $ref->{paid};
2276    $subtotaldebit += $ref->{debit};
2277    $subtotalcredit += $ref->{credit};
2278
2279    $totalnetamount += $ref->{netamount};
2280    $totalamount += $ref->{amount};
2281    $totalpaid += $ref->{paid};
2282    $totaldebit += $ref->{debit};
2283    $totalcredit += $ref->{credit};
2284
2285    $module = ($ref->{invoice}) ? ($form->{ARAP} eq 'AR') ? "is.pl" : "ir.pl" : $form->{script};
2286    $module = ($ref->{till}) ? "ps.pl" : $module;
2287
2288    $column_data{invnumber} = "<td><a href=$module?action=edit&id=$ref->{id}&path=$form->{path}&login=$form->{login}&callback=$callback>$ref->{invnumber}&nbsp;</a></td>";
2289
2290    for (qw(notes description memo)) { $ref->{$_} =~ s/\r?\n/<br>/g }
2291    for (qw(transdate datepaid duedate)) { $column_data{$_} = "<td nowrap>$ref->{$_}&nbsp;</td>" }
2292    for (qw(department ordnumber ponumber notes warehouse shippingpoint shipvia waybill employee till source memo description projectnumber taxnumber address city zipcode country dcn paymentaccount paymentmethod)) { $column_data{$_} = "<td>$ref->{$_}&nbsp;</td>" }
2293    $column_data{$namefld} = "<td>$ref->{$namefld}&nbsp;</td>";
2294
2295    if ($ref->{paymentdiff} <= 0) {
2296      $column_data{paymentdiff} = qq|<td class="plus1" align=right>$ref->{paymentdiff}&nbsp;</td>|;
2297    } else {
2298      $column_data{paymentdiff} = qq|<td class="plus0" align=right>+$ref->{paymentdiff}&nbsp;</td>|;
2299    }
2300
2301    for (qw(id curr accno)) { $column_data{$_} = "<td>$ref->{$_}</td>" }
2302
2303    $column_data{name} = qq|<td><a href=ct.pl?path=$form->{path}&login=$form->{login}&action=edit&id=$ref->{"$form->{vc}_id"}&db=$form->{vc}&callback=$callback>$ref->{name}</a></td>|;
2304
2305    if ($ref->{id} != $sameid) {
2306      $j++; $j %= 2;
2307    }
2308
2309    print "
2310        <tr class=listrow$j>
2311";
2312
2313    for (@column_index) { print "\n$column_data{$_}" }
2314
2315    print qq|
2316        </tr>
2317|;
2318    $sameid = $ref->{id};
2319  }
2320
2321  if ($form->{l_subtotal} eq 'Y') {
2322    &subtotal;
2323  }
2324
2325  $form->{rowcount} = $i;
2326
2327  # print totals
2328  print qq|
2329        <tr class=listtotal>
2330|;
2331
2332  for (@column_index) { $column_data{$_} = "<td>&nbsp;</td>" }
2333
2334  $column_data{netamount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalnetamount, $form->{precision}, "&nbsp;")."</th>";
2335  $column_data{tax} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount - $totalnetamount, $form->{precision}, "&nbsp;")."</th>";
2336  $column_data{amount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount, $form->{precision}, "&nbsp;")."</th>";
2337  $column_data{paid} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalpaid, $form->{precision}, "&nbsp;")."</th>";
2338  $column_data{due} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount - $totalpaid, $form->{precision}, "&nbsp;")."</th>";
2339  $column_data{debit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totaldebit, $form->{precision}, "&nbsp;")."</th>";
2340  $column_data{credit} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalcredit, $form->{precision}, "&nbsp;")."</th>";
2341
2342  if ($form->{l_curr}) {
2343    for $item (@curr) {
2344      $column_data{"${item}_netamount"} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $total{$item}{netamount}, $form->{precision}, "&nbsp;")."</th>";
2345      $column_data{"${item}_tax"} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $total{$item}{amount} - $total{$item}{netamount}, $form->{precision}, "&nbsp;")."</th>";
2346      $column_data{"${item}_amount"} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $total{$item}{amount}, $form->{precision}, "&nbsp;")."</th>";
2347      $column_data{"${item}_paid"} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $total{$item}{paid}, $form->{precision}, "&nbsp;")."</th>";
2348      $column_data{"${item}_due"} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $total{$item}{amount} - $total{$item}{paid}, $form->{precision}, "&nbsp;")."</th>";
2349    }
2350  }
2351
2352  for (@column_index) { print "\n$column_data{$_}" }
2353
2354  if ($myconfig{acs} !~ /$form->{ARAP}--$form->{ARAP}/) {
2355    $i = 1;
2356
2357    $button{'Select all'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Select all').qq|"> |;
2358    $button{'Select all'}{order} = $i++;
2359
2360    $button{'Deselect all'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Deselect all').qq|"> |;
2361    $button{'Deselect all'}{order} = $i++;
2362
2363    if ($form->{ARAP} eq 'AR') {
2364      unless ($form->{till}) {
2365        $button{'AR--Add Transaction'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('AR Transaction').qq|"> |;
2366        $button{'AR--Add Transaction'}{order} = $i++;
2367        $button{'AR--Sales Invoice'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Sales Invoice.').qq|"> |;
2368        $button{'AR--Sales Invoice'}{order} = $i++;
2369      }
2370
2371      $button{'AR--Delete Transactions'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Delete Transactions').qq|"> |;
2372      $button{'AR--Delete Transactions'}{order} = $i++;
2373
2374      if ($myconfig{acs} =~ /AR--Add Transaction/) {
2375        $myconfig{acs} .= ";AR--Delete Transactions";
2376      }
2377
2378    } else {
2379      $button{'AP--Add Transaction'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('AP Transaction').qq|"> |;
2380      $button{'AP--Add Transaction'}{order} = $i++;
2381      $button{'AP--Vendor Invoice'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Vendor Invoice.').qq|"> |;
2382      $button{'AP--Vendor Invoice'}{order} = $i++;
2383
2384      $button{'AP--Delete Transactions'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Delete Transactions').qq|"> |;
2385      $button{'AP--Delete Transactions'}{order} = $i++;
2386
2387      if ($myconfig{acs} =~ /AP--Add Transaction/) {
2388        $myconfig{acs} .= ";AP--Delete Transactions";
2389      }
2390
2391    }
2392
2393    if ($form->{deselect}) {
2394      delete $button{'Select all'};
2395    } else {
2396      delete $button{'Deselect all'};
2397    }
2398
2399    foreach $item (split /;/, $myconfig{acs}) {
2400      delete $button{$item};
2401    }
2402  }
2403
2404  print qq|
2405	</tr>
2406      </table>
2407    </td>
2408  </tr>
2409  <tr>
2410    <td><hr size=3 noshade></td>
2411  </tr>
2412</table>
2413
2414<br>
2415|;
2416
2417  $form->{helpref} = $form->escape($form->{helpref},1);
2418
2419  $form->hide_form("$form->{vc}", "$form->{vc}_id");
2420  $form->hide_form(qw(rowcount helpref callback path login));
2421
2422  foreach $item (sort { $a->{order} <=> $b->{order} } %button) {
2423    print $item->{code};
2424  }
2425
2426  if ($form->{menubar}) {
2427    require "$form->{path}/menu.pl";
2428    &menubar;
2429  }
2430
2431  print qq|
2432</form>
2433
2434</body>
2435</html>
2436|;
2437
2438}
2439
2440
2441sub subtotal {
2442
2443  for (@column_index) { $column_data{$_} = "<th>&nbsp;</th>" }
2444
2445  $column_data{tax} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount, $form->{precision}, "&nbsp;")."</th>";
2446  $column_data{amount} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount, $form->{precision}, "&nbsp;")."</th>";
2447  $column_data{paid} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalpaid, $form->{precision}, "&nbsp;")."</th>";
2448  $column_data{due} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount - $subtotalpaid, $form->{precision}, "&nbsp;")."</th>";
2449  $column_data{debit} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotaldebit, $form->{precision}, "&nbsp;")."</th>";
2450  $column_data{credit} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalcredit, $form->{precision}, "&nbsp;")."</th>";
2451
2452
2453  if ($form->{l_curr}) {
2454    for $item (@curr) {
2455      $column_data{"${item}_tax"} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotal{$item}{amount} - $subtotal{$item}{netamount}, $form->{precision}, "&nbsp;")."</th>";
2456      $column_data{"${item}_amount"} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotal{$item}{amount}, $form->{precision}, "&nbsp;")."</th>";
2457      $column_data{"${item}_paid"} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotal{$item}{paid}, $form->{precision}, "&nbsp;")."</th>";
2458      $column_data{"${item}_due"} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotal{$item}{mount} - $subtotal{$item}{paid}, $form->{precision}, "&nbsp;")."</th>";
2459    }
2460  }
2461
2462  $subtotalnetamount = 0;
2463  $subtotalamount = 0;
2464  $subtotalpaid = 0;
2465  $subtotaldebit = 0;
2466  $subtotalcredit = 0;
2467
2468  print "<tr class=listsubtotal>";
2469
2470  for (@column_index) { print "\n$column_data{$_}" }
2471
2472print "
2473</tr>
2474";
2475
2476  for $item (@curr) {
2477    for (map { "${item}_$_" } qw(tax amount paid due)) { $column_data{$_} = "<th>&nbsp;</th>" }
2478    $subtotal{$item}{netamount} = 0;
2479    $subtotal{$item}{amount} = 0;
2480    $subtotal{$item}{paid} = 0;
2481  }
2482
2483}
2484
2485
2486sub select_all {
2487
2488  $form->{callback} .= "&allbox=1&deselect=1";
2489  for $i (1 .. $form->{rowcount}) { $form->{callback} .= "&checked_$i=1" }
2490  $form->redirect;
2491
2492}
2493
2494
2495sub deselect_all { $form->redirect }
2496
2497
2498sub delete_transactions {
2499
2500  for (1 .. $form->{rowcount}) {
2501    if ($form->{"id_$_"}) {
2502      $ok = 1;
2503      last;
2504    }
2505  }
2506
2507  $form->error($locale->text('Nothing selected!')) unless $ok;
2508
2509  $form->{title} = $locale->text('Confirm!');
2510
2511  $form->header;
2512
2513  print qq|
2514<body>
2515
2516<form method="post" name="main" action=$form->{script}>
2517
2518<h2 class=confirm>$form->{title}</h2>
2519
2520<h4>|.$locale->text('Are you sure you want to delete the following Transactions?').qq|</h4>|;
2521
2522  for (1 .. $form->{rowcount}) {
2523    if ($form->{"id_$_"}) {
2524      print qq|$form->{"reference_$_"}<br>|;
2525    }
2526  }
2527
2528  $form->{action} = "yes__delete_transactions";
2529
2530  $form->hide_form;
2531
2532  print qq|
2533<p>
2534<input name=action class=submit type=submit value="|.$locale->text('Yes, delete transactions').qq|">
2535
2536</form>
2537
2538</body>
2539</html>
2540|;
2541
2542}
2543
2544
2545sub yes__delete_transactions {
2546
2547  $SIG{INT} = 'IGNORE';
2548
2549  $form->redirect if AA->delete_transactions(\%myconfig, \%$form, $spool);
2550
2551  $form->error($locale->text('Unable to delete:')." $form->{notdeleted}");
2552
2553}
2554
2555
2556