X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/33183828a27f58d1a6674d634c18d5cdc632ffd2..a32fcad2cf3fabf15c9dd10ddc83ce703db66ad8:/SL/Controller/DeliveryOrder.pm diff --git a/SL/Controller/DeliveryOrder.pm b/SL/Controller/DeliveryOrder.pm index 1806c245f..8f3d838c8 100644 --- a/SL/Controller/DeliveryOrder.pm +++ b/SL/Controller/DeliveryOrder.pm @@ -6,6 +6,7 @@ use parent qw(SL::Controller::Base); use SL::Helper::Flash qw(flash_later); use SL::Helper::Number qw(_format_number_units _parse_number); use SL::Presenter::Tag qw(select_tag hidden_tag div_tag); +use SL::Presenter::DeliveryOrder qw(delivery_order_status_line); use SL::Locale::String qw(t8); use SL::SessionFile::Random; use SL::PriceSource; @@ -18,6 +19,7 @@ use SL::DB::History; use SL::DB::Order; use SL::DB::Default; use SL::DB::Unit; +use SL::DB::Order; use SL::DB::Part; use SL::DB::PartClassification; use SL::DB::PartsGroup; @@ -35,7 +37,7 @@ use SL::Helper::UserPreferences::PositionsScrollbar; use SL::Helper::UserPreferences::UpdatePositions; use SL::Controller::Helper::GetModels; -use SL::Controller::DeliveryOrder::TypeData; +use SL::Controller::DeliveryOrder::TypeData qw(:types); use List::Util qw(first sum0); use List::UtilsBy qw(sort_by uniq_by); @@ -54,7 +56,7 @@ use Rose::Object::MakeMethods::Generic # safety __PACKAGE__->run_before('check_auth', - except => [ qw(pack_stock_information) ]); + except => [ qw(update_stock_information) ]); __PACKAGE__->run_before('get_unalterable_data', only => [ qw(save save_as_new save_and_delivery_order save_and_invoice save_and_ap_transaction @@ -85,7 +87,11 @@ sub action_add_from_order { # this interfers with init_order $self->{converted_from_oe_id} = delete $::form->{id}; - # TODO copy data and remember to link them on save + $self->type_data->validate($::form->{type}); + + my $order = SL::DB::Order->new(id => $self->{converted_from_oe_id})->load; + + $self->order(SL::DB::DeliveryOrder->new_from($order, type => $::form->{type})); $self->action_add; } @@ -915,16 +921,20 @@ sub action_stock_in_out_dialog { ); } -# we're using the old YAML based stock packing, but don't want to do this in -# the frontend so we're doing a tiny roundtrip to the backend, back the info in -# perl, serve it back to the frontend and store it in the DOM there -sub action_pack_stock_information { +sub action_update_stock_information { my ($self) = @_; my $stock_info = $::form->{stock_info}; + my $unit = $::form->{unit}; my $yaml = SL::YAML::Dump($stock_info); + my $stock_qty = $self->calculate_stock_in_out_from_stock_info($unit, $stock_info); + + my $response = { + stock_info => $yaml, + stock_qty => $stock_qty, + }; - $self->render(\$yaml, { layout => 0, process => 0 }); + $self->render(\ SL::JSON::to_json($response), { layout => 0, type => 'json', process => 0 }); } sub merge_stock_data { @@ -949,8 +959,8 @@ sub merge_stock_data { } else { for my $sinfo (@{ $stock_info }) { my $bin = SL::DB::Bin->load_cached($sinfo->{bin_id}); - $sinfo->{warehouse_description} = $bin->warehouse->description; - $sinfo->{bin_description} = $bin->escription; + $sinfo->{warehousedescription} = $bin->warehouse->description; + $sinfo->{bindescription} = $bin->description; map { $sinfo->{"stock_$_"} = $sinfo->{$_} } qw(qty unit); } } @@ -1030,6 +1040,8 @@ sub action_transfer_stock { return $self->js->flash("error", t8('The parts for this order have already been transferred'))->render; } + my $inout = $self->type_data->properties('transfer'); + my $errors = $self->save; if (@$errors) { @@ -1040,7 +1052,7 @@ sub action_transfer_stock { my $order = $self->order; # TODO move to type data - my $trans_type = $self->type_data->properties('transfer') eq 'in' + my $trans_type = $inout eq 'in' ? SL::DB::Manager::TransferType->find_by(direction => "id", description => "stock") : SL::DB::Manager::TransferType->find_by(direction => "out", description => "shipped"); @@ -1050,8 +1062,9 @@ sub action_transfer_stock { for my $stock (@{ $item->delivery_order_stock_entries }) { my $transfer = SL::DB::Inventory->new_from($stock); $transfer->trans_type($trans_type); + $transfer->qty($transfer->qty * -1) if $inout eq 'out'; - push @transfer_requests, $transfer; + push @transfer_requests, $transfer if defined $transfer->qty && $transfer->qty != 0; }; } @@ -1059,12 +1072,18 @@ sub action_transfer_stock { $self->js->flash("error", t8("No stock to transfer"))->render; } - SL::DB->with_transaction(sub { + SL::DB->client->with_transaction(sub { $_->save for @transfer_requests; - $self->order->update_attributes(deliverd => 1); + $self->order->update_attributes(delivered => 1); }); - $self->js->flash("info", t8("Stock transfered"))->render; + $self->js + ->flash("info", t8("Stock transfered")) + ->run('kivi.ActionBar.setDisabled', '#transfer_out_action', t8('The parts for this order have already been transferred')) + ->run('kivi.ActionBar.setDisabled', '#transfer_in_action', t8('The parts for this order have already been transferred')) + ->replaceWith('#data-status-line', delivery_order_status_line($self->order)) + ->render; + } sub js_load_second_row { @@ -1362,7 +1381,7 @@ sub make_item { $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; + push @save, $obj if $obj->qty; } $item->delivery_order_stock_entries(@save); @@ -1571,8 +1590,8 @@ sub save { if ($::form->{converted_from_oe_id}) { my @converted_from_oe_ids = split ' ', $::form->{converted_from_oe_id}; foreach my $converted_from_oe_id (@converted_from_oe_ids) { - my $src = SL::DB::DeliveryOrder->new(id => $converted_from_oe_id)->load; - $src->update_attributes(closed => 1) if $src->type =~ /_quotation$/; + my $src = SL::DB::Order->new(id => $converted_from_oe_id)->load; + $src->update_attributes(closed => 1) if $src->type =~ /_quotation$/ && $self->order->is_type(PURCHASE_DELIVERY_ORDER_TYPE); $src->link_to_record($self->order); } if (scalar @{ $::form->{converted_from_orderitems_ids} || [] }) { @@ -1859,22 +1878,27 @@ sub setup_edit_action_bar { t8('Delete'), call => [ 'kivi.DeliveryOrder.delete_order' ], confirm => $::locale->text('Do you really want to delete this object?'), - disabled => !$self->order->id ? t8('This object has not been saved yet.') : undef, + disabled => !$self->order->id ? t8('This object has not been saved yet.') : + $self->order->delivered ? t8('The parts for this order have already been transferred') : undef, only_if => $self->type_data->show_menu("delete"), ], combobox => [ action => [ t8('Transfer out'), - submit => [ '#order_form', { action => "DeliveryOrder/transfer_stock" } ], - disabled => $self->order->delivered ? t8('The parts for this order have already been transferred') : undef, + id => 'transfer_out_action', + call => [ 'kivi.DeliveryOrder.save', 'transfer_stock' ], + disabled => !$self->order->id ? t8('This object has not been saved yet.') : + $self->order->delivered ? t8('The parts for this order have already been transferred') : undef, only_if => $self->type_data->properties('transfer') eq 'out', confirm => t8('Do you really want to transfer the stock and set this order to delivered?'), ], action => [ t8('Transfer in'), - submit => [ '#order_form', { action => "DeliveryOrder/transfer_stock" } ], - disabled => $self->order->delivered ? t8('The parts for this order have already been transferred') : undef, + id => 'transfer_in_action', + call => [ 'kivi.DeliveryOrder.save', 'transfer_stock' ], + disabled => !$self->order->id ? t8('This object has not been saved yet.') : + $self->order->delivered ? t8('The parts for this order have already been transferred') : undef, only_if => $self->type_data->properties('transfer') eq 'in', confirm => t8('Do you really want to transfer the stock and set this order to delivered?'), ], @@ -2114,19 +2138,31 @@ sub store_pdf_to_webdav_and_filemanagement { return @errors; } +sub calculate_stock_in_out_from_stock_info { + my ($self, $unit, $stock_info) = @_; + + return "" if !$unit; + + my %units_by_name = map { $_->name => $_ } @{ SL::DB::Manager::Unit->get_all }; + + my $sum = sum0 map { + $units_by_name{$_->{unit}}->convert_to($_->{qty}, $units_by_name{$unit}) + } @$stock_info; + + my $content = _format_number_units($sum, 2, $units_by_name{$unit}, $units_by_name{$unit}); + + return $content; +} + sub calculate_stock_in_out { - my ($self, $item) = @_; + my ($self, $item, $stock_info) = @_; return "" if !$item->part || !$item->part->unit || !$item->unit; - my $in_out = $self->type_data->transfer; - - my $do_qty = $item->qty; my $sum = sum0 map { $_->unit_obj->convert_to($_->qty, $item->unit_obj) } $item->delivery_order_stock_entries; - my $matches = $do_qty == $sum; my $content = _format_number_units($sum, 2, $item->unit_obj, $item->part->unit_obj); return $content;