stpd.py 2.28 KB
from sqlalchemy import (
    Column,
    Integer,
    DateTime,
    ForeignKey,
    UniqueConstraint,
    Float,
    func,
    Time)
from sqlalchemy.schema import FetchedValue
from sqlalchemy.orm import (relationship,    backref)

from datetime import datetime

from opensipkd.pjdl.models.spt_invoice import PjdlInvoice
from . import PAD_TABLE_ARGS, DefaultModel, Base
from . import (DBSession)
from opensipkd.tools import date_from_str, ymd


class PjdlStpd(Base, DefaultModel):
    __tablename__ = 'pad_stpd'
    __table_args__ = (
        UniqueConstraint('tahun', 'stpdno'),
        PAD_TABLE_ARGS
    )
    id = Column(Integer, primary_key=True)
    tahun = Column(Integer, nullable=False)
    stpdno = Column(Integer, nullable=False)
    stpdtgl = Column(DateTime, nullable=False)
    spt_id = Column(Integer, ForeignKey(PjdlInvoice.id), nullable=False, )
    jatuhtempotgl = Column(DateTime)
    printed = Column(Integer, nullable=False)
    stpdcount = Column(Integer, nullable=False)
    bunga = Column(Float)
    opsen_bunga = Column(Float)
    enabled = Column(Integer)
    create_date = Column(DateTime)
    create_uid = Column(Integer)
    write_date = Column(DateTime)
    write_uid = Column(Integer)

    invoice = relationship(
        'PjdlInvoice', primaryjoin='PjdlInvoice.id == PjdlStpd.spt_id', backref='stpd')

    @classmethod
    def get_join(cls):
        result = DBSession.query(cls.spt_id, func.max(cls.id).label('id'),
                                 func.max(cls.stpdno).label('stpdno'),
                                 func.max(cls.stpdcount).label('stpdcount'),
                                 func.max(cls.stpdtgl).label('stpdtgl'),
                                 func.max(cls.jatuhtempotgl).label(
                                     'jatuhtempotgl'),
                                 func.max(cls.bunga).label('bunga')).\
            group_by(cls.spt_id).subquery()
        return result

    @classmethod
    def generate_stpdno(cls, tahun=None):
        if not tahun:
            tahun = datetime.now().year
        result = DBSession.query(func.max(cls.stpdno)).\
            filter_by(tahun=tahun).scalar()
        result = result and result > 0 and result+1 or 1
        return result

    @classmethod
    def get_stpno(self, id=None):
        stpno = func.get_stpno(id)
        return stpno