/
OE.pm
2139 lines (1756 loc) · 66.2 KB
/
OE.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
=head1 NAME
LedgerSMB::OE - Order Entry
=cut
#=====================================================================
# LedgerSMB
# Small Medium Business Accounting software
# http://www.ledgersmb.org/
#
# Copyright (C) 2006
# This work contains copyrighted information from a number of sources all used
# with permission.
#
# This file contains source code included with or based on SQL-Ledger which
# is Copyright Dieter Simader and DWS Systems Inc. 2000-2005 and licensed
# under the GNU General Public License version 2 or, at your option, any later
# version. For a full list including contact information of contributors,
# maintainers, and copyright holders, see the CONTRIBUTORS file.
#
# Original Copyright Notice from SQL-Ledger 2.6.17 (before the fork):
# Copyright (C) 2001
#
# Author: DWS Systems Inc.
# Web: http://www.sql-ledger.org
#
# Contributors:
#
#======================================================================
#
# This file has undergone whitespace cleanup
#
#======================================================================
#
# Order entry module
# Quotation
#
#======================================================================
package OE;
use LedgerSMB::Magic qw(OEC_QUOTATION OEC_RFQ);
use LedgerSMB::Num2text;
use LedgerSMB::Setting;
use LedgerSMB::Tax;
use Log::Any;
my $logger = Log::Any->get_logger(category => 'OE');
=over
=item get_files
Returns a list of files associated with the existing transaction. This is
provisional, and will change for 1.4 as the GL transaction functionality is
{ref_key => $self->{id}, file_class => 2}
rewritten
=cut
sub get_files {
my ($self, $form, $locale) = @_;
return if !$form->{id};
my $file = LedgerSMB::File->new;
@{$form->{files}} = $file->list({ref_key => $form->{id}, file_class => 2});
@{$form->{file_links}} = $file->list_links(
{ref_key => $form->{id}, file_class => 2}
);
}
=item get_type
Sets the type field for an existing order or quotation
=cut
sub get_type {
my ($self, $form) = @_;
my $dbh = $form->{dbh};
my @types = qw(null sales_order purchase_order sales_quotation
request_quotation);
my $sth = $dbh->prepare('select oe_class_id from oe where id = ?');
$sth->execute($form->{id});
my ($class) = $sth->fetchrow_array;
$form->{type} = $types[$class];
$sth->finish;
}
sub _db_prepare_vars {
my $self = shift;
for (@_) {
if ( !$self->{$_} and $self->{$_} ne "0" ) {
undef $self->{$_};
}
}
}
sub save {
my ( $self, $myconfig, $form ) = @_;
$form->all_business_units;
_db_prepare_vars( $form,
"quonumber", "transdate", "vendor_id", "entity_id",
"reqdate", "taxincluded", "shippingpoint", "shipvia",
"currency", "department_id", "employee_id", "language_code",
"ponumber", "terms"
);
my $dbh = $form->{dbh};
my $b_unit_sth = $dbh->prepare(
"INSERT INTO business_unit_oitem (entry_id, class_id, bu_id)
VALUES (currval('orderitems_id_seq'), ?, ?)"
);
my @queryargs;
my $quotation;
my $ordnumber;
my $numberfld;
my $class_id;
$form->{vc} = ( $form->{vc} eq 'customer' ) ? 'customer' : 'vendor';
if ( $form->{type} =~ /_order$/ ) {
$quotation = "0";
$ordnumber = "ordnumber";
if ($form->{vc} eq 'customer'){
$numberfld = "sonumber";
$class_id = 1;
} else {
$numberfld = "ponumber";
$class_id = 2;
}
}
else {
$quotation = "1";
$ordnumber = "quonumber";
if ( $form->{vc} eq 'customer' ) {
$numberfld = "sqnumber";
$class_id = OEC_QUOTATION;
} else {
$numberfld = "rfqnumber";
$class_id = OEC_RFQ;
}
}
$form->{"$ordnumber"} =
$form->update_defaults( $myconfig, $numberfld, $dbh )
if $form->should_update_defaults($ordnumber);
my $query;
my $sth;
my $null;
my $exchangerate = 0;
( $null, $form->{employee_id} ) = split /--/, $form->{employee};
if ( !$form->{employee_id} ) {
( $form->{employee}, $form->{employee_id} ) = $form->get_employee;
$form->{employee} = "$form->{employee}--$form->{employee_id}";
}
my $ml = ( $form->{type} eq 'sales_order' ) ? 1 : -1;
$query = qq|
SELECT p.assembly
FROM parts p WHERE p.id = ?|;
my $pth = $dbh->prepare($query) || $form->dberror($query);
if ( $form->{id} ) {
$query = qq|SELECT id FROM oe WHERE id = $form->{id}|;
if ( $dbh->selectrow_array($query) ) {
$query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
$sth = $dbh->prepare($query);
$sth->execute( $form->{id} ) || $form->dberror($query);
$query = qq|DELETE FROM new_shipto WHERE oe_id = ?|;
$sth = $dbh->prepare($query);
$sth->execute( $form->{id} ) || $form->dberror($query);
}
else { # id is not in the database
delete $form->{id};
}
}
my $did_insert = 0;
if ( !$form->{id} ) {
if (! $form->{workflow_id}) {
my $wf = $form->{_wire}->get('workflows')
->create_workflow( 'Order/Quote' );
$form->{workflow_id} = $wf->id;
}
$query = qq|SELECT nextval('oe_id_seq')|;
$sth = $dbh->prepare($query) || $form->dberror($query);
$sth->execute || $form->dberror($query);
( $form->{id} ) = $sth->fetchrow_array;
$sth->finish;
if ( !$form->{reqdate} ) {
$form->{reqdate} = undef;
}
if ( !$form->{transdate} ) {
$form->{transdate} = "now";
}
if ( ( $form->{closed} ne 't' ) and ( $form->{closed} ne "1" ) ) {
$form->{closed} = 'f';
}
$query = qq|
INSERT INTO oe
(id, ordnumber, quonumber, transdate,
reqdate, shippingpoint, shipvia,
notes, intnotes, curr, closed,
person_id, language_code, ponumber, terms,
quotation, oe_class_id, entity_credit_account, workflow_id)
VALUES
(?, ?, ?,
?, ?, ?,
?, ?, ?, ?, ?,
?, ?, ?, ?,
?, ?, ?, ?)|;
@queryargs = (
$form->{id},
$form->{ordnumber}, $form->{quonumber},
$form->{transdate}, $form->{reqdate},
$form->{shippingpoint}, $form->{shipvia},
$form->{notes}, $form->{intnotes},
$form->{currency}, $form->{closed},
$form->{person_id},
$form->{language_code}, $form->{ponumber},
$form->{terms}, $quotation, $class_id, $form->{"$form->{vc}_id"},
$form->{workflow_id}
);
$sth = $dbh->prepare($query);
$sth->execute(@queryargs) || $form->dberror($query);
$sth->finish;
}
my $amount;
my $linetotal;
my $discount;
my $project_id;
my $taxrate;
my $taxamount;
my $fxsellprice;
my %taxbase;
my @taxaccounts;
my %taxaccounts;
my $netamount = 0;
my $rowcount = $form->{rowcount};
for my $i ( 1 .. $rowcount ) {
$form->{"ship_$i"} = 0 unless $form->{"ship_$i"};
_db_prepare_vars( $form, "orderitems_id_$i", "id_$i", "description_$i",
"project_id_$i" );
for (qw(qty ship)) {
$form->{"${_}_$i"} =
$form->parse_amount( $myconfig, $form->{"${_}_$i"} );
}
$form->{"discount_$i"} =
$form->parse_amount( $myconfig, $form->{"discount_$i"} ) / 100;
$form->{"sellprice_$i"} =
$form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
if ( $form->{"qty_$i"} ) {
$pth->execute( $form->{"id_$i"} );
$ref = $pth->fetchrow_hashref(NAME_lc);
for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
$pth->finish;
$fxsellprice = $form->{"sellprice_$i"};
my ($dec) = ( $form->{"sellprice_$i"} =~ /\.(\d+)/ );
$dec = length $dec;
my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
$discount =
$form->round_amount(
$form->{"sellprice_$i"} * $form->{"discount_$i"},
$decimalplaces );
$form->{"sellprice_$i"} =
$form->round_amount( $form->{"sellprice_$i"} - $discount,
$decimalplaces );
$linetotal =
$form->round_amount( $form->{"sellprice_$i"} * $form->{"qty_$i"},
2 );
@taxaccounts = Tax::init_taxes( $form, $form->{"taxaccounts_$i"},
$form->{taxaccounts} );
if ( $form->{taxincluded} ) {
$taxamount =
Tax::calculate_taxes( \@taxaccounts, $form, $linetotal, 1 );
$form->{"sellprice_$i"} =
Tax::extract_taxes( \@taxaccounts, $form,
$form->{"sellprice_$i"} );
$taxbase =
Tax::extract_taxes( \@taxaccounts, $form, $linetotal );
}
else {
$taxamount =
Tax::apply_taxes( \@taxaccounts, $form, $linetotal );
$taxbase = $linetotal;
}
if ( @taxaccounts && $form->round_amount( $taxamount, 2 ) == 0 ) {
if ( $form->{taxincluded} ) {
foreach my $item (@taxaccounts) {
$taxamount = $form->round_amount( $item->value, 2 );
$taxaccounts{ $item->account } += $taxamount;
$taxdiff += $taxamount;
$taxbase{ $item->account } += $taxbase;
}
$taxaccounts{ $taxaccounts[0]->account } += $taxdiff;
}
else {
foreach my $item (@taxaccounts) {
$taxaccounts{ $item->account } += $item->value;
$taxbase{ $item->account } += $taxbase;
}
}
}
else {
foreach my $item (@taxaccounts) {
$taxaccounts{ $item->account } += $item->value;
$taxbase{ $item->account } += $taxbase;
}
}
$netamount += $form->{"sellprice_$i"} * $form->{"qty_$i"};
$project_id = "0";
if ( $form->{"projectnumber_$i"} ne "" ) {
( $null, $project_id ) = split /--/,
$form->{"projectnumber_$i"};
}
$project_id = $form->{"project_id_$i"}
if $form->{"project_id_$i"};
if ( !$form->{"reqdate_$i"} ) {
$form->{"reqdate_$i"} = undef;
}
@queryargs = ();
# save detail record in orderitems table
$query = qq|INSERT INTO orderitems (
trans_id, parts_id, description, qty, sellprice,
discount, unit, reqdate, ship,
serialnumber, notes, precision)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)|;
$sth = $dbh->prepare($query);
push( @queryargs,
$form->{id}, $form->{"id_$i"},
$form->{"description_$i"}, $form->{"qty_$i"},
$fxsellprice, $form->{"discount_$i"},
$form->{"unit_$i"}, $form->{"reqdate_$i"},
$form->{"ship_$i"},
$form->{"serialnumber_$i"}, $form->{"notes_$i"},
$form->{"precision_$i"}
);
$sth->execute(@queryargs) || $form->dberror($query);
$form->{"sellprice_$i"} = $fxsellprice;
for my $cls(@{$form->{bu_class}}){
if ($form->{"b_unit_$cls->{id}_$i"}){
$b_unit_sth->execute($cls->{id}, $form->{"b_unit_$cls->{id}_$i"});
}
}
}
$form->{"discount_$i"} *= 100;
}
# set values which could be empty
for (qw(entity_id taxincluded closed quotation)) {
$form->{$_} *= 1;
}
# add up the tax
my $tax = 0;
for ( keys %taxaccounts ) { $tax += $taxaccounts{$_} }
$amount = $form->round_amount( $netamount + $tax, 2 );
$netamount = $form->round_amount( $netamount, 2 );
if ( $form->{currency} eq $form->{defaultcurrency} ) {
$form->{exchangerate} = 1;
}
else {
$exchangerate = "";
}
$form->{exchangerate} =
($exchangerate)
? $exchangerate
: $form->parse_amount( $myconfig, $form->{exchangerate} );
( $null, $form->{department_id} ) = split( /--/, $form->{department} );
for (qw(department_id terms)) { $form->{$_} *= 1 }
if ($did_insert) {
$query = qq|
UPDATE oe SET
amount_tc = ?,
netamount_tc = ?,
taxincluded = ?,
WHERE id = ?|;
@queryargs = ( $amount, $netamount, $form->{taxincluded}, $form->{id} );
}
else {
# save OE record
$query = qq|
UPDATE oe set
ordnumber = ?,
quonumber = ?,
transdate = ?,
amount_tc = ?,
netamount_tc = ?,
reqdate = ?,
taxincluded = ?,
shippingpoint = ?,
shipvia = ?,
notes = ?,
intnotes = ?,
curr = ?,
closed = ?,
quotation = ?,
person_id = ?,
language_code = ?,
ponumber = ?,
terms = ?
WHERE id = ?|;
if ( !$form->{reqdate} ) {
$form->{reqdate} = undef;
}
@queryargs = (
$form->{ordnumber}, $form->{quonumber},
$form->{transdate}, $amount,
$netamount, $form->{reqdate},
$form->{taxincluded}, $form->{shippingpoint},
$form->{shipvia}, $form->{notes},
$form->{intnotes}, $form->{currency},
$form->{closed}, $quotation,
$form->{employee_id},
$form->{language_code}, $form->{ponumber},
$form->{terms}, $form->{id}
);
}
$sth = $dbh->prepare($query) || $form->dberror($query);
$form->{ordtotal} = $amount;
$sth->execute(@queryargs) || $form->dberror($query);
# add shipto
$form->{name} = $form->{ $form->{vc} };
$form->{name} =~ s/--$form->{"$form->{vc}_id"}//;
$form->add_shipto($form->{id}, 1);
# save printed, emailed
$form->save_status($dbh);
if ( $form->{type} =~ /_order$/ ) {
# adjust onhand
&adj_inventory( $dbh, $myconfig, $form );
}
my %audittrail = (
tablename => 'oe',
reference => ( $form->{type} =~ /_order$/ )
? $form->{ordnumber}
: $form->{quonumber},
formname => $form->{type},
action => 'saved',
id => $form->{id}
);
return 1;
}
sub delete {
my ( $self, $myconfig, $form ) = @_;
my $dbh = $form->{dbh};
$query = qq|
SELECT o.parts_id, o.ship, p.inventory_accno_id, p.assembly
FROM orderitems o
JOIN parts p ON (p.id = o.parts_id)
WHERE trans_id = ?|;
$sth = $dbh->prepare($query);
$sth->execute( $form->{id} ) || $form->dberror($query);
if ( $form->{type} =~ /_order$/ ) {
$ml = ( $form->{type} eq 'purchase_order' ) ? -1 : 1;
while ( my ( $id, $ship, $inv, $assembly ) = $sth->fetchrow_array ) {
$form->update_balance( $dbh, "parts", "onhand", "id = $id",
$ship * $ml )
if ( $inv || $assembly );
}
}
$sth->finish;
# delete status entries
$query = qq|DELETE FROM status WHERE trans_id = ?|;
$sth = $dbh->prepare($query);
$sth->execute( $form->{id} ) || $form->dberror($query);
$sth->finish;
# delete OE record
$query = qq|DELETE FROM oe WHERE id = ?|;
$sth = $dbh->prepare($query);
$sth->execute( $form->{id} ) || $form->dberror($query);
$sth->finish;
# delete individual entries
$query = qq|DELETE FROM orderitems WHERE trans_id = ?|;
$sth->finish;
$query = qq|DELETE FROM new_shipto WHERE oe_id = ?|;
$sth = $dbh->prepare($query);
$sth->execute( $form->{id} ) || $form->dberror($query);
$sth->finish;
return 1;
}
sub retrieve {
use LedgerSMB::PriceMatrix;
my ( $self, $myconfig, $form ) = @_;
my $dbh = $form->{dbh};
my $bu_sth = $dbh->prepare(
qq|SELECT * FROM business_unit_oitem
WHERE entry_id = ? |
);
my $query;
my $sth;
my $var;
my $ref;
@{$form->{currencies}} =
(LedgerSMB::Setting->new(%$form))->get_currencies;
$form->{defaultcurrency} = $form->{currencies}->[0];
$form->{lock_description} = $form->get_setting( 'lock_description' );
if ( $form->{id} ) {
# retrieve order
$query = qq|
SELECT o.ordnumber, o.transdate, o.reqdate, o.terms, o.workflow_id,
o.taxincluded, o.shippingpoint, o.shipvia,
o.notes, o.intnotes, o.curr AS currency,
pe.first_name \|\| ' ' \|\| pe.last_name AS employee,
o.person_id AS employee_id,
o.entity_credit_account, vc.name as legal_name,
o.amount_tc AS invtotal, o.closed, o.reqdate,
o.quonumber, o.language_code,
o.ponumber, cr.entity_class,
ns.location_id as locationid
FROM oe o
JOIN entity_credit_account cr ON (cr.id = o.entity_credit_account)
JOIN entity vc ON (cr.entity_id = vc.id)
LEFT JOIN person pe ON (o.person_id = pe.entity_id)
LEFT JOIN new_shipto ns ON ns.oe_id = o.id
WHERE o.id = ?|;
$sth = $dbh->prepare($query);
$sth->execute( $form->{id} ) || $form->dberror($query);
$ref = $sth->fetchrow_hashref('NAME_lc');
if ($ref->{entity_class} == 2){
$form->{vc} = 'customer';
} elsif ($ref->{entity_class} == 1){
$form->{vc} = 'vendor';
}
$form->{$form->{vc}} = $ref->{legal_name};
$form->{"$form->{vc}_id"} = $ref->{entity_credit_account};
$form->db_parse_numeric(sth=>$sth, hashref=>$ref);
for ( keys %$ref ) { $form->{$_} = $ref->{$_} }
$sth->finish;
$query = qq|SELECT * FROM new_shipto WHERE oe_id = ?|;
$sth = $dbh->prepare($query);
$sth->execute( $form->{id} ) || $form->dberror($query);
$ref = $sth->fetchrow_hashref('NAME_lc');
for ( keys %$ref ) { $form->{$_} = $ref->{$_} unless ( $_ eq "id") }
$sth->finish;
# get printed, emailed
$query = qq|
SELECT s.printed, s.emailed, s.formname
FROM status s
WHERE s.trans_id = ?|;
$sth = $dbh->prepare($query);
$sth->execute( $form->{id} ) || $form->dberror($query);
while ( $ref = $sth->fetchrow_hashref(NAME_lc) ) {
$form->{printed} .= "$ref->{formname} "
if $ref->{printed};
$form->{emailed} .= "$ref->{formname} "
if $ref->{emailed};
}
$sth->finish;
for (qw(printed emailed)) { $form->{$_} =~ s/ +$//g }
# retrieve individual items
$query = qq|
SELECT o.id AS orderitems_id,
COALESCE(CASE WHEN pv.partnumber <> ''
THEN pv.partnumber ELSE null
END, p.partnumber) AS partnumber,
p.assembly,
o.description, o.qty, o.sellprice, o.precision,
o.parts_id AS id, o.unit, o.discount, p.bin,
o.reqdate, o.ship, o.serialnumber,
o.notes, pg.partsgroup,
p.partsgroup_id, p.partnumber AS sku,
p.listprice, p.lastcost, p.weight, p.onhand,
p.inventory_accno_id, p.income_accno_id,
p.expense_accno_id, t.description
AS partsgrouptranslation
FROM orderitems o
JOIN parts p ON (o.parts_id = p.id)
LEFT JOIN partsgroup pg ON (p.partsgroup_id = pg.id)
LEFT JOIN partsvendor pv ON (pv.parts_id = p.id
AND pv.credit_id = ?)
LEFT JOIN partsgroup_translation t
ON (t.trans_id = p.partsgroup_id
AND t.language_code = ?)
WHERE o.trans_id = ?
ORDER BY o.id|;
$sth = $dbh->prepare($query);
# The use of vendor_id below helps ensure that partsvendor drops out
# for sales orders. --CT
$sth->execute( $form->{vendor_id}, $form->{language_code}, $form->{id} )
|| $form->dberror($query);
# foreign exchange rates
&exchangerate_defaults;
# query for price matrix
my $pmh = PriceMatrix::price_matrix_query( $dbh, $form );
# taxes
$query = qq|
SELECT c.accno FROM account c
JOIN partstax pt ON (pt.chart_id = c.id)
WHERE pt.parts_id = ?|;
my $tth = $dbh->prepare($query) || $form->dberror($query);
my $taxrate;
my $ptref;
my $sellprice;
while ( $ref = $sth->fetchrow_hashref('NAME_lc') ) {
PriceMatrix::price_matrix( $pmh, $ref, $form->{transdate},
$decimalplaces, $form, $myconfig );
$form->db_parse_numeric(sth=>$sth, hashref=>$ref);
$bu_sth->execute($ref->{orderitems_id});
while ( $buref = $bu_sth->fetchrow_hashref(NAME_lc) ) {
$ref->{"b_unit_$buref->{class_id}"} = $buref->{bu_id};
}
($decimalplaces) = ( $ref->{sellprice} =~ /\.(\d+)/ );
$decimalplaces = length $decimalplaces;
$decimalplaces = ( $decimalplaces > 2 ) ? $decimalplaces : 2;
$tth->execute( $ref->{id} );
$ref->{taxaccounts} = "";
$taxrate = 0;
while ( $ptref = $tth->fetchrow_hashref(NAME_lc) ) {
$ref->{taxaccounts} .= "$ptref->{accno} ";
$taxrate += $form->{"$ptref->{accno}_rate"};
}
$tth->finish;
chop $ref->{taxaccounts};
# multiply by exchangerate
$ref->{sellprice} =
$form->round_amount(
$ref->{sellprice} *
( $form->{ $form->{currency} } // 1),
$decimalplaces );
for (qw(listprice lastcost)) {
$ref->{$_} =
$form->round_amount(
$ref->{$_} / ($form->{ $form->{currency} } // 1),
$decimalplaces );
}
$ref->{partsgroup} = $ref->{partsgrouptranslation}
if $ref->{partsgrouptranslation};
push @{ $form->{form_details} }, $ref;
}
$sth->finish;
# get recurring transaction
$form->get_recurring;
}
else {
delete $form->{notes};
}
}
sub exchangerate_defaults {
$form->{ $form->{currency} } = ($form->{exchangerate} // 0)
if $form->{exchangerate};
$form->{ $form->{currency} } ||= 1;
$form->{ $form->{defaultcurrency} } = 1;
}
sub order_details {
my ( $self, $myconfig, $form ) = @_;
my $dbh = $form->{dbh};
my $query;
my $sth;
my $item;
my $i;
my @sortlist = ();
my @queryargs;
my @taxaccounts;
my %taxaccounts; # I don't think this works.
my $tax;
my $taxrate;
my $taxamount;
$query = qq|
SELECT inventory_accno_id, income_accno_id,
expense_accno_id, assembly FROM parts
WHERE id = ?|;
my $pth = $dbh->prepare($query) || $form->dberror($query);
my $sortby;
# sort items by project and partsgroup
foreach my $i ( 1 .. $form->{rowcount} ) {
if ( $form->{"id_$i"} ) {
# account numbers
$pth->execute( $form->{"id_$i"} );
$ref = $pth->fetchrow_hashref(NAME_lc);
for ( keys %$ref ) { $form->{"${_}_$i"} = $ref->{$_} }
$pth->finish;
$form->{partsgroup} = "";
$form->{projectnumber} = "";
if ( $form->{grouppartsgroup} )
{
$inventory_accno_id =
( $form->{"inventory_accno_id_$i"} || $form->{"assembly_$i"} )
? "1"
: "";
( $form->{partsgroup} ) = split /--/, $form->{"partsgroup_$i"};
}
$sortby = qq|$projectnumber$form->{partsgroup}|;
if ( $form->{sortby} ne 'runningnumber' ) {
for (qw(partnumber description bin)) {
$sortby .= $form->{"${_}_$i"}
if $form->{sortby} eq $_;
}
}
push @sortlist,
[
$i,
"$projectnumber$form->{partsgroup}" . "$inventory_accno_id",
$form->{projectnumber},
$projectnumber_id,
$form->{partsgroup},
$sortby
];
}
}
delete $form->{projectnumber};
# sort the whole thing by project and group
@sortlist = sort { $a->[5] cmp $b->[5] } @sortlist; ## no critic (ProhibitMagicNumbers) sniff
# if there is a warehouse limit picking
if ( $form->{warehouse_id} && $form->{formname} =~ /(pick|packing)_list/ ) {
# run query to check for inventory
$query = qq|
SELECT sum(qty) AS qty FROM warehouse_inventory
WHERE parts_id = ? AND warehouse_id = ?|;
$sth = $dbh->prepare($query) || $form->dberror($query);
foreach my $i ( 1 .. $form->{rowcount} ) {
$sth->execute( $form->{"id_$i"}, $form->{warehouse_id} )
|| $form->dberror;
my @qtylist = $sth->fetchrow_array;
$form->db_parse_numeric(sth=>$sth, arrayref=>\@qtylist);
($qty) = @qtylist; $sth->fetchrow_array;
$sth->finish;
$form->{"qty_$i"} = 0 if $qty == 0;
if ( $form->parse_amount( $myconfig, $form->{"ship_$i"} ) > $qty ) {
$form->{"ship_$i"} = $form->format_amount( $myconfig, $qty );
}
}
}
my $runningnumber = 1;
my $sameitem = "";
my $subtotal;
my $k = scalar @sortlist;
my $j = 0;
foreach my $item (@sortlist) {
$i = $item->[0];
$j++;
if ( $form->{groupprojectnumber} || $form->{grouppartsgroup} ) {
if ( $item->[1] ne $sameitem ) {
$sameitem = $item->[1];
$ok = 0;
if ( $form->{groupprojectnumber} ) {
$ok = $form->{"projectnumber_$i"};
}
if ( $form->{grouppartsgroup} ) {
$ok = $form->{"partsgroup_$i"}
unless $ok;
}
if ($ok) {
if ( $form->{"inventory_accno_id_$i"}
|| $form->{"assembly_$i"} )
{
push( @{ $form->{part} }, "" );
push( @{ $form->{service} }, NULL );
}
else {
push( @{ $form->{part} }, NULL );
push( @{ $form->{service} }, "" );
}
push( @{ $form->{description} }, $item->[2] );
for (
qw(taxrates runningnumber
number sku qty ship unit bin
serialnumber requiredate
projectnumber sellprice
listprice netprice discount
discountrate linetotal weight
itemnotes)
)
{
push( @{ $form->{$_} }, "" );
}
push( @{ $form->{lineitems} }, { amount => 0, tax => 0 } );
}
}
}
$form->{"qty_$i"} = $form->parse_amount( $myconfig, $form->{"qty_$i"} );
$form->{"ship_$i"} =
$form->parse_amount( $myconfig, $form->{"ship_$i"} );
if ( $form->{"qty_$i"} ) {
$form->{discount} = [] if ref $form->{discount} ne 'ARRAY';
$form->{totalqty} += $form->{"qty_$i"};
$form->{totalship} += $form->{"ship_$i"};
$form->{totalweight} +=
( $form->{"weight_$i"} * $form->{"qty_$i"} );
$form->{totalweightship} +=
( $form->{"weight_$i"} * $form->{"ship_$i"} );
# add number, description and qty to $form->{number}
push( @{ $form->{runningnumber} }, $runningnumber++ );
push( @{ $form->{number} }, qq|$form->{"partnumber_$i"}| );
push( @{ $form->{sku} }, qq|$form->{"sku_$i"}| );
push( @{ $form->{item_description} }, qq|$form->{"description_$i"}| );
push( @{ $form->{itemnotes} }, $form->{"notes_$i"} );
push(
@{ $form->{qty} },
$form->format_amount( $myconfig, $form->{"qty_$i"} )
);
push(
@{ $form->{ship} },
$form->format_amount( $myconfig, $form->{"ship_$i"} )
);
push( @{ $form->{unit} }, qq|$form->{"unit_$i"}| );
push( @{ $form->{bin} }, qq|$form->{"bin_$i"}| );
push( @{ $form->{serialnumber} }, qq|$form->{"serialnumber_$i"}| );
push( @{ $form->{requiredate} }, qq|$form->{"reqdate_$i"}| );
push( @{ $form->{projectnumber} },
qq|$form->{"projectnumber_$i"}| );
push( @{ $form->{sellprice} }, $form->{"sellprice_$i"} );
push( @{ $form->{listprice} }, $form->{"listprice_$i"} );
push(
@{ $form->{weight} },
$form->format_amount(
$myconfig, $form->{"weight_$i"} * $form->{"ship_$i"}
)
);
my $sellprice =
$form->parse_amount( $myconfig, $form->{"sellprice_$i"} );
my ($dec) = ( $sellprice =~ /\.(\d+)/ );
$dec = length $dec;
my $decimalplaces = ( $dec > 2 ) ? $dec : 2;
my $discount = $form->round_amount(
$sellprice * $form->parse_amount( $myconfig, $form->{"discount_$i"} ) / 100, $decimalplaces
);
# keep a netprice as well, (sellprice - discount)
$form->{"netprice_$i"} = $sellprice - $discount;
my $linetotal =
$form->round_amount( $form->{"qty_$i"} * $form->{"netprice_$i"},
2 );
if ( $form->{"inventory_accno_id_$i"}
|| $form->{"assembly_$i"} )
{
push( @{ $form->{part} }, $form->{"sku_$i"} );
push( @{ $form->{service} }, NULL );
$form->{totalparts} += $linetotal;
}
else {
push( @{ $form->{service} }, $form->{"sku_$i"} );
push( @{ $form->{part} }, NULL );
$form->{totalservices} += $linetotal;
}
push(
@{ $form->{netprice} },
( $form->{"netprice_$i"} )
? $form->format_amount( $myconfig, $form->{"netprice_$i"},
$decimalplaces )
: " "
);
$discount =
($discount)
? $form->format_amount( $myconfig, $discount * -1,
$decimalplaces )
: " ";
push( @{ $form->{discount} }, $discount );
push(
@{ $form->{discountrate} },
$form->format_amount( $myconfig, $form->{"discount_$i"} )
);
$form->{ordtotal} += $linetotal;
# this is for the subtotals for grouping
$subtotal += $linetotal;