|
@@ -1,51 +1,40 @@
|
|
|
class PreSalesController < ApplicationController
|
|
class PreSalesController < ApplicationController
|
|
|
before_action :set_pre_sale, only: [:show, :edit, :update, :destroy]
|
|
before_action :set_pre_sale, only: [:show, :edit, :update, :destroy]
|
|
|
|
|
|
|
|
- # GET /pre_sales/new
|
|
|
|
|
- def new; end
|
|
|
|
|
-
|
|
|
|
|
# POST /pre_sales
|
|
# POST /pre_sales
|
|
|
# POST /pre_sales.json
|
|
# POST /pre_sales.json
|
|
|
def create
|
|
def create
|
|
|
- total_in_pre_sales = 0
|
|
|
|
|
- @pre_sale = PreSale.new(pre_sale_params)
|
|
|
|
|
- @pre_sale.user_id = current_user.id
|
|
|
|
|
-
|
|
|
|
|
- # Sumar los totales de los presales en caso que exista.
|
|
|
|
|
- PreSale.where(user_id: @pre_sale.user_id).each do |pre|
|
|
|
|
|
- total_in_pre_sales = total_in_pre_sales + pre.total
|
|
|
|
|
|
|
+ @pre_sale = PreSale.find_by(product_id: params[:pre_sale][:product_id], user_id: current_user.id)
|
|
|
|
|
+ if @pre_sale.present? # si ya existe un pre sale con ese producto
|
|
|
|
|
+ @pre_sale.quantity += 1
|
|
|
|
|
+ else
|
|
|
|
|
+ @is_new = true
|
|
|
|
|
+ @pre_sale = PreSale.new(pre_sale_params)
|
|
|
|
|
+ @pre_sale.user_id = current_user.id
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
|
|
+ total_in_pre_sales = PreSale.where(user_id: @pre_sale.user_id).sum(:total)
|
|
|
|
|
+
|
|
|
#precio unitario
|
|
#precio unitario
|
|
|
@pre_sale.unit_price = @pre_sale.product.get_price_sale(OpenCashRegister.find(@pre_sale.open_cash_register_id).pointsale.id)
|
|
@pre_sale.unit_price = @pre_sale.product.get_price_sale(OpenCashRegister.find(@pre_sale.open_cash_register_id).pointsale.id)
|
|
|
-
|
|
|
|
|
@pre_sale.get_totals
|
|
@pre_sale.get_totals
|
|
|
-
|
|
|
|
|
total_in_pre_sales += @pre_sale.total
|
|
total_in_pre_sales += @pre_sale.total
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
respond_to do |format|
|
|
|
if has_enough_stock?(@pre_sale)
|
|
if has_enough_stock?(@pre_sale)
|
|
|
#cuando la venta es a credito checar que el credito permita esta nueva venta
|
|
#cuando la venta es a credito checar que el credito permita esta nueva venta
|
|
|
- if @pre_sale.sale_type == "credit"
|
|
|
|
|
|
|
+ case @pre_sale.sale_type
|
|
|
|
|
+ when "credit" then
|
|
|
debiting = @pre_sale.customer.get_debiting
|
|
debiting = @pre_sale.customer.get_debiting
|
|
|
- if @pre_sale.customer.sale_approved?(debiting + total_in_pre_sales)
|
|
|
|
|
- if @pre_sale.save
|
|
|
|
|
- format.js
|
|
|
|
|
- else
|
|
|
|
|
- format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
|
|
|
- end
|
|
|
|
|
|
|
+ if @pre_sale.customer.sale_approved?(debiting + total_in_pre_sales) && @pre_sale.save
|
|
|
|
|
+ format.js
|
|
|
else
|
|
else
|
|
|
@pre_sale.errors.add(:customer_id, "El cliente ya no tiene crédito disponible.")
|
|
@pre_sale.errors.add(:customer_id, "El cliente ya no tiene crédito disponible.")
|
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
|
- end
|
|
|
|
|
- elsif @pre_sale.sale_type == "cash"
|
|
|
|
|
- if @pre_sale.save
|
|
|
|
|
format.js
|
|
format.js
|
|
|
- else
|
|
|
|
|
- format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
|
|
|
end
|
|
end
|
|
|
- elsif @pre_sale.sale_type == "reserved"
|
|
|
|
|
- if @pre_sale.save
|
|
|
|
|
|
|
+ when "cash", "reserved" then
|
|
|
|
|
+ if @pre_sale.save
|
|
|
format.js
|
|
format.js
|
|
|
else
|
|
else
|
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
@@ -62,34 +51,31 @@ class PreSalesController < ApplicationController
|
|
|
# PATCH/PUT /pre_sales/1
|
|
# PATCH/PUT /pre_sales/1
|
|
|
# PATCH/PUT /pre_sales/1.json
|
|
# PATCH/PUT /pre_sales/1.json
|
|
|
def update
|
|
def update
|
|
|
|
|
+ dup_presale = @pre_sale.dup
|
|
|
@pre_sale.quantity = params[:pre_sale][:quantity].to_i
|
|
@pre_sale.quantity = params[:pre_sale][:quantity].to_i
|
|
|
availableProduct = AvailableProduct.find_by(pointsale_id: current_user.pointsale_id, product_id: @pre_sale.product_id)
|
|
availableProduct = AvailableProduct.find_by(pointsale_id: current_user.pointsale_id, product_id: @pre_sale.product_id)
|
|
|
|
|
+ total_in_pre_sales = PreSale.where(user_id: current_user.id, product_id: @pre_sale.product_id).where.not(id: @pre_sale.id).sum(:total)
|
|
|
|
|
|
|
|
- same_product_quantity = 0
|
|
|
|
|
- total_in_pre_sales = 0
|
|
|
|
|
- PreSale.where(user_id: current_user.id, product_id: @pre_sale.product_id).where.not(id: @pre_sale.id).each do |pre|
|
|
|
|
|
- total_in_pre_sales += pre.total
|
|
|
|
|
- same_product_quantity += pre.quantity
|
|
|
|
|
- end
|
|
|
|
|
-
|
|
|
|
|
- total_quantity = same_product_quantity + @pre_sale.quantity
|
|
|
|
|
respond_to do |format|
|
|
respond_to do |format|
|
|
|
- if availableProduct.stock >= total_quantity
|
|
|
|
|
|
|
+ if availableProduct.stock >= @pre_sale.quantity
|
|
|
@pre_sale.get_totals
|
|
@pre_sale.get_totals
|
|
|
total_in_pre_sales += @pre_sale.total
|
|
total_in_pre_sales += @pre_sale.total
|
|
|
- if !@pre_sale.credit?
|
|
|
|
|
- @pre_sale.update(pre_sale_params)
|
|
|
|
|
- format.json { render json: @pre_sale }
|
|
|
|
|
- elsif @pre_sale.customer.sale_approved?(@pre_sale.customer.get_debiting + total_in_pre_sales)
|
|
|
|
|
|
|
+ case @pre_sale.sale_type
|
|
|
|
|
+ when "credit" then
|
|
|
|
|
+ if @pre_sale.customer.sale_approved?(@pre_sale.customer.get_debiting + total_in_pre_sales)
|
|
|
|
|
+ @pre_sale.update(pre_sale_params)
|
|
|
|
|
+ format.json { render json: @pre_sale }
|
|
|
|
|
+ else
|
|
|
|
|
+ @pre_sale.errors.add(:base, "El cliente ya no tiene crédito disponible")
|
|
|
|
|
+ format.json { render json: { errors: @pre_sale.errors.values, presale: dup_presale, quantity: dup_presale.quantity }, status: :unprocessable_entity }
|
|
|
|
|
+ end
|
|
|
|
|
+ when "cash", "reserved" then
|
|
|
@pre_sale.update(pre_sale_params)
|
|
@pre_sale.update(pre_sale_params)
|
|
|
format.json { render json: @pre_sale }
|
|
format.json { render json: @pre_sale }
|
|
|
- else
|
|
|
|
|
- @pre_sale.errors.add(:base, "El cliente ya no tiene crédito disponible")
|
|
|
|
|
- format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
|
|
|
end
|
|
end
|
|
|
else
|
|
else
|
|
|
@pre_sale.errors.add(:base, "Stock insuficiente del producto #{@pre_sale.product.name}")
|
|
@pre_sale.errors.add(:base, "Stock insuficiente del producto #{@pre_sale.product.name}")
|
|
|
- format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
|
|
|
|
|
+ format.json { render json: { errors: @pre_sale.errors.values, presale: dup_presale, quantity: dup_presale.quantity }, status: :unprocessable_entity }
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
@@ -110,57 +96,42 @@ class PreSalesController < ApplicationController
|
|
|
@product = pointsale.products.find_by("barcode = ? and stock > 0", params[:barcode])
|
|
@product = pointsale.products.find_by("barcode = ? and stock > 0", params[:barcode])
|
|
|
respond_to do |format|
|
|
respond_to do |format|
|
|
|
if @product.blank?
|
|
if @product.blank?
|
|
|
|
|
+ @pre_sale = PreSale.new
|
|
|
|
|
+ @pre_sale.errors.add(:product_id, "No se encontro el producto")
|
|
|
format.js { render action: "create" }
|
|
format.js { render action: "create" }
|
|
|
else
|
|
else
|
|
|
- # ir sumando todos los presales que tiene, para ver si lo abarca el credito
|
|
|
|
|
- PreSale.where(user_id: current_user.id).each do |pre|
|
|
|
|
|
- total_in_pre_sales = total_in_pre_sales + pre.total
|
|
|
|
|
- end
|
|
|
|
|
-
|
|
|
|
|
- @pre_sale = PreSale.new
|
|
|
|
|
- @pre_sale.customer_id = params[:customer_id]
|
|
|
|
|
- @pre_sale.user_id = current_user.id
|
|
|
|
|
- @pre_sale.open_cash_register_id = params[:open_cash_register_id]
|
|
|
|
|
- @pre_sale.product_id = @product.id
|
|
|
|
|
- @pre_sale.sale_type = params[:sale_type]
|
|
|
|
|
- @pre_sale.quantity = 1
|
|
|
|
|
|
|
+ total_in_pre_sales = PreSale.where(user_id: current_user.id).sum(:total)
|
|
|
|
|
+ @pre_sale = PreSale.new(customer_id: params[:customer_id], user_id: current_user.id, open_cash_register_id: params[:open_cash_register_id], product_id: @product.id, sale_type: params[:sale_type], quantity: 1)
|
|
|
|
|
|
|
|
#precio unitario
|
|
#precio unitario
|
|
|
@pre_sale.unit_price = @pre_sale.product.get_price_sale(OpenCashRegister.find(@pre_sale.open_cash_register_id).pointsale.id)
|
|
@pre_sale.unit_price = @pre_sale.product.get_price_sale(OpenCashRegister.find(@pre_sale.open_cash_register_id).pointsale.id)
|
|
|
-
|
|
|
|
|
@pre_sale.get_totals
|
|
@pre_sale.get_totals
|
|
|
-
|
|
|
|
|
- total_in_pre_sales = total_in_pre_sales + @pre_sale.total
|
|
|
|
|
|
|
+ total_in_pre_sales += @pre_sale.total
|
|
|
|
|
|
|
|
#cuando la venta es a credito checar que el credito permita esta nueva venta
|
|
#cuando la venta es a credito checar que el credito permita esta nueva venta
|
|
|
if has_enough_stock?(@pre_sale)
|
|
if has_enough_stock?(@pre_sale)
|
|
|
- if @pre_sale.sale_type == "credit"
|
|
|
|
|
|
|
+ #cuando la venta es a credito checar que el credito permita esta nueva venta
|
|
|
|
|
+ case @pre_sale.sale_type
|
|
|
|
|
+ when "credit" then
|
|
|
debiting = @pre_sale.customer.get_debiting
|
|
debiting = @pre_sale.customer.get_debiting
|
|
|
- if @pre_sale.customer.sale_approved?(debiting + total_in_pre_sales)
|
|
|
|
|
- if @pre_sale.save
|
|
|
|
|
- format.js { render action: "create" }
|
|
|
|
|
- end
|
|
|
|
|
|
|
+ if @pre_sale.customer.sale_approved?(debiting + total_in_pre_sales) && @pre_sale.save
|
|
|
|
|
+ format.js { render action: "create" }
|
|
|
else
|
|
else
|
|
|
@pre_sale.errors.add(:customer_id, "El cliente ya no tiene crédito disponible.")
|
|
@pre_sale.errors.add(:customer_id, "El cliente ya no tiene crédito disponible.")
|
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
|
- format.js { render action: "create" }
|
|
|
|
|
|
|
+ format.js
|
|
|
end
|
|
end
|
|
|
- elsif @pre_sale.sale_type == "cash"
|
|
|
|
|
- if @pre_sale.save
|
|
|
|
|
|
|
+ when "cash", "reserved" then
|
|
|
|
|
+ if @pre_sale.save
|
|
|
format.js { render action: "create" }
|
|
format.js { render action: "create" }
|
|
|
else
|
|
else
|
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
|
end
|
|
end
|
|
|
- elsif @pre_sale.sale_type == "reserved"
|
|
|
|
|
- if @pre_sale.save
|
|
|
|
|
- format.js
|
|
|
|
|
- else
|
|
|
|
|
- format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
|
|
|
- end
|
|
|
|
|
end
|
|
end
|
|
|
else
|
|
else
|
|
|
@pre_sale.errors.add(:base, "Stock insuficiente del producto #{@pre_sale.product.name}")
|
|
@pre_sale.errors.add(:base, "Stock insuficiente del producto #{@pre_sale.product.name}")
|
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
format.json { render json: @pre_sale.errors.values, status: :unprocessable_entity }
|
|
|
|
|
+ format.js { render action: "create" }
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
@@ -213,14 +184,8 @@ class PreSalesController < ApplicationController
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
def has_enough_stock?(pre_sale)
|
|
def has_enough_stock?(pre_sale)
|
|
|
- same_product_quantity = 0
|
|
|
|
|
- PreSale.where(user_id: current_user.id, product_id: pre_sale.product_id).each do |pre|
|
|
|
|
|
- same_product_quantity += pre.quantity
|
|
|
|
|
- end
|
|
|
|
|
-
|
|
|
|
|
availableProduct = AvailableProduct.find_by(pointsale_id: current_user.pointsale_id, product_id: pre_sale.product_id)
|
|
availableProduct = AvailableProduct.find_by(pointsale_id: current_user.pointsale_id, product_id: pre_sale.product_id)
|
|
|
-
|
|
|
|
|
- if availableProduct.stock >= (same_product_quantity + pre_sale.quantity)
|
|
|
|
|
|
|
+ if availableProduct.stock >= pre_sale.quantity
|
|
|
return true
|
|
return true
|
|
|
else
|
|
else
|
|
|
return false
|
|
return false
|