Class: Piggybak::LineItem
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Piggybak::LineItem
- Defined in:
- app/models/piggybak/line_item.rb
Class Method Summary collapse
Instance Method Summary collapse
- #admin_label ⇒ Object
- #decrease_inventory ⇒ Object
-
#destroy_associated_item ⇒ Object
Dependent destroy is not working as expected, so this is in place.
- #increase_inventory ⇒ Object
-
#initialize_line_item ⇒ Object
TODO: Possibly replace all initializers below with database defaults.
- #postprocess_payment ⇒ Object
- #preprocess ⇒ Object
- #preprocess_payment ⇒ Object
- #preprocess_sellable ⇒ Object
- #preprocess_shipment ⇒ Object
- #sellable_id_enum ⇒ Object
- #update_inventory ⇒ Object
Class Method Details
.line_item_type_select ⇒ Object
117 118 119 |
# File 'app/models/piggybak/line_item.rb', line 117 def self.line_item_type_select Piggybak.config.line_item_types.select { |k, v| v[:visible] }.collect { |k, v| [k.to_s.humanize.titleize, k] } end |
.sorted_line_item_types ⇒ Object
152 153 154 |
# File 'app/models/piggybak/line_item.rb', line 152 def self.sorted_line_item_types Piggybak::Config.line_item_types.sort { |a, b| (a[1][:sort] || 100) <=> (b[1][:sort] || 100) }.collect { |a| a[0] } end |
Instance Method Details
#admin_label ⇒ Object
125 126 127 128 129 130 131 |
# File 'app/models/piggybak/line_item.rb', line 125 def admin_label if self.line_item_type == 'sellable' "#{self.quantity} x #{self.description} ($#{sprintf("%.2f", self.unit_price)}): $#{sprintf("%.2f", self.price)}".gsub('"', '"') else "#{self.description}: $#{sprintf("%.2f", self.price)}".gsub('"', '"') end end |
#decrease_inventory ⇒ Object
133 134 135 |
# File 'app/models/piggybak/line_item.rb', line 133 def decrease_inventory self.sellable.update_inventory(-1 * self.quantity) end |
#destroy_associated_item ⇒ Object
Dependent destroy is not working as expected, so this is in place
107 108 109 110 111 112 113 114 115 |
# File 'app/models/piggybak/line_item.rb', line 107 def destroy_associated_item line_item_type_sym = self.line_item_type.to_sym if Piggybak.config.line_item_types[line_item_type_sym].has_key?(:nested_attrs) if Piggybak.config.line_item_types[line_item_type_sym][:nested_attrs] b = self.send("#{line_item_type_sym}") b.destroy if b.present? end end end |
#increase_inventory ⇒ Object
137 138 139 |
# File 'app/models/piggybak/line_item.rb', line 137 def increase_inventory self.sellable.update_inventory(self.quantity) end |
#initialize_line_item ⇒ Object
TODO: Possibly replace all initializers below with database defaults
23 24 25 26 27 28 29 |
# File 'app/models/piggybak/line_item.rb', line 23 def initialize_line_item self.quantity ||= 1 self.price ||= 0 # TODO: Fix this, should default in database self.created_at ||= Time.now end |
#postprocess_payment ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 |
# File 'app/models/piggybak/line_item.rb', line 94 def postprocess_payment return true if !self.new_record? if self.payment.process(self.order) self.price = -1*self.order.total_due self.order.total_due = 0 return true else return false end end |
#preprocess ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/piggybak/line_item.rb', line 31 def preprocess # TODO: Investigate if this is unnecessary if you use reject_if on accepts_nested_attributes_for Piggybak.config.line_item_types.each do |k, v| if v.has_key?(:nested_attrs) && k != self.line_item_type.to_sym self.send("#{k}=", nil) end end method = "preprocess_#{self.line_item_type}" self.send(method) if self.respond_to?(method) end |
#preprocess_payment ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'app/models/piggybak/line_item.rb', line 85 def preprocess_payment if self.new_record? self.build_payment if self.payment.nil? self.payment.payment_method_id ||= Piggybak::PaymentMethod.where(active: true).first.id self.description = "Payment" self.price = 0 end end |
#preprocess_sellable ⇒ 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 |
# File 'app/models/piggybak/line_item.rb', line 43 def preprocess_sellable if self.sellable_id.nil? self.errors.add(:sellable_id, "Sellable can't be blank") return end sellable = Piggybak::Sellable.where(id: self.sellable_id).first return if sellable.nil? # Inventory check quantity_change = 0 if self.new_record? quantity_change = self.quantity.to_i elsif self.changes.keys.include?("quantity") && self.quantity > self.quantity_was quantity_change = self.quantity - self.quantity_was end if sellable.quantity < quantity_change && !sellable.unlimited_inventory self.errors.add(:sellable_id, "Insufficient inventory by #{quantity_change - sellable.quantity} unit(s).") return end self.description = sellable.description self.unit_price = sellable.price self.price = self.unit_price*self.quantity.to_i end |
#preprocess_shipment ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/models/piggybak/line_item.rb', line 70 def preprocess_shipment if !self._destroy if (self.new_record? || self.shipment.status != 'shipped') && self.shipment && self.shipment.shipping_method calculator = self.shipment.shipping_method.klass.constantize self.price = calculator.rate(self.shipment.shipping_method, self.order) self.price = ((self.price*100).to_i).to_f/100 self.description = self.shipment.shipping_method.description end if self.shipment.nil? || self.shipment.shipping_method.nil? self.price = 0.00 self.description = "Shipping" end end end |
#sellable_id_enum ⇒ Object
121 122 123 |
# File 'app/models/piggybak/line_item.rb', line 121 def sellable_id_enum ::Piggybak::Sellable.all.collect { |s| ["#{s.description}: $#{s.price}", s.id ] } end |
#update_inventory ⇒ Object
141 142 143 144 145 146 147 148 149 150 |
# File 'app/models/piggybak/line_item.rb', line 141 def update_inventory if self.sellable_id != self.sellable_id_was old_sellable = Sellable.where(id: self.sellable_id_was).first old_sellable.update_inventory(self.quantity_was) self.sellable.update_inventory(-1*self.quantity) else quantity_diff = self.quantity_was - self.quantity self.sellable.update_inventory(quantity_diff) end end |