marei: headline/colspec keys for SimpleTabular
[kivitendo-erp.git] / SL / Controller / ShopPart.pm
1 package SL::Controller::ShopPart;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::BackgroundJob::ShopPartMassUpload;
8 use SL::System::TaskServer;
9 use Data::Dumper;
10 use SL::Locale::String qw(t8);
11 use SL::DB::ShopPart;
12 use SL::DB::Shop;
13 use SL::DB::File;
14 use SL::DB::ShopImage;
15 use SL::DB::Default;
16 use SL::Helper::Flash;
17 use SL::Controller::Helper::ParseFilter;
18 use MIME::Base64;
19
20 use Rose::Object::MakeMethods::Generic
21 (
22    scalar                 => [ qw(price_sources) ],
23   'scalar --get_set_init' => [ qw(shop_part file shops) ],
24 );
25
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) ]);
29
30 #
31 # actions
32 #
33
34 sub action_create_or_edit_popup {
35   my ($self) = @_;
36
37   $self->render_shop_part_edit_dialog();
38 }
39
40 sub action_update_shop {
41   my ($self, %params) = @_;
42
43   my $shop_part = SL::DB::Manager::ShopPart->find_by(id => $::form->{shop_part_id});
44   die unless $shop_part;
45
46   require SL::Shop;
47   my $shop = SL::Shop->new( config => $shop_part->shop );
48
49   my $connect = $shop->check_connectivity;
50   if($connect->{success}){
51     my $return    = $shop->connector->update_part($self->shop_part, 'all');
52
53     # the connector deals with parsing/result verification, just needs to return success or failure
54     if ( $return == 1 ) {
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') ) )
61              ->render;
62     } else {
63       $self->js->flash('error', t8('The shop part wasn\'t updated.'))->render;
64     }
65   }else{
66     $self->js->flash('error', t8('The shop part wasn\'t updated. #1', $connect->{data}->{version}))->render;
67   }
68
69
70 }
71
72 sub action_show_files {
73   my ($self) = @_;
74
75   my $images = SL::DB::Manager::ShopImage->get_all( where => [ 'files.object_id' => $::form->{id}, ], with_objects => 'file', sort_by => 'position' );
76
77   $self->render('shop_part/_list_images', { header => 0 }, IMAGES => $images);
78 }
79
80 sub action_ajax_delete_file {
81   my ( $self ) = @_;
82   $self->file->delete;
83
84   $self->js
85     ->run('kivi.ShopPart.show_images',$self->file->object_id)
86     ->render();
87 }
88
89 sub action_get_categories {
90   my ($self) = @_;
91
92   require SL::Shop;
93   my $shop = SL::Shop->new( config => $self->shop_part->shop );
94
95   my $connect = $shop->check_connectivity;
96   if($connect->{success}){
97     my $categories = $shop->connector->get_categories;
98
99     $self->js
100       ->run(
101         'kivi.ShopPart.shop_part_dialog',
102         t8('Shopcategories'),
103         $self->render('shop_part/categories', { output => 0 }, CATEGORIES => $categories )
104       )
105       ->reinit_widgets;
106       $self->js->render;
107   }else{
108     $self->js->flash('error', t8('Can\'t connect to shop. #1', $connect->{data}->{version}))->render;
109   }
110
111 }
112
113 sub action_show_price_n_pricesource {
114   my ($self) = @_;
115
116   my ( $price, $price_src_str ) = $self->get_price_n_pricesource($::form->{pricesource});
117
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');
124   }
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)
127            ->render;
128 }
129
130 sub action_show_stock {
131   my ($self) = @_;
132   my ( $stock_local, $stock_onlineshop, $active_online );
133
134   require SL::Shop;
135   my $shop = SL::Shop->new( config => $self->shop_part->shop );
136
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};
141   }
142
143   $stock_local = $self->shop_part->part->onhand;
144
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)
147            ->render;
148 }
149
150 sub action_get_n_write_categories {
151   my ($self) = @_;
152
153   my @shop_parts =  @{ $::form->{shop_parts_ids} || [] };
154   foreach my $part(@shop_parts){
155
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};
161     my @cat = ();
162     for(keys %$online_cat){
163       my @cattmp;
164       push @cattmp,$online_cat->{$_}->{id};
165       push @cattmp,$online_cat->{$_}->{name};
166       push @cat,\@cattmp;
167     }
168     my $attributes->{shop_category} = \@cat;
169     my $active->{active} = $online_article->{data}->{active};
170     $shop_part->assign_attributes(%{$attributes}, %{$active});
171     $shop_part->save;
172   }
173   $self->redirect_to( action => 'list_articles' );
174 }
175
176 sub action_save_categories {
177   my ($self) = @_;
178
179   my @categories =  @{ $::form->{categories} || [] };
180
181     my @cat = ();
182     foreach my $cat ( @categories) {
183       my @cattmp;
184       push( @cattmp,$cat );
185       push( @cattmp,$::form->{"cat_id_${cat}"} );
186       push( @cat,\@cattmp );
187     }
188
189   my $categories->{shop_category} = \@cat;
190
191   my $params = delete($::form->{shop_part}) || { };
192
193   $self->shop_part->assign_attributes(%{ $params });
194   $self->shop_part->assign_attributes(%{ $categories });
195
196   $self->shop_part->save;
197
198   flash('info', t8('The categories has been saved.'));
199
200   $self->js->run('kivi.ShopPart.close_dialog')
201            ->flash('info', t8("Updated categories"))
202            ->render;
203 }
204
205 sub action_reorder {
206   my ($self) = @_;
207   require SL::DB::ShopImage;
208   SL::DB::ShopImage->reorder_list(@{ $::form->{image_id} || [] });
209
210   $self->render(\'', { type => 'json' });
211 }
212
213 sub action_list_articles {
214   my ($self) = @_;
215
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';
219
220   my $articles = SL::DB::Manager::ShopPart->get_all( %filter ,with_objects => [ 'part','shop' ], sort_by => $sort_by );
221
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;
225   }
226
227   $self->render('shop_part/_list_articles', title => t8('Webshops articles'), SHOP_PARTS => $articles);
228 }
229
230 sub action_upload_status {
231   my ($self) = @_;
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);
234
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();
237   $self->js->render;
238 }
239
240 sub action_mass_upload {
241   my ($self) = @_;
242
243   my @shop_parts =  @{ $::form->{shop_parts_ids} || [] };
244
245   my $job = SL::DB::BackgroundJob->new(
246         type                 => 'once',
247         active               => 1,
248         package_name         => 'ShopPartMassUpload',
249         )->set_data(
250         shop_part_record_ids => [ @shop_parts ],
251         todo                 => $::form->{upload_todo},
252         status               => SL::BackgroundJob::ShopPartMassUpload->WAITING_FOR_EXECUTION(),
253         conversation         => [ ],
254         num_uploaded         => 0,
255    )->update_next_run_at;
256
257    SL::System::TaskServer->new->wake_up;
258
259    my $html = $self->render('shop_part/_upload_status', { output => 0 }, job => $job);
260
261    $self->js
262       ->html('#status_mass_upload', $html)
263       ->run('kivi.ShopPart.massUploadStarted')
264       ->render;
265 }
266
267 sub action_update {
268   my ($self) = @_;
269
270   $self->create_or_update;
271 }
272
273 sub render_shop_part_edit_dialog {
274   my ($self) = @_;
275
276   $self->js
277     ->run(
278       'kivi.ShopPart.shop_part_dialog',
279       t8('Shop part'),
280       $self->render('shop_part/edit', { output => 0 })
281     )
282     ->reinit_widgets;
283
284   $self->js->render;
285 }
286
287 sub create_or_update {
288   my ($self) = @_;
289
290   my $is_new = !$self->shop_part->id;
291
292   my $params = delete($::form->{shop_part}) || { };
293
294   $self->shop_part->assign_attributes(%{ $params });
295
296   $self->shop_part->save;
297
298   my ( $price, $price_src_str ) = $self->get_price_n_pricesource($self->shop_part->active_price_source);
299 if(!$is_new){
300   flash('info', $is_new ? t8('The shop part has been created.') : t8('The shop part has been saved.'));
301   $self->js->html('#shop_part_description_' . $self->shop_part->id, $self->shop_part->shop_description)
302            ->html('#shop_part_active_' . $self->shop_part->id, $self->shop_part->active)
303            ->html('#price_' . $self->shop_part->id, $::form->format_amount(\%::myconfig,$price,2))
304            ->html('#active_price_source_' . $self->shop_part->id, $price_src_str)
305            ->run('kivi.ShopPart.close_dialog')
306            ->flash('info', t8("Updated shop part"))
307            ->render;
308          }else{
309     $self->redirect_to(controller => 'Part', action => 'edit', 'part.id' => $self->shop_part->part_id);
310   }
311 }
312
313 #
314 # internal stuff
315 #
316 sub add_javascripts  {
317   $::request->{layout}->add_javascripts(qw(kivi.ShopPart.js));
318 }
319
320 sub load_pricesources {
321   my ($self) = @_;
322
323   my $pricesources;
324   push( @{ $pricesources } , { id => "master_data/sellprice", name => t8("Master Data")." - ".t8("Sellprice") },
325                              { id => "master_data/listprice", name => t8("Master Data")." - ".t8("Listprice") },
326                              { id => "master_data/lastcost",  name => t8("Master Data")." - ".t8("Lastcost") }
327                              );
328   my $pricegroups = SL::DB::Manager::Pricegroup->get_all;
329   foreach my $pg ( @$pricegroups ) {
330     push( @{ $pricesources } , { id => "pricegroup/".$pg->id, name => t8("Pricegroup") . " - " . $pg->pricegroup} );
331   };
332
333   $self->price_sources( $pricesources );
334 }
335
336 sub get_price_n_pricesource {
337   my ($self,$pricesource) = @_;
338
339   my ( $price_src_str, $price_src_id ) = split(/\//,$pricesource);
340
341   require SL::DB::Pricegroup;
342   require SL::DB::Part;
343   my $price;
344   if ($price_src_str eq "master_data") {
345     my $part       = SL::DB::Manager::Part->find_by( id => $self->shop_part->part_id );
346     $price         = $part->$price_src_id;
347     $price_src_str = $price_src_id;
348     }else{
349     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];
350     #my $part       = SL::DB::Manager::Part->find_by( id => $self->shop_part->part_id, 'prices.'.pricegroup_id => $price_src_id );
351     my $pricegrp   = SL::DB::Manager::Pricegroup->find_by( id => $price_src_id )->pricegroup;
352     $price         = $part->prices->[0]->price;
353     $price_src_str = $pricegrp;
354   }
355   return($price,$price_src_str);
356 }
357
358 sub check_auth {
359   $::auth->assert('shop_part_edit');
360 }
361
362 sub init_shop_part {
363   if ($::form->{shop_part_id}) {
364     SL::DB::Manager::ShopPart->find_by(id => $::form->{shop_part_id});
365   } else {
366     SL::DB::ShopPart->new(shop_id => $::form->{shop_id}, part_id => $::form->{part_id});
367   };
368 }
369
370 sub init_file {
371   my $file = $::form->{id} ? SL::DB::File->new(id => $::form->{id})->load : SL::DB::File->new;
372   return $file;
373 }
374
375 sub init_shops {
376   SL::DB::Shop->shops_dd;
377 }
378
379 1;
380
381 __END__
382
383 =encoding utf-8
384
385
386 =head1 NAME
387
388 SL::Controller::ShopPart - Controller for managing ShopParts
389
390 =head1 SYNOPSIS
391
392 ShopParts are configured in a tab of the corresponding part.
393
394 =head1 ACTIONS
395
396 =over 4
397
398 =item C<action_update_shop>
399
400 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.
401
402 =item C<action_show_files>
403
404
405
406 =item C<action_ajax_delete_file>
407
408
409
410 =item C<action_get_categories>
411
412
413
414 =item C<action_show_price_n_pricesource>
415
416
417
418 =item C<action_show_stock>
419
420
421
422 =item C<action_get_n_write_categories>
423
424 Can be used to sync the categories of a shoppart with the categories from online.
425
426 =item C<action_save_categories>
427
428 The ShopwareConnector works with the CategoryID @categories[x][0] in others/new Connectors it must be tested
429 Each assigned categorie is saved with id,categorie_name an multidimensional array and could be expanded with categoriepath or what is needed
430
431 =item C<action_reorder>
432
433
434
435 =item C<action_upload_status>
436
437
438
439 =item C<action_mass_upload>
440
441
442
443 =item C<action_update>
444
445
446
447 =item C<create_or_update>
448
449
450
451 =item C<render_shop_part_edit_dialog>
452
453 when self->shop_part is called in template, it will be an existing shop_part with id,
454 or a new shop_part with only part_id and shop_id set
455
456 =item C<add_javascripts>
457
458
459 =item C<load_pricesources>
460
461 the price sources to use for the article: sellprice, lastcost,
462 listprice, or one of the pricegroups. It overwrites the default pricesource from the shopconfig.
463 TODO: implement valid pricerules for the article
464
465 =item C<get_price_n_pricesource>
466
467
468 =item C<check_auth>
469
470
471 =item C<init_shop_part>
472
473
474 =item C<init_file>
475
476
477 =item C<init_shops>
478
479 data for drop down filter options
480
481 =back
482
483 =head1 TODO
484
485 CheckAuth
486 Pricesrules, pricessources aren't fully implemented yet.
487
488 =head1 AUTHORS
489
490 G. Richardson E<lt>information@kivitendo-premium.deE<gt>
491 W. Hahn E<lt>wh@futureworldsearch.netE<gt>
492
493 =cut