promotion
This commit is contained in:
@@ -24,7 +24,7 @@ class Api::BillController < Api::ApiController
|
|||||||
@sale = Sale.new
|
@sale = Sale.new
|
||||||
@status, @sale_id = @sale.generate_invoice_from_order(params[:order_id], current_login_employee, get_cashier)
|
@status, @sale_id = @sale.generate_invoice_from_order(params[:order_id], current_login_employee, get_cashier)
|
||||||
end
|
end
|
||||||
# Promotion.promo_activate(@sale)
|
Promotion.promo_activate(@sale)
|
||||||
else
|
else
|
||||||
@status = false
|
@status = false
|
||||||
@error_message = "No Current Open Shift"
|
@error_message = "No Current Open Shift"
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ class Promotion < ApplicationRecord
|
|||||||
accepts_nested_attributes_for :promotion_products , :allow_destroy => true
|
accepts_nested_attributes_for :promotion_products , :allow_destroy => true
|
||||||
|
|
||||||
PROMO_TYPE1 = "Quantity"
|
PROMO_TYPE1 = "Quantity"
|
||||||
PROMO_TYPE2 = "Net_off"
|
PROMO_TYPE2 = "Net_off" # 3000 => - 500 => 2500 [total]
|
||||||
PROMO_TYPE3 = "Net_price"
|
PROMO_TYPE3 = "Net_price" # 1800 => 1000 => 1000
|
||||||
PROMO_TYPE4 = "Percentage"
|
PROMO_TYPE4 = "Percentage"
|
||||||
|
|
||||||
def self.promo_activate(saleObj)
|
def self.promo_activate(saleObj)
|
||||||
@@ -18,7 +18,7 @@ class Promotion < ApplicationRecord
|
|||||||
puts "promoList - " + promoList.size.to_s
|
puts "promoList - " + promoList.size.to_s
|
||||||
if promoList.size > 0
|
if promoList.size > 0
|
||||||
itemList = combine_item(saleObj)
|
itemList = combine_item(saleObj)
|
||||||
is_promo_day(promoList,day, itemList)
|
is_promo_day(promoList,day, itemList, saleObj.sale_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -31,63 +31,63 @@ class Promotion < ApplicationRecord
|
|||||||
itemList = saleObj.sale_items.group(:product_code).sum(:qty)
|
itemList = saleObj.sale_items.group(:product_code).sum(:qty)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.is_promo_day(promoList, day, orderitemList)
|
def self.is_promo_day(promoList, day, orderitemList, sale_id)
|
||||||
puts "Today date - " + day.to_s
|
puts "Today date - " + day.to_s
|
||||||
promoList.each do |promo|
|
promoList.each do |promo|
|
||||||
dayresult = promo.promo_day.include?(day.to_s)
|
dayresult = promo.promo_day.include?(day.to_s)
|
||||||
if dayresult
|
if dayresult
|
||||||
orderitemList.each do |item|
|
orderitemList.each do |item|
|
||||||
find_promo_item(promo, item)
|
find_promo_item(promo, item, sale_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.find_promo_item(promo, orderitem)
|
def self.find_promo_item(promo, orderitem, sale_id)
|
||||||
if promo.original_product.to_s == orderitem[0].to_s
|
if promo.original_product.to_s == orderitem[0].to_s
|
||||||
if promo.min_qty.to_i > orderitem[1].to_i
|
if promo.min_qty.to_i > orderitem[1].to_i
|
||||||
return false
|
return false
|
||||||
else
|
else
|
||||||
check_promo_type(promo,orderitem)
|
check_promo_type(promo,orderitem, sale_id)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.check_promo_type(promo, orderitem)
|
def self.check_promo_type(promo, orderitem, sale_id)
|
||||||
|
same, promo_product = check_giveaway_product(promo, orderitem[0])
|
||||||
if promo.promo_type == Promotion::PROMO_TYPE1
|
if promo.promo_type == Promotion::PROMO_TYPE1
|
||||||
same, foc_min_qty = check_giveaway_product(promo, orderitem[0])
|
|
||||||
if same
|
if same
|
||||||
give_promotion_same_product(orderitem[1], promo.min_qty, foc_min_qty, orderitem)
|
give_promotion_same_product(orderitem[1], promo.min_qty, promo_product.min_qty, orderitem, sale_id)
|
||||||
else
|
else
|
||||||
find_promo_item_in_orderlist
|
give_promotion_second_product(orderitem[1], promo.min_qty, promo_product.item_code, orderitem, sale_id)
|
||||||
end
|
end
|
||||||
elsif promo.promo_type == Promotion::PROMO_TYPE2
|
elsif promo.promo_type == Promotion::PROMO_TYPE2
|
||||||
|
give_promotion_nett_off(same,promo_product,promo.min_qty, orderitem, sale_id)
|
||||||
elsif promo.promo_type == Promotion::PROMO_TYPE3
|
elsif promo.promo_type == Promotion::PROMO_TYPE3
|
||||||
|
# net price
|
||||||
elsif promo.promo_type == Promotion::PROMO_TYPE4
|
elsif promo.promo_type == Promotion::PROMO_TYPE4
|
||||||
|
# Percentage
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.check_giveaway_product(promo, orderitem)
|
def self.check_giveaway_product(promo, orderitem)
|
||||||
promo.promotion_products.each do |promo_product|
|
promo.promotion_products.each do |promo_product|
|
||||||
if promo_product.item_code == orderitem
|
if promo_product.item_code == orderitem
|
||||||
return true, promo_product.min_qty.to_i
|
return true, promo_product
|
||||||
else
|
else
|
||||||
return false, 0
|
return false, promo_product
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem)
|
def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem, sale_id)
|
||||||
puts " Order qty: " + qty.to_s + " / promoqty: " + promoqty.to_s + " / giveaway: " + foc_min_qty.to_s
|
puts " Order qty: " + qty.to_s + " / promoqty: " + promoqty.to_s + " / giveaway: " + foc_min_qty.to_s
|
||||||
multiple = qty.to_i / promoqty.to_i # loop count
|
multiple = qty.to_i / promoqty.to_i # loop count
|
||||||
charge_qty = 0
|
charge_qty = 0
|
||||||
foc_qty = 0
|
foc_qty = 0
|
||||||
if multiple > 0
|
if multiple > 0
|
||||||
multiple.times.each do |key|
|
multiple.times.each do |key|
|
||||||
if qty >= promoqty
|
if qty > promoqty
|
||||||
charge_qty += promoqty
|
charge_qty += promoqty
|
||||||
different = qty - promoqty
|
different = qty - promoqty
|
||||||
qty = different
|
qty = different
|
||||||
@@ -98,61 +98,80 @@ class Promotion < ApplicationRecord
|
|||||||
qty = qty - foc_min_qty
|
qty = qty - foc_min_qty
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
charge_qty += qty
|
||||||
|
end
|
||||||
|
end
|
||||||
if multiple == foc_qty
|
if multiple == foc_qty
|
||||||
charge_qty += qty
|
charge_qty += qty
|
||||||
|
end
|
||||||
else
|
else
|
||||||
charge_qty += qty
|
charge_qty += qty
|
||||||
end
|
end
|
||||||
end
|
item = OrderItem.find_by_item_code(orderitem[0])
|
||||||
end
|
update_existing_item(foc_qty, item, sale_id, "promotion", item.price)
|
||||||
else
|
|
||||||
charge_qty = qty
|
|
||||||
end
|
|
||||||
if qty == promoqty
|
|
||||||
update_existing_item(foc_qty, orderitem)
|
|
||||||
else
|
|
||||||
update_existing_item(foc_qty, orderitem)
|
|
||||||
end
|
|
||||||
puts "Charged - " + charge_qty.to_s
|
puts "Charged - " + charge_qty.to_s
|
||||||
puts "FOC - " + foc_qty.to_s
|
puts "FOC - " + foc_qty.to_s
|
||||||
end
|
end
|
||||||
|
# AA - 10 # 3 # BB # orderList, #S34345
|
||||||
def find_promo_item_in_orderlist(promo_item_code, orderitemList)
|
def self.give_promotion_second_product(orderitem_count, foc_min_qty, promo_product, orderitem, sale_id)
|
||||||
orderitemList.each do |item|
|
puts "..... orderitem_count: " + orderitem_count.to_s + " / foc_min_qty: " + foc_min_qty.to_s + " /promo_product: " + promo_product + " orderitem: " + orderitem.to_s
|
||||||
if item.item_code == promo_item_code
|
promotion_qty = orderitem_count.to_i / foc_min_qty.to_i # get foc item qty
|
||||||
give_promotion_second_product(item)
|
item = OrderItem.find_by_item_code(promo_product)
|
||||||
else
|
update_existing_item(promotion_qty, item, sale_id, "promotion", item.price)
|
||||||
add_promotion_second_item
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def give_promotion_second_product(item, foc_min_qty)
|
def self.update_existing_item(foc_qty, item, sale_id, type, item_price)
|
||||||
if item.qty > foc_min_qty
|
|
||||||
|
|
||||||
else
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.update_existing_item(foc_qty, item)
|
|
||||||
|
|
||||||
sale_item = SaleItem.new
|
sale_item = SaleItem.new
|
||||||
sale_item.product_code = item.item_code
|
sale_item.product_code = item.item_code
|
||||||
sale_item.product_name = item.item_name + " (promotion)"
|
sale_item.product_name = item.item_name + "("+ type +")"
|
||||||
sale_item.product_alt_name = item.alt_name
|
sale_item.product_alt_name = item.alt_name
|
||||||
sale_item.account_id = item.account_id
|
sale_item.account_id = item.account_id
|
||||||
sale_item.remark = " Promotion "
|
sale_item.remark = type
|
||||||
|
sale_item.qty = foc_qty * (-1)
|
||||||
|
|
||||||
|
sale_item.unit_price = item_price * (-1)
|
||||||
|
sale_item.taxable_price = item_price
|
||||||
|
sale_item.price = foc_qty * item_price * (-1)
|
||||||
|
|
||||||
sale_item.qty = foc_qty
|
|
||||||
sale_item.unit_price = item.price
|
|
||||||
sale_item.taxable_price = item.price
|
|
||||||
sale_item.is_taxable = item.taxable
|
sale_item.is_taxable = item.taxable
|
||||||
|
sale_item.sale_id = sale_id
|
||||||
|
|
||||||
sale_item.price = foc_qty * item.price
|
sale_item.save
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.add_promotion_second_item
|
|
||||||
|
def self.give_promotion_nett_off(same, promo_product, foc_min_qty, orderitem, sale_id)
|
||||||
|
puts " same: " + same.to_s + " promo_product: " + promo_product.item_code.to_s + " foc_min_qty: " + foc_min_qty.to_s + " orderitem: " + orderitem.to_s
|
||||||
|
|
||||||
|
if same
|
||||||
|
foc_qty = orderitem[1].to_i / foc_min_qty
|
||||||
|
item = OrderItem.find_by_item_code(orderitem[0])
|
||||||
|
update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off)
|
||||||
|
else
|
||||||
|
foc_qty = find_second_item_qty(sale_id, promo_product.item_code)
|
||||||
|
item = OrderItem.find_by_item_code(promo_product.item_code)
|
||||||
|
update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.give_promotion_nett_price(same, promo_product, foc_min_qty, orderitem, sale_id)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.give_promotion_discount()
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.find_second_item_qty(sale_id, promo_item)
|
||||||
|
saleObj = Sale.find_by_sale_id(sale_id)
|
||||||
|
itemList = combine_item(saleObj)
|
||||||
|
itemList.each do |item|
|
||||||
|
if item[0] == promo_item
|
||||||
|
return item[1]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user