2022-05-27 09:30:48 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
from citext import CIText
|
2023-04-04 14:56:27 +00:00
|
|
|
from dateutil.tz import tzutc
|
2022-06-21 14:40:21 +00:00
|
|
|
from flask import g
|
2023-03-31 16:06:22 +00:00
|
|
|
from sortedcontainers import SortedSet
|
|
|
|
from sqlalchemy import BigInteger, Column, Integer
|
2022-05-27 09:30:48 +00:00
|
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
|
|
from sqlalchemy.orm import backref, relationship
|
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.resources.models import Thing
|
2022-05-27 14:32:54 +00:00
|
|
|
from ereuse_devicehub.resources.user.models import User
|
2023-03-21 11:08:13 +00:00
|
|
|
from ereuse_devicehub.teal.db import CASCADE_OWN, URL
|
2022-05-27 09:30:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Transfer(Thing):
|
|
|
|
"""
|
|
|
|
The transfer is a transfer of possession of devices between
|
|
|
|
a user and a code (not system user)
|
|
|
|
"""
|
|
|
|
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
|
|
|
|
code = Column(CIText(), default='', nullable=False)
|
|
|
|
date = Column(db.TIMESTAMP(timezone=True))
|
|
|
|
description = Column(CIText(), default='', nullable=True)
|
|
|
|
lot_id = db.Column(
|
|
|
|
UUID(as_uuid=True),
|
2022-06-03 11:13:50 +00:00
|
|
|
db.ForeignKey('lot.id', use_alter=True, name='lot_transfer'),
|
|
|
|
nullable=False,
|
2022-05-27 09:30:48 +00:00
|
|
|
)
|
|
|
|
lot = relationship(
|
|
|
|
'Lot',
|
|
|
|
backref=backref('transfer', lazy=True, uselist=False, cascade=CASCADE_OWN),
|
|
|
|
primaryjoin='Transfer.lot_id == Lot.id',
|
|
|
|
)
|
2022-05-27 14:32:54 +00:00
|
|
|
user_from_id = db.Column(UUID(as_uuid=True), db.ForeignKey(User.id), nullable=True)
|
|
|
|
user_from = db.relationship(User, primaryjoin=user_from_id == User.id)
|
|
|
|
user_to_id = db.Column(UUID(as_uuid=True), db.ForeignKey(User.id), nullable=True)
|
|
|
|
user_to = db.relationship(User, primaryjoin=user_to_id == User.id)
|
2022-05-30 14:32:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def closed(self):
|
|
|
|
if self.date:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2022-06-03 11:13:50 +00:00
|
|
|
|
2022-06-21 14:40:21 +00:00
|
|
|
def type_transfer(self):
|
|
|
|
if self.user_from == g.user:
|
|
|
|
return 'Outgoing'
|
|
|
|
|
|
|
|
if self.user_to == g.user:
|
|
|
|
return 'Incoming'
|
|
|
|
|
|
|
|
return 'Temporary'
|
|
|
|
|
2022-06-03 11:13:50 +00:00
|
|
|
|
2022-06-03 18:01:13 +00:00
|
|
|
class DeliveryNote(Thing):
|
2022-06-03 11:13:50 +00:00
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
|
|
|
|
number = Column(CIText(), default='', nullable=False)
|
|
|
|
date = Column(db.TIMESTAMP(timezone=True))
|
|
|
|
units = Column(Integer, default=0)
|
|
|
|
weight = Column(Integer, default=0)
|
|
|
|
|
|
|
|
transfer_id = db.Column(
|
|
|
|
UUID(as_uuid=True),
|
|
|
|
db.ForeignKey('transfer.id'),
|
|
|
|
nullable=False,
|
|
|
|
)
|
|
|
|
transfer = relationship(
|
|
|
|
'Transfer',
|
|
|
|
backref=backref('delivery_note', lazy=True, uselist=False, cascade=CASCADE_OWN),
|
2022-06-03 18:01:13 +00:00
|
|
|
primaryjoin='DeliveryNote.transfer_id == Transfer.id',
|
2022-06-03 11:13:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-06-03 18:01:13 +00:00
|
|
|
class ReceiverNote(Thing):
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
|
|
|
|
number = Column(CIText(), default='', nullable=False)
|
|
|
|
date = Column(db.TIMESTAMP(timezone=True))
|
|
|
|
units = Column(Integer, default=0)
|
|
|
|
weight = Column(Integer, default=0)
|
|
|
|
|
|
|
|
transfer_id = db.Column(
|
|
|
|
UUID(as_uuid=True),
|
|
|
|
db.ForeignKey('transfer.id'),
|
|
|
|
nullable=False,
|
|
|
|
)
|
2022-06-03 11:13:50 +00:00
|
|
|
transfer = relationship(
|
|
|
|
'Transfer',
|
|
|
|
backref=backref('receiver_note', lazy=True, uselist=False, cascade=CASCADE_OWN),
|
2022-06-03 18:01:13 +00:00
|
|
|
primaryjoin='ReceiverNote.transfer_id == Transfer.id',
|
2022-06-03 11:13:50 +00:00
|
|
|
)
|
2023-02-14 19:03:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TransferCustomerDetails(Thing):
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
|
|
|
|
company_name = Column(CIText(), nullable=True)
|
|
|
|
location = Column(CIText(), nullable=True)
|
|
|
|
logo = Column(URL(), nullable=True)
|
|
|
|
|
|
|
|
transfer_id = db.Column(
|
|
|
|
UUID(as_uuid=True),
|
|
|
|
db.ForeignKey('transfer.id'),
|
|
|
|
nullable=False,
|
|
|
|
)
|
|
|
|
transfer = relationship(
|
|
|
|
'Transfer',
|
|
|
|
backref=backref(
|
|
|
|
'customer_details', lazy=True, uselist=False, cascade=CASCADE_OWN
|
|
|
|
),
|
|
|
|
primaryjoin='TransferCustomerDetails.transfer_id == Transfer.id',
|
|
|
|
)
|
2023-03-31 16:06:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
_sorted_documents = {
|
|
|
|
'order_by': lambda: DeviceDocument.created,
|
|
|
|
'collection_class': SortedSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceDocument(Thing):
|
|
|
|
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4)
|
|
|
|
type = Column(db.CIText(), nullable=True)
|
|
|
|
date = Column(db.DateTime, nullable=True)
|
|
|
|
id_document = Column(db.CIText(), nullable=True)
|
|
|
|
description = Column(db.CIText(), nullable=True)
|
|
|
|
owner_id = db.Column(
|
|
|
|
UUID(as_uuid=True),
|
|
|
|
db.ForeignKey(User.id),
|
|
|
|
nullable=False,
|
|
|
|
default=lambda: g.user.id,
|
|
|
|
)
|
|
|
|
owner = db.relationship(User, primaryjoin=owner_id == User.id)
|
|
|
|
device_id = db.Column(BigInteger, db.ForeignKey('device.id'), nullable=False)
|
|
|
|
device = db.relationship(
|
|
|
|
'Device',
|
|
|
|
primaryjoin='DeviceDocument.device_id == Device.id',
|
|
|
|
backref=backref(
|
|
|
|
'documents', lazy=True, cascade=CASCADE_OWN, **_sorted_documents
|
|
|
|
),
|
|
|
|
)
|
|
|
|
file_name = Column(db.CIText(), nullable=True)
|
|
|
|
file_hash = Column(db.CIText(), nullable=True)
|
|
|
|
url = db.Column(URL(), nullable=True)
|
|
|
|
|
|
|
|
# __table_args__ = (
|
|
|
|
# db.Index('document_id', id, postgresql_using='hash'),
|
|
|
|
# db.Index('type_doc', type, postgresql_using='hash')
|
|
|
|
# )
|
2023-04-04 14:56:27 +00:00
|
|
|
|
|
|
|
def get_url(self) -> str:
|
|
|
|
if self.url:
|
|
|
|
return self.url.to_text()
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def __lt__(self, other):
|
|
|
|
return self.created.replace(tzinfo=tzutc()) < other.created.replace(
|
|
|
|
tzinfo=tzutc()
|
|
|
|
)
|