Promotion Setting Issue

This commit is contained in:
Myat Zin Wai Maw
2019-07-22 09:56:39 +06:30
parent a378caed08
commit fc0612b2f5

View File

@@ -1,3 +1,4 @@
#new_promotion
class Promotion < ApplicationRecord class Promotion < ApplicationRecord
validates_presence_of :promo_code,:promo_start_date,:promo_end_date,:promo_start_hour,:promo_end_hour,:promo_type,:original_product,:min_qty validates_presence_of :promo_code,:promo_start_date,:promo_end_date,:promo_start_hour,:promo_end_hour,:promo_type,:original_product,:min_qty
@@ -19,8 +20,28 @@ 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, saleObj.sale_id) itemList.each do|list|
list.each do |l|
array_same.push({
item_instance_code: l.item_instance_code,
order_id:l.order_id,
qty:l.qty
})
end
end
end end
item_list =[]
array_same.group_by do |same|
same.values_at :item_instance_code
end.map do |(item_instance_code), array_same|
quantities = array_same.map { |p| p[:qty] }
qty = quantities.all? ? quantities.reduce(:+) : nil
item_list.push({ item_instance_code: item_instance_code, qty: qty })
end
Rails.logger.debug "-------promo_activate array_same-------"
puts item_list.to_json
is_promo_day(promoList,day, item_list, saleObj.sale_id)
end end
def self.is_between_promo_datetime(current_day,current_time) #database is not local time def self.is_between_promo_datetime(current_day,current_time) #database is not local time
@@ -34,7 +55,8 @@ class Promotion < ApplicationRecord
# array_same =[] # array_same =[]
itemList = [] itemList = []
saleObj.sale_orders.each do |so| saleObj.sale_orders.each do |so|
itemList << OrderItem.where("order_id = ?",so.order_id).group(["item_instance_code", "order_id"]).sum(:qty) # itemList << OrderItem.where("order_id = ?",so.order_id).group(["item_instance_code", "order_id"]).sum(:qty)
itemList << OrderItem.where("order_id = ?",so.order_id).select(:item_instance_code,:order_id,:qty)
end end
return itemList return itemList
@@ -53,21 +75,21 @@ class Promotion < ApplicationRecord
end end
def self.find_promo_item(promo, orderitem, sale_id) def self.find_promo_item(promo, orderitem, sale_id)
# item_code = OrderItem.find_by_item_instance_code(orderitem[0][0]).item_code item_code = OrderItem.find_by_item_instance_code(orderitem[:item_instance_code]).item_code
orderitem.each do |odr_item| # orderitem.each do |odr_item|
if promo.original_product.downcase.to_s == odr_item[0][0] if promo.original_product.downcase.to_s == orderitem[:item_instance_code]
if promo.min_qty.to_i > odr_item[1].to_i if promo.min_qty.to_i > orderitem[:qty].to_i
return false return false
else else
check_promo_type(promo,odr_item, sale_id) check_promo_type(promo,orderitem, sale_id)
end end
end end
end # end
end end
def self.check_promo_type(promo, orderitem, sale_id) def self.check_promo_type(promo, orderitem, sale_id)
promo.promotion_products.each do |promo_product| promo.promotion_products.each do |promo_product|
if promo_product.item_code.downcase == orderitem[0][0] if promo_product.item_code.downcase == orderitem[:item_instance_code]
same = true same = true
else else
# return false, promo_product # return false, promo_product
@@ -76,9 +98,9 @@ class Promotion < ApplicationRecord
# same, promo_product = check_giveaway_product(promo, orderitem[0][0]) # same, promo_product = check_giveaway_product(promo, orderitem[0][0])
if promo.promo_type == Promotion::PROMO_TYPE1 if promo.promo_type == Promotion::PROMO_TYPE1
if same if same
give_promotion_same_product(orderitem[1], promo.min_qty, promo_product.min_qty, orderitem, sale_id,promo_product.item_code) give_promotion_same_product(orderitem[:qty], promo.min_qty, promo_product.min_qty, orderitem, sale_id,promo_product.item_code)
else else
give_promotion_second_product(orderitem[1], promo.min_qty, promo_product.item_code, orderitem, sale_id) give_promotion_second_product(orderitem[:qty], 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
@@ -107,6 +129,7 @@ class Promotion < ApplicationRecord
end end
def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem, sale_id,promo_product) def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem, sale_id,promo_product)
item =''
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
@@ -133,20 +156,44 @@ class Promotion < ApplicationRecord
else else
charge_qty += qty charge_qty += qty
end end
item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first sale_order =SaleOrder.where("sale_id=?",sale_id)
if promo_product.downcase != item.item_instance_code price =0
# item = item source =''
# else sale_order.each do|order|
# item = OrderItem.find_by_item_code(promo_product) item = OrderItem.where("item_instance_code = ? and order_id = ?",orderitem[:item_instance_code],order.order_id).first
item = OrderItem.where("item_code = '#{promo_product}' and order_id = '#{orderitem[0][1]}'").first source = Order.find(order.order_id).source
item ={
item_code:item.item_code,
item_instance_code:item.item_instance_code,
item_name:item.item_name,
alt_name:item.alt_name,
account_id:item.account_id,
price:item.price
}
end
if promo_product.downcase != item[:item_instance_code]
sale_order.each do|order|
item = OrderItem.where("item_code = ? and order_id = ?",promo_product,order.order_id).first
source = Order.find(order.order_id).source
item ={
item_code:item.item_code,
item_instance_code:item.item_instance_code,
item_name:item.item_name,
alt_name:item.alt_name,
account_id:item.account_id,
price:item.price
}
end
end end
if !item.nil? if !item.nil?
source = Order.find(item.order_id).source update_existing_item(foc_qty, item, sale_id, "promotion", item[:price],source)
update_existing_item(foc_qty, item, sale_id, "promotion", item.price,source)
end end
end end
# AA - 10 # 3 # BB # orderList, #S34345 # AA - 10 # 3 # BB # orderList, #S34345
def self.give_promotion_second_product(orderitem_count, foc_min_qty, promo_product, orderitem, sale_id) def self.give_promotion_second_product(orderitem_count, foc_min_qty, promo_product, orderitem, sale_id)
source =''
item =''
puts "..... orderitem_count: " + orderitem_count.to_s + " / foc_min_qty: " + foc_min_qty.to_s + " /promo_product: " + promo_product + " orderitem: " + orderitem.to_s puts "..... orderitem_count: " + orderitem_count.to_s + " / foc_min_qty: " + foc_min_qty.to_s + " /promo_product: " + promo_product + " orderitem: " + orderitem.to_s
promotion_qty = orderitem_count.to_i / foc_min_qty.to_i # get foc item qty promotion_qty = orderitem_count.to_i / foc_min_qty.to_i # get foc item qty
foc_qty = find_second_item_qty(sale_id, promo_product) foc_qty = find_second_item_qty(sale_id, promo_product)
@@ -154,27 +201,42 @@ class Promotion < ApplicationRecord
promotion_qty = foc_qty promotion_qty = foc_qty
end end
# item = OrderItem.find_by_item_instance_code(promo_product) # item = OrderItem.find_by_item_instance_code(promo_product)
item = OrderItem.where("item_instance_code = '#{promo_product}' and order_id = '#{orderitem[0][1]}'").first sale_order =SaleOrder.where("sale_id=?",sale_id)
price =0
sale_order.each do|order|
item = OrderItem.where("item_instance_code = ? and order_id = ?",orderitem[:item_instance_code],order.order_id).first
price +=item.price
source = Order.find(order.order_id).source
item ={
item_code:item.item_code,
item_instance_code:item.item_instance_code,
item_name:item.item_name,
alt_name:item.alt_name,
account_id:item.account_id,
price:item.price
}
end
# item = OrderItem.where("item_instance_code = '#{promo_product}' and order_id = '#{orderitem[0][1]}'").first
if !item.nil? if !item.nil?
source = Order.find(item.order_id).source # source = Order.find(item.order_id).source
update_existing_item(promotion_qty, item, sale_id, "promotion", item.price,source) update_existing_item(promotion_qty, item, sale_id, "promotion", item.price,source)
end end
end end
def self.update_existing_item(qty, item, sale_id, type, item_price,source) def self.update_existing_item(qty, item, sale_id, type, item_price,source)
menu_category = MenuCategory.get_menu_category(item.item_code) #get menu category for menu items menu_category = MenuCategory.get_menu_category(item[:item_code]) #get menu category for menu items
sale_item = SaleItem.new sale_item = SaleItem.new
if !menu_category.nil? if !menu_category.nil?
sale_item.menu_category_code = menu_category.code sale_item.menu_category_code = menu_category.code
sale_item.menu_category_name = menu_category.name sale_item.menu_category_name = menu_category.name
end end
sale_item.product_code = item.item_code sale_item.product_code = item[:item_code]
sale_item.item_instance_code = item.item_instance_code sale_item.item_instance_code = item[:item_instance_code]
sale_item.product_name = item.item_name + "(promotion)" sale_item.product_name = item[:item_name] + "(promotion)"
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 = type sale_item.remark = type
sale_item.status = "promotion" sale_item.status = "promotion"
sale_item.qty = qty * (-1) sale_item.qty = qty * (-1)
@@ -193,22 +255,50 @@ class Promotion < ApplicationRecord
def self.give_promotion_nett_off(same, promo_product, foc_min_qty, orderitem, sale_id) 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 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
sale_order =SaleOrder.where("sale_id=?",sale_id)
if same if same
foc_qty = orderitem[1].to_i / foc_min_qty foc_qty = orderitem[:qty].to_i / foc_min_qty
# item = OrderItem.find_by_item_instance_code(orderitem[0][0]) # item = OrderItem.find_by_item_instance_code(orderitem[0][0])
item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first
price =0
sale_order.each do|order|
item = OrderItem.where("item_instance_code = ? and order_id = ?",orderitem[:item_instance_code],order.order_id).first
price +=item.price
source = Order.find(order.order_id).source
item ={
item_code:item.item_code,
item_instance_code:item.item_instance_code,
item_name:item.item_name,
alt_name:item.alt_name,
account_id:item.account_id,
price:item.price
}
end
# item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first
if !item.nil? if !item.nil?
source = Order.find(item.order_id).source # source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source) update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source)
end end
else else
foc_qty = orderitem[1].to_i / foc_min_qty foc_qty = orderitem[:qty].to_i / foc_min_qty
# foc_qty = find_second_item_qty(sale_id, promo_product.item_code) # foc_qty = find_second_item_qty(sale_id, promo_product.item_code)
# item = OrderItem.find_by_item_instance_code(promo_product.item_code) # item = OrderItem.find_by_item_instance_code(promo_product.item_code)
item = OrderItem.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first # item = OrderItem.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first
sale_order.each do|order|
item = OrderItem.where("item_instance_code = ? and order_id = ?",promo_product.item_code,order.order_id).first
source = Order.find(order.order_id).source
item ={
item_code:item.item_code,
item_instance_code:item.item_instance_code,
item_name:item.item_name,
alt_name:item.alt_name,
account_id:item.account_id,
price:item.price
}
end
if !item.nil? if !item.nil?
source = Order.find(item.order_id).source # source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source) update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source)
end end
@@ -217,28 +307,58 @@ class Promotion < ApplicationRecord
def self.give_promotion_nett_price(same, promo_product, foc_min_qty, orderitem, sale_id) def self.give_promotion_nett_price(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 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
sale_order =SaleOrder.where("sale_id=?",sale_id)
source =''
item =''
price =0
if same if same
foc_qty = orderitem[1].to_i / foc_min_qty foc_qty = orderitem[1].to_i / foc_min_qty
# item = OrderItem.find_by_item_instance_code(orderitem[0][0]) # need to specify with menu item instance # item = OrderItem.find_by_item_instance_code(orderitem[0][0]) # need to specify with menu item instance
item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first # item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first
sale_order.each do|order|
item = OrderItem.where("item_instance_code = ? and order_id = ?",orderitem[:item_instance_code],order.order_id).first
source = Order.find(order.order_id).source
price +=item.price
item ={
item_code:item.item_code,
item_instance_code:item.item_instance_code,
item_name:item.item_name,
alt_name:item.alt_name,
account_id:item.account_id,
price:item.price
}
end
if !item.nil? if !item.nil?
price = item.price.to_i - promo_product.net_price.to_i price = item[:price].to_i - promo_product.net_price.to_i
source = Order.find(item.order_id).source # source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source) update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source)
end end
else else
order_qty = find_second_item_qty(sale_id, promo_product.item_code)# need to check for qty order_qty = find_second_item_qty(sale_id, promo_product.item_code)# need to check for qty
foc_qty = orderitem[1].to_i / foc_min_qty foc_qty = orderitem[:qty].to_i / foc_min_qty
if foc_qty > order_qty if foc_qty > order_qty
foc_qty = order_qty foc_qty = order_qty
end end
# item = OrderItem.find_by_item_instance_code(promo_product.item_code) # item = OrderItem.find_by_item_instance_code(promo_product.item_code)
item = OrderItem.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first # item = OrderItem.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first
sale_order.each do|order|
item = OrderItem.where("item_instance_code = ? and order_id = ?",promo_product.item_code,order.order_id).first
source = Order.find(order.order_id).source
price +=item.price
item ={
item_code:item.item_code,
item_instance_code:item.item_instance_code,
item_name:item.item_name,
alt_name:item.alt_name,
account_id:item.account_id,
price:item.price
}
end
if !item.nil? if !item.nil?
price = item.price - promo_product.net_price price = item[:price].to_i - promo_product.net_price.to_i
source = Order.find(item.order_id).source # source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source) update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source)
end end
@@ -247,15 +367,32 @@ class Promotion < ApplicationRecord
def self.give_promotion_discount(same, promo_product, foc_min_qty, orderitem, sale_id) def self.give_promotion_discount(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 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
sale_order =SaleOrder.where("sale_id=?",sale_id)
source =''
item =''
price =0
if same if same
foc_qty = orderitem[1].to_i / foc_min_qty foc_qty = orderitem[:qty].to_i / foc_min_qty
# item = OrderItem.find_by_item_instance_code(orderitem[0][0]) # item = OrderItem.find_by_item_instance_code(orderitem[0][0])
item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first # item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first
sale_order.each do|order|
item = OrderItem.where("item_instance_code = ? and order_id = ?",orderitem[:item_instance_code],order.order_id).first
source = Order.find(order.order_id).source
price +=item.price
item ={
item_code:item.item_code,
item_instance_code:item.item_instance_code,
item_name:item.item_name,
alt_name:item.alt_name,
account_id:item.account_id,
price:item.price
}
end
# total = orderitem[1].to_i * item.price # total = orderitem[1].to_i * item.price
if !item.nil? if !item.nil?
total = item.price total = item[:price]
price = calculate_discount(total, promo_product.percentage) price = calculate_discount(total, promo_product.percentage)
source = Order.find(item.order_id).source # source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion discount", price,source) update_existing_item(foc_qty, item, sale_id, "promotion discount", price,source)
end end
@@ -268,12 +405,24 @@ class Promotion < ApplicationRecord
foc_qty = order_qty foc_qty = order_qty
end end
# item = OrderItem.find_by_item_instance_code(promo_product.item_code) # item = OrderItem.find_by_item_instance_code(promo_product.item_code)
item = OrderItem.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first # item = OrderItem.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first
sale_order.each do|order|
item = OrderItem.where("item_instance_code = ? and order_id = ?",promo_product.item_code,order.order_id).first
source = Order.find(order.order_id).source
item ={
item_code:item.item_code,
item_instance_code:item.item_instance_code,
item_name:item.item_name,
alt_name:item.alt_name,
account_id:item.account_id,
price:item.price
}
end
# total = item.price * foc_qty # total = item.price * foc_qty
if !item.nil? if !item.nil?
total = item.price total = item[:price]
price = calculate_discount(total, promo_product.percentage) price = calculate_discount(total, promo_product.percentage)
source = Order.find(item.order_id).source # source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion discount", price,source) update_existing_item(foc_qty, item, sale_id, "promotion discount", price,source)
end end
end end