spt_tegur.py 2.91 KB
from datetime import datetime
from sqlalchemy import (Column, Integer, DateTime, ForeignKey,
                        UniqueConstraint, BigInteger, func)
from sqlalchemy.orm import (relationship)
from . import PAD_TABLE_ARGS, DefaultModel, Base
from . import (PjdlWp, PjdlOp, PjdlPajak,)


class PjdlSptTegur(Base, DefaultModel):
    __tablename__ = 'pad_spt_tegur'
    __table_args__ = (
        UniqueConstraint('tahun', 'tegurno'),
        PAD_TABLE_ARGS
    )
    id = Column(BigInteger, primary_key=True, index=True)
    create_date = Column(DateTime)
    create_uid = Column(Integer)
    write_date = Column(DateTime)
    write_uid = Column(Integer)
    tahun = Column(Integer, nullable=False, index=True)
    tegurno = Column(Integer, nullable=False, index=True)
    tegurtgl = Column(DateTime, nullable=False, index=True)
    masa_bln = Column(Integer, nullable=False, index=True)
    masa_thn = Column(Integer, nullable=False, index=True)
    masa_dari = Column(DateTime, nullable=False, index=True)
    masa_sd = Column(DateTime, nullable=False, index=True)
    jatuhtempotgl = Column(DateTime, nullable=False, index=True)
    ke = Column(Integer, nullable=False, index=True)
    customer_id = Column(ForeignKey(
        PjdlWp.id, onupdate='CASCADE'), nullable=False, index=True)
    customer_usaha_id = Column(
        ForeignKey(PjdlOp.id, ondelete='CASCADE', onupdate='CASCADE'),
        nullable=False, index=True)
    pajak_id = Column(ForeignKey(PjdlPajak.id, onupdate='CASCADE'),
                      nullable=False)
    wp = relationship('PjdlWp',  backref='pad_spt_tegur')
    op = relationship('PjdlOp',  backref='pad_spt_tegur')
    pajak = relationship('PjdlPajak', backref='pad_spt_tegur')

    @classmethod
    def get_tegurno(cls, tahun=None):
        if not tahun:
            tahun = datetime.now().year
        no = cls.query_from([func.max(cls.tegurno)])\
            .filter_by(tahun=tahun).scalar()
        return no and no + 1 or 1
    

    @classmethod
    def get_ke(cls, value):
        customer_usaha_id = value["customer_usaha_id"]
        ke = cls.query_from([func.max(cls.ke)])\
            .filter_by(customer_usaha_id=customer_usaha_id,
                       masa_thn=value["masa_thn"],
                       masa_bln=value["masa_bln"],
                       ).scalar()
        
        ke = ke and ke + 1 or 1
        if ke>3:
            raise Exception("Teguran hanya 3 kali silahkan tetapkan dengan SKPD Jabatan")
        return ke
    
    @classmethod
    def get_prev(cls, value):
        customer_usaha_id = value["customer_usaha_id"]
        jth_tempo = cls.query_from([func.max(cls.jatuhtempotgl)])\
            .filter_by(customer_usaha_id=customer_usaha_id,
                       masa_thn=value["masa_thn"],
                       masa_bln=value["masa_bln"],
                       ).scalar()
        if jth_tempo and jth_tempo > value["tegurtgl"]:
            raise Exception("Teguran sebelumnya belum jatuh tempo")