commit 63720c4e063fdd6b3297048f429037c30af1a7f8
Author: Werner Hahn <wh@futureworldsearch.net>
Date:   Thu Apr 21 20:41:07 2016 +0200

    ShopPart: Test Artikel wird mit json im LWP::UserAgent in den Shop hochgeladen

diff --git a/SL/Controller/ShopPart.pm b/SL/Controller/ShopPart.pm
index e144e45a6..70fe194e4 100644
--- a/SL/Controller/ShopPart.pm
+++ b/SL/Controller/ShopPart.pm
@@ -7,6 +7,7 @@ use parent qw(SL::Controller::Base);
 use Data::Dumper;
 use SL::Locale::String qw(t8);
 use SL::DB::ShopPart;
+use SL::DB::File;
 use SL::DB::Default;
 use SL::Helper::Flash;
 
@@ -32,10 +33,26 @@ sub action_update_shop {
 
   my $shop_part = SL::DB::Manager::ShopPart->find_by(id => $::form->{shop_part_id});
   die unless $shop_part;
+  $main::lxdebug->dump(0, 'WH: ShopPart',\$shop_part);
+
+  #my $part = SL::DB::Manager::Part->find_by(id => $shop_part->{part_id});
+  #$main::lxdebug->dump(0, 'WH: Part',\$part);
+
+  #my $cvars = { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $part->cvars_by_config } };
+  #$main::lxdebug->dump(0, 'WH: CVARS',\$cvars);
+
+  #my $images = SL::DB::Manager::File->get_all_sorted( where => [ trans_id => $shop_part->{part_id}, modul => 'shop_part', file_content_type => { like => 'image/%' } ], sort_by => 'position' );
+  #$main::lxdebug->dump(0, 'WH: Images',\$images);
+
+  #$part->{shop_part}  = $shop_part;
+  #$part->{cvars}      = $cvars;
+  #$part->{images}     = $images;
+
+  #$main::lxdebug->dump(0, 'WH: Part II',\$part);
   require SL::Shop;
   my $shop = SL::Shop->new( config => $shop_part->shop );
 
-  # TODO: generate data to upload to shop
+  # TODO: generate data to upload to shop. Goes to SL::Connector::XXXConnector. Here the object holds all data from parts, shop_parts, files, custom_variables for one article
   my $part_hash = $shop_part->part->as_tree;
   my $json      = SL::JSON::to_json($part_hash);
   my $return    = $shop->connector->update_part($self->shop_part, $json);
@@ -56,7 +73,6 @@ sub action_update_shop {
 sub action_show_files {
   my ($self) = @_;
 
-  require SL::DB::File;
   my $images = SL::DB::Manager::File->get_all_sorted( where => [ trans_id => $::form->{id}, modul => $::form->{modul}, file_content_type => { like => 'image/%' } ], sort_by => 'position' );
 
   $self->render('shop_part/_list_images', { header => 0 }, IMAGES => $images);
@@ -214,5 +230,5 @@ shop. Generates a  Calls some ClientJS functions to modifiy original page.
 =head1 AUTHORS
 
 G. Richardson E<lt>information@kivitendo-premium.deE<gt>
-
+W. Hahn E<lt>wh@futureworldsearch.netE<gt>
 =cut
diff --git a/SL/ShopConnector/Shopware.pm b/SL/ShopConnector/Shopware.pm
index bd37488fd..f80215450 100644
--- a/SL/ShopConnector/Shopware.pm
+++ b/SL/ShopConnector/Shopware.pm
@@ -5,6 +5,7 @@ use strict;
 use parent qw(SL::ShopConnector::Base);
 
 use SL::JSON;
+use JSON;
 use LWP::UserAgent;
 use LWP::Authen::Digest;
 use SL::DB::ShopOrder;
@@ -26,10 +27,8 @@ sub get_new_orders {
   for(my $i=1;$i<=350;$i++) {
     my $data = $self->connector->get("https://$url/api/orders/$ordnumber?useNumberAsId=true");
     $ordnumber++;
-    $::lxdebug->dump(0, "WH: DATA ", \$data);
     my $data_json = $data->content;
     my $import = SL::JSON::decode_json($data_json);
-    $::lxdebug->dump(0, "WH: IMPORT ", \$import);
     # Mapping to table shoporders
     my %columns = (
       amount                  => $import->{data}->{invoiceAmount},
@@ -96,19 +95,14 @@ sub get_new_orders {
       shop_trans_id           => $import->{data}->{id},
       tax_included            => ($import->{data}->{net} == 0 ? 0 : 1)
     );
-    $::lxdebug->dump(0, "WH: COLUMNS ", \%columns);
     my $insert = SL::DB::ShopOrder->new(%columns);
     $insert->save;
     my $id = $insert->id;
-    #$::lxdebug->dump(0, "WH: ID ", $insert->id);
 
-    #my @positions = @{ $import->{data}->{details} };
     my @positions = sort { Sort::Naturally::ncmp($a->{"partnumber"}, $b->{"partnumber"}) } @{ $import->{data}->{details} };
-    # $::lxdebug->dump(0, "WH: POSITIONS ", \@positions);
     my $position = 1;
     foreach my $pos(@positions) {
       my %pos_columns = ( description => $pos->{articleName},
-        #     id          => $pos->{id},
                           partnumber  => $pos->{articleNumber},
                           price       => $pos->{price},
                           quantity    => $pos->{quantity},
@@ -120,7 +114,6 @@ sub get_new_orders {
       my $pos_insert = SL::DB::ShopOrderItem->new(%pos_columns);
       $pos_insert->save;
       $position++;
-      #$::lxdebug->dump(0,"WH: POS ", \%pos_columns);
     }
     # Versandkosten als Position am ende einfügen Dreschflegelspezifisch event. konfigurierbar machen
     if (my $shipping = $import->{data}->{dispatch}->{name}) {
@@ -130,9 +123,7 @@ sub get_new_orders {
                                   'Standard Versand'            => { 'partnumber' => '905500'},
                                   'Kostenloser Versand'         => { 'partnumber' => '905500'},
                                 );
-      $main::lxdebug->message(0, "WH: SHIPPING1: $shipping ");
       my %shipping_pos = ( description => $import->{data}->{dispatch}->{name},
-        #      id          => 0,
                            partnumber  => $shipping_partnumbers{$shipping}->{partnumber},
                            price       => $import->{data}->{invoiceShipping},
                            quantity    => 1,
@@ -141,12 +132,10 @@ sub get_new_orders {
                            shop_trans_id  => 0,
                            shop_order_id  => $id,
                          );
-      $main::lxdebug->dump(0, 'WH: SHIPPING: ', \%shipping_pos);
       my $shipping_pos_insert = SL::DB::ShopOrderItem->new(%shipping_pos);
       $shipping_pos_insert->save;
     }
   }
-  # return $import;
 };
 
 sub get_categories {
@@ -173,9 +162,57 @@ sub get_article {
 }
 
 sub get_articles {
+  my ($self, $json_data) = @_;
+  $main::lxdebug->dump(0, 'WH: JSON: ', \$json_data);
+
 }
 
-sub set_article {
+sub update_part {
+  my ($self, $json) = @_;
+  $main::lxdebug->dump(0, 'WH: UPDATE SELF: ', \$self);
+  $main::lxdebug->dump(0, 'WH: UPDATE PART: ', \$json);
+  my $url = $self->url;
+  my $part = SL::DB::Manager::Part->find_by(id => $json->{part_id});
+  $main::lxdebug->dump(0, 'WH: Part',\$part);
+
+  my $cvars = { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $part->cvars_by_config } };
+  $main::lxdebug->dump(0, 'WH: CVARS',\$cvars);
+$main::lxdebug->dump(0, 'WH: Partnumber', $part->{partnumber});
+
+  my $data = $self->connector->get("http://$url/api/articles/$part->{partnumber}?useNumberAsId=true");
+  my $data_json = $data->content;
+  my $import = SL::JSON::decode_json($data_json);
+  $main::lxdebug->dump(0, 'WH: IMPORT', \$import);
+
+  my $shop_data =  {  name        => $part->{description} ,
+                      taxId       => 1 ,
+                      mainDetail  => { number => $part->{partnumber}  ,
+                                        test        => 'test' ,
+                                     }
+                                 }
+                  ;
+#$main::lxdebug->dump(0, 'WH: SHOPDATA',\%shop_data);
+my $dataString = SL::JSON::encode_json($shop_data);
+$main::lxdebug->dump(0, 'WH: JSONDATA2',$dataString);
+#my $daten = SL::JSON::decode_json($dataString);
+#$dataString =~ s/{|}//g;
+#$dataString = "{".$dataString."}";
+#my $json_data      = SL::JSON::to_json($shop_data);
+#$main::lxdebug->dump(0, 'WH: JSONDATA3',$daten);
+#my $dataString2 = JSON::encode_json($shop_data);
+#$main::lxdebug->dump(0, 'WH: JSONDATA4',$dataString2);
+#$main::lxdebug->message(0, "WH: isuccess: ". $import->{success});
+my $json = '{"name": "foo", "taxId": 1}';
+  if($import->{success}){
+    #update
+$main::lxdebug->message(0, "WH: if success: ". $import->{success});
+  }else{
+    #upload
+$main::lxdebug->message(0, "WH: else success: ". $import->{success});
+    my $upload = $self->connector->post("http://$url/api/articles/",Content => $dataString);
+$main::lxdebug->dump(0, "WH:2 else success: ", \$upload);
+  }
+
 }
 
 sub init_url {
diff --git a/js/kivi.shop_part.js b/js/kivi.shop_part.js
index c7c7b0a7f..fe1f8a25c 100644
--- a/js/kivi.shop_part.js
+++ b/js/kivi.shop_part.js
@@ -46,7 +46,7 @@ namespace('kivi.shop_part', function(ns) {
     var form = $('form').serializeArray();
     form.push( { name: 'action', value: 'ShopPart/update' }
     );
-
+alert(form.join('\n'));
     $.post('controller.pl', form, function(data) {
       kivi.eval_json_result(data);
     });
diff --git a/templates/webpages/ic/tabs/_shop.html b/templates/webpages/ic/tabs/_shop.html
index 6b2b2c8b0..a8b4d19dd 100644
--- a/templates/webpages/ic/tabs/_shop.html
+++ b/templates/webpages/ic/tabs/_shop.html
@@ -41,6 +41,7 @@
 [% # L.dump(shop_part) %]
 <hr>
   [%- IF CUSTOM_VARIABLES.size %]
+[%  # L.dump(CUSTOM_VARIABLES) %]
   <div id="shop_custom_variables">
    <p>
     <table>
diff --git a/templates/webpages/shop_part/categories.html b/templates/webpages/shop_part/categories.html
index 847de3398..8489ea6f8 100644
--- a/templates/webpages/shop_part/categories.html
+++ b/templates/webpages/shop_part/categories.html
@@ -28,6 +28,7 @@
   [% END %]
   <div><h2>[% LxERP.t8("Shopcategories") %]</h2>
       [% FOREACH row = CATEGORIES %]
+<!-- TODO: Is still hardcoded 'Root' is shopware specified -->
         [% IF row.name == 'Root' %]
           [% IF row.childrenCount >= 1 %]
             [% INCLUDE recurse data=row.children %]
