Files
sx-fc/app/models/shop.rb
2017-08-17 17:40:18 +06:30

117 lines
2.9 KiB
Ruby

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