Class: Comee::Core::SalesOrderItem

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ransackable_associations(_auth_object = nil) ⇒ Object



95
96
97
# File 'app/models/comee/core/sales_order_item.rb', line 95

def self.ransackable_associations(_auth_object = nil)
  %w[customer_order_item product sales_order purchase_order_item source unit]
end

.ransackable_attributes(_auth_object = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/comee/core/sales_order_item.rb', line 76

def self.ransackable_attributes(_auth_object = nil)
  %w[
    id
    customer_item_no
    customer_item_description
    customer_order_item_id
    delivery_date
    eb_number
    handover_date
    lead_time
    product_id
    sales_order_id
    purchase_order_item_id
    source_id
    unit_id
    processing_status
  ]
end

Instance Method Details

#calculate_handover_dateObject



29
30
31
32
33
# File 'app/models/comee/core/sales_order_item.rb', line 29

def calculate_handover_date
  return if handover_date

  self.handover_date = sales_order.handover_date
end

#calculate_price_changeObject



45
46
47
48
49
50
51
52
53
# File 'app/models/comee/core/sales_order_item.rb', line 45

def calculate_price_change
  if customer_order_item.unit_id == unit_id
    self.price_diff = price - customer_order_item.price
  else
    service = CustomerOrderService.new
    co_price = service.convert_price(customer_order_item.price, customer_order_item.unit_id, unit_id)
    self.price_diff = price - co_price
  end
end

#calculate_quantity_changeObject



55
56
57
58
59
60
61
62
63
# File 'app/models/comee/core/sales_order_item.rb', line 55

def calculate_quantity_change
  if customer_order_item.unit_id == unit_id
    self.quantity_diff = quantity - customer_order_item.quantity
  else
    service = CustomerOrderService.new
    co_quantity = service.convert_quantity(customer_order_item.quantity, customer_order_item.unit_id, unit_id)
    self.quantity_diff = quantity - co_quantity
  end
end

#calculate_total_priceObject



35
36
37
# File 'app/models/comee/core/sales_order_item.rb', line 35

def calculate_total_price
  self.total_price = (price * quantity).round(2)
end

#quantity_remainingObject



65
66
67
# File 'app/models/comee/core/sales_order_item.rb', line 65

def quantity_remaining
  quantity - (quantity_delivered + quantity_canceled)
end

#update_sales_orderObject



39
40
41
42
43
# File 'app/models/comee/core/sales_order_item.rb', line 39

def update_sales_order
  sales_order.calculate_total_price
  sales_order.calculate_vat
  sales_order.save!
end

#update_sales_order_purchase_statusObject



69
70
71
72
73
74
# File 'app/models/comee/core/sales_order_item.rb', line 69

def update_sales_order_purchase_status
  return unless sales_order.sales_order_items.all.all?(&:fully_processed?)

  sales_order.purchase_status = SalesOrder.purchase_statuses[:processed]
  sales_order.save!
end