Update transaction on updating commercial_document

79 views
Skip to first unread message

Prashanta Mahato

unread,
Nov 12, 2013, 5:01:11 AM11/12/13
to plutu...@googlegroups.com
 

Hello All,

I am using Plutus gem to make an invoice, which is editable.My Invoice Controller and model code are given below, can you advice me how to improve my code more efficientely,mainly when I am updating an invoice.


Thanks for your guidance.


Invoice Controller for create and Update Invoic


def create

Invoice.transaction do

@invoice = Invoice.new(invoice_params)

if @invoice.save

@transaction = Invoice.make_transaction(@invoice)

raise ActiveRecord::Rollback unless @transaction.save

end

end

redirect_to invoices_path,:notice=>"#{@transaction.errors.first}" if !@transaction.save

redirect_to @invoice if @transaction.save

end


def update

Invoice.transaction do

if @invoice.update_attributes(invoice_params)

@transaction = Invoice.update_transaction(@invoice)

raise ActiveRecord::Rollback unless @transaction.save

else

redirect_to render action: 'edit'

redirect_to { render json: @invoice.errors, status: :unprocessable_entity }

end

end

redirect_to @invoice if @transaction.save

end


In my Invoice Model

has_one :transaction, :as => :commercial_document,:class_name => "Plutus::Transaction"


def self.make_transaction(invoice)

transaction = Plutus::Transaction.build(:transaction_date => invoice.issue_date,:description => "Being Invoice Created", :commercial_document => invoice,

:debits=>self.debits(invoice),:credits=>self.credits(invoice))

transaction

end


def self.update_transaction(invoice)

prev_transaction = Plutus::Transaction.find(invoice.transaction.id)

prev_transaction.debit_amounts.delete_all

prev_transaction.credit_amounts.delete_all

prev_transaction.delete

transaction = Plutus::Transaction.build(:transaction_date => invoice.issue_date,:description => "Being Invoice Created", :commercial_document => invoice,

:debits=>self.debits(invoice),:credits=>self.credits(invoice))

transaction

end



regards.

Prashanta Mahato

mbulat

unread,
Nov 14, 2013, 12:00:05 PM11/14/13
to plutu...@googlegroups.com
I'm not sure exactly what you are asking. However in general it's probably a bad idea to allow transactions to be deleted or edited. Plutus does not have a separate ledger posting process, and as such, any reports that are being generated on your accounts would be changing by allowing transactions to be edited.

I would advise having a process where you ensure any invoices generated are correct prior to creating a transaction in Plutus. 
Reply all
Reply to author
Forward
0 new messages