]> wagnertech.de Git - mfinanz.git/blobdiff - SL/Controller/Order.pm
Auftrags-Controller: PriceSources: Umstellung auf discount_from_source.
[mfinanz.git] / SL / Controller / Order.pm
index cc0f57e833466b59b6ca94aae615a7b5e31df91c..33628ba466317ef711cec221acb31629136a26e9 100644 (file)
@@ -18,13 +18,15 @@ use SL::DB::Employee;
 use SL::DB::Project;
 use SL::DB::Default;
 use SL::DB::Unit;
+use SL::DB::Price;
 
 use SL::Helper::DateTime;
 use SL::Helper::CreatePDF qw(:all);
 
 use List::Util qw(max first);
-use List::MoreUtils qw(none pairwise);
+use List::MoreUtils qw(none pairwise first_index);
 use English qw(-no_match_vars);
+use File::Spec;
 
 use Rose::Object::MakeMethods::Generic
 (
@@ -119,19 +121,25 @@ sub action_create_pdf {
   $sfile->fh->print($pdf);
   $sfile->fh->close;
 
-  my $tmp_filename = $sfile->file_name;
-  my $pdf_filename = t8('Sales Order') . '_' . $self->order->ordnumber . '.pdf';
+  my $key = join('_', Time::HiRes::gettimeofday(), int rand 1000000000000);
+  $::auth->set_session_value("Order::create_pdf-${key}" => $sfile->file_name);
+
+  $::form->{formname}  = $self->type;
+  $::form->{language} = 'de';
+  my $pdf_filename =  $::form->get_formname_translation . '_' . $self->order->ordnumber . '.pdf';
 
   $self->js
-    ->run('download_pdf', $tmp_filename, $pdf_filename)
+    ->run('download_pdf', $pdf_filename, $key)
     ->flash('info', t8('The PDF has been created'))->render($self);
 }
 
 sub action_download_pdf {
   my ($self) = @_;
 
+  my $key = $::form->{key};
+  my $tmp_filename = $::auth->get_session_value("Order::create_pdf-${key}");
   return $self->send_file(
-    $::form->{tmp_filename},
+    $tmp_filename,
     type => 'application/pdf',
     name => $::form->{pdf_filename},
   );
@@ -237,31 +245,39 @@ sub action_save_and_delivery_order {
 sub action_customer_vendor_changed {
   my ($self) = @_;
 
-  if ($self->cv eq 'customer') {
-    $self->order->customer(SL::DB::Manager::Customer->find_by_or_create(id => $::form->{cv_id}));
-
-  } elsif ($self->cv eq 'vendor') {
-    $self->order->vendor(SL::DB::Manager::Vendor->find_by_or_create(id => $::form->{cv_id}));
-  }
+  my $cv_method = $self->cv;
 
-  if ($self->order->{$self->cv}->contacts && scalar @{ $self->order->{$self->cv}->contacts } > 0) {
+  if ($self->order->$cv_method->contacts && scalar @{ $self->order->$cv_method->contacts } > 0) {
     $self->js->show('#cp_row');
   } else {
     $self->js->hide('#cp_row');
   }
 
-  if ($self->order->{$self->cv}->shipto && scalar @{ $self->order->{$self->cv}->shipto } > 0) {
+  if ($self->order->$cv_method->shipto && scalar @{ $self->order->$cv_method->shipto } > 0) {
     $self->js->show('#shipto_row');
   } else {
     $self->js->hide('#shipto_row');
   }
 
+  $self->order->taxzone_id($self->order->$cv_method->taxzone_id);
+
+  if ($self->order->is_sales) {
+    $self->order->taxincluded(defined($self->order->$cv_method->taxincluded_checked)
+                              ? $self->order->$cv_method->taxincluded_checked
+                              : $::myconfig{taxincluded_checked});
+  }
+
+  $self->_recalc();
+
   $self->js
-    ->replaceWith('#order_cp_id',     $self->build_contact_select)
-    ->replaceWith('#order_shipto_id', $self->build_shipto_select)
-    ->val('#order_taxzone_id', $self->order->{$self->cv}->taxzone_id)
-    ->focus('#order_' . $self->cv . '_id')
-    ->render($self);
+    ->replaceWith('#order_cp_id',       $self->build_contact_select)
+    ->replaceWith('#order_shipto_id',   $self->build_shipto_select)
+    ->val(        '#order_taxzone_id',  $self->order->taxzone_id)
+    ->val(        '#order_taxincluded', $self->order->taxincluded)
+    ->focus(      '#order_' . $self->cv . '_id');
+
+  $self->_js_redisplay_amounts_and_taxes;
+  $self->js->render();
 }
 
 sub action_add_item {
@@ -274,17 +290,40 @@ sub action_add_item {
   my $item = SL::DB::OrderItem->new;
   $item->assign_attributes(%$form_attr);
 
-  my $part        = SL::DB::Part->new(id => $form_attr->{parts_id})->load;
-  my $cv_method   = $self->cv;
-  my $cv_discount = $self->order->$cv_method? $self->order->$cv_method->discount : 0.0;
+  my $part         = SL::DB::Part->new(id => $form_attr->{parts_id})->load;
+
+  my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order);
+
+  my $price_src;
+  if ($item->sellprice) {
+    $price_src = $price_source->price_from_source("");
+    $price_src->price($item->sellprice);
+  } else {
+    $price_src = $price_source->best_price
+           ? $price_source->best_price
+           : $price_source->price_from_source("");
+    $price_src->price(0) if !$price_source->best_price;
+  }
+
+  my $discount_src;
+  if ($item->discount) {
+    $discount_src = $price_source->discount_from_source("");
+    $discount_src->discount($item->discount);
+  } else {
+    $discount_src = $price_source->best_discount
+                  ? $price_source->best_discount
+                  : $price_source->discount_from_source("");
+    $discount_src->discount(0) if !$price_source->best_discount;
+  }
 
   my %new_attr;
-  $new_attr{part}        = $part;
-  $new_attr{description} = $part->description if ! $item->description;
-  $new_attr{qty}         = 1.0                if ! $item->qty;
-  $new_attr{unit}        = $part->unit;
-  $new_attr{sellprice}   = $part->sellprice   if ! $item->sellprice;
-  $new_attr{discount}    = $cv_discount       if ! $item->discount;
+  $new_attr{part}                   = $part;
+  $new_attr{description}            = $part->description if ! $item->description;
+  $new_attr{qty}                    = 1.0                if ! $item->qty;
+  $new_attr{sellprice}              = $price_src->price;
+  $new_attr{discount}               = $discount_src->discount;
+  $new_attr{active_price_source}    = $price_src;
+  $new_attr{active_discount_source} = $discount_src;
 
   # add_custom_variables adds cvars to an orderitem with no cvars for saving, but
   # they cannot be retrieved via custom_variables until the order/orderitem is
@@ -327,6 +366,15 @@ sub action_recalc_amounts_and_taxes {
   $self->js->render();
 }
 
+sub action_price_popup {
+  my ($self) = @_;
+
+  my $idx  = first_index { $_ eq $::form->{item_id} } @{ $::form->{orderitem_ids} };
+  my $item = $self->order->items->[$idx];
+
+  $self->render_price_dialog($item);
+}
+
 sub _js_redisplay_linetotals {
   my ($self) = @_;
 
@@ -432,13 +480,34 @@ sub build_tax_rows {
   my ($self) = @_;
 
   my $rows_as_html;
-  foreach my $tax (@{ $self->{taxes} }) {
+  foreach my $tax (sort { $a->{tax}->rate cmp $b->{tax}->rate } @{ $self->{taxes} }) {
     $rows_as_html .= $self->p->render('order/tabs/_tax_row', TAX => $tax, TAXINCLUDED => $self->order->taxincluded);
   }
   return $rows_as_html;
 }
 
 
+sub render_price_dialog {
+  my ($self, $record_item) = @_;
+
+  my $price_source = SL::PriceSource->new(record_item => $record_item, record => $self->order);
+
+  $self->js
+    ->run(
+      'kivi.io.price_chooser_dialog',
+      t8('Available Prices'),
+      $self->render('order/tabs/_price_sources_dialog', { output => 0 }, price_source => $price_source)
+    )
+    ->reinit_widgets;
+
+#   if (@errors) {
+#     $self->js->text('#dialog_flash_error_content', join ' ', @errors);
+#     $self->js->show('#dialog_flash_error');
+#   }
+
+  $self->js->render;
+}
+
 sub _make_order {
   my ($self) = @_;
 
@@ -454,7 +523,6 @@ sub _make_order {
   return $order;
 }
 
-
 sub _recalc {
   my ($self) = @_;
 
@@ -483,16 +551,17 @@ sub _get_unalterable_data {
     if ($item->id) {
       # load data from orderitems (db)
       my $db_item = SL::DB::OrderItem->new(id => $item->id)->load;
-      $item->$_($db_item->$_) for qw(active_discount_source active_price_source longdescription);
+      $item->$_($db_item->$_) for qw(longdescription);
     } else {
       # set data from part (or other sources)
       $item->longdescription($item->part->notes);
-      #$item->active_price_source('');
-      #$item->active_discount_source('');
     }
 
     # autovivify all cvars that are not in the form (cvars_by_config can do it).
-    $item->cvars_by_config;
+    # workaround to pre-parse number-cvars (parse_custom_variable_values does not parse number values).
+    foreach my $var (@{ $item->cvars_by_config }) {
+      $var->unparsed_value($::form->parse_amount(\%::myconfig, $var->{__unparsed_value})) if ($var->config->type eq 'number' && exists($var->{__unparsed_value}));
+    }
     $item->parse_custom_variable_values;
   }
 }
@@ -531,6 +600,13 @@ sub _pre_render {
 
   $self->{current_employee_id} = SL::DB::Manager::Employee->current->id;
 
+  foreach  my $item (@{$self->order->items}) {
+    my $price_source = SL::PriceSource->new(record_item => $item, record => $self->order);
+    $item->active_price_source(   $price_source->price_from_source(   $item->active_price_source   ));
+    $item->active_discount_source($price_source->discount_from_source($item->active_discount_source));
+
+  }
+
   $::request->{layout}->use_javascript("${_}.js")  for qw(ckeditor/ckeditor ckeditor/adapters/jquery);
 }
 
@@ -538,8 +614,8 @@ sub _create_pdf {
   my ($order, $pdf_ref, $params) = @_;
 
   my $print_form = Form->new('');
-  $print_form->{type}     = 'sales_order';
-  $print_form->{formname} = 'sales_order',
+  $print_form->{type}     = $order->type;
+  $print_form->{formname} = $order->type;
   $print_form->{format}   = $params->{format} || 'pdf',
   $print_form->{media}    = $params->{media}  || 'file';