04cd69a59c4ea30a1cd54a721d43bf19d073cece
[kivitendo-erp.git] / SL / ShopConnector / Shopware.pm
1 package SL::ShopConnector::Shopware;
2
3 use strict;
4
5 use parent qw(SL::ShopConnector::Base);
6
7
8 use SL::JSON;
9 use LWP::UserAgent;
10 use LWP::Authen::Digest;
11 use SL::DB::ShopOrder;
12 use SL::DB::ShopOrderItem;
13 use SL::DB::History;
14 use DateTime::Format::Strptime;
15 use SL::DB::File;
16 use Data::Dumper;
17 use Sort::Naturally ();
18 use SL::Helper::Flash;
19 use Encode qw(encode_utf8);
20 use SL::File;
21 use File::Slurp;
22
23 use Rose::Object::MakeMethods::Generic (
24   'scalar --get_set_init' => [ qw(connector url) ],
25 );
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       $self->set_orderstatus($import->{data}->{id}, "fetched");
50       $of++;
51     }else{
52       flash_later('error', $::locale->text('Database errors: #1', @errors));
53     }
54     %fetched_orders = (shop_description => $self->config->description, number_of_orders => $of);
55   } else {
56     my %error_msg  = (
57       shop_id          => $self->config->id,
58       shop_description => $self->config->description,
59       message          => "Error: $data->status_line",
60       error            => 1,
61     );
62     %fetched_orders = %error_msg;
63   }
64
65   return \%fetched_orders;
66 }
67
68 sub get_new_orders {
69   my ($self, $id) = @_;
70
71   my $url              = $self->url;
72   my $last_order_number = $self->config->last_order_number;
73   my $otf              = $self->config->orders_to_fetch;
74   my $of               = 0;
75   my $last_data      = $self->connector->get($url . "api/orders/$last_order_number?useNumberAsId=true");
76   my $last_data_json = $last_data->content;
77   my $last_import    = SL::JSON::decode_json($last_data_json);
78
79   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});
80
81   my $dbh = SL::DB->client;
82   my @errors;
83   my %fetched_orders;
84   if ($orders_data->is_success && $orders_data->content_type eq 'application/json'){
85     my $orders_data_json = $orders_data->content;
86     my $orders_import    = SL::JSON::decode_json($orders_data_json);
87     foreach my $shoporder(@{ $orders_import->{data} }){
88
89       my $data      = $self->connector->get($url . "api/orders/" . $shoporder->{id});
90       my $data_json = $data->content;
91       my $import    = SL::JSON::decode_json($data_json);
92
93       $dbh->with_transaction( sub{
94           $self->import_data_to_shop_order($import);
95
96           $self->config->assign_attributes( last_order_number => $shoporder->{number});
97           $self->config->save;
98           1;
99       })or do {
100         push @errors,($::locale->text('Saving failed. Error message from the database: #1', $dbh->error));
101       };
102
103       if(!@errors){
104         $self->set_orderstatus($shoporder->{id}, "fetched");
105         $of++;
106       }else{
107         flash_later('error', $::locale->text('Database errors: #1', @errors));
108       }
109     }
110     %fetched_orders = (shop_description => $self->config->description, number_of_orders => $of);
111   } else {
112     my %error_msg  = (
113       shop_id          => $self->config->id,
114       shop_description => $self->config->description,
115       message          => "Error: $orders_data->status_line",
116       error            => 1,
117     );
118     %fetched_orders = %error_msg;
119   }
120
121   return \%fetched_orders;
122 }
123
124 sub import_data_to_shop_order {
125   my ( $self, $import ) = @_;
126   my $shop_order = $self->map_data_to_shoporder($import);
127
128   $shop_order->save;
129   my $id = $shop_order->id;
130
131   my @positions = sort { Sort::Naturally::ncmp($a->{"articleNumber"}, $b->{"articleNumber"}) } @{ $import->{data}->{details} };
132   #my @positions = sort { Sort::Naturally::ncmp($a->{"partnumber"}, $b->{"partnumber"}) } @{ $import->{data}->{details} };
133   my $position = 1;
134   my $active_price_source = $self->config->price_source;
135   #Mapping Positions
136   foreach my $pos(@positions) {
137     my $price = $::form->round_amount($pos->{price},2);
138     my %pos_columns = ( description          => $pos->{articleName},
139                         partnumber           => $pos->{articleNumber},
140                         price                => $price,
141                         quantity             => $pos->{quantity},
142                         position             => $position,
143                         tax_rate             => $pos->{taxRate},
144                         shop_trans_id        => $pos->{articleId},
145                         shop_order_id        => $id,
146                         active_price_source  => $active_price_source,
147                       );
148     my $pos_insert = SL::DB::ShopOrderItem->new(%pos_columns);
149     $pos_insert->save;
150     $position++;
151   }
152   $shop_order->positions($position-1);
153
154   if ( $self->config->shipping_costs_parts_id ) {
155     my $shipping_part = SL::DB::Part->find_by( id => $self->config->shipping_costs_parts_id);
156     my %shipping_pos = ( description    => $import->{data}->{dispatch}->{name},
157                          partnumber     => $shipping_part->partnumber,
158                          price          => $import->{data}->{invoiceShipping},
159                          quantity       => 1,
160                          position       => $position,
161                          shop_trans_id  => 0,
162                          shop_order_id  => $id,
163                        );
164     my $shipping_pos_insert = SL::DB::ShopOrderItem->new(%shipping_pos);
165     $shipping_pos_insert->save;
166   }
167
168   my $customer = $shop_order->get_customer;
169
170   if(ref($customer)){
171     $shop_order->kivi_customer_id($customer->id);
172   }
173   $shop_order->save;
174 }
175
176 sub map_data_to_shoporder {
177   my ($self, $import) = @_;
178
179   my $parser = DateTime::Format::Strptime->new( pattern   => '%Y-%m-%dT%H:%M:%S',
180                                                   locale    => 'de_DE',
181                                                   time_zone => 'local'
182                                                 );
183   my $orderdate = $parser->parse_datetime($import->{data}->{orderTime});
184
185   my $shop_id      = $self->config->id;
186   my $tax_included = $self->config->pricetype;
187
188   # Mapping to table shoporders. See http://community.shopware.com/_detail_1690.html#GET_.28Liste.29
189   my %columns = (
190     amount                  => $import->{data}->{invoiceAmount},
191     billing_city            => $import->{data}->{billing}->{city},
192     billing_company         => $import->{data}->{billing}->{company},
193     billing_country         => $import->{data}->{billing}->{country}->{name},
194     billing_department      => $import->{data}->{billing}->{department},
195     billing_email           => $import->{data}->{customer}->{email},
196     billing_fax             => $import->{data}->{billing}->{fax},
197     billing_firstname       => $import->{data}->{billing}->{firstName},
198     #billing_greeting        => ($import->{data}->{billing}->{salutation} eq 'mr' ? 'Herr' : 'Frau'),
199     billing_lastname        => $import->{data}->{billing}->{lastName},
200     billing_phone           => $import->{data}->{billing}->{phone},
201     billing_street          => $import->{data}->{billing}->{street},
202     billing_vat             => $import->{data}->{billing}->{vatId},
203     billing_zipcode         => $import->{data}->{billing}->{zipCode},
204     customer_city           => $import->{data}->{billing}->{city},
205     customer_company        => $import->{data}->{billing}->{company},
206     customer_country        => $import->{data}->{billing}->{country}->{name},
207     customer_department     => $import->{data}->{billing}->{department},
208     customer_email          => $import->{data}->{customer}->{email},
209     customer_fax            => $import->{data}->{billing}->{fax},
210     customer_firstname      => $import->{data}->{billing}->{firstName},
211     #customer_greeting       => ($import->{data}->{billing}->{salutation} eq 'mr' ? 'Herr' : 'Frau'),
212     customer_lastname       => $import->{data}->{billing}->{lastName},
213     customer_phone          => $import->{data}->{billing}->{phone},
214     customer_street         => $import->{data}->{billing}->{street},
215     customer_vat            => $import->{data}->{billing}->{vatId},
216     customer_zipcode        => $import->{data}->{billing}->{zipCode},
217     customer_newsletter     => $import->{data}->{customer}->{newsletter},
218     delivery_city           => $import->{data}->{shipping}->{city},
219     delivery_company        => $import->{data}->{shipping}->{company},
220     delivery_country        => $import->{data}->{shipping}->{country}->{name},
221     delivery_department     => $import->{data}->{shipping}->{department},
222     delivery_email          => "",
223     delivery_fax            => $import->{data}->{shipping}->{fax},
224     delivery_firstname      => $import->{data}->{shipping}->{firstName},
225     #delivery_greeting       => ($import->{data}->{shipping}->{salutation} eq 'mr' ? 'Herr' : 'Frau'),
226     delivery_lastname       => $import->{data}->{shipping}->{lastName},
227     delivery_phone          => $import->{data}->{shipping}->{phone},
228     delivery_street         => $import->{data}->{shipping}->{street},
229     delivery_vat            => $import->{data}->{shipping}->{vatId},
230     delivery_zipcode        => $import->{data}->{shipping}->{zipCode},
231     host                    => $import->{data}->{shop}->{hosts},
232     netamount               => $import->{data}->{invoiceAmountNet},
233     order_date              => $orderdate,
234     payment_description     => $import->{data}->{payment}->{description},
235     payment_id              => $import->{data}->{paymentId},
236     remote_ip               => $import->{data}->{remoteAddress},
237     sepa_account_holder     => $import->{data}->{paymentIntances}->{accountHolder},
238     sepa_bic                => $import->{data}->{paymentIntances}->{bic},
239     sepa_iban               => $import->{data}->{paymentIntances}->{iban},
240     shipping_costs          => $import->{data}->{invoiceShipping},
241     shipping_costs_net      => $import->{data}->{invoiceShippingNet},
242     shop_c_billing_id       => $import->{data}->{billing}->{customerId},
243     shop_c_billing_number   => $import->{data}->{billing}->{number},
244     shop_c_delivery_id      => $import->{data}->{shipping}->{id},
245     shop_customer_id        => $import->{data}->{customerId},
246     shop_customer_number    => $import->{data}->{billing}->{number},
247     shop_customer_comment   => $import->{data}->{customerComment},
248     shop_id                 => $shop_id,
249     shop_ordernumber        => $import->{data}->{number},
250     shop_trans_id           => $import->{data}->{id},
251     tax_included            => $tax_included eq "brutto" ? 1 : 0,
252   );
253
254   my $shop_order = SL::DB::ShopOrder->new(%columns);
255   return $shop_order;
256 }
257
258 sub get_categories {
259   my ($self) = @_;
260
261   my $url        = $self->url;
262   my $data       = $self->connector->get($url . "api/categories");
263   my $data_json  = $data->content;
264   my $import     = SL::JSON::decode_json($data_json);
265   my @daten      = @{$import->{data}};
266   my %categories = map { ($_->{id} => $_) } @daten;
267
268   for(@daten) {
269     my $parent = $categories{$_->{parentId}};
270     $parent->{children} ||= [];
271     push @{$parent->{children}},$_;
272   }
273
274   return \@daten;
275 }
276
277 sub get_version {
278   my ($self) = @_;
279
280   my $url       = $self->url;
281   my $data      = $self->connector->get($url . "api/version");
282   my $type = $data->content_type;
283   my $status_line = $data->status_line;
284
285   if($data->is_success && $type eq 'application/json'){
286     my $data_json = $data->content;
287     return SL::JSON::decode_json($data_json);
288   }else{
289     my %return = ( success => 0,
290                    data    => { version => $url . ": " . $status_line, revision => $type },
291                    message => "Server not found or wrong data type",
292                 );
293     return \%return;
294   }
295 }
296
297 sub update_part {
298   my ($self, $shop_part, $todo) = @_;
299
300   #shop_part is passed as a param
301   die unless ref($shop_part) eq 'SL::DB::ShopPart';
302
303   my $url = $self->url;
304   my $part = SL::DB::Part->new(id => $shop_part->part_id)->load;
305
306   # CVARS to map
307   my $cvars = { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $part->cvars_by_config } };
308
309   my @cat = ();
310   foreach my $row_cat ( @{ $shop_part->shop_category } ) {
311     my $temp = { ( id => @{$row_cat}[0], ) };
312     push ( @cat, $temp );
313   }
314
315   my @upload_img = $shop_part->get_images;
316   my $tax_n_price = $shop_part->get_tax_and_price;
317   my $price = $tax_n_price->{price};
318   my $taxrate = $tax_n_price->{tax};
319   # mapping to shopware still missing attributes,metatags
320   my %shop_data;
321
322   if($todo eq "price"){
323     %shop_data = ( mainDetail => { number   => $part->partnumber,
324                                    prices   =>  [ { from             => 1,
325                                                     price            => $price,
326                                                     customerGroupKey => 'EK',
327                                                   },
328                                                 ],
329                                   },
330                  );
331   }elsif($todo eq "stock"){
332     %shop_data = ( mainDetail => { number   => $part->partnumber,
333                                    inStock  => $part->onhand,
334                                  },
335                  );
336   }elsif($todo eq "price_stock"){
337     %shop_data =  ( mainDetail => { number   => $part->partnumber,
338                                     inStock  => $part->onhand,
339                                     prices   =>  [ { from             => 1,
340                                                      price            => $price,
341                                                      customerGroupKey => 'EK',
342                                                    },
343                                                  ],
344                                    },
345                    );
346   }elsif($todo eq "active"){
347     %shop_data =  ( mainDetail => { number   => $part->partnumber,
348                                    },
349                     active => ($part->partnumber == 1 ? 0 : 1),
350                    );
351   }elsif($todo eq "all"){
352   # mapping to shopware still missing attributes,metatags
353     %shop_data =  (   name              => $part->description,
354                       mainDetail        => { number   => $part->partnumber,
355                                              inStock  => $part->onhand,
356                                              prices   =>  [ {          from   => 1,
357                                                                        price  => $price,
358                                                             customerGroupKey  => 'EK',
359                                                             },
360                                                           ],
361                                              active   => $shop_part->active,
362                                              #attribute => { attr1  => $cvars->{CVARNAME}->{value}, } , #HowTo handle attributes
363                                        },
364                       supplier          => 'AR', # Is needed by shopware,
365                       descriptionLong   => $shop_part->shop_description,
366                       active            => $shop_part->active,
367                       images            => [ @upload_img ],
368                       __options_images  => { replace => 1, },
369                       categories        => [ @cat ],
370                       description       => $shop_part->shop_description,
371                       categories        => [ @cat ],
372                       tax               => $taxrate,
373                     )
374                   ;
375   }
376
377   my $dataString = SL::JSON::to_json(\%shop_data);
378   $dataString    = encode_utf8($dataString);
379
380   my $upload_content;
381   my $upload;
382   my ($import,$data,$data_json);
383   my $partnumber = $::form->escape($part->partnumber);#shopware don't accept / in articlenumber
384   # 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
385   # LWP->post = create LWP->put = update
386     $data       = $self->connector->get($url . "api/articles/$partnumber?useNumberAsId=true");
387     $data_json  = $data->content;
388     $import     = SL::JSON::decode_json($data_json);
389   if($import->{success}){
390     #update
391     my $partnumber  = $::form->escape($part->partnumber);#shopware don't accept / in articlenumber
392     $upload         = $self->connector->put($url . "api/articles/$partnumber?useNumberAsId=true", Content => $dataString);
393     my $data_json   = $upload->content;
394     $upload_content = SL::JSON::decode_json($data_json);
395   }else{
396     #upload
397     $upload         = $self->connector->post($url . "api/articles/", Content => $dataString);
398     my $data_json   = $upload->content;
399     $upload_content = SL::JSON::decode_json($data_json);
400   }
401   # don't know if this is needed
402   if(@upload_img) {
403     my $partnumber = $::form->escape($part->partnumber);#shopware don't accept / in articlenumber
404     my $imgup      = $self->connector->put($url . "api/generatearticleimages/$partnumber?useNumberAsId=true");
405   }
406
407   return $upload_content->{success};
408 }
409
410 sub get_article {
411   my ($self,$partnumber) = @_;
412
413   my $url       = $self->url;
414   $partnumber   = $::form->escape($partnumber);#shopware don't accept / in articlenumber
415   my $data      = $self->connector->get($url . "api/articles/$partnumber?useNumberAsId=true");
416   my $data_json = $data->content;
417   return SL::JSON::decode_json($data_json);
418 }
419
420 sub set_orderstatus {
421   my ($self,$order_id, $status) = @_;
422   if ($status eq "fetched") { $status = 1; }
423   if ($status eq "completed") { $status = 2; }
424   my %new_status = (orderStatusId => $status);
425   my $new_status_json = SL::JSON::to_json(\%new_status);
426   $self->connector->put($self->url . "api/orders/$order_id", Content => $new_status_json);
427 }
428
429 sub init_url {
430   my ($self) = @_;
431   $self->url($self->config->protocol . "://" . $self->config->server . ":" . $self->config->port . $self->config->path);
432 }
433
434 sub init_connector {
435   my ($self) = @_;
436   my $ua = LWP::UserAgent->new;
437   $ua->credentials(
438       $self->config->server . ":" . $self->config->port,
439       $self->config->realm,
440       $self->config->login => $self->config->password
441   );
442
443   return $ua;
444
445 }
446
447 1;
448
449 __END__
450
451 =encoding utf-8
452
453 =head1 NAME
454
455 SL::Shopconnecter::Shopware - connector for shopware 5
456
457 =head1 SYNOPSIS
458
459
460 =head1 DESCRIPTION
461
462 This is the connector to shopware.
463 In this file you can do the mapping to your needs.
464 see https://developers.shopware.com/developers-guide/rest-api/
465 for more information.
466
467 =head1 METHODS
468
469 =over 4
470
471 =item C<get_one_order>
472
473 Fetches one order specified by ordnumber
474
475 =item C<get_new_orders>
476
477 Fetches new order by parameters from shop configuration
478
479 =item C<import_data_to_shop_order>
480
481 Creates on shoporder object from json
482 Here is the mapping for the positions.
483 see https://developers.shopware.com/developers-guide/rest-api/
484 for detailed information
485
486 =item C<map_data_to_shoporder>
487
488 Here is the mapping for the order data.
489 see https://developers.shopware.com/developers-guide/rest-api/
490 for detailed information
491
492 =item C<get_categories>
493
494 =item C<get_version>
495
496 Use this for test Connection
497 see SL::Shop
498
499 =item C<update_part>
500
501 Here is the mapping for the article data.
502 see https://developers.shopware.com/developers-guide/rest-api/
503 for detailed information
504
505 =item C<get_article>
506
507 =back
508
509 =head1 INITS
510
511 =over 4
512
513 =item init_url
514
515 build an url for LWP
516
517 =item init_connector
518
519 =back
520
521 =head1 TODO
522
523 Pricesrules, pricessources aren't fully implemented yet.
524 Payments aren't implemented( need to map payments from Shopware like invoice, paypal etc. to payments in kivitendo)
525
526 =head1 BUGS
527
528 None yet. :)
529
530 =head1 AUTHOR
531
532 W. Hahn E<lt>wh@futureworldsearch.netE<gt>
533
534 =cut