tbp.py 16.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.orm import (
    relationship,
    backref
    )
from sqlalchemy import (Column, Integer, String, SmallInteger, UniqueConstraint, 
                        Date, BigInteger, ForeignKey, func, extract, case, DateTime, Float)
from ..models import SipkdBase, SipkdDBSession, CommonModel
from ..models import DaftUnit, Bendahara, RekPdpt, BkuTbp

################
# TRANSAKSI TBP#
################
class Tbp(SipkdBase, CommonModel):
    __tablename__  = 'tbp'
    __table_args__ = {'extend_existing':True}    
    unitkey     = Column(String(10), primary_key=True)
    notbp       = Column(String(50), primary_key=True)
    keybend1    = Column(String(10)) 
    kdstatus    = Column(String(3) )
    keybend2    = Column(String(10)) 
    idxkode     = Column(Integer)
    tgltbp      = Column(DateTime) 
    penyetor    = Column(String(100)) 
    alamat      = Column(String(200)) 
    uraitbp     = Column(String(254)) 
    tglvalid    = Column(DateTime) 
    @classmethod
    def query_kode(cls, unitkey, notbp):
        return SipkdDBSession.query(cls).filter_by(unitkey =unitkey,
            notbp = notbp)
            
    @classmethod
    def tambah(cls, values):
        # keybend = '2084_'
        #unit_kode = '3.01.01.02.'
        # unitkey = DaftUnit.get_key(unit_kode) 
        # keybend = '2084_'
        # seharusnya parameter nip dimana keybend diquery dari tabel bendahara
        # seperti unitkey
        # unit_kode = '3.01.01.02.'
        # UNIT kode harusnya parameter didalam values
	    #perubahan mulai disini
        unit_kode = values['unit_kode']
        nip = values['nip']
        unitkey = DaftUnit.get_key(unit_kode)
        keybend = Bendahara.get_key(unitkey,nip)
        #perubahan selesai disini
        mtgkey = RekPdpt.get_key(values['rekening_kd'])
        
        values['unitkey']  = unitkey
        values['keybend1']  = keybend
        values['keybend2']  = keybend
        values['idxkode']  = '1'
        values['kenaikan'] = 0
        values['mtgkey']  = mtgkey
        values['nojetra'] = '11'
        
        row_tbp = SipkdDBSession.query(Tbp).\
                filter_by(notbp=values['notbp'], unitkey=unitkey).first()
        if not row_tbp:
            row_tbp = Tbp()
            
        #row_skp.npwpd    = row.kode[:18][:-10]
        #row_skp.tglvalid = datetime.now()
        
        row_tbp.from_dict(values)
        SipkdDBSession.add(row_tbp)
        SipkdDBSession.flush()
            
        if values['nilai']<>0:
            row_tbpdet = TbpDet()
            row_tbpdet.from_dict(values)
            SipkdDBSession.add(row_tbpdet)
            SipkdDBSession.flush()
            
        #jurnal SKP dengan stored procedure
        row_tbp.tglvalid = row_tbp.tgltbp
        SipkdDBSession.add(row_tbp)
        SipkdDBSession.flush()
        
        row_bku = SipkdDBSession.query(BkuTbp).\
                filter_by(notbp=row_tbp.notbp, unitkey=unitkey, 
                    nobkuskpd=row_tbp.notbp).\
                first()
        if not row_bku:
            row_bku = BkuTbp()
        row_bku.unitkey     = row_tbp.unitkey
        row_bku.nobkuskpd   = row_tbp.notbp
        row_bku.notbp       = row_tbp.notbp
        row_bku.idxttd      = row_tbp.keybend2
        row_bku.tglbkuskpd  = row_tbp.tgltbp
        row_bku.uraian      = row_tbp.uraitbp
        row_bku.keybend     = row_tbp.keybend1
        SipkdDBSession.add(row_bku)
        SipkdDBSession.flush()
        row_bku.tglvalid    = row_tbp.tgltbp
        SipkdDBSession.add(row_bku)
        SipkdDBSession.flush()
        
        return True
    @classmethod
    def hapus(cls, values):
	    #perubahan mulai disini
        unit_kode = values['unit_kode']
        nip = values['nip']
        unitkey = DaftUnit.get_key(unit_kode) 
        keybend = Bendahara.get_key(unitkey,nip)
        #perubahan selesai disini
        # keybend = '2084_'
        # unit_kode = '3.01.01.02.'
        row_bku = SipkdDBSession.query(BkuTbp).\
                filter_by(notbp=values['notbp'], unitkey=unitkey).delete()
        row_tbpdet = SipkdDBSession.query(TbpDet).\
                filter_by(unitkey = unitkey, notbp = values['notbp']).delete()
        row_tbp = SipkdDBSession.query(Tbp).\
                filter_by(unitkey = unitkey, notbp = values['notbp']).delete()
        SipkdDBSession.flush()
        return True
    
            
class TbpDet(SipkdBase, CommonModel):
    __tablename__  = 'tbpdetd'
    __table_args__ = {'extend_existing':True}    
    mtgkey      = Column(String(10), primary_key=True)
    nojetra     = Column(String(2), primary_key=True)
    unitkey     = Column(String(10), primary_key=True)
    notbp       = Column(String(50), primary_key=True)
    nilai       = Column(Float)
    @classmethod
    def query_kode(cls, unitkey, notbp, mtgkey, nojetra):
        return SipkdDBSession.query(cls).filter_by(unitkey =unitkey,
                  notbp = notbp, mtgkey=mtgkey, nojetra=nojetra)

def save_tbp(data, post=0):
    unitkey  = '_' in data['unit_kd'] and data['unit_kd'] or DaftUnit.get_key(data['unit_kd'])
    keybend  = '_' in data['bendahara_kd'] and data['bendahara_kd'] or Bendahara.get_key(unitkey, data['bendahara_kd'])
    if post:
        row = Tbp.query_kode(unitkey, data['notbp']).first()
        if not row:
            row = Tbp()
            row.unitkey = unitkey
        row.from_dict(data)
        #row.idxkode  = '1' #pendapatan
        #row.kenaikan = 0
        row.keybend1 = keybend
        row.keybend2 = keybend
        #row_skp.kdstatus = '70'
        #row_skp.npwpd    = row.npwpd[-10:]
        #row_skp.tglskp   = row.tgl_tetap
        #row_skp.penyetor = row.nama
        #row_skp.alamat   = row.alamat
        #row_skp.uraiskp  = row.rekening_nm
        #row_skp.tgltempo = row.jth_tempo
        #row_skp.tglvalid = datetime.now()
        #row_skp.bunga    = row.denda+row.bunga
        SipkdDBSession.add(row)
        SipkdDBSession.flush()
        for item in data['items']:
            mtgkey  = '_' in item['rekening_kd'] and item['rekening_kd'] or \
                          RekPdpt.get_key(item['rekening_kd'])
            nojetra = item['nojetra']
            rowdet = TbpDet.query_kode(row.unitkey, row.notbp, mtgkey, nojetra).first()
            if not rowdet:
                rowdet = TbpDet()
                rowdet.unitkey = row.unitkey
                rowdet.notbp   = row.notbp
                rowdet.mtgkey  = mtgkey
                rowdet.nojetra = nojetra
            rowdet.nilai   = item['nilai']
            SipkdDBSession.add(rowdet)
            SipkdDBSession.flush()

        #row_skp.tglvalid = row_skp.tglskp
        #SipkdDBSession.add(row_skp)
        #SipkdDBSession.flush()
        #row.posted = 1
        #DBSession.add(row)
        #DBSession.flush()
    # else:
        # unitkey = SipkdUnit.get_key_by_kode(row.unit_kd)
        # noskp = "%s/%s" % (row.kode, row.ref_kode)

        # row_skpdet = SipkdDBSession.query(SipkdSkpDet).\
                                 # filter_by(unitkey = unitkey,
                                           # noskp   = noskp).delete()
        # SipkdDBSession.flush()

        # row_skp = SipkdDBSession.query(SipkdSkp).\
                                 # filter_by(unitkey = unitkey,
                                           # noskp   = noskp).delete()
        # SipkdDBSession.flush()
        # row.posted = 0
        # DBSession.add(row)
        # DBSession.flush()


###########
# Posting #
###########   
# def query_post_id(id):
    # return DBSession.query(ARPaymentBphtb).filter(ARPaymentBphtb.id==id)

# @view_config(route_name='ar-payment-bphtb-post', renderer='json',
             # permission='ar-payment-bphtb-post')
# def view_posting(request):
    # if request.POST:
        # controls = dict(request.POST.items())
        # n_id_not_found = 0
        # n_row_zero     = 0
        # n_posted       = 0
        # n_id           = 0
        # msg = ""
        # for id in controls['id'].split(","):
            # row    = query_post_id(id).first()
            # if not row:
                # n_id_not_found = n_id_not_found + 1
                # continue

            # if not row.nilai:
                # n_row_zero = n_row_zero + 1
                # continue

            # if request.session['posted']==0 and row.posted:
                # n_posted = n_posted + 1
                # continue

            # if request.session['posted']==1 and not row.posted:
                # n_posted = n_posted + 1
                # continue

            # n_id = n_id + 1

            # id_inv = row.id

            # if request.session['posted']==0:
                # unitkey = SipkdUnit.get_key_by_kode(row.unit_kd)
                # kodekey = row.kode
                # #CEK DULU DATA SKP
                # #row_skp = SipkdDBSession.query(SipkdSkp).\
                # #                         filter_by(UNITKEY = unitkey,
                # #                                   NOSKP   = row.ref_kode) #cek by no bayar
                # #if row_skp:
                # #    statuskd = '64' #Penerimaan (Rek.Bend)-Penetapan
                # #else:
                # #    statuskd = '63' #Penerimaan (Rek.Bend)-Tanpa Penetapan
                
                # if not row.ref_kode or row.ref_kode=='0' or row.ref_kode=='00':
                    # statuskd = '63' #Penerimaan (Rek.Bend)-Tanpa Penetapan
                # else:
                    # statuskd = '64' #Penerimaan (Rek.Bend)-Penetapan
                # row_tbp = SipkdTbp()
                # row_tbp.unitkey  = unitkey
                # row_tbp.notbp    = kodekey 
                # row_tbp.keybend1 = '1797_'

                # row_tbp.kdstatus = statuskd
                # row_tbp.keybend2 = '1797_'
                # row_tbp.idxkode  = '1' #pendapatan
                # row_tbp.tgltbp   = row.tgl_trans
                # row_tbp.penyetor = row.nama
                # row_tbp.alamat   = row.alamat
                # row_tbp.uraitbp  = row.rekening_nm
                # row_tbp.tglvalid = row.tgl_trans
                # SipkdDBSession.add(row_tbp)
                # SipkdDBSession.flush()
                
                # if row.pokok+row.denda+row.bunga>0:  
                    # row_tbpdet = SipkdTbpDet()
                    # row_tbpdet.unitkey = unitkey
                    # row_tbpdet.notbp   = kodekey
                    # row_tbpdet.nilai   = row.pokok+row.denda+row.bunga
                    # row_tbpdet.mtgkey  = SipkdRek4.get_key_by_kode(row.rekening_kd)
                    # row_tbpdet.nojetra = '11' #Penerimaan STS/TBP
                    # SipkdDBSession.add(row_tbpdet)
                    # SipkdDBSession.flush()
                
                   
                # row_bku = SipkdBkuTbp()
                # row_bku.unitkey     = unitkey
                # row_bku.nobkuskpd   = kodekey
                # row_bku.notbp       = kodekey
                # row_bku.idxttd      = '1797_'
                # row_bku.tglbkuskpd  = row.tgl_trans
                # row_bku.uraian      = row.nama
                # row_bku.tglvalid    = row.tgl_trans
                # row_bku.keybend     = '1797_'
                # SipkdDBSession.add(row_bku)
                # SipkdDBSession.flush()
                # row_bku.tglvalid    = row.tgl_trans
                # SipkdDBSession.add(row_bku)
                # SipkdDBSession.flush()
    
                # """"if (row.denda+row.bunga)>0:  
                    # row_tbpdet = SipkdTbpDet()
                    # row_tbpdet.UNITKEY = unitkey
                    # row_tbpdet.NOTBP   = kodekey
                    # row_tbpdet.NILAI   = row.denda+row.bunga
                    # row_tbpdet.MTGKEY  = SipkdRek4.get_key_by_kode(row.rekening_kd)
                    # row_tbpdet.NOJETRA = '11' #Penerimaan STS/TBP
                    # SipkdDBSession.add(row_tbpdet)
                    # SipkdDBSession.flush()
                
                
                # if row_skp:
                    # row_skptbp = SipkdSkpTbp()
                    # row_skptbp.UNITKEY = unitkey
                    # row_skptbp.NOTBP   = row_tbp.NOTBP
                    # row_skptbp.NOSKP   = row.ref_kode
                    # SipkdDBSession.add(row_skptbp)
                    # SipkdDBSession.flush()
                # """
                
                # row.posted = 1
                # DBSession.add(row)
                # DBSession.flush()
            # else:
                # unitkey = SipkdUnit.get_key_by_kode(row.unit_kd)
                # notbp = row.kode
                # row_bku = SipkdDBSession.query(SipkdBkuTbp).\
                                         # filter_by(unitkey = unitkey,
                                                   # notbp   = notbp,
                                                   # ).delete()
                                                   
                # row_tbpdet = SipkdDBSession.query(SipkdTbpDet).\
                                         # filter_by(unitkey = unitkey,
                                                   # notbp   = notbp).delete()
                # # row_skptbp = SipkdDBSession.query(SipkdSkpTbp).\
                                         # # filter_by(unitkey = unitkey,
                                                   # # notbp   = notbp).delete()
                # row_tbp = SipkdDBSession.query(SipkdTbp).\
                                         # filter_by(unitkey = unitkey,
                                                   # notbp   = notbp).delete()
                # SipkdDBSession.flush()
                # row.posted = 0
                # DBSession.add(row)
                # DBSession.flush()

        # if n_id_not_found > 0:
            # msg = '%s Data Tidan Ditemukan %s \n' % (msg,n_id_not_found)
        # if n_row_zero > 0:
            # msg = '%s Data Dengan Nilai 0 sebanyak %s \n' % (msg,n_row_zero)
        # if n_posted>0:
            # msg = '%s Data Tidak Di Proses %s \n' % (msg,n_posted)
        # msg = '%s Data Di Proses %s ' % (msg,n_id)
        
        # return dict(success = True,
                    # msg     = msg)
                    
    # return dict(success = False,
                # msg     = 'Terjadi kesalahan proses')

                

    
#############
# UnPosting #
#############   
# @view_config(route_name='ar-payment-bphtb-unpost', renderer='json',
             # permission='ar-payment-bphtb-unpost')
# def view_unposting(request):
    # row    = query_id(request).first()
    # if not row:
        # return id_not_found(request)
    # if not row.nilai:
        # request.session.flash('Data tidak dapat diunposting, karena bernilai 0.', 'error')
        # return route_list(request)
    # if not row.posted:
        # request.session.flash('Data Belum di posting', 'error')
        # return route_list(request)
    # id_inv = row.id
    # unitkey = SipkdUnit.get_key_by_kode(row.unit_kd)
    # notbp = row.kode
            
    # row_tbpdet = SipkdDBSession.query(SipkdTbpDet).\
                             # filter_by(UNITKEY = unitkey,
                                       # NOTBP   = notbp).delete()
                                       
    # row_skptbp = SipkdDBSession.query(SipkdSkpTbp).\
                             # filter_by(UNITKEY = unitkey,
                                       # NOTBP   = notbp).delete()
    
    # row_tbp = SipkdDBSession.query(SipkdTbp).\
                             # filter_by(UNITKEY = unitkey,
                                       # NOTBP   = notbp).delete()
    # SipkdDBSession.flush()

    # request.session.flash('Data Berhasil diunposting')
    # row.posted = 0
    # DBSession.add(row)
    # DBSession.flush()
    # return route_list(request)
# ##########
# # CSV #
# ##########

# @view_config(route_name='ar-payment-bphtb-csv', renderer='csv',
             # permission='ar-payment-bphtb-csv')
# def view_csv(request):
    # ses = request.session
    # req = request
    # params   = req.params
    # url_dict = req.matchdict
    # q = DBSession.query(ARPaymentBphtb.id,
        # ARPaymentBphtb.unit_kd,
        # ARPaymentBphtb.kode,
        # ARPaymentBphtb.nama,
        # ARPaymentBphtb.rekening_kd,
        # ARPaymentBphtb.rekening_nm,
        # ARPaymentBphtb.pokok,
        # ARPaymentBphtb.denda,
        # ARPaymentBphtb.bunga,
        # ARPaymentBphtb.posted,
        # ARPaymentBphtb.tgl_trans,
        # ARPaymentBphtb.tgl_validasi,
        # ).filter(ARPaymentBphtb.tahun==ses['tahun'],
                 # between(ARPaymentBphtb.tgl_trans, ses['tanggal'], ses['tanggal_to']),
                 # ARPaymentBphtb.posted==ses['posted']
        # ) 
              
    # r = q.first()
    # header = r.keys()
    # query = q.all()
    # rows = []
    # for item in query:
        # rows.append(list(item))

    # # override attributes of response
    # filename = 'ar-payment.csv'
    # request.response.content_disposition = 'attachment;filename=' + filename

    # return {
      # 'header': header,
      # 'rows': rows,
    # }