commit 3e234947004a2232815577842e46ee912de85cb9
Author: Werner Hahn <wh@futureworldsearch.net>
Date:   Tue Apr 19 20:00:17 2016 +0200

    Shopmodul: Kategorien werden in die DB geschrieben
    
    Conflicts:
            SL/DB/MetaSetup/ShopPart.pm
    
    Shoppart: Shopkategorien sind zugeordnet und werden mit checkbox angezeigt
    
    Kategorien werden mit ID und Name in die DB geschrieben. Ein Komma im Name erzeugt noch Fehler. Der Pfad muss noch gemacht werden
    
    Conflicts:
            SL/ShopConnector/Shopware.pm
    
    ShopPart: ArtikelKategorien werden über ID in den Shop dem Artikle zugeordnet

diff --git a/SL/Controller/ShopPart.pm b/SL/Controller/ShopPart.pm
index eb85d80b7..d04194a7a 100644
--- a/SL/Controller/ShopPart.pm
+++ b/SL/Controller/ShopPart.pm
@@ -56,31 +56,22 @@ sub action_update_shop {
 
 sub action_show_files {
   my ($self) = @_;
-$main::lxdebug->dump(0, 'WH: Show_Files',\$::form);
+
   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' );
-  $main::lxdebug->dump(0, 'WH: ',\$images);
-  #my $html = $self->render('shop_part/_list_images', { output => 0 }, IMAGES => $images);
-  $self->render('shop_part/_list_images', { header => 0 }, IMAGES => $images);
-  #$main::lxdebug->dump(0, 'WH: ',\$html);
-
-  #$self->js->html('#shop_images', $html);
-  #$self->js->render;
-
-  # $self->render('customer_vendor_turnover/count_open_items_by_year', { layout => 0 });
 
+  $self->render('shop_part/_list_images', { header => 0 }, IMAGES => $images);
 
 }
 
 sub action_get_categories {
   my ($self) = @_;
 
-  my $shop_part = SL::DB::Manager::ShopPart->find_by(id => $::form->{shop_part_id});
-  die unless $shop_part;
+#  my $shop_part = SL::DB::Manager::ShopPart->find_by(id => $::form->{shop_part_id});
+#  die unless $shop_part;
   require SL::Shop;
-  my $shop = SL::Shop->new( config => $shop_part->shop );
+  my $shop = SL::Shop->new( config => $self->shop_part->shop );
   my $categories = $shop->connector->get_categories;
-  $main::lxdebug->dump(0, 'WH: GET',\$categories);
 
   $self->js
     ->run(
@@ -145,21 +136,35 @@ sub render_shop_part_edit_dialog {
   $self->js->render;
 }
 
-sub render_shop_categories_edit_dialog {
+sub action_save_categories {
   my ($self) = @_;
 
-  # when self->shop_part is called in template, it will be an existing shop_part with id,
-  # or a new shop_part with only part_id and shop_id set
-  $self->js
-    ->run(
-      'kivi.shop_part.shop_part_dialog',
-      t8('Shopcategories'),
-      $self->render('shop_part/categories', { output => 0 }) #, shop_part => $self->shop_part)
-    )
-    ->reinit_widgets;
+  my @categories =  @{ $::form->{categories} || [] };
+  $main::lxdebug->dump(0, 'WH: KATEGORIEN: ', \@categories);
+  my @cat = ();
+  foreach my $cat ( @categories) {
+    # TODO das koma macht Probleme z.B kategorie "Feldsalat, Rapunzel"
+    my @temp = [split(/,/,$cat)];
+    push( @cat, @temp );
+  }
+  $main::lxdebug->dump(0, 'WH: KAT2:',\@cat);
 
-  $self->js->render;
+  my $categories->{shop_category} = \@cat;
+
+  my $params = delete($::form->{shop_part}) || { };
+
+  $self->shop_part->assign_attributes(%{ $params });
+  $self->shop_part->assign_attributes(%{ $categories });
+
+  $self->shop_part->save;
+
+  flash('info', t8('The categories has been saved.'));
+
+  $self->js->run('kivi.shop_part.close_dialog')
+           ->flash('info', t8("Updated categories"))
+           ->render;
 }
+
 sub action_reorder {
   my ($self) = @_;
 $main::lxdebug->message(0, "WH:REORDER ");
diff --git a/SL/DB/ShopPart.pm b/SL/DB/ShopPart.pm
index ee82732c8..f8b29cd4a 100644
--- a/SL/DB/ShopPart.pm
+++ b/SL/DB/ShopPart.pm
@@ -9,7 +9,7 @@ use strict;
 use SL::DB::MetaSetup::ShopPart;
 use SL::DB::Manager::ShopPart;
 use SL::DB::Helper::AttrHTML;
-use SL::DB::Helper::ActsAsList;
+#use SL::DB::Helper::ActsAsList;
 
 __PACKAGE__->meta->initialize;
 __PACKAGE__->attr_html('shop_description');
diff --git a/SL/ShopConnector/Shopware.pm b/SL/ShopConnector/Shopware.pm
index 732f4e0e4..e4b078c5a 100644
--- a/SL/ShopConnector/Shopware.pm
+++ b/SL/ShopConnector/Shopware.pm
@@ -173,10 +173,115 @@ sub get_categories {
 sub get_article {
 }
 
-sub get_articles {
-}
+sub update_part {
+  my ($self, $shop_part, $json) = @_;
+
+  #shop_part is passed as a param
+  die unless ref($shop_part) eq 'SL::DB::ShopPart';
+
+  $main::lxdebug->dump(0, 'WH: UPDATE SHOPPART: ', \$shop_part);
+  $main::lxdebug->dump(0, 'WH: UPDATE SELF: ', \$self);
+  $main::lxdebug->dump(0, 'WH: UPDATE JSON: ', \$json);
+  my $url = $self->url;
+  my $part = SL::DB::Part->new(id => $shop_part->{part_id})->load;
+  #my $part = $shop_part->part;
+  $main::lxdebug->dump(0, 'WH: Part',\$part);
+
+  # TODO: Prices (pricerules, pricegroups,
+  my $cvars = { map { ($_->config->name => { value => $_->value_as_text, is_valid => $_->is_valid }) } @{ $part->cvars_by_config } };
+  #my $categories = { map { ( name => $_) } @{ $shop_part->{shop_category} } };
+  my @cat = ();
+  foreach my $row_cat ( @{ $shop_part->shop_category } ) {
+    $main::lxdebug->dump(0, 'WH:ROWCAT ',\$row_cat);
+
+    my $temp = { ( id => @{$row_cat}[0], ) };
+    $main::lxdebug->dump(0, 'WH: TEMP: ', \$temp);
+
+    push ( @cat, $temp );
+    #push ( @cat, map { ( name => $_[1]) } @{ $row_cat } );
+  }
+  $main::lxdebug->dump(0, 'WH: CATEGORIES',\@cat);
+  my $images = SL::DB::Manager::File->get_all( where => [ modul => 'shop_part', trans_id => $part->{id} ]);
+  $main::lxdebug->dump(0, 'WH: IMAGES',\@{ $images } );
+  my $images2 = { map {
+                    ( link        => 'data:' . $_->{file_content_type} . ';base64,' . MIME::Base64::encode($_->{file_content},''),
+                      description => $_->{title},
+                      position    => $_->{position},
+                      extension   => 'jpg', # muss $extionsion sein
+                      path        => $_->{filename}, # muss $path sein
+                    ) } @{ $images } };
+  $main::lxdebug->dump(0, 'WH: IMAGES 2 ',\$images2);
+
+  my @images3 = ();
+  foreach my $img (@{ $images }) {
+    $main::lxdebug->dump(0, 'WH: FOR: ', \$img);
+
+    my ($path, $extension) = (split /\./, $img->{filename});
+    $main::lxdebug->message(0, "WH: PATH: $path Ext: $extension");
+
+    my $temp ={
+                      ( link        => 'data:' . $img->{file_content_type} . ';base64,' . MIME::Base64::encode($img->{file_content},''),
+                        description => $img->{title},
+                        position    => $img->{position},
+                        extension   => $extension,
+                        path        => $path,
+                      )}    ;
+    push( @images3, $temp);
+  }
+  $main::lxdebug->dump(0, 'WH: IMAGES 3 ',\@images3);
+
+  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);
+  $main::lxdebug->dump(0, 'WH: Active', $shop_part->active);
+
+
+  my %shop_data =  (  name          => $part->{description},
+                      taxId         => 4, # TODO Hardcoded kann auch der taxwert sein zB. tax => 19.00
+                      mainDetail    => { number   => $part->{partnumber},
+                                         inStock  => $part->{onhand},
+                                         prices   =>  [ {          from   => 1,
+                                                                   price  => $part->{sellprice},
+                                                        customerGroupKey  => 'EK',
+                                                      },
+                                                    ],
+                                       },
+                      supplier      => $cvars->{freifeld_7}->{value},
+                      description   => $shop_part->{shop_description},
+                      active        => $shop_part->active,
+                      images        => [ @images3 ],
+                      __options_images => { replace => 1, },
+                      categories    => [ @cat ], #{ path => 'Deutsch|test2' }, ], #[ $categories ],
+
+                    )
+                  ;
+$main::lxdebug->dump(0, 'WH: SHOPDATA', \%shop_data );
+my $dataString = SL::JSON::to_json(\%shop_data);
+$dataString = encode_utf8($dataString);
+$main::lxdebug->message(0, 'WH: JSONDATA2 '.$dataString);
+  if($import->{success}){
+$main::lxdebug->message(0, "WH: if success: ". $import->{success});
+  my %del_img =  ( images        => [ {} ], ) ;
+  my $del_imgString = SL::JSON::to_json(\%del_img);
+  #my $delImg = $self->connector->put("http://$url/api/articles/$part->{partnumber}?useNumberAsId=true",Content => $del_imgString);
+    #update
+    my $upload = $self->connector->put("http://$url/api/articles/$part->{partnumber}?useNumberAsId=true",Content => $dataString);
+    my $data_json = $upload->content;
+    my $upload_content = SL::JSON::decode_json($data_json);
+$main::lxdebug->dump(0, "WH:2 else success: ", \$upload);
+    return $upload_content->{success};
+  }else{
+    #upload
+$main::lxdebug->message(0, "WH: else success: ". $import->{success});
+    my $upload = $self->connector->post("http://$url/api/articles/",Content => $dataString);
+    my $data_json = $upload->content;
+    my $upload_content = SL::JSON::decode_json($data_json);
+$main::lxdebug->dump(0, "WH:2 else success: ", \$upload);
+    return $upload_content->{success};
+  }
 
-sub set_article {
+>>>>>>> b616804... Kategorien werden mit ID und Name in die DB geschrieben. Ein Komma im Name erzeugt noch Fehler. Der Pfad muss noch gemacht werden
 }
 
 sub init_url {
diff --git a/sql/Pg-upgrade2/shop_parts_2.sql b/sql/Pg-upgrade2/shop_parts_2.sql
new file mode 100644
index 000000000..988124192
--- /dev/null
+++ b/sql/Pg-upgrade2/shop_parts_2.sql
@@ -0,0 +1,6 @@
+-- @tag: shop_parts_2
+-- @description: Add tables for part information for shop
+-- @charset: UTF-8
+-- @depends: release_3_3_0 shops
+-- @ignore: 0
+ALTER TABLE shop_parts ALTER COLUMN shop_category TYPE TEXT[] USING array[shop_category];
diff --git a/templates/webpages/shop_part/categories.html b/templates/webpages/shop_part/categories.html
new file mode 100644
index 000000000..df3a559e7
--- /dev/null
+++ b/templates/webpages/shop_part/categories.html
@@ -0,0 +1,54 @@
+[%- USE HTML %]
+[%- USE T8 %]
+[%- USE L -%]
+[%- USE P -%]
+[%- USE LxERP -%]
+[%- USE Dumper -%]
+
+[%  LxERP.t8("Part") %]: [% HTML.escape(SELF.shop_part.part.displayable_name) %]<br>
+[%  LxERP.t8("Shop") %]: [% HTML.escape(SELF.shop_part.shop.description) %]
+<br>
+[% # Dumper.dump_html(SELF.shop_part.shop_category) %]
+<br>
+[% # Dumper.dump_html(CATEGORIES) %]
+
+<form action="controller.pl" method="post">
+  [% BLOCK recurse %]
+  [% # path = '' %]<!-- TODO: Pfad wg neuer Kategorie im Shop anlegen -->
+    [% FOREACH obj = data %]
+      <ul>
+        <li>
+        [% checked = '' %]
+        [% # path = path _ obj.name %]
+        [% # test = path.split('\|') %]
+        [% # Dumper.dump_html(SELF.shop_part.shop_category.size) %]
+        [% IF SELF.shop_part.shop_category.1.grep(obj.name).size %]
+          [% checked = 'checked' %]
+        [% ELSE %]
+          [% FOREACH cat_row = SELF.shop_part.shop_category %]
+            [% IF cat_row.1.grep(obj.name).size %]
+              [% checked = 'checked' %]
+            [% END %]
+          [% END %]
+        [% END %]
+          [% L.checkbox_tag('categories[]',value=obj.id _"," _ obj.name _ ",PFAD", checked=checked) %][% HTML.escape(obj.name) %]</li>
+        [% IF obj.childrenCount >= 1 %]
+          [% # path = path _ '|' %]
+          [% INCLUDE recurse data=obj.children %]
+        [% END %]
+      </ul>
+    [% END %]
+  [% END %]
+  <div><h2>[% LxERP.t8("Shopcategories") %]</h2>
+      [% FOREACH row = CATEGORIES %]
+        [% IF row.name == 'Root' %]
+          [% IF row.childrenCount >= 1 %]
+            [% path = '' %]
+            [% INCLUDE recurse data=row.children path=path %]
+          [% END %]
+        [% END %]
+      [% END %]
+  </div>
+    [% L.button_tag("kivi.shop_part.save_categories(" _ SELF.shop_part.id _", " _ SELF.shop_part.shop.id _")", LxERP.t8("Save"))  %]</td>
+</form>
+
diff --git a/templates/webpages/shop_part/edit.html b/templates/webpages/shop_part/edit.html
index 0929bc77b..333fd3358 100644
--- a/templates/webpages/shop_part/edit.html
+++ b/templates/webpages/shop_part/edit.html
@@ -42,10 +42,6 @@
      <td>[% LxERP.t8("Meta Tags") %]</td>
      <td>[% L.input_tag("shop_part.meta_tags", SELF.shop_part.meta_tags, size=2) %]</td>
     </tr>
-    <tr>
-     <td>[% LxERP.t8("Shop Category") %]</td>
-     <td>[% L.input_tag("shop_part.shop_category", SELF.shop_part.category, size=2) %]</td>
-    </tr>
     </table>
     [% # L.dump(SELF.shop_part) %]
 
