Subversion Repositories sql-ledger-old

Rev

Rev 114 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 finnarne 1
#=====================================================================
2
# SQL-Ledger Accounting
3
# Copyright (c) 1998-2002
4
#
5
#  Author: Dieter Simader
6
#   Email: dsimader@sql-ledger.org
7
#     Web: http://www.sql-ledger.org
8
#
9
#
10
# This program is free software; you can redistribute it and/or modify
11
# it under the terms of the GNU General Public License as published by
12
# the Free Software Foundation; either version 2 of the License, or
13
# (at your option) any later version.
14
#
15
# This program is distributed in the hope that it will be useful,
16
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
# GNU General Public License for more details.
19
# You should have received a copy of the GNU General Public License
20
# along with this program; if not, write to the Free Software
21
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
#======================================================================
23
#
24
# Inventory invoicing module
25
#
26
#======================================================================
27
 
28
 
29
use SL::IS;
30
use SL::PE;
31
 
32
require "$form->{path}/io.pl";
33
require "$form->{path}/arap.pl";
34
 
35
 
36
1;
37
# end of main
38
 
39
 
40
 
41
sub add {
42
 
43
  $form->{title} = $locale->text('Add Sales Invoice');
44
 
45
  &invoice_links;
46
  &prepare_invoice;
47
  &display_form;
48
 
49
}
50
 
51
 
52
sub edit {
53
 
54
  $form->{title} = $locale->text('Edit Sales Invoice');
55
 
56
  &invoice_links;
57
  &prepare_invoice;
58
  &display_form;
59
 
60
}
61
 
62
 
63
sub invoice_links {
64
 
65
  # create links
66
  $form->create_links("AR", \%myconfig, "customer");
67
 
68
  IS->get_customer(\%myconfig, \%$form);
69
  IS->retrieve_invoice(\%myconfig, \%$form);
70
 
114 finnarne 71
  $form->get_partsgroup(\%myconfig);
72
  if (@{ $form->{all_partsgroup} }) {
73
    $form->{selectpartsgroup} = "<option>\n";
74
    map { $form->{selectpartsgroup} .= "<option>$_->{partsgroup}\n" } @{ $form->{all_partsgroup} };
75
  }
76
 
77
  if (@{ $form->{all_project} }) {
78
    $form->{selectprojectnumber} = "<option>\n";
79
    map { $form->{selectprojectnumber} .= "<option>$_->{projectnumber}\n" } @{ $form->{all_project} };
80
  }
81
 
82
 
2 finnarne 83
  # currencies
84
  @curr = split /:/, $form->{currencies};
85
  chomp $curr[0];
86
  $form->{defaultcurrency} = $curr[0];
87
 
88
  map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
89
 
90
  $form->{oldcustomer} = "$form->{customer}--$form->{customer_id}";
114 finnarne 91
  $form->{oldinvdate} = $form->{invdate};
2 finnarne 92
 
93
  if (@{ $form->{all_customer} }) {
94
    $form->{customer} = qq|$form->{customer}--$form->{customer_id}|;
95
    map { $form->{selectcustomer} .= "<option>$_->{name}--$_->{id}\n" } (@{ $form->{all_customer} });
96
  }
97
 
98
  # forex
99
  $form->{forex} = $form->{exchangerate};
100
  $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
101
 
102
  foreach $key (keys %{ $form->{AR_links} }) {
103
 
104
    foreach $ref (@{ $form->{AR_links}{$key} }) {
105
      $form->{"select$key"} .= "<option>$ref->{accno}--$ref->{description}\n";
106
    }
107
 
108
    if ($key eq "AR_paid") {
109
      for $i (1 .. scalar @{ $form->{acc_trans}{$key} }) {
110
        $form->{"AR_paid_$i"} = "$form->{acc_trans}{$key}->[$i-1]->{accno}--$form->{acc_trans}{$key}->[$i-1]->{description}";
111
        # reverse paid
112
        $form->{"paid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{amount} * -1;
113
        $form->{"datepaid_$i"} = $form->{acc_trans}{$key}->[$i-1]->{transdate};
114
        $form->{"forex_$i"} = $form->{"exchangerate_$i"} = $form->{acc_trans}{$key}->[$i-1]->{exchangerate};
115
        $form->{"source_$i"} = $form->{acc_trans}{$key}->[$i-1]->{source};
116
        $form->{paidaccounts} = $i;
117
      }
118
    } else {
119
      $form->{$key} = "$form->{acc_trans}{$key}->[0]->{accno}--$form->{acc_trans}{$key}->[0]->{description}";
120
    }
121
  }
122
 
123
  $form->{paidaccounts} = 1 unless (exists $form->{paidaccounts});
124
 
19 finnarne 125
  $form->{AR} = $form->{AR_1} unless $form->{id};
126
 
114 finnarne 127
  $form->{locked} = ($form->{revtrans}) ? '1' : ($form->datetonum($form->{invdate}, \%myconfig) <= $form->datetonum($form->{closedto}, \%myconfig));
2 finnarne 128
 
114 finnarne 129
  $form->{readonly} = 1 if $myconfig{acs} =~ /AR--Sales Invoice/;
130
 
2 finnarne 131
}
132
 
133
 
134
sub prepare_invoice {
135
 
136
  $form->{type} = "invoice";
137
  $form->{format} = "html";
138
  $form->{media} = "screen";
139
 
140
  if ($form->{id}) {
141
 
142
    map { $form->{$_} =~ s/"/&quot;/g } qw(invnumber ordnumber shippingpoint notes);
143
 
144
    foreach $ref (@{ $form->{invoice_details} } ) {
145
      $i++;
146
      map { $form->{"${_}_$i"} = $ref->{$_} } keys %{ $ref };
147
      $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
148
 
149
      ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
150
      $dec = length $dec;
151
      $decimalplaces = ($dec > 2) ? $dec : 2;
152
 
153
      $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
154
      $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
155
 
156
      map { $form->{"${_}_$i"} =~ s/"/&quot;/g } qw(partnumber description unit);
157
      $form->{rowcount} = $i;
158
    }
159
  }
160
 
161
}
162
 
163
 
164
 
165
sub form_header {
166
 
167
 
168
  # set option selected
169
  foreach $item (qw(AR customer currency)) {
170
    $form->{"select$item"} =~ s/ selected//;
171
    $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
172
  }
173
 
174
  $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
175
 
176
  $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
177
  $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
178
 
179
 
180
  $exchangerate = "";
181
  if ($form->{currency} ne $form->{defaultcurrency}) {
182
    if ($form->{forex}) {
114 finnarne 183
      $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td>$form->{exchangerate}<input type=hidden name=exchangerate value=$form->{exchangerate}></td>|;
2 finnarne 184
    } else {
114 finnarne 185
      $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
2 finnarne 186
    }
187
  }
188
  $exchangerate .= qq|
189
<input type=hidden name=forex value=$form->{forex}>
190
|;
191
 
192
  $customer = ($form->{selectcustomer}) ? qq|<select name=customer>$form->{selectcustomer}</select>\n<input type=hidden name="selectcustomer" value="$form->{selectcustomer}">| : qq|<input name=customer value="$form->{customer}" size=35>|;
193
 
194
  $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
195
 
114 finnarne 196
 
2 finnarne 197
  $form->header;
198
 
199
  print qq|
200
<body>
201
 
202
<form method=post action=$form->{script}>
203
 
204
<input type=hidden name=id value=$form->{id}>
205
 
206
<input type=hidden name=type value=$form->{type}>
207
<input type=hidden name=media value=$form->{media}>
208
<input type=hidden name=format value=$form->{format}>
209
 
210
<input type=hidden name=title value="$form->{title}">
211
<input type=hidden name=vc value="customer">
212
<input type=hidden name=employee value="$form->{employee}">
213
 
214
<input type=hidden name=discount value=$form->{discount}>
215
<input type=hidden name=creditlimit value=$form->{creditlimit}>
216
<input type=hidden name=creditremaining value=$form->{creditremaining}>
217
 
114 finnarne 218
<input type=hidden name=terms value=$form->{terms}>
219
 
2 finnarne 220
<input type=hidden name=closedto value=$form->{closedto}>
221
<input type=hidden name=locked value=$form->{locked}>
222
 
114 finnarne 223
<input type=hidden name=oldinvdate value=$form->{oldinvdate}>
224
 
225
<input type=hidden name=selectpartsgroup value="$form->{selectpartsgroup}">
226
<input type=hidden name=selectprojectnumber value="$form->{selectprojectnumber}">
227
 
2 finnarne 228
<table width=100%>
229
  <tr class=listtop>
19 finnarne 230
    <th class=listtop>$form->{title}</font></th>
2 finnarne 231
  </tr>
232
  <tr height="5"></tr>
233
  <tr>
234
    <td>
235
      <table width=100%>
114 finnarne 236
        <tr valign=top>
2 finnarne 237
          <td>
114 finnarne 238
            <table>
2 finnarne 239
              <tr>
240
                <th align=right nowrap>|.$locale->text('Record in').qq|</th>
114 finnarne 241
                <td colspan=3><select name=AR>$form->{selectAR}</select></td>
2 finnarne 242
                <input type=hidden name=selectAR value="$form->{selectAR}">
243
              </tr>
244
              <tr>
245
                <th align=right nowrap>|.$locale->text('Customer').qq|</th>
114 finnarne 246
                <td colspan=3>$customer</td>
2 finnarne 247
                <input type=hidden name=customer_id value=$form->{customer_id}>
248
                <input type=hidden name=oldcustomer value="$form->{oldcustomer}">
249
              </tr>
250
              <tr>
251
                <td></td>
114 finnarne 252
                <td colspan=3>
2 finnarne 253
                  <table width=100%>
254
                    <tr>
114 finnarne 255
                      <th align=left nowrap>|.$locale->text('Credit Limit').qq|</th>
2 finnarne 256
                      <td>$form->{creditlimit}</td>
114 finnarne 257
                      <th align=left nowrap>|.$locale->text('Remaining').qq|</th>
2 finnarne 258
                      <td class="plus$n">$form->{creditremaining}</font></td>
259
                    </tr>
260
                  </table>
261
                </td>
262
              </tr>
263
              <tr>
264
                <th align=right nowrap>|.$locale->text('Currency').qq|</th>
265
                <td><select name=currency>$form->{selectcurrency}</select></td>
266
                <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
267
                <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
268
                <input type=hidden name=fxgain_accno value=$form->{fxgain_accno}>
269
                <input type=hidden name=fxloss_accno value=$form->{fxloss_accno}>
114 finnarne 270
                $exchangerate
2 finnarne 271
              </tr>
272
              <tr>
273
                <th align=right nowrap>|.$locale->text('Ship via').qq|</th>
114 finnarne 274
                <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
2 finnarne 275
              </tr>
276
            </table>
277
          </td>
278
          <td align=right>
114 finnarne 279
            <table>
2 finnarne 280
              <tr>
281
                <th align=right nowrap>|.$locale->text('Invoice Number').qq|</th>
282
                <td><input name=invnumber size=11 value="$form->{invnumber}"></td>
283
              </tr>
284
              <tr>
285
                <th align=right>|.$locale->text('Invoice Date').qq|</th>
286
                <td><input name=invdate size=11 title="$myconfig{dateformat}" value=$form->{invdate}></td>
287
              </tr>
288
              <tr>
114 finnarne 289
                <th align=right>|.$locale->text('Due Date').qq|</th>
2 finnarne 290
                <td><input name=duedate size=11 title="$myconfig{dateformat}" value=$form->{duedate}></td>
291
              </tr>
292
              <tr>
293
                <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
294
                <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
295
              </tr>
296
            </table>
297
          </td>
298
        </tr>
299
      </table>
300
    </td>
301
  </tr>
302
  <tr>
303
    <td>
304
    </td>
305
  </tr>
306
<!-- shipto are in hidden variables -->
307
 
308
<input type=hidden name=shiptoname value="$form->{shiptoname}">
309
<input type=hidden name=shiptoaddr1 value="$form->{shiptoaddr1}">
310
<input type=hidden name=shiptoaddr2 value="$form->{shiptoaddr2}">
311
<input type=hidden name=shiptoaddr3 value="$form->{shiptoaddr3}">
312
<input type=hidden name=shiptoaddr4 value="$form->{shiptoaddr4}">
313
<input type=hidden name=shiptocontact value="$form->{shiptocontact}">
314
<input type=hidden name=shiptophone value="$form->{shiptophone}">
315
<input type=hidden name=shiptofax value="$form->{shiptofax}">
316
<input type=hidden name=shiptoemail value="$form->{shiptoemail}">
317
 
318
<!-- email variables -->
319
<input type=hidden name=message value="$form->{message}">
320
<input type=hidden name=email value="$form->{email}">
321
<input type=hidden name=subject value="$form->{subject}">
322
<input type=hidden name=cc value="$form->{cc}">
323
<input type=hidden name=bcc value="$form->{bcc}">
324
 
325
<input type=hidden name=taxaccounts value="$form->{taxaccounts}">
326
|;
327
 
328
  foreach $item (split / /, $form->{taxaccounts}) {
329
    print qq|
330
<input type=hidden name="${item}_rate" value="$form->{"${item}_rate"}">
331
<input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
332
<input type=hidden name="${item}_taxnumber" value="$form->{"${item}_taxnumber"}">
333
|;
334
  }
335
 
336
}
337
 
338
 
339
 
340
sub form_footer {
341
 
342
  $form->{invtotal} = $form->{invsubtotal};
343
 
344
  if (($rows = $form->numtextrows($form->{notes}, 50, 8)) < 2) {
345
    $rows = 2;
346
  }
347
  $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
348
 
114 finnarne 349
 
350
  $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
351
 
352
  if ($form->{taxaccounts}) {
353
    $taxincluded = qq|
354
                <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|.$locale->text('Tax Included').qq|</b><br><br>
355
|;
356
  }
2 finnarne 357
 
358
  if (!$form->{taxincluded}) {
359
 
360
    foreach $item (split / /, $form->{taxaccounts}) {
361
      if ($form->{"${item}_base"}) {
362
        $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
363
        $form->{invtotal} += $form->{"${item}_total"};
364
        $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
365
 
366
        $tax .= qq|
367
              <tr>
368
                <th align=right>$form->{"${item}_description"}</th>
369
                <td align=right>$form->{"${item}_total"}</td>
370
              </tr>
371
|;
372
      }
373
    }
374
 
375
    $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
376
 
377
    $subtotal = qq|
378
              <tr>
379
                <th align=right>|.$locale->text('Subtotal').qq|</th>
380
                <td align=right>$form->{invsubtotal}</td>
381
              </tr>
382
|;
383
 
384
  }
385
 
386
  $form->{oldinvtotal} = $form->{invtotal};
387
  $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
388
 
389
  print qq|
390
  <tr>
391
    <td>
392
      <table width=100%>
393
        <tr valign=bottom>
394
          <td>
395
            <table>
396
              <tr>
397
                <th align=left>|.$locale->text('Notes').qq|</th>
398
              </tr>
399
              <tr>
400
                <td>$notes</td>
401
              </tr>
402
            </table>
403
          </td>
114 finnarne 404
          <td align=right>
405
            $taxincluded
406
            <table>
2 finnarne 407
              $subtotal
408
              $tax
409
              <tr>
410
                <th align=right>|.$locale->text('Total').qq|</th>
411
                <td align=right>$form->{invtotal}</td>
412
              </tr>
413
            </table>
414
          </td>
415
        </tr>
416
      </table>
417
    </td>
418
  </tr>
419
  <tr>
420
    <td>
421
      <table width=100%>
422
        <tr class=listheading>
19 finnarne 423
          <th class=listheading colspan=5>|.$locale->text('Payments')
2 finnarne 424
          .qq|</font></th>
425
        </tr>
426
|;
427
 
114 finnarne 428
  if ($form->{currency} eq $form->{defaultcurrency}) {
429
    @column_index = qw(datepaid source paid AR_paid);
430
  } else {
431
    @column_index = qw(datepaid source paid exchangerate AR_paid);
432
  }
2 finnarne 433
 
434
  $column_data{datepaid} = "<th>".$locale->text('Date')."</th>";
435
  $column_data{paid} = "<th>".$locale->text('Amount')."</th>";
436
  $column_data{exchangerate} = "<th>".$locale->text('Exch')."</th>";
437
  $column_data{AR_paid} = "<th>".$locale->text('Account')."</th>";
438
  $column_data{source} = "<th>".$locale->text('Source')."</th>";
439
 
440
  print "
441
        <tr>
442
";
443
  map { print "$column_data{$_}\n" } @column_index;
444
  print "
445
        </tr>
446
";
447
 
448
  $form->{paidaccounts}++ if ($form->{"paid_$form->{paidaccounts}"});
449
  for $i (1 .. $form->{paidaccounts}) {
450
 
451
    print "
452
        <tr>\n";
453
 
454
    $form->{"selectAR_paid_$i"} = $form->{selectAR_paid};
455
    $form->{"selectAR_paid_$i"} =~ s/option>\Q$form->{"AR_paid_$i"}\E/option selected>$form->{"AR_paid_$i"}/;
456
 
457
    # format amounts
458
    $totalpaid += $form->{"paid_$i"};
459
    $form->{"paid_$i"} = $form->format_amount(\%myconfig, $form->{"paid_$i"}, 2);
460
    $form->{"exchangerate_$i"} = $form->format_amount(\%myconfig, $form->{"exchangerate_$i"});
461
 
462
    $exchangerate = qq|&nbsp;|;
463
    if ($form->{currency} ne $form->{defaultcurrency}) {
464
      if ($form->{"forex_$i"}) {
465
        $exchangerate = qq|<input type=hidden name="exchangerate_$i" value=$form->{"exchangerate_$i"}>$form->{"exchangerate_$i"}|;
466
      } else {
467
        $exchangerate = qq|<input name="exchangerate_$i" size=10 value=$form->{"exchangerate_$i"}>|;
468
      }
469
    }
470
 
471
    $exchangerate .= qq|
472
<input type=hidden name="forex_$i" value=$form->{"forex_$i"}>
473
|;
474
 
475
    $column_data{"paid_$i"} = qq|<td align=center><input name="paid_$i" size=11 value=$form->{"paid_$i"}></td>|;
476
    $column_data{"exchangerate_$i"} = qq|<td align=center>$exchangerate</td>|;
477
    $column_data{"AR_paid_$i"} = qq|<td align=center><select name="AR_paid_$i">$form->{"selectAR_paid_$i"}</select></td>|;
478
    $column_data{"datepaid_$i"} = qq|<td align=center><input name="datepaid_$i" size=11 title="$myconfig{dateformat}" value=$form->{"datepaid_$i"}></td>|;
479
    $column_data{"source_$i"} = qq|<td align=center><input name="source_$i" size=11 value="$form->{"source_$i"}"></td>|;
480
 
481
    map { print qq|$column_data{"${_}_$i"}\n| } @column_index;
482
    print "
483
        </tr>\n";
484
  }
485
 
486
  print qq|
487
<input type=hidden name=paidaccounts value=$form->{paidaccounts}>
488
<input type=hidden name=selectAR_paid value="$form->{selectAR_paid}">
489
<input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
490
<input type=hidden name=oldtotalpaid value=$totalpaid>
491
      </table>
492
    </td>
493
  </tr>
494
  <tr>
495
    <td>
496
|;
497
 
498
  &print_options;
499
 
500
  print qq|
501
    </td>
502
  </tr>
503
  <tr>
504
    <td><hr size=3 noshade></td>
505
  </tr>
506
</table>
507
|;
508
 
509
 
510
  $invdate = $form->datetonum($form->{invdate}, \%myconfig);
511
  $closedto = $form->datetonum($form->{closedto}, \%myconfig);
512
 
114 finnarne 513
  if (! $form->{readonly}) {
514
    if ($form->{id}) {
515
      print qq|
516
      <input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
517
      <input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
518
      <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
519
      <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
520
  |;
521
 
2 finnarne 522
      if (!$form->{locked}) {
523
        print qq|
524
        <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">
525
        <input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
114 finnarne 526
  |;
2 finnarne 527
      }
528
 
529
      print qq|
114 finnarne 530
        <input class=submit type=submit name=action value="|.$locale->text('Post as new').qq|">
531
        <input class=submit type=submit name=action value="|.$locale->text('Order').qq|">
532
  |;
2 finnarne 533
 
114 finnarne 534
    } else {
535
      if ($invdate > $closedto) {
536
        print qq|<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
537
        <input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
538
        <input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
539
        <input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
540
        <input class=submit type=submit name=action value="|.$locale->text('Post').qq|">|;
541
      }
2 finnarne 542
    }
543
  }
544
 
545
  print qq|
546
 
547
<input type=hidden name=rowcount value=$form->{rowcount}>
548
 
549
<input name=callback type=hidden value="$form->{callback}">
550
 
551
<input type=hidden name=path value=$form->{path}>
552
<input type=hidden name=login value=$form->{login}>
553
<input type=hidden name=password value=$form->{password}>
554
 
555
</form>
556
 
557
</body>
558
</html>
559
|;
560
 
561
}
562
 
563
 
564
sub update {
565
 
566
  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
567
 
568
  &check_name(customer);
114 finnarne 569
 
570
  if ($form->{invdate} ne $form->{oldinvdate}) {
571
    $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
572
    $form->{oldinvdate} = $form->{invdate};
573
  }
2 finnarne 574
 
575
 
576
  $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, 'buy')));
577
 
578
  for $i (1 .. $form->{paidaccounts}) {
579
    if ($form->{"paid_$i"}) {
580
      map { $form->{"${_}_$i"} = $form->parse_amount(\%myconfig, $form->{"${_}_$i"}) } qw(paid exchangerate);
581
 
582
      $form->{"exchangerate_$i"} = $exchangerate if ($form->{"forex_$i"} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{"datepaid_$i"}, 'buy')));
583
    }
584
  }
585
 
586
  $i = $form->{rowcount};
587
  $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
588
 
589
  # if last row empty, check the form otherwise retrieve new item
114 finnarne 590
  if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
2 finnarne 591
 
592
    $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
593
    &check_form;
594
 
595
  } else {
596
 
597
    IS->retrieve_item(\%myconfig, \%$form);
114 finnarne 598
 
2 finnarne 599
    $rows = scalar @{ $form->{item_list} };
600
 
601
    $form->{"discount_$i"}      = $form->format_amount(\%myconfig, $form->{discount} * 100);
602
 
114 finnarne 603
    if ($rows > 0) {
2 finnarne 604
      $form->{"qty_$i"}         = ($form->{"qty_$i"} * 1) ? $form->{"qty_$i"} : 1;
605
 
606
      if ($rows > 1) {
607
 
608
        &select_item;
609
        exit;
610
 
611
      } else {
612
 
613
        $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
614
 
615
        map { $form->{item_list}[$i]{$_} =~ s/"/&quot;/g } qw(partnumber description unit);
616
        map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
617
 
618
        $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
619
        ($dec) = ($s =~ /\.(\d+)/);
620
        $dec = length $dec;
621
        $decimalplaces = ($dec > 2) ? $dec : 2;
622
 
623
        if ($sellprice) {
624
          $form->{"sellprice_$i"} = $sellprice;
625
        } else {
626
          # if there is an exchange rate adjust sellprice
627
          $form->{"sellprice_$i"} /= $exchangerate;
628
        }
629
 
630
        $form->{"listprice_$i"} /= $exchangerate;
631
 
632
        $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
633
        map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
634
        map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
635
        map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{"taxaccounts_$i"} if !$form->{taxincluded};
636
 
637
        $form->{creditremaining} -= $amount;
638
 
639
 
640
        map { $form->{"${_}_$i"} = $form->format_amount(\%myconfig, $form->{"${_}_$i"}, $decimalplaces) } qw(sellprice listprice);
641
 
642
        $form->{"qty_$i"} =  $form->format_amount(\%myconfig, $form->{"qty_$i"});
643
 
644
      }
645
 
646
      &display_form;
647
 
648
    } else {
649
      # ok, so this is a new part
650
      # ask if it is a part or service item
651
 
114 finnarne 652
      if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
653
        $form->{rowcount}--;
654
        $form->{"discount_$i"} = "";
655
        &display_form;
656
      } else {
2 finnarne 657
 
114 finnarne 658
        $form->{"id_$i"}          = 0;
659
        $form->{"unit_$i"}        = $locale->text('ea');
660
 
661
        &new_item;
662
 
663
      }
2 finnarne 664
    }
665
  }
666
}
667
 
668
 
669
 
670
sub post {
671
 
672
  $form->isblank("invnumber", $locale->text('Invoice Number missing!'));
673
  $form->isblank("invdate", $locale->text('Invoice Date missing!'));
674
  $form->isblank("customer", $locale->text('Customer missing!'));
675
 
676
  # if oldcustomer ne customer redo form
677
  if (&check_name(customer)) {
678
    &update;
679
    exit;
680
  }
681
 
682
  &validate_items;
683
 
684
  $closedto = $form->datetonum($form->{closedto}, \%myconfig);
685
  $invdate = $form->datetonum($form->{invdate}, \%myconfig);
686
 
687
  $form->error($locale->text('Cannot post invoice for a closed period!')) if ($invdate <= $closedto);
688
 
114 finnarne 689
  $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
2 finnarne 690
 
691
  for $i (1 .. $form->{paidaccounts}) {
692
    if ($form->{"paid_$i"}) {
693
      $datepaid = $form->datetonum($form->{"datepaid_$i"}, \%myconfig);
694
 
695
      $form->isblank("datepaid_$i", $locale->text('Payment date missing!'));
696
 
697
      $form->error($locale->text('Cannot post payment for a closed period!')) if ($datepaid <= $closedto);
698
 
699
      if ($form->{currency} ne $form->{defaultcurrency}) {
700
        $form->{"exchangerate_$i"} = $form->{exchangerate} if ($invdate == $datepaid);
114 finnarne 701
        $form->isblank("exchangerate_$i", $locale->text('Exchange rate for payment missing!'));
2 finnarne 702
      }
703
    }
704
  }
705
 
706
 
707
  ($form->{AR}) = split /--/, $form->{AR};
708
 
709
  $form->{label} = $locale->text('Invoice');
710
 
711
  $form->{id} = 0 if $form->{postasnew};
712
 
713
  $form->redirect($locale->text('Invoice posted!')) if (IS->post_invoice(\%myconfig, \%$form));
714
  $form->error($locale->text('Cannot post invoice!'));
715
 
716
}
717
 
718
 
719
 
720
 
721
sub delete {
722
 
723
  $form->header;
724
 
725
  print qq|
726
<body>
727
 
728
<form method=post action=$form->{script}>
729
|;
730
 
731
  # delete action variable
732
  delete $form->{action};
733
 
734
  foreach $key (keys %$form) {
735
    $form->{$key} =~ s/"/&quot;/g;
736
    print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
737
  }
738
 
739
  print qq|
740
<h2 class=confirm>|.$locale->text('Confirm!').qq|</font></h2>
741
 
742
<h4>|.$locale->text('Are you sure you want to delete Invoice Number').qq| $form->{invnumber}
743
</h4>
744
 
745
<p>
746
<input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
747
</form>
748
|;
749
 
750
 
751
}
752
 
753
 
754
 
755
sub yes {
756
 
757
  $form->redirect($locale->text('Invoice deleted!')) if (IS->delete_invoice(\%myconfig, \%$form));
758
  $form->error($locale->text('Cannot delete invoice!'));
759
 
760
}
761
 
762