From 58cb9570786b3b25590ac3dfb2c221f06ad10fca Mon Sep 17 00:00:00 2001 From: Aung Myo Date: Thu, 7 Jun 2018 16:13:10 +0630 Subject: [PATCH] update promotion for nil item --- app/models/promotion.rb | 69 +++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/app/models/promotion.rb b/app/models/promotion.rb index 5bc6a686..4271045e 100755 --- a/app/models/promotion.rb +++ b/app/models/promotion.rb @@ -139,11 +139,10 @@ class Promotion < ApplicationRecord # item = OrderItem.find_by_item_code(promo_product) item = OrderItem.where("item_code = '#{promo_product}' and order_id = '#{orderitem[0][1]}'").first end - source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion", item.price,source) - - puts "Charged - " + charge_qty.to_s - puts "FOC - " + foc_qty.to_s + if !item.nil? + source = Order.find(item.order_id).source + 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) @@ -155,9 +154,10 @@ class Promotion < ApplicationRecord end # item = OrderItem.find_by_item_instance_code(promo_product) item = OrderItem.where("item_instance_code = '#{promo_product}' and order_id = '#{orderitem[0][1]}'").first - - source = Order.find(item.order_id).source - update_existing_item(promotion_qty, item, sale_id, "promotion", item.price,source) + if !item.nil? + source = Order.find(item.order_id).source + update_existing_item(promotion_qty, item, sale_id, "promotion", item.price,source) + end end @@ -190,15 +190,21 @@ class Promotion < ApplicationRecord foc_qty = orderitem[1].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 - source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source) + 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[1].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 - source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off,source) + 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 @@ -208,9 +214,12 @@ class Promotion < ApplicationRecord 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.where("item_instance_code = '#{orderitem[0][0]}' and order_id = '#{orderitem[0][1]}'").first - 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) + 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[1].to_i / foc_min_qty @@ -220,9 +229,12 @@ class Promotion < ApplicationRecord 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 - price = item.price - promo_product.net_price - source = Order.find(item.order_id).source - update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source) + if !item.nil? + price = item.price - promo_product.net_price + source = Order.find(item.order_id).source + update_existing_item(foc_qty, item, sale_id, "promotion nett price", price,source) + end + end end @@ -233,10 +245,13 @@ class Promotion < ApplicationRecord # 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 # total = orderitem[1].to_i * item.price - 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) + 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[1].to_i / foc_min_qty @@ -248,10 +263,12 @@ class Promotion < ApplicationRecord # 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 # total = item.price * foc_qty - 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) + 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 end