Auftrags-Controller: Preisquellen bei Erneuern von Positionen aus Stammdaten …
[kivitendo-erp.git] / t / shop / shop_order.t
index 853322d..e40ec2f 100644 (file)
@@ -5,6 +5,7 @@ use lib 't';
 use Support::TestSetup;
 use Carp;
 use Test::Exception;
+use SL::DBUtils qw(check_trgm);
 use SL::Dev::ALL;
 use SL::Dev::Part qw(new_part);
 use SL::Dev::Shop qw(new_shop new_shop_part new_shop_order);
@@ -16,12 +17,16 @@ use SL::Controller::ShopOrder;
 use Data::Dumper;
 
 my ($shop, $shop_order, $shop_part, $part, $customer, $employee);
+my ($transdate);
 
 sub reset_state {
   my %params = @_;
 
   clear_up();
 
+  $transdate = DateTime->today_local;
+  $transdate->set_year(2019) if $transdate->year == 2020; # use year 2019 in 2020, because of tax rate change in Germany
+
   $shop = new_shop->save;
   $part = new_part->save;
   $shop_part = new_shop_part(part => $part, shop => $shop)->save;
@@ -35,13 +40,13 @@ sub reset_state {
                           )->save;
 }
 
-sub save_shorcontroller_to_string {
+sub save_shopcontroller_to_string {
 
   my $output;
-  open(my $outputFH, '<', \$output) or die "OUTPUT";
+  open(my $outputFH, '>', \$output) or die "OUTPUT";
   my $oldFH = select $outputFH;
-  my $shor_controller = SL::Controller::ShopOrder->new;
-  $shor_controller->action_transfer;
+  my $shop_controller = SL::Controller::ShopOrder->new;
+  $shop_controller->action_transfer;
 
   select $oldFH;
   close $outputFH;
@@ -53,7 +58,7 @@ sub test_transfer {
   $::form->{import_id} = $params{import_id};
   $::form->{customer} =  $params{customer};
   my $test_name = 'Test Controller Action Transfer';
-  save_shorcontroller_to_string();
+  save_shopcontroller_to_string();
   my @links_record = RecordLinks->get_links( 'from_table' => 'shop_orders',
                                             'from_id'    => $params{import_id},
                                             'to_table'   => 'oe',
@@ -67,18 +72,22 @@ Support::TestSetup::login();
 
 reset_state();
 
+my $trgm = check_trgm($::form->get_standard_dbh());
+
 my $shop_trans_id = 1;
 
 $shop_order = new_shop_order(
   shop              => $shop,
+  transfer_date     => $transdate,
   shop_trans_id     => $shop_trans_id,
+  order_date        => $transdate->datetime,
   amount            => 59.5,
   billing_lastname  => 'Schmidt',
   billing_firstname => 'Sven',
   billing_company   => 'Evil Inc',
-  billing_street    => 'Evil Street',
+  billing_street    => 'Evil Street 666',
   billing_zipcode   => $customer->zipcode,
-  billing_email     => $customer->email,
+  billing_email     => 'email',
 );
 
 my $shop_order_item = SL::DB::ShopOrderItem->new(
@@ -107,21 +116,39 @@ my $customer_different = new_customer(
 $fuzzy_customers = $shop_order->check_for_existing_customers;
 is(scalar @{ $fuzzy_customers }, 1, 'still only found 1 matching customer (zipcode equal + street dissimilar');
 
-note('adding a similar customer');
+note('adding 2 similar customers and 1 dissimilar but same email');
 my $customer_similar = new_customer(
   name    => "Different Name",
-  street  => 'Good Street', # difference not large enough from "Evil Street", street matches
+  street  => 'Evil Street 666', # difference not large enough from "Evil Street", street matches
   zipcode => $customer->zipcode,
   email   => "foo",
 )->save;
+my $customer_similar_2 = new_customer(
+  name    => "Different Name",
+  street  => 'Evil Straet', # difference not large enough from "Evil Street", street matches
+  zipcode => $customer->zipcode,
+  email   => "foofoo",
+)->save;
+my $customer_same_email = new_customer(
+  name    => "Different Name",
+  street  => 'Angel Way', # difference large enough from "Evil Street", street not matches , same email
+  zipcode => $customer->zipcode,
+  email   => 'email',
+)->save;
+my $customers = SL::DB::Manager::Customer->get_all();
+
 $fuzzy_customers = $shop_order->check_for_existing_customers;
-is(scalar @{ $fuzzy_customers }, 2, 'found 2 matching customers (zipcode equal + street similar)');
+if($trgm){
+  is(scalar @{ $fuzzy_customers }, 4, 'found 4 matching customers (zipcode equal + street similar + same email) trgm_pg is installed');
+}else{
+  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');
+}
 
 is($shop->description   , 'testshop' , 'shop description ok');
 is($shop_order->shop_id , $shop->id  , "shop_id ok");
 
 note('testing convert_to_sales_order');
-my $order = $shop_order->convert_to_sales_order(employee => $employee, customer => $customer);
+my $order = $shop_order->convert_to_sales_order(employee => $employee, customer => $customer, transdate => $shop_order->order_date);
 $order->calculate_prices_and_taxes;
 $order->save;