promotion
This commit is contained in:
@@ -9,4 +9,151 @@ class Promotion < ApplicationRecord
|
|||||||
PROMO_TYPE2 = "Net_off"
|
PROMO_TYPE2 = "Net_off"
|
||||||
PROMO_TYPE3 = "Net_price"
|
PROMO_TYPE3 = "Net_price"
|
||||||
PROMO_TYPE4 = "Percentage"
|
PROMO_TYPE4 = "Percentage"
|
||||||
|
|
||||||
|
def self.promo_activate
|
||||||
|
saleObj = Sale.first # Test
|
||||||
|
current_day = Time.now.strftime("%Y-%m-%d")
|
||||||
|
current_time = Time.now.strftime('%H:%M:%S')
|
||||||
|
day = Date.today.wday
|
||||||
|
promoList = is_between_promo_datetime(current_day,current_time)
|
||||||
|
puts "promoList - " + promoList.size.to_s
|
||||||
|
if promoList.size > 0
|
||||||
|
itemList = combine_item(saleObj)
|
||||||
|
is_promo_day(promoList,day, itemList)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.is_between_promo_datetime(current_day,current_time) #database is not local time
|
||||||
|
promoList = Promotion.where("(TO_CHAR(promo_start_date, 'YYYY-MM-DD') <=? AND TO_CHAR(promo_end_date, 'YYYY-MM-DD') >=?) AND (promo_start_hour < ? AND promo_end_hour > ?)", current_day, current_day, current_time, current_time)
|
||||||
|
return promoList
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.combine_item(saleObj)
|
||||||
|
itemList = saleObj.sale_items.group(:product_code).sum(:qty)
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.is_promo_day(promoList, day, orderitemList)
|
||||||
|
puts "Today date - " + day.to_s
|
||||||
|
promoList.each do |promo|
|
||||||
|
dayresult = promo.promo_day.include?(day.to_s)
|
||||||
|
if dayresult
|
||||||
|
orderitemList.each do |item|
|
||||||
|
find_promo_item(promo, item)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.find_promo_item(promo, orderitem)
|
||||||
|
if promo.original_product.to_s == orderitem[0].to_s
|
||||||
|
if promo.min_qty.to_i > orderitem[1].to_i
|
||||||
|
return false
|
||||||
|
else
|
||||||
|
check_promo_type(promo,orderitem)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.check_promo_type(promo, orderitem)
|
||||||
|
if promo.promo_type == Promotion::PROMO_TYPE1
|
||||||
|
same, foc_min_qty = check_giveaway_product(promo, orderitem[0])
|
||||||
|
if same
|
||||||
|
give_promotion_same_product(orderitem[1], promo.min_qty, foc_min_qty, orderitem)
|
||||||
|
else
|
||||||
|
find_promo_item_in_orderlist
|
||||||
|
end
|
||||||
|
elsif promo.promo_type == Promotion::PROMO_TYPE2
|
||||||
|
|
||||||
|
elsif promo.promo_type == Promotion::PROMO_TYPE3
|
||||||
|
|
||||||
|
elsif promo.promo_type == Promotion::PROMO_TYPE4
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.check_giveaway_product(promo, orderitem)
|
||||||
|
promo.promotion_products.each do |promo_product|
|
||||||
|
if promo_product.item_code == orderitem
|
||||||
|
return true, promo_product.min_qty.to_i
|
||||||
|
else
|
||||||
|
return false, 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem)
|
||||||
|
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
|
||||||
|
charge_qty = 0
|
||||||
|
foc_qty = 0
|
||||||
|
if multiple > 0
|
||||||
|
multiple.times.each do |key|
|
||||||
|
if qty >= promoqty
|
||||||
|
charge_qty += promoqty
|
||||||
|
different = qty - promoqty
|
||||||
|
qty = different
|
||||||
|
if different == 0
|
||||||
|
foc_qty += foc_min_qty
|
||||||
|
else
|
||||||
|
foc_qty += foc_min_qty
|
||||||
|
qty = qty - foc_min_qty
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if multiple == foc_qty
|
||||||
|
charge_qty += qty
|
||||||
|
else
|
||||||
|
charge_qty += qty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
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 "FOC - " + foc_qty.to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def find_promo_item_in_orderlist(promo_item_code, orderitemList)
|
||||||
|
orderitemList.each do |item|
|
||||||
|
if item.item_code == promo_item_code
|
||||||
|
give_promotion_second_product(item)
|
||||||
|
else
|
||||||
|
add_promotion_second_item
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def give_promotion_second_product(item, foc_min_qty)
|
||||||
|
if item.qty > foc_min_qty
|
||||||
|
|
||||||
|
else
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.update_existing_item(foc_qty, item)
|
||||||
|
|
||||||
|
sale_item = SaleItem.new
|
||||||
|
sale_item.product_code = item.item_code
|
||||||
|
sale_item.product_name = item.item_name + " (promotion)"
|
||||||
|
sale_item.product_alt_name = item.alt_name
|
||||||
|
sale_item.account_id = item.account_id
|
||||||
|
sale_item.remark = " Promotion "
|
||||||
|
|
||||||
|
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.price = foc_qty * item.price
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.add_promotion_second_item
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,116 +1,4 @@
|
|||||||
class Shop < ApplicationRecord
|
class Shop < ApplicationRecord
|
||||||
|
|
||||||
def promo_activate
|
|
||||||
current_day = Time.now.strftime("%Y-%d-%m")
|
|
||||||
current_time = Time.now.strftime('%H:%M')
|
|
||||||
day = Date.today.wday
|
|
||||||
promoList = is_between_promo_datetime(current_day,current_time)
|
|
||||||
if promoList.size > 0
|
|
||||||
itemList = combine_item(saleObj)
|
|
||||||
is_promo_day(promoList,day, itemList)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def is_between_promo_datetime(current_day,current_time)
|
|
||||||
promoList = Promotion.where('( promo_start_date < ? AND promo_end_date > ?) AND (promo_start_time < ? AND promo_end_time > ?)', current_day, current_day, current_time, current_time)
|
|
||||||
return promoList
|
|
||||||
end
|
|
||||||
|
|
||||||
def combine_item(saleObj)
|
|
||||||
itemList = saleObj.sale_items.group(:product_code).sum(:qty)
|
|
||||||
end
|
|
||||||
|
|
||||||
def is_promo_day(promoList, day, orderitemList)
|
|
||||||
promoList.each do |promo|
|
|
||||||
dayresult = promo.promo_day.include?(day)
|
|
||||||
if dayresult
|
|
||||||
orderitemList.each do |item|
|
|
||||||
find_promo_item(promo, item)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_promo_item(promo, orderitem)
|
|
||||||
if promo.prouduct_item == orderitem
|
|
||||||
if promo.minmum_qty < orderitem.qty
|
|
||||||
return false
|
|
||||||
else
|
|
||||||
same, promo_item_code = check_giveaway_product(promo, orderitem)
|
|
||||||
if same
|
|
||||||
give_promotion_same_product
|
|
||||||
else
|
|
||||||
find_promo_item_in_orderlist
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def check_giveaway_product(promo, orderitem)
|
|
||||||
promo.promotion_products.each do |promo_product|
|
|
||||||
if promo_product.item_code == orderitem.item_code
|
|
||||||
return true, promo_product.item_code
|
|
||||||
else
|
|
||||||
return false, promo_product.item_code
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.give_promotion_same_product(qty, promoqty, foc_min_qty)
|
|
||||||
multiple = qty / promoqty # loop count
|
|
||||||
charge_qty = 0
|
|
||||||
foc_qty = 0
|
|
||||||
if multiple > 0
|
|
||||||
multiple.times.each do |key|
|
|
||||||
if qty >= promoqty
|
|
||||||
charge_qty += promoqty
|
|
||||||
different = qty - promoqty
|
|
||||||
qty = different
|
|
||||||
if different == 0
|
|
||||||
Shop.add_promotion_item
|
|
||||||
foc_qty += foc_min_qty
|
|
||||||
else
|
|
||||||
foc_qty += foc_min_qty
|
|
||||||
qty = qty - foc_min_qty
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if multiple == foc_qty
|
|
||||||
charge_qty += qty
|
|
||||||
else
|
|
||||||
charge_qty += qty
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
charge_qty = qty
|
|
||||||
end
|
|
||||||
puts "Charged - " + charge_qty.to_s
|
|
||||||
puts "FOC - " + foc_qty.to_s
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_promo_item_in_orderlist(promo_item_code, orderitemList)
|
|
||||||
orderitemList.each do |item|
|
|
||||||
if item.item_code == promo_item_code
|
|
||||||
give_promotion_second_product(item)
|
|
||||||
else
|
|
||||||
add_promotion_second_item
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def give_promotion_second_product(item, foc_min_qty)
|
|
||||||
if item.qty > foc_min_qty
|
|
||||||
|
|
||||||
else
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.add_promotion_item
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.add_promotion_second_item
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user