Class: PurchaseOrder

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/purchase_order.rb

Overview

Schema Information

Table name: purchase_orders

id                   :integer(4)      not null, primary key
supplier_id          :integer(4)      not null
invoice_number       :string(255)
tracking_number      :string(255)
notes                :string(255)
state                :string(255)
ordered_at           :datetime        not null
estimated_arrival_on :date
created_at           :datetime
updated_at           :datetime
total_cost           :decimal(8, 2)   default(0.0), not null

Constant Summary

INCOMPLETE =
'incomplete'
PENDING =
'pending'
RECEIVED =
'received'
STATES =
[PENDING, INCOMPLETE, RECEIVED]

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Array[PurchaseOrder]) admin_grid(params = {})

paginated results from the admin PurchaseOrder grid

Parameters:

  • (Optional params)

Returns:



158
159
160
161
162
163
164
# File 'app/models/purchase_order.rb', line 158

def self.admin_grid(params = {})
  grid = includes(:supplier)
  grid = grid.where("suppliers.name LIKE ?",                  "#{params[:supplier_name]}%")   if params[:supplier_name].present?
  grid = grid.where("purchase_orders.invoice_number LIKE ?",  "#{params[:invoice_number]}%")  if params[:invoice_number].present?
  grid = grid.where("purchase_orders.tracking_number LIKE ?", "#{params[:tracking_number]}%") if params[:tracking_number].present?
  grid
end

+ (Array[PurchaseOrder]) receiving_admin_grid(params = {})

paginated results from the admin PurchaseOrder grid for PO to receive

Parameters:

  • (Optional params)

Returns:



170
171
172
173
174
175
176
177
# File 'app/models/purchase_order.rb', line 170

def self.receiving_admin_grid(params = {})
  grid = where(['purchase_orders.state != ?', PurchaseOrder::RECEIVED])#.where("suppliers.name = ?", params[:name])
  grid = grid.includes([:supplier, :purchase_order_variants])
  grid = grid.where("suppliers.name LIKE ?",                  "#{params[:supplier_name]}%")   if params[:supplier_name].present?
  grid = grid.where("purchase_orders.invoice_number LIKE ?",  "#{params[:invoice_number]}%")  if params[:invoice_number].present?
  grid = grid.where("purchase_orders.tracking_number LIKE ?", "#{params[:tracking_number]}%") if params[:tracking_number].present?
  grid
end

Instance Method Details

- (Object) display_estimated_arrival_on



96
97
98
# File 'app/models/purchase_order.rb', line 96

def display_estimated_arrival_on
  estimated_arrival_on? ? estimated_arrival_on.to_s(:format => :us_date) : ""
end

- (String) display_received

returns "Yes" if the PO has been received, otherwise "No"

Parameters:

  • (none)

Returns:

  • (String)

    "Yes" or "No"



92
93
94
# File 'app/models/purchase_order.rb', line 92

def display_received
  receive_po ? 'Yes' : 'No'
end

- (String) display_tracking_number

returns "the tracking #" if the tracking # exists, otherwise "N/A"

Parameters:

  • (none)

Returns:

  • (String)

    "Yes" or "No"



104
105
106
# File 'app/models/purchase_order.rb', line 104

def display_tracking_number
  tracking_number? ? tracking_number : 'N/A'
end

- (Object) pay_for_order



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/purchase_order.rb', line 128

def pay_for_order
  now = Time.zone.now
  if self.batches.empty?
      batch = self.batches.create()
      transaction = ReceivePurchaseOrder.new()##  This is a type of transaction
      credit = self.transaction_ledgers.new(:transaction_account_id => TransactionAccount::CASH_ID,     :debit => 0,      :credit => total_cost, :period => "#{now.month}-#{now.year}")
      debit  = self.transaction_ledgers.new(:transaction_account_id => TransactionAccount::EXPENSE_ID, :debit => total_cost, :credit => 0,      :period => "#{now.month}-#{now.year}")
      transaction.transaction_ledgers.push(credit)
      transaction.transaction_ledgers.push(debit)
      batch.transactions.push(transaction)
      batch.save
  else # thus we are paying after having received the item from credit
    batch       = batches.first
    transaction = ReceivePurchaseOrder.new()

    debit   = self.transaction_ledgers.new(:transaction_account_id => TransactionAccount::EXPENSE_ID, :debit => total_cost, :credit => 0,       :period => "#{now.month}-#{now.year}")
    credit  = self.transaction_ledgers.new(:transaction_account_id => TransactionAccount::ACCOUNTS_PAYABLE_ID,    :debit => 0,      :credit => total_cost,  :period => "#{now.month}-#{now.year}")

    transaction.transaction_ledgers.push(credit)
    transaction.transaction_ledgers.push(debit)

    batch.transactions.push(transaction)
    batch.save
  end
end

- (Object) receive_order_from_credit



116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/purchase_order.rb', line 116

def receive_order_from_credit

      batch = self.batches.create()
      transaction = ReceivePurchaseOrder.new()##  This is a type of transaction
      credit = self.transaction_ledgers.new(:transaction_account_id => TransactionAccount::CASH_ID,     :debit => 0,      :credit => total_cost, :period => "#{now.month}-#{now.year}")
      debit  = self.transaction_ledgers.new(:transaction_account_id => TransactionAccount::ACCOUNTS_PAYABLE_ID, :debit => total_cost, :credit => 0,      :period => "#{now.month}-#{now.year}")
      transaction.transaction_ledgers.push(credit)
      transaction.transaction_ledgers.push(debit)
      batch.transactions.push(transaction)
      batch.save
end

- (Boolean) receive_po

in the admin form this is the method called when the form is created, The method determines if the order has already been received

Returns:

  • (Boolean)


73
74
75
# File 'app/models/purchase_order.rb', line 73

def receive_po
  (state == RECEIVED)
end

- (none) receive_po=(answer)

in the admin form this is the method called when the form is submitted, The method sets the PO to complete, pays for the order in the accounting peice and adds the inventory to stock

Parameters:

  • value (String)

    for set_keywords in a products form

Returns:

  • (none)


62
63
64
65
66
67
# File 'app/models/purchase_order.rb', line 62

def receive_po=(answer)

  if (answer == 'true' || answer == '1') && (state != RECEIVED)
    complete!
  end
end

- (none) receive_variants

called by state machine after the PO is complete. adds the inventory to stock

Parameters:

  • (none)

Returns:

  • (none)


81
82
83
84
85
86
# File 'app/models/purchase_order.rb', line 81

def receive_variants
  po_variants = PurchaseOrderVariant.where(:purchase_order_id => self.id).lock("LOCK IN SHARE MODE").all
  po_variants.each do |po_variant|
    po_variant.receive! unless po_variant.is_received?
  end
end

- (String) supplier_name

returns "Suppliers name" if the supplier exists, otherwise "N/A"

Parameters:

  • (none)

Returns:

  • (String)

    "Yes" or "No"



112
113
114
# File 'app/models/purchase_order.rb', line 112

def supplier_name
  supplier.name rescue 'N/A'
end