class Origami::DiscountsController < BaseOrigamiController
  authorize_resource :class => false

  #discount page show from origami index with selected order
  def index
	  sale_id = params[:id]
    if Sale.exists?(sale_id)
      @sale_data = Sale.find(sale_id)
    end

    @accounts = Account.all
  end

  #discount page show from origami index with selected order
  def create
    sale_id = params[:sale_id]
    discount_items = JSON.parse(params[:discount_items])
    overall_discount = params[:overall_discount]
    sub_total = params[:sub_total]

    if Sale.exists?(sale_id)
      sale = Sale.find(sale_id)
      table_id = sale.bookings[0].dining_facility_id
      table_type = DiningFacility.find(table_id).type
      # sale.total_discount = overall_discount.to_f        
      # sale.total_amount = sub_total.to_f
      # sale.grand_total = (sub_total.to_f - overall_discount.to_f) + sale.total_tax;  
      # sale.save   
      if discount_items.length > 0
        #save sale item for discount
        discount_items.each do |di|
          origin_sale_item = SaleItem.find(di["id"])
          sale_item = SaleItem.new

          sale_item.sale_id = sale_id
          sale_item.product_code = origin_sale_item != nil ? origin_sale_item.product_code : sale_id
          sale_item.product_name = di["name"]
          sale_item.product_alt_name = ""
          sale_item.remark = "Discount"

          sale_item.qty = 1
          sale_item.unit_price = di["price"]
          sale_item.taxable_price = di["price"]
          sale_item.is_taxable = 0

          sale_item.price = di["price"]
          sale_item.save
        end                 
      end 

      # Re-calc All Amount in Sale
      sale.compute_by_sale_items(sale_id, sale.sale_items, overall_discount.to_f)     
    end  

    dining = {:table_id => table_id, :table_type => table_type }
    render :json => dining.to_json
  end

  # Remove selected discount Items
  def remove_discount_items
    sale_id = params[:sale_id]
    discount_items = JSON.parse(params[:discount_items])
    if Sale.exists?(sale_id)
      sale = Sale.find(sale_id)    
      table_id = sale.bookings[0].dining_facility_id
      table_type = DiningFacility.find(table_id).type
              
      if discount_items.length > 0       
        #destroy sale item for discount
        discount_items.each do |di|          
          sale_item = SaleItem.find(di["id"])                 
          sale.total_amount = (sale.total_amount + sale_item.price.abs)
          sale_item.destroy
        end                 
      end  

      # sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax;   
      # sale.save
      # Re-calc All Amount in Sale
      sale.compute_by_sale_items(sale_id, sale.sale_items, sale.total_discount)  
    end

    dining = {:table_id => table_id, :table_type => table_type }

    render :json => dining.to_json
  end

  # Remove all discount Items
  def remove_all_discount
    sale_id = params[:id]

    if Sale.exists?(sale_id)
      sale = Sale.find(sale_id)    
      table_id = sale.bookings[0].dining_facility_id
      table_type = DiningFacility.find(table_id).type
                    
      #destroy all discount sale item
      sale.sale_items.each do |si|  
        if si.remark == "Discount" && si.price < 0                                   
          sale.total_amount = (sale.total_amount + si.price.abs)
          si.destroy
        end
      end                 

      # sale.total_discount = 0
      # sale.grand_total = (sale.total_amount - sale.total_discount) + sale.total_tax;   
      # sale.save

      # Re-calc All Amount in Sale
      sale.compute_by_sale_items(sale_id, sale.sale_items, 0)
    end

    dining = {:table_id => table_id, :table_type => table_type }

    render :json => dining.to_json
  end

  #discount for selected order
  # def create
  # 	sale_id = params[:sale_id]
  #   sale_item_id = params[:sale_item_id]
  # 	discount_type = params[:discount_type]
  # 	discount_value = params[:discount_value]
  # 	discount_amount = params[:discount_amount]
  # 	grand_total = params[:grand_total]
  #   product_name = "Overall Discount"

  # 	if discount_type == 0
  # 		remark="Discount " + discount_amount + " as net"
  # 	else
  # 		remark="Discount " + discount_amount + " as percentage"
  # 	end

  # 	#update discount for sale
  # 	sale = Sale.find(sale_id)
  # 	sale.total_discount = sale.total_discount + discount_amount.to_f
  # 	sale.grand_total = grand_total
	 #  sale.save

  # 	#save sale item for discount
  #   if sale_item_id != nil
  #     origin_sale_item = SaleItem.find(sale_item_id)
  #     product_name = origin_sale_item.product_name + "-Disocunt"
  #   end
  #   sale_item = SaleItem.new

  #   #pull
  #   sale_item.sale_id = sale_id
  #   sale_item.product_code = origin_sale_item != nil ? origin_sale_item.product_code : sale_id
  #   sale_item.product_name = product_name
  #   sale_item.remark = remark

  #   sale_item.qty = 1
  #   sale_item.unit_price = (0-discount_amount.to_f)
  #   sale_item.taxable_price = discount_amount
  #   sale_item.is_taxable = 0

  #   sale_item.price = sale_item.qty * sale_item.unit_price
  #   sale_item.save

  #   redirect_to origami_path(sale_id)
  # end

end
