class Origami::SaleEditController < BaseOrigamiController
  authorize_resource class: false
  # Index for sale item void OR edit
  def edit
    sale_id     = params[:sale_id]
    if params[:table_id]
      @table_id   = params[:table_id]
      @table_type = DiningFacility.find(@table_id).type
    else
      @table_id = nil
      @table_type = nil
    end
    
    @cashier_type = params[:type]
    @saleobj    = Sale.find(sale_id)
  end

  # create item void. make duplicate old record and update qty and price
  def item_void
    saleitemId = params[:sale_item_id]
    remark     = params[:remark]
    access_code = params[:access_code]
    order_source = params[:type]
    saleitemObj = SaleItem.find(saleitemId)
    saleitemObj.status = 'void'
    saleitemObj.remark = remark
    saleitemObj.save
    @newsaleitem = SaleItem.new
    @newsaleitem = saleitemObj.dup
    @newsaleitem.save
    @newsaleitem.qty = saleitemObj.qty * -1
    @newsaleitem.price = saleitemObj.price * -1
    @newsaleitem.is_taxable = 1
    @newsaleitem.taxable_price = saleitemObj.taxable_price * -1
    @newsaleitem.product_name = saleitemObj.product_name + ' (VOID)'
    @newsaleitem.remark = remark
    @newsaleitem.save

    # re-calc tax
    saleObj = Sale.find(saleitemObj.sale_id)

    
    booking = Booking.find_by_sale_id(saleitemObj.sale_id)
    booking.booking_orders.each do |bo|
      order = Order.find(bo.order_id)
      order.order_items.each do |o|
        if saleitemObj.product_code == o.item_code
          o.qty = saleitemObj.qty * -1
          o.price = saleitemObj.price * -1
          o.save
        end
      end
    end
    # FOr Sale Audit 
      action_by = current_user.name
      if access_code != "null" && current_user.role == "cashier"
        action_by = Employee.find_by_emp_id(access_code).name
      end
    remark = "Void Sale Item ID #{saleitemObj.sale_item_id} | Receipt No #{saleObj.receipt_no} | Item Name ->#{saleitemObj.product_name}-Product Code ->#{saleitemObj.product_code}-Instance Code ->#{saleitemObj.item_instance_code}"
    sale_audit = SaleAudit.record_audit_for_edit(saleitemObj.sale_id,current_user.name, action_by,remark,"SALEITEMVOID" )

    saleObj.compute_by_sale_items(saleObj.sale_id, saleObj.sale_items, saleObj.total_discount,order_source)
    ProductCommission.create_product_commission(@newsaleitem, saleitemObj)
  end

  def item_foc
    saleitemId = params[:sale_item_id]
    remark     = params[:remark]
    access_code = params[:access_code]
    order_source = params[:type]
    saleitemObj = SaleItem.find(saleitemId)
    saleitemObj.status = 'foc'
    saleitemObj.remark = remark
    saleitemObj.save
    @newsaleitem = SaleItem.new
    @newsaleitem = saleitemObj.dup
    @newsaleitem.save
    @newsaleitem.qty = saleitemObj.qty * -1      
    @newsaleitem.unit_price = saleitemObj.unit_price * 1
    @newsaleitem.taxable_price = saleitemObj.taxable_price * -1
    @newsaleitem.price = saleitemObj.price * -1
    @newsaleitem.product_name = saleitemObj.product_name + ' (FOC)'
    @newsaleitem.remark = remark
    @newsaleitem.save

    # re-calc tax
    saleObj = Sale.find(saleitemObj.sale_id)

    order_id = SaleOrder.find_by_sale_id(saleitemObj.sale_id).order_id
    order = Order.find(order_id)

    action_by = current_user.name
      if access_code != "null" && current_user.role == "cashier"
        action_by = Employee.find_by_emp_id(access_code).name
      end
    remark = "FOC Sale Item ID #{saleitemObj.sale_item_id} | Receipt No #{saleObj.receipt_no} | Item Name ->#{saleitemObj.product_name}-Product Code ->#{saleitemObj.product_code}-Instance Code ->#{saleitemObj.item_instance_code}Receipt No #{saleObj.receipt_no}"
    sale_audit = SaleAudit.record_audit_for_edit(saleitemObj.sale_id,current_user.name, action_by,remark,"SALEITEMFOC" )

    saleObj.compute_by_sale_items(saleObj.sale_id, saleObj.sale_items, saleObj.total_discount,order_source)
    ProductCommission.create_product_commission(@newsaleitem, saleitemObj)
  end

  # def item_edit
  #   saleitemId = params[:sale_item_id]
  #   update_qty = params[:update_qty]
  #   update_price = params[:update_price]
  #   saleitemObj = SaleItem.find(saleitemId)
  #   saleitemObj.status = 'void'
  #   saleitemObj.save
  #   @newsaleitem = SaleItem.new
  #   @newsaleitem = saleitemObj.dup
  #   @newsaleitem.save
  #   @newsaleitem.qty = update_qty
  #   @newsaleitem.price = update_price
  #   @newsaleitem.unit_price = update_price
  #   @newsaleitem.taxable_price = update_price
  #   @newsaleitem.is_taxable = 0
  #   @newsaleitem.remark = 'edit'
  #   @newsaleitem.product_name = saleitemObj.product_name + " - updated"
  #   @newsaleitem.save
  # end

  def item_edit
    saleitemId = params[:sale_item_id]
    update_qty = params[:update_qty]
    update_price = params[:update_price]
    access_code = params[:access_code]
    order_source = params[:type]
    saleitemObj = SaleItem.find(saleitemId)
    original_qty = saleitemObj.qty
    original_price = saleitemObj.unit_price
    sale        = Sale.find(saleitemObj.sale_id)

    saleitemObj.qty = update_qty
    saleitemObj.price = update_qty.to_f * update_price.to_f
    saleitemObj.unit_price = update_price
    saleitemObj.taxable_price = update_qty.to_f * update_price.to_f

    action_by = current_user.name
      if access_code != "null" && current_user.role == "cashier"
        action_by = Employee.find_by_emp_id(access_code).name
      end
    remark = "Update From Qty=>#{original_qty} Price=>[#{original_price}] to qty=>#{update_qty} Price =>[#{saleitemObj.price}]| Receipt No #{sale.receipt_no} | Item Name ->#{saleitemObj.product_name}-Product Code ->#{saleitemObj.product_code}-Instance Code ->#{saleitemObj.item_instance_code}"
    sale_audit = SaleAudit.record_audit_for_edit(saleitemObj.sale_id,current_user.name, action_by,remark,"SALEITEMEDIT" )
    # saleitemObj.remark        = 'edit'

    unless saleitemObj.product_name.include? 'updated'
      saleitemObj.product_name = saleitemObj.product_name + ' (UPDATED)'
    end
    
    saleitemObj.save

    # re-calc tax
    saleObj = Sale.find(saleitemObj.sale_id)

    order_id = SaleOrder.find_by_sale_id(saleitemObj.sale_id).order_id
    order = Order.find(order_id)

    order.order_items.each do |o|
      if saleitemObj.product_code == o.item_code
        o.qty = update_qty
        o.price = update_price
        o.save
      end
    end

    saleObj.compute_by_sale_items(saleObj.sale_id, saleObj.sale_items, saleObj.total_discount,order_source)

    ProductCommission.edit_product_commission(saleitemObj)
  end

  # make cancel void item
  def item_void_cancel
    saleitemId = params[:sale_item_id]
    access_code = params[:access_code]
    order_source = params[:type]
    saleitemObj = SaleItem.find(saleitemId)
    both = SaleItem.where('product_code=?', saleitemObj.product_code)
    both.each do |item|
      if item.qty.to_i > 0
        item.status = nil
        item.save
      end
    end
    saleitemObj.destroy

    # re-calc tax
    saleObj = Sale.find(saleitemObj.sale_id)

    order_id = SaleOrder.find_by_sale_id(saleitemObj.sale_id).order_id
    order = Order.find(order_id)

    action_by = current_user.name
      if access_code != "null" && current_user.role == "cashier"
        action_by = Employee.find_by_emp_id(access_code).name
      end
    remark = "Cancle Void Sale Item ID #{saleitemObj.sale_item_id} | Item Name ->#{saleitemObj.product_name}-Product Code ->#{saleitemObj.product_code}-Instance Code ->#{saleitemObj.item_instance_code}|Receipt No #{saleObj.receipt_no}"
    sale_audit = SaleAudit.record_audit_for_edit(saleitemObj.sale_id,current_user.name, action_by,remark,"ITEMCANCELVOID" )

    saleObj.compute_by_sale_items(saleObj.sale_id, saleObj.sale_items, saleObj.total_discount, order_source)
    ProductCommission.remove_product_commission(saleitemObj)
  end

  # remove all void items
  def cancel_all_void
    sale_id = params[:sale_id]
    order_source = params[:type]
    saleObj = Sale.find(sale_id)
    saleObj.sale_items.each do |item|
      if item.qty.to_i < 0
        item.destroy
      else
        item.status = nil
      end
      item.save
      ProductCommission.remove_product_commission(item)
    end

    # re-calc tax
    saleObj.compute_by_sale_items(saleObj.sale_id, saleObj.sale_items, saleObj.total_discount,order_source)
  end

  def apply_void
    sale_id = params[:sale_id]
    order_source = params[:type]
    saleObj = Sale.find(sale_id)
    saleObj.compute_without_void(order_source)
    saleObj.sale_items.each do |item|
      ProductCommission.remove_product_commission(item)
    end
  end
  
end
