Prefer account holder, shoppers over legacy Customers.people

but until all are migrated, support both
This commit is contained in:
Lance Edgar 2023-06-14 14:28:01 -05:00
parent eab3b75ae5
commit 961cf803f2
4 changed files with 62 additions and 23 deletions

View file

@ -4,16 +4,7 @@
<%def name="object_helpers()">
${parent.object_helpers()}
<% people = h.OrderedDict() %>
% if instance.person:
<% people[instance.person.uuid] = instance.person %>
% endif
% if instance.customer:
% for person in instance.customer.people:
<% people[person.uuid] = person %>
% endfor
% endif
${view_profiles_helper(people.values())}
${view_profiles_helper(show_profiles_people)}
</%def>
${parent.body()}

View file

@ -27,6 +27,7 @@ Customer Views
from collections import OrderedDict
import sqlalchemy as sa
from sqlalchemy import orm
import colander
from pyramid.httpexceptions import HTTPNotFound
@ -125,10 +126,18 @@ class CustomerView(MasterView):
'customers.expose_people',
default=True)
def query(self, session):
query = super().query(session)
model = self.model
return query.outerjoin(model.Person,
model.Person.uuid == model.Customer.account_holder_uuid)
def configure_grid(self, g):
super(CustomerView, self).configure_grid(g)
super().configure_grid(g)
app = self.get_rattail_app()
model = self.model
route_prefix = self.get_route_prefix()
legacy = app.get_clientele_handler().should_use_legacy_people()
# customer key
field = self.get_customer_key_field()
@ -162,15 +171,23 @@ class CustomerView(MasterView):
# email_preference
g.set_enum('email_preference', self.enum.EMAIL_PREFERENCE)
# account_holder_*_name
g.set_filter('account_holder_first_name', model.Person.first_name)
g.set_filter('account_holder_last_name', model.Person.last_name)
# person
g.set_renderer('person', self.grid_render_person)
if legacy:
LegacyPerson = orm.aliased(model.Person)
g.set_joiner('person', lambda q:
q.outerjoin(model.CustomerPerson,
sa.and_(
model.CustomerPerson.customer_uuid == model.Customer.uuid,
model.CustomerPerson.ordinal == 1))\
.outerjoin(model.Person))
.outerjoin(LegacyPerson))
g.set_sorter('person', LegacyPerson.display_name)
else:
g.set_sorter('person', model.Person.display_name)
g.set_renderer('person', self.grid_render_person)
# active_in_pos
if self.get_expose_active_in_pos():
@ -395,6 +412,10 @@ class CustomerView(MasterView):
if kwargs['show_profiles_helper']:
people = OrderedDict()
if customer.account_holder:
person = customer.account_holder
people.setdefault(person.uuid, person)
for shopper in customer.shoppers:
person = shopper.person
people.setdefault(person.uuid, person)
@ -434,6 +455,7 @@ class CustomerView(MasterView):
url = self.request.route_url('people.view', uuid=person.uuid)
return tags.link_to(text, url)
# TODO: remove if no longer used
def render_people(self, customer, field):
people = customer.people
if not people:

View file

@ -282,6 +282,25 @@ class MemberView(MasterView):
'withdrew',
)
def template_kwargs_view(self, **kwargs):
kwargs = super().template_kwargs_view(**kwargs)
member = kwargs['instance']
people = OrderedDict()
if member.person:
person = member.person
people.setdefault(person.uuid, person)
if member.customer:
customer = member.customer
if customer.account_holder:
person = customer.account_holder
people.setdefault(person.uuid, person)
for person in customer.people:
people.setdefault(person.uuid, person)
kwargs['show_profiles_people'] = list(people.values())
return kwargs
def render_default_email(self, member, field):
if member.emails:
return member.emails[0].address

View file

@ -371,12 +371,15 @@ class PersonView(MasterView):
return tags.link_to(text, url)
def render_customers(self, person, field):
customers = person._customers
app = self.get_rattail_app()
clientele = app.get_clientele_handler()
customers = clientele.get_customers_for_account_holder(person)
if not customers:
return ""
return
items = []
for customer in customers:
customer = customer.customer
text = str(customer)
if customer.number:
text = "(#{}) {}".format(customer.number, text)
@ -384,6 +387,7 @@ class PersonView(MasterView):
text = "({}) {}".format(customer.id, text)
url = self.request.route_url('customers.view', uuid=customer.uuid)
items.append(HTML.tag('li', c=[tags.link_to(text, url)]))
return HTML.tag('ul', c=items)
def render_members(self, person, field):
@ -595,6 +599,7 @@ class PersonView(MasterView):
app = self.get_rattail_app()
clientele = app.get_clientele_handler()
expose_shoppers = self.customers_should_expose_shoppers()
expose_people = self.customers_should_expose_people()
customers = clientele.get_customers_for_account_holder(person)
key = self.get_customer_key_field()
@ -609,8 +614,6 @@ class PersonView(MasterView):
'name': customer.name,
'view_url': self.request.route_url('customers.view',
uuid=customer.uuid),
'people': [self.get_context_person(p)
for p in customer.people],
'addresses': [self.get_context_address(a)
for a in customer.addresses],
'external_links': [],
@ -624,6 +627,10 @@ class PersonView(MasterView):
context['shoppers'] = [self.get_context_shopper(s)
for s in customer.shoppers]
if expose_people:
context['people'] = [self.get_context_person(p)
for p in customer.people]
for supp in self.iter_view_supplements():
if hasattr(supp, 'get_context_for_customer'):
context = supp.get_context_for_customer(customer, context)