class Foodcourt::SaleEditController < BaseFoodcourtController
  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 = saleitemObj.dup
    @newsaleitem.parent_id = saleitemObj.sale_item_id
    @newsaleitem.qty = saleitemObj.qty * -1
    @newsaleitem.price = saleitemObj.price * -1
    @newsaleitem.is_taxable = saleitemObj.is_taxable
    @newsaleitem.taxable_price = saleitemObj.taxable_price * -1
    @newsaleitem.product_name = saleitemObj.product_name + ' (VOID)'
    @newsaleitem.remark = remark
    @newsaleitem.save

    if saleitemObj.discount_item
      saleitemObj.discount_item.destroy
    end

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

    # 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.total_discount, nil, order_source)
  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 = saleitemObj.dup
    @newsaleitem.parent_id = saleitemObj.sale_item_id
    @newsaleitem.qty = saleitemObj.qty * -1
    @newsaleitem.unit_price = saleitemObj.unit_price * 1
    @newsaleitem.is_taxable = saleitemObj.is_taxable
    @newsaleitem.taxable_price = saleitemObj.taxable_price * -1
    @newsaleitem.price = saleitemObj.price * -1
    @newsaleitem.product_name = saleitemObj.product_name + ' (FOC)'
    @newsaleitem.remark = remark
    @newsaleitem.save

    if saleitemObj.discount_item
      saleitemObj.discount_item.destroy
    end

    # 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)
    oqs_item = AssignedOrderItem.assigned_order_item_by_job(order_id)
    ActionCable.server.broadcast "order_queue_station_channel",order: oqs_item

    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.total_discount, nil, order_source)
  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'


    saleitemObj.save

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

    sale.compute_by_sale_items(saleObj.total_discount, nil, order_source)
  end

  # make cancel void item
  def item_void_cancel
    Sale.transaction do
      saleitemId = params[:sale_item_id]
      access_code = params[:access_code]
      order_source = params[:type]

      saleitemObj = SaleItem.find(saleitemId)

      saleObj = saleitemObj.sale

      if saleitemObj.status == 'void'
        saleitemObj.void_item.destroy
      else saleitemObj.status == 'foc'
        saleitemObj.foc_item.destroy
      end

      saleitemObj.update(status: nil, remark: nil)
      # re-calc tax

      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.total_discount, nil, order_source)
    end
  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.where(status: ['foc', 'void']).each do |item|
      if item.qty.to_i < 0
        item.destroy
      else
        item.status = nil
      end
      item.save
    end
    saleObj.sale_items.reset
    # re-calc tax
    saleObj.compute_by_sale_items(saleObj.total_discount,nil, order_source)
  end

  def apply_void
    sale_id = params[:sale_id]
    order_source = params[:type]
    saleObj = Sale.find(sale_id)
    saleObj.compute_by_sale_items(saleObj.total_discount, nil, order_source)
  end

end
