from app import db from datetime import datetime from sqlalchemy import event from app.utils.regex_utils import RegularExpression from decimal import Decimal class ManholeDomesticChamber(db.Model): __tablename__ = "manhole_domestic_chamber" id = db.Column(db.Integer, primary_key=True) # Foreign Key to Subcontractor table subcontractor_id = db.Column(db.Integer, db.ForeignKey("subcontractors.id"), nullable=False) # Relationship for easy access (subcontractor.subcontractor_name) subcontractor = db.relationship("Subcontractor", backref="manhole_domestic_chamber_records") # Basic Fields RA_Bill_No=db.Column(db.String(500)) Location = db.Column(db.String(500)) MH_NO = db.Column(db.String(100)) Depth_of_MH = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) # Excavation categories d_0_to_0_75 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_0_76_to_1_05 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_1_06_to_1_65 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_1_66_to_2_15 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_2_16_to_2_65 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_2_66_to_3_15 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_3_16_to_3_65= db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_3_66_to_4_15 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_4_16_to_4_65 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_4_66_to_5_15 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_5_16_to_5_65 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_5_66_to_6_15 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_6_16_to_6_65 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_6_66_to_7_15 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_7_16_to_7_65 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_7_66_to_8_15 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_8_16_to_8_65 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_8_66_to_9_15 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) d_9_16_to_9_65 = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) Domestic_Chambers = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) DWC_Pipe_Length = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) UPVC_Pipe_Length = db.Column(db.Numeric(10, 2), default=Decimal("0.00")) Total = db.Column(db.Numeric(12, 2), default=Decimal("0.00")) Remarks = db.Column(db.String(500), default="Import File") created_at = db.Column(db.DateTime,default=datetime.now,nullable=False) def __repr__(self): return f"" def serialize(self): return {c.name: getattr(self, c.name) for c in self.__table__.columns} # AUTO TOTAL USING REGEX def calculate_mh_dc_total(mapper, connection, target): total = Decimal("0.00") for column in target.__table__.columns: if RegularExpression.D_RANGE_PATTERN.match(column.name): value = getattr(target, column.name) if value is not None: total += Decimal(value) target.Total = total.quantize(Decimal("0.01")) event.listen(ManholeDomesticChamber, "before_insert", calculate_mh_dc_total) event.listen(ManholeDomesticChamber, "before_update", calculate_mh_dc_total)