class SalePayment < ApplicationRecord
  self.primary_key = "sale_payment_id"

  #primary key - need to be unique generated for multiple shops
  before_create :generate_custom_id

  belongs_to :sale

  attr_accessor :received_amount, :card_payment_reference, :voucher_no, :giftcard_no, :customer_id, :external_payment_status

  def process_payment(invoice, action_by, cash_amount, payment_method)
    self.sale = invoice
    self.received_amount = cash_amount
    amount_due = invoice.grand_total

    #get all payment for this invoices
    invoice.sale_payments.each do |payment|
      if (payment.payment_status == "paid" )
        amount_due = amount_due - payment.payment_amount
      end
    end

    if (amount_due > 0)
      payment_status = false
      #route to payment type
      case payment_method
        when "cash"
          payment_status = cash_payment
        when "creditnote"
          if !self.sale.customer_id.nil?
            payment_status = creditnote_payment(self.customer_id)
          end
        when "visa"
           payment_status = external_terminal_card_payment(:visa)
        when "master"
           payment_status = external_terminal_card_payment(:master)
        when "jcb"
           payment_status = external_terminal_card_payment(:jcb)
        when "mpu"
           payment_status = external_terminal_card_payment(:mpu)
        when "unionpay"
           payment_status = external_terminal_card_payment(:unionpay)
        when "vochure"
           payment_status = vochure_payment
        when "giftcard"
           payment_status = giftcard_payment
        when "paypar"
          payment_status = paypar_payment
      else
         puts "it was something else"
      end

      #record an payment in sale-audit
      remark = "Payment #{payment_method}- for Invoice #{invoice.receipt_no} Due [#{amount_due}]| pay amount -> #{cash_amount} | Payment Status ->#{payment_status}"
      sale_audit = SaleAudit.record_payment(invoice.id, remark, action_by)

      # update complete order items in oqs
      SaleOrder.where("sale_id = '#{ invoice.sale_id }'").find_each do |sodr|
        AssignedOrderItem.where("order_id = '#{ sodr.order_id }'").find_each do |aoi|
          aoi.delivery_status = 1
          aoi.save
        end
      end

      return true, self.save
    else
      #record an payment in sale-audit
      remark = "No outstanding Amount - Grand Total [#{invoice.grand_total}] | Due [#{amount_due}] | Paid [#{invoice.amount_received}]"
      sale_audit = SaleAudit.record_payment(invoice.id, remark,action_by)

      return false, "No outstanding Amount"
    end

  end

  def self.get_paypar_account(url,token,membership_id,campaign_type_id,merchant_uid,auth_token)
    # Control for Paypar Cloud
    begin
      response = HTTParty.get(url,
              :body => { app_token: token,membership_id:membership_id,campaign_type_id:campaign_type_id,merchant_uid:merchant_uid,auth_token:auth_token}.to_json,
              :headers => {
                        'Content-Type' => 'application/json',
                        'Accept' => 'application/json'
              }, :timeout => 10
        )
    rescue Net::OpenTimeout
        response = { status: false }

    rescue OpenURI::HTTPError
      response = { status: false}

    rescue SocketError
      response = { status: false}
    end
    return response;
  end

  def self.redeem(paypar_url,token,membership_id,received_amount,sale_id)
      membership_actions_data = MembershipAction.find_by_membership_type("redeem");

      if !membership_actions_data.nil?

        url = paypar_url.to_s + membership_actions_data.gateway_url.to_s
        merchant_uid = membership_actions_data.merchant_account_id
        auth_token = membership_actions_data.auth_token
        campaign_type_id = membership_actions_data.additional_parameter["campaign_type_id"]
        sale_data = Sale.find_by_sale_id(sale_id)

        if sale_data
          others = 0
          sale_data.sale_payments.each do |sale_payment|
              others = others + sale_payment.payment_amount
          end
          redeem_prices = sale_data.grand_total - others
          # Control for Paypar Cloud
          begin
            response = HTTParty.post(url,
                  :body => { generic_customer_id:membership_id,total_amount: redeem_prices,total_sale_transaction_amount: sale_data.grand_total,redeem_amount:received_amount,receipt_no:sale_data.receipt_no,campaign_type_id:campaign_type_id,account_no:"",merchant_uid:merchant_uid,auth_token:auth_token}.to_json,
                  :headers => {
                            'Content-Type' => 'application/json',
                            'Accept' => 'application/json'
                  },
                  :timeout => 10
            )
          rescue Net::OpenTimeout
            response = false

          rescue OpenURI::HTTPError
            response = { status: false}

          rescue SocketError
            response = { status: false}
          end
        else
            response = false;
        end

      else
        response =false;
      end
      return response;

  end

  private
  def cash_payment
    payment_status = false
    self.payment_method = "cash"
    self.payment_amount = self.received_amount
    self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
    self.payment_status = "paid"
    payment_method = self.save!
    sale_update_payment_status(self.received_amount)
    return payment_status
  end

  def creditnote_payment(customer_id)
    payment_status = false

    self.payment_method = "creditnote"
    self.payment_amount = self.received_amount
    self.customer_id  = self.customer_id
    self.outstanding_amount = 0 - self.received_amount.to_f
    self.payment_status = "outstanding"
    payment_method = self.save!

    sale_update_payment_status(self.received_amount)
    return payment_status
  end

  def external_terminal_card_payment(method)
    payment_status = false

    self.payment_method = method
    self.payment_amount = self.received_amount
    self.payment_reference  = self.card_payment_reference
    self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
    self.payment_status = "paid"
    payment_method = self.save!

    sale_update_payment_status(self.received_amount)
    return payment_status
  end

  def voucher_payment
    payment_status = false

    #Next time - validate if the vochure number is valid - within
    self.payment_method = "voucher"
    self.payment_amount = self.received_amount
    self.payment_reference  = self.voucher_no
    self.outstanding_amount = self.sale.grand_total- self.received_amount
    self.payment_status = "paid"
    payment_method = self.save!
    sale_update_payment_status(self.received_amount)

    return payment_status

  end

  def giftcard_payment
    payment_status = false

    #Next time - validate if the vochure number is valid - within
    self.payment_method = "giftcard"
    self.payment_amount = self.received_amount
    self.payment_reference  = self.giftcard_no
    self.outstanding_amount = self.sale.grand_total- self.received_amount
    self.payment_status = "paid"
    payment_method = self.save!
    sale_update_payment_status(self.received_amount)

    return payment_status

  end

  def paypar_payment
    payment_status = false

    #Next time - validate if the vochure number is valid - within
    customer_data = Customer.find_by_customer_id(self.sale.customer_id)
    membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
    membership_data = SalePayment.redeem(membership_setting.gateway_url,membership_setting.auth_token,customer_data.membership_id,self.received_amount,self.sale.sale_id)

    if membership_data["status"]==true
      self.payment_method = "paypar"
      self.payment_amount = self.received_amount
      self.payment_reference  = self.voucher_no
      self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
      self.payment_status = "pending"
      payment_method = self.save!
      SalePayment.where(:sale_payment_id => self.sale_payment_id).update_all(:payment_status => 'paid')
      sale_update_payment_status(self.received_amount.to_f)

    else
      sale_update_payment_status(0)
    end
    return payment_status

  end

  def sale_update_payment_status(paid_amount)
    #update amount_outstanding
    self.sale.amount_received =   self.sale.amount_received.to_f + paid_amount.to_f
    self.sale.save!
    self.sale.amount_changed = self.sale.amount_received.to_f - self.sale.grand_total.to_f
    all_received_amount = 0.0
    sObj = Sale.find(self.sale_id)
    is_credit = 0
    sObj.sale_payments.each do |spay|
        all_received_amount += spay.payment_amount.to_f
        if spay.payment_method == "creditnote"
          is_credit = 1
        end
    end
    if (self.sale.grand_total <= all_received_amount)
      if is_credit == 0
        self.sale.payment_status = "paid"
      else
       self.sale.payment_status = "outstanding"
      end
      self.sale.sale_status = "completed"
      self.sale.save!
      table_update_status(sObj)
      rebat(sObj)
      if paid_amount != "0.0"
        update_shift
      end
    end

  end

  def update_shift
    shift = ShiftSale.current_open_shift(self.sale.cashier_id)
    if !shift.nil?
      shift.update(self.sale)
      self.sale.shift_sale_id = shift.id
      self.sale.save
    end
  end

  def table_update_status(sale_obj)
    status = true
    booking = Booking.find_by_sale_id(sale_obj.id)
    if booking
      table = DiningFacility.find(booking.dining_facility_id)
      bookings = table.bookings
      bookings.each do |tablebooking|
        if tablebooking.booking_status != 'moved'
         if tablebooking.sale.sale_status != 'completed' && tablebooking.sale.sale_status != 'void'
            status = false
         end
       end
      end
      if status
         table.status = "available"
         table.save
      end
    end
  end

  def rebat(sObj)
    rebate_prices = SaleItem.calculate_rebate_by_account(sObj.sale_items)

    generic_customer_id = sObj.customer.membership_id
    if generic_customer_id != nil || generic_customer_id != "" || generic_customer_id != 0
      paypar = sObj.sale_payments
      payparcost = 0
      paypar.each do |pp|
        if pp.payment_method == "paypar"
          payparcost = payparcost + pp.payment_amount
        end
      end
      # overall_dis = SaleItem.get_overall_discount(sObj.id)
      overall_dis = sObj.total_discount

   		total_amount = rebate_prices - payparcost - overall_dis
      if total_amount > 0
     		receipt_no = sObj.receipt_no
        membership      = MembershipSetting.find_by_membership_type("paypar_url")
        memberaction    = MembershipAction.find_by_membership_type("rebate")
        merchant_uid    = memberaction.merchant_account_id.to_s
        campaign_type_id = memberaction.additional_parameter["campaign_type_id"]
        auth_token       = memberaction.auth_token.to_s
        url             = membership.gateway_url.to_s + memberaction.gateway_url.to_s

        # Control for Paypar Cloud
        begin
          response        = HTTParty.post(url, :body => { generic_customer_id:generic_customer_id ,total_sale_transaction_amount: sObj.grand_total,merchant_uid:merchant_uid,total_amount: total_amount,campaign_type_id: campaign_type_id,
                                                        receipt_no: receipt_no,auth_token:auth_token}.to_json,
                                             :headers => {
                                                      'Content-Type' => 'application/json',
                                                      'Accept'       => 'application/json'
                                              }, :timeout => 10)
        rescue Net::OpenTimeout
          response = { status: false }

        rescue OpenURI::HTTPError
          response = { status: false}

        rescue SocketError
          response = { status: false}
        end
       return response
       # puts response.to_json
        end
      end
  end



  private
  def generate_custom_id
    self.sale_payment_id = SeedGenerator.generate_id(self.class.name, "SPI")
  end
end
