Class: Comee::Core::Invoice

Inherits:
ApplicationRecord show all
Defined in:
app/models/comee/core/invoice.rb

Constant Summary collapse

ADVANCE =
"Advance".freeze
DELIVERY =
"Delivery".freeze
DIRECT =
"Direct".freeze
INVOICE_TYPES =
[ADVANCE, DELIVERY, DIRECT].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



111
112
113
# File 'app/models/comee/core/invoice.rb', line 111

def self.ransackable_associations(_auth_object = nil)
  ["sales_order"]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/comee/core/invoice.rb', line 95

def self.ransackable_attributes(_auth_object = nil)
  %w[
    id
    sales_order_id
    invoice_no
    invoice_type
    date_issued
    ship_name
    delivery_date
    voyage_no
    status
    payment_status
    created_at
  ]
end

Instance Method Details

#advance?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/comee/core/invoice.rb', line 54

def advance?
  invoice_type == ADVANCE
end

#calculate_amount_creditedObject



89
90
91
92
93
# File 'app/models/comee/core/invoice.rb', line 89

def calculate_amount_credited
  credited = CreditNote.where(invoice_id: id, status: CreditNote.statuses[:approved])
  total = credited.sum(:total_price) + credited.sum(:vat)
  total.round(2)
end

#calculate_amount_paidObject



84
85
86
87
# File 'app/models/comee/core/invoice.rb', line 84

def calculate_amount_paid
  payments = Payment.where(invoice_id: id)
  payments.sum(:amount)
end

#calculate_due_dateObject



72
73
74
# File 'app/models/comee/core/invoice.rb', line 72

def calculate_due_date
  self.due_date = date_issued + 30.days
end

#calculate_totalObject



76
77
78
79
80
81
82
# File 'app/models/comee/core/invoice.rb', line 76

def calculate_total
  items = InvoiceItem.where(invoice_id: id)
  additionals = AdditionalItem.where(invoice_id: id)
  total = (items.sum(:total_price) + additionals.sum(:total_price))
  total *= (pct_advanced / 100) if advance?
  total.round(2)
end

#calculate_total_priceObject



62
63
64
# File 'app/models/comee/core/invoice.rb', line 62

def calculate_total_price
  self.total_price = calculate_total
end

#calculate_vatObject



66
67
68
69
70
# File 'app/models/comee/core/invoice.rb', line 66

def calculate_vat
  return unless sales_order.client.tax_code == "Inland"

  self.vat = (calculate_total * 0.19).round(2)
end

#delivery?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/comee/core/invoice.rb', line 50

def delivery?
  invoice_type == DELIVERY
end

#direct?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/comee/core/invoice.rb', line 58

def direct?
  invoice_type == DIRECT
end

#generate_invoice_noObject



115
116
117
118
# File 'app/models/comee/core/invoice.rb', line 115

def generate_invoice_no
  year = Date.current.year.to_s
  self.invoice_no = Util.generate_number("Invoice", "invoice_no", year, "-", 300000)
end