From 9702299fa7b88df75a6936a3cbd027133b38b2f5 Mon Sep 17 00:00:00 2001 From: Nweni Date: Thu, 17 Aug 2017 17:40:18 +0630 Subject: [PATCH] Update --- app/models/shop.rb | 114 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/app/models/shop.rb b/app/models/shop.rb index 1cf1119c..dbb0930e 100644 --- a/app/models/shop.rb +++ b/app/models/shop.rb @@ -1,2 +1,116 @@ 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