feat(financial_status):add update project_price
This commit is contained in:
@@ -3,10 +3,52 @@ import uuid
|
|||||||
from app.config import TaskEnv
|
from app.config import TaskEnv
|
||||||
from app.models.financial_status import StatusFinancialModel
|
from app.models.financial_status import StatusFinancialModel
|
||||||
from core.repositories.base import BaseRepository
|
from core.repositories.base import BaseRepository
|
||||||
|
from core.service.mongo_db import use_collection
|
||||||
|
|
||||||
|
|
||||||
class FinancialStatusRepository(BaseRepository):
|
class FinancialStatusRepository(BaseRepository):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(collection=TaskEnv.CLN_FINANCIAL_STATUS, model=StatusFinancialModel)
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ class ProjectRepository(BaseRepository[ProjectModel]):
|
|||||||
}
|
}
|
||||||
await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).insert_one(price)
|
await use_collection(TaskEnv.CLN_PROJECT_PRICE_REPORT).insert_one(price)
|
||||||
else:
|
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
|
return True
|
||||||
|
|
||||||
async def create(self,coding:ProjectModel):
|
async def create(self,coding:ProjectModel):
|
||||||
|
|||||||
Reference in New Issue
Block a user