Revision c0b5a026
Von Werner Hahn vor mehr als 7 Jahren hinzugefügt
SL/DB/ShopOrder.pm | ||
---|---|---|
111 | 111 |
|
112 | 112 |
sub check_for_existing_customers { |
113 | 113 |
my ($self, %params) = @_; |
114 |
|
|
115 |
my $name = $self->billing_lastname ne '' ? $self->billing_firstname . " " . $self->billing_lastname : ''; |
|
116 |
my $lastname = $self->billing_lastname ne '' ? "%" . $self->billing_lastname . "%" : ''; |
|
117 |
my $company = $self->billing_company ne '' ? "%" . $self->billing_company . "%" : ''; |
|
118 |
my $street = $self->billing_street ne '' ? $self->billing_street : ''; |
|
119 |
|
|
120 |
# Fuzzysearch for street to find e.g. "Dorfstrasse - Dorfstr. - Dorfstraße" |
|
121 |
my $fs_query = <<SQL; |
|
114 |
my $customers; |
|
115 |
|
|
116 |
my $name = $self->billing_lastname ne '' ? $self->billing_firstname . " " . $self->billing_lastname : ''; |
|
117 |
my $lastname = $self->billing_lastname ne '' ? "%" . $self->billing_lastname . "%" : ''; |
|
118 |
my $company = $self->billing_company ne '' ? "%" . $self->billing_company . "%" : ''; |
|
119 |
my $street = $self->billing_street ne '' ? $self->billing_street : ''; |
|
120 |
my $street_not_fuzzy = $self->billing_street ne '' ? "%" . $self->billing_street . "%" : ''; |
|
121 |
my $zipcode = $self->billing_street ne '' ? $self->billing_zipcode : ''; |
|
122 |
my $email = $self->billing_street ne '' ? $self->billing_email : ''; |
|
123 |
|
|
124 |
if($self->check_trgm) { |
|
125 |
# Fuzzysearch for street to find e.g. "Dorfstrasse - Dorfstr. - Dorfstraße" |
|
126 |
my $fs_query = <<SQL; |
|
122 | 127 |
SELECT * |
123 | 128 |
FROM customer |
124 | 129 |
WHERE ( |
... | ... | |
134 | 139 |
) AND obsolete = 'F' |
135 | 140 |
SQL |
136 | 141 |
|
137 |
my @values = ($lastname, $company, $self->billing_zipcode, $street, $self->billing_zipcode, $self->billing_email); |
|
142 |
my @values = ($lastname, $company, $self->billing_zipcode, $street, $self->billing_zipcode, $self->billing_email);
|
|
138 | 143 |
|
139 |
my $customers = SL::DB::Manager::Customer->get_objects_from_sql( |
|
140 |
sql => $fs_query, |
|
141 |
args => \@values, |
|
142 |
); |
|
144 |
$customers = SL::DB::Manager::Customer->get_objects_from_sql( |
|
145 |
sql => $fs_query, |
|
146 |
args => \@values, |
|
147 |
); |
|
148 |
}else{ |
|
149 |
# If trgm extension is not installed |
|
150 |
$customers = SL::DB::Manager::Customer->get_all( |
|
151 |
where => [ |
|
152 |
or => [ |
|
153 |
and => [ |
|
154 |
or => [ 'name' => { ilike => $lastname }, |
|
155 |
'name' => { ilike => $company }, |
|
156 |
], |
|
157 |
'zipcode' => { ilike => $zipcode }, |
|
158 |
], |
|
159 |
and => [ |
|
160 |
and => [ 'street' => { ilike => $street_not_fuzzy }, |
|
161 |
'zipcode' => { ilike => $zipcode }, |
|
162 |
], |
|
163 |
], |
|
164 |
or => [ 'email' => { ilike => $email } ], |
|
165 |
], |
|
166 |
], |
|
167 |
); |
|
168 |
} |
|
143 | 169 |
|
144 | 170 |
return $customers; |
145 | 171 |
} |
... | ... | |
208 | 234 |
return $result || ($self->id <=> $other->id); |
209 | 235 |
} |
210 | 236 |
|
237 |
sub check_trgm { |
|
238 |
my ( $self ) = @_; |
|
239 |
|
|
240 |
my $dbh = $::form->get_standard_dbh(); |
|
241 |
my $sql = "SELECT installed_version FROM pg_available_extensions WHERE name = 'pg_trgm'"; |
|
242 |
my @version = selectall_hashref_query($::form, $dbh, $sql); |
|
243 |
|
|
244 |
return 1 if($version[0]->{installed_version}); |
|
245 |
return 0; |
|
246 |
} |
|
247 |
|
|
211 | 248 |
1; |
212 | 249 |
|
213 | 250 |
__END__ |
... | ... | |
244 | 281 |
|
245 | 282 |
=item C<compare_to> |
246 | 283 |
|
284 |
=item C<check_trgm> |
|
285 |
|
|
286 |
Checks if the postgresextension pg_trgm is installed and return 0 or 1. |
|
287 |
|
|
247 | 288 |
=back |
248 | 289 |
|
249 | 290 |
=head1 TODO |
t/shop/shop_order.t | ||
---|---|---|
67 | 67 |
|
68 | 68 |
reset_state(); |
69 | 69 |
|
70 |
my $trgm = SL::DB::ShopOrder->check_trgm; |
|
71 |
|
|
70 | 72 |
my $shop_trans_id = 1; |
71 | 73 |
|
72 | 74 |
$shop_order = new_shop_order( |
... | ... | |
76 | 78 |
billing_lastname => 'Schmidt', |
77 | 79 |
billing_firstname => 'Sven', |
78 | 80 |
billing_company => 'Evil Inc', |
79 |
billing_street => 'Evil Street', |
|
81 |
billing_street => 'Evil Street 666',
|
|
80 | 82 |
billing_zipcode => $customer->zipcode, |
81 |
billing_email => $customer->email,
|
|
83 |
billing_email => 'email',
|
|
82 | 84 |
); |
83 | 85 |
|
84 | 86 |
my $shop_order_item = SL::DB::ShopOrderItem->new( |
... | ... | |
107 | 109 |
$fuzzy_customers = $shop_order->check_for_existing_customers; |
108 | 110 |
is(scalar @{ $fuzzy_customers }, 1, 'still only found 1 matching customer (zipcode equal + street dissimilar'); |
109 | 111 |
|
110 |
note('adding a similar customer');
|
|
112 |
note('adding 2 similar customers and 1 dissimilar but same email');
|
|
111 | 113 |
my $customer_similar = new_customer( |
112 | 114 |
name => "Different Name", |
113 |
street => 'Good Street', # difference not large enough from "Evil Street", street matches
|
|
115 |
street => 'Evil Street 666', # difference not large enough from "Evil Street", street matches
|
|
114 | 116 |
zipcode => $customer->zipcode, |
115 | 117 |
email => "foo", |
116 | 118 |
)->save; |
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, |
|
123 |
email => "foofoo", |
|
124 |
)->save; |
|
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, |
|
129 |
email => 'email', |
|
130 |
)->save; |
|
131 |
my $customers = SL::DB::Manager::Customer->get_all(); |
|
132 |
|
|
117 | 133 |
$fuzzy_customers = $shop_order->check_for_existing_customers; |
118 |
is(scalar @{ $fuzzy_customers }, 2, 'found 2 matching customers (zipcode equal + street similar)'); |
|
134 |
if($trgm){ |
|
135 |
is(scalar @{ $fuzzy_customers }, 4, 'found 4 matching customers (zipcode equal + street similar + same email) trgm_pg is installed'); |
|
136 |
}else{ |
|
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'); |
|
138 |
} |
|
119 | 139 |
|
120 | 140 |
is($shop->description , 'testshop' , 'shop description ok'); |
121 | 141 |
is($shop_order->shop_id , $shop->id , "shop_id ok"); |
Auch abrufbar als: Unified diff
WebshopApi: Trigram Abhängigkeit aufgelöst.
Wird nur genutzt, wenn auch installiert
Tests dementsprechend angepasst und erweitert