From e43a7a5333cb46e48992c96797323d613ee10433 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 22 Nov 2021 01:34:21 +0100 Subject: [PATCH] DeliveryOrder: stock roundtrip --- SL/Controller/DeliveryOrder.pm | 42 ++++++++++++++++++- SL/DB/DeliveryOrderItemsStock.pm | 5 +++ js/kivi.DeliveryOrder.js | 4 +- .../webpages/delivery_order/tabs/_row.html | 2 +- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/SL/Controller/DeliveryOrder.pm b/SL/Controller/DeliveryOrder.pm index 61e614e66..33b35bbab 100644 --- a/SL/Controller/DeliveryOrder.pm +++ b/SL/Controller/DeliveryOrder.pm @@ -911,7 +911,7 @@ sub action_stock_in_out_dialog { do_unit => $unit, delivered => $self->order->delivered, row => $row, - itme_id => $item_id, + item_id => $item_id, ); } @@ -1272,6 +1272,8 @@ sub load_order { # You need a custom shipto object to call cvars_by_config to get the cvars. $self->order->custom_shipto(SL::DB::Shipto->new(module => 'OE', custom_variables => [])) if !$self->order->custom_shipto; + $self->prepare_stock_info($_) for $self->order->items; + return $self->order; } @@ -1321,6 +1323,9 @@ sub make_order { push @items, $item; $pos++; } + + $self->prepare_stock_info($_) for $order->items, @items; + $order->add_items(grep {!$_->id} @items); return $order; @@ -1343,6 +1348,26 @@ sub make_item { # saved. Adding empty custom_variables to new orderitem here solves this problem. $item ||= SL::DB::DeliveryOrderItem->new(custom_variables => []); + # handle stock info + if (my $stock_info = delete $attr->{stock_info}) { + my %existing = map { $_->id => $_ } $item->delivery_order_stock_entries; + my @save; + + for my $line (@{ DO->unpack_stock_information(packed => $stock_info) }) { + # lookup existing or make new + my $obj = delete $existing{$line->{delivery_order_items_stock_id}} + // SL::DB::DeliveryOrderItemsStock->new; + + # assign attributes + $obj->$_($line->{$_}) for qw(bin_id warehouse_id chargenumber qty unit); + $obj->bestbefore_as_date($line->{bestfbefore}) + if $line->{bestbefore} && $::instance_conf->get_show_bestbefore; + push @save, $obj; + } + + $item->delivery_order_stock_entries(@save); + } + $item->assign_attributes(%$attr); if ($is_new) { @@ -1428,6 +1453,21 @@ sub new_item { return $item; } +sub prepare_stock_info { + my ($self, $item) = @_; + + $item->{stock_info} = SL::YAML::Dump([ + map +{ + delivery_order_items_stock_id => $_->id, + qty => $_->qty, + warehouse_id => $_->warehouse_id, + bin_id => $_->bin_id, + chargenumber => $_->chargenumber, + unit => $_->unit, + }, $item->delivery_order_stock_entries + ]); +} + sub setup_order_from_cv { my ($order) = @_; diff --git a/SL/DB/DeliveryOrderItemsStock.pm b/SL/DB/DeliveryOrderItemsStock.pm index 97fb2a19a..dc6f00d92 100644 --- a/SL/DB/DeliveryOrderItemsStock.pm +++ b/SL/DB/DeliveryOrderItemsStock.pm @@ -13,6 +13,11 @@ __PACKAGE__->meta->add_relationship( class => 'SL::DB::Inventory', column_map => { id => 'delivery_order_items_stock_id' }, }, + unit_obj => { + type => 'many to one', + class => 'SL::DB::Unit', + column_map => { unit => 'name' }, + }, ); __PACKAGE__->meta->initialize; diff --git a/js/kivi.DeliveryOrder.js b/js/kivi.DeliveryOrder.js index 87fca36fb..3877e9b5f 100644 --- a/js/kivi.DeliveryOrder.js +++ b/js/kivi.DeliveryOrder.js @@ -91,8 +91,8 @@ namespace('kivi.DeliveryOrder', function(ns) { type: $("#type").val(), parts_id: $row.find("[name$=parts_id]").val(), unit: $row.find("[name$=unit]").val(), - qty_as_number: $("qty_" + id).val(), - stock: $("stock_" + id).val(), + qty_as_number: $row.find("[name$=qty_as_number]").val(), + stock: $row.find("[name$=stock_info]").val(), item_id: id, row: $row.attr("id"), }, diff --git a/templates/webpages/delivery_order/tabs/_row.html b/templates/webpages/delivery_order/tabs/_row.html index ce9ce75ae..ee28a31cc 100644 --- a/templates/webpages/delivery_order/tabs/_row.html +++ b/templates/webpages/delivery_order/tabs/_row.html @@ -16,8 +16,8 @@ [%- END %] [% L.hidden_tag("orderitem_ids[+]", ID) %] [% L.hidden_tag("converted_from_orderitems_ids[+]", ITEM.converted_from_orderitems_id) %] - [% L.hidden_tag("stock_info[+]", ITEM.stock_info, class="data-stock-info") %] [% L.hidden_tag("order.orderitems[+].id", ITEM.id, id='item_' _ ID) %] + [% L.hidden_tag("order.orderitems[].stock_info", ITEM.stock_info, class="data-stock-info") %] [% L.hidden_tag("order.orderitems[].parts_id", ITEM.parts_id) %] -- 2.20.1