55 lines
2.3 KiB
Python
55 lines
2.3 KiB
Python
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
|
|
|
|
|
|
|
|
|
|
|