5 use Support::TestSetup;
9 use SL::Dev::Part qw(new_part);
10 use SL::Dev::Shop qw(new_shop new_shop_part new_shop_order);
11 use SL::Dev::CustomerVendor qw(new_customer);
13 use SL::DB::ShopOrder;
14 use SL::DB::ShopOrderItem;
15 use SL::Controller::ShopOrder;
18 my ($shop, $shop_order, $shop_part, $part, $customer, $employee);
26 $transdate = DateTime->today_local;
27 $transdate->set_year(2019) if $transdate->year == 2020; # use year 2019 in 2020, because of tax rate change in Germany
29 $shop = new_shop->save;
30 $part = new_part->save;
31 $shop_part = new_shop_part(part => $part, shop => $shop)->save;
33 $employee = SL::DB::Manager::Employee->current || croak "No employee";
35 $customer = new_customer( name => 'Evil Inc',
36 street => 'Evil Street',
38 email => 'evil@evilinc.com'
42 sub save_shopcontroller_to_string {
45 open(my $outputFH, '>', \$output) or die "OUTPUT";
46 my $oldFH = select $outputFH;
47 my $shop_controller = SL::Controller::ShopOrder->new;
48 $shop_controller->action_transfer;
56 $::form = Support::TestSetup->create_new_form;
57 $::form->{import_id} = $params{import_id};
58 $::form->{customer} = $params{customer};
59 my $test_name = 'Test Controller Action Transfer';
60 save_shopcontroller_to_string();
61 my @links_record = RecordLinks->get_links( 'from_table' => 'shop_orders',
62 'from_id' => $params{import_id},
65 is($links_record[0]->{from_id} , $params{import_id}, "record from id check");
66 is($links_record[0]->{from_table} , 'shop_orders' , "record from table <shop_orders> check");
67 is($links_record[0]->{to_table} , 'oe' , "record to table <oe> check");
70 Support::TestSetup::login();
74 my $trgm = SL::DB::ShopOrder->check_trgm;
76 my $shop_trans_id = 1;
78 $shop_order = new_shop_order(
80 transfer_date => $transdate,
81 shop_trans_id => $shop_trans_id,
82 order_date => $transdate->datetime,
84 billing_lastname => 'Schmidt',
85 billing_firstname => 'Sven',
86 billing_company => 'Evil Inc',
87 billing_street => 'Evil Street 666',
88 billing_zipcode => $customer->zipcode,
89 billing_email => 'email',
92 my $shop_order_item = SL::DB::ShopOrderItem->new(
93 partnumber => $part->partnumber,
97 shop_trans_id => $shop_trans_id,
99 $shop_order->shop_order_items( [ $shop_order_item ] );
102 note('testing check_for_existing_customers');
103 my $fuzzy_customers = $shop_order->check_for_existing_customers;
105 is(scalar @{ $fuzzy_customers }, 1, 'found 1 matching customer');
106 is($fuzzy_customers->[0]->name, 'Evil Inc', 'matched customer Evil Inc');
108 note('adding a not-so-similar customer');
109 my $customer_different = new_customer(
110 name => "Different Name",
111 street => 'Good Straet', # difference large enough from "Evil Street"
112 zipcode => $customer->zipcode,
115 $fuzzy_customers = $shop_order->check_for_existing_customers;
116 is(scalar @{ $fuzzy_customers }, 1, 'still only found 1 matching customer (zipcode equal + street dissimilar');
118 note('adding 2 similar customers and 1 dissimilar but same email');
119 my $customer_similar = new_customer(
120 name => "Different Name",
121 street => 'Evil Street 666', # difference not large enough from "Evil Street", street matches
122 zipcode => $customer->zipcode,
125 my $customer_similar_2 = new_customer(
126 name => "Different Name",
127 street => 'Evil Straet', # difference not large enough from "Evil Street", street matches
128 zipcode => $customer->zipcode,
131 my $customer_same_email = new_customer(
132 name => "Different Name",
133 street => 'Angel Way', # difference large enough from "Evil Street", street not matches , same email
134 zipcode => $customer->zipcode,
137 my $customers = SL::DB::Manager::Customer->get_all();
139 $fuzzy_customers = $shop_order->check_for_existing_customers;
141 is(scalar @{ $fuzzy_customers }, 4, 'found 4 matching customers (zipcode equal + street similar + same email) trgm_pg is installed');
143 is(scalar @{ $fuzzy_customers }, 3, 'found 3 matching customers (zipcode equal + %street% + same email) trgm_pg is not installed, could be 4 with trgm_pg');
146 is($shop->description , 'testshop' , 'shop description ok');
147 is($shop_order->shop_id , $shop->id , "shop_id ok");
149 note('testing convert_to_sales_order');
150 my $order = $shop_order->convert_to_sales_order(employee => $employee, customer => $customer, transdate => $shop_order->order_date);
151 $order->calculate_prices_and_taxes;
154 is(ref($order), 'SL::DB::Order', 'order ok');
155 is($order->amount, 59.5, 'order amount ok');
156 is($order->netamount, 50, 'order netamount ok');
158 test_transfer( import_id => $shop_order->id , customer => $customer->id );
167 "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(OrderItem Order);
168 "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(ShopPart Part ShopOrderItem ShopOrder Shop Customer);