2022-12-20 19:37:37 -06:00
|
|
|
# -*- coding: utf-8; -*-
|
|
|
|
################################################################################
|
|
|
|
#
|
|
|
|
# Rattail -- Retail Software Framework
|
2024-04-16 18:23:49 -05:00
|
|
|
# Copyright © 2010-2024 Lance Edgar
|
2022-12-20 19:37:37 -06:00
|
|
|
#
|
|
|
|
# This file is part of Rattail.
|
|
|
|
#
|
|
|
|
# Rattail is free software: you can redistribute it and/or modify it under the
|
|
|
|
# terms of the GNU General Public License as published by the Free Software
|
|
|
|
# Foundation, either version 3 of the License, or (at your option) any later
|
|
|
|
# version.
|
|
|
|
#
|
|
|
|
# Rattail is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
# details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along with
|
|
|
|
# Rattail. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""
|
|
|
|
Vendor views, w/ Quickbooks integration
|
|
|
|
"""
|
|
|
|
|
2024-04-16 18:23:49 -05:00
|
|
|
import json
|
|
|
|
|
|
|
|
import colander
|
|
|
|
from deform import widget as dfwidget
|
|
|
|
from pyramid.renderers import render
|
|
|
|
from webhelpers2.html import HTML, tags
|
|
|
|
|
|
|
|
from tailbone import grids
|
2022-12-20 19:37:37 -06:00
|
|
|
from tailbone.views import ViewSupplement
|
|
|
|
|
|
|
|
|
|
|
|
class VendorViewSupplement(ViewSupplement):
|
|
|
|
"""
|
|
|
|
Vendor view supplement for Quickbooks integration
|
|
|
|
"""
|
|
|
|
route_prefix = 'vendors'
|
|
|
|
|
|
|
|
def get_grid_query(self, query):
|
|
|
|
model = self.model
|
|
|
|
return query.outerjoin(model.QuickbooksVendor)
|
|
|
|
|
|
|
|
def configure_grid(self, g):
|
|
|
|
model = self.model
|
|
|
|
g.set_filter('quickbooks_name', model.QuickbooksVendor.quickbooks_name)
|
|
|
|
g.set_filter('quickbooks_bank_account', model.QuickbooksVendor.quickbooks_bank_account)
|
|
|
|
g.set_filter('quickbooks_terms', model.QuickbooksVendor.quickbooks_terms)
|
|
|
|
|
|
|
|
def configure_form(self, f):
|
2024-04-16 18:23:49 -05:00
|
|
|
|
|
|
|
# quickbooks_name
|
2022-12-20 19:37:37 -06:00
|
|
|
f.append('quickbooks_name')
|
2024-04-16 18:23:49 -05:00
|
|
|
|
|
|
|
# quickbooks_bank_account
|
2022-12-20 19:37:37 -06:00
|
|
|
f.append('quickbooks_bank_account')
|
2024-04-16 18:23:49 -05:00
|
|
|
|
|
|
|
# quickbooks_bank_accounts
|
|
|
|
f.append('quickbooks_bank_accounts_')
|
|
|
|
f.set_renderer('quickbooks_bank_accounts_', self.render_quickbooks_bank_accounts)
|
|
|
|
f.set_node('quickbooks_bank_accounts_', BankAccounts())
|
2024-04-30 19:34:15 -05:00
|
|
|
f.set_widget('quickbooks_bank_accounts_', BankAccountsWidget(request=self.request))
|
2024-04-16 18:23:49 -05:00
|
|
|
|
|
|
|
# quickbooks_terms
|
2022-12-20 19:37:37 -06:00
|
|
|
f.append('quickbooks_terms')
|
|
|
|
|
2024-04-16 18:23:49 -05:00
|
|
|
def render_quickbooks_bank_accounts(self, vendor, field):
|
|
|
|
accounts = getattr(vendor, 'quickbooks_bank_accounts')
|
|
|
|
if accounts:
|
2024-04-30 19:34:15 -05:00
|
|
|
g = make_accounts_grid(self.request)
|
2024-04-16 18:23:49 -05:00
|
|
|
return HTML.literal(g.render_table_element(data_prop='quickbooksBankAccountsData'))
|
|
|
|
|
|
|
|
def objectify(self, vendor, form, data):
|
|
|
|
model = self.model
|
|
|
|
old_accounts = vendor.quickbooks_bank_accounts
|
|
|
|
new_accounts = data['quickbooks_bank_accounts_']
|
|
|
|
|
|
|
|
for new_account in new_accounts:
|
|
|
|
old_account = old_accounts.get(new_account['store_uuid'])
|
|
|
|
if old_account:
|
|
|
|
if old_account.account_number != new_account['account_number']:
|
|
|
|
old_account.account_number = new_account['account_number']
|
|
|
|
else:
|
|
|
|
account = model.QuickbooksVendorBankAccount()
|
|
|
|
account.store_uuid = new_account['store_uuid']
|
|
|
|
account.account_number = new_account['account_number']
|
|
|
|
vendor.quickbooks_bank_accounts[account.store_uuid] = account
|
|
|
|
self.Session.add(account)
|
|
|
|
|
|
|
|
final_store_uuids = set([a['store_uuid'] for a in new_accounts])
|
|
|
|
for old_account in list(vendor.quickbooks_bank_accounts.values()):
|
|
|
|
if old_account.store_uuid not in final_store_uuids:
|
|
|
|
self.Session.delete(old_account)
|
|
|
|
|
|
|
|
return vendor
|
|
|
|
|
|
|
|
def template_kwargs(self, **kwargs):
|
|
|
|
app = self.get_rattail_app()
|
|
|
|
form = kwargs.get('form')
|
|
|
|
if form:
|
|
|
|
|
|
|
|
# quickbooks bank accounts
|
|
|
|
vendor = kwargs['instance']
|
|
|
|
accounts = []
|
|
|
|
for account in vendor.quickbooks_bank_accounts.values():
|
|
|
|
store = account.store
|
|
|
|
accounts.append({
|
|
|
|
'uuid': account.uuid,
|
|
|
|
'store': f'{store.id} - {store.name}',
|
|
|
|
'store_uuid': store.uuid,
|
|
|
|
'store_id': store.id,
|
|
|
|
'store_name': store.name,
|
|
|
|
'account_number': account.account_number,
|
|
|
|
})
|
|
|
|
accounts.sort(key=lambda a: a['store_id'])
|
|
|
|
# nb. this is needed for widget *and* readonly template
|
|
|
|
form.set_json_data('quickbooksBankAccountsData', accounts)
|
|
|
|
# TODO: these are needed by widget
|
|
|
|
stores = []
|
|
|
|
for store in app.get_active_stores(self.Session()):
|
|
|
|
stores.append({
|
|
|
|
'uuid': store.uuid,
|
|
|
|
'display': f'{store.id} - {store.name}',
|
|
|
|
})
|
|
|
|
form.include_template('/vendors/quickbooks_bank_accounts_js.mako', {
|
|
|
|
'store_options': stores,
|
|
|
|
})
|
|
|
|
|
|
|
|
return kwargs
|
|
|
|
|
2022-12-20 19:37:37 -06:00
|
|
|
def get_version_child_classes(self):
|
|
|
|
model = self.model
|
|
|
|
return [model.QuickbooksVendor]
|
|
|
|
|
|
|
|
|
2024-04-30 19:34:15 -05:00
|
|
|
def make_accounts_grid(request):
|
2024-04-16 18:23:49 -05:00
|
|
|
g = grids.Grid('quickbooks_bank_accounts',
|
2024-04-30 19:34:15 -05:00
|
|
|
request=request,
|
|
|
|
data=[], # empty data
|
2024-04-16 18:23:49 -05:00
|
|
|
columns=[
|
|
|
|
'store',
|
|
|
|
'account_number',
|
|
|
|
])
|
|
|
|
return g
|
|
|
|
|
|
|
|
|
|
|
|
class BankAccount(colander.MappingSchema):
|
|
|
|
|
|
|
|
store_uuid = colander.SchemaNode(colander.String())
|
|
|
|
|
|
|
|
account_number = colander.SchemaNode(colander.String())
|
|
|
|
|
|
|
|
|
|
|
|
class BankAccounts(colander.SequenceSchema):
|
|
|
|
|
|
|
|
account = BankAccount()
|
|
|
|
|
|
|
|
|
|
|
|
class BankAccountsWidget(dfwidget.Widget):
|
|
|
|
|
|
|
|
def serialize(self, field, cstruct, **kw):
|
2024-04-30 19:34:15 -05:00
|
|
|
g = make_accounts_grid(self.request)
|
2024-04-16 18:23:49 -05:00
|
|
|
|
|
|
|
g.main_actions.append(
|
|
|
|
grids.GridAction('edit', icon='edit',
|
|
|
|
click_handler='quickbooksBankAccountEdit(props.row)'))
|
|
|
|
|
|
|
|
g.main_actions.append(
|
|
|
|
grids.GridAction('delete', icon='trash',
|
|
|
|
click_handler='quickbooksBankAccountDelete(props.row)'))
|
|
|
|
|
|
|
|
widget = render('/vendors/quickbooks_bank_accounts_widget.mako', {
|
|
|
|
'grid': g,
|
|
|
|
})
|
|
|
|
|
|
|
|
return HTML.tag('div', c=[
|
|
|
|
HTML.literal(widget),
|
|
|
|
tags.hidden(field.name, **{':value': "quickbooksBankAccountsFinal"}),
|
|
|
|
])
|
|
|
|
|
|
|
|
def deserialize(self, field, pstruct):
|
|
|
|
return json.loads(pstruct)
|
|
|
|
|
|
|
|
|
2022-12-20 19:37:37 -06:00
|
|
|
def includeme(config):
|
|
|
|
VendorViewSupplement.defaults(config)
|