Commit 271ccc2c by Ooh-Ao

borrow

parent 4d1f4a91
...@@ -6,13 +6,6 @@ from uuid import uuid4 ...@@ -6,13 +6,6 @@ from uuid import uuid4
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from ..config.database import Base from ..config.database import Base
import uuid
from datetime import datetime
from sqlalchemy import Column, Integer, String, DateTime ,ForeignKey
from sqlalchemy.dialects.postgresql import UUID
from ..config.database import Base
from sqlalchemy.orm import relationship
class BorrowTransaction(Base): class BorrowTransaction(Base):
__tablename__ = "borrow_transactions" __tablename__ = "borrow_transactions"
...@@ -24,9 +17,15 @@ class BorrowTransaction(Base): ...@@ -24,9 +17,15 @@ class BorrowTransaction(Base):
returned_date = Column(DateTime, nullable=True) returned_date = Column(DateTime, nullable=True)
created_at = Column(DateTime, nullable=False, server_default=func.now()) created_at = Column(DateTime, nullable=False, server_default=func.now())
# ฟิลด์สำหรับการอนุมัติ # Approval fields
approved_by = Column(UUID(as_uuid=True), ForeignKey("member.memberId"), nullable=True) approved_by = Column(UUID(as_uuid=True), ForeignKey("member.memberId"), nullable=True)
approved_at = Column(DateTime, nullable=True) approved_at = Column(DateTime, nullable=True)
rejected_reason = Column(String, nullable=True) rejected_reason = Column(String, nullable=True)
project_equipment = relationship("ProjectEquipment", back_populates="borrow_transactions") # Relationships
\ No newline at end of file # Relationship สำหรับผู้เบิก
member = relationship("Member", foreign_keys=[memberId], back_populates="borrow_transactions", lazy="joined")
# Relationship สำหรับ ProjectEquipment
project_equipment = relationship("ProjectEquipment", back_populates="borrow_transactions", lazy="joined")
# Relationship สำหรับผู้อนุมัติ
approved_by_member = relationship("Member", foreign_keys=[approved_by], back_populates="borrow_transactions", lazy="joined")
...@@ -35,6 +35,9 @@ class Member(Base): ...@@ -35,6 +35,9 @@ class Member(Base):
# ProjectEmployees = relationship("ProjectEmployee", back_populates="member") # ProjectEmployees = relationship("ProjectEmployee", back_populates="member")
project_member = relationship("ProjectMember", back_populates="member") project_member = relationship("ProjectMember", back_populates="member")
borrow_transactions = relationship("BorrowTransaction", foreign_keys="[BorrowTransaction.memberId]", back_populates="member")
# Relationship สำหรับ BorrowTransaction ที่ผู้อนุมัติ
approved_borrow_transactions = relationship("BorrowTransaction", foreign_keys="[BorrowTransaction.approved_by]", back_populates="approved_by_member")
# ฟังก์ชันเพื่อแฮชรหัสผ่านก่อนบันทึก # ฟังก์ชันเพื่อแฮชรหัสผ่านก่อนบันทึก
def hash_password(self, password): def hash_password(self, password):
self.passwordHash = bcrypt.hash(password) self.passwordHash = bcrypt.hash(password)
......
# myproject/schemas/borrow_schema.py
from pydantic import BaseModel from pydantic import BaseModel
from uuid import UUID from uuid import UUID
from typing import Optional from typing import Optional
from datetime import datetime from datetime import datetime
from .member_schema import MemberResponse # Schema สำหรับ Member
from .project_equipment_schema import ProjectEquipmentResponse # Schema สำหรับ ProjectEquipment
class BorrowTransactionBase(BaseModel): class BorrowTransactionBase(BaseModel):
peId: Optional[UUID] = None peId: Optional[UUID] = None
quantity_borrowed: int quantity_borrowed: int
...@@ -20,6 +24,12 @@ class BorrowTransactionResponse(BorrowTransactionBase): ...@@ -20,6 +24,12 @@ class BorrowTransactionResponse(BorrowTransactionBase):
created_at: datetime created_at: datetime
peId: UUID peId: UUID
memberId: UUID memberId: UUID
# แสดงข้อมูลของสมาชิกที่เบิกอุปกรณ์ (จาก memberId)
member: Optional[MemberResponse] = None
# แสดงข้อมูลของ project_equipment ที่ผูกกับ peId ซึ่งจะมีข้อมูลอุปกรณ์ (ผ่าน relationship ใน ORM)
project_equipment: Optional[ProjectEquipmentResponse] = None
# แสดงข้อมูลของสมาชิกที่อนุมัติ (จาก approved_by)
approved_by_member: Optional[MemberResponse] = None
class Config: class Config:
orm_mode = True orm_mode = True
\ No newline at end of file
...@@ -289,12 +289,12 @@ ...@@ -289,12 +289,12 @@
<td> <td>
<div class="flex items-center"> <div class="flex items-center">
<div class="me-2"> <div class="me-2">
<!-- <span class="avatar avatar-md avatar-rounded"> <span class="avatar avatar-md avatar-rounded">
<img src={{product.img}} alt=""> <img src={{product.member.getPicture()}} alt="">
</span> --> </span>
</div> </div>
<div class="font-semibold"> <div class="font-semibold">
{{product.memberId}} {{product.member.getFullname()}}
</div> </div>
</div> </div>
</td> </td>
...@@ -302,7 +302,7 @@ ...@@ -302,7 +302,7 @@
<td>{{product.created_at}}</td> <td>{{product.created_at}}</td>
<td>{{product.returned_date}}</td> <td>{{product.returned_date}}</td>
<td> {{product.status}}</td> <td> {{product.status}}</td>
<td> {{product.approved_by}}</td> <td> {{product.approved_by_member.getFullname()}}</td>
<!-- <td> <!-- <td>
<span class="badge bg-light text-default"> {{product.category}}</span> <span class="badge bg-light text-default"> {{product.category}}</span>
</td> </td>
......
import { TranslateService } from "@ngx-translate/core"; import { TranslateService } from "@ngx-translate/core";
import { BaseModel } from "./base.model"; import { BaseModel } from "./base.model";
import { UserProfileModel } from "./user.model";
import { EquipmentModel } from "./equipments.model";
export class BorrowTransactionsModel extends BaseModel { export class BorrowTransactionsModel extends BaseModel {
peId: string;
quantity_borrowed: number; quantity_borrowed: number;
status: string; status: string;
returned_date: string; returned_date: string;
borrowId: string;
created_at: string;
peId: string;
memberId: string; memberId: string;
approved_by: string; approved_by: string;
borrowId: string;
created_at: string;
member: UserProfileModel;
project_equipment: EquipmentModel;
approved_by_member: UserProfileModel;
constructor(data?: Partial<BorrowTransactionsModel>, translateService?: TranslateService) { constructor(data?: Partial<BorrowTransactionsModel>, translateService?: TranslateService) {
super(data, translateService); super(data, translateService);
...@@ -20,5 +25,8 @@ export class BorrowTransactionsModel extends BaseModel { ...@@ -20,5 +25,8 @@ export class BorrowTransactionsModel extends BaseModel {
this.returned_date = data?.returned_date ?? '' this.returned_date = data?.returned_date ?? ''
this.memberId = data?.memberId ?? '' this.memberId = data?.memberId ?? ''
this.approved_by = data?.approved_by ?? '' this.approved_by = data?.approved_by ?? ''
this.member = data?.member ? new UserProfileModel(data.member) : new UserProfileModel();
this.approved_by_member = data?.approved_by_member ? new UserProfileModel(data.approved_by_member) : new UserProfileModel();
this.project_equipment = data?.project_equipment ? new EquipmentModel(data.project_equipment) : new EquipmentModel();
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment