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
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 100 101 102 103 |
# File 'app/models/dscf/marketplace/order.rb', line 75 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
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 70 71 72 73 |
# File 'app/models/dscf/marketplace/order.rb', line 43 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
39 40 41 |
# File 'app/models/dscf/marketplace/order.rb', line 39 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
35 36 37 |
# File 'app/models/dscf/marketplace/order.rb', line 35 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 received_bank_name transaction_reference total_amount created_at updated_at] end |
Instance Method Details
#calculate_total_amount ⇒ Object
155 156 157 |
# File 'app/models/dscf/marketplace/order.rb', line 155 def calculate_total_amount self.total_amount = order_items.sum { |item| item.quantity * item.unit_price } end |
#can_be_completed? ⇒ Boolean
136 137 138 139 140 141 142 143 144 |
# File 'app/models/dscf/marketplace/order.rb', line 136 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
146 147 148 149 150 151 152 153 |
# File 'app/models/dscf/marketplace/order.rb', line 146 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
128 129 130 |
# File 'app/models/dscf/marketplace/order.rb', line 128 def delivery? fulfillment_type == "delivery" end |
#requires_delivery_order? ⇒ Boolean
132 133 134 |
# File 'app/models/dscf/marketplace/order.rb', line 132 def requires_delivery_order? delivery? && !completed? end |
#self_pickup? ⇒ Boolean
Fulfillment type methods
124 125 126 |
# File 'app/models/dscf/marketplace/order.rb', line 124 def self_pickup? fulfillment_type == "self_pickup" end |
#supplier ⇒ Object
115 116 117 118 119 120 121 |
# File 'app/models/dscf/marketplace/order.rb', line 115 def supplier if rfq_based? quotation&.business elsif direct_listing? listing&.business end end |
#total_amount ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'app/models/dscf/marketplace/order.rb', line 105 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 |