Revision cafccbb1
Von Kivitendo Admin vor mehr als 7 Jahren hinzugefügt
t/helper/shipped_qty.t | ||
---|---|---|
1 |
use strict; |
|
2 |
use Test::More; |
|
3 |
|
|
4 |
use lib 't'; |
|
5 |
use Support::TestSetup; |
|
6 |
use Carp; |
|
7 |
use Test::Exception; |
|
8 |
use Data::Dumper; |
|
9 |
use SL::DB::Part; |
|
10 |
use SL::DB::Inventory; |
|
11 |
use SL::DB::TransferType; |
|
12 |
use SL::DB::Order; |
|
13 |
use SL::DB::DeliveryOrder; |
|
14 |
use SL::DB::Customer; |
|
15 |
use SL::DB::Vendor; |
|
16 |
use SL::DB::RecordLink; |
|
17 |
use SL::DB::DeliveryOrderItemsStock; |
|
18 |
use SL::DB::Bin; |
|
19 |
use SL::WH; |
|
20 |
use SL::AM; |
|
21 |
use SL::Dev::ALL; |
|
22 |
use SL::Helper::ShippedQty; |
|
23 |
use DateTime; |
|
24 |
|
|
25 |
Support::TestSetup::login(); |
|
26 |
|
|
27 |
clear_up(); |
|
28 |
|
|
29 |
my ($customer, $vendor, @parts, $unit); |
|
30 |
|
|
31 |
$customer = SL::Dev::CustomerVendor::create_customer(name => 'Testkunde' )->save; |
|
32 |
$vendor = SL::Dev::CustomerVendor::create_vendor( name => 'Testlieferant')->save; |
|
33 |
|
|
34 |
my $default_sellprice = 10; |
|
35 |
my $default_lastcost = 4; |
|
36 |
|
|
37 |
my ($wh) = SL::Dev::Inventory->create_warehouse_and_bins(); |
|
38 |
my $bin1 = SL::DB::Manager::Bin->find_by(description => "Bin 1"); |
|
39 |
my $bin2 = SL::DB::Manager::Bin->find_by(description => "Bin 2"); |
|
40 |
|
|
41 |
my %part_defaults = ( |
|
42 |
sellprice => $default_sellprice, |
|
43 |
warehouse_id => $wh->id, |
|
44 |
bin_id => $bin1->id |
|
45 |
); |
|
46 |
|
|
47 |
# create 3 parts to be used in test |
|
48 |
for my $i ( 1 .. 4 ) { |
|
49 |
SL::Dev::Part::create_part( %part_defaults, partnumber => $i, description => "part $i test" )->save; |
|
50 |
}; |
|
51 |
|
|
52 |
my $part1 = SL::DB::Manager::Part->find_by( partnumber => '1' ); |
|
53 |
my $part2 = SL::DB::Manager::Part->find_by( partnumber => '2' ); |
|
54 |
my $part3 = SL::DB::Manager::Part->find_by( partnumber => '3' ); |
|
55 |
my $part4 = SL::DB::Manager::Part->find_by( partnumber => '4' ); |
|
56 |
|
|
57 |
my @part_ids; # list of all part_ids to run checks against |
|
58 |
push( @part_ids, $_->id ) foreach ( $part1, $part2, $part3, $part4 ); |
|
59 |
my %default_transfer_params = ( wh => $wh, bin => $bin1, unit => 'Stck'); |
|
60 |
|
|
61 |
|
|
62 |
# test purchases first, so there is actually stock available when sales is tested |
|
63 |
|
|
64 |
note("testing purchases, no fill_up"); |
|
65 |
|
|
66 |
my $purchase_order = SL::Dev::Record::create_purchase_order( |
|
67 |
save => 1, |
|
68 |
orderitems => [ SL::Dev::Record::create_order_item(part => $part1, qty => 11), |
|
69 |
SL::Dev::Record::create_order_item(part => $part2, qty => 12), |
|
70 |
SL::Dev::Record::create_order_item(part => $part3, qty => 13), |
|
71 |
] |
|
72 |
); |
|
73 |
|
|
74 |
SL::Helper::ShippedQty |
|
75 |
->new(require_stock_out => 1) # should make no difference while there is no delivery order |
|
76 |
->calculate($purchase_order) |
|
77 |
->write_to_objects; |
|
78 |
|
|
79 |
is($purchase_order->orderitems->[0]->{shipped_qty}, undef, "first purchase orderitem has no shipped_qty"); |
|
80 |
is($purchase_order->orderitems->[0]->{delivered}, '', "first purchase orderitem is not delivered"); |
|
81 |
|
|
82 |
my $purchase_orderitem_part1 = SL::DB::Manager::OrderItem->find_by( parts_id => $part1->id, trans_id => $purchase_order->id); |
|
83 |
|
|
84 |
is($purchase_orderitem_part1->shipped_qty, undef, "OrderItem shipped_qty method ok"); |
|
85 |
|
|
86 |
is($purchase_order->closed, 0, 'purchase order is open'); |
|
87 |
is($purchase_order->delivered, '', 'purchase order is not delivered'); |
|
88 |
|
|
89 |
note('converting purchase order to delivery order'); |
|
90 |
# create purchase delivery order from purchase order |
|
91 |
my $purchase_delivery_order = $purchase_order->convert_to_delivery_order; |
|
92 |
is($purchase_order->closed, 0, 'purchase order is open'); |
|
93 |
is($purchase_order->delivered, 1, 'purchase order is now delivered'); |
|
94 |
|
|
95 |
SL::Helper::ShippedQty |
|
96 |
->new(require_stock_out => 0) |
|
97 |
->calculate($purchase_order) |
|
98 |
->write_to_objects; |
|
99 |
|
|
100 |
is($purchase_order->orderitems->[0]->{shipped_qty}, 11, "require_stock_out => 0: first purchase orderitem has shipped_qty"); |
|
101 |
is($purchase_order->orderitems->[0]->{delivered}, 1, "require_stock_out => 0: first purchase orderitem is delivered"); |
|
102 |
|
|
103 |
Rose::DB::Object::Helpers::forget_related($purchase_order, 'orderitems'); |
|
104 |
$purchase_order->orderitems; |
|
105 |
|
|
106 |
SL::Helper::ShippedQty |
|
107 |
->new(require_stock_out => 1) |
|
108 |
->calculate($purchase_order) |
|
109 |
->write_to_objects; |
|
110 |
|
|
111 |
is($purchase_order->orderitems->[0]->{shipped_qty}, undef, "require_stock_out => 1: first purchase orderitem has no shipped_qty"); |
|
112 |
is($purchase_order->orderitems->[0]->{delivered}, '', "require_stock_out => 1: first purchase orderitem is not delivered"); |
|
113 |
|
|
114 |
# ship items from delivery order |
|
115 |
SL::Dev::Inventory::transfer_purchase_delivery_order($purchase_delivery_order); |
|
116 |
|
|
117 |
Rose::DB::Object::Helpers::forget_related($purchase_order, 'orderitems'); |
|
118 |
$purchase_order->orderitems; |
|
119 |
|
|
120 |
SL::Helper::ShippedQty |
|
121 |
->new(require_stock_out => 1) # shouldn't make a difference now after shipping |
|
122 |
->calculate($purchase_order) |
|
123 |
->write_to_objects; |
|
124 |
|
|
125 |
is($purchase_order->orderitems->[0]->{shipped_qty}, 11, "require_stock_out => 1: first purchase orderitem has shipped_qty"); |
|
126 |
is($purchase_order->orderitems->[0]->{delivered}, 1, "require_stock_out => 1: first purchase orderitem is delivered"); |
|
127 |
|
|
128 |
my $purchase_orderitem_part1 = SL::DB::Manager::OrderItem->find_by(parts_id => $part1->id, trans_id => $purchase_order->id); |
|
129 |
|
|
130 |
is($purchase_orderitem_part1->shipped_qty(require_stock_out => 1), 11, "OrderItem shipped_qty from helper ok"); |
|
131 |
|
|
132 |
|
|
133 |
note('testing sales, no fill_up'); |
|
134 |
|
|
135 |
my $sales_order = SL::Dev::Record::create_sales_order( |
|
136 |
save => 1, |
|
137 |
orderitems => [ SL::Dev::Record::create_order_item(part => $part1, qty => 5), |
|
138 |
SL::Dev::Record::create_order_item(part => $part2, qty => 6), |
|
139 |
SL::Dev::Record::create_order_item(part => $part3, qty => 7), |
|
140 |
] |
|
141 |
); |
|
142 |
|
|
143 |
SL::Helper::ShippedQty |
|
144 |
->new(require_stock_out => 1) # should make no difference while there is no delivery order |
|
145 |
->calculate($sales_order) |
|
146 |
->write_to_objects; |
|
147 |
|
|
148 |
is($sales_order->orderitems->[0]->{shipped_qty}, undef, "first sales orderitem has no shipped_qty"); |
|
149 |
is($sales_order->orderitems->[0]->{delivered}, '', "first sales orderitem is not delivered"); |
|
150 |
|
|
151 |
my $orderitem_part1 = SL::DB::Manager::OrderItem->find_by(parts_id => $part1->id, trans_id => $sales_order->id); |
|
152 |
my $orderitem_part2 = SL::DB::Manager::OrderItem->find_by(parts_id => $part2->id, trans_id => $sales_order->id); |
|
153 |
|
|
154 |
is($orderitem_part1->shipped_qty, undef, "OrderItem shipped_qty method ok"); |
|
155 |
|
|
156 |
# create sales delivery order from sales order |
|
157 |
my $sales_delivery_order = $sales_order->convert_to_delivery_order; |
|
158 |
|
|
159 |
SL::Helper::ShippedQty |
|
160 |
->new(require_stock_out => 0) |
|
161 |
->calculate($sales_order) |
|
162 |
->write_to_objects; |
|
163 |
|
|
164 |
is($sales_order->orderitems->[0]->{shipped_qty}, 5, "require_stock_out => 0: first sales orderitem has shipped_qty"); |
|
165 |
is($sales_order->orderitems->[0]->{delivered}, 1, "require_stock_out => 0: first sales orderitem is delivered"); |
|
166 |
|
|
167 |
Rose::DB::Object::Helpers::forget_related($sales_order, 'orderitems'); |
|
168 |
$sales_order->orderitems; |
|
169 |
|
|
170 |
SL::Helper::ShippedQty |
|
171 |
->new(require_stock_out => 1) |
|
172 |
->calculate($sales_order) |
|
173 |
->write_to_objects; |
|
174 |
|
|
175 |
is($sales_order->orderitems->[0]->{shipped_qty}, undef, "require_stock_out => 1: first sales orderitem has no shipped_qty"); |
|
176 |
is($sales_order->orderitems->[0]->{delivered}, '', "require_stock_out => 1: first sales orderitem is not delivered"); |
|
177 |
|
|
178 |
# ship items from delivery order |
|
179 |
SL::Dev::Inventory::transfer_sales_delivery_order($sales_delivery_order); |
|
180 |
|
|
181 |
Rose::DB::Object::Helpers::forget_related($sales_order, 'orderitems'); |
|
182 |
$sales_order->orderitems; |
|
183 |
|
|
184 |
SL::Helper::ShippedQty |
|
185 |
->new(require_stock_out => 1) |
|
186 |
->calculate($sales_order) |
|
187 |
->write_to_objects; |
|
188 |
|
|
189 |
is($sales_order->orderitems->[0]->{shipped_qty}, 5, "require_stock_out => 1: first sales orderitem has no shipped_qty"); |
|
190 |
is($sales_order->orderitems->[0]->{delivered}, 1, "require_stock_out => 1: first sales orderitem is not delivered"); |
|
191 |
|
|
192 |
$orderitem_part1 = SL::DB::Manager::OrderItem->find_by(parts_id => $part1->id, trans_id => $sales_order->id); |
|
193 |
|
|
194 |
is($orderitem_part1->shipped_qty(require_stock_out => 1), 5, "OrderItem shipped_qty from helper ok"); |
|
195 |
|
|
196 |
|
|
197 |
note('misc tests'); |
|
198 |
my $number_of_linked_items = SL::DB::Manager::RecordLink->get_all_count( where => [ from_table => 'orderitems', to_table => 'delivery_order_items' ] ); |
|
199 |
is ($number_of_linked_items , 6, "6 record_links for items, 3 from sales order, 3 from purchase order"); |
|
200 |
|
|
201 |
clear_up(); |
|
202 |
|
|
203 |
done_testing; |
|
204 |
|
|
205 |
sub clear_up { |
|
206 |
foreach ( qw(Inventory DeliveryOrderItem DeliveryOrder Price OrderItem Order Part Customer Vendor Bin Warehouse) ) { |
|
207 |
"SL::DB::Manager::${_}"->delete_all(all => 1); |
|
208 |
} |
|
209 |
}; |
Auch abrufbar als: Unified diff
Erster Test für shipped_qty.t