diff --git a/brewman/brewman/models/master.py b/brewman/brewman/models/master.py index e23053fd..fee318c6 100644 --- a/brewman/brewman/models/master.py +++ b/brewman/brewman/models/master.py @@ -26,7 +26,7 @@ class Product(Base): batches = relationship('Batch', backref='product') inventories = relationship('Inventory', backref='product') - ledger = relationship('Ledger', primaryjoin="Ledger.id==Product.ledger_id") + ledger = relationship('Ledger', primaryjoin="Ledger.id==Product.ledger_id", backref='products') def __init__(self, code=None, name=None, units=None, fraction=None, fraction_units=None, yeild=None, show_for_purchase=None, product_group_id=None, ledger_id=None, price=None, discontinued=None): @@ -205,6 +205,15 @@ class LedgerBase(Base): DBSession.add(self) return self + def can_delete(self): + if self.is_active: + return False, 'Account is active' + if len(self.journals) > 0: + return False, 'Account has journal entries' + if len(self.products) > 0: + return False, 'Account has products' + return True + @classmethod def get_code(cls, type): code = DBSession.query(func.max(LedgerBase.code)).filter(LedgerBase.type == type).one()[0] diff --git a/brewman/brewman/static/offline.appcache b/brewman/brewman/static/offline.appcache index 10f73873..a077ae0e 100644 --- a/brewman/brewman/static/offline.appcache +++ b/brewman/brewman/static/offline.appcache @@ -1,6 +1,6 @@ CACHE MANIFEST -# version 2012-12-07.7 +# version 2012-12-07.8 CACHE: /partial/404.html diff --git a/brewman/brewman/static/partial/account-detail.html b/brewman/brewman/static/partial/account-detail.html index 2fe0698a..bc509f1d 100644 --- a/brewman/brewman/static/partial/account-detail.html +++ b/brewman/brewman/static/partial/account-detail.html @@ -48,5 +48,9 @@
+
diff --git a/brewman/brewman/views/account.py b/brewman/brewman/views/account.py index 0cdcc684..b1e0b78c 100644 --- a/brewman/brewman/views/account.py +++ b/brewman/brewman/views/account.py @@ -3,6 +3,7 @@ from pyramid.response import Response from pyramid.view import view_config import transaction +from brewman.models import DBSession from brewman.models.master import CostCenter, Ledger, LedgerType, LedgerBase from brewman.models.validation_exception import ValidationError @@ -16,7 +17,7 @@ def html(request): return {} -@view_config(request_method='POST', route_name='account', renderer='json', permission='Accounts') +@view_config(request_method='POST', route_name='api_account', renderer='json', permission='Accounts') def save(request): try: item = Ledger(code=0, name=request.json_body['Name'], type=int(request.json_body['Type']), @@ -31,7 +32,7 @@ def save(request): return response -@view_config(request_method='POST', route_name='account_id', renderer='json', permission='Accounts') +@view_config(request_method='POST', route_name='api_account_id', renderer='json', permission='Accounts') def update(request): try: item = Ledger.by_id(uuid.UUID(request.matchdict['id'])) @@ -52,15 +53,17 @@ def update(request): return response -@view_config(request_method='DELETE', route_name='account_id', renderer='json', permission='Accounts') +@view_config(request_method='DELETE', route_name='api_account_id', renderer='json', permission='Accounts') def delete(request): - id = request.matchdict.get('id', None) - if id is None: - response = Response("Account is Null") - response.status_int = 500 - return response + account = Ledger.by_id(uuid.UUID(request.matchdict['id'])) + can_delete, reason = account.can_delete() + if can_delete: + DBSession.delete(account) + transaction.commit() + return account_info(None) else: - response = Response("Account deletion not implemented") + transaction.abort() + response = Response("Cannot delete account because {0}".format(reason)) response.status_int = 500 return response diff --git a/brewman/brewman/views/employee.py b/brewman/brewman/views/employee.py index 764cafb5..04e764de 100644 --- a/brewman/brewman/views/employee.py +++ b/brewman/brewman/views/employee.py @@ -7,7 +7,6 @@ import transaction from brewman.models.master import CostCenter, Employee, LedgerBase from brewman.models.validation_exception import ValidationError -from brewman.views.account import ledger_list @view_config(route_name='employee_list', renderer='brewman:templates/angular_base.mako', permission='Authenticated') @view_config(request_method='GET', route_name='employee_id', renderer='brewman:templates/angular_base.mako',