sspd_payment.py 3.19 KB
from datetime import datetime
from sqlalchemy import (
    Column,    Integer,    DateTime,    ForeignKey,    UniqueConstraint,
    String,    SmallInteger,    BigInteger,    Float,    cast,    func,    Time)
from sqlalchemy.schema import FetchedValue
from sqlalchemy.orm import (relationship,    backref)

from . import PAD_TABLE_ARGS, DefaultModel, Base
from . import (PjdlInvoice, DBSession)


class PjdlPayment(Base, DefaultModel):
    __tablename__ = 'pad_sspd'
    __table_args__ = (
        UniqueConstraint('tahun', 'sspdno'),
        PAD_TABLE_ARGS
    )

    id = Column(BigInteger, primary_key=True, index=True)
    tahun = Column(Integer, nullable=False, index=True)
    sspdno = Column(Integer, nullable=False, index=True)
    sspdtgl = Column(DateTime, nullable=False, index=True)
    spt_id = Column(ForeignKey(PjdlInvoice.id), nullable=False, index=True)
    bunga = Column(Float(53), server_default=FetchedValue())
    bulan_telat = Column(Integer, server_default=FetchedValue())
    hitung_bunga = Column(SmallInteger, server_default=FetchedValue())
    printed = Column(SmallInteger, server_default=FetchedValue())
    enabled = Column(SmallInteger, server_default=FetchedValue())
    create_date = Column(DateTime)
    create_uid = Column(Integer)
    write_date = Column(DateTime)
    write_uid = Column(Integer)
    sspdjam = Column(Time)
    tp_id = Column(Integer, server_default=FetchedValue())
    is_validated = Column(Integer, server_default=FetchedValue())
    keterangan = Column(String(255))
    denda = Column(BigInteger, server_default=FetchedValue())
    jml_bayar = Column(BigInteger, server_default=FetchedValue())
    is_valid = Column(Integer, server_default=FetchedValue())
    cancel_bunga = Column(BigInteger)
    cancel_denda = Column(BigInteger)
    cancel_date = Column(DateTime)
    cancel_jml_bayar = Column(BigInteger)
    cancel_uid = Column(Integer)
    opsen_denda = Column(BigInteger)
    opsen_bayar = Column(BigInteger)
    opsen_bunga = Column(BigInteger)
    posted = Column(SmallInteger, nullable=False,
                    server_default=FetchedValue())
    posting_simda_penetapan = Column(
        SmallInteger, nullable=False, server_default=FetchedValue())
    posting_simda_non_penetapan = Column(
        SmallInteger, nullable=False, server_default=FetchedValue())

    spt = relationship(
        'PjdlInvoice', primaryjoin='PjdlPayment.spt_id == PjdlInvoice.id', backref='pad_sspds')

    @classmethod
    def get_no_bayar(cls):
        strid = cast(cls.id, String)
        strid = func.lpad(strid, 6, '0')

        hasil = func.concat(cls.tahun, '-')
        hasil = func.concat(hasil, strid)

        return hasil

    @classmethod
    def get_bayarno(cls):
        strTahun = cast(cls.tahun, String)
        strSspdno = cast(cls.sspdno, String)
        fixSspdno = func.lpad(strSspdno, 6, '0')

        result = strTahun + '-' + fixSspdno

        return result

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