sub check_for_existing_customers {
my ($self, %params) = @_;
-
- my $name = $self->billing_lastname ne '' ? $self->billing_firstname . " " . $self->billing_lastname : '';
- my $lastname = $self->billing_lastname ne '' ? "%" . $self->billing_lastname . "%" : '';
- my $company = $self->billing_company ne '' ? "%" . $self->billing_company . "%" : '';
- my $street = $self->billing_street ne '' ? $self->billing_street : '';
-
- # Fuzzysearch for street to find e.g. "Dorfstrasse - Dorfstr. - Dorfstraße"
- my $fs_query = <<SQL;
+ my $customers;
+
+ my $name = $self->billing_lastname ne '' ? $self->billing_firstname . " " . $self->billing_lastname : '';
+ my $lastname = $self->billing_lastname ne '' ? "%" . $self->billing_lastname . "%" : '';
+ my $company = $self->billing_company ne '' ? "%" . $self->billing_company . "%" : '';
+ my $street = $self->billing_street ne '' ? $self->billing_street : '';
+ my $street_not_fuzzy = $self->billing_street ne '' ? "%" . $self->billing_street . "%" : '';
+ my $zipcode = $self->billing_street ne '' ? $self->billing_zipcode : '';
+ my $email = $self->billing_street ne '' ? $self->billing_email : '';
+
+ if($self->check_trgm) {
+ # Fuzzysearch for street to find e.g. "Dorfstrasse - Dorfstr. - Dorfstraße"
+ my $fs_query = <<SQL;
SELECT *
FROM customer
WHERE (
) AND obsolete = 'F'
SQL
- my @values = ($lastname, $company, $self->billing_zipcode, $street, $self->billing_zipcode, $self->billing_email);
+ my @values = ($lastname, $company, $self->billing_zipcode, $street, $self->billing_zipcode, $self->billing_email);
- my $customers = SL::DB::Manager::Customer->get_objects_from_sql(
- sql => $fs_query,
- args => \@values,
- );
+ $customers = SL::DB::Manager::Customer->get_objects_from_sql(
+ sql => $fs_query,
+ args => \@values,
+ );
+ }else{
+ # If trgm extension is not installed
+ $customers = SL::DB::Manager::Customer->get_all(
+ where => [
+ or => [
+ and => [
+ or => [ 'name' => { ilike => $lastname },
+ 'name' => { ilike => $company },
+ ],
+ 'zipcode' => { ilike => $zipcode },
+ ],
+ and => [
+ and => [ 'street' => { ilike => $street_not_fuzzy },
+ 'zipcode' => { ilike => $zipcode },
+ ],
+ ],
+ or => [ 'email' => { ilike => $email } ],
+ ],
+ ],
+ );
+ }
return $customers;
}
return $result || ($self->id <=> $other->id);
}
+sub check_trgm {
+ my ( $self ) = @_;
+
+ my $dbh = $::form->get_standard_dbh();
+ my $sql = "SELECT installed_version FROM pg_available_extensions WHERE name = 'pg_trgm'";
+ my @version = selectall_hashref_query($::form, $dbh, $sql);
+
+ return 1 if($version[0]->{installed_version});
+ return 0;
+}
+
1;
__END__
=item C<compare_to>
+=item C<check_trgm>
+
+Checks if the postgresextension pg_trgm is installed and return 0 or 1.
+
=back
=head1 TODO
reset_state();
+my $trgm = SL::DB::ShopOrder->check_trgm;
+
my $shop_trans_id = 1;
$shop_order = new_shop_order(
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(
$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");