erp_invoicing 3.0.0 → 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/app/controllers/erp_invoicing/erp_app/organizer/bill_pay/accounts_controller.rb +98 -46
  2. data/app/controllers/erp_invoicing/erp_app/organizer/bill_pay/base_controller.rb +1 -1
  3. data/app/controllers/erp_invoicing/erp_app/shared/billing_accounts_controller.rb +111 -0
  4. data/app/controllers/erp_invoicing/erp_app/shared/files_controller.rb +52 -0
  5. data/app/controllers/erp_invoicing/erp_app/shared/invoices_controller.rb +208 -0
  6. data/app/controllers/erp_invoicing/sms_controller.rb +156 -0
  7. data/app/models/billing_account.rb +204 -0
  8. data/app/models/extensions/document.rb +5 -0
  9. data/app/models/extensions/financial_txn.rb +5 -0
  10. data/app/models/extensions/party.rb +10 -0
  11. data/app/models/invoice.rb +100 -7
  12. data/app/models/invoice_item.rb +35 -2
  13. data/app/models/invoice_payment_strategy_type.rb +5 -0
  14. data/app/models/invoice_payment_term.rb +4 -0
  15. data/app/models/invoice_payment_term_set.rb +6 -0
  16. data/app/models/invoice_payment_term_type.rb +3 -0
  17. data/app/models/payment_application.rb +6 -1
  18. data/app/models/recurring_payment.rb +40 -0
  19. data/config/routes.rb +8 -1
  20. data/db/data_migrations/20111121153349_create_bill_pay_organizer_application.rb +1 -1
  21. data/db/data_migrations/20120118181839_create_invoice_management_desktop_application.rb +26 -0
  22. data/db/data_migrations/20120229174322_add_billpay_widget.rb +28 -0
  23. data/db/migrate/20111121000000_invoicing_services.rb +170 -103
  24. data/db/migrate/20120228184317_add_text_to_pay_to_recurring_payments.rb +13 -0
  25. data/db/migrate/20120301155722_add_billing_date_to_billing_account.rb +13 -0
  26. data/lib/erp_invoicing/engine.rb +5 -0
  27. data/lib/erp_invoicing/version.rb +7 -1
  28. data/lib/erp_invoicing.rb +1 -0
  29. data/public/javascripts/erp_app/desktop/applications/invoice_management/billing_accounts_panel.js +80 -0
  30. data/public/javascripts/erp_app/desktop/applications/invoice_management/invoices_panel.js +48 -0
  31. data/public/javascripts/erp_app/desktop/applications/invoice_management/module.js +36 -0
  32. data/public/javascripts/erp_app/organizer/applications/bill_pay/base.js +88 -76
  33. data/public/javascripts/erp_app/organizer/applications/bill_pay/extensions.js +6 -97
  34. data/public/javascripts/erp_app/organizer/applications/bill_pay/make_payment_window.js +19 -13
  35. data/public/javascripts/erp_app/organizer/applications/bill_pay/payment_accounts_grid_panel.js +2 -1
  36. data/public/javascripts/erp_app/shared/add_invoice_window.js +220 -0
  37. data/public/javascripts/erp_app/shared/billing_accounts_grid_panel.js +318 -0
  38. data/public/javascripts/erp_app/shared/invoice_items_grid_panel.js +152 -0
  39. data/public/javascripts/erp_app/shared/invoices_grid_panel.js +363 -0
  40. data/public/javascripts/erp_app/shared/payments_grid_panel.js +74 -0
  41. data/spec/dummy/Rakefile +7 -0
  42. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  43. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  44. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  45. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  46. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  47. data/spec/dummy/config/application.rb +50 -0
  48. data/spec/dummy/config/boot.rb +10 -0
  49. data/spec/dummy/config/database.yml +8 -0
  50. data/spec/dummy/config/environment.rb +5 -0
  51. data/spec/dummy/config/environments/spec.rb +27 -0
  52. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  53. data/spec/dummy/config/initializers/inflections.rb +10 -0
  54. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  55. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  56. data/spec/dummy/config/initializers/session_store.rb +8 -0
  57. data/spec/dummy/config/initializers/wrap_parameters.rb +12 -0
  58. data/spec/dummy/config/locales/en.yml +5 -0
  59. data/spec/dummy/config/routes.rb +3 -0
  60. data/spec/dummy/config.ru +4 -0
  61. data/spec/dummy/public/404.html +26 -0
  62. data/spec/dummy/public/422.html +26 -0
  63. data/spec/dummy/public/500.html +26 -0
  64. data/spec/dummy/public/favicon.ico +0 -0
  65. data/spec/dummy/script/rails +6 -0
  66. data/spec/factories/basic.rb +9 -0
  67. data/spec/models/billing_account_spec.rb +78 -0
  68. data/spec/models/invoice_item_spec.rb +22 -0
  69. data/spec/models/invoice_spec.rb +58 -0
  70. data/spec/models/payment_application_spec.rb +17 -0
  71. data/spec/spec_helper.rb +60 -0
  72. metadata +148 -67
  73. data/app/widgets/bill_pay/base.rb +0 -230
  74. data/app/widgets/bill_pay/helpers/controller/bill_pay_controller_helper.rb +0 -3
  75. data/app/widgets/bill_pay/helpers/view/bill_pay_view_helper.rb +0 -3
  76. data/app/widgets/bill_pay/javascript/bill_pay.js +0 -11
  77. data/app/widgets/bill_pay/views/_menu.html.erb +0 -63
  78. data/app/widgets/bill_pay/views/_payment_history_table.html.erb +0 -115
  79. data/app/widgets/bill_pay/views/_statement_table.html.erb +0 -117
  80. data/app/widgets/bill_pay/views/account_home.html.erb +0 -10
  81. data/app/widgets/bill_pay/views/index.html.erb +0 -4
  82. data/app/widgets/bill_pay/views/make_payment.html.erb +0 -24
  83. data/app/widgets/bill_pay/views/payment_account_forms/bank_account.html.erb +0 -28
  84. data/app/widgets/bill_pay/views/payment_account_forms/credit_card.html.erb +0 -48
  85. data/app/widgets/bill_pay/views/payment_accounts.html.erb +0 -120
  86. data/app/widgets/bill_pay/views/payment_history.html.erb +0 -104
  87. data/app/widgets/bill_pay/views/pdf/layout.pdf.erb +0 -35
  88. data/app/widgets/bill_pay/views/statements.html.erb +0 -4
  89. data/public/javascripts/erp_app/organizer/applications/bill_pay/accounts_grid_panel.js +0 -182
@@ -44,58 +44,110 @@ module ErpInvoicing
44
44
  render :json => transactions
45
45
  end
46
46
 
47
+ def make_payment_on_account
48
+ #get configuration
49
+
50
+ @message = nil
51
+
52
+ #get subscription selection if it is there
53
+ @subscription_selection = get_subscription_selection if params[:subscription_selection]
54
+
55
+ #get convenience_fee if it is there
56
+ @convenience_fee = get_convenience_fee if params[:convenience_fee]
57
+
58
+ #get additional payments
59
+ #@aditional_payments = get_additional_payments
60
+
61
+ @billing_account_payment_amts = get_billing_account_amounts
62
+ Rails.logger.debug("##### Billing Acct Payments: #{@billing_account_payment_amts}")
63
+ @payment_account_root = BizTxnAcctRoot.find(params[:payment_account_id])
64
+ @payment_account = @payment_account_root.account
65
+ @payment_date = params[:payment_date]
66
+ @total_payment = params[:total_payment]
67
+
68
+ money = Money.create(
69
+ :amount => @total_payment.to_f,
70
+ :description => "Online Payment",
71
+ :currency => Currency.usd
72
+ )
73
+ financial_txn = FinancialTxn.create(
74
+ :apply_date => Date.strptime(@payment_date,"%m/%d/%Y"),
75
+ :money => money
76
+ )
77
+ financial_txn.description = "Online Payment"
78
+ financial_txn.account = @payment_account_root
79
+ financial_txn.save
80
+
81
+ @billing_account_payment_amts.each do |hash|
82
+ amount = hash[:amount].to_f rescue 0
83
+ if amount > 0
84
+ comment = (params[:short_payment_comment] and hash[:short_payment]) ? params[:short_payment_comment] : nil
85
+ PaymentApplication.create(
86
+ :financial_txn => financial_txn,
87
+ :payment_applied_to => hash[:billing_account],
88
+ :money => Money.create(:amount => hash[:amount].to_f),
89
+ :comment => comment
90
+ )
91
+ end
92
+ end
93
+
94
+ if financial_txn.apply_date == Date.today
95
+ case @payment_account.class.to_s
96
+ when "BankAccount"
97
+ financial_txn.txn_type = BizTxnType.ach_sale
98
+ financial_txn.save
99
+ result = @payment_account.purchase(financial_txn, ErpCommerce::Config.active_merchant_gateway_wrapper)
100
+ if !result[:payment].nil? and result[:payment].success
101
+ @authorization_code = result[:payment].authorization_code
102
+ else
103
+ @message = result[:message]
104
+ end
105
+ when "CreditCardAccount"
106
+ financial_txn.txn_type = BizTxnType.cc_sale
107
+ financial_txn.save
108
+ result = @payment_account.purchase(financial_txn, params[:cvv], ErpCommerce::Config.active_merchant_gateway_wrapper)
109
+ if !result[:payment].nil? and result[:payment].success
110
+ @authorization_code = result[:payment].authorization_code
111
+ else
112
+ @message = result[:message]
113
+ end
114
+ end
115
+ end
116
+
117
+ if @message.nil?
118
+ render :json => {:success => true, :message => "Payment Successful!" }
119
+ else
120
+ render :json => {:success => false, :message => @message }
121
+ end
122
+ end
123
+
47
124
  def payment_accounts
48
- payment_accounts = {:payment_accounts => [
49
- {
50
- :id => 1,
51
- :description => 'My Bank Account',
52
- :account_type => 'Bank Account',
53
- :routing_number => '0123456',
54
- :account_number => '9999321',
55
- :edit_action => :edit_back_account
56
- },
57
- {
58
- :id => 2,
59
- :description => 'American Express',
60
- :account_type => 'Credit Card',
61
- :card_number => '1111222233334444',
62
- :security_code => '123',
63
- :exp_year => '9',
64
- :exp_month => '2012',
65
- :name_on_card => 'John Doe',
66
- :edit_action => :edit_credit_card_account
125
+ party = Party.find(params[:party_id])
126
+
127
+ results = party.accounts.map do |acct|
128
+ if acct.biz_txn_acct_type == "CreditCardAccount" || acct.biz_txn_acct_type == 'BankAccount'
129
+ { :id => acct.id,
130
+ :description => acct.description,
131
+ :account_type => acct.biz_txn_acct_type
67
132
  }
68
- ]}
133
+ end
134
+ end
135
+
136
+ results.reject!{|x| x.nil?}
69
137
 
70
- render :json => payment_accounts
138
+ render :json => results
71
139
  end
72
140
 
73
141
  def generate_statement
74
- @statement = {
75
- :statement_date => '10/31/2011',
76
- :transactions => [
77
- {
78
- :date => '10/10/2011',
79
- :description => 'Monthly Billing Amount',
80
- :amount => help.number_to_currency(50.00)
81
- },
82
- {
83
- :date => '10/15/2011',
84
- :description => 'Payment Recieved',
85
- :amount => help.number_to_currency(-50.00)
86
- }
87
- ]
88
- }
89
- @title = "10/31/2011 Statement"
90
- @body_options = {:file => File.join(Widgets::BillPay::Base.views_location,'_statement_table.html.erb'), :locals => {:transactions => @statement[:transactions]}}
91
- html = render_to_string(
92
- :file => File.join(Widgets::BillPay::Base.views_location,'pdf','layout.pdf.erb'),
93
- :layout => false
94
- )
95
142
 
96
- kit = PDFKit.new(html)
97
- send_data(kit.to_pdf, :filename => "Statement.pdf", :type => "application/pdf")
98
- return
143
+ end
144
+ def get_billing_account_amounts
145
+ billing_account_payment_amts = []
146
+ billing_account = BillingAccount.find(params["billing_account_id"])
147
+ amount = params[:total_payment]
148
+ billing_account_payment_amts << {:billing_account => billing_account, :amount => amount, :short_payment => (amount.to_f < billing_account.balance)}
149
+
150
+ billing_account_payment_amts
99
151
  end
100
152
 
101
153
  end
@@ -104,5 +156,5 @@ module ErpInvoicing
104
156
  end#ErpApp
105
157
  end#ErpInvoicing
106
158
 
107
-
159
+
108
160
 
@@ -7,7 +7,7 @@ module ErpInvoicing
7
7
  def menu
8
8
  menu = []
9
9
 
10
- menu << {:text => 'Bill Pay', :leaf => true, :iconCls => 'icon-credit_cards', :applicationCardId => "billpay_layout"}
10
+ menu << {:text => 'Bill Pay', :leaf => true, :iconCls => 'icon-creditcards', :applicationCardId => "billpay-application"}
11
11
 
12
12
  render :json => menu
13
13
  end
@@ -0,0 +1,111 @@
1
+ module ErpInvoicing
2
+ module ErpApp
3
+ module Shared
4
+ class BillingAccountsController < ::ErpApp::Desktop::BaseController
5
+ @@date_format = "%m/%d/%Y"
6
+
7
+ def index
8
+ render :json => if request.put?
9
+ update_billing_account
10
+ elsif request.post?
11
+ create_billing_account
12
+ elsif request.get?
13
+ get_billing_accounts
14
+ elsif request.delete?
15
+ delete_billing_account
16
+ end
17
+ end
18
+
19
+ def get_billing_accounts
20
+ result = {:success => true, :billing_accounts => [], :totalCount => 0}
21
+
22
+ sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first)
23
+ sort = sort_hash[:sort] || 'created_at'
24
+ dir = sort_hash[:dir] || 'DESC'
25
+ limit = params[:limit] || 50
26
+ start = params[:start] || 0
27
+ party_id = params[:party_id] || nil
28
+
29
+ billing_accounts = (party_id.blank? ? BillingAccount.scoped.includes(:financial_txn_account) : BillingAccount.scoped.includes(:financial_txn_account => {:biz_txn_acct_root => :biz_txn_acct_party_roles}))
30
+ billing_accounts = billing_accounts.where(:financial_txn_accounts => {:account_number => params[:account_number]}) unless params[:account_number].blank?
31
+ billing_accounts = billing_accounts.where(:financial_txn_accounts => {:biz_txn_acct_roots => {:biz_txn_acct_party_roles => {:party_id => party_id }}}) unless party_id.blank?
32
+ billing_accounts = billing_accounts.order("financial_txn_accounts.#{sort} #{dir}").limit(limit).offset(start)
33
+ result[:totalCount] = billing_accounts.count
34
+
35
+ result.tap do |result_tap|
36
+ result_tap[:billing_accounts] = billing_accounts.all.collect do |billing_account|
37
+ billing_account.to_hash(:only => [], :methods => [:id,:send_paper_bills,:payable_online,:account_number,:calculate_balance,:payment_due,:balance,:balance_date,:due_date])
38
+ end#end billing_accounts collect
39
+ result_tap[:totalCount] = BillingAccount.count(:all)
40
+ end#end result tap
41
+ end
42
+
43
+ def create_billing_account
44
+ if params[:billingAccount]
45
+ data = params[:billingAccount]
46
+ else
47
+ data = params
48
+ data[:due_date] = Date.strptime(params[:due_date], @@date_format)
49
+ data[:balance_date] = Date.strptime(params[:balance_date], @@date_format)
50
+ end
51
+
52
+ billing_account = BillingAccount.new
53
+ billing_account.account_number = data[:account_number]
54
+ billing_account.calculate_balance = data[:calculate_balance]
55
+ billing_account.payment_due = data[:payment_due]
56
+ billing_account.balance = data[:balance]
57
+ billing_account.due_date = data[:due_date]
58
+ billing_account.balance_date = data[:balance_date]
59
+ billing_account.payable_online = data[:payable_online]
60
+ billing_account.send_paper_bills = data[:send_paper_bills]
61
+
62
+ if params[:party_id]
63
+ party = Party.find(params[:party_id])
64
+ primary_role_type = BizTxnAcctPtyRtype.find_by_internal_identifier('primary')
65
+ billing_account.add_party_with_role(party, primary_role_type)
66
+ end
67
+
68
+ if billing_account.save
69
+ {:success => true, :billing_accounts => billing_account.to_hash(:only => [], :methods => [:id,:send_paper_bills,:payable_online,:account_number,:calculate_balance,:payment_due,:balance,:balance_date,:due_date])}
70
+ else
71
+ {:success => false}
72
+ end
73
+ end
74
+
75
+ def update_billing_account
76
+ if params[:billingAccount]
77
+ data = params[:billingAccount]
78
+ else
79
+ data = params
80
+ data[:due_date] = Date.strptime(params[:due_date], @@date_format)
81
+ data[:balance_date] = Date.strptime(params[:balance_date], @@date_format)
82
+ end
83
+
84
+ billing_account = BillingAccount.find(data[:id])
85
+
86
+ billing_account.account_number = data[:account_number]
87
+ billing_account.calculate_balance = data[:calculate_balance]
88
+ billing_account.due_date = data[:due_date]
89
+ billing_account.balance_date = data[:balance_date]
90
+ billing_account.payable_online = data[:payable_online]
91
+ billing_account.send_paper_bills = data[:send_paper_bills]
92
+ billing_account.payment_due = data[:payment_due]
93
+ billing_account.balance = data[:balance]
94
+
95
+ if billing_account.save
96
+ {:success => true, :billing_accounts => billing_account.to_hash(:only => [], :methods => [:id,:send_paper_bills,:payable_online,:account_number,:calculate_balance,:payment_due,:balance,:balance_date,:due_date])}
97
+ else
98
+ {:success => false}
99
+ end
100
+ end
101
+
102
+ def delete_billing_account
103
+ BillingAccount.find(params[:id]).destroy
104
+
105
+ {:success => true}
106
+ end
107
+
108
+ end#BillingAccountsController
109
+ end#Shared
110
+ end#ErpApp
111
+ end#ErpInvoicing
@@ -0,0 +1,52 @@
1
+ module ErpInvoicing
2
+ module ErpApp
3
+ module Shared
4
+ class FilesController < ::ErpApp::Desktop::FileManager::BaseController
5
+ before_filter :set_invoice
6
+
7
+ def base_path
8
+ @base_path = File.join(@file_support.root, Rails.application.config.erp_tech_svcs.file_assets_location, 'documents', @invoice.invoice_number)
9
+ end
10
+
11
+ def upload_file
12
+ clear_files
13
+
14
+ result = {}
15
+ upload_path = request.env['HTTP_EXTRAPOSTDATA_DIRECTORY'].blank? ? params[:directory] : request.env['HTTP_EXTRAPOSTDATA_DIRECTORY']
16
+ name = request.env['HTTP_X_FILE_NAME'].blank? ? params[:file_data].original_filename : request.env['HTTP_X_FILE_NAME']
17
+ data = request.env['HTTP_X_FILE_NAME'].blank? ? params[:file_data] : request.raw_post
18
+
19
+ begin
20
+ @invoice.add_file(data, File.join(base_path, name))
21
+ result = {:success => true}
22
+ rescue Exception=>ex
23
+ logger.error ex.message
24
+ logger.error ex.backtrace.join("\n")
25
+ result = {:success => false, :error => "Error uploading file."}
26
+ end
27
+
28
+ #the awesome uploader widget whats this to mime type text, leave it render :inline
29
+ render :inline => result.to_json
30
+ end
31
+
32
+ def set_invoice
33
+ @invoice = Invoice.find(params[:invoice_id])
34
+ end
35
+
36
+ def set_file_support
37
+ @file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage)
38
+ end
39
+
40
+ private
41
+
42
+ def clear_files
43
+ @invoice.files.each do |file|
44
+ result, message, is_folder = @file_support.delete_file(File.join(@file_support.root,file.directory,file.name))
45
+ file.destroy
46
+ end
47
+ end
48
+
49
+ end#FilesController
50
+ end#Shared
51
+ end#ErpApp
52
+ end#ErpInvoicing
@@ -0,0 +1,208 @@
1
+ module ErpInvoicing
2
+ module ErpApp
3
+ module Shared
4
+ class InvoicesController < ::ErpApp::Desktop::BaseController
5
+
6
+ def parties
7
+ render :json => {
8
+ :parties => Party.all.collect{|party|
9
+ {:id => party.id, :description => party.description}
10
+ }
11
+ }
12
+ end
13
+
14
+ def sms_notification
15
+ invoice = Invoice.find(params[:id])
16
+ if invoice.billing_account.send_sms_notification
17
+ render :json => {:success => true}
18
+ else
19
+ render :json => {:success => false}
20
+ end
21
+ end
22
+
23
+ def billing_accounts
24
+ render :json => {
25
+ :billing_accounts => BillingAccount.all.collect{|billing_account|
26
+ {:id => billing_account.id, :account_number => billing_account.account_number}
27
+ }
28
+ }
29
+ end
30
+
31
+ def email_invoice
32
+ invoice = Invoice.find(params[:id])
33
+ DocumentMailer.email_document(params[:to_email], invoice.document).deliver
34
+
35
+ render :json => {:success => true}
36
+ end
37
+
38
+ def print_invoice
39
+ invoice = Invoice.find(params[:id])
40
+ send_data(File.read(invoice.files.first.data.path), {type: 'application/pdf', disposition: 'inline'})
41
+ end
42
+
43
+ def invoices
44
+ render :json => if request.put?
45
+ #update_invoice #not implemented yet
46
+ elsif request.get?
47
+ get_invoices
48
+ elsif request.delete?
49
+ delete_invoice
50
+ end
51
+ end
52
+
53
+ def get_invoices
54
+ result = {:success => true, :invoices => [], :totalCount => 0}
55
+
56
+ sort_hash = params[:sort].blank? ? {} : Hash.symbolize_keys(JSON.parse(params[:sort]).first)
57
+ sort = sort_hash[:sort] || 'created_at'
58
+ dir = sort_hash[:dir] || 'DESC'
59
+ limit = params[:limit] || 50
60
+ start = params[:start] || 0
61
+
62
+ invoices = Invoice.scoped
63
+ invoices = invoices.where('invoice_number = ?', params[:invoice_number]) unless params[:invoice_number].blank?
64
+ invoices = invoices.where('billing_account_id = ?', params[:billing_account_id]) unless params[:billing_account_id].blank?
65
+ invoices = invoices.order("#{sort} #{dir}").limit(limit).offset(start)
66
+ result[:totalCount] = invoices.count
67
+
68
+
69
+ invoices.all.each do |invoice|
70
+ result[:invoices] << {
71
+ :id => invoice.id,
72
+ :invoice_number => invoice.invoice_number,
73
+ :description => invoice.description,
74
+ :message => invoice.message,
75
+ :invoice_date => invoice.invoice_date,
76
+ :due_date => invoice.due_date,
77
+ :billing_account => invoice.billing_account.account_number,
78
+ :payment_due => invoice.payment_due
79
+ #:billed_to_party => (invoice.find_parties_by_role_type('billed_to').first.description rescue ''),
80
+ #:billed_from_party => (invoice.find_parties_by_role_type('billed_from').first.description rescue '')
81
+ }
82
+ end
83
+
84
+ result
85
+ end
86
+
87
+ def create_invoice
88
+ invoice_date = Date.strptime(params[:invoice_date],"%m/%d/%Y")
89
+ due_date = Date.strptime(params[:due_date],"%m/%d/%Y")
90
+
91
+ invoice = Invoice.new(
92
+ :invoice_number => params[:invoice_number],
93
+ :invoice_date => invoice_date,
94
+ :due_date => due_date,
95
+ :message => params[:message],
96
+ :description => params[:description]
97
+ )
98
+
99
+ if invoice.save
100
+ billing_account = BillingAccount.find(params[:billing_account_id])
101
+ billing_account.invoices << invoice
102
+ billing_account.save
103
+
104
+ render :json => {:success => true}
105
+ else
106
+
107
+ render :json => {:success => false}
108
+ end
109
+ end
110
+
111
+ def delete_invoice
112
+ Invoice.find(params[:id]).destroy
113
+
114
+ {:success => true}
115
+ end
116
+
117
+ def invoice_items
118
+ render :json => if request.post?
119
+ create_invoice_item
120
+ elsif request.put?
121
+ update_invoice_item
122
+ elsif request.get?
123
+ get_invoice_items
124
+ elsif request.delete?
125
+ delete_invoice_item
126
+ end
127
+ end
128
+
129
+ def create_invoice_item
130
+ data = params[:data]
131
+ invoice = Invoice.find(params[:invoice_id])
132
+ invoice_item = InvoiceItem.create(:amount => data[:amount].to_f, :quantity => data[:quantity], :item_description => data[:item_description])
133
+ invoice.items << invoice_item
134
+
135
+ {:success => true,
136
+ :invoice_items => {
137
+ :item_description => invoice_item.item_description,
138
+ :quantity => invoice_item.quantity,
139
+ :amount => invoice_item.amount,
140
+ :id => invoice_item.id,
141
+ :created_at => invoice_item.created_at
142
+ }
143
+ }
144
+ end
145
+
146
+ def update_invoice_item
147
+ data = params[:data]
148
+ invoice_item = InvoiceItem.find(data[:id])
149
+ invoice_item.amount = data[:amount].to_f
150
+ invoice_item.quantity = data[:quantity]
151
+ invoice_item.item_description = data[:item_description]
152
+ invoice_item.save
153
+
154
+ {:success => true,
155
+ :invoice_items => {
156
+ :item_description => invoice_item.item_description,
157
+ :quantity => invoice_item.quantity,
158
+ :amount => invoice_item.amount,
159
+ :id => invoice_item.id,
160
+ :created_at => invoice_item.created_at
161
+ }
162
+ }
163
+ end
164
+
165
+ def get_invoice_items
166
+ result = {:success => true, :invoice_items => [], :totalCount => 0}
167
+
168
+ invoice = Invoice.find(params[:invoice_id])
169
+ result[:totalCount] = invoice.items.count
170
+ invoice.items.each do |item|
171
+ result[:invoice_items] << {
172
+ :item_description => item.item_description,
173
+ :quantity => item.quantity,
174
+ :amount => item.amount,
175
+ :id => item.id,
176
+ :created_at => item.created_at
177
+ }
178
+ end
179
+ result
180
+ end
181
+
182
+ def delete_invoice_item
183
+ InvoiceItem.find(params[:id]).destroy
184
+
185
+ {:success => true}
186
+ end
187
+
188
+ def invoice_payments
189
+ result = {:success => true, :payments => [], :totalCount => 0}
190
+
191
+ invoice = Invoice.find(params[:invoice_id])
192
+
193
+ invoice.payment_applications.each do |item|
194
+ result[:payments] << {
195
+ :payment_date => item.financial_txn.apply_date,
196
+ :post_date => item.financial_txn.captured_payment.created_at,
197
+ :payment_account => item.financial_txn.account.description,
198
+ :amount => item.financial_txn.money.amount
199
+ }
200
+ end
201
+
202
+ render :json => result
203
+ end
204
+
205
+ end#InvoicesController
206
+ end#Shared
207
+ end#ErpApp
208
+ end#ErpInvoicing