Class: Comee::Core::SalesOrder

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

Constant Summary collapse

OWN =
"Own".freeze
AGENT =
"Agent".freeze
SHIPPING_ARRANGEMENTS =
[OWN, AGENT].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



76
77
78
# File 'app/models/comee/core/sales_order.rb', line 76

def self.ransackable_associations(_auth_object = nil)
  %w[client customer_order shipment_address]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/comee/core/sales_order.rb', line 62

def self.ransackable_attributes(_auth_object = nil)
  %w[
    id
    order_number
    status
    purchase_status
    consignee
    destination
    customer_order_id
    consolidator_date
    shipping_date
  ]
end

Instance Method Details

#calculate_totalObject



52
53
54
55
56
# File 'app/models/comee/core/sales_order.rb', line 52

def calculate_total
  order_items = SalesOrderItem.where(sales_order_id: id)
  services = AdditionalService.where(sales_order_id: id)
  (order_items.sum(&:total_price) + services.sum(&:total_price)).round(2)
end

#calculate_total_priceObject



42
43
44
# File 'app/models/comee/core/sales_order.rb', line 42

def calculate_total_price
  self.total_price = calculate_total
end

#calculate_vatObject



46
47
48
49
50
# File 'app/models/comee/core/sales_order.rb', line 46

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

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

#files_urlObject



80
81
82
# File 'app/models/comee/core/sales_order.rb', line 80

def files_url
  files.attached? ? files.map { |file| Rails.application.routes.url_helpers.rails_blob_url(file, only_path: false) } : []
end

#generate_order_numberObject



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

def generate_order_number
  self.order_number = Util.generate_number("SalesOrder", "order_number", "SO")
end

#set_parent_client_nameObject



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

def set_parent_client_name
  self.parent_client_name = client.parent&.name
end