Class: OrderLineItem
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- OrderLineItem
- Defined in:
- app/models/order_line_item.rb
Overview
create_table :order_line_items do |t|
t.integer :order_txn_id
t.integer :order_line_item_type_id
t.integer :product_offer_id
t.string :product_offer_description
t.integer :product_instance_id,
t.string :product_instance_description
t.integer :product_type_id
t.string :product_type_description
t.decimal :sold_price, :precision => 8, :scale => 2
t.integer :sold_price_uom
t.integer :sold_amount
t.integer :sold_amount_uom
t.integer :quantity
t.integer :unit_of_measurement_id
t.decimal :unit_price, :precision => 8, :scale => 2
t.boolean :taxable
t.decimal :sales_tax, :precision => 8, :scale => 2
t.timestamps
end
add_index :order_line_items, :order_txn_id add_index :order_line_items, :order_line_item_type_id add_index :order_line_items, :product_instance_id add_index :order_line_items, :product_type_id add_index :order_line_items, :product_offer_id
Instance Method Summary collapse
-
#calculate_tax(ctx = {}) ⇒ Object
calculates tax and save to sales_tax.
- #clone ⇒ Object
-
#dba_organization ⇒ Object
helper method to get dba_organization related to this order_line_item.
- #destroy_order_line_item_relationships ⇒ Object
-
#line_item_record ⇒ Object
determine the record this OrderLineItem pertains to can be a ProductOffer, ProductInstance or ProductType.
-
#order_line_record ⇒ Object
Allow for polymorphic subtypes of this class.
- #taxed? ⇒ Boolean
-
#to_label ⇒ Object
Alias for to_s.
-
#to_s ⇒ Object
description of line_item_record.
-
#total_amount(currency = Currency.usd) ⇒ Object
get the total charges for a order_line_item.
Instance Method Details
#calculate_tax(ctx = {}) ⇒ Object
calculates tax and save to sales_tax
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/order_line_item.rb', line 111 def calculate_tax(ctx={}) taxation = ErpOrders::Taxation.new tax = 0 if self.taxed? tax = taxation.calculate_tax(self, ctx.merge({ amount: (self.sold_price * (self.quantity || 1)) })) end # only get charges that are USD currency charge_lines.joins(:money).joins(:charge_type) .where('money.currency_id' => Currency.usd) .where('charge_types.taxable' => true).readonly(false).each do |charge_line| tax += charge_line.calculate_tax(ctx) end self.sales_tax = tax self.save tax end |
#clone ⇒ Object
163 164 165 166 167 168 |
# File 'app/models/order_line_item.rb', line 163 def clone order_line_item_dup = dup order_line_item_dup.order_txn_id = nil order_line_item_dup end |
#dba_organization ⇒ Object
helper method to get dba_organization related to this order_line_item
57 58 59 |
# File 'app/models/order_line_item.rb', line 57 def dba_organization order_txn.find_party_by_role('dba_org') end |
#destroy_order_line_item_relationships ⇒ Object
61 62 63 |
# File 'app/models/order_line_item.rb', line 61 def destroy_order_line_item_relationships OrderLineItemRelationship.where("order_line_item_id_from = ? or order_line_item_id_to = ?", self.id, self.id).destroy_all end |
#line_item_record ⇒ Object
determine the record this OrderLineItem pertains to can be a ProductOffer, ProductInstance or ProductType
151 152 153 154 155 156 157 158 159 160 161 |
# File 'app/models/order_line_item.rb', line 151 def line_item_record if product_offer product_offer else if product_instance product_instance else product_type end end end |
#order_line_record ⇒ Object
Allow for polymorphic subtypes of this class
48 |
# File 'app/models/order_line_item.rb', line 48 belongs_to :order_line_record, :polymorphic => true |
#taxed? ⇒ Boolean
52 53 54 |
# File 'app/models/order_line_item.rb', line 52 def taxed? line_item_record.taxable? end |
#to_label ⇒ Object
Alias for to_s
136 137 138 |
# File 'app/models/order_line_item.rb', line 136 def to_label to_s end |
#to_s ⇒ Object
description of line_item_record
141 142 143 144 145 146 147 |
# File 'app/models/order_line_item.rb', line 141 def to_s if line_item_record line_item_record.description else nil end end |
#total_amount(currency = Currency.usd) ⇒ Object
get the total charges for a order_line_item. The total will be returned as Money. There may be multiple Monies assocated with an order, such as points and dollars. To handle this, the method should return an array of Monies if a currency is passed in return the amount for only that currency
70 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 100 101 102 103 104 105 106 107 108 |
# File 'app/models/order_line_item.rb', line 70 def total_amount(currency=Currency.usd) if currency and currency.is_a?(String) currency = Currency.send(currency) end charges = {} # get sold price # TODO currency will eventually need to be accounted for here. Solid price should probably be a money record if self.sold_price charges["USD"] ||= {amount: 0} charges["USD"][:amount] += (self.sold_price * (self.quantity || 1)) end # get all of the charge lines associated with the order_line charge_lines.each do |charge| charge_money = charge.money total_by_currency = charges[charge_money.currency.internal_identifier] unless total_by_currency total_by_currency = { amount: 0 } end total_by_currency[:amount] += charge_money.amount unless charge_money.amount.nil? charges[charge_money.currency.internal_identifier] = total_by_currency end # if currency was based only return that amount # if there is only one currency then return that amount # if there is more than once currency return the hash if currency charges[currency.internal_identifier][:amount].round(2) elsif charges.keys.count == 1 charges end end |