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
# Order entry module
25
#
26
#======================================================================
27
 
28
 
29
use SL::OE;
30
use SL::IR;
31
use SL::IS;
32
use SL::PE;
33
 
34
require "$form->{path}/io.pl";
35
require "$form->{path}/arap.pl";
36
 
37
1;
38
# end of main
39
 
40
 
41
sub add {
42
 
43
  if ($form->{type} eq 'purchase_order') {
44
    $form->{title} = $locale->text('Add Purchase Order');
45
    $form->{vc} = 'vendor';
46
  }
47
  if ($form->{type} eq 'sales_order') {
48
    $form->{title} = $locale->text('Add Sales Order');
49
    $form->{vc} = 'customer';
50
  }
51
 
52
  &order_links;
53
  &prepare_order;
54
  &display_form;
55
 
56
}
57
 
58
 
59
sub edit {
60
 
61
  if ($form->{type} eq 'purchase_order') {
62
    $form->{title} = $locale->text('Edit Purchase Order');
63
    $form->{vc} = 'vendor';
64
  }
65
  if ($form->{type} eq 'sales_order') {
66
    $form->{title} = $locale->text('Edit Sales Order');
67
    $form->{vc} = 'customer';
68
  }
69
 
70
  &order_links;
71
  &prepare_order;
72
 
73
  &display_form;
74
 
75
}
76
 
77
 
78
sub order_links {
79
 
80
  # get vendors / customers
81
  $form->all_vc(\%myconfig, $form->{vc});
82
 
114 finnarne 83
  $form->get_partsgroup(\%myconfig);
84
  if (@{ $form->{all_partsgroup} }) {
85
    $form->{selectpartsgroup} = "<option>\n";
86
    map { $form->{selectpartsgroup} .= "<option>$_->{partsgroup}\n" } @{ $form->{all_partsgroup} };
87
  }
88
 
89
 
2 finnarne 90
  # retrieve order
91
  OE->retrieve_order(\%myconfig, \%$form);
114 finnarne 92
 
93
  if (@{ $form->{all_project} }) {
94
    $form->{selectprojectnumber} = "<option>\n";
95
    map { $form->{selectprojectnumber} .= "<option>$_->{projectnumber}\n" } @{ $form->{all_project} };
96
  }
97
 
98
 
2 finnarne 99
  $taxincluded = $form->{taxincluded};
100
  $form->{shipto} = 1 if $form->{id};
101
 
114 finnarne 102
  $notes = $form->{notes};
103
 
2 finnarne 104
  # get customer / vendor
105
  if ($form->{type} eq 'purchase_order') {
106
    IR->get_vendor(\%myconfig, \%$form);
107
  }
108
  if ($form->{type} eq 'sales_order') {
109
    IS->get_customer(\%myconfig, \%$form);
110
  }
111
 
114 finnarne 112
  $form->{notes} = $notes if $notes;
2 finnarne 113
  ($form->{$form->{vc}}) = split /--/, $form->{$form->{vc}};
114
  $form->{"old$form->{vc}"} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
115
 
116
  # build the popup menus
117
  if (@{ $form->{"all_$form->{vc}"} }) {
118
    $form->{$form->{vc}} = qq|$form->{$form->{vc}}--$form->{"$form->{vc}_id"}|;
119
    map { $form->{"select$form->{vc}"} .= "<option>$_->{name}--$_->{id}\n" } (@{ $form->{"all_$form->{vc}"} });
120
  }
121
 
122
  # currencies
123
  @curr = split /:/, $form->{currencies};
124
  chomp $curr[0];
125
  $form->{defaultcurrency} = $curr[0];
126
 
127
  map { $form->{selectcurrency} .= "<option>$_\n" } @curr;
128
 
129
  $form->{taxincluded} = $taxincluded if ($form->{id});
130
 
131
  # forex
132
  $form->{forex} = $form->{exchangerate};
133
 
134
}
135
 
136
 
137
sub prepare_order {
138
 
139
  $form->{format} = "html";
140
  $form->{media} = "screen";
141
 
142
  if ($form->{id}) {
143
 
144
    map { $form->{$_} =~ s/"/&quot;/g } qw(invnumber shippingpoint notes shiptoname shiptoaddr1 shiptoaddr2 shiptoaddr3 shiptoaddr4 shiptocontact);
145
 
146
    foreach $ref (@{ $form->{order_details} } ) {
147
      $i++;
148
      map { $form->{"${_}_$i"} = $ref->{$_} } keys %{ $ref };
149
      $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"} * 100);
150
 
151
      ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
152
      $dec = length $dec;
153
      $decimalplaces = ($dec > 2) ? $dec : 2;
154
 
155
      $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
156
      $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
157
 
158
      map { $form->{"${_}_$i"} =~ s/"/&quot;/g } qw(partnumber description unit);
159
      $form->{rowcount} = $i;
160
    }
161
  }
162
 
114 finnarne 163
  if ($form->{type} eq 'sales_order') {
164
    $form->{readonly} = 1 if $myconfig{acs} =~ /Order Entry--Sales Order/;
165
  }
166
  if ($form->{type} eq 'purchase_order') {
167
    $form->{readonly} = 1 if $myconfig{acs} =~ /Order Entry--Purchase Order/;
168
  }
169
 
2 finnarne 170
}
171
 
172
 
173
sub form_header {
174
 
175
  $checkedopen = ($form->{closed}) ? "" : "checked";
176
  $checkedclosed = ($form->{closed}) ? "checked" : "";
177
 
178
  if ($form->{id}) {
179
    $openclosed = qq|
180
      <tr>
114 finnarne 181
        <td colspan=2 align=center>
2 finnarne 182
          <table>
183
            <tr>
184
              <th nowrap><input name=closed type=radio class=radio value=0 $checkedopen> |.$locale->text('Open').qq|</th>
185
              <th nowrap><input name=closed type=radio class=radio value=1 $checkedclosed> |.$locale->text('Closed').qq|</th>
186
            </tr>
187
          </table>
188
        </td>
189
      </tr>
190
|;
191
  }
192
 
193
  # set option selected
194
  foreach $item ($form->{vc}, currency) {
195
    $form->{"select$item"} =~ s/ selected//;
196
    $form->{"select$item"} =~ s/option>\Q$form->{$item}\E/option selected>$form->{$item}/;
197
  }
198
 
199
  $form->{exchangerate} = $form->format_amount(\%myconfig, $form->{exchangerate});
200
 
201
  $form->{creditlimit} = $form->format_amount(\%myconfig, $form->{creditlimit}, 0, "0");
202
  $form->{creditremaining} = $form->format_amount(\%myconfig, $form->{creditremaining}, 0, "0");
203
 
204
  $exchangerate = qq|
205
<input type=hidden name=forex value=$form->{forex}>
206
|;
207
 
208
  if ($form->{currency} ne $form->{defaultcurrency}) {
209
    if ($form->{forex}) {
114 finnarne 210
      $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td>$form->{exchangerate}</td>
2 finnarne 211
      <input type=hidden name=exchangerate value=$form->{exchangerate}>
212
|;
213
    } else {
114 finnarne 214
      $exchangerate .= qq|<th align=right>|.$locale->text('Exchange Rate').qq|</th><td><input name=exchangerate size=10 value=$form->{exchangerate}></td>|;
2 finnarne 215
    }
216
  }
217
 
218
 
219
  $vclabel = ucfirst $form->{vc};
220
  $vclabel = $locale->text($vclabel);
221
 
222
  if ($form->{type} eq 'sales_order') {
223
 
224
    $n = ($form->{creditremaining} =~ /-/) ? "0" : "1";
225
 
226
    $creditremaining = qq|
227
              <tr>
228
                <td></td>
114 finnarne 229
                <td colspan=3>
2 finnarne 230
                  <table width=100%>
231
                    <tr>
114 finnarne 232
                      <th align=left nowrap>|.$locale->text('Credit Limit').qq|</th>
2 finnarne 233
                      <td>$form->{creditlimit}</td>
114 finnarne 234
                      <th align=left nowrap>|.$locale->text('Remaining').qq|</th>
2 finnarne 235
                      <td class="plus$n">$form->{creditremaining}</td>
236
                    </tr>
237
                  </table>
238
                </td>
239
              </tr>
240
|;
241
  }
242
 
243
  $vc = ($form->{"select$form->{vc}"}) ? qq|<select name=$form->{vc}>$form->{"select$form->{vc}"}</select>\n<input type=hidden name="select$form->{vc}" value="$form->{"select$form->{vc}"}">| : qq|<input name=$form->{vc} value="$form->{$form->{vc}}" size=35>|;
244
 
245
 
246
  $form->header;
247
 
248
  print qq|
249
<body>
250
 
251
<form method=post action=$form->{script}>
252
 
253
<input type=hidden name=id value=$form->{id}>
254
 
255
<input type=hidden name=type value=$form->{type}>
256
<input type=hidden name=media value=$form->{media}>
257
<input type=hidden name=format value=$form->{format}>
258
 
259
<input type=hidden name=vc value=$form->{vc}>
260
<input type=hidden name=employee value="$form->{employee}">
261
 
262
<input type=hidden name=title value="$form->{title}">
263
 
264
<input type=hidden name=discount value=$form->{discount}>
265
<input type=hidden name=creditlimit value=$form->{creditlimit}>
266
<input type=hidden name=creditremaining value=$form->{creditremaining}>
267
 
114 finnarne 268
<input type=hidden name=selectpartsgroup value="$form->{selectpartsgroup}">
269
<input type=hidden name=selectprojectnumber value="$form->{selectprojectnumber}">
270
 
2 finnarne 271
<table width=100%>
19 finnarne 272
  <tr>
273
    <th class=listtop>$form->{title}</th>
2 finnarne 274
  </tr>
275
  <tr height="5"></tr>
276
  <tr>
277
    <td>
278
      <table width="100%">
279
        <tr valign=top>
280
          <td>
114 finnarne 281
            <table>
2 finnarne 282
              <tr>
283
                <th align=right>$vclabel</th>
114 finnarne 284
                <td colspan=3>$vc</td>
2 finnarne 285
                <input type=hidden name=$form->{vc}_id value=$form->{"$form->{vc}_id"}>
286
                <input type=hidden name="old$form->{vc}" value="$form->{"old$form->{vc}"}">
287
              </tr>
288
              $creditremaining
289
              <tr>
290
                <th align=right>|.$locale->text('Currency').qq|</th>
291
                <td><select name=currency>$form->{selectcurrency}</select></td>
292
                <input type=hidden name=selectcurrency value="$form->{selectcurrency}">
293
                <input type=hidden name=defaultcurrency value=$form->{defaultcurrency}>
114 finnarne 294
                $exchangerate
2 finnarne 295
              </tr>
296
              <tr>
297
                <th align=right>|.$locale->text('Ship via').qq|</th>
114 finnarne 298
                <td colspan=3><input name=shippingpoint size=35 value="$form->{shippingpoint}"></td>
2 finnarne 299
              </tr>
300
            </table>
301
          </td>
302
          <td align=right>
303
            <table width=100%>
304
              $openclosed
305
              <tr>
306
                <th align=right nowrap>|.$locale->text('Order Number').qq|</th>
307
                <td><input name=ordnumber size=11 value="$form->{ordnumber}"></td>
308
              </tr>
309
              <tr>
310
                <th align=right nowrap>|.$locale->text('Order Date').qq|</th>
311
                <td><input name=orddate size=11 title="$myconfig{dateformat}" value=$form->{orddate}></td>
312
              </tr>
313
              <tr>
314
                <th align=right nowrap=true>|.$locale->text('Required by').qq|</th>
315
                <td><input name=reqdate size=11 title="$myconfig{dateformat}" value=$form->{reqdate}></td>
316
              </tr>
317
              <tr>
114 finnarne 318
                <th align=right nowrap>|.$locale->text('Terms: Net').qq|</th>
319
                <td nowrap=true><input name=terms size="3" maxlength="3" value=$form->{terms}> |.$locale->text('days').qq|</td>
2 finnarne 320
              </tr>
321
            </table>
322
          </td>
323
        </tr>
324
      </table>
325
    </td>
326
  </tr>
327
 
328
<!-- shipto are in hidden variables -->
329
 
330
<input type=hidden name=shiptoname value="$form->{shiptoname}">
331
<input type=hidden name=shiptoaddr1 value="$form->{shiptoaddr1}">
332
<input type=hidden name=shiptoaddr2 value="$form->{shiptoaddr2}">
333
<input type=hidden name=shiptoaddr3 value="$form->{shiptoaddr3}">
334
<input type=hidden name=shiptoaddr4 value="$form->{shiptoaddr4}">
335
<input type=hidden name=shiptocontact value="$form->{shiptocontact}">
336
<input type=hidden name=shiptophone value="$form->{shiptophone}">
337
<input type=hidden name=shiptofax value="$form->{shiptofax}">
338
<input type=hidden name=shiptoemail value="$form->{shiptoemail}">
339
 
340
<!-- email variables -->
341
<input type=hidden name=message value="$form->{message}">
342
<input type=hidden name=email value="$form->{email}">
343
<input type=hidden name=subject value="$form->{subject}">
344
<input type=hidden name=cc value="$form->{cc}">
345
<input type=hidden name=bcc value="$form->{bcc}">
346
 
347
<input type=hidden name=taxpart value="$form->{taxpart}">
348
<input type=hidden name=taxservice value="$form->{taxservice}">
349
 
350
<input type=hidden name=taxaccounts value="$form->{taxaccounts}">
351
|;
352
 
353
  foreach $item (split / /, $form->{taxaccounts}) {
354
    print qq|
355
<input type=hidden name="${item}_rate" value=$form->{"${item}_rate"}>
356
<input type=hidden name="${item}_description" value="$form->{"${item}_description"}">
357
|;
358
  }
359
 
360
}
361
 
362
 
363
sub form_footer {
364
 
365
  $form->{invtotal} = $form->{invsubtotal};
366
 
367
  if (($rows = $form->numtextrows($form->{notes}, 50, 8)) < 2) {
368
    $rows = 2;
369
  }
370
  $notes = qq|<textarea name=notes rows=$rows cols=50 wrap=soft>$form->{notes}</textarea>|;
371
 
114 finnarne 372
  $taxincluded = "";
373
  $form->{taxincluded} = ($form->{taxincluded}) ? "checked" : "";
374
  if ($form->{taxaccounts}) {
375
    $taxincluded = qq|
376
                <input name=taxincluded class=checkbox type=checkbox value=1 $form->{taxincluded}> <b>|.$locale->text('Tax Included').qq|</b><br><br>
377
|;
378
  }
2 finnarne 379
 
380
  if (!$form->{taxincluded}) {
381
 
382
    foreach $item (split / /, $form->{taxaccounts}) {
383
      if ($form->{"${item}_base"}) {
384
        $form->{invtotal} += $form->{"${item}_total"} = $form->round_amount($form->{"${item}_base"} * $form->{"${item}_rate"}, 2);
385
        $form->{"${item}_total"} = $form->format_amount(\%myconfig, $form->{"${item}_total"}, 2);
386
 
387
        $tax .= qq|
388
              <tr>
389
                <th align=right>$form->{"${item}_description"}</th>
390
                <td align=right>$form->{"${item}_total"}</td>
391
              </tr>
392
|;
393
      }
394
    }
395
 
396
    $form->{invsubtotal} = $form->format_amount(\%myconfig, $form->{invsubtotal}, 2, 0);
397
 
398
    $subtotal = qq|
399
              <tr>
400
                <th align=right>|.$locale->text('Subtotal').qq|</th>
401
                <td align=right>$form->{invsubtotal}</td>
402
              </tr>
403
|;
404
 
405
  }
406
 
407
  $form->{oldinvtotal} = $form->{invtotal};
408
  $form->{invtotal} = $form->format_amount(\%myconfig, $form->{invtotal}, 2, 0);
409
 
410
  print qq|
411
  <tr>
412
    <td>
413
      <table width=100%>
414
        <tr valign=bottom>
415
          <td>
416
            <table>
417
              <tr>
418
                <th align=left>|.$locale->text('Notes').qq|</th>
419
              </tr>
420
              <tr>
421
                <td>$notes</td>
422
              </tr>
423
            </table>
424
          </td>
114 finnarne 425
          <td align=right>
426
            $taxincluded
427
            <table>
2 finnarne 428
              $subtotal
429
              $tax
430
              <tr>
431
                <th align=right>|.$locale->text('Total').qq|</th>
432
                <td align=right>$form->{invtotal}</td>
433
              </tr>
434
            </table>
435
          </td>
436
        </tr>
437
      </table>
438
    </td>
439
  </tr>
440
<input type=hidden name=oldinvtotal value=$form->{oldinvtotal}>
441
<input type=hidden name=oldtotalpaid value=$totalpaid>
442
  <tr>
443
    <td>
444
|;
445
 
446
  &print_options;
447
 
448
  print qq|
449
    </td>
450
  </tr>
451
  <tr>
452
    <td><hr size=3 noshade></td>
453
  </tr>
454
</table>
455
 
456
<br>
114 finnarne 457
|;
458
 
459
  if (! $form->{readonly}) {
460
    print qq|
2 finnarne 461
<input class=submit type=submit name=action value="|.$locale->text('Update').qq|">
462
<input class=submit type=submit name=action value="|.$locale->text('Ship to').qq|">
463
<input class=submit type=submit name=action value="|.$locale->text('Print').qq|">
464
<input class=submit type=submit name=action value="|.$locale->text('E-mail').qq|">
465
<input class=submit type=submit name=action value="|.$locale->text('Save').qq|">
466
|;
467
 
468
    if ($form->{id}) {
469
      print qq|
470
<input class=submit type=submit name=action value="|.$locale->text('Save as new').qq|">
471
<input class=submit type=submit name=action value="|.$locale->text('Delete').qq|">
472
<input class=submit type=submit name=action value="|.$locale->text('Invoice').qq|">
473
|;
474
    }
114 finnarne 475
  }
2 finnarne 476
 
477
  print qq|
478
 
479
<input type=hidden name=rowcount value=$form->{rowcount}>
480
 
481
<input name=callback type=hidden value="$form->{callback}">
482
 
483
<input type=hidden name=path value=$form->{path}>
484
<input type=hidden name=login value=$form->{login}>
485
<input type=hidden name=password value=$form->{password}>
486
 
487
</form>
488
 
489
</body>
490
</html>
491
|;
492
 
493
}
494
 
495
 
496
sub update {
497
 
498
  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(exchangerate creditlimit creditremaining);
499
 
500
  &check_name($form->{vc});
501
 
502
 
503
  $buysell = 'buy';
504
  $buysell = 'sell' if ($form->{vc} eq 'vendor');
505
  $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{orddate}, $buysell)));
506
 
507
 
508
  my $i = $form->{rowcount};
509
  $exchangerate = ($form->{exchangerate}) ? $form->{exchangerate} : 1;
510
 
114 finnarne 511
  if (($form->{"partnumber_$i"} eq "") && ($form->{"description_$i"} eq "") && ($form->{"partsgroup_$i"} eq "")) {
2 finnarne 512
 
513
    $form->{creditremaining} += ($form->{oldinvtotal} - $form->{oldtotalpaid});
514
    &check_form;
114 finnarne 515
 
2 finnarne 516
  } else {
517
 
518
    if ($form->{type} eq 'purchase_order') {
519
      IR->retrieve_item(\%myconfig, \%$form);
520
    }
521
    if ($form->{type} eq 'sales_order') {
522
      IS->retrieve_item(\%myconfig, \%$form);
523
    }
524
 
525
    my $rows = scalar @{ $form->{item_list} };
526
 
527
    $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{discount} * 100);
528
 
529
    if ($rows) {
530
      $form->{"qty_$i"}                     = 1 unless ($form->{"qty_$i"});
531
 
532
      if ($rows > 1) {
533
 
534
        &select_item;
535
        exit;
536
 
537
      } else {
538
 
539
        $sellprice = $form->parse_amount(\%myconfig, $form->{"sellprice_$i"});
540
 
541
        map { $form->{item_list}[$i]{$_} =~ s/"/&quot;/g } qw(partnumber description unit);
542
        map { $form->{"${_}_$i"} = $form->{item_list}[0]{$_} } keys %{ $form->{item_list}[0] };
543
 
544
        $s = ($sellprice) ? $sellprice : $form->{"sellprice_$i"};
545
 
546
        ($dec) = ($s =~ /\.(\d+)/);
547
        $dec = length $dec;
548
        $decimalplaces = ($dec > 2) ? $dec : 2;
549
 
550
        if ($sellprice) {
551
          $form->{"sellprice_$i"} = $sellprice;
552
        } else {
553
          # if there is an exchange rate adjust sellprice
554
          $form->{"sellprice_$i"} /= $exchangerate;
555
        }
556
 
557
        $amount = $form->{"sellprice_$i"} * $form->{"qty_$i"} * (1 - $form->{"discount_$i"} / 100);
558
        map { $form->{"${_}_base"} = 0 } (split / /, $form->{taxaccounts});
559
        map { $form->{"${_}_base"} += $amount } (split / /, $form->{"taxaccounts_$i"});
560
        map { $amount += ($form->{"${_}_base"} * $form->{"${_}_rate"}) } split / /, $form->{taxaccounts} if !$form->{taxincluded};
561
 
562
        $form->{creditremaining} -= $amount;
563
 
564
        $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
565
        $form->{"qty_$i"} =  $form->format_amount(\%myconfig, $form->{"qty_$i"});
566
      }
567
 
568
      &display_form;
569
 
570
    } else {
571
      # ok, so this is a new part
572
      # ask if it is a part or service item
114 finnarne 573
 
574
      if ($form->{"partsgroup_$i"} && ($form->{"partsnumber_$i"} eq "") && ($form->{"description_$i"} eq "")) {
575
        $form->{rowcount}--;
576
        $form->{"discount_$i"} = "";
577
        &display_form;
578
      } else {
579
 
580
        $form->{"id_$i"}                = 0;
581
        $form->{"unit_$i"}      = $locale->text('ea');
2 finnarne 582
 
114 finnarne 583
        &new_item;
2 finnarne 584
 
114 finnarne 585
      }
2 finnarne 586
    }
587
  }
588
}
589
 
590
 
591
 
592
sub search {
593
 
594
  if ($form->{type} eq 'purchase_order') {
595
    $form->{title} = $locale->text('Purchase Orders');
596
    $form->{vc} = 'vendor';
597
  }
598
  if ($form->{type} eq 'sales_order') {
599
    $form->{title} = $locale->text('Sales Orders');
600
    $form->{vc} = 'customer';
601
  }
602
 
603
  # setup vendor / customer selection
604
  $form->all_vc(\%myconfig, "$form->{vc}");
605
 
606
  map { $vc .= "<option>$_->{name}--$_->{id}\n" } @{ $form->{"all_$form->{vc}"} };
607
 
608
  $vclabel = ucfirst $form->{vc};
609
  $vclabel = $locale->text($vclabel);
610
 
611
# $locale->text('Vendor')
612
# $locale->text('Customer')
613
 
614
  $vc = ($vc) ? qq|<select name=$form->{vc}><option>\n$vc</select>| : qq|<input name=$form->{vc} size=35>|;
615
 
616
  $form->header;
617
 
618
  print qq|
619
<body>
620
 
621
<form method=post action=$form->{script}>
622
 
623
<table width=100%>
624
  <tr>
625
    <th class=listtop>$form->{title}</th>
626
  </tr>
627
  <tr height="5"></tr>
628
  <tr>
629
    <td>
630
      <table>
631
        <tr>
632
          <th align=right>$vclabel</th>
633
          <td colspan=3>$vc</td>
634
        </tr>
635
        <tr>
636
          <th align=right>|.$locale->text('Order Number').qq|</th>
637
          <td colspan=3><input name=ordnumber size=20></td>
638
        </tr>
639
        <tr>
640
          <th align=right>|.$locale->text('From').qq|</th>
641
          <td><input name=transdatefrom size=11 title="$myconfig{dateformat}"></td>
19 finnarne 642
          <th align=right>|.$locale->text('to').qq|</th>
2 finnarne 643
          <td><input name=transdateto size=11 title="$myconfig{dateformat}"></td>
644
        </tr>
645
        <input type=hidden name=sort value=transdate>
646
        <tr>
647
          <th align=right>|.$locale->text('Include in Report').qq|</th>
648
          <td colspan=3>
649
            <table>
650
              <tr>
19 finnarne 651
                <td><input name="open" class=checkbox type=checkbox value=1 checked> |.$locale->text('Open').qq|</td>
652
                <td><input name="closed" class=checkbox type=checkbox value=1 $form->{closed}> |.$locale->text('Closed').qq|</td>
2 finnarne 653
              </tr>
654
              <tr>
19 finnarne 655
                <td><input name="l_id" class=checkbox type=checkbox value=Y> |.$locale->text('ID').qq|</td>
656
                <td><input name="l_ordnumber" class=checkbox type=checkbox value=Y checked> |.$locale->text('Order Number').qq|</td>
657
                <td><input name="l_transdate" class=checkbox type=checkbox value=Y checked> |.$locale->text('Order Date').qq|</td>
658
                <td><input name="l_reqdate" class=checkbox type=checkbox value=Y checked> |.$locale->text('Required by').qq|</td>
2 finnarne 659
              </tr>
660
              <tr>
19 finnarne 661
                <td><input name="l_name" class=checkbox type=checkbox value=Y checked> $vclabel</td>
662
                <td><input name="l_netamount" class=checkbox type=checkbox value=Y> |.$locale->text('Amount').qq|</td>
663
                <td><input name="l_tax" class=checkbox type=checkbox value=Y> |.$locale->text('Tax').qq|</td>
664
                <td><input name="l_amount" class=checkbox type=checkbox value=Y checked> |.$locale->text('Total').qq|</td>
2 finnarne 665
              </tr>
666
              <tr>
19 finnarne 667
                <td><input name="l_subtotal" class=checkbox type=checkbox value=Y> |.$locale->text('Subtotal').qq|</td>
2 finnarne 668
              </tr>
669
            </table>
670
          </td>
671
        </tr>
672
      </table>
673
    </td>
674
  </tr>
675
  <tr><td colspan=4><hr size=3 noshade></td></tr>
676
</table>
677
 
678
<br>
679
<input type=hidden name=nextsub value=orders>
680
<input type=hidden name=path value=$form->{path}>
681
<input type=hidden name=login value=$form->{login}>
682
<input type=hidden name=password value=$form->{password}>
683
<input type=hidden name=vc value=$form->{vc}>
684
<input type=hidden name=type value=$form->{type}>
685
 
686
<input class=submit type=submit name=action value="|.$locale->text('Continue').qq|">
687
</form>
688
 
689
</body>
690
</html>
691
|;
692
 
693
}
694
 
695
 
696
sub orders {
697
 
698
  $name = $form->{$form->{vc}};
699
 
700
  # split vendor / customer
701
  ($form->{$form->{vc}}, $form->{"$form->{vc}_id"}) = split(/--/, $form->{$form->{vc}});
702
 
703
  OE->transactions(\%myconfig, \%$form);
704
 
705
  # construct href
706
  $href = "$form->{script}?path=$form->{path}&action=orders&type=$form->{type}&vc=$form->{vc}&login=$form->{login}&password=$form->{password}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&open=$form->{open}&closed=$form->{closed}&ordnumber=" . $form->escape($form->{ordnumber}) . "&$form->{vc}=" . $form->escape($form->{$form->{vc}});
707
 
708
  # construct callback
709
  $name =~ s/&/_/g;
710
  $callback = "$form->{script}?path=$form->{path}&action=orders&type=$form->{type}&vc=$form->{vc}&login=$form->{login}&password=$form->{password}&transdatefrom=$form->{transdatefrom}&transdateto=$form->{transdateto}&open=$form->{open}&closed=$form->{closed}&ordnumber=$form->{ordnumber}&$form->{vc}=$name";
711
 
712
  @columns = $form->sort_columns(qw(transdate reqdate id ordnumber name netamount tax amount curr open closed));
713
 
714
  $form->{l_open} = $form->{l_closed} = "Y" if ($form->{open} && $form->{closed}) ;
715
 
716
  foreach $item (@columns) {
717
    if ($form->{"l_$item"} eq "Y") {
718
      push @column_index, $item;
719
 
720
      # add column to href and callback
721
      $callback .= "&l_$item=Y";
722
      $href .= "&l_$item=Y";
723
    }
724
  }
725
 
726
  if ($form->{l_subtotal} eq 'Y') {
727
    $callback .= "&l_subtotal=Y";
728
    $href .= "&l_subtotal=Y";
729
  }
730
 
114 finnarne 731
 
732
  $i = 1;
2 finnarne 733
  if ($form->{vc} eq 'vendor') {
734
    $form->{title} = $locale->text('Purchase Orders');
735
    $name = $locale->text('Vendor');
114 finnarne 736
 
737
    if ($myconfig{acs} !~ /Order Entry--Order Entry/) {
738
      $button{'Order Entry--Purchase Order'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Purchase Order').qq|"> |;
739
      $button{'Order Entry--Purchase Order'}{order} = $i++;
740
    }
741
 
2 finnarne 742
  }
743
  if ($form->{vc} eq 'customer') {
744
    $form->{title} = $locale->text('Sales Orders');
745
    $name = $locale->text('Customer');
114 finnarne 746
 
747
    if ($myconfig{acs} !~ /Order Entry--Order Entry/) {
748
      $button{'Order Entry--Sales Order'}{code} = qq|<input class=submit type=submit name=action value="|.$locale->text('Sales Order').qq|"> |;
749
      $button{'Order Entry--Sales Order'}{order} = $i++;
750
    }
751
 
2 finnarne 752
  }
753
 
114 finnarne 754
  foreach $item (split /;/, $myconfig{acs}) {
755
    delete $button{$item};
756
  }
757
 
2 finnarne 758
  $column_header{id} = qq|<th><a class=listheading href=$href&sort=id>|.$locale->text('ID').qq|</a></th>|;
759
  $column_header{transdate} = qq|<th><a class=listheading href=$href&sort=transdate>|.$locale->text('Date').qq|</a></th>|;
760
  $column_header{reqdate} = qq|<th><a class=listheading href=$href&sort=reqdate>|.$locale->text('Required by').qq|</a></th>|;
761
  $column_header{ordnumber} = qq|<th><a class=listheading href=$href&sort=ordnumber>|.$locale->text('Order').qq|</a></th>|;
762
  $column_header{name} = qq|<th><a class=listheading href=$href&sort=name>$name</a></th>|;
19 finnarne 763
  $column_header{netamount} = qq|<th class=listheading>|.$locale->text('Amount').qq|</th>|;
764
  $column_header{tax} = qq|<th class=listheading>|.$locale->text('Tax').qq|</th>|;
765
  $column_header{amount} = qq|<th class=listheading>|.$locale->text('Total').qq|</th>|;
766
  $column_header{curr} = qq|<th class=listheading>|.$locale->text('Curr').qq|</th>|;
767
  $column_header{open} = qq|<th class=listheading>|.$locale->text('O').qq|</th>|;
768
  $column_header{closed} = qq|<th class=listheading>|.$locale->text('C').qq|</th>|;
2 finnarne 769
 
770
  if ($form->{open}) {
771
    $option = $locale->text('Open');
772
  }
773
  if ($form->{closed}) {
774
    $option .= " : " if $form->{open};
775
    $option .= $locale->text('Closed');
776
  }
777
  if ($form->{$form->{vc}}) {
778
    $option .= "\n<br>";
779
    $option .= ucfirst $form->{vc};
780
    $option .= " : $form->{$form->{vc}}";
781
  }
782
  if ($form->{transdatefrom}) {
783
    $option .= "\n<br>".$locale->text('From')." ".$locale->date(\%myconfig, $form->{transdatefrom}, 1);
784
  }
785
  if ($form->{transdateto}) {
19 finnarne 786
    $option .= "\n<br>".$locale->text('to')." ".$locale->date(\%myconfig, $form->{transdateto}, 1);
2 finnarne 787
  }
788
 
789
  $form->header;
790
 
791
  $colspan = $#column_index + 1;
792
 
793
  print qq|
794
<body>
795
 
796
<table width=100%>
797
  <tr><th class=listtop colspan=$colspan>$form->{title}</th></tr>
798
  <tr height="5"></tr>
799
  <tr>
800
    <td colspan=$colspan>$option</td>
801
  </tr>
802
  <tr class=listheading>|;
803
 
804
map { print "\n$column_header{$_}" } @column_index;
805
 
806
print qq|
807
  </tr>
808
|;
809
 
810
  # add sort and escape callback
811
  $callback = $form->escape($callback . "&sort=$form->{sort}");
812
 
813
  if (@{ $form->{OE} }) {
814
    $sameitem = $form->{OE}->[0]->{$form->{sort}};
815
  }
816
 
817
  foreach $oe (@{ $form->{OE} }) {
818
 
819
    if ($form->{l_subtotal} eq 'Y') {
820
      if ($sameitem ne $oe->{$form->{sort}}) {
821
        &subtotal;
822
        $sameitem = $oe->{$form->{sort}};
823
      }
824
    }
825
 
826
    map { $oe->{$_} *= $oe->{exchangerate} } (qw(netamount amount));
827
 
828
    $column_data{netamount} = "<td align=right>".$form->format_amount(\%myconfig, $oe->{netamount}, 2, "&nbsp;")."</td>";
829
    $column_data{tax} = "<td align=right>".$form->format_amount(\%myconfig, $oe->{amount} - $oe->{netamount}, 2, "&nbsp;")."</td>";
830
    $column_data{amount} = "<td align=right>".$form->format_amount(\%myconfig, $oe->{amount}, 2, "&nbsp;")."</td>";
831
 
832
    $totalnetamount += $oe->{netamount};
833
    $totalamount += $oe->{amount};
834
 
835
    $subtotalnetamount += $oe->{netamount};
836
    $subtotalamount += $oe->{amount};
837
 
838
    $column_data{id} = "<td>$oe->{id}</td>";
839
    $column_data{transdate} = "<td>$oe->{transdate}&nbsp;</td>";
840
    $column_data{reqdate} = "<td>$oe->{reqdate}&nbsp;</td>";
841
 
842
    $column_data{ordnumber} = "<td><a href=oe.pl?path=$form->{path}&action=edit&type=$form->{type}&id=$oe->{id}&login=$form->{login}&password=$form->{password}&callback=$callback>$oe->{ordnumber}</a></td>";
843
    $column_data{name} = "<td>$oe->{name}</td>";
844
 
845
    if ($oe->{closed}) {
846
      $column_data{closed} = "<td align=center>X</td>";
847
      $column_data{open} = "<td>&nbsp;</td>";
848
    } else {
849
      $column_data{closed} = "<td>&nbsp;</td>";
850
      $column_data{open} = "<td align=center>X</td>";
851
    }
852
 
853
    $i++; $i %= 2;
854
    print "<tr class=listrow$i>";
855
 
856
    map { print "\n$column_data{$_}" } @column_index;
857
 
858
    print qq|
859
    </tr>
860
|;
861
 
862
  }
863
 
864
  if ($form->{l_subtotal} eq 'Y') {
865
    &subtotal;
866
  }
867
 
868
  # print totals
869
  print qq|<tr class=listtotal>|;
870
 
871
  map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
872
 
19 finnarne 873
  $column_data{netamount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalnetamount, 2, "&nbsp;")."</th>";
874
  $column_data{tax} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount - $totalnetamount, 2, "&nbsp;")."</th>";
875
  $column_data{amount} = "<th class=listtotal align=right>".$form->format_amount(\%myconfig, $totalamount, 2, "&nbsp;")."</th>";
2 finnarne 876
 
877
  map { print "\n$column_data{$_}" } @column_index;
878
 
879
  print qq|
880
  </tr>
881
  <tr><td colspan=8><hr size=3 noshade></td></tr>
882
</table>
883
 
884
<br>
885
<form method=post action=$form->{script}>
886
 
887
<input name=callback type=hidden value="$form->{callback}">
888
 
889
<input type=hidden name=type value=$form->{type}>
890
<input type=hidden name=vc value=$form->{vc}>
891
 
892
<input type=hidden name=path value=$form->{path}>
893
<input type=hidden name=login value=$form->{login}>
894
<input type=hidden name=password value=$form->{password}>
114 finnarne 895
|;
2 finnarne 896
 
114 finnarne 897
  foreach $item (sort { $a->{order} <=> $b->{order} } %button) {
898
    print $item->{code};
899
  }
2 finnarne 900
 
114 finnarne 901
  print qq|
2 finnarne 902
</form>
903
 
904
</body>
905
</html>
906
|;
907
 
908
}
909
 
910
 
911
 
912
sub subtotal {
913
 
914
  map { $column_data{$_} = "<td>&nbsp;</td>" } @column_index;
915
 
19 finnarne 916
  $column_data{netamount} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalnetamount, 2, "&nbsp;")."</th>";
917
  $column_data{tax} = "<td class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount - $subtotalnetamount, 2, "&nbsp;")."</th>";
918
  $column_data{amount} = "<th class=listsubtotal align=right>".$form->format_amount(\%myconfig, $subtotalamount, 2, "&nbsp;")."</th>";
2 finnarne 919
 
920
  $subtotalnetamount = 0;
921
  $subtotalamount = 0;
922
 
923
  print "<tr class=listsubtotal>";
924
 
925
  map { print "\n$column_data{$_}" } @column_index;
926
 
927
  print qq|
928
  </tr>
929
|;
930
 
931
}
932
 
933
 
934
sub save {
935
 
936
  $form->isblank("ordnumber", $locale->text('Order Number missing!'));
937
  $form->isblank("orddate", $locale->text('Order Date missing!'));
938
 
939
  $msg = ucfirst $form->{vc};
940
  $form->isblank($form->{vc}, $locale->text($msg . " missing!"));
941
 
942
# $locale->text('Customer missing!');
943
# $locale->text('Vendor missing!');
944
 
114 finnarne 945
  $form->isblank("exchangerate", $locale->text('Exchange rate missing!')) if ($form->{currency} ne $form->{defaultcurrency});
2 finnarne 946
 
947
  &validate_items;
948
 
949
  # if the name changed get new values
950
  if (&check_name($form->{vc})) {
951
    &update;
952
    exit;
953
  }
954
 
955
 
956
  # this is for the notes section for the [email] Subject
957
  $form->{label} = ($form->{type} eq 'sales_order') ? $locale->text('Sales Order') : $locale->text('Purchase Order');
958
 
959
  $form->{id} = 0 if $form->{saveasnew};
960
 
961
  $form->redirect($locale->text('Order saved!')) if (OE->save_order(\%myconfig, \%$form));
962
  $form->error($locale->text('Cannot save order!'));
963
 
964
}
965
 
966
 
967
 
968
sub delete {
969
 
970
  $form->header;
971
 
972
  print qq|
973
<body>
974
 
975
<form method=post action=$form->{script}>
976
|;
977
 
978
  # delete action variable
979
  delete $form->{action};
980
 
981
  foreach $key (keys %$form) {
982
    $form->{$key} =~ s/"/&quot;/g;
983
    print qq|<input type=hidden name=$key value="$form->{$key}">\n|;
984
  }
985
 
986
  print qq|
987
<h2 class=confirm>|.$locale->text('Confirm!').qq|</h2>
988
 
989
<h4>|
990
  .$locale->text('Are you sure you want to delete Order Number').qq| $form->{ordnumber}
991
</h4>
992
<p>
993
<input name=action class=submit type=submit value="|.$locale->text('Yes').qq|">
994
</form>
995
 
996
</body>
997
</html>
998
|;
999
 
1000
 
1001
}
1002
 
1003
 
1004
 
1005
sub yes {
1006
 
1007
  $form->redirect($locale->text('Order deleted!')) if (OE->delete_order(\%myconfig, \%$form));
1008
  $form->error($locale->text('Cannot delete order!'));
1009
 
1010
}
1011
 
1012
 
1013
sub invoice {
1014
 
1015
  $form->isblank("ordnumber", $locale->text('Order Number missing!'));
1016
  $form->isblank("orddate", $locale->text('Order Date missing!'));
1017
 
114 finnarne 1018
 
2 finnarne 1019
  # if the name changed get new values
1020
  if (&check_name($form->{vc})) {
1021
    &update;
1022
    exit;
1023
  }
1024
 
1025
  $form->{closed} = 1;
1026
  OE->save_order(\%myconfig, \%$form);
1027
 
1028
  $form->{orddate} = $form->{transdate} = $form->{invdate} = $form->current_date(\%myconfig);
1029
  $form->{duedate} = $form->current_date(\%myconfig, $form->{invdate}, $form->{terms} * 1);
1030
 
1031
  $form->{id} = '';
1032
  $form->{closed} = 0;
1033
  $form->{rowcount}--;
1034
  $form->{shipto} = 1;
1035
 
1036
  &create_backorder;
1037
 
1038
  if ($form->{type} eq 'purchase_order') {
114 finnarne 1039
    $form->{title} = $locale->text('Add Vendor Invoice');
2 finnarne 1040
    $form->{script} = 'ir.pl';
1041
    $script = "ir";
1042
    $buysell = 'sell';
1043
  }
1044
  if ($form->{type} eq 'sales_order') {
1045
    $form->{title} = $locale->text('Add Sales Invoice');
1046
    $form->{script} = 'is.pl';
1047
    $script = "is";
1048
    $buysell = 'buy';
1049
  }
1050
 
1051
 
1052
  # bo creates the id, reset it
114 finnarne 1053
  map { $form->{$_} = "" } qw(id subject message cc bcc);
2 finnarne 1054
  $form->{$form->{vc}} =~ s/--.*//g;
114 finnarne 1055
  $form->{type} = "invoice";
2 finnarne 1056
 
1057
  # locale messages
1058
  $locale = new Locale "$myconfig{countrycode}", "$script";
1059
 
1060
  require "$form->{path}/$form->{script}";
1061
 
1062
  map { $form->{"select$_"} = "" } ($form->{vc}, currency);
1063
 
1064
  map { $form->{$_} = $form->parse_amount(\%myconfig, $form->{$_}) } qw(creditlimit creditremaining);
1065
 
1066
  $currency = $form->{currency};
114 finnarne 1067
  $notes = $form->{notes};
1068
 
2 finnarne 1069
  &invoice_links;
1070
 
1071
  $form->{currency} = $currency;
1072
  $form->{exchangerate} = "";
1073
  $form->{forex} = "";
1074
  $form->{exchangerate} = $exchangerate if ($form->{forex} = ($exchangerate = $form->check_exchangerate(\%myconfig, $form->{currency}, $form->{invdate}, $buysell)));
1075
 
1076
  $form->{creditremaining} -= ($form->{oldinvtotal} - $form->{ordtotal});
1077
 
1078
  &prepare_invoice;
1079
 
114 finnarne 1080
  $form->{notes} = $notes;
1081
 
2 finnarne 1082
  # format amounts
1083
  for $i (1 .. $form->{rowcount}) {
19 finnarne 1084
    $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
2 finnarne 1085
 
1086
    ($dec) = ($form->{"sellprice_$i"} =~ /\.(\d+)/);
1087
    $dec = length $dec;
1088
    $decimalplaces = ($dec > 2) ? $dec : 2;
1089
 
1090
    $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"}, $decimalplaces);
1091
    $form->{"qty_$i"} = $form->format_amount(\%myconfig, $form->{"qty_$i"});
1092
 
1093
    map { $form->{"${_}_$i"} =~ s/"/&quot;/g } qw(partnumber description unit);
114 finnarne 1094
 
1095
    $form->{"deliverydate_$i"} = $form->{"reqdate_$i"};
1096
 
2 finnarne 1097
  }
1098
 
1099
  &display_form;
1100
 
1101
}
1102
 
1103
 
1104
sub create_backorder {
1105
 
1106
  # figure out if we need to create a backorder
1107
  # items aren't saved if qty != 0
1108
 
1109
  for $i (1 .. $form->{rowcount}) {
1110
    $totalqty += $qty = $form->parse_amount($myconfig, $form->{"qty_$i"});
1111
    $totalship += $ship = $form->parse_amount($myconfig, $form->{"ship_$i"});
1112
 
1113
    $form->{"qty_$i"} = $qty - $ship;
1114
  }
1115
 
1116
  if ($totalship == 0) {
1117
    map { $form->{"ship_$_"} = $form->{"qty_$_"} } (1 .. $form->{rowcount});
1118
    $form->{ordtotal} = 0;
1119
    return;
1120
  }
1121
 
1122
  if ($totalqty == $totalship) {
1123
    map { $form->{"qty_$_"} = $form->{"ship_$_"} } (1 .. $form->{rowcount});
1124
    $form->{ordtotal} = 0;
1125
    return;
1126
  }
1127
 
114 finnarne 1128
  @flds = (qw(partnumber description qty ship unit sellprice discount id inventory_accno bin income_accno expense_accno listprice assembly taxaccounts partsgroup reqdate));
2 finnarne 1129
 
1130
  OE->save_order(\%myconfig, \%$form);
1131
 
1132
  # rebuild rows for invoice
1133
  @a = ();
1134
  $count = 0;
1135
 
1136
  for $i (1 .. $form->{rowcount}) {
1137
 
1138
    # reformat values if there was a backorder quantity
1139
    if ($form->{"qty_$i"}) {
1140
      $form->{"discount_$i"} = $form->format_amount(\%myconfig, $form->{"discount_$i"});
1141
      $form->{"sellprice_$i"} = $form->format_amount(\%myconfig, $form->{"sellprice_$i"});
1142
    }
1143
 
1144
    $form->{"qty_$i"} = $form->{"ship_$i"};
1145
 
1146
    if ($form->{"qty_$i"}) {
1147
      push @a, {};
1148
      $j = $#a;
1149
      map { $a[$j]->{$_} = $form->{"${_}_$i"} } @flds;
1150
      $count++;
1151
    }
1152
  }
1153
 
1154
  $form->redo_rows(\@flds, \@a, $count, $form->{rowcount});
1155
  $form->{rowcount} = $count;
1156
 
1157
}
1158
 
1159
 
1160
 
1161
sub save_as_new {
1162
 
1163
  $form->{saveasnew} = 1;
1164
  $form->{closed} = 0;
1165
  &save;
1166
 
1167
}
1168
 
1169
 
114 finnarne 1170
sub sales_order { &add };
1171
sub purchase_order { &add };
1172