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);
25 $shop = new_shop->save;
26 $part = new_part->save;
27 $shop_part = new_shop_part(part => $part, shop => $shop)->save;
29 $employee = SL::DB::Manager::Employee->current || croak "No employee";
31 $customer = new_customer( name => 'Evil Inc',
32 street => 'Evil Street',
34 email => 'evil@evilinc.com'
38 sub save_shorcontroller_to_string {
41 open(my $outputFH, '>', \$output) or die "OUTPUT";
42 my $oldFH = select $outputFH;
43 my $shor_controller = SL::Controller::ShopOrder->new;
44 $shor_controller->action_transfer;
52 $::form = Support::TestSetup->create_new_form;
53 $::form->{import_id} = $params{import_id};
54 $::form->{customer} = $params{customer};
55 my $test_name = 'Test Controller Action Transfer';
56 save_shorcontroller_to_string();
57 my @links_record = RecordLinks->get_links( 'from_table' => 'shop_orders',
58 'from_id' => $params{import_id},
61 is($links_record[0]->{from_id} , $params{import_id}, "record from id check");
62 is($links_record[0]->{from_table} , 'shop_orders' , "record from table <shop_orders> check");
63 is($links_record[0]->{to_table} , 'oe' , "record to table <oe> check");
66 Support::TestSetup::login();
70 my $trgm = SL::DB::ShopOrder->check_trgm;
72 my $shop_trans_id = 1;
74 $shop_order = new_shop_order(
76 shop_trans_id => $shop_trans_id,
78 billing_lastname => 'Schmidt',
79 billing_firstname => 'Sven',
80 billing_company => 'Evil Inc',
81 billing_street => 'Evil Street 666',
82 billing_zipcode => $customer->zipcode,
83 billing_email => 'email',
86 my $shop_order_item = SL::DB::ShopOrderItem->new(
87 partnumber => $part->partnumber,
91 shop_trans_id => $shop_trans_id,
93 $shop_order->shop_order_items( [ $shop_order_item ] );
96 note('testing check_for_existing_customers');
97 my $fuzzy_customers = $shop_order->check_for_existing_customers;
99 is(scalar @{ $fuzzy_customers }, 1, 'found 1 matching customer');
100 is($fuzzy_customers->[0]->name, 'Evil Inc', 'matched customer Evil Inc');
102 note('adding a not-so-similar customer');
103 my $customer_different = new_customer(
104 name => "Different Name",
105 street => 'Good Straet', # difference large enough from "Evil Street"
106 zipcode => $customer->zipcode,
109 $fuzzy_customers = $shop_order->check_for_existing_customers;
110 is(scalar @{ $fuzzy_customers }, 1, 'still only found 1 matching customer (zipcode equal + street dissimilar');
112 note('adding 2 similar customers and 1 dissimilar but same email');
113 my $customer_similar = new_customer(
114 name => "Different Name",
115 street => 'Evil Street 666', # difference not large enough from "Evil Street", street matches
116 zipcode => $customer->zipcode,
119 my $customer_similar_2 = new_customer(
120 name => "Different Name",
121 street => 'Evil Straet', # difference not large enough from "Evil Street", street matches
122 zipcode => $customer->zipcode,
125 my $customer_same_email = new_customer(
126 name => "Different Name",
127 street => 'Angel Way', # difference large enough from "Evil Street", street not matches , same email
128 zipcode => $customer->zipcode,
131 my $customers = SL::DB::Manager::Customer->get_all();
133 $fuzzy_customers = $shop_order->check_for_existing_customers;
135 is(scalar @{ $fuzzy_customers }, 4, 'found 4 matching customers (zipcode equal + street similar + same email) trgm_pg is installed');
137 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');
140 is($shop->description , 'testshop' , 'shop description ok');
141 is($shop_order->shop_id , $shop->id , "shop_id ok");
143 note('testing convert_to_sales_order');
144 my $order = $shop_order->convert_to_sales_order(employee => $employee, customer => $customer);
145 $order->calculate_prices_and_taxes;
148 is(ref($order), 'SL::DB::Order', 'order ok');
149 is($order->amount, 59.5, 'order amount ok');
150 is($order->netamount, 50, 'order netamount ok');
152 test_transfer( import_id => $shop_order->id , customer => $customer->id );
161 "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(OrderItem Order);
162 "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(ShopPart Part ShopOrderItem ShopOrder Shop Customer);