5 use Support::TestSetup;
 
   8 use SL::DBUtils qw(check_trgm);
 
  10 use SL::Dev::Part qw(new_part);
 
  11 use SL::Dev::Shop qw(new_shop new_shop_part new_shop_order);
 
  12 use SL::Dev::CustomerVendor qw(new_customer);
 
  14 use SL::DB::ShopOrder;
 
  15 use SL::DB::ShopOrderItem;
 
  16 use SL::Controller::ShopOrder;
 
  19 my ($shop, $shop_order, $shop_part, $part, $customer, $employee);
 
  27   $transdate = DateTime->today_local;
 
  28   $transdate->set_year(2019) if $transdate->year == 2020; # use year 2019 in 2020, because of tax rate change in Germany
 
  30   $shop = new_shop->save;
 
  31   $part = new_part->save;
 
  32   $shop_part = new_shop_part(part => $part, shop => $shop)->save;
 
  34   $employee = SL::DB::Manager::Employee->current || croak "No employee";
 
  36   $customer = new_customer( name    => 'Evil Inc',
 
  37                             street  => 'Evil Street',
 
  39                             email   => 'evil@evilinc.com'
 
  43 sub save_shopcontroller_to_string {
 
  46   open(my $outputFH, '>', \$output) or die "OUTPUT";
 
  47   my $oldFH = select $outputFH;
 
  48   my $shop_controller = SL::Controller::ShopOrder->new;
 
  49   $shop_controller->action_transfer;
 
  57   $::form = Support::TestSetup->create_new_form;
 
  58   $::form->{import_id} = $params{import_id};
 
  59   $::form->{customer} =  $params{customer};
 
  60   my $test_name = 'Test Controller Action Transfer';
 
  61   save_shopcontroller_to_string();
 
  62   my @links_record = RecordLinks->get_links( 'from_table' => 'shop_orders',
 
  63                                             'from_id'    => $params{import_id},
 
  66   is($links_record[0]->{from_id}    , $params{import_id}, "record from id check");
 
  67   is($links_record[0]->{from_table} , 'shop_orders'     , "record from table <shop_orders> check");
 
  68   is($links_record[0]->{to_table}   , 'oe'              , "record to table <oe> check");
 
  71 Support::TestSetup::login();
 
  75 my $trgm = check_trgm($::form->get_standard_dbh());
 
  77 my $shop_trans_id = 1;
 
  79 $shop_order = new_shop_order(
 
  81   transfer_date     => $transdate,
 
  82   shop_trans_id     => $shop_trans_id,
 
  83   order_date        => $transdate->datetime,
 
  85   billing_lastname  => 'Schmidt',
 
  86   billing_firstname => 'Sven',
 
  87   billing_company   => 'Evil Inc',
 
  88   billing_street    => 'Evil Street 666',
 
  89   billing_zipcode   => $customer->zipcode,
 
  90   billing_email     => 'email',
 
  93 my $shop_order_item = SL::DB::ShopOrderItem->new(
 
  94   partnumber    => $part->partnumber,
 
  98   shop_trans_id => $shop_trans_id,
 
 100 $shop_order->shop_order_items( [ $shop_order_item ] );
 
 103 note('testing check_for_existing_customers');
 
 104 my $fuzzy_customers = $shop_order->check_for_existing_customers;
 
 106 is(scalar @{ $fuzzy_customers }, 1, 'found 1 matching customer');
 
 107 is($fuzzy_customers->[0]->name, 'Evil Inc', 'matched customer Evil Inc');
 
 109 note('adding a not-so-similar customer');
 
 110 my $customer_different = new_customer(
 
 111   name    => "Different Name",
 
 112   street  => 'Good Straet', # difference large enough from "Evil Street"
 
 113   zipcode => $customer->zipcode,
 
 116 $fuzzy_customers = $shop_order->check_for_existing_customers;
 
 117 is(scalar @{ $fuzzy_customers }, 1, 'still only found 1 matching customer (zipcode equal + street dissimilar');
 
 119 note('adding 2 similar customers and 1 dissimilar but same email');
 
 120 my $customer_similar = new_customer(
 
 121   name    => "Different Name",
 
 122   street  => 'Evil Street 666', # difference not large enough from "Evil Street", street matches
 
 123   zipcode => $customer->zipcode,
 
 126 my $customer_similar_2 = new_customer(
 
 127   name    => "Different Name",
 
 128   street  => 'Evil Straet', # difference not large enough from "Evil Street", street matches
 
 129   zipcode => $customer->zipcode,
 
 132 my $customer_same_email = new_customer(
 
 133   name    => "Different Name",
 
 134   street  => 'Angel Way', # difference large enough from "Evil Street", street not matches , same email
 
 135   zipcode => $customer->zipcode,
 
 138 my $customers = SL::DB::Manager::Customer->get_all();
 
 140 $fuzzy_customers = $shop_order->check_for_existing_customers;
 
 142   is(scalar @{ $fuzzy_customers }, 4, 'found 4 matching customers (zipcode equal + street similar + same email) trgm_pg is installed');
 
 144   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');
 
 147 is($shop->description   , 'testshop' , 'shop description ok');
 
 148 is($shop_order->shop_id , $shop->id  , "shop_id ok");
 
 150 note('testing convert_to_sales_order');
 
 151 my $order = $shop_order->convert_to_sales_order(employee => $employee, customer => $customer, transdate => $shop_order->order_date);
 
 152 $order->calculate_prices_and_taxes;
 
 155 is(ref($order), 'SL::DB::Order', 'order ok');
 
 156 is($order->amount,    59.5, 'order amount ok');
 
 157 is($order->netamount, 50,   'order netamount ok');
 
 159 test_transfer( import_id => $shop_order->id , customer => $customer->id );
 
 168   "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(OrderItem Order);
 
 169   "SL::DB::Manager::${_}"->delete_all(all => 1) for qw(ShopPart Part ShopOrderItem ShopOrder Shop Customer);