From 3d110774bad5b83ec06a16d2c53d0ce1e8757150 Mon Sep 17 00:00:00 2001 From: Myat Zin Wai Maw Date: Thu, 25 Jul 2019 14:29:46 +0630 Subject: [PATCH] promotion issue second item --- app/models/promotion.rb | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app/models/promotion.rb b/app/models/promotion.rb index f5fbaf9e..e6487afb 100755 --- a/app/models/promotion.rb +++ b/app/models/promotion.rb @@ -426,14 +426,30 @@ class Promotion < ApplicationRecord 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 |item| - item.each do |i| - if i[0][0] == promo_item.downcase - return i[1] + 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 + }) + 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 end return 0 end