1 package SL::Controller::ShopPart;
 
   5 use parent qw(SL::Controller::Base);
 
   7 use SL::BackgroundJob::ShopPartMassUpload;
 
   8 use SL::System::TaskServer;
 
  10 use SL::Locale::String qw(t8);
 
  14 use SL::DB::ShopImage;
 
  16 use SL::Helper::Flash;
 
  17 use SL::Controller::Helper::ParseFilter;
 
  20 use Rose::Object::MakeMethods::Generic
 
  22    scalar                 => [ qw(price_sources) ],
 
  23   'scalar --get_set_init' => [ qw(shop_part file shops) ],
 
  26 __PACKAGE__->run_before('check_auth');
 
  27 __PACKAGE__->run_before('add_javascripts', only => [ qw(edit_popup list_articles) ]);
 
  28 __PACKAGE__->run_before('load_pricesources',    only => [ qw(create_or_edit_popup) ]);
 
  34 sub action_create_or_edit_popup {
 
  37   $self->render_shop_part_edit_dialog();
 
  40 sub action_update_shop {
 
  41   my ($self, %params) = @_;
 
  43   my $shop_part = SL::DB::Manager::ShopPart->find_by(id => $::form->{shop_part_id});
 
  44   die unless $shop_part;
 
  47   my $shop = SL::Shop->new( config => $shop_part->shop );
 
  49   my $connect = $shop->check_connectivity;
 
  50   if($connect->{success}){
 
  51     my $return    = $shop->connector->update_part($self->shop_part, 'all');
 
  53     # the connector deals with parsing/result verification, just needs to return success or failure
 
  55       my $now = DateTime->now;
 
  56       my $attributes->{last_update} = $now;
 
  57       $self->shop_part->assign_attributes(%{ $attributes });
 
  58       $self->shop_part->save;
 
  59       $self->js->html('#shop_part_last_update_' . $shop_part->id, $now->to_kivitendo('precision' => 'minute'))
 
  60              ->flash('info', t8("Updated part [#1] in shop [#2] at #3", $shop_part->part->displayable_name, $shop_part->shop->description, $now->to_kivitendo('precision' => 'minute') ) )
 
  63       $self->js->flash('error', t8('The shop part wasn\'t updated.'))->render;
 
  66     $self->js->flash('error', t8('The shop part wasn\'t updated. #1', $connect->{data}->{version}))->render;
 
  72 sub action_show_files {
 
  75   my $images = SL::DB::Manager::ShopImage->get_all( where => [ 'files.object_id' => $::form->{id}, ], with_objects => 'file', sort_by => 'position' );
 
  77   $self->render('shop_part/_list_images', { header => 0 }, IMAGES => $images);
 
  80 sub action_ajax_delete_file {
 
  85     ->run('kivi.ShopPart.show_images',$self->file->object_id)
 
  89 sub action_get_categories {
 
  93   my $shop = SL::Shop->new( config => $self->shop_part->shop );
 
  95   my $connect = $shop->check_connectivity;
 
  96   if($connect->{success}){
 
  97     my $categories = $shop->connector->get_categories;
 
 101         'kivi.ShopPart.shop_part_dialog',
 
 102         t8('Shopcategories'),
 
 103         $self->render('shop_part/categories', { output => 0 }, CATEGORIES => $categories )
 
 108     $self->js->flash('error', t8('Can\'t connect to shop. #1', $connect->{data}->{version}))->render;
 
 113 sub action_show_price_n_pricesource {
 
 116   my ( $price, $price_src_str ) = $self->get_price_n_pricesource($::form->{pricesource});
 
 118   if( $price_src_str eq 'sellprice'){
 
 119     $price_src_str = t8('Sellprice');
 
 120   }elsif( $price_src_str eq 'listprice'){
 
 121     $price_src_str = t8('Listprice');
 
 122   }elsif( $price_src_str eq 'lastcost'){
 
 123     $price_src_str = t8('Lastcost');
 
 125   $self->js->html('#price_' . $self->shop_part->id, $::form->format_amount(\%::myconfig,$price,2))
 
 126            ->html('#active_price_source_' . $self->shop_part->id, $price_src_str)
 
 130 sub action_show_stock {
 
 132   my ( $stock_local, $stock_onlineshop, $active_online );
 
 135   my $shop = SL::Shop->new( config => $self->shop_part->shop );
 
 137   if($self->shop_part->last_update) {
 
 138     my $shop_article = $shop->connector->get_article($self->shop_part->part->partnumber);
 
 139     $stock_onlineshop = $shop_article->{data}->{mainDetail}->{inStock};
 
 140     $active_online = $shop_article->{data}->{active};
 
 143   $stock_local = $self->shop_part->part->onhand;
 
 145   $self->js->html('#stock_' . $self->shop_part->id, $::form->format_amount(\%::myconfig,$stock_local,0)."/".$::form->format_amount(\%::myconfig,$stock_onlineshop,0))
 
 146            ->html('#toogle_' . $self->shop_part->id,$active_online)
 
 150 sub action_get_n_write_categories {
 
 153   my @shop_parts =  @{ $::form->{shop_parts_ids} || [] };
 
 154   foreach my $part(@shop_parts){
 
 156     my $shop_part = SL::DB::Manager::ShopPart->get_all( where => [id => $part], with_objects => ['part', 'shop'])->[0];
 
 157     require SL::DB::Shop;
 
 158     my $shop = SL::Shop->new( config => $shop_part->shop );
 
 159     my $online_article = $shop->connector->get_article($shop_part->part->partnumber);
 
 160     my $online_cat = $online_article->{data}->{categories};
 
 162     for(keys %$online_cat){
 
 164       push @cattmp,$online_cat->{$_}->{id};
 
 165       push @cattmp,$online_cat->{$_}->{name};
 
 168     my $attributes->{shop_category} = \@cat;
 
 169     my $active->{active} = $online_article->{data}->{active};
 
 170     $shop_part->assign_attributes(%{$attributes}, %{$active});
 
 173   $self->redirect_to( action => 'list_articles' );
 
 176 sub action_save_categories {
 
 179   my @categories =  @{ $::form->{categories} || [] };
 
 182     foreach my $cat ( @categories) {
 
 184       push( @cattmp,$cat );
 
 185       push( @cattmp,$::form->{"cat_id_${cat}"} );
 
 186       push( @cat,\@cattmp );
 
 189   my $categories->{shop_category} = \@cat;
 
 191   my $params = delete($::form->{shop_part}) || { };
 
 193   $self->shop_part->assign_attributes(%{ $params });
 
 194   $self->shop_part->assign_attributes(%{ $categories });
 
 196   $self->shop_part->save;
 
 198   flash('info', t8('The categories has been saved.'));
 
 200   $self->js->run('kivi.ShopPart.close_dialog')
 
 201            ->flash('info', t8("Updated categories"))
 
 207   require SL::DB::ShopImage;
 
 208   SL::DB::ShopImage->reorder_list(@{ $::form->{image_id} || [] });
 
 210   $self->render(\'', { type => 'json' });
 
 213 sub action_list_articles {
 
 216   my %filter      = ($::form->{filter} ? parse_filter($::form->{filter}) : query => [ 'shop.obsolete' => 0 ]);
 
 217   my $sort_by     = $::form->{sort_by} ? $::form->{sort_by} : 'part.partnumber';
 
 218   $sort_by .=$::form->{sort_dir} ? ' DESC' : ' ASC';
 
 220   my $articles = SL::DB::Manager::ShopPart->get_all( %filter ,with_objects => [ 'part','shop' ], sort_by => $sort_by );
 
 222   foreach my $article (@{ $articles}) {
 
 223     my $images = SL::DB::Manager::ShopImage->get_all_count( where => [ 'files.object_id' => $article->part->id, ], with_objects => 'file', sort_by => 'position' );
 
 224     $article->{images} = $images;
 
 227   $self->render('shop_part/_list_articles', title => t8('Webshops articles'), SHOP_PARTS => $articles);
 
 230 sub action_upload_status {
 
 232   my $job     = SL::DB::BackgroundJob->new(id => $::form->{job_id})->load;
 
 233   my $html    = $self->render('shop_part/_upload_status', { output => 0 }, job => $job);
 
 235   $self->js->html('#status_mass_upload', $html);
 
 236   $self->js->run('kivi.ShopPart.massUploadFinished') if $job->data_as_hash->{status} == SL::BackgroundJob::ShopPartMassUpload->DONE();
 
 240 sub action_mass_upload {
 
 243   my @shop_parts =  @{ $::form->{shop_parts_ids} || [] };
 
 245   my $job = SL::DB::BackgroundJob->new(
 
 248         package_name         => 'ShopPartMassUpload',
 
 250         shop_part_record_ids => [ @shop_parts ],
 
 251         todo                 => $::form->{upload_todo},
 
 252         status               => SL::BackgroundJob::ShopPartMassUpload->WAITING_FOR_EXECUTION(),
 
 255    )->update_next_run_at;
 
 257    SL::System::TaskServer->new->wake_up;
 
 259    my $html = $self->render('shop_part/_upload_status', { output => 0 }, job => $job);
 
 262       ->html('#status_mass_upload', $html)
 
 263       ->run('kivi.ShopPart.massUploadStarted')
 
 269   $self->create_or_update;
 
 272 sub render_shop_part_edit_dialog {
 
 277       'kivi.ShopPart.shop_part_dialog',
 
 279       $self->render('shop_part/edit', { output => 0 })
 
 286 sub create_or_update {
 
 289   my $is_new = !$self->shop_part->id;
 
 290   my $params = delete($::form->{shop_part}) || { };
 
 291   $self->shop_part->assign_attributes(%{ $params });
 
 292   $self->shop_part->save;
 
 294   my ( $price, $price_src_str ) = $self->get_price_n_pricesource($self->shop_part->active_price_source);
 
 297     flash('info', $is_new ? t8('The shop part has been created.') : t8('The shop part has been saved.'));
 
 298     $self->js->html('#shop_part_description_' . $self->shop_part->id, $self->shop_part->shop_description)
 
 299            ->html('#shop_part_active_' . $self->shop_part->id, $self->shop_part->active)
 
 300            ->html('#price_' . $self->shop_part->id, $::form->format_amount(\%::myconfig,$price,2))
 
 301            ->html('#active_price_source_' . $self->shop_part->id, $price_src_str)
 
 302            ->run('kivi.ShopPart.close_dialog')
 
 303            ->flash('info', t8("Updated shop part"))
 
 306     $self->redirect_to(controller => 'Part', action => 'edit', 'part.id' => $self->shop_part->part_id);
 
 313 sub add_javascripts  {
 
 314   $::request->{layout}->add_javascripts(qw(kivi.ShopPart.js));
 
 317 sub load_pricesources {
 
 321   push( @{ $pricesources } , { id => "master_data/sellprice", name => t8("Master Data")." - ".t8("Sellprice") },
 
 322                              { id => "master_data/listprice", name => t8("Master Data")." - ".t8("Listprice") },
 
 323                              { id => "master_data/lastcost",  name => t8("Master Data")." - ".t8("Lastcost") }
 
 325   my $pricegroups = SL::DB::Manager::Pricegroup->get_all;
 
 326   foreach my $pg ( @$pricegroups ) {
 
 327     push( @{ $pricesources } , { id => "pricegroup/".$pg->id, name => t8("Pricegroup") . " - " . $pg->pricegroup} );
 
 330   $self->price_sources( $pricesources );
 
 333 sub get_price_n_pricesource {
 
 334   my ($self,$pricesource) = @_;
 
 336   my ( $price_src_str, $price_src_id ) = split(/\//,$pricesource);
 
 338   require SL::DB::Pricegroup;
 
 339   require SL::DB::Part;
 
 341   if ($price_src_str eq "master_data") {
 
 342     my $part       = SL::DB::Manager::Part->find_by( id => $self->shop_part->part_id );
 
 343     $price         = $part->$price_src_id;
 
 344     $price_src_str = $price_src_id;
 
 346     my $part       = SL::DB::Manager::Part->get_all( where => [id => $self->shop_part->part_id, 'prices.'.pricegroup_id => $price_src_id], with_objects => ['prices'],limit => 1)->[0];
 
 347     #my $part       = SL::DB::Manager::Part->find_by( id => $self->shop_part->part_id, 'prices.'.pricegroup_id => $price_src_id );
 
 348     my $pricegrp   = SL::DB::Manager::Pricegroup->find_by( id => $price_src_id )->pricegroup;
 
 349     $price         = $part->prices->[0]->price;
 
 350     $price_src_str = $pricegrp;
 
 352   return($price,$price_src_str);
 
 356   $::auth->assert('shop_part_edit');
 
 360   if ($::form->{shop_part_id}) {
 
 361     SL::DB::Manager::ShopPart->find_by(id => $::form->{shop_part_id});
 
 363     SL::DB::ShopPart->new(shop_id => $::form->{shop_id}, part_id => $::form->{part_id});
 
 368   my $file = $::form->{id} ? SL::DB::File->new(id => $::form->{id})->load : SL::DB::File->new;
 
 373   SL::DB::Shop->shops_dd;
 
 385 SL::Controller::ShopPart - Controller for managing ShopParts
 
 389 ShopParts are configured in a tab of the corresponding part.
 
 395 =item C<action_update_shop>
 
 397 To be called from the "Update" button of the shoppart, for manually syncing/upload one part with its shop. Calls some ClientJS functions to modifiy original page.
 
 399 =item C<action_show_files>
 
 403 =item C<action_ajax_delete_file>
 
 407 =item C<action_get_categories>
 
 411 =item C<action_show_price_n_pricesource>
 
 415 =item C<action_show_stock>
 
 419 =item C<action_get_n_write_categories>
 
 421 Can be used to sync the categories of a shoppart with the categories from online.
 
 423 =item C<action_save_categories>
 
 425 The ShopwareConnector works with the CategoryID @categories[x][0] in others/new Connectors it must be tested
 
 426 Each assigned categorie is saved with id,categorie_name an multidimensional array and could be expanded with categoriepath or what is needed
 
 428 =item C<action_reorder>
 
 432 =item C<action_upload_status>
 
 436 =item C<action_mass_upload>
 
 440 =item C<action_update>
 
 444 =item C<create_or_update>
 
 448 =item C<render_shop_part_edit_dialog>
 
 450 when self->shop_part is called in template, it will be an existing shop_part with id,
 
 451 or a new shop_part with only part_id and shop_id set
 
 453 =item C<add_javascripts>
 
 456 =item C<load_pricesources>
 
 458 the price sources to use for the article: sellprice, lastcost,
 
 459 listprice, or one of the pricegroups. It overwrites the default pricesource from the shopconfig.
 
 460 TODO: implement valid pricerules for the article
 
 462 =item C<get_price_n_pricesource>
 
 468 =item C<init_shop_part>
 
 476 data for drop down filter options
 
 483 Pricesrules, pricessources aren't fully implemented yet.
 
 487 G. Richardson E<lt>information@kivitendo-premium.deE<gt>
 
 488 W. Hahn E<lt>wh@futureworldsearch.netE<gt>