From 21aab01b3d93d77e8bc4629be037fc63a98406bb Mon Sep 17 00:00:00 2001
From: Philippe
Date: Wed, 24 Nov 2021 11:22:34 +0100
Subject: [PATCH] adding ajax for sotck_movement_p_articles, fixing select
width in pane hover form for stock movement_p_article, fixinf id_color method
---
.../admin/price_documents_controller.rb | 2 -
.../admin/stock_movement_lines_controller.rb | 2 +-
app/helpers/application_helper.rb | 17 ++-------
app/models/stock_movement.rb | 6 +++
app/models/stock_movement_line.rb | 9 ++++-
.../admin/price_documents/show.html.haml | 5 ++-
.../_price_line_block.html.haml | 2 +-
app/views/admin/price_lines/update.js.erb | 2 +-
.../_form.html.haml | 12 +++---
.../_add_p_article_form.html.haml | 4 +-
...ock_movement_line_p_article_form.html.haml | 37 ++++++++++++++++++-
.../admin/stock_movement_lines/update.js.erb | 13 ++++++-
.../admin/stock_movements/show.html.haml | 36 +++++++++++++-----
13 files changed, 104 insertions(+), 43 deletions(-)
diff --git a/app/controllers/admin/price_documents_controller.rb b/app/controllers/admin/price_documents_controller.rb
index c5252ec..f62a22d 100644
--- a/app/controllers/admin/price_documents_controller.rb
+++ b/app/controllers/admin/price_documents_controller.rb
@@ -8,10 +8,8 @@ class Admin::PriceDocumentsController < ApplicationController
def generate_stocks
@price_document = PriceDocument.find(params[:id])
-
@price_document.price_line_block.generate_stock
-
redirect_to [:admin, @price_document]
end
diff --git a/app/controllers/admin/stock_movement_lines_controller.rb b/app/controllers/admin/stock_movement_lines_controller.rb
index 44d0da1..e7bf6fd 100644
--- a/app/controllers/admin/stock_movement_lines_controller.rb
+++ b/app/controllers/admin/stock_movement_lines_controller.rb
@@ -56,7 +56,7 @@ class Admin::StockMovementLinesController < ApplicationController
def update
@stock_movement_line = StockMovementLine.find(params[:id])
-
+ @stock_movement = @stock_movement_line.stock_movement
if @stock_movement_line.update_attributes(params.require(:stock_movement_line).permit!)
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index b16997b..4665339 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -82,22 +82,11 @@ module ApplicationHelper
def id_color(price_line)
if price_line.p_articles.count == 0
- return "background-color: red"
+ return "red"
elsif price_line.p_articles.count < price_line.qte
- return "background-color: orange"
+ return "orange"
else
- return "background-color: #70ff29"
- end
- end
-
-
- def id_color_array(price_line)
- if price_line.p_articles.count == 0
- return "background-color", "red"
- elsif price_line.p_articles.count < price_line.qte
- return "background-color", "orange"
- else
- return "background-color", "#70ff29"
+ return "#70ff29"
end
end
diff --git a/app/models/stock_movement.rb b/app/models/stock_movement.rb
index 06bba9b..2f4ecd7 100644
--- a/app/models/stock_movement.rb
+++ b/app/models/stock_movement.rb
@@ -151,5 +151,11 @@ class StockMovement < ApplicationRecord
return to_r
end
+
+ def stock_generable
+ return self.stock_movement_lines.joins(:p_articles).count == self.stock_movement_lines.sum(:qte) ? true : false
+ #exclure les produit non stockable et sans SN
+ #return self.price_line_block.price_lines.joins(:p_product_ref).joins(:p_product).where("p_products.stockable = ?", true).where("p_products.with_serial_number = ?", true).joins(:p_articles).count < self.price_line_block.price_lines.joins(:p_product_ref).joins(:p_product).where("p_products.stockable = ?", true).where("p_products.with_serial_number = ?", true).sum(:qte) ? false : true
+ end
end
diff --git a/app/models/stock_movement_line.rb b/app/models/stock_movement_line.rb
index 2f8ca64..fbfde03 100644
--- a/app/models/stock_movement_line.rb
+++ b/app/models/stock_movement_line.rb
@@ -4,8 +4,8 @@ class StockMovementLine < ApplicationRecord
has_many :stock_movement_line_p_articles
has_many :p_articles, through: :stock_movement_line_p_articles
- accepts_nested_attributes_for :stock_movement_line_p_articles
- accepts_nested_attributes_for :p_articles, allow_destroy: true
+ accepts_nested_attributes_for :stock_movement_line_p_articles, allow_destroy: true
+ accepts_nested_attributes_for :p_articles, allow_destroy: true, allow_destroy: true
acts_as_caching :fields => [:tot_amount_ht, :price_u_ht]
@@ -19,4 +19,9 @@ class StockMovementLine < ApplicationRecord
self.ct_tot_amount_ht
end
end
+
+ def affected_qty
+ return self.p_articles.count
+ end
+
end
diff --git a/app/views/admin/price_documents/show.html.haml b/app/views/admin/price_documents/show.html.haml
index 8109eb4..9d9ab4b 100644
--- a/app/views/admin/price_documents/show.html.haml
+++ b/app/views/admin/price_documents/show.html.haml
@@ -210,7 +210,7 @@
-price_line_block.price_lines.each do |price_line|
%tbody
%tr{id: "price_line_row_#{price_line.id}"}
- %td{style: id_color(price_line), id: "price_line_#{price_line.id}_id", onclick: "$('.p_articles_#{price_line.id}').toggle('800','swing');"}
+ %td{style: "background-color: #{id_color(price_line)}", id: "price_line_#{price_line.id}_id", onclick: "$('.p_articles_#{price_line.id}').toggle('800','swing');"}
="##{price_line.id}"
%td
=price_line.ref
@@ -232,7 +232,8 @@
= link_to i(:"trash-o"), admin_price_line_path(price_line), method: :delete, data: { confirm: 'Voulez-vous vraiment supprimer cet enregistrement ? ' } , :remote => true
= link_to i(:pencil), edit_admin_price_line_path(price_line), :remote => true
= link_to i(:eye), admin_price_line_path(price_line), :remote => true
- = link_to i(:"mobile-alt"), add_p_article_admin_price_lines_path(p_product_ref_id: price_line.p_product_ref.id, price_line_id: price_line.id), :remote => true if @price_document.price_document_type_id != 6 and price_line.p_product_ref.p_product.stockable and price_line.p_product_ref.p_product.with_serial_number
+ = link_to i(:"mobile-alt"), add_p_article_admin_price_lines_path(p_product_ref_id: price_line.p_product_ref.id, price_line_id: price_line.id), :remote => true if @price_document.price_document_type_id != 6 and price_line.p_product_ref.p_product.stockable and price_line.p_product_ref.p_product.with_serial_number and !price_line_block.stock_ok
+ = i(:"mobile-alt", {style: "color: rgb(143 143 143 / 80%)"}) if price_line_block.stock_ok
-price_line.p_articles.each do |p_article|
%tr{class: "p_articles_lines p_articles_#{price_line.id}"}
%td
diff --git a/app/views/admin/price_line_blocks/_price_line_block.html.haml b/app/views/admin/price_line_blocks/_price_line_block.html.haml
index 7dcb1f5..4ee7530 100644
--- a/app/views/admin/price_line_blocks/_price_line_block.html.haml
+++ b/app/views/admin/price_line_blocks/_price_line_block.html.haml
@@ -78,7 +78,7 @@
%tbody
-price_line_block.price_lines.each do |price_line|
%tr
- %td{style: id_color(price_line), id: "price_line_#{price_line.id}", onclick: "$('.p_articles_#{price_line.id}').toggle('800','swing');"}
+ %td{style: "background-color: #{id_color(price_line)}", id: "price_line_#{price_line.id}", onclick: "$('.p_articles_#{price_line.id}').toggle('800','swing');"}
="##{price_line.id}"
%td
=price_line.ref
diff --git a/app/views/admin/price_lines/update.js.erb b/app/views/admin/price_lines/update.js.erb
index cfb2ec3..6ed9223 100644
--- a/app/views/admin/price_lines/update.js.erb
+++ b/app/views/admin/price_lines/update.js.erb
@@ -1,6 +1,6 @@
$('#price_line_<%= @price_line.id %>_qte').text(<%= @price_line.qte %>)
$('#price_line_<%= @price_line.id %>_affected_qty').text(<%= @price_line.affected_qty %>)
-$('#price_line_<%= @price_line.id %>_id').css( "<%= id_color_array(@price_line)[0] %>", "<%= id_color_array(@price_line)[1] %>" )
+$('#price_line_<%= @price_line.id %>_id').css( "background-color", "<%= id_color(@price_line) %>" )
<% if !@price_document.stock_ok and @price_document.stock_generable %>
$('#generate_stock_button_price_document_<%= @price_document.id %>').removeClass('disabled')
diff --git a/app/views/admin/stock_movement_line_p_articles/_form.html.haml b/app/views/admin/stock_movement_line_p_articles/_form.html.haml
index 6eddc2e..404caad 100644
--- a/app/views/admin/stock_movement_line_p_articles/_form.html.haml
+++ b/app/views/admin/stock_movement_line_p_articles/_form.html.haml
@@ -1,5 +1,7 @@
-= form.input :p_article_id,
- as: :select,
- collection: PArticle.available_articles.joins(:p_product_ref).where(p_product_ref: @stock_movement_line.p_product_ref).map{|p_article| [p_article.serialized_name, p_article.id]},
- label: "Article(s) associé(s)",
- input_html: {class:"p_article_select w-100"}
+.field
+ = form.input :p_article_id,
+ as: :select,
+ collection: PArticle.available_articles.joins(:p_product_ref).where(p_product_ref: @stock_movement_line.p_product_ref).map{|p_article| [p_article.serialized_name, p_article.id]},
+ label: "Article(s) associé(s)",
+ input_html: {class:"p_article_select w-100", style:"width: 70%;"}
+ %p.destroy=link_to_remove_fields "Supprimer l'article", form, {:class => "btn btn-danger", js: "remove_disabledClass();"}
diff --git a/app/views/admin/stock_movement_lines/_add_p_article_form.html.haml b/app/views/admin/stock_movement_lines/_add_p_article_form.html.haml
index e65dc34..42d5acb 100644
--- a/app/views/admin/stock_movement_lines/_add_p_article_form.html.haml
+++ b/app/views/admin/stock_movement_lines/_add_p_article_form.html.haml
@@ -6,8 +6,8 @@
= f.semantic_fields_for :p_articles do |form|
=render :partial => "admin/p_articles/form", :locals => {:form => form, @stock_movement_line => f.object}
- %p= link_to_add_fields "Ajouter un article", f, :p_articles, {:class => "btn btn-primary", data: {qty_to_reach: @stock_movement_line.qte, qty: f.object.p_articles.count, btn_id: "add_p_article_id" }, js: "add_disabledClass();"}# if f.object.p_articles.count < f.object.qte
- -# %p= link_to_add_fields "Ajouter un article", f, :p_articles, {:class => "btn btn-primary disabled"} if f.object.p_articles.count >= f.object.qte
+ %p= link_to_add_fields "Ajouter un article", f, :p_articles, {:class => "btn btn-primary", data: {qty_to_reach: @stock_movement_line.qte, qty: f.object.p_articles.count, btn_id: "add_p_article_id" }, js: "add_disabledClass();"} if f.object.p_articles.count < f.object.qte
+ %p= link_to_add_fields "Ajouter un article", f, :p_articles, {:class => "btn btn-primary disabled"} if f.object.p_articles.count >= f.object.qte
=render :partial => "qi/actions", :locals => {:f => f}
diff --git a/app/views/admin/stock_movement_lines/_add_stock_movement_line_p_article_form.html.haml b/app/views/admin/stock_movement_lines/_add_stock_movement_line_p_article_form.html.haml
index cdef601..1f83431 100644
--- a/app/views/admin/stock_movement_lines/_add_stock_movement_line_p_article_form.html.haml
+++ b/app/views/admin/stock_movement_lines/_add_stock_movement_line_p_article_form.html.haml
@@ -8,10 +8,43 @@
%p= link_to_add_fields "Ajouter un article", f, :stock_movement_line_p_articles, {:class => "btn btn-primary add_stock_movement_line_p_article"} if @stock_movement_line.p_articles.count < @stock_movement_line.qte
=render :partial => "qi/actions", :locals => {:f => f}
-
-
:javascript
$('.p_article_select').select2();
$('.add_stock_movement_line_p_article').click(function() {
$('.p_article_select').last().select2();
});
+
+
+ var qty = parseInt($('[data-btn-id="add_p_article_id"]').data("qty"))
+ window.localStorage.setItem('qty', qty);
+ var qtyToReach = parseInt($('[data-btn-id="add_p_article_id"]').data("qty-to-reach"))
+
+ function add_disabledClass() {
+
+ var qty = parseInt(window.localStorage.getItem('qty'))
+ qty += 1
+ window.localStorage.setItem('qty', qty);
+ $('[data-btn-id="add_p_article_id"]').attr('data-qty', qty);
+
+ if (qty >= qtyToReach) {
+ $('[data-btn-id="add_p_article_id"]').addClass('disabled')
+ } else {
+ $('[data-btn-id="add_p_article_id"]').removeClass('disabled')
+ }
+
+ }
+
+ function remove_disabledClass() {
+
+ var qty = parseInt(window.localStorage.getItem('qty'))
+ qty -= 1
+ window.localStorage.setItem('qty', qty);
+ $('[data-btn-id="add_p_article_id"]').attr('data-qty', qty);
+
+ if (qty >= qtyToReach) {
+ $('[data-btn-id="add_p_article_id"]').addClass('disabled')
+ } else {
+ $('[data-btn-id="add_p_article_id"]').removeClass('disabled')
+ }
+
+ }
diff --git a/app/views/admin/stock_movement_lines/update.js.erb b/app/views/admin/stock_movement_lines/update.js.erb
index b308543..dc9cacf 100644
--- a/app/views/admin/stock_movement_lines/update.js.erb
+++ b/app/views/admin/stock_movement_lines/update.js.erb
@@ -1,2 +1,13 @@
-$('#stock_movement_line_row_<%= @stock_movement_line.id %>').replaceWith("<%= escape_javascript(render(@stock_movement_line))%>");
+$('#stock_movement_line_<%= @stock_movement_line.id %>_qte').text(<%= @stock_movement_line.qte %>)
+$('#stock_movement_line_<%= @stock_movement_line.id %>_affected_qty').text(<%= @stock_movement_line.affected_qty %>)
+$('#stock_movement_line_<%= @stock_movement_line.id %>_id').css( "background-color", "<%= id_color(@stock_movement_line) %>" )
+
+<% if !@stock_movement.stock_done and @stock_movement.stock_generable %>
+ $('#generate_stock_button_stock_movement_<%= @stock_movement.id %>').removeClass('disabled')
+<% else %>
+ $('#generate_stock_button_stock_movement_<%= @stock_movement.id %>').addClass('disabled')
+<% end %>
+
+
+//$('#stock_movement_line_row_<%= @stock_movement_line.id %>').replaceWith("<%= escape_javascript(render(@stock_movement_line))%>");
close_pane_hover();
\ No newline at end of file
diff --git a/app/views/admin/stock_movements/show.html.haml b/app/views/admin/stock_movements/show.html.haml
index a7f1dfd..011230f 100644
--- a/app/views/admin/stock_movements/show.html.haml
+++ b/app/views/admin/stock_movements/show.html.haml
@@ -20,23 +20,26 @@
%h3 Contenu
%table.table.table-hover.table-stripped
%tr
- %th ID
+ %th{style: "width: 25px;"} ID
%th Produit
- %th DLUO ??
+ -# %th DLUO ??
%th Qté
+ %th Qté saisie
%th Prix / conditionnement
%th Valeur
%th Actions
-@stock_movement.stock_movement_lines.each do |stock_movement_line|
%tr
- %td
+ %td{style: "background-color: #{id_color(stock_movement_line)}", id: "stock_movement_line_#{stock_movement_line.id}_id", onclick: "$('.p_articles_#{stock_movement_line.id}').toggle('800','swing');"}
=stock_movement_line.id
%td
=stock_movement_line.p_product_ref.name if stock_movement_line.p_product_ref
- %td
- =l stock_movement_line.dluo if stock_movement_line.dluo
- %td
+ -# %td
+ -# =l stock_movement_line.dluo if stock_movement_line.dluo
+ %td{id: "stock_movement_line_#{stock_movement_line.id}_qte"}
=stock_movement_line.qte
+ %td{id: "stock_movement_line_#{stock_movement_line.id}_affected_qty"}
+ =stock_movement_line.affected_qty
%td
=number_to_currency(stock_movement_line.tot_amount_ht / stock_movement_line.qte) if stock_movement_line.tot_amount_ht
%td
@@ -46,14 +49,24 @@
= link_to i(:pencil), edit_admin_stock_movement_line_path(stock_movement_line), :remote => true
= link_to i(:eye), admin_stock_movement_line_path(stock_movement_line), :remote => true
-if @stock_movement.movement_type == "cred"
- = link_to i(:"mobile-alt"), add_p_article_admin_stock_movement_lines_path(p_product_ref_id: stock_movement_line.p_product_ref.id, stock_movement_line_id: stock_movement_line.id), :remote => true
+ = link_to i(:"mobile-alt"), add_p_article_admin_stock_movement_lines_path(p_product_ref_id: stock_movement_line.p_product_ref.id, stock_movement_line_id: stock_movement_line.id), :remote => true if !@stock_movement.stock_done
+ = i(:"mobile-alt", {style: "color: rgb(143 143 143 / 80%)"}) if @stock_movement.stock_done
-else
- = link_to i(:"mobile-alt"), add_stock_movement_line_p_article_admin_stock_movement_lines_path(p_product_ref_id: stock_movement_line.p_product_ref.id, stock_movement_line_id: stock_movement_line.id), :remote => true
-
+ = link_to i(:"mobile-alt"), add_stock_movement_line_p_article_admin_stock_movement_lines_path(p_product_ref_id: stock_movement_line.p_product_ref.id, stock_movement_line_id: stock_movement_line.id), :remote => true if !@stock_movement.stock_done
+ = i(:"mobile-alt", {style: "color: rgb(143 143 143 / 80%)"})if @stock_movement.stock_done
+ -stock_movement_line.p_articles.each do |p_article|
+ %tr{class: "stock_movement_lines p_articles_#{stock_movement_line.id}"}
+ %td
+ %td="##{p_article.id}"
+ %td=p_article.p_product_ref.name
+ %td
+ - p_article.p_article_serial_nums.each do |sn|
+ ="#{sn.p_serial_num_type.name} - #{sn.value} /"
%p.mt-5
- =link_to "Générer les stocks", generate_stocks_admin_stock_movement_path(@stock_movement), :class => "btn btn-primary" if !@stock_movement.stock_done
+ =link_to "Générer les stocks", generate_stocks_admin_stock_movement_path(@stock_movement), :class => "btn btn-primary", id: "generate_stock_button_stock_movement_#{@stock_movement.id}" if !@stock_movement.stock_done and @stock_movement.stock_generable
+ =link_to "Générer les stocks", generate_stocks_admin_stock_movement_path(@stock_movement), :class => "btn btn-primary disabled", id: "generate_stock_button_stock_movement_#{@stock_movement.id}" if !@stock_movement.stock_done and !@stock_movement.stock_generable
-if @stock_movement.stock_done
%h3 Stocks générés
@@ -72,3 +85,6 @@
.clear
=render :partial => "qi/qi_ordered_table", :locals => {:qi_ordered_table_collection => @line_stocks}
+
+:javascript
+ $('.stock_movement_lines').hide();
\ No newline at end of file