class Api::DiscountsController < Api::ApiController

  def create
    sale_id = params[:sale_id]
    sale_item_id = params[:sale_item_id]
    discount = params[:discount].to_d
    discount_type = params[:discount_type]

    @sale = Sale.find(sale_id)
    @booking = @sale.booking
    dining_facility = @booking.dining_facility
    action_by = current_login_employee.name

    bill_discount = @sale.total_discount

    if sale_item_id
      sale_item = SaleItem.find(sale_item_id)
      discount_item = sale_item.discount_item

      if discount > 0
        unless discount_item
          discount_item = sale_item.build_discount_item
        end

        discount_item.discount = discount
        if discount_type == 'nett'
          discount_item.discount_type = 'nett'
          discount_item.product_name = "#{sale_item.product_name} - discount"
          unit_price = discount
        elsif discount_type == 'percentage'
          discount_item.discount_type = 'percentage'
          discount_item.product_name = "#{sale_item.product_name} - discount(#{discount}%)"
          unit_price = sale_item.unit_price * (discount / 100)
        end

        price = unit_price * sale_item.qty

        discount_item.menu_category_code = sale_item.menu_category_code
        discount_item.menu_category_name = sale_item.menu_category_name

        discount_item.sale_id = sale_id
        discount_item.product_code = sale_item != nil ? sale_item.product_code : sale_id
        discount_item.item_instance_code = sale_item.item_instance_code
        discount_item.product_alt_name = ""
        discount_item.status = "Discount"

        discount_item.qty = sale_item.qty
        discount_item.unit_price = unit_price
        discount_item.taxable_price = -price
        discount_item.is_taxable = sale_item.is_taxable
        discount_item.account_id = sale_item.account_id

        discount_item.price = -price
        discount_item.save

        remark = "Discount Item Name ->#{discount_item.product_name}-Product Code ->#{discount_item.product_code} | Price [#{discount_item.price}] | Receipt No #{@sale.receipt_no} "

        sale_audit = SaleAudit.record_audit_discount(discount_item.sale_id, @sale.cashier_name, action_by, remark, "ITEMDISCOUNT" )
      else
        discount_item.destroy

        if dining_facility.nil?
          remark = "Remove Item Discount Item Name ->#{discount_item.product_name}-Product Code ->#{discount_item.product_code} | Price [#{discount_item.price}] | Receipt No #{@sale.receipt_no} | Table- No Table "
        else
          remark = "Remove Item Discount Item Name ->#{discount_item.product_name}-Product Code ->#{discount_item.product_code} | Price [#{discount_item.price}] | Receipt No #{@sale.receipt_no} | Table- #{dining_facility.name} "
        end
        sale_audit = SaleAudit.record_audit_discount(@sale.sale_id, @sale.cashier_name, action_by, remark, "REMOVEITEMDISCOUNT" )
      end
    else
      if discount > 0
        if discount_type == 'nett'
          bill_discount = discount
        elsif discount_type == 'percentage'
          bill_discount = @sale.total_amount * (discount / 100)
        end

        if dining_facility.nil?
          remark = "Discount Overall Price [#{bill_discount}]| Receipt No #{@sale.receipt_no} | Table- no Table "
        else
          remark = "Discount Overall Price [#{bill_discount}]| Receipt No #{@sale.receipt_no} | Table- #{dining_facility.name} "
        end

        sale_audit = SaleAudit.record_audit_discount(@sale.sale_id, @sale.cashier_name, action_by, remark, "OVERALLDISCOUNT" )
      else
        if dining_facility.nil?
          remark = "Remove Discount Sale Id [#{@sale.sale_id}]| Receipt No #{@sale.receipt_no} | Table- No Table"
        else
          remark = "Remove Discount Sale Id [#{@sale.sale_id}]| Receipt No #{@sale.receipt_no} | Table- #{dining_facility.name} "
        end
        sale_audit = SaleAudit.record_audit_discount(@sale.sale_id, @sale.cashier_name, action_by, remark, "REMOVEALLDISCOUNT" )

        bill_discount = 0.0.to_d
      end
    end

    @sale.compute_by_sale_items(bill_discount, nil, order_source)
  end

  def destroy
    sale_id = params[:sale_id]
    sale_item_id = params[:sale_item_id]

    @sale = Sale.find(sale_id)
    @booking = @sale.booking
    dining_facility = @booking.dining_facility
    action_by = current_login_employee.name

    bill_discount = @sale.total_discount

    if sale_item_id
      sale_item = SaleItem.find(sale_item_id)
      discount_item = sale_item.discount_item
      discount_item.destroy

      if dining_facility.nil?
        remark = "Remove Item Discount Item Name ->#{discount_item.product_name}-Product Code ->#{discount_item.product_code} | Price [#{discount_item.price}] | Receipt No #{@sale.receipt_no} | Table- No Table "
      else
        remark = "Remove Item Discount Item Name ->#{discount_item.product_name}-Product Code ->#{discount_item.product_code} | Price [#{discount_item.price}] | Receipt No #{@sale.receipt_no} | Table- #{dining_facility.name} "
      end
      sale_audit = SaleAudit.record_audit_discount(@sale.sale_id, @sale.cashier_name, action_by, remark, "REMOVEITEMDISCOUNT" )
    else
      if dining_facility.nil?
        remark = "Remove Discount Sale Id [#{@sale.sale_id}]| Receipt No #{@sale.receipt_no} | Table- No Table"
      else
        remark = "Remove Discount Sale Id [#{@sale.sale_id}]| Receipt No #{@sale.receipt_no} | Table- #{dining_facility.name} "
      end
      sale_audit = SaleAudit.record_audit_discount(@sale.sale_id, @sale.cashier_name, action_by, remark, "REMOVEALLDISCOUNT" )

      bill_discount = 0.0.to_d
    end
    @sale.compute_by_sale_items(bill_discount, nil, order_source)
  end

  private
  def order_source
    params[:order_source] || 'food_court'
  end
end
