Revision e66c12b7
Von Werner Hahn vor mehr als 6 Jahren hinzugefügt
SL/ShopConnector/Shopware.pm | ||
---|---|---|
11 | 11 |
use SL::DB::ShopOrder; |
12 | 12 |
use SL::DB::ShopOrderItem; |
13 | 13 |
use SL::DB::History; |
14 |
use DateTime::Format::Strptime; |
|
14 | 15 |
use SL::DB::File; |
15 | 16 |
use Data::Dumper; |
16 | 17 |
use Sort::Naturally (); |
... | ... | |
23 | 24 |
'scalar --get_set_init' => [ qw(connector url) ], |
24 | 25 |
); |
25 | 26 |
|
27 |
sub get_order_max { |
|
28 |
my ($self) = @_; |
|
29 |
my %params = ( sort => { property => 'number', |
|
30 |
direction => 'DESC', |
|
31 |
}, |
|
32 |
); |
|
33 |
my $url = $self->url; |
|
34 |
my $data = $self->connector->get($url . "api/orders?limit=1", %params); |
|
35 |
my $data_json = $data->content; |
|
36 |
$main::lxdebug->dump(0, 'WH:MAX1 ', $data->content); |
|
37 |
|
|
38 |
my $import = SL::JSON::decode_json($data_json); |
|
39 |
$main::lxdebug->dump(0, 'WH:MAX ', $import->{data}); |
|
40 |
$main::lxdebug->dump(0, 'WH:MAX3 ', $import->{data}->{number}); |
|
41 |
|
|
42 |
} |
|
43 |
|
|
26 | 44 |
sub get_new_orders { |
27 | 45 |
my ($self, $id) = @_; |
28 | 46 |
|
29 | 47 |
my $url = $self->url; |
30 | 48 |
my $ordnumber = $self->config->last_order_number + 1; |
31 | 49 |
my $otf = $self->config->orders_to_fetch; |
32 |
|
|
33 |
my $i;
|
|
34 |
for ($i=1;$i<=$otf;$i++) {
|
|
50 |
my $of = 0; |
|
51 |
$self->get_order_max;
|
|
52 |
for(1 .. $otf) {
|
|
35 | 53 |
|
36 | 54 |
my $data = $self->connector->get($url . "api/orders/$ordnumber?useNumberAsId=true"); |
37 | 55 |
my $data_json = $data->content; |
38 | 56 |
my $import = SL::JSON::decode_json($data_json); |
57 |
|
|
39 | 58 |
if ($import->{success}){ |
59 |
my $shop_order = $self->map_data_to_shoporder($import); |
|
40 | 60 |
|
41 |
# Mapping to table shoporders. See http://community.shopware.com/_detail_1690.html#GET_.28Liste.29 |
|
42 |
my %columns = ( |
|
43 |
amount => $import->{data}->{invoiceAmount}, |
|
44 |
billing_city => $import->{data}->{billing}->{city}, |
|
45 |
billing_company => $import->{data}->{billing}->{company}, |
|
46 |
billing_country => $import->{data}->{billing}->{country}->{name}, |
|
47 |
billing_department => $import->{data}->{billing}->{department}, |
|
48 |
billing_email => $import->{data}->{customer}->{email}, |
|
49 |
billing_fax => $import->{data}->{billing}->{fax}, |
|
50 |
billing_firstname => $import->{data}->{billing}->{firstName}, |
|
51 |
#billing_greeting => ($import->{data}->{billing}->{salutation} eq 'mr' ? 'Herr' : 'Frau'), |
|
52 |
billing_lastname => $import->{data}->{billing}->{lastName}, |
|
53 |
billing_phone => $import->{data}->{billing}->{phone}, |
|
54 |
billing_street => $import->{data}->{billing}->{street}, |
|
55 |
billing_vat => $import->{data}->{billing}->{vatId}, |
|
56 |
billing_zipcode => $import->{data}->{billing}->{zipCode}, |
|
57 |
customer_city => $import->{data}->{billing}->{city}, |
|
58 |
customer_company => $import->{data}->{billing}->{company}, |
|
59 |
customer_country => $import->{data}->{billing}->{country}->{name}, |
|
60 |
customer_department => $import->{data}->{billing}->{department}, |
|
61 |
customer_email => $import->{data}->{customer}->{email}, |
|
62 |
customer_fax => $import->{data}->{billing}->{fax}, |
|
63 |
customer_firstname => $import->{data}->{billing}->{firstName}, |
|
64 |
#customer_greeting => ($import->{data}->{billing}->{salutation} eq 'mr' ? 'Herr' : 'Frau'), |
|
65 |
customer_lastname => $import->{data}->{billing}->{lastName}, |
|
66 |
customer_phone => $import->{data}->{billing}->{phone}, |
|
67 |
customer_street => $import->{data}->{billing}->{street}, |
|
68 |
customer_vat => $import->{data}->{billing}->{vatId}, |
|
69 |
customer_zipcode => $import->{data}->{billing}->{zipCode}, |
|
70 |
customer_newsletter => $import->{data}->{customer}->{newsletter}, |
|
71 |
delivery_city => $import->{data}->{shipping}->{city}, |
|
72 |
delivery_company => $import->{data}->{shipping}->{company}, |
|
73 |
delivery_country => $import->{data}->{shipping}->{country}->{name}, |
|
74 |
delivery_department => $import->{data}->{shipping}->{department}, |
|
75 |
delivery_email => "", |
|
76 |
delivery_fax => $import->{data}->{shipping}->{fax}, |
|
77 |
delivery_firstname => $import->{data}->{shipping}->{firstName}, |
|
78 |
#delivery_greeting => ($import->{data}->{shipping}->{salutation} eq 'mr' ? 'Herr' : 'Frau'), |
|
79 |
delivery_lastname => $import->{data}->{shipping}->{lastName}, |
|
80 |
delivery_phone => $import->{data}->{shipping}->{phone}, |
|
81 |
delivery_street => $import->{data}->{shipping}->{street}, |
|
82 |
delivery_vat => $import->{data}->{shipping}->{vatId}, |
|
83 |
delivery_zipcode => $import->{data}->{shipping}->{zipCode}, |
|
84 |
host => $import->{data}->{shop}->{hosts}, |
|
85 |
netamount => $import->{data}->{invoiceAmountNet}, |
|
86 |
order_date => $import->{data}->{orderTime}, |
|
87 |
payment_description => $import->{data}->{payment}->{description}, |
|
88 |
payment_id => $import->{data}->{paymentId}, |
|
89 |
remote_ip => $import->{data}->{remoteAddress}, |
|
90 |
sepa_account_holder => $import->{data}->{paymentIntances}->{accountHolder}, |
|
91 |
sepa_bic => $import->{data}->{paymentIntances}->{bic}, |
|
92 |
sepa_iban => $import->{data}->{paymentIntances}->{iban}, |
|
93 |
shipping_costs => $import->{data}->{invoiceShipping}, |
|
94 |
shipping_costs_net => $import->{data}->{invoiceShippingNet}, |
|
95 |
shop_c_billing_id => $import->{data}->{billing}->{customerId}, |
|
96 |
shop_c_billing_number => $import->{data}->{billing}->{number}, |
|
97 |
shop_c_delivery_id => $import->{data}->{shipping}->{id}, |
|
98 |
shop_customer_id => $import->{data}->{customerId}, |
|
99 |
shop_customer_number => $import->{data}->{billing}->{number}, |
|
100 |
shop_customer_comment => $import->{data}->{customerComment}, |
|
101 |
shop_data => "", |
|
102 |
shop_id => $self->config->id, |
|
103 |
shop_ordernumber => $import->{data}->{number}, |
|
104 |
shop_trans_id => $import->{data}->{id}, |
|
105 |
tax_included => $self->config->pricetype eq "brutto" ? 1 : 0, |
|
106 |
); |
|
107 |
my $shop_order = SL::DB::ShopOrder->new(%columns); |
|
108 | 61 |
$shop_order->save; |
62 |
$of ++; |
|
109 | 63 |
my $id = $shop_order->id; |
110 | 64 |
|
111 | 65 |
my @positions = sort { Sort::Naturally::ncmp($a->{"partnumber"}, $b->{"partnumber"}) } @{ $import->{data}->{details} }; |
... | ... | |
128 | 82 |
$position++; |
129 | 83 |
} |
130 | 84 |
$shop_order->{positions} = $position-1; |
131 |
|
|
132 |
# Only Customers which are not found will be applied |
|
133 |
my $name = $shop_order->billing_lastname ne '' ? $shop_order->billing_firstname . " " . $shop_order->billing_lastname : ''; |
|
134 |
my $lastname = $shop_order->billing_lastname ne '' ? "%" . $shop_order->billing_lastname . "%" : ''; |
|
135 |
my $company = $shop_order->billing_company ne '' ? "%" . $shop_order->billing_company . "%" : ''; |
|
136 |
my $street = $shop_order->billing_street ne '' ? $shop_order->billing_street : ''; |
|
137 |
# Fuzzysearch for street to find e.g. "Dorfstrasse - Dorfstr. - Dorfstraße" |
|
138 |
my $dbh = $::form->get_standard_dbh(); |
|
139 |
my $fs_query = "SELECT id FROM customer WHERE ( ( ( name ILIKE ? |
|
140 |
OR name ILIKE ? |
|
141 |
) |
|
142 |
AND zipcode ILIKE ? |
|
143 |
) |
|
144 |
OR ( street % ? |
|
145 |
AND zipcode ILIKE ? |
|
146 |
) |
|
147 |
OR email ILIKE ? |
|
148 |
)"; |
|
149 |
my @values = ($lastname, $company, $shop_order->billing_zipcode, $street, $shop_order->billing_zipcode, $shop_order->billing_email); |
|
150 |
my @c_ids = selectall_array_query($::form, $dbh, $fs_query, @values); |
|
151 |
|
|
152 |
if(!scalar(@c_ids)){ |
|
153 |
|
|
154 |
my %address = ( 'name' => $shop_order->billing_firstname . " " . $shop_order->billing_lastname, |
|
155 |
'department_1' => $shop_order->billing_company, |
|
156 |
'department_2' => $shop_order->billing_department, |
|
157 |
'street' => $shop_order->billing_street, |
|
158 |
'zipcode' => $shop_order->billing_zipcode, |
|
159 |
'city' => $shop_order->billing_city, |
|
160 |
'email' => $shop_order->billing_email, |
|
161 |
'country' => $shop_order->billing_country, |
|
162 |
'greeting' => $shop_order->billing_greeting, |
|
163 |
'contact' => ($shop_order->billing_greeting eq "Herr" ? "Sehr geehrter Herr $lastname" : "Sehr geehrte Frau $lastname"), |
|
164 |
'fax' => $shop_order->billing_fax, |
|
165 |
'phone' => $shop_order->billing_phone, |
|
166 |
'ustid' => $shop_order->billing_vat, |
|
167 |
'taxincluded_checked' => $self->config->pricetype eq "brutto" ? 1 : 0, |
|
168 |
'taxincluded' => $self->config->pricetype eq "brutto" ? 1 : 0, |
|
169 |
'pricegroup_id' => (split '\/',$self->config->price_source)[0] eq "pricegroup" ? (split '\/',$self->config->price_source)[1] : undef, |
|
170 |
'taxzone_id' => $self->config->taxzone_id, |
|
171 |
'currency' => 1, # TODO hardcoded |
|
172 |
#'payment_id' => 7345,# TODO hardcoded |
|
173 |
); |
|
174 |
my $customer = SL::DB::Customer->new(%address); |
|
175 |
|
|
176 |
$customer->save; |
|
177 |
my $snumbers = "customernumber_" . $customer->customernumber; |
|
178 |
SL::DB::History->new( |
|
179 |
trans_id => $customer->id, |
|
180 |
snumbers => $snumbers, |
|
181 |
employee_id => SL::DB::Manager::Employee->current->id, |
|
182 |
addition => 'SAVED', |
|
183 |
what_done => 'Shopimport', |
|
184 |
)->save(); |
|
185 |
$shop_order->{kivi_customer_id} = $customer->id; |
|
85 |
my $customer = $shop_order->get_customer; |
|
86 |
if(ref($customer) eq "SL::DB::Customer"){ |
|
87 |
$shop_order->kivi_customer_id($customer->id); |
|
186 | 88 |
$shop_order->save; |
187 |
|
|
188 |
}elsif(scalar(@c_ids) == 1){ |
|
189 |
my $customer = SL::DB::Manager::Customer->get_first( query => [ |
|
190 |
id => $c_ids[0], |
|
191 |
email => $shop_order->billing_email, |
|
192 |
street => $shop_order->billing_street, |
|
193 |
zipcode => $shop_order->billing_zipcode, |
|
194 |
city => $shop_order->billing_city, |
|
195 |
name => $name, |
|
196 |
]); |
|
197 |
|
|
198 |
if(ref $customer){ |
|
199 |
$shop_order->{kivi_customer_id} = $customer->id; |
|
200 |
$shop_order->save; |
|
201 |
} |
|
202 |
}else{ |
|
203 |
my $customer = SL::DB::Manager::Customer->get_first( query => [ #or => [id => \@c_ids ], |
|
204 |
name => $name, |
|
205 |
street => $shop_order->billing_street, |
|
206 |
zipcode => $shop_order->billing_zipcode, |
|
207 |
email => $shop_order->billing_email, |
|
208 |
] |
|
209 |
); |
|
210 |
|
|
211 |
if(ref $customer){ |
|
212 |
$shop_order->{kivi_customer_id} = $customer->id; |
|
213 |
$shop_order->save; |
|
214 |
} |
|
215 | 89 |
} |
216 | 90 |
|
217 | 91 |
my $attributes->{last_order_number} = $ordnumber; |
... | ... | |
221 | 95 |
} |
222 | 96 |
} |
223 | 97 |
my $shop = $self->config->description; |
224 |
my @fetched_orders = ($shop,$i); |
|
225 |
return \@fetched_orders; |
|
226 |
}; |
|
98 |
my %fetched_orders = (shop_id => $self->config->id, number_of_orders => $of); |
|
99 |
return \%fetched_orders; |
|
100 |
} |
|
101 |
|
|
102 |
sub map_data_to_shoporder { |
|
103 |
my ($self, $import) = @_; |
|
104 |
my $parser = DateTime::Format::Strptime->new( pattern => '%Y-%m-%dT%H:%M:%S', |
|
105 |
locale => 'de_DE', |
|
106 |
time_zone => 'local' |
|
107 |
); |
|
108 |
my $orderdate = $parser->parse_datetime($import->{data}->{orderTime}); |
|
109 |
# Mapping to table shoporders. See http://community.shopware.com/_detail_1690.html#GET_.28Liste.29 |
|
110 |
my %columns = ( |
|
111 |
amount => $import->{data}->{invoiceAmount}, |
|
112 |
billing_city => $import->{data}->{billing}->{city}, |
|
113 |
billing_company => $import->{data}->{billing}->{company}, |
|
114 |
billing_country => $import->{data}->{billing}->{country}->{name}, |
|
115 |
billing_department => $import->{data}->{billing}->{department}, |
|
116 |
billing_email => $import->{data}->{customer}->{email}, |
|
117 |
billing_fax => $import->{data}->{billing}->{fax}, |
|
118 |
billing_firstname => $import->{data}->{billing}->{firstName}, |
|
119 |
#billing_greeting => ($import->{data}->{billing}->{salutation} eq 'mr' ? 'Herr' : 'Frau'), |
|
120 |
billing_lastname => $import->{data}->{billing}->{lastName}, |
|
121 |
billing_phone => $import->{data}->{billing}->{phone}, |
|
122 |
billing_street => $import->{data}->{billing}->{street}, |
|
123 |
billing_vat => $import->{data}->{billing}->{vatId}, |
|
124 |
billing_zipcode => $import->{data}->{billing}->{zipCode}, |
|
125 |
customer_city => $import->{data}->{billing}->{city}, |
|
126 |
customer_company => $import->{data}->{billing}->{company}, |
|
127 |
customer_country => $import->{data}->{billing}->{country}->{name}, |
|
128 |
customer_department => $import->{data}->{billing}->{department}, |
|
129 |
customer_email => $import->{data}->{customer}->{email}, |
|
130 |
customer_fax => $import->{data}->{billing}->{fax}, |
|
131 |
customer_firstname => $import->{data}->{billing}->{firstName}, |
|
132 |
#customer_greeting => ($import->{data}->{billing}->{salutation} eq 'mr' ? 'Herr' : 'Frau'), |
|
133 |
customer_lastname => $import->{data}->{billing}->{lastName}, |
|
134 |
customer_phone => $import->{data}->{billing}->{phone}, |
|
135 |
customer_street => $import->{data}->{billing}->{street}, |
|
136 |
customer_vat => $import->{data}->{billing}->{vatId}, |
|
137 |
customer_zipcode => $import->{data}->{billing}->{zipCode}, |
|
138 |
customer_newsletter => $import->{data}->{customer}->{newsletter}, |
|
139 |
delivery_city => $import->{data}->{shipping}->{city}, |
|
140 |
delivery_company => $import->{data}->{shipping}->{company}, |
|
141 |
delivery_country => $import->{data}->{shipping}->{country}->{name}, |
|
142 |
delivery_department => $import->{data}->{shipping}->{department}, |
|
143 |
delivery_email => "", |
|
144 |
delivery_fax => $import->{data}->{shipping}->{fax}, |
|
145 |
delivery_firstname => $import->{data}->{shipping}->{firstName}, |
|
146 |
#delivery_greeting => ($import->{data}->{shipping}->{salutation} eq 'mr' ? 'Herr' : 'Frau'), |
|
147 |
delivery_lastname => $import->{data}->{shipping}->{lastName}, |
|
148 |
delivery_phone => $import->{data}->{shipping}->{phone}, |
|
149 |
delivery_street => $import->{data}->{shipping}->{street}, |
|
150 |
delivery_vat => $import->{data}->{shipping}->{vatId}, |
|
151 |
delivery_zipcode => $import->{data}->{shipping}->{zipCode}, |
|
152 |
host => $import->{data}->{shop}->{hosts}, |
|
153 |
netamount => $import->{data}->{invoiceAmountNet}, |
|
154 |
order_date => $orderdate, |
|
155 |
payment_description => $import->{data}->{payment}->{description}, |
|
156 |
payment_id => $import->{data}->{paymentId}, |
|
157 |
remote_ip => $import->{data}->{remoteAddress}, |
|
158 |
sepa_account_holder => $import->{data}->{paymentIntances}->{accountHolder}, |
|
159 |
sepa_bic => $import->{data}->{paymentIntances}->{bic}, |
|
160 |
sepa_iban => $import->{data}->{paymentIntances}->{iban}, |
|
161 |
shipping_costs => $import->{data}->{invoiceShipping}, |
|
162 |
shipping_costs_net => $import->{data}->{invoiceShippingNet}, |
|
163 |
shop_c_billing_id => $import->{data}->{billing}->{customerId}, |
|
164 |
shop_c_billing_number => $import->{data}->{billing}->{number}, |
|
165 |
shop_c_delivery_id => $import->{data}->{shipping}->{id}, |
|
166 |
shop_customer_id => $import->{data}->{customerId}, |
|
167 |
shop_customer_number => $import->{data}->{billing}->{number}, |
|
168 |
shop_customer_comment => $import->{data}->{customerComment}, |
|
169 |
shop_id => $self->config->id, |
|
170 |
shop_ordernumber => $import->{data}->{number}, |
|
171 |
shop_trans_id => $import->{data}->{id}, |
|
172 |
tax_included => $self->config->pricetype eq "brutto" ? 1 : 0, |
|
173 |
); |
|
174 |
my $shop_order = SL::DB::ShopOrder->new(%columns); |
|
175 |
return $shop_order; |
|
176 |
} |
|
227 | 177 |
|
228 | 178 |
sub get_categories { |
229 | 179 |
my ($self) = @_; |
... | ... | |
257 | 207 |
return SL::JSON::decode_json($data_json); |
258 | 208 |
}else{ |
259 | 209 |
my %return = ( success => 0, |
260 |
data => { version => $url . ": " . $status_line, revision => $type }, |
|
210 |
data => { version => $url . ": " . $status_line, revision => $type },
|
|
261 | 211 |
message => "Server not found or wrong data type", |
262 | 212 |
); |
263 | 213 |
return \%return; |
... | ... | |
291 | 241 |
my @upload_img = (); |
292 | 242 |
foreach my $img (@{ $images }) { |
293 | 243 |
my $file = SL::File->get(id => $img->file->id ); |
294 |
my $file_path = $file->get_file; |
|
295 | 244 |
my ($path, $extension) = (split /\./, $file->file_name); |
296 | 245 |
my $content = File::Slurp::read_file($file->get_file); |
297 |
|
|
298 |
my $temp ={ ( link => 'data:' . $file->mime_type . ';base64,' . MIME::Base64::encode($content), |
|
246 |
my $temp ={ ( link => 'data:' . $file->mime_type . ';base64,' . MIME::Base64::encode($content, ""), #$content, # MIME::Base64::encode($content), |
|
299 | 247 |
description => $img->file->title, |
300 | 248 |
position => $img->position, |
301 | 249 |
extension => $extension, |
... | ... | |
315 | 263 |
# } |
316 | 264 |
|
317 | 265 |
# get the right price |
266 |
# TODO In extra Helper?? |
|
318 | 267 |
my ( $price_src_str, $price_src_id ) = split(/\//,$shop_part->active_price_source); |
319 | 268 |
require SL::DB::Part; |
320 | 269 |
my $price; |
... | ... | |
401 | 350 |
|
402 | 351 |
my $dataString = SL::JSON::to_json(\%shop_data); |
403 | 352 |
$dataString = encode_utf8($dataString); |
353 |
|
|
404 | 354 |
my $upload_content; |
405 | 355 |
my $upload; |
406 | 356 |
if($import->{success}){ |
... | ... | |
454 | 404 |
); |
455 | 405 |
|
456 | 406 |
return $ua; |
407 |
|
|
457 | 408 |
} |
458 | 409 |
|
459 | 410 |
1; |
Auch abrufbar als: Unified diff
Shopmodul: Bilder hochladen geht jetzt
Bestellungen überarbeitet, Teile aus dem ShowareConnector ausgelagert, da diese allgemeingültig auch für andere Connectoren sind
Typo