chart_detail.py 4.61 KB
import sys
import re
import logging
import os
import qrcode
import base64
from email.utils import parseaddr
from sqlalchemy import not_, func, or_, desc
from datetime import datetime
from time import gmtime, strftime, strptime
from pyramid.view import (
    view_config,
    )
from pyramid.httpexceptions import (
    HTTPFound,
    )
import colander
from deform import (
    Form,
    widget,
    ValidationFailure,
    )
from ..tools import (
    _DTnumberformat,
    multi_dict_values,
    odt_export,
    terbilang,
    thousand,
    dmy,
    BULANS,
    get_settings
    )
from ..models import DBSession
from ..models.isipkd import(
      Pegawai, ObjekPajak, SubjekPajak, ARInvoice,
      Unit, UserUnit,Wilayah, Pajak, Rekening, 
      ARSts, ARStsItem, ARSspd, ARTbp
      )

from datatables import (
    ColumnDT, DataTables)
    
from ..security import group_finder,group_in

from daftar import (STATUS, deferred_status,
                    daftar_subjekpajak, deferred_subjekpajak,
                    daftar_objekpajak, deferred_objekpajak,
                    daftar_wilayah, deferred_wilayah,
                    daftar_unit, deferred_unit,
                    daftar_pajak, deferred_pajak,
                    auto_op_nm, auto_unit_nm, auto_wp_nm, auto_wp_nm3
                    )


SESS_ADD_FAILED = 'Gagal tambah Tagihan'
SESS_EDIT_FAILED = 'Gagal edit Tagihan'
log = logging.getLogger(__name__)
                    
    
@view_config(route_name='chart-det', renderer='templates/chart_det.pt')
def view_detail(request):
    params = request.params
    url_dict = request.matchdict
    kd = ''
    unit = ''
    
    if 'kode' in params and params['kode']:
        rekening = Rekening.query().filter(func.trim(Rekening.kode)==params['kode'].strip()).first()
        level = rekening.level_id
        kd = rekening.kode.strip()
        unit = rekening.nama.strip()
        rek = Rekening.query().filter(Rekening.level_id==(level+1)).order_by(Rekening.kode).all()
    else:
        rek = Rekening.query().filter(Rekening.level_id==2).order_by(Rekening.kode).all()
    reks = []
    for r in rek:
        reks.append(dict(
                kode=r.kode.strip(), 
                nama=r.nama.strip(), 
                target=[0,0,0,0,0,0,0,0,0,0,0,0], 
                realisasi=[0,0,0,0,0,0,0,0,0,0,0,0], 
                persen=[0,0,0,0,0,0,0,0,0,0,0,0]
                ))
                
    payments = DBSession.query(
                    ARSspd.tgl_bayar.label('tanggal'),
                    ARSspd.bayar.label('jumlah'),
                    ARInvoice.rek_kode.label('rek_kode'),
                    Unit.kode.label('kode'),
                    Unit.nama.label('nama'),
            ).join(ARInvoice, ARInvoice.id == ARSspd.arinvoice_id).\
              join(Unit, func.trim(Unit.kode) == func.trim(ARInvoice.unit_kode)).\
            filter(ARSspd.tahun_id==params['year']).order_by(ARInvoice.unit_kode)\
            .all()
    for p in payments:
        for re in reks:
            if p.rek_kode.strip().startswith(re['kode']):
                ## chart realisasi
                for x in range(12):
                    if p.tanggal.month == x+1:
                        re['realisasi'][x] += int(p.jumlah)

    invoices = DBSession.query(
                    ARInvoice.tgl_tetap.label('tanggal'),
                    ARInvoice.jumlah.label('jumlah'),
                    ARInvoice.rek_kode.label('rek_kode'),
                    Unit.kode.label('kode'),
                    Unit.nama.label('nama'),
            ).join(Unit, func.trim(Unit.kode) == func.trim(ARInvoice.unit_kode)).\
            filter(ARInvoice.tahun_id==params['year']).order_by(ARInvoice.unit_kode)\
            .all()
    for i in invoices:
        for re in reks:
            if i.rek_kode.strip().startswith(re['kode']):
                ## chart target
                for x in range(12):
                    if i.tanggal.month == x+1:
                        re['target'][x] += int(p.jumlah)
                        
    ##Hitung persen
    for re in reks:
        for x in range(12):
            re['persen'][x] = re['target'][x]>0 and str(round((float(re['realisasi'][x])/float(re['target'][x]))*100,2)) or '0'
    ##hanya yang isi saja yang diambil
    #re_ = []
    #for re in reks:
    #    if sum(re['target'])>0 and sum(re['realisasi'])>0:
    #        re_.append(re)
    #
    #if len(re_)==0:
    #   raise HTTPFound(location=request.route_url('home'))
    #data = re_
    #count_unit = len(re_) > 3 and 3 or 4
    
    if len(reks)==0:
       raise HTTPFound(location=request.route_url('home'))
    data = reks
    count_unit = len(reks) > 3 and 3 or 4

    return dict(data=data, count_unit=count_unit, kd=kd, unit=unit)