rounding decimal to print_settings precision

This commit is contained in:
Thein Lin Kyaw
2019-11-06 17:34:42 +06:30
parent 33670b450e
commit 155cb0a9eb
11 changed files with 467 additions and 802 deletions

View File

@@ -11,37 +11,23 @@ class Promotion < ApplicationRecord
PROMO_TYPE3 = "Net_price" # 1800 => 1000 => 1000
PROMO_TYPE4 = "Percentage"
def is_promo_day
promo_day.include? Date.today.wday.to_s
end
def self.promo_activate(saleObj)
array_same =[]
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)
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
})
promoList.each do |promo|
if promo.is_promo_day
if item = saleObj.qty_of(promo.original_product)
check_promo_type(promo, item, saleObj)
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
def self.is_between_promo_datetime(current_day,current_time) #database is not local time
@@ -49,45 +35,7 @@ class Promotion < ApplicationRecord
return promoList
end
def self.combine_item(saleObj)
# order_id = saleObj.sale_orders[0][0].order_id
# itemList = OrderItem.where("order_id = ?", order_id).group(:item_instance_code).sum(:qty)
# array_same =[]
itemList = []
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).select(:item_instance_code,:order_id,:qty)
end
return itemList
end
def self.is_promo_day(promoList, day, orderitemList, sale_id)
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, sale_id)
end
end
end
end
def self.find_promo_item(promo, orderitem, sale_id)
item_code = OrderItem.find_by_item_instance_code(orderitem[:item_instance_code]).item_code
# orderitem.each do |odr_item|
if promo.original_product.downcase.to_s == orderitem[:item_instance_code]
if promo.min_qty.to_i > orderitem[:qty].to_i
return false
else
check_promo_type(promo,orderitem, sale_id)
end
end
# end
end
def self.check_promo_type(promo, orderitem, sale_id)
def self.check_promo_type(promo, orderitem, saleObj)
promo.promotion_products.each do |promo_product|
if promo_product.item_code.downcase == orderitem[:item_instance_code]
same = true
@@ -97,20 +45,20 @@ class Promotion < ApplicationRecord
end
# same, promo_product = check_giveaway_product(promo, orderitem[0][0])
if promo.promo_type == Promotion::PROMO_TYPE1
if same
give_promotion_same_product(orderitem[:qty], promo.min_qty, promo_product.min_qty, orderitem, sale_id,promo_product.item_code)
else
give_promotion_second_product(orderitem[:qty], promo.min_qty, promo_product.item_code, orderitem, sale_id)
if same
give_promotion_same_product(orderitem[:qty], promo.min_qty, promo_product.min_qty, orderitem, saleObj, promo_product.item_code)
else
give_promotion_second_product(orderitem[:qty], promo.min_qty, promo_product.item_code, orderitem, saleObj)
end
elsif promo.promo_type == Promotion::PROMO_TYPE2
give_promotion_nett_off(same,promo_product,promo.min_qty, orderitem, sale_id)
give_promotion_nett_off(same,promo_product,promo.min_qty, orderitem, saleObj)
elsif promo.promo_type == Promotion::PROMO_TYPE3
give_promotion_nett_price(same,promo_product,promo.min_qty, orderitem, sale_id)
give_promotion_nett_price(same,promo_product,promo.min_qty, orderitem, saleObj)
elsif promo.promo_type == Promotion::PROMO_TYPE4
give_promotion_discount(same,promo_product,promo.min_qty, orderitem, sale_id)
give_promotion_discount(same,promo_product,promo.min_qty, orderitem, saleObj)
end
end
end
@@ -128,106 +76,66 @@ class Promotion < ApplicationRecord
# end
end
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
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
def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem, saleObj,promo_product)
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
charge_qty += qty
foc_qty += foc_min_qty
qty = qty - foc_min_qty
end
else
charge_qty += qty
end
if multiple == foc_qty
charge_qty += qty
end
else
end
if multiple == foc_qty
charge_qty += qty
end
sale_order =SaleOrder.where("sale_id=?",sale_id)
price =0
source =''
sale_order.each do|order|
source = Order.find(order.order_id).source
end
else
charge_qty += qty
end
item = OrderItem.where("item_instance_code = ?",orderitem[:item_instance_code]).order("order_items_id DESC").first
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
}
if promo_product.downcase != item[:item_instance_code]
price = 0
source = saleObj.orders.first.source
item = saleObj.order_items.where(item_instance_code: promo_product).first
sale_order.each do|order|
source = Order.find(order.order_id).source
end
item = OrderItem.where("item_code = ?",promo_product).order("order_items_id DESC").first
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?
update_existing_item(foc_qty, item, sale_id, "promotion", item[:price],source)
end
if !item.nil?
update_existing_item(foc_qty, item, sale_id, "promotion", item[:price], source)
end
end
# AA - 10 # 3 # BB # orderList, #S34345
def self.give_promotion_second_product(orderitem_count, foc_min_qty, promo_product, orderitem, sale_id)
source =''
item =''
def self.give_promotion_second_product(orderitem_count, foc_min_qty, promo_product, orderitem, saleObj)
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
foc_qty = find_second_item_qty(sale_id, promo_product)
foc_qty = saleObj.qty_of(promo_product).qty rescue 0
if (foc_qty < promotion_qty)
promotion_qty = foc_qty
end
# item = OrderItem.find_by_item_instance_code(promo_product)
sale_order =SaleOrder.where("sale_id=?",sale_id)
price =0
sale_order.each do|order|
source = Order.find(order.order_id).source
end
item = OrderItem.where("item_instance_code = ?",promo_product).order("order_items_id DESC").first
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
}
# item = OrderItem.where("item_instance_code = '#{promo_product}' and order_id = '#{orderitem[0][1]}'").first
price = 0
source = saleObj.orders.first.source
item = saleObj.order_items.where(item_instance_code: promo_product).first
if !item.nil?
# source = Order.find(item.order_id).source
update_existing_item(promotion_qty, item, sale_id, "promotion", item[:price],source)
end
end
def self.update_existing_item(qty, item, sale_id, type, item_price,source)
if qty >0
def self.update_existing_item(qty, item, saleObj, type, item_price,source)
if qty > 0
menu_category = MenuCategory.get_menu_category(item[:item_code]) #get menu category for menu items
sale_item = SaleItem.new
if !menu_category.nil?
sale_item.menu_category_code = menu_category.code
@@ -247,216 +155,84 @@ class Promotion < ApplicationRecord
sale_item.price = qty * item_price * (-1)
sale_item.is_taxable = 1
sale_item.sale_id = sale_id
sale_item.sale = saleObj
sale_item.save
sale = Sale.find(sale_id)
sale.compute_by_sale_items(sale.id, sale.sale_items, sale.total_discount,nil,source)
saleObj.compute_by_sale_items(saleObj.id, saleObj.sale_items, saleObj.total_discount,nil,source)
end
end
def self.give_promotion_nett_off(same, promo_product, foc_min_qty, orderitem, sale_id)
item =''
source =''
def self.give_promotion_nett_off(same, promo_product, foc_min_qty, orderitem, saleObj)
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
foc_qty = orderitem[:qty].to_i / foc_min_qty
# item = OrderItem.find_by_item_instance_code(orderitem[0][0])
price =0
sale_order.each do|order|
source = Order.find(order.order_id).source
end
item = OrderItem.where("item_instance_code = ?",orderitem[:item_instance_code]).order("order_items_id DESC").first
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
}
# item = OrderItem.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first
if !item.nil?
# source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source)
end
else
foc_qty = orderitem[:qty].to_i / foc_min_qty
# 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.where("item_instance_code = '#{promo_product.item_code}' and order_id = '#{orderitem[0][1]}'").first
sale_order.each do|order|
source = Order.find(order.order_id).source
end
item = OrderItem.where("item_instance_code = ?",promo_product.item_code).order("order_items_id DESC").first
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
}
if !item.nil?
# source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source)
end
end
end
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
sale_order =SaleOrder.where("sale_id=?",sale_id)
source =''
item =''
if same
foc_qty = orderitem[:qty].to_i / foc_min_qty
# 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
sale_order.each do|order|
source = Order.find(order.order_id).source
end
item = OrderItem.where("item_instance_code = ?",orderitem[:item_instance_code]).order("order_items_id DESC").first
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
}
if !item.nil?
price = item[:price].to_i - promo_product.net_price.to_i
# source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source)
end
else
order_qty = find_second_item_qty(sale_id, promo_product.item_code)# need to check for qty
foc_qty = orderitem[:qty].to_i / foc_min_qty
if !same
order_qty = saleObj.qty_of(promo_product.item_code).qty rescue 0
foc_qty = orderitem[:qty].to_i / foc_min_qty
if foc_qty > order_qty
foc_qty = order_qty
end
# 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
sale_order.each do|order|
source = Order.find(order.order_id).source
end
item = OrderItem.where("item_instance_code",promo_product.item_code).order("order_items_id DESC").first
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?
price = item[:price].to_i - promo_product.net_price.to_i
# source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source)
end
source = saleObj.orders.first.source
item = saleObj.order_items.where(item_instance_code: promo_product.item_code).first
if !item.nil?
update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source)
end
end
def self.give_promotion_discount(same, promo_product, foc_min_qty, orderitem, sale_id)
def self.give_promotion_nett_price(same, promo_product, foc_min_qty, orderitem, saleObj)
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 =''
if same
foc_qty = orderitem[:qty].to_i / foc_min_qty
if !same
order_qty = saleObj.qty_of(promo_product.item_code).qty rescue 0
foc_qty = orderitem[:qty].to_i / foc_min_qty
# 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
sale_order.each do|order|
source = Order.find(order.order_id).source
end
item = OrderItem.where("item_instance_code = ?",orderitem[:item_instance_code]).order("order_items_id DESC").first
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
}
# total = orderitem[1].to_i * item.price
if !item.nil?
total = item[:price]
price = calculate_discount(total, promo_product.percentage)
# source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion discount", price,source)
end
else
order_qty = find_second_item_qty(sale_id, promo_product.item_code) #need to check
foc_qty = orderitem[:qty].to_i / foc_min_qty
# give total qty is 1
#foc_qty = (foc_qty - foc_qty) + 1
if foc_qty > order_qty
foc_qty = order_qty
end
# 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
sale_order.each do|order|
source = Order.find(order.order_id).source
end
item = OrderItem.where("item_instance_code = ?",promo_product.item_code).order("order_items_id DESC").first
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
}
# total = item.price * foc_qty
if !item.nil?
total = item[:price]
price = calculate_discount(total, promo_product.percentage)
# source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, sale_id, "promotion discount", price,source)
end
end
source = saleObj.orders.first.source
item = saleObj.order_items.where(item_instance_code: promo_product.item_code).first
if !item.nil?
price = item[:price].to_i - promo_product.net_price.to_i
update_existing_item(foc_qty, item, saleObj, "promotion nett price", price,source)
end
end
def self.find_second_item_qty(sale_id, promo_item)
array_same =[]
saleObj = Sale.find_by_sale_id(sale_id)
itemList = combine_item(saleObj)
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
})
def self.give_promotion_discount(same, promo_product, foc_min_qty, orderitem, saleObj)
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
foc_qty = orderitem[:qty].to_i / foc_min_qty
if !same
order_qty = saleObj.qty_of(promo_product.item_code).qty rescue 0
foc_qty = orderitem[:qty].to_i / foc_min_qty
if foc_qty > order_qty
foc_qty = order_qty
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
item_list.each do |item|
if item[:item_instance_code] == promo_item.downcase
return item[:qty].to_i
end
end
return 0
end
def self.calculate_discount(total, percentage)
return (total.to_i * percentage.to_i) / 100
end
source = saleObj.orders.first.source
item = saleObj.order_items.where(item_instance_code: promo_product.item_code).first
puts saleObj
puts item
# total = orderitem[1].to_i * item.price
if item
total = item[:price]
price = calculate_discount(total, promo_product.percentage)
# source = Order.find(item.order_id).source
update_existing_item(foc_qty, item, saleObj, "promotion discount", price,source)
end
end
def self.calculate_discount(total, percentage)
return (total * percentage) / 100
end
end