From de769f3faf46e22cc6b477cd859fa9206c4fef82 Mon Sep 17 00:00:00 2001 From: Nweni Date: Wed, 23 Aug 2017 17:37:13 +0630 Subject: [PATCH] promotion --- app/controllers/api/bill_controller.rb | 2 +- app/models/promotion.rb | 127 ++++++++++++++----------- 2 files changed, 74 insertions(+), 55 deletions(-) diff --git a/app/controllers/api/bill_controller.rb b/app/controllers/api/bill_controller.rb index 7ee80f00..2b323d0f 100644 --- a/app/controllers/api/bill_controller.rb +++ b/app/controllers/api/bill_controller.rb @@ -24,7 +24,7 @@ class Api::BillController < Api::ApiController @sale = Sale.new @status, @sale_id = @sale.generate_invoice_from_order(params[:order_id], current_login_employee, get_cashier) end - # Promotion.promo_activate(@sale) + Promotion.promo_activate(@sale) else @status = false @error_message = "No Current Open Shift" diff --git a/app/models/promotion.rb b/app/models/promotion.rb index 778f7638..32b40ec4 100644 --- a/app/models/promotion.rb +++ b/app/models/promotion.rb @@ -6,8 +6,8 @@ class Promotion < ApplicationRecord accepts_nested_attributes_for :promotion_products , :allow_destroy => true PROMO_TYPE1 = "Quantity" - PROMO_TYPE2 = "Net_off" - PROMO_TYPE3 = "Net_price" + PROMO_TYPE2 = "Net_off" # 3000 => - 500 => 2500 [total] + PROMO_TYPE3 = "Net_price" # 1800 => 1000 => 1000 PROMO_TYPE4 = "Percentage" def self.promo_activate(saleObj) @@ -18,7 +18,7 @@ class Promotion < ApplicationRecord puts "promoList - " + promoList.size.to_s if promoList.size > 0 itemList = combine_item(saleObj) - is_promo_day(promoList,day, itemList) + is_promo_day(promoList,day, itemList, saleObj.sale_id) end end @@ -31,63 +31,63 @@ class Promotion < ApplicationRecord itemList = saleObj.sale_items.group(:product_code).sum(:qty) end - def self.is_promo_day(promoList, day, orderitemList) + 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) + find_promo_item(promo, item, sale_id) end end end end - def self.find_promo_item(promo, orderitem) + def self.find_promo_item(promo, orderitem, sale_id) if promo.original_product.to_s == orderitem[0].to_s if promo.min_qty.to_i > orderitem[1].to_i return false else - check_promo_type(promo,orderitem) + check_promo_type(promo,orderitem, sale_id) end end end - def self.check_promo_type(promo, orderitem) + def self.check_promo_type(promo, orderitem, sale_id) + same, promo_product = check_giveaway_product(promo, orderitem[0]) if promo.promo_type == Promotion::PROMO_TYPE1 - same, foc_min_qty = check_giveaway_product(promo, orderitem[0]) if same - give_promotion_same_product(orderitem[1], promo.min_qty, foc_min_qty, orderitem) + give_promotion_same_product(orderitem[1], promo.min_qty, promo_product.min_qty, orderitem, sale_id) else - find_promo_item_in_orderlist + 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 elsif promo.promo_type == Promotion::PROMO_TYPE4 - + # Percentage end end def self.check_giveaway_product(promo, orderitem) promo.promotion_products.each do |promo_product| if promo_product.item_code == orderitem - return true, promo_product.min_qty.to_i + return true, promo_product else - return false, 0 + return false, promo_product end end end - def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem) + def self.give_promotion_same_product(qty, promoqty, foc_min_qty, orderitem, sale_id) 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 + if qty > promoqty charge_qty += promoqty different = qty - promoqty qty = different @@ -98,61 +98,80 @@ class Promotion < ApplicationRecord qty = qty - foc_min_qty end else - if multiple == foc_qty - charge_qty += qty - else - charge_qty += qty - end + charge_qty += qty end end + if multiple == foc_qty + charge_qty += qty + end else - charge_qty = qty - end - if qty == promoqty - update_existing_item(foc_qty, orderitem) - else - update_existing_item(foc_qty, orderitem) + charge_qty += qty end + item = OrderItem.find_by_item_code(orderitem[0]) + update_existing_item(foc_qty, item, sale_id, "promotion", item.price) + puts "Charged - " + charge_qty.to_s puts "FOC - " + foc_qty.to_s end - - def find_promo_item_in_orderlist(promo_item_code, orderitemList) - orderitemList.each do |item| - if item.item_code == promo_item_code - give_promotion_second_product(item) - else - add_promotion_second_item - end - end + # 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 + promotion_qty = orderitem_count.to_i / foc_min_qty.to_i # get foc item qty + item = OrderItem.find_by_item_code(promo_product) + update_existing_item(promotion_qty, item, sale_id, "promotion", item.price) end - def give_promotion_second_product(item, foc_min_qty) - if item.qty > foc_min_qty - - else - - end - end - - def self.update_existing_item(foc_qty, item) + def self.update_existing_item(foc_qty, item, sale_id, type, item_price) sale_item = SaleItem.new sale_item.product_code = item.item_code - sale_item.product_name = item.item_name + " (promotion)" + sale_item.product_name = item.item_name + "("+ type +")" sale_item.product_alt_name = item.alt_name sale_item.account_id = item.account_id - sale_item.remark = " Promotion " + sale_item.remark = type + sale_item.qty = foc_qty * (-1) + + sale_item.unit_price = item_price * (-1) + sale_item.taxable_price = item_price + sale_item.price = foc_qty * item_price * (-1) - sale_item.qty = foc_qty - sale_item.unit_price = item.price - sale_item.taxable_price = item.price sale_item.is_taxable = item.taxable + sale_item.sale_id = sale_id - sale_item.price = foc_qty * item.price + sale_item.save end - def self.add_promotion_second_item + + 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 + + if same + foc_qty = orderitem[1].to_i / foc_min_qty + item = OrderItem.find_by_item_code(orderitem[0]) + update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off) + else + foc_qty = find_second_item_qty(sale_id, promo_product.item_code) + item = OrderItem.find_by_item_code(promo_product.item_code) + update_existing_item(foc_qty, item, sale_id, "promotion nett off", promo_product.net_off) + end + end + + def self.give_promotion_nett_price(same, promo_product, foc_min_qty, orderitem, sale_id) end + + def self.give_promotion_discount() + + end + + def self.find_second_item_qty(sale_id, promo_item) + saleObj = Sale.find_by_sale_id(sale_id) + itemList = combine_item(saleObj) + itemList.each do |item| + if item[0] == promo_item + return item[1] + end + end + end + end