This information is currently visible to the Customer in their account transactions page - I think the page just adds up the total credits and debits at each shop and displays it on this page
If the Shop manager could also see this amount easily it saves them pulling the Payments report anytime they are trying to work out the state of a customers account balance
(slight enhancement to ease our workaround account credits process :)
nb. concern? question check performance implications?
size S I'd say.
related to #5173 and https://github.com/openfoodfoundation/openfoodnetwork/pull/5031/files
I'll take a look into this!
In progress, I think I'm going to need to learn a bit more to figure this one out.
hello David! Not an easy one if you starting with OFN!
I think you should create a service under app/services called CustomerBalance that takes a customer and does something like this:
https://github.com/openfoodfoundation/openfoodnetwork/pull/5031/files#diff-d92a88617a8371ec293fbd1c06389eb8R14
class CustomerBalance
def initialize(customer)
@customer = customer
end
def call
payment_total - completed_order_total
end
private
def payment_total
payments.sum(&:amount)
end
# Lists all complete user payments including payments in incomplete or canceled orders
def payments
Spree::Payment.where(order_id: customer_orders, state: "completed")
end
def customer_orders
Spree::Order.where(distributor_id: @customer.enterprise, email: @customer.email)
end
def completed_order_total
completed_not_cancelled_orders.sum(&:total)
end
def completed_not_cancelled_orders
customer_orders.complete.not_state(:canceled)
end
NOTE: looking up orders by email (and not customer id or user id) includes orders completed before the user registered in OFN (guest orders).
This service will be useful at some point but for the customers page (this issue) you may need to load this data through an ActiveRecord query that loads this data for all customers at once, otherwise this service will generate a couple of queries per customer....
I hope this helps.
Thanks @luisramos0 , this is really detailed and helpful! You're right, not a good first issue, I forgot to look for the tag. I don't think I'm going to be able to progress it in the short term, so it might be best to unassign me in case someone else can pick it up.
Can I get some advise on the UI on how to represent the positive negative balance?
This is what we are doing in the orders list (credit owned shows a negative value):

I think we should do something similar but should I get it exactly the same using "credit owned" and "balance due" tags like for orders? And the negative value for credit owned to customer?
This is where I am at in terms of code right now:

On a related topic, in this issue I am just using the existing UserBalanceCalculator to get the customer balance but there seems to be some problems with it as we can see @lin-d-hop trying to fix it here: https://github.com/openfoodfoundation/openfoodnetwork/pull/5031
Shall I go into that as part of this issue?
I found this hard when we did the order page and still find it hard! I think include the 'credit' and 'balance' because otherwise it is just confusing.
I don't know about the calculator - if it is not working properly then probably (yes please) but aware of paper cut scope creep.
Ping @chezaorchard @emilyjeanrogers re. Customer credits
Agree re papercut scope creep...
Butttt..... The UserBalanceCalculator does have problems and having you look at this and your confidence that it is correct @luisramos0 would be a huge relief for me. I would love to have the confidence that this can be used widely through the platform.
Perhaps we can timebox it? If the investigations and fix take longer than 2-3 hours then it needs its own issue?
I would agree that our users will appreciate the 'Balance Due' and 'Credit Owed' text. Based on my knowledge of our users this will save a lot of support queries!
ok, thanks for you feedback :+1:
this is how it looks with the labels:

I am going to have a look at the calculation logic now.
ok, I gave a good look and improved the PR related to the calculation of the balance:
https://github.com/openfoodfoundation/openfoodnetwork/pull/5031#issuecomment-710240422
Anyway, this issue here and PR (add balance to customers pages) are related to it but 100% independent. We can move this issue and PR forward and let the balance calculation PR #5031 follow it's way independently (it needs product feedback that can take a while).
Clarifying the relationship in terms of calculations: for now this PR will show a balance in the customers page that does not include payments of cancelled orders (one of the problems #5031 tries to solve). But when #5031 is merged, this customers page will pick up the improvement and show the good balances :+1:
Sorry I cam into this late but just want to note, from a design on papercuts pov what is here:

is good for a first version within papercuts territory but in general, theres a lot of 'UI mixed messages' between this column and the transactions section described in #5031 e.g.
These can form some of the design thoughts for non-papercut improvements to these sections I'll add in our design notion section until we have defined the inception/design pipeline process for these
that's awesome feedback @Erioldoesdesign i am looking forward improving those things!
regarding #5031 maybe useful to clarify that the page in there is for the customer, this one in this PR is for the manager. No user will see the two pages with the same data except for a manager buying in their own shop which is an edge case I think.