kohir.py 3.78 KB
from sqlalchemy import (
    Column,
    Integer,
    DateTime,
    UniqueConstraint,
    String,
    SmallInteger,
    Index,
    BigInteger,
    text,
    func
)
from datetime import datetime
from dateutil.relativedelta import relativedelta
from opensipkd.pjdl.models.spt_invoice import PjdlInvoice
from . import DBSession
from . import PAD_TABLE_ARGS, DefaultModel, Base


class PjdlKohir(Base, DefaultModel):
    __tablename__ = 'pad_kohir'
    __table_args__ = (
        UniqueConstraint('tahun', 'kohirno', 'usaha_id'),
        Index('pad_kohir_tahun_usaha_id_kohirno_idx',
              'tahun', 'usaha_id', 'kohirno'),
        PAD_TABLE_ARGS
    )

    id = Column(BigInteger, primary_key=True)
    tahun = Column(Integer, nullable=False, index=True)
    usaha_id = Column(Integer)
    kohirno = Column(Integer, nullable=False, index=True)
    kohirtgl = Column(DateTime, nullable=False, index=True)
    sptpd_id = Column(BigInteger, nullable=False, index=True)
    spt_id = Column(BigInteger, nullable=False, index=True)
    enabled = Column(SmallInteger)
    create_date = Column(DateTime)
    create_uid = Column(Integer)
    write_date = Column(DateTime)
    write_uid = Column(Integer)
    is_cancel = Column(Integer, server_default=text("0"))
    bap_no = Column(String)
    bap_date = Column(DateTime)
    cancel_no = Column(BigInteger)
    cancel_date = Column(DateTime)
    cancel_thn = Column(Integer)
    cancel_uid = Column(Integer)

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

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

    @classmethod
    def query_spt_id(cls, spt_id=None):
        q = DBSession.query(cls).\
            filter_by(spt_id=spt_id)
        return q

    @classmethod
    def get_spt_kohirno(self):
        kohirno = func.get_spt_kohirno(self.spt_id)
        return kohirno

    @classmethod
    def get_spt_kohirno2(self):
        kohirno = func.get_spt_kohirno(self.id)
        return kohirno

    @classmethod
    def proses(cls, row, user):
        now = datetime.now()
        kohir = PjdlKohir.query().filter(PjdlKohir.spt_id == row.id).first()
        if kohir:
            return ("error", "Data sudah ditetapkan")

        if row.sptno < 0:
            row.sptno = PjdlInvoice.get_spt_no(row)
            no_urut = str(row.sptno).zfill(5)
            no_urut6 = str(row.sptno).zfill(6)
            from opensipkd.pjdl.apps import MODULE_CLASS

            spt_no_tpl = MODULE_CLASS.spt_no_tpl

            row.spt_no = spt_no_tpl.format(kd_pemda=MODULE_CLASS.kd_pemda,
                                           tahun=row.tahun, no_urut=no_urut,
                                           no_urut6=no_urut6)
            row.terimatgl = now.date()
            row.jatuhtempotgl = now+relativedelta(days=row.spt_typ.max_day)
            row.status_pembayaran = 0
            DBSession.add(row)
        kohir = PjdlKohir()
        kohir.spt_id = row.id
        kohir.kohirtgl = now.date()
        kohir.tahun = now.year
        kohir.enabled = 1
        kohir.create_date = now
        kohir.create_uid = user.id
        kohir.kohirno = PjdlKohir.get_kohir_no(row=kohir)
        kohir.usaha_id = row.op.usaha_id
        DBSession.add(kohir)
        DBSession.flush()
        return ("success", "Data sudah ditetapkan")