Class: Dscf::Marketplace::Order
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Dscf::Marketplace::Order
- Defined in:
- app/models/dscf/marketplace/order.rb
Class Method Summary collapse
- .create_from_listing(listing, user, quantity, dropoff_address = nil, payment_method = nil) ⇒ Object
- .create_from_quotation(quotation, dropoff_address = nil, payment_method = nil) ⇒ Object
- .ransackable_associations(_auth_object = nil) ⇒ Object
-
.ransackable_attributes(_auth_object = nil) ⇒ Object
Ransack configuration for secure filtering.
Instance Method Summary collapse
- #calculate_total_amount ⇒ Object
- #can_be_completed? ⇒ Boolean
- #confirm! ⇒ Object
- #delivery? ⇒ Boolean
- #requires_delivery_order? ⇒ Boolean
-
#self_pickup? ⇒ Boolean
Fulfillment type methods.
- #supplier ⇒ Object
- #total_amount ⇒ Object
Class Method Details
.create_from_listing(listing, user, quantity, dropoff_address = nil, payment_method = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'app/models/dscf/marketplace/order.rb', line 71 def self.create_from_listing(listing, user, quantity, dropoff_address = nil, payment_method = nil) return nil unless listing.status == "active" && quantity <= listing.quantity attributes = { order_type: :direct_listing, status: :pending, fulfillment_type: dropoff_address.present? ? :delivery : :self_pickup, listing: listing, user: user, # Keep for backward compatibility ordered_by: user, ordered_to: listing.business, dropoff_address: dropoff_address, total_amount: listing.price * quantity } attributes[:payment_method] = payment_method if payment_method.present? order = create!(attributes) order.order_items.create!( listing: listing, product: listing.supplier_product.product, unit: listing.supplier_product.product.unit, quantity: quantity, unit_price: listing.price, status: :pending ) order end |
.create_from_quotation(quotation, dropoff_address = nil, payment_method = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/dscf/marketplace/order.rb', line 39 def self.create_from_quotation(quotation, dropoff_address = nil, payment_method = nil) return nil unless quotation.accepted? attributes = { order_type: :rfq_based, status: :processing, fulfillment_type: dropoff_address.present? ? :delivery : :self_pickup, quotation: quotation, user: quotation.request_for_quotation.user, # Keep for backward compatibility ordered_by: quotation.request_for_quotation.user, ordered_to: quotation.business, dropoff_address: dropoff_address, total_amount: quotation.total_price } attributes[:payment_method] = payment_method if payment_method.present? order = create!(attributes) quotation.quotation_items.each do |item| order.order_items.create!( quotation_item: item, product: item.product, unit: item.unit, quantity: item.quantity, unit_price: item.unit_price, status: :pending ) end order end |
.ransackable_associations(_auth_object = nil) ⇒ Object
35 36 37 |
# File 'app/models/dscf/marketplace/order.rb', line 35 def self.ransackable_associations(_auth_object = nil) %w[quotation listing user ordered_by ordered_to delivery_order dropoff_address order_items] end |
.ransackable_attributes(_auth_object = nil) ⇒ Object
Ransack configuration for secure filtering
31 32 33 |
# File 'app/models/dscf/marketplace/order.rb', line 31 def self.ransackable_attributes(_auth_object = nil) %w[id quotation_id listing_id user_id ordered_by_id ordered_to_id delivery_order_id dropoff_address_id order_type status fulfillment_type payment_method total_amount created_at updated_at] end |
Instance Method Details
#calculate_total_amount ⇒ Object
151 152 153 |
# File 'app/models/dscf/marketplace/order.rb', line 151 def calculate_total_amount self.total_amount = order_items.sum { |item| item.quantity * item.unit_price } end |
#can_be_completed? ⇒ Boolean
132 133 134 135 136 137 138 139 140 |
# File 'app/models/dscf/marketplace/order.rb', line 132 def can_be_completed? if self_pickup? # Self-pickup orders can be completed immediately after confirmation confirmed? || processing? else # Delivery orders require delivery_order association delivery_order.present? && delivery_order.delivered? end end |
#confirm! ⇒ Object
142 143 144 145 146 147 148 149 |
# File 'app/models/dscf/marketplace/order.rb', line 142 def confirm! return false unless pending? update!(status: :confirmed) # Update order items without reloading to avoid association issues order_items.update_all(status: OrderItem.statuses[:confirmed]) true end |
#delivery? ⇒ Boolean
124 125 126 |
# File 'app/models/dscf/marketplace/order.rb', line 124 def delivery? fulfillment_type == "delivery" end |
#requires_delivery_order? ⇒ Boolean
128 129 130 |
# File 'app/models/dscf/marketplace/order.rb', line 128 def requires_delivery_order? delivery? && !completed? end |
#self_pickup? ⇒ Boolean
Fulfillment type methods
120 121 122 |
# File 'app/models/dscf/marketplace/order.rb', line 120 def self_pickup? fulfillment_type == "self_pickup" end |
#supplier ⇒ Object
111 112 113 114 115 116 117 |
# File 'app/models/dscf/marketplace/order.rb', line 111 def supplier if rfq_based? quotation&.business elsif direct_listing? listing&.business end end |
#total_amount ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'app/models/dscf/marketplace/order.rb', line 101 def total_amount # Return stored value if it exists and is greater than 0, otherwise calculate stored_value = super calculated_value = order_items.sum { |item| item.quantity * item.unit_price } # Return stored value if it's set and valid, otherwise return calculated value return stored_value if stored_value.present? && stored_value > 0 calculated_value end |