From 688ce932cc0ed3b8f0f4c2aaecfa650dbcf0c7df Mon Sep 17 00:00:00 2001 From: Nweni Date: Wed, 23 Aug 2017 18:07:58 +0630 Subject: [PATCH] promotion --- app/models/promotion.rb | 42 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/app/models/promotion.rb b/app/models/promotion.rb index 32b40ec4..ed727038 100644 --- a/app/models/promotion.rb +++ b/app/models/promotion.rb @@ -61,12 +61,15 @@ class Promotion < ApplicationRecord else give_promotion_second_product(orderitem[1], promo.min_qty, promo_product.item_code, orderitem, sale_id) end + elsif promo.promo_type == Promotion::PROMO_TYPE2 give_promotion_nett_off(same,promo_product,promo.min_qty, orderitem, sale_id) + elsif promo.promo_type == Promotion::PROMO_TYPE3 - # net price + give_promotion_nett_price(same,promo_product,promo.min_qty, orderitem, sale_id) + elsif promo.promo_type == Promotion::PROMO_TYPE4 - # Percentage + give_promotion_nett_price(same,promo_product,promo.min_qty, orderitem, sale_id) end end @@ -125,7 +128,7 @@ class Promotion < ApplicationRecord sale_item = SaleItem.new sale_item.product_code = item.item_code - sale_item.product_name = item.item_name + "("+ type +")" + sale_item.product_name = item.item_name + "(promotion)" sale_item.product_alt_name = item.alt_name sale_item.account_id = item.account_id sale_item.remark = type @@ -157,11 +160,37 @@ class Promotion < ApplicationRecord 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 + if same + foc_qty = orderitem[1].to_i / foc_min_qty + item = OrderItem.find_by_item_code(orderitem[0]) + price = item.price - promo_product.net_price + update_existing_item(foc_qty, item, sale_id, "promotion nett price", price) + else + foc_qty = find_second_item_qty(sale_id, promo_product.item_code) + item = OrderItem.find_by_item_code(promo_product.item_code) + price = item.price - promo_product.net_price + update_existing_item(foc_qty, item, sale_id, "promotion nett price", price) + end end - def self.give_promotion_discount() - + 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 + + if same + foc_qty = orderitem[1].to_i / foc_min_qty + item = OrderItem.find_by_item_code(orderitem[0]) + total = orderitem[1].to_i * item.price + price = calculate_discount(total, promo_product.percentage) + update_existing_item(foc_qty, item, sale_id, "promotion discount", price) + else + foc_qty = find_second_item_qty(sale_id, promo_product.item_code) + item = OrderItem.find_by_item_code(promo_product.item_code) + total = item.price * foc_qty + price = calculate_discount(total, promo_product.percentage) + update_existing_item(foc_qty, item, sale_id, "promotion discount", price) + end end def self.find_second_item_qty(sale_id, promo_item) @@ -174,4 +203,7 @@ class Promotion < ApplicationRecord end end + def self.calculate_discount(total, discount) + self (total.to_i * discount.to_i) / 100 + end end