diff --git a/brewman/views/Management/rebase.py b/brewman/views/Management/rebase.py index 91c3e874..9e542bc6 100644 --- a/brewman/views/Management/rebase.py +++ b/brewman/views/Management/rebase.py @@ -18,8 +18,8 @@ def rebase(request): date = request.matchdict.get('date', None) date = datetime.datetime.strptime(date, '%d-%b-%Y') user_id = uuid.UUID(request.authenticated_userid) - voucher_l, accounts = opening_accounts(date, user_id, request.dbsession) - voucher_b, batches = opening_batches(date, user_id, request.dbsession) + voucher_l = opening_accounts(date, user_id, request.dbsession) + voucher_b = opening_batches(date, user_id, request.dbsession) starred_vouchers = save_starred(date, request.dbsession) request.dbsession.flush() delete_data(date, starred_vouchers, request.dbsession) @@ -31,7 +31,8 @@ def rebase(request): request.dbsession.add(j) for i in voucher_b.inventories: request.dbsession.add(i) - cleanup_lint(date, accounts, batches, request.dbsession) + request.dbsession.flush() + cleanup_lint(date, request.dbsession) # request.dbsession.execute('RESET statement_timeout;') transaction.commit() return {} @@ -78,7 +79,6 @@ def save_starred(date, dbsession): def opening_accounts(date, user_id, dbsession): running_total = 0 - accounts = [] sum_func = func.sum(Journal.signed_amount) query = dbsession.query( AccountBase, sum_func @@ -112,17 +112,15 @@ def opening_accounts(date, user_id, dbsession): journal = Journal(amount=abs(amount), debit=-1 if amount < 0 else 1, account_id=account.id, cost_centre_id=account.cost_centre_id) voucher.journals.append(journal) - accounts.append(account.id) if running_total != 0: journal = Journal(amount=abs(amount), debit=-1 if amount * -1 < 0 else 1, account_id=AccountBase.suspense(), cost_centre_id=CostCentre.cost_centre_overall()) voucher.journals.append(journal) - return voucher, accounts + return voucher def opening_batches(date, user_id, dbsession): total = 0 - batches = [] sum_func = func.sum(Journal.debit * Inventory.quantity) query = dbsession.query( Batch, sum_func @@ -156,7 +154,6 @@ def opening_batches(date, user_id, dbsession): inventory = Inventory(product_id=batch.product_id, batch=batch, quantity=quantity, rate=batch.rate, tax=batch.tax, discount=batch.discount) voucher.inventories.append(inventory) - batches.append(batch.id) voucher.journals.append(Journal( amount=abs(total), debit=-1, @@ -169,7 +166,7 @@ def opening_batches(date, user_id, dbsession): account_id=AccountBase.all_purchases(), cost_centre_id=CostCentre.cost_centre_purchase() )) - return voucher, batches + return voucher def delete_data(date, vouchers, dbsession): @@ -215,14 +212,11 @@ def delete_data(date, vouchers, dbsession): ) -def cleanup_lint(date, accounts, batches, dbsession): +def cleanup_lint(date, dbsession): # Insert executes on the end so keep list of batches and journals dbsession.execute( Batch.__table__.delete( - and_( - ~Batch.id.in_(dbsession.query(distinct(Inventory.batch_id)).subquery()), - ~Batch.id.in_(batches) - ) + ~Batch.id.in_(dbsession.query(distinct(Inventory.batch_id)).subquery()) ) ) dbsession.execute( @@ -235,7 +229,8 @@ def cleanup_lint(date, accounts, batches, dbsession): Employee.__table__.delete( and_( ~Employee.id.in_(dbsession.query(distinct(Journal.account_id)).subquery()), - ~Employee.id.in_(accounts), + ~Employee.id.in_(dbsession.query(distinct(Fingerprint.employee_id)).subquery()), + ~Employee.id.in_(dbsession.query(distinct(Attendance.employee_id)).subquery()), Employee.id.in_( dbsession.query( AccountBase.id @@ -262,7 +257,6 @@ def cleanup_lint(date, accounts, batches, dbsession): Account.__table__.delete( and_( ~Account.id.in_(dbsession.query(distinct(Journal.account_id)).subquery()), - ~Account.id.in_(accounts), Account.is_fixture == False, Account.is_starred == False, Account.account_type == Account.__mapper_args__['polymorphic_identity']