vendor.py 13.1 KB
"""
Module proses:
Save
Invoice To Vendor
Invoice To Customer
"""

import hashlib
import logging
from importlib import import_module

from ..models import (flush_row, PartnerProduk)
from ..tools import (get_settings)

# from opensipkd.pasar.models import PartnerLog
log = logging.getLogger(__name__)


class VendorClass(object):
    def __init__(self, vendor_produk, invoice_det, **kwargs):
        """
        :param vendor_produk:
        :param bill_no:
        :param customer_id:
        :param cust_trx:
        :param row:
        """
        self.settings = get_settings()
        self.vendor_produk = vendor_produk
        self.invoice_det = invoice_det
        self.v_produk_kd = vendor_produk.kode
        self.kategori = vendor_produk.produk.kategori.kode
        self.id_pel = invoice_det and invoice_det.id_pel or None
        self.request = None
        self.response = None
        self.result = None
        self.vend_inv_no = invoice_det and invoice_det.vend_inv_no or None
        self.status = invoice_det and invoice_det.status or 0

        self.serial_number = invoice_det and hasattr(invoice_det, 'serial_number') \
                             and invoice_det.serial_number or None
        self.amt_buy = 0
        self.amt_sell = 0
        self.discount = 0
        self.trx_id = 0

    def set_response(self, data=None, message=None, code=999, typ="inquiry"):
        if not data and message:
            message = message and message or "No Response From Biller"
            data = dict(code=code,
                        message=message)

        self.result = data
        self.save_log(typ)
        log.info("Result: %s" % data)
        return data

    def set_success(self, data=None, typ="payment"):
        self.status = 1
        self.result = data
        data = data and data or {}
        data.update(dict(code=0,
                         message="SUCCESS",
                         status="SUCCESS"))
        func = self.vendor_produk.produk.kategori.module
        pckgs = 'agratek.api.merchant.views.vendor'

        log.info("Module: %s Pckgs: %s" % (func, pckgs))

        modul = import_module(pckgs)
        cls = hasattr(modul, func) and getattr(modul, func) or None
        if cls:
            data['denom'] = self.invoice_det.produk.kode
            data['id_pel'] = self.id_pel
            data['nama'] = 'nama' in data and data['nama'] or None
            cls_module = cls(data)
            data = cls_module.sukses()

        return self.set_response(data, typ=typ)

    def set_failed(self, data=None, message=None, typ="payment"):
        self.status = -3
        data = data and data or {}
        message = message and message or "FAILED"
        data.update(dict(denom=self.vendor_produk.produk.kode,
                         id_pel=self.id_pel,
                         code=13,
                         message=message,
                         status="FAILED"))
        return self.set_response(data, typ=typ)

    def set_double(self, data=None, message=None, typ="payment"):
        self.status = -3
        message = message and message or "DOUBLE"
        data = data and data or {}
        data.update(dict(denom=self.vendor_produk.produk.kode,
                         id_pel=self.id_pel,
                         code=67,
                         message=message,
                         status="FAILED"))
        return self.set_response(data, typ=typ)

    def set_pending(self, data=None, message=None, typ="payment"):
        self.status = -1
        data = data and data or {}
        message = "TRX ke {} sedang di proses".format(self.id_pel)
        data.update(dict(denom=self.vendor_produk.produk.kode,
                         id_pel=self.id_pel,
                         code=68,
                         status="PENDING",
                         message=message)
                    )
        return self.set_response(data, typ=typ)

    def get_price(self, pokok=0):
        product_id = self.invoice_det.produk_id
        self.amt_buy = pokok + self.vendor_produk.harga
        if hasattr(self.invoice_det, "customer_id"):
            customer_id = self.invoice_det.customer_id
        else:
            customer_id = self.invoice_det.h2h_ar_invoice.customer_id

        harga = pokok + self.vendor_produk.produk.harga
        self.discount = PartnerProduk.get_discount(customer_id, product_id)
        self.amt_sell = harga - self.discount
        return dict(
            subtotal=int(harga),
            discount=int(self.discount),
            total=int(self.amt_sell)
        )

    def save_log(self, typ):
        self.invoice_det.status = self.status
        if self.result:
            if "total" in self.result:
                self.amt_sell = self.result["total"]
            if "rincian" in self.result and self.result["rincian"] \
                and "token" in self.result["rincian"] \
                and self.result["rincian"]["token"]:
                self.serial_number = self.result["rincian"]["token"]
                self.result["serial_number"] = self.serial_number
        if hasattr(self.invoice_det, 'serial_number'):
            self.invoice_det.serial_number = self.serial_number

        if self.vend_inv_no:
            self.invoice_det.vend_inv_no = self.vend_inv_no

        if self.amt_buy:
            self.invoice_det.amt_buy = self.amt_buy

        if self.amt_sell:
            self.invoice_det.amt_sell = self.amt_sell

        if typ == 'inquiry':
            self.invoice_det.inquiry = dict(request=self.request,
                                            response=self.response,
                                            result=self.result)
        elif typ == 'payment':
            self.invoice_det.payment = dict(request=self.request,
                                            response=self.response,
                                            result=self.result)
        elif typ == 'advice':
            self.invoice_det.advice = dict(request=self.request,
                                           response=self.response,
                                           result=self.result)
        elif typ == 'info':
            self.invoice_det.info = dict(request=self.request,
                                         response=self.response,
                                         result=self.result)

        flush_row(self.invoice_det)


class Result(object):
    def __init__(self, values, **kwargs):
        self.values = values
        if "rincian" in values:
            discount = int(self.values["discount"])
            subtotal = int(values["rincian"]["total"])
            self.values["subtotal"] = subtotal
            self.values["discount"] = discount
            self.values["total"] = subtotal - discount

    """
    {
     "denom": "TSEL5",
     "id_pel": "0812121212",
     "subtotal": 5675,
     "discount": 0,
     "total": 5675,
     "code":0
     "status": "PENDING"
     "message":""
    }
    """

    def set_denom(self):
        return {
            'denom': self.values['denom'],
            'id_pel': self.values['id_pel']}

    def set_value(self):
        result = {
            'subtotal': self.values['subtotal'],
            'discount': self.values['discount'],
            'total': self.values['total']}
        if self.values["nama"]:
            result.update({'nama': self.values["nama"]})
        serial_numnber = 'serial_number' in self.values and self.values["serial_number"] or ""
        result.update({'serial_number': serial_numnber})

        return result

    def sukses(self):
        result = self.set_denom()
        result.update(self.set_value())
        result.update({'code': 0,
                       'message': 'Transaksi Berhasil',
                       'status': 'SUCCESS',
                       })
        return result

    def pending(self):
        result = self.set_denom()
        result.update(self.set_value())
        result.update({'code': 68,
                       'message': 'Silahkan lakukan manual advice setelah beberapa saat',
                       'status': 'PENDING'})
        return result

    def gagal(self):
        result = self.set_denom()
        result.update({'code': 99,
                       'message': 'Silahkan coba beberapa saat lagi',
                       'status': 'FAILED',
                       })
        return result

    def not_found(self):
        result = self.set_denom()
        result.update({'code': 55,
                       'message': 'Data Tidak ditemukan',
                       'status': 'FAILED',
                       })
        return result

    def allready_paid(self):
        result = self.set_denom()
        result.update({'code': 54,
                       'message': 'Data sudah dibayar',
                       'status': 'FAILED',
                       })
        return result


class PulsaResult(Result):
    pass


class Rincian(Result):
    def rincian(self):
        pass  # Override Please

    def sukses(self):
        result = super().sukses()
        result["rincian"] = self.rincian()
        return result

    def pending(self):
        result = super().pending()
        result["rincian"] = self.rincian()
        return result


class PlnPreResult(Rincian):
    def rincian(self):
        log.info("Values: {}".format(self.values))
        pokok = self.values["rincian"]["pokok"]
        admin = self.values["rincian"]["admin"]
        denda = self.values["rincian"]["denda"]
        result = dict(
            pokok=pokok,
            denda=denda,
            admin=admin,
            total=pokok + denda + admin,
            tarif=self.values["rincian"]["tarif"],
            no_meter=self.values["rincian"]["no_meter"],
            power=self.values["rincian"]["power"],
        )
        if self.values["rincian"]["token"]:
            result.update({'token': self.values["rincian"]["token"]})

        return result


class PlnPascaResult(Rincian):
    def rincian(self):
        pokok = self.values["rincian"]["pokok"]
        admin = self.values["rincian"]["admin"]
        denda = self.values["rincian"]["denda"]
        result = dict(
            period=self.values["rincian"]["period"],
            jml_bulan=self.values["rincian"]["jml_bulan"],
            pokok=pokok,
            denda=denda,
            admin=admin,
            total=pokok + denda + admin,
            tarif=self.values["rincian"]["tarif"],
            no_meter=self.values["rincian"]["no_meter"],
            power=self.values["rincian"]["power"],
        )
        return result

class PgnResult(Rincian):
    def rincian(self):
        pokok = self.values["rincian"]["pokok"]
        admin = self.values["rincian"]["admin"]
        denda = self.values["rincian"]["denda"]

        return dict(
            pokok=self.values["rincian"]["pokok"],
            denda=self.values["rincian"]["denda"],
            admin=self.values["rincian"]["admin"],
            total=pokok + denda + admin,
            period=self.values["rincian"]["period"],
            meter=self.values["rincian"]["meter"],
            penggunaan=self.values["rincian"]["penggunaan"],
        )


class MfResult(Rincian):
    def rincian(self):
        pokok = self.values["rincian"]["pokok"]
        admin = self.values["rincian"]["admin"]
        denda = self.values["rincian"]["denda"]
        coll_fee = self.values["rincian"]["coll_fee"]
        return dict(
            pokok=pokok,
            denda=denda,
            admin=admin,
            coll_fee=coll_fee,
            total=pokok + denda + admin+ coll_fee,
            period=self.values["rincian"]["period"],
            jth_tempo=self.values["rincian"]["jth_tempo"],
            platform=self.values["rincian"]["platform"],
        )


class TelkomResult(Rincian):
    def rincian(self):
        pokok = self.values["rincian"]["pokok"]
        admin = self.values["rincian"]["admin"]
        denda = self.values["rincian"]["denda"]
        return dict(
            period=self.values["rincian"]["period"],
            pokok=pokok,
            denda=denda,
            admin=admin,
            total=pokok + denda + admin,
        )


class BpjsResult(Rincian):
    def rincian(self):
        pokok = self.values["rincian"]["pokok"]
        admin = self.values["rincian"]["admin"]
        denda = self.values["rincian"]["denda"]
        return dict(
            bill_rest=self.values["rincian"]["bill_rest"],
            cabang=self.values["rincian"]["cabang"],
            nm_cabang=self.values["rincian"]["nm_cabang"],
            anggota=self.values["rincian"]["anggota"],
            jml_bulan=self.values["rincian"]["jml_bulan"],
            pokok=pokok,
            denda=denda,
            admin=admin,
            total=pokok + denda + admin,
        )


class PdamResult(Rincian):
    def rincian(self):
        pokok = self.values["rincian"]["pokok"]
        admin = self.values["rincian"]["admin"]
        denda = self.values["rincian"]["denda"]
        result = dict(
            period=self.values["rincian"]["period"],
            jml_bulan=self.values["rincian"]["jml_bulan"],
            meter=self.values["rincian"]["meter"],
            pokok=pokok,
            denda=denda,
            admin=admin,
            total=pokok + denda + admin,

        )
        return result

def sha256(hash_string):
    return hashlib.sha256(hash_string.encode()).hexdigest()