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,remark=nil)
    self.sale = invoice
    self.received_amount = cash_amount
    self.payment_reference = remark
    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
      membership_data = nil
      #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 "alipay"
          payment_status = external_terminal_card_payment(:alipay)
        when "vochure"
           payment_status = vochure_payment
        when "giftcard"
           payment_status = giftcard_payment
        when "paypar"
          payment_status = paypar_payment
        when "foc"
          payment_status = foc_payment
        when "paymal"
          payment_status,membership_data = paymal_payment
        when "JunctionPay"
          payment_status = junction_pay_payment
        when "dinga"
          payment_status,membership_data = dinga_payment
        when "GiftVoucher"
          payment_status = giftvoucher_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,membership_data
    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; version=3'
              }, :timeout => 10
        )
    rescue Net::OpenTimeout
        response = { status: false }

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

    rescue SocketError
      response = { status: false}
    end
    Rails.logger.debug "Get Paypar Account "
    Rails.logger.debug response.to_json
    return response
  end

  def self.get_paypar_account_data(url,token,merchant_uid,auth_token,account_no,amount,receipt_no)
    # Control for Paypar Cloud
    begin
      response = HTTParty.get(url,
              :body => { merchant_uid:merchant_uid,auth_token:auth_token,receipt_no: receipt_no,
                account_no: account_no, amount: amount}.to_json,
              :headers => {
                        'Content-Type' => 'application/json',
                        'Accept' => 'application/json; version=3'
              }, :timeout => 10
        )
    rescue Net::OpenTimeout
        response = { status: false }

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

    rescue SocketError
      response = { status: false}
    end
    Rails.logger.debug "Get Paypar Account "
    Rails.logger.debug response.to_json
    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 => { membership_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; version=3'
                  },
                  :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

      Rails.logger.debug "Redeem response"
      Rails.logger.debug response.to_json

      return response;

  end

  def self.create_payment(paypar_url,token,membership_id,received_amount,sale_id)
      membership_actions_data = MembershipAction.find_by_membership_type("create_payment");
      sale_data = Sale.find_by_sale_id(sale_id)
      customer_data = Customer.find_by_customer_id(sale_data.customer_id)
      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
        

        if sale_data
          others = 0
          sale_data.sale_payments.each do |sale_payment|
              others = others + sale_payment.payment_amount
          end
          payment_prices = sale_data.grand_total - others

          if membership_id.to_i <= 0
          membership_id  = customer_data.membership_id
            params  = { membership_id:membership_id,
                            amount:received_amount,
                            receipt_no:sale_data.receipt_no,
                            merchant_uid:merchant_uid,
                            auth_token:auth_token}.to_json
          else
            params  = { account_no:membership_id,
                            amount:received_amount,
                            receipt_no:sale_data.receipt_no,
                            merchant_uid:merchant_uid,
                            auth_token:auth_token}.to_json
          end

          
          # Control for Paypar Cloud
          begin
            response = HTTParty.post(url,
                  :body => params,
                  :headers => {
                            'Content-Type' => 'application/json',
                            'Accept' => 'application/json; version=3'
                  },
                  :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

      Rails.logger.debug "Create Payment response"
      Rails.logger.debug response.to_json

      return response;

  end

    # Check for Card Payment
  def self.get_sale_payments_by_card(sale_payments)
    # Check for Card Payment
    sale_payments.each do |sp|
      if sp.payment_method == "jcb" || sp.payment_method == "mpu" || sp.payment_method == "visa" || sp.payment_method == "master" || sp.payment_method == "unionpay" || sp.payment_method == "alipay"
        return true;
      end
    end  
  end

  private
  def cash_payment
    status = false
    sale_payments_data = SalePayment.find_by_sale_id(self.sale_id)
    if sale_payments_data.nil?
      status = true
    end

    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,status)
    return payment_status
  end

  def foc_payment
    payment_status = false

    # add to sale item with foc
    sale_items = SaleItem.where("sale_id='#{ self.sale.sale_id }'")
    
    sale_items.each do|item|
      SaleItem.update_existing_item(item.qty, item, self.sale.sale_id, "foc", item.unit_price, item.price)
    end

    self.payment_method = "foc"
    self.payment_amount = self.received_amount
    # self.outstanding_amount = self.sale.grand_total.to_f - self.received_amount.to_f
    self.outstanding_amount = 0.00
    self.payment_status = "paid"
    payment_method = self.save!
    # sale_update_payment_status(self.received_amount)  
    sale_update_payment_status(0)  
    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)

     #record an payment in sale-audit
      remark = "#{membership_data} Redeem- for Customer #{self.sale.customer_id} Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
      sale_audit = SaleAudit.record_paymal(self.sale.sale_id, remark, 1)

    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 paymal_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)
    account_no = self.payment_reference
    # if account_no.to_i <= 0
    #   account_no  = customer_data.membership_id
    # end
    membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
    membership_data = SalePayment.create_payment(membership_setting.gateway_url,membership_setting.auth_token,account_no,self.received_amount,self.sale.sale_id)

     #record an payment in sale-audit
      remark = "#{membership_data} PayMal Payment- for Customer #{self.sale.customer_id} Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
      sale_audit = SaleAudit.record_paymal(self.sale.sale_id, remark, 1)

    if membership_data["status"]==true
      self.payment_method = "paymal"
      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 = "paid"
      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,membership_data
  end

  def junction_pay_payment
    payment_status = false

    #Next time - validate if the vochure number is valid - within
    self.payment_method = "JunctionPay"
    self.payment_amount = self.received_amount
    # self.payment_reference  = self.payment_reference
    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 dinga_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)

    account_no = self.payment_reference
    # if account_no == 0
    #   account_no  = customer_data.customer_id
    # end

    membership_setting = MembershipSetting.find_by_membership_type("paypar_url")
    membership_data = SalePayment.create_payment(membership_setting.gateway_url,membership_setting.auth_token,account_no,self.received_amount,self.sale.sale_id)

     #record an payment in sale-audit
      remark = "#{membership_data} Dinga Payment- for Customer #{self.sale.customer_id} Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
      sale_audit = SaleAudit.record_paymal(self.sale.sale_id, remark, 1)

    if membership_data["status"]==true
      self.payment_method = "dinga"
      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 = "paid"
      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,membership_data

  end

  def giftvoucher_payment
    payment_status = false
    self.payment_method = "giftvoucher"
    self.payment_amount = self.received_amount
    self.payment_reference  = self.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 sale_update_payment_status(paid_amount,check_foc = false)    
    #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
    is_foc    = 0
    method_status   = false
    sObj.sale_payments.each do |spay|
        all_received_amount += spay.payment_amount.to_f
        if spay.payment_method == "creditnote"
          is_credit = 1
        end
        if spay.payment_method == "foc"
          is_foc = 1
        end
        if spay.payment_method == "cash" || spay.payment_method == "foc" || spay.payment_method == "creditnote"
          method_status = true
        end
    end

    if (self.sale.grand_total <= all_received_amount) && method_status
      if is_credit == 0
        self.sale.payment_status = "paid"
      else
       self.sale.payment_status = "outstanding"
      end

      if is_foc == 0
        self.sale.payment_status = "paid"
      else
       self.sale.payment_status = "foc"
      end
     
      self.sale.sale_status = "completed"

      if MembershipSetting.find_by_rebate(1) && is_foc == 0 && is_credit == 0
        response = rebat(sObj)
        
         #record an payment in sale-audit
         remark = "#{response} Rebate- for Customer #{self.sale.customer_id} | Sale Id [#{self.sale.sale_id}]| pay amount -> #{self.received_amount} "
         sale_audit = SaleAudit.record_paymal(sObj.sale_id, remark, 1)

        if !response.nil?
          if response["status"] == true
          self.sale.rebate_status = 'true'
          end
          if response["status"] == false
            self.sale.rebate_status = 'false'
          end

          if response[:status] == false
            self.sale.rebate_status = 'false'
          end
          if response[:status] == "no_member"
            self.sale.rebate_status = nil
          end
        end
      end

      self.sale.save!
      
      if check_foc
        table_update_status(sObj)
        update_shift
      elsif paid_amount.to_f > 0 #|| paid_amount != "0.0"
        table_update_status(sObj)
        update_shift
      elsif method_status && paid_amount.to_f == 0 && is_credit == 0
        table_update_status(sObj)
        update_shift
      end
    end
  end

  # update for cashier shift
  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
    else
      shift = ShiftSale.current_shift
      shift.update(self.sale)
      self.sale.shift_sale_id = shift.id
      self.sale.save
    end
  end

  def table_update_status(sale_obj)
    status = true
    sale_count = 0
    booking = Booking.find_by_sale_id(sale_obj.id)
    if booking
      if booking.dining_facility_id.to_i > 0
        table = DiningFacility.find(booking.dining_facility_id)
        bookings = table.bookings
        bookings.each do |tablebooking|
          if tablebooking.booking_status != 'moved'
            if tablebooking.sale_id
              if tablebooking.sale.sale_status != 'completed' && tablebooking.sale.sale_status != 'void' && tablebooking.sale.sale_status != 'spoile' && tablebooking.sale.sale_status != 'waste' 
                status = false
                sale_count += 1
              else
                status = true
              end
            else
              status = false
              sale_count += 1
            end
          end
        end
        if status && sale_count == 0
           table.status = "available"
           table.save
        end

        # type  = 'payment'
        #Send to background job for processing
        # OrderBroadcastJob.perform_later(table,type)
        #if ENV["SERVER_MODE"] != 'cloud'
        # if ENV["SERVER_MODE"] == 'cloud'
        #    from = request.subdomain  + "." + request.domain
        # else 
        #   from = ""
        # end
        #   ActionCable.server.broadcast "order_channel",table: table,type:type,from:from
        #end
      end
    end
  end

  def rebat(sObj)
    rebate_prices,campaign_method = SaleItem.calculate_rebate_by_account(sObj.sale_items)
    generic_customer_id = sObj.customer.membership_id
    if generic_customer_id.present?

      paypar     = sObj.sale_payments
      payparcost = 0
      credit     = 0

      paypar.each do |pp|
        if pp.payment_method == "paypar"
          payparcost = payparcost + pp.payment_amount
        end
        if pp.payment_method == "creditnote"
          credit = 1
        end
      end
      # overall_dis = SaleItem.get_overall_discount(sObj.id)
      overall_dis   = sObj.total_discount
      
      if credit != 1
        membership        = MembershipSetting.find_by_membership_type("paypar_url")
        memberaction      = MembershipAction.find_by_membership_type("get_member_campaign")
        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.get(url, 
                                :body => { 
                                  member_group_id:sObj.customer.membership_type,
                                  merchant_uid:merchant_uid,
                                  campaign_type_id: campaign_type_id,
                                  auth_token:auth_token
                                }.to_json,
                                :headers => {
                                  'Content-Type' => 'application/json',
                                  'Accept'       => 'application/json; version=3'
                                }, :timeout => 10)
        rescue Net::OpenTimeout
           response = { "status": false , "message": " Connection timeout" }
        rescue OpenURI::HTTPError
           response = { "status": false, "message": "Can't connect server"}
          
        rescue SocketError
           response = { "status": false, "message": "Can't connect server"}
        end

        redeem_amount = payparcost + overall_dis

        total_percentage = 0

        type_arr = []
        Rails.logger.debug "Get Member Campaign"
        Rails.logger.debug response.to_json
        # Check for present response fields
        if response["membership_campaign_data"].present?
          response["membership_campaign_data"].each do |a|
            data = {:type => a["rules_type"], :percentage => a["change_unit"].to_i * a["base_unit"].to_i}
            total_percentage = total_percentage + a["change_unit"].to_i * a["base_unit"].to_i
          
            type_arr.push(data)
          end
        end

        rebate_arr =[]
        campaign_method.each do |a|
          data = {:type => a[:type], :amount => a[:amount]}
          type_arr.each do |si|
            if si[:type] == a[:type]
              if credit == 1
                 data[:amount] = 0
              else
                amount = (redeem_amount / total_percentage)*si[:percentage]
                actual = a[:amount] - amount
                data[:amount] = actual
              end
              
            end
          end
          rebate_arr.push(data)
        end
        Rails.logger.debug "Rebage Response"
          Rails.logger.debug rebate_arr.to_json
     		total_amount = rebate_prices - payparcost - overall_dis

        if credit == 1
          total_amount = 0
        end

        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,
                                    campaign_method: rebate_arr.to_json,
                                    auth_token:auth_token
                                  }.to_json,
                                  :headers => {
                                    'Content-Type' => 'application/json',
                                    'Accept'       => 'application/json; version=3'
                                  }, :timeout => 10)
          rescue Net::OpenTimeout
             response = { "status": false , "message": "Connect To" }
          rescue OpenURI::HTTPError
             response = { "status": false, "message": "Can't connect server"}
            
          rescue SocketError
             response = { "status": false, "message": "Can't connect server"}
          end
          Rails.logger.debug "Rebage Response"
          Rails.logger.debug response.to_json
          return response
        end
      end
    else
       response = { "status": "no_member", "message": "Not membership"}  
    end

  end

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