diff --git a/app/repositories/financial_status.py b/app/repositories/financial_status.py index 771478e..f56fc63 100644 --- a/app/repositories/financial_status.py +++ b/app/repositories/financial_status.py @@ -3,10 +3,52 @@ import uuid from app.config import TaskEnv from app.models.financial_status import StatusFinancialModel from core.repositories.base import BaseRepository +from core.service.mongo_db import use_collection class FinancialStatusRepository(BaseRepository): def __init__(self): super().__init__(collection=TaskEnv.CLN_FINANCIAL_STATUS, model=StatusFinancialModel) + async def create(self, coding: StatusFinancialModel): + result = await super().create(coding) + price = await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).find_one({'project_id': coding.project_id}) + if price and coding.amount: + await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one( + {'project_id': coding.project_id}, + {"$inc": {'amount_status': coding.amount}} + ) + return result + + async def update(self, _id: uuid.UUID, _update: StatusFinancialModel): + _old_dic = await use_collection(TaskEnv.CLN_FINANCIAL_STATUS).find_one({'_id':_id}) + change = 0 + result = await super().update(_id, _update) + price = await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).find_one({'project_id': _update.project_id}) + if price: + _old = StatusFinancialModel(**_old_dic) + if 'amount' not in _old_dic and _update.amount: + change = _update.amount + if _old.amount != _update.amount: + change = _update.amount - _old.amount + if change > 0 : + await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one( + {'project_id': _update.project_id}, + {"$inc": {'amount_status': change}} + ) + return result + async def delete(self, _id: uuid.UUID): + _old_dic = await use_collection(TaskEnv.CLN_FINANCIAL_STATUS).find_one({'_id': _id}) + await super().delete(_id) + price = await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).find_one({'project_id': _old_dic['project_id']}) + if price and 'amount_status' in price: + await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one( + {'project_id': _old_dic['project_id']}, + {"$inc": {'amount_status': -(_old_dic['amount'])}} + ) + return True + + + + diff --git a/app/repositories/project.py b/app/repositories/project.py index 72c7181..adf3aee 100644 --- a/app/repositories/project.py +++ b/app/repositories/project.py @@ -47,7 +47,7 @@ class ProjectRepository(BaseRepository[ProjectModel]): } await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).insert_one(price) else: - await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one({'project_id': update.id}, {'first_amount': update.monitor.detail.first_amount}) + await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).update_one({'project_id': update.id}, {'$set':{'first_amount': update.monitor.detail.first_amount}}) return True async def create(self,coding:ProjectModel):