import datetime from uuid import UUID from app.config import TaskEnv from app.models.bill_delay import BillDelayModel from app.models.project import ProjectModel from core.repositories.base import BaseRepository from core.service.mongo_db import use_collection class BillDelayRepository(BaseRepository): def __init__(self): super().__init__(collection=TaskEnv.CLN_BILL_DELAY, model=BillDelayModel) def create_sum(self, coding: BillDelayModel) -> BillDelayModel: all_total = 0 if coding.financial: total = 0 if coding.financial.status: total += coding.financial.status if coding.financial.prepayment: total += coding.financial.prepayment if coding.financial.modification: total += coding.financial.modification if coding.financial.good_job: total += coding.financial.good_job coding.financial.total = total all_total += total if coding.force_major: total = 0 if coding.force_major.force_event: total += coding.force_major.force_event if coding.force_major.material: total += coding.force_major.material if coding.force_major.rules: total += coding.force_major.rules if coding.force_major.define_indicators: total += coding.force_major.define_indicators coding.force_major.total = total all_total += total if coding.technical: total = 0 if coding.technical.map_technical: total += coding.technical.map_technical if coding.technical.permit: total += coding.technical.permit if coding.technical.purchase: total += coding.technical.purchase if coding.technical.suspense: total += coding.technical.suspense coding.technical.total = total all_total += total if coding.extra_new_work: all_total += coding.extra_new_work if coding.other: all_total += coding.other if coding.illegal: all_total += int(coding.illegal) if coding.overlapping: all_total -= coding.overlapping if all_total: coding.total_day = all_total return coding async def create(self, coding: BillDelayModel): coding = self.create_sum(coding) delay = await super().create(coding) project = await use_collection(TaskEnv.CLN_PROJECT).find_one({"_id": coding.project_id}) project_model = ProjectModel(**project) if 'date_delay' in project: date_delay = project_model.date_delay + datetime.timedelta(days=coding.total_day) await use_collection(TaskEnv.CLN_PROJECT).update_one({"_id": coding.project_id}, {"$inc": {"all_delay_day":coding.total_day},"$set":{"date_delay":date_delay}}) else: await use_collection(TaskEnv.CLN_PROJECT).update_one({"_id": coding.project_id}, {"$inc": {"all_delay_day":coding.total_day}}) return delay async def update(self, _id: UUID, update: BillDelayModel): old = await self.get(_id) old_model = BillDelayModel(**old) update = self.create_sum(update) project = await use_collection(TaskEnv.CLN_PROJECT).find_one({"_id": update.project_id}) project_model = ProjectModel(**project) if old_model.total_day != update.total_day: diff = update.total_day - old_model.total_day date_delay = project_model.date_delay + datetime.timedelta(days=diff) await use_collection(TaskEnv.CLN_PROJECT).update_one({"_id": update.project_id}, {"$inc": {"all_delay_day": diff},"$set":{"date_delay":date_delay}}) return await super().update(_id, update)