mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-18 02:20:37 -05:00
Add a customer payments page
Payments on the customer page lists the customer's QuickBooks payments, newest first, with the invoices each was applied to, memo, amount, unapplied credit and totals; each links to the payment page and its PDF. A new View payments permission allows the list, payment page and PDF; Receive payments includes it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM
This commit is contained in:
10 files changed
+133
-7
No files matched your search
@@ -129,6 +129,7 @@ Customer records are automatically updated in the local database when changes oc
|
||||
* With none ticked, the payment is applied to the customer's open invoices **oldest first**, like QuickBooks does; the page shows how it will be split as the amount is typed, and it's worked out again from QuickBooks when saving. Anything more than the open balances stays as a credit.
|
||||
* Tick **Deposit** to keep the whole payment as a credit on the customer's account without paying any invoice (also what happens when there are no open invoices).
|
||||
* The saved payment's page shows where it was applied and has **Print**, QuickBooks' payment PDF.
|
||||
* **Payments** on the customer page lists all of the customer's payments from QuickBooks, newest first: date, method, reference no., the invoices each was applied to (or Deposit), memo, amount and what's still unapplied, with totals. Each date opens the payment's page and **Print** opens its PDF.
|
||||
|
||||
### QuickBooks custom fields
|
||||
|
||||
@@ -165,7 +166,8 @@ Administration → Items lists the QuickBooks product/service list (synced from
|
||||
* **Write estimates**: create and edit estimates
|
||||
* **Email estimates**: email estimates to customers (Save and send, and the Email link)
|
||||
* **Create issues from estimates**: the Create issues page (was redmine_qbo_calendar's "Create issues from appointment estimates")
|
||||
* **Receive payments**: the Receive payment page, the payment page and its PDF
|
||||
* **View payments**: a customer's Payments list, the payment page and its PDF
|
||||
* **Receive payments**: the Receive payment page, plus everything View payments allows
|
||||
* **Write invoices**: create and edit invoices, and import time and estimates into them
|
||||
* **Email invoices**: email invoices to customers
|
||||
|
||||
|
||||
@@ -9,13 +9,27 @@
|
||||
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
# Receives customer payments in QuickBooks: payment type and amount, applied to open invoices oldest first, or
|
||||
# saved as a deposit. A saved payment can be printed (QuickBooks' payment PDF).
|
||||
# saved as a deposit. A saved payment can be printed (QuickBooks' payment PDF). A customer's payments are
|
||||
# listed on their payments page.
|
||||
class QboPaymentsController < ApplicationController
|
||||
|
||||
before_action :authorize_global
|
||||
|
||||
QBO_ERRORS = QboEstimatesController::QBO_ERRORS
|
||||
|
||||
# The customer's payments, newest first, read from QuickBooks
|
||||
def index
|
||||
@customer = Customer.find_by(id: params[:customer_id])
|
||||
return render_404 unless @customer
|
||||
|
||||
@payments = RedmineQboBilling::PaymentWriter.customer_payments(@customer)
|
||||
@totals = RedmineQboBilling::PaymentWriter.payment_totals(@payments)
|
||||
rescue *QBO_ERRORS => e
|
||||
@payments = []
|
||||
@totals = RedmineQboBilling::PaymentWriter.payment_totals([])
|
||||
flash.now[:error] = qbo_message(e)
|
||||
end
|
||||
|
||||
def new
|
||||
customer = Customer.find_by(id: params[:customer_id]) if params[:customer_id].present?
|
||||
@form = RedmineQboBilling::PaymentWriter.new_form(customer: customer, user: User.current)
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
<% if RedmineQboBilling.invoice_writer? %>
|
||||
<%= render partial: 'qbo_invoices/customer_link', locals: { customer: @customer } %>
|
||||
<% end %>
|
||||
<% if RedmineQboBilling.payment_viewer? %>
|
||||
<%= link_to sprite_icon('list', l(:label_payments)), qbo_payments_path(customer_id: @customer.id), class: 'icon icon-list' %>
|
||||
<% end %>
|
||||
<% if RedmineQboBilling.payment_receiver? %>
|
||||
<%= link_to sprite_icon('add', l(:button_receive_payment)), new_qbo_payment_path(customer_id: @customer.id), class: 'icon icon-add' %>
|
||||
<% else %>
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<% html_title @customer.name, l(:label_payments) %>
|
||||
|
||||
<div class="contextual">
|
||||
<% if RedmineQboBilling.payment_receiver? %>
|
||||
<%= link_to sprite_icon('add', l(:button_receive_payment)), new_qbo_payment_path(customer_id: @customer.id), class: 'icon icon-add' %>
|
||||
<% end %>
|
||||
<%= link_to sprite_icon('link', l(:button_open_in_quickbooks)),
|
||||
"https://#{RedmineQbo.sandbox_mode? ? 'sandbox' : 'app'}.qbo.intuit.com/app/customerdetail?nameId=#{@customer.id}",
|
||||
class: 'icon icon-link', target: :_blank, rel: 'noopener' %>
|
||||
</div>
|
||||
|
||||
<h2>
|
||||
<%= link_to l(:label_customers), customers_path %> »
|
||||
<%= link_to @customer.name, customer_path(@customer) %> »
|
||||
<%= l(:label_payments) %>
|
||||
</h2>
|
||||
|
||||
<% if @payments.empty? %>
|
||||
<p class="nodata"><%= l(:label_no_payments) %></p>
|
||||
<% else %>
|
||||
<div class="autoscroll">
|
||||
<table class="list qbo-payments">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><%= l(:label_payment_date) %></th>
|
||||
<th><%= l(:label_payment_method) %></th>
|
||||
<th><%= l(:label_payment_reference) %></th>
|
||||
<th class="applied"><%= l(:label_payment_applies_to) %></th>
|
||||
<th class="memo"><%= l(:label_memo) %></th>
|
||||
<th class="amount"><%= l(:label_amount) %></th>
|
||||
<th class="amount"><%= l(:label_payment_unapplied) %></th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<% @payments.each do |payment| %>
|
||||
<% applied = RedmineQboBilling::PaymentWriter.applied_invoices(payment) %>
|
||||
<% unapplied = BigDecimal(payment.unapplied_amount.to_s.presence || '0') %>
|
||||
<tr>
|
||||
<td class="date"><%= link_to format_date(payment.txn_date), qbo_payment_path(payment.id) %></td>
|
||||
<td><%= RedmineQboBilling::PaymentWriter.payment_method_name(payment) %></td>
|
||||
<td><%= payment.payment_ref_number %></td>
|
||||
<td class="applied">
|
||||
<% if applied.empty? %>
|
||||
<em><%= l(:label_payment_deposit) %></em>
|
||||
<% else %>
|
||||
<% applied.each do |invoice_id, number, amount| %>
|
||||
<div>
|
||||
<%= link_to "##{number}", RedmineQboBilling.invoice_writer? ? edit_qbo_invoice_path(invoice_id) : invoice_path(invoice_id) %>
|
||||
<%= number_to_currency(amount) %>
|
||||
</div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td class="memo"><%= truncate(payment.private_note.to_s, length: 60) %></td>
|
||||
<td class="amount"><%= number_to_currency(payment.total) %></td>
|
||||
<td class="amount"><%= unapplied > 0 ? number_to_currency(unapplied) : '' %></td>
|
||||
<td class="buttons">
|
||||
<%= link_to sprite_icon('application-pdf', l(:button_print_payment)), pdf_qbo_payment_path(payment.id),
|
||||
class: 'icon icon-pdf', target: :_blank %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</tbody>
|
||||
<tfoot>
|
||||
<tr class="total">
|
||||
<td colspan="5"><strong><%= l(:label_payment_totals) %></strong> (<%= @payments.size %>)</td>
|
||||
<td class="amount"><strong><%= number_to_currency(@totals[:received]) %></strong></td>
|
||||
<td class="amount"><strong><%= @totals[:unapplied] > 0 ? number_to_currency(@totals[:unapplied]) : '' %></strong></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</tfoot>
|
||||
</table>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<style>
|
||||
table.qbo-payments td { vertical-align: top; }
|
||||
table.qbo-payments .applied, table.qbo-payments .memo { text-align: left; }
|
||||
table.qbo-payments .amount { text-align: right; white-space: nowrap; }
|
||||
table.qbo-payments tfoot td { border-top: 2px solid #ccc; }
|
||||
</style>
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<div class="contextual">
|
||||
<%= link_to sprite_icon('application-pdf', l(:button_print_payment)), pdf_qbo_payment_path(@payment.id), class: 'icon icon-pdf', target: :_blank %>
|
||||
<% if @customer %>
|
||||
<% if @customer && RedmineQboBilling.payment_receiver? %>
|
||||
<%= link_to sprite_icon('add', l(:button_receive_payment)), new_qbo_payment_path(customer_id: @customer.id), class: 'icon icon-add' %>
|
||||
<% end %>
|
||||
<%= link_to sprite_icon('link', l(:button_open_in_quickbooks)),
|
||||
@@ -13,7 +13,8 @@
|
||||
|
||||
<h2>
|
||||
<%= link_to l(:label_customers), customers_path %> »
|
||||
<% if @customer %><%= link_to @customer.name, customer_path(@customer) %> »<% end %>
|
||||
<% if @customer %><%= link_to @customer.name, customer_path(@customer) %> »
|
||||
<%= link_to l(:label_payments), qbo_payments_path(customer_id: @customer.id) %> »<% end %>
|
||||
<%= l(:label_payment) %> <%= number_to_currency(@payment.total) %>
|
||||
</h2>
|
||||
|
||||
|
||||
@@ -308,6 +308,10 @@ en:
|
||||
label_payment_method: "Payment method"
|
||||
label_payment_reference: "Reference no."
|
||||
label_payment_unapplied: "Unapplied (credit)"
|
||||
label_payments: "Payments"
|
||||
label_no_payments: "No payments from this customer in QuickBooks"
|
||||
label_payment_totals: "Total"
|
||||
permission_view_payments: "View payments"
|
||||
notice_payment_not_found: "Payment not found in QuickBooks"
|
||||
notice_payment_saved: "Payment of %{amount} saved in QuickBooks"
|
||||
text_no_payment_methods: "QuickBooks' payment methods couldn't be read. Check the QuickBooks connection."
|
||||
|
||||
+2
-2
@@ -68,8 +68,8 @@ resources :qbo_invoices, only: [:new, :create, :edit, :update] do
|
||||
end
|
||||
end
|
||||
|
||||
# Receive payment pages
|
||||
resources :qbo_payments, only: [:new, :create, :show] do
|
||||
# Receive payment pages, and a customer's payments (/qbo_payments?customer_id=)
|
||||
resources :qbo_payments, only: [:index, :new, :create, :show] do
|
||||
member do
|
||||
get :pdf
|
||||
end
|
||||
|
||||
@@ -49,7 +49,8 @@ Redmine::Plugin.register :redmine_qbo do
|
||||
permission :write_invoices, { qbo_invoices: [:new, :create, :edit, :update, :import_options],
|
||||
qbo_estimates: [:customer_info, :items] }, global: true
|
||||
permission :send_invoices, { qbo_invoices: [:send_email] }, global: true
|
||||
permission :receive_payments, { qbo_payments: [:new, :create, :show, :pdf, :open_invoices],
|
||||
permission :view_payments, { qbo_payments: [:index, :show, :pdf] }, global: true
|
||||
permission :receive_payments, { qbo_payments: [:index, :new, :create, :show, :pdf, :open_invoices],
|
||||
qbo_estimates: [:customer_info] }, global: true
|
||||
# The product/service list, accounts and their syncs are admin-only (see ItemsController, AccountsController)
|
||||
|
||||
|
||||
@@ -43,6 +43,11 @@ module RedmineQboBilling
|
||||
user.allowed_to?(:receive_payments, nil, global: true)
|
||||
end
|
||||
|
||||
# True when the user may see customer payments (receiving them includes seeing them)
|
||||
def self.payment_viewer?(user = User.current)
|
||||
user.allowed_to?(:view_payments, nil, global: true) || payment_receiver?(user)
|
||||
end
|
||||
|
||||
# True when the user may write invoices
|
||||
def self.invoice_writer?(user = User.current)
|
||||
user.allowed_to?(:write_invoices, nil, global: true)
|
||||
|
||||
@@ -105,6 +105,20 @@ module RedmineQboBilling
|
||||
saved
|
||||
end
|
||||
|
||||
# Every payment the customer has made, newest first (by date, then QuickBooks id), read from QuickBooks
|
||||
def customer_payments(customer)
|
||||
payments = with_service('Payment') do |service|
|
||||
service.all("SELECT * FROM Payment WHERE CustomerRef = '#{customer.id.to_i}' ORDERBY TxnDate DESC")
|
||||
end
|
||||
Array(payments).sort_by { |p| [p.txn_date || Date.new(1900), p.id.to_i] }.reverse
|
||||
end
|
||||
|
||||
# Totals for the customer's payment list: amount received and what's still unapplied (credit)
|
||||
def payment_totals(payments)
|
||||
sum = ->(values) { values.sum(BigDecimal('0')) { |v| BigDecimal(v.to_s.presence || '0') } }
|
||||
{ received: sum.call(payments.map(&:total)), unapplied: sum.call(payments.map(&:unapplied_amount)) }
|
||||
end
|
||||
|
||||
def load(id)
|
||||
payment = with_service('Payment') { |service| service.fetch_by_id(id) }
|
||||
raise Error, I18n.t(:notice_payment_not_found) unless payment
|
||||
|
||||
Reference in new issue
Block a user