dph_rekap.py 6.59 KB
from datetime import datetime
import logging
from pyramid.httpexceptions import (HTTPFound, )
from pyramid.view import (view_config)
from ..models import PbbmDBSession
from ..models import Sppt, Kecamatan, Kelurahan, PembayaranSppt
from ..tools import BUKUS, bukus_sorted
from . import ColumnDT, DataTables, BaseView
from tangsel.base.models import UserArea, ResDesa, UserGroup, Group, User
from ..models import PembayaranDph
from tangsel.tools.pbb import FixDati2, nop_formatted, query_nop, FixKelurahan
from tangsel.models import DBSession
from sqlalchemy import not_, func, or_, desc, and_

log = logging.getLogger(__name__)

class View(BaseView):
    def __init__(self, request):
        super().__init__(request)
        self.table = PembayaranDph

    ########
    # List #
    ########
    @view_config(route_name='pbbm-dph-rekap', renderer='templates/list_dph_rekap.pt',
                 permission='pbb-monitoring-dph')
    def view_list(self):
        kecamatan = self.ses['kd_kecamatan']
        kelurahan = self.ses['kd_kelurahan']

        data_awal = UserArea.user_desa(self.req.user.id).all()

        if data_awal :
            kdkec = tuple(k.kode[6:9] for k in data_awal if hasattr(k, 'kode') and k.kode)
            kdkel = tuple(k.kode[10:13] for k in data_awal if hasattr(k, 'kode') and k.kode)
            iddesa = tuple(k.desa_id for k in data_awal if hasattr(k, 'desa_id') and k.desa_id)

            if hasattr(data_awal[0], 'group_name') and data_awal[0].group_name in ('PBBM UPT'):
                kecamatans = Kecamatan.query(). \
                    filter(Kecamatan.kd_kecamatan.in_(kdkec)). \
                    order_by('kd_kecamatan').all()
                    
                kelurahans = Kelurahan.query(). \
                    filter(Kelurahan.kd_kecamatan == kecamatan). \
                    order_by('kd_kecamatan','kd_kelurahan').all()
                    
            elif hasattr(data_awal[0], 'group_name') and data_awal[0].group_name in ('PBBM Kecamatan'):
                self.ses['kd_kecamatan'] = kdkec[0]
                kecamatans = Kecamatan.query(). \
                    filter(Kecamatan.kd_kecamatan.in_(kdkec)). \
                    order_by('kd_kecamatan').all()
                    
                kelurahans = Kelurahan.query(). \
                    filter(Kelurahan.kd_kecamatan.in_(kdkec)). \
                    order_by('kd_kecamatan','kd_kelurahan').all()
            else:
                user = UserArea.allow_area(self.req.user.id, iddesa)
                if user :
                    kecamatan = kdkec
                    self.ses['kd_kecamatan'] = kdkec[0]
                    kelurahan = kdkel
                    self.ses['kd_kelurahan'] = kdkel[0]
                    
                    kecamatans = Kecamatan.query(). \
                        filter(Kecamatan.kd_kecamatan.in_(kecamatan)). \
                        order_by('kd_kecamatan').all()
                    kelurahans = Kelurahan.query(). \
                        filter(and_(Kelurahan.kd_kecamatan.in_(kecamatan), Kelurahan.kd_kelurahan.in_(kelurahan))). \
                        order_by('kd_kecamatan','kd_kelurahan').all()
        else:
            kecamatans = Kecamatan.query().order_by('kd_kecamatan').all()
            kelurahans = Kelurahan.query(). \
                filter_by(kd_kecamatan=self.ses['kd_kecamatan']). \
                order_by('kd_kelurahan').all()
                
        return dict(project=self.req.app_name,
                    kecamatans=kecamatans,
                    kelurahans=kelurahans,
                    tahun=datetime.now().year)

    ##########
    # Action #
    ##########
    @view_config(route_name='pbbm-dph-rekap-act', renderer='json',
                 permission='pbb-monitoring-dph')
    def view_act(self):
        request = self.req
        ses = request.session
        url_dict = request.matchdict
        if url_dict['act'] == 'grid':
            columns = [
                ColumnDT(func.concat(PembayaranDph.kd_propinsi, func.concat(".", 
                        func.concat(PembayaranDph.kd_dati2, func.concat(".", 
                        func.concat(PembayaranDph.kd_kecamatan, func.concat(".", 
                        func.concat(PembayaranDph.kd_kelurahan, func.concat(".", 
                        func.concat(PembayaranDph.kd_blok, func.concat(".", 
                        func.concat(PembayaranDph.no_urut, func.concat(".", 
                        func.concat(PembayaranDph.kd_jns_op, func.concat(".", 
                        func.concat(PembayaranDph.thn_pajak_sppt, func.concat(".", 
                        func.concat(PembayaranDph.pembayaran_ke))))))))))))))))), mData="id"),
                ColumnDT(query_nop(self.table), mData="nop"),
                ColumnDT(PembayaranDph.thn_pajak_sppt, mData='tahun'),
                ColumnDT(PembayaranDph.nm_wp_sppt, mData='nama'),
                ColumnDT(PembayaranDph.tgl_jatuh_tempo_sppt,
                            mData='jatuh_Tempo'),
                ColumnDT(PembayaranDph.pbb_yg_harus_dibayar_sppt,
                            mData='pokok'),
                ColumnDT(PembayaranDph.denda, mData='denda'),
                ColumnDT(PembayaranDph.bayar, mData='bayar'),
                ColumnDT(PembayaranDph.tgl_pembayaran,
                            mData='tgl_pembayaran'),
                ColumnDT(PembayaranDph.posted, mData='posted'),
                ColumnDT(PembayaranDph.status, mData='status'),
                ColumnDT(PembayaranDph.pembayaran_ke, mData='ke'),
                ColumnDT(User.user_name, mData='user_name'),
            ]
            query = DBSession.query().select_from(PembayaranDph).\
                outerjoin(User, and_(
                    User.id == PembayaranDph.create_uid)). \
                filter(PembayaranDph.tgl_pembayaran.between(self.dt_awal, self.dt_akhir))
            log.debug(f"tanggal awal {self.dt_awal}")
            if ses['kd_kecamatan'] != '000':
                query = query.filter(PembayaranDph.kd_kecamatan == ses['kd_kecamatan'])
            if ses['kd_kelurahan'] != '000':
                query = query.filter(PembayaranDph.kd_kelurahan == ses['kd_kelurahan'])
            if str(ses['tahun']) != '0000':
                query = query.filter(PembayaranDph.thn_pajak_sppt == ses['tahun'])
                
            query = query.order_by(desc(PembayaranDph.id))
                
            row_table = DataTables(request.GET, query, columns)
            return row_table.output_result()


def route_list(request):
    return HTTPFound(location=request.route_url('pbbm-dph-rekap'))