| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- """WorkRecord model for Work Statistics System."""
- from datetime import datetime, timezone, date
- from app import db
- class WorkRecord(db.Model):
- """WorkRecord model representing a work entry for a person on a specific date.
-
- Attributes:
- id: Primary key, auto-incremented
- person_id: Foreign key to Person
- item_id: Foreign key to Item
- work_date: Date of the work
- quantity: Number of items produced (positive integer)
- created_at: Timestamp when the record was created
- updated_at: Timestamp when the record was last updated
- """
- __tablename__ = 'work_records'
-
- id = db.Column(db.Integer, primary_key=True, autoincrement=True)
- person_id = db.Column(db.Integer, db.ForeignKey('persons.id'), nullable=False)
- item_id = db.Column(db.Integer, db.ForeignKey('items.id'), nullable=False)
- work_date = db.Column(db.Date, nullable=False)
- quantity = db.Column(db.Integer, nullable=False)
- created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
- updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
-
- # Relationships
- person = db.relationship('Person', backref=db.backref('work_records', lazy='dynamic'))
- item = db.relationship('Item', backref=db.backref('work_records', lazy='dynamic'))
-
- @property
- def total_price(self):
- """Calculate total price as unit_price * quantity.
-
- Returns:
- Float representing the total price
- """
- if self.item:
- return self.item.unit_price * self.quantity
- return 0.0
-
- def to_dict(self):
- """Convert model to dictionary for JSON serialization.
-
- Returns:
- Dictionary representation of the work record
- """
- return {
- 'id': self.id,
- 'person_id': self.person_id,
- 'person_name': self.person.name if self.person else None,
- 'item_id': self.item_id,
- 'item_name': self.item.name if self.item else None,
- 'unit_price': self.item.unit_price if self.item else None,
- 'work_date': self.work_date.isoformat() if self.work_date else None,
- 'quantity': self.quantity,
- 'total_price': self.total_price,
- 'created_at': self.created_at.isoformat() if self.created_at else None,
- 'updated_at': self.updated_at.isoformat() if self.updated_at else None
- }
-
- def __repr__(self):
- return f'<WorkRecord {self.id}: Person {self.person_id}, Item {self.item_id}, Qty {self.quantity}>'
|