Refactor: Flush opening vouchers to database before cleaning of lint.

This removes the need to keep of list of accounts/batches to not be deleted.
Do not delete employees with attendance and fingerprints.
This commit is contained in:
Amritanshu 2018-11-26 08:54:39 +05:30
parent cfdd49f96f
commit bdb8bc7005

View File

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