24 |
24 |
'scalar --get_set_init' => [ qw(connector url) ],
|
25 |
25 |
);
|
26 |
26 |
|
|
27 |
sub get_one_order {
|
|
28 |
my ($self, $ordnumber) = @_;
|
|
29 |
|
|
30 |
my $dbh = SL::DB::client;
|
|
31 |
my $of = 0;
|
|
32 |
my $url = $self->url;
|
|
33 |
my $data = $self->connector->get($url . "api/orders/$ordnumber?useNumberAsId=true");
|
|
34 |
my @errors;
|
|
35 |
|
|
36 |
my %fetched_orders;
|
|
37 |
if ($data->is_success && $data->content_type eq 'application/json'){
|
|
38 |
my $data_json = $data->content;
|
|
39 |
my $import = SL::JSON::decode_json($data_json);
|
|
40 |
my $shoporder = $import->{data};
|
|
41 |
$dbh->with_transaction( sub{
|
|
42 |
$self->import_data_to_shop_order($import);
|
|
43 |
1;
|
|
44 |
})or do {
|
|
45 |
push @errors,($::locale->text('Saving failed. Error message from the database: #1', $dbh->error));
|
|
46 |
};
|
|
47 |
|
|
48 |
if(!@errors){
|
|
49 |
$of++;
|
|
50 |
}else{
|
|
51 |
flash_later('error', $::locale->text('Database errors: #1', @errors));
|
|
52 |
}
|
|
53 |
%fetched_orders = (shop_description => $self->config->description, number_of_orders => $of);
|
|
54 |
} else {
|
|
55 |
my %error_msg = (
|
|
56 |
shop_id => $self->config->id,
|
|
57 |
shop_description => $self->config->description,
|
|
58 |
message => "Error: $data->status_line",
|
|
59 |
error => 1,
|
|
60 |
);
|
|
61 |
%fetched_orders = %error_msg;
|
|
62 |
}
|
|
63 |
|
|
64 |
return \%fetched_orders;
|
|
65 |
}
|
|
66 |
|
27 |
67 |
sub get_new_orders {
|
28 |
68 |
my ($self, $id) = @_;
|
29 |
69 |
|
30 |
70 |
my $url = $self->url;
|
31 |
|
my $ordnumber = $self->config->last_order_number + 1;
|
|
71 |
my $last_order_number = $self->config->last_order_number;
|
32 |
72 |
my $otf = $self->config->orders_to_fetch;
|
33 |
73 |
my $of = 0;
|
34 |
|
my $orders_data = $self->connector->get($url . "api/orders?limit=$otf&filter[0][property]=number&filter[0][expression]=>&filter[0][value]=" . $self->config->last_order_number);
|
35 |
|
my $orders_data_json = $orders_data->content;
|
36 |
|
my $orders_import = SL::JSON::decode_json($orders_data_json);
|
37 |
|
|
38 |
|
if ($orders_import->{success}){
|
|
74 |
my $last_data = $self->connector->get($url . "api orders/$last_order_number?useNumberAsId=true");
|
|
75 |
my $last_data_json = $last_data->content;
|
|
76 |
my $last_import = SL::JSON::decode_json($last_data_json);
|
|
77 |
|
|
78 |
my $orders_data = $self->connector->get($url . "api/orders?limit=$otf&filter[1][property]=status&filter[1][value]=0&filter[0][property]=id&filter[0][expression]=>&filter[0][value]=" . $last_import->{data}->{id});
|
|
79 |
|
|
80 |
my $dbh = SL::DB->client;
|
|
81 |
my @errors;
|
|
82 |
my %fetched_orders;
|
|
83 |
if ($orders_data->is_success && $orders_data->content_type eq 'application/json'){
|
|
84 |
my $orders_data_json = $orders_data->content;
|
|
85 |
my $orders_import = SL::JSON::decode_json($orders_data_json);
|
39 |
86 |
foreach my $shoporder(@{ $orders_import->{data} }){
|
40 |
87 |
|
41 |
88 |
my $data = $self->connector->get($url . "api/orders/" . $shoporder->{id});
|
42 |
89 |
my $data_json = $data->content;
|
43 |
90 |
my $import = SL::JSON::decode_json($data_json);
|
44 |
91 |
|
45 |
|
$self->import_data_to_shop_order($import);
|
46 |
|
|
47 |
|
$self->config->assign_attributes( last_order_number => $ordnumber);
|
48 |
|
$self->config->save;
|
49 |
|
$ordnumber++;
|
50 |
|
$of++;
|
|
92 |
$dbh->with_transaction( sub{
|
|
93 |
$self->import_data_to_shop_order($import);
|
|
94 |
|
|
95 |
$self->config->assign_attributes( last_order_number => $shoporder->{number});
|
|
96 |
$self->config->save;
|
|
97 |
1;
|
|
98 |
})or do {
|
|
99 |
push @errors,($::locale->text('Saving failed. Error message from the database: #1', $dbh->error));
|
|
100 |
};
|
|
101 |
|
|
102 |
if(!@errors){
|
|
103 |
$of++;
|
|
104 |
}else{
|
|
105 |
flash_later('error', $::locale->text('Database errors: #1', @errors));
|
|
106 |
}
|
51 |
107 |
}
|
|
108 |
%fetched_orders = (shop_description => $self->config->description, number_of_orders => $of);
|
|
109 |
} else {
|
|
110 |
my %error_msg = (
|
|
111 |
shop_id => $self->config->id,
|
|
112 |
shop_description => $self->config->description,
|
|
113 |
message => "Error: $orders_data->status_line",
|
|
114 |
error => 1,
|
|
115 |
);
|
|
116 |
%fetched_orders = %error_msg;
|
52 |
117 |
}
|
53 |
|
my $shop = $self->config->description;
|
54 |
|
my %fetched_orders = (shop_id => $self->config->description, number_of_orders => $of);
|
|
118 |
|
55 |
119 |
return \%fetched_orders;
|
56 |
120 |
}
|
57 |
121 |
|
... | ... | |
62 |
126 |
$shop_order->save;
|
63 |
127 |
my $id = $shop_order->id;
|
64 |
128 |
|
65 |
|
my @positions = sort { Sort::Naturally::ncmp($a->{"partnumber"}, $b->{"partnumber"}) } @{ $import->{data}->{details} };
|
|
129 |
my @positions = sort { Sort::Naturally::ncmp($a->{"articleNumber"}, $b->{"articleNumber"}) } @{ $import->{data}->{details} };
|
|
130 |
#my @positions = sort { Sort::Naturally::ncmp($a->{"partnumber"}, $b->{"partnumber"}) } @{ $import->{data}->{details} };
|
66 |
131 |
my $position = 1;
|
67 |
132 |
my $active_price_source = $self->config->price_source;
|
68 |
133 |
#Mapping Positions
|
... | ... | |
82 |
147 |
$pos_insert->save;
|
83 |
148 |
$position++;
|
84 |
149 |
}
|
85 |
|
$shop_order->{positions} = $position-1;
|
|
150 |
$shop_order->positions($position-1);
|
86 |
151 |
|
87 |
152 |
my $customer = $shop_order->get_customer;
|
88 |
153 |
|
89 |
154 |
if(ref($customer)){
|
90 |
155 |
$shop_order->kivi_customer_id($customer->id);
|
91 |
|
$shop_order->save;
|
92 |
156 |
}
|
|
157 |
$shop_order->save;
|
93 |
158 |
}
|
94 |
159 |
|
95 |
160 |
sub map_data_to_shoporder {
|
... | ... | |
103 |
168 |
|
104 |
169 |
my $shop_id = $self->config->id;
|
105 |
170 |
my $tax_included = $self->config->pricetype;
|
|
171 |
|
106 |
172 |
# Mapping to table shoporders. See http://community.shopware.com/_detail_1690.html#GET_.28Liste.29
|
107 |
173 |
my %columns = (
|
108 |
174 |
amount => $import->{data}->{invoiceAmount},
|
... | ... | |
219 |
285 |
die unless ref($shop_part) eq 'SL::DB::ShopPart';
|
220 |
286 |
|
221 |
287 |
my $url = $self->url;
|
222 |
|
my $part = SL::DB::Part->new(id => $shop_part->{part_id})->load;
|
|
288 |
my $part = SL::DB::Part->new(id => $shop_part->part_id)->load;
|
223 |
289 |
|
224 |
290 |
# CVARS to map
|
225 |
291 |
my $cvars = { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $part->cvars_by_config } };
|
... | ... | |
238 |
304 |
my %shop_data;
|
239 |
305 |
|
240 |
306 |
if($todo eq "price"){
|
241 |
|
%shop_data = ( mainDetail => { number => $part->{partnumber},
|
|
307 |
%shop_data = ( mainDetail => { number => $part->partnumber,
|
242 |
308 |
prices => [ { from => 1,
|
243 |
309 |
price => $price,
|
244 |
310 |
customerGroupKey => 'EK',
|
... | ... | |
247 |
313 |
},
|
248 |
314 |
);
|
249 |
315 |
}elsif($todo eq "stock"){
|
250 |
|
%shop_data = ( mainDetail => { number => $part->{partnumber},
|
251 |
|
inStock => $part->{onhand},
|
|
316 |
%shop_data = ( mainDetail => { number => $part->partnumber,
|
|
317 |
inStock => $part->onhand,
|
252 |
318 |
},
|
253 |
319 |
);
|
254 |
320 |
}elsif($todo eq "price_stock"){
|
255 |
|
%shop_data = ( mainDetail => { number => $part->{partnumber},
|
256 |
|
inStock => $part->{onhand},
|
|
321 |
%shop_data = ( mainDetail => { number => $part->partnumber,
|
|
322 |
inStock => $part->onhand,
|
257 |
323 |
prices => [ { from => 1,
|
258 |
324 |
price => $price,
|
259 |
325 |
customerGroupKey => 'EK',
|
... | ... | |
262 |
328 |
},
|
263 |
329 |
);
|
264 |
330 |
}elsif($todo eq "active"){
|
265 |
|
%shop_data = ( mainDetail => { number => $part->{partnumber},
|
|
331 |
%shop_data = ( mainDetail => { number => $part->partnumber,
|
266 |
332 |
},
|
267 |
|
active => ($part->{partnumber} == 1 ? 0 : 1),
|
|
333 |
active => ($part->partnumber == 1 ? 0 : 1),
|
268 |
334 |
);
|
269 |
335 |
}elsif($todo eq "all"){
|
270 |
336 |
# mapping to shopware still missing attributes,metatags
|
271 |
|
%shop_data = ( name => $part->{description},
|
272 |
|
mainDetail => { number => $part->{partnumber},
|
273 |
|
inStock => $part->{onhand},
|
|
337 |
%shop_data = ( name => $part->description,
|
|
338 |
mainDetail => { number => $part->partnumber,
|
|
339 |
inStock => $part->onhand,
|
274 |
340 |
prices => [ { from => 1,
|
275 |
341 |
price => $price,
|
276 |
342 |
customerGroupKey => 'EK',
|
... | ... | |
280 |
346 |
#attribute => { attr1 => $cvars->{CVARNAME}->{value}, } , #HowTo handle attributes
|
281 |
347 |
},
|
282 |
348 |
supplier => 'AR', # Is needed by shopware,
|
283 |
|
descriptionLong => $shop_part->{shop_description},
|
|
349 |
descriptionLong => $shop_part->shop_description,
|
284 |
350 |
active => $shop_part->active,
|
285 |
351 |
images => [ @upload_img ],
|
286 |
352 |
__options_images => { replace => 1, },
|
287 |
353 |
categories => [ @cat ],
|
288 |
|
description => $shop_part->{shop_description},
|
|
354 |
description => $shop_part->shop_description,
|
289 |
355 |
categories => [ @cat ],
|
290 |
356 |
tax => $taxrate,
|
291 |
357 |
)
|
... | ... | |
298 |
364 |
my $upload_content;
|
299 |
365 |
my $upload;
|
300 |
366 |
my ($import,$data,$data_json);
|
301 |
|
my $partnumber = $::form->escape($part->{partnumber});#shopware don't accept / in articlenumber
|
|
367 |
my $partnumber = $::form->escape($part->partnumber);#shopware don't accept / in articlenumber
|
302 |
368 |
# Shopware RestApi sends an erroremail if configured and part not found. But it needs this info to decide if update or create a new article
|
303 |
369 |
# LWP->post = create LWP->put = update
|
304 |
370 |
$data = $self->connector->get($url . "api/articles/$partnumber?useNumberAsId=true");
|
... | ... | |
306 |
372 |
$import = SL::JSON::decode_json($data_json);
|
307 |
373 |
if($import->{success}){
|
308 |
374 |
#update
|
309 |
|
my $partnumber = $::form->escape($part->{partnumber});#shopware don't accept / in articlenumber
|
|
375 |
my $partnumber = $::form->escape($part->partnumber);#shopware don't accept / in articlenumber
|
310 |
376 |
$upload = $self->connector->put($url . "api/articles/$partnumber?useNumberAsId=true", Content => $dataString);
|
311 |
377 |
my $data_json = $upload->content;
|
312 |
378 |
$upload_content = SL::JSON::decode_json($data_json);
|
... | ... | |
318 |
384 |
}
|
319 |
385 |
# don't know if this is needed
|
320 |
386 |
if(@upload_img) {
|
321 |
|
my $partnumber = $::form->escape($part->{partnumber});#shopware don't accept / in articlenumber
|
|
387 |
my $partnumber = $::form->escape($part->partnumber);#shopware don't accept / in articlenumber
|
322 |
388 |
my $imgup = $self->connector->put($url . "api/generatearticleimages/$partnumber?useNumberAsId=true");
|
323 |
389 |
}
|
324 |
390 |
|
... | ... | |
377 |
443 |
|
378 |
444 |
=over 4
|
379 |
445 |
|
|
446 |
=item C<get_one_order>
|
|
447 |
|
|
448 |
Fetches one order specified by ordnumber
|
|
449 |
|
380 |
450 |
=item C<get_new_orders>
|
381 |
451 |
|
|
452 |
Fetches new order by parameters from shop configuration
|
|
453 |
|
382 |
454 |
=item C<import_data_to_shop_order>
|
383 |
455 |
|
384 |
456 |
Creates on shoporder object from json
|
WebshopApi: ShopConnector überarbeitet