diff --git a/app/models/promotion.rb b/app/models/promotion.rb index d60f25a9..dfab86e9 100644 --- a/app/models/promotion.rb +++ b/app/models/promotion.rb @@ -23,7 +23,7 @@ class Promotion < ApplicationRecord end def self.is_between_promo_datetime(current_day,current_time) #database is not local time - promoList = Promotion.where("(date_format(promo_start_date, 'YYYY-MM-DD') <=? AND date_format(promo_end_date, 'YYYY-MM-DD') >=?) AND (promo_start_hour < ? AND promo_end_hour > ?)", current_day, current_day, current_time, current_time) + promoList = Promotion.where("(TO_CHAR(promo_start_date, 'YYYY-MM-DD') <=? AND TO_CHAR(promo_end_date, 'YYYY-MM-DD') >=?) AND (promo_start_hour < ? AND promo_end_hour > ?)", current_day, current_day, current_time, current_time) return promoList end @@ -45,8 +45,8 @@ class Promotion < ApplicationRecord end def self.find_promo_item(promo, orderitem, sale_id) - item_code = OrderItem.find_by_item_instance_code(orderitem[0]).item_code - if promo.original_product.to_s == item_code + # item_code = OrderItem.find_by_item_instance_code(orderitem[0]).item_code + if promo.original_product.to_s == orderitem[0] if promo.min_qty.to_i > orderitem[1].to_i return false else @@ -77,7 +77,7 @@ class Promotion < ApplicationRecord def self.check_giveaway_product(promo, orderitem) promo.promotion_products.each do |promo_product| - if promo_product.item_code == OrderItem.find_by_item_instance_code(orderitem).item_code + if promo_product.item_code == orderitem return true, promo_product else return false, promo_product @@ -113,12 +113,11 @@ class Promotion < ApplicationRecord charge_qty += qty end item = OrderItem.find_by_item_instance_code(orderitem[0]) - byebug - # if promo_product == OrderItem.find_by_item_instance_code(orderitem[0]).item_code - # item = OrderItem.find_by_item_instance_code(orderitem[0]) - # else - # item = OrderItem.find_by_item_code(promo_product) - # end + if promo_product == OrderItem.find_by_item_instance_code(orderitem[0]).item_code + item = OrderItem.find_by_item_instance_code(orderitem[0]) + else + item = OrderItem.find_by_item_code(promo_product) + end update_existing_item(foc_qty, item, sale_id, "promotion", item.price) puts "Charged - " + charge_qty.to_s @@ -127,12 +126,13 @@ class Promotion < ApplicationRecord # AA - 10 # 3 # BB # orderList, #S34345 def self.give_promotion_second_product(orderitem_count, foc_min_qty, promo_product, orderitem, sale_id) puts "..... orderitem_count: " + orderitem_count.to_s + " / foc_min_qty: " + foc_min_qty.to_s + " /promo_product: " + promo_product + " orderitem: " + orderitem.to_s + byebug 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) if (foc_qty < promotion_qty) promotion_qty = foc_qty end - item = OrderItem.find_by_item_instance_code(promo_product,orderID) + item = OrderItem.find_by_item_instance_code(promo_product) update_existing_item(promotion_qty, item, sale_id, "promotion", item.price) end @@ -152,7 +152,6 @@ class Promotion < ApplicationRecord sale_item.is_taxable = false sale_item.sale_id = sale_id - sale_item.save sale = Sale.find(sale_id) sale.compute_by_sale_items(sale.id, sale.sale_items, sale.total_discount) @@ -163,7 +162,7 @@ class Promotion < ApplicationRecord 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 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]) update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off) else @@ -213,7 +212,7 @@ class Promotion < ApplicationRecord saleObj = Sale.find_by_sale_id(sale_id) itemList = combine_item(saleObj) itemList.each do |item| - if OrderItem.find_by_item_instance_code(item[0]).item_code == promo_item + if item[0] == promo_item return item[1] end end