Ejemplo n.º 1
0
class ml3040(object):
    def __init__(self, **opts):
        self.opts = opts
        if self.setVariables():
            if "args" in self.opts:
                self.tname = self.stpl
                self.mesno = 0
                self.doEnd()
            else:
                self.mainProcess()
                if "wait" in self.opts:
                    self.df.mstFrame.wait_window()
                else:
                    self.opts["mf"].startLoop()

    def setVariables(self):
        gc = GetCtl(self.opts["mf"])
        ctlmst = gc.getCtl("ctlmst", self.opts["conum"])
        if not ctlmst:
            return
        self.taxdf = ctlmst["ctm_taxdf"]
        memctl = gc.getCtl("memctl", self.opts["conum"])
        if not memctl:
            return
        self.ldays = memctl["mcm_ldays"]
        self.lme = memctl["mcm_lme"]
        self.stpl = memctl["mcm_sttpl"]
        self.fromad = memctl["mcm_emadd"]
        self.sql = Sql(self.opts["mf"].dbm, [
            "ctlmes", "ctlmst", "memmst", "memtrn", "memadd", "memcat",
            "memctc", "memctp", "memctk", "memkon", "memlnk", "tplmst"
        ],
                       prog=self.__class__.__name__)
        if self.sql.error:
            return
        t = time.localtime()
        self.sysdtw = (t[0] * 10000) + (t[1] * 100) + t[2]
        yy = int(self.lme / 10000)
        mm = (int(self.lme / 100) % 100) + 1
        while mm > 12:
            yy += 1
            mm -= 12
        self.nme = mthendDate((yy * 10000) + (mm * 100) + 1)
        mm += 1
        while mm > 12:
            yy += 1
            mm -= 12
        self.nne = mthendDate((yy * 10000) + (mm * 100) + 1)
        self.trtypes = []
        for t in mltrtp:
            self.trtypes.append(t[0])
        return True

    def mainProcess(self):
        tpm = {
            "stype":
            "R",
            "tables": ("tplmst", ),
            "cols": (("tpm_tname", "", 0, "Template"), ("tpm_title", "", 0,
                                                        "Title", "Y")),
            "where": [("tpm_type", "=", "R"), ("tpm_system", "=", "MEM")],
            "order":
            "tpm_tname"
        }
        self.mlm = {
            "stype":
            "R",
            "tables": ("memmst", ),
            "cols":
            (("mlm_memno", "", 0, "Mem-No"), ("mlm_oldno", "", 0, "Old-No"),
             ("mlm_idnum", "", 0, "Identity-Numb"), ("mlm_gender", "", 0, "G"),
             ("mlm_state", "", 0, "S"), ("mlm_surname", "", 0, "Surname", "Y"),
             ("mlm_names", "", 0, "Names", "F")),
            "where": [],
            "order":
            "mlm_surname, mlm_names",
            "sort":
            False
        }
        mss = {
            "stype":
            "R",
            "tables": ("ctlmes", ),
            "cols": (("mss_message", "", 0, "Num"), ("mss_detail", "NA", 50,
                                                     "Details")),
            "where": [("mss_system", "=", "MEM")],
            "order":
            "mss_message"
        }
        r1s = (("All", "Z"), ("Active", "A"), ("Deceased", "D"),
               ("Inactive", "I"), ("Resigned", "R"), ("Suspended",
                                                      "S"), ("Defaulted", "X"))
        r2s = (("Yes", "Y"), ("Range", "R"), ("Singles", "S"),
               ("Print Only", "P"), ("Email Only", "E"))
        r3s = (("Yes", "Y"), ("No", "N"))
        r4s = (("Yes", "Y"), ("No", "N"), ("Current", "C"))
        r5s = (("Number", "N"), ("Surname", "M"))
        fld = ((("T", 0, 0, 0), "INA", 20, "Template Name", "", self.stpl, "Y",
                self.doTplNam, tpm, None,
                None), (("T", 0, 1, 0), ("IRB", r1s), 0, "Status", "", "A",
                        "Y", self.doStatus, None, None, None),
               (("T", 0, 2, 0), ("IRB", r2s), 0, "Whole File", "", "Y", "Y",
                self.doWhole, None, None, None),
               (("T", 0, 3, 0), "IUI", 6, "Member Start", "Starting Number", 1,
                "Y", self.doMember, self.mlm, None,
                None), (("T", 0, 3, 0), "IUI", 6, "End", "Ending Number",
                        999999, "Y", self.doMember, self.mlm, None, None),
               (("T", 0, 4, 0), ("IRB", r3s), 0, "Current Year Only", "", "N",
                "Y", self.doCurrent, None, None,
                None), (("T", 0, 5, 0), ("IRB", r4s), 0, "Zero Balances", "",
                        "C", "Y", self.doZeros, None, None, None),
               (("T", 0, 6, 0), ("IRB", r3s), 0, "Paid Transactions", "", "Y",
                "Y", self.doPaid, None, None,
                None), (("T", 0, 7, 0), ("IRB", r3s), 0, "Minus Balances", "",
                        "N", "Y", self.doMinus, None, None,
                        None), (("T", 0, 8, 0), "IUI", 3, "Message Number", "",
                                "", "Y", self.doMessno, mss, None, ("efld", )),
               (("T", 0, 9, 0), "ID1", 10, "Statement Date", "", self.sysdtw,
                "Y", self.doDat, None, None,
                ("efld", )), (("T", 0, 10, 0), ("IRB", r5s), 0, "Sort Order",
                              "", "N", "Y", self.doSort, None, None, None))
        tnd = ((self.doEnd, "Y"), )
        txt = (self.doExit, )
        self.df = TartanDialog(self.opts["mf"],
                               eflds=fld,
                               tend=tnd,
                               txit=txt,
                               view=("N", "V"),
                               mail=("B", "Y"))

    def doTplNam(self, frt, pag, r, c, p, i, w):
        acc = self.sql.getRec("tplmst",
                              where=[("tpm_tname", "=", w),
                                     ("tpm_type", "=", "S"),
                                     ("tpm_system", "=", "MEM")],
                              limit=1)
        if not acc:
            return "Invalid Template Name"
        self.tname = w

    def doStatus(self, frt, pag, r, c, p, i, w):
        self.status = w
        self.mlm["where"] = [("mlm_cono", "=", self.opts["conum"])]
        if self.status != "Z":  # Not All
            self.mlm["where"].append(("mlm_state", "=", self.status))

    def doWhole(self, frt, pag, r, c, p, i, w):
        self.whole = w
        if self.whole != "R":
            self.df.loadEntry(frt, pag, p + 1, data="")
            self.df.loadEntry(frt, pag, p + 2, data="")
            return "sk2"

    def doMember(self, frt, pag, r, c, p, i, w):
        if p == 3:
            self.start = w
        elif w < self.start:
            return "Invalid Member, Before From"
        else:
            self.to = w

    def doCurrent(self, frt, pag, r, c, p, i, w):
        self.current = w

    def doZeros(self, frt, pag, r, c, p, i, w):
        self.zeros = w
        if self.zeros == "Y":
            self.paid = "Y"
            self.df.loadEntry(frt, pag, p + 1, data=self.paid)
            return "sk1"

    def doPaid(self, frt, pag, r, c, p, i, w):
        self.paid = w

    def doMinus(self, frt, pag, r, c, p, i, w):
        self.minus = w

    def doMessno(self, frt, pag, r, c, p, i, w):
        if w:
            acc = self.sql.getRec("ctlmes",
                                  cols=["mss_detail"],
                                  where=[("mss_system", "=", "MEM"),
                                         ("mss_message", "=", w)],
                                  limit=1)
            if not acc:
                return "Invalid Message Number"
        self.mesno = w

    def doDat(self, frt, pag, r, c, p, i, w):
        self.pro = False
        self.ynd = False
        if w > self.nne:
            showError(self.opts["mf"].body, "Month End Error",
                      "More than Two Month Ends are Missing, Aborting ...")
            return "rf"
        if w > self.nme:
            if self.nme == self.opts["period"][2][0]:
                ok = askQuestion(self.opts["mf"].body, "Year End Error",
                    "A Year End as at %s Has Not Been Run\n\n"\
                    "Would You Like to Raise Pro-Forma Charges?" %
                    self.opts["period"][2][1])
                if ok == "yes":
                    self.pro = True
                    self.ynd = True
            else:
                ok = askQuestion(self.opts["mf"].body, "Month End Error",
                    "A Month End as at %s Has Not Been Run\n\n"\
                    "Would You Like to Raise Pro-Forma Charges?" %
                    CCD(self.nme, "D1", 10).disp)
                if ok == "yes":
                    self.pro = True
        self.datew = w
        self.dated = self.df.t_disp[pag][0][p]
        self.curdt = int(w / 100)

    def doSort(self, frt, pag, r, c, p, i, w):
        self.sort = w

    def doEnd(self):
        if "args" in self.opts:
            self.whole = "S"
            self.current = "N"
            self.zeros = "Y"
            self.paid = "Y"
            self.minus = "Y"
            self.mes = None
            self.mes = self.sql.getRec("ctlmes",
                                       cols=["mss_detail"],
                                       where=[("mss_system", "=", "MEM"),
                                              ("mss_message", "=", 1)],
                                       limit=1)
            self.curdt = int(self.opts["args"][1] / 100)
            self.dated = CCD(self.opts["args"][1], "D1", 10).disp
            self.pro = False
            self.repprt = self.opts["args"][2]
            self.repeml = self.opts["args"][3]
            recs = [self.opts["args"][0]]
        else:
            self.df.closeProcess()
            self.repprt = self.df.repprt
            self.repeml = self.df.repeml
            whr = [("mlm_cono", "=", self.opts["conum"])]
            if self.status != "Z":  # Not All
                whr.append(("mlm_state", "=", self.status))
            if self.sort == "N":
                odr = "mlm_memno"
            else:
                odr = "mlm_surname"
            if self.whole == "S":
                recs = getSingleRecords(
                    self.opts["mf"],
                    "memmst", ("mlm_memno", "mlm_surname", "mlm_names"),
                    head=["X", "Number", "Surname", "Names"],
                    where=whr,
                    order=odr,
                    selcol="mlm_surname")
            else:
                if self.whole == "R":
                    whr.append(("mlm_memno", "between", self.start, self.to))
                recs = self.sql.getRec("memmst", where=whr, order=odr)
                if not recs:
                    showError(self.opts["mf"].body, "Error",
                              "No Accounts Selected")
                    if "wait" not in self.opts:
                        self.opts["mf"].closeLoop()
                    return
            # Remove all linked accounts
            col = self.sql.memmst_col
            nos = []
            for acc in recs:
                nos.append(acc[col.index("mlm_memno")])
            chk = copyList(recs)
            for acc in chk:
                whr = [("mll_cono", "=", self.opts["conum"]),
                       ("mll_lnkno", "=", acc[col.index("mlm_memno")])]
                lnk = self.sql.getRec("memlnk", where=whr, limit=1)
                if lnk and lnk[1] in nos:
                    recs.remove(acc)
        if recs:
            self.emadd = self.repeml[2]
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()
            if self.repeml[1] == "N":
                mess = "Printing Statements"
            else:
                mess = "Printing and Emailing Statements"
            p = ProgressBar(self.opts["mf"].body,
                            typ=mess,
                            mxs=len(recs),
                            esc=True)
            for num, rec in enumerate(recs):
                p.displayProgress(num)
                if p.quit:
                    break
                if self.pro:
                    self.doProForma(rec)
                self.doProcess(rec)
            p.closeProgress()
            if self.pro:
                self.opts["mf"].dbm.rollbackDbase()
            if p.quit or not self.form.page:
                pass
            elif self.repeml[1] == "N" or self.emadd:
                self.repeml[2] = self.emadd
                self.doPrint()
        if "wait" not in self.opts:
            self.opts["mf"].closeLoop()

    def doLoadStatic(self):
        cmc = self.sql.ctlmst_col
        ctm = self.sql.getRec("ctlmst",
                              where=[("ctm_cono", "=", self.opts["conum"])],
                              limit=1)
        for fld in cmc:
            dat = ctm[cmc.index(fld)]
            if fld in self.form.tptp:
                if fld == "ctm_logo":
                    self.form.letterhead(cmc, ctm, fld, dat)
                    continue
                self.form.tptp[fld][1] = dat
        if "letterhead" in self.form.tptp:
            self.form.letterhead(cmc, ctm, "letterhead", None)
        self.form.document_date(self.dated)
        self.form.bank_details(cmc, ctm, 0)

    def doProForma(self, rec):
        self.refno = 0
        dmc = self.sql.memmst_col
        acc = [rec[dmc.index("mlm_memno")]]
        lnk = self.sql.getRec("memlnk",
                              cols=["mll_lnkno"],
                              where=[("mll_cono", "=", self.opts["conum"]),
                                     ("mll_memno", "=", acc[0])])
        for l in lnk:
            chk = self.sql.getRec("memmst",
                                  cols=["mlm_state"],
                                  where=[("mlm_cono", "=", self.opts["conum"]),
                                         ("mlm_memno", "=", l[0])],
                                  limit=1)
            if chk and chk[0] == "A":
                acc.append(l[0])
        for memno in acc:
            if self.ynd:
                data = doChkCatChg(self.opts["mf"], self.opts["conum"], memno,
                                   self.datew)
                if data:
                    if not data[3]:
                        self.doRaiseCharge(memno,
                                           "B",
                                           data[0],
                                           data[1],
                                           data[2],
                                           data[3],
                                           data[4],
                                           data[5],
                                           skip=True)
                    self.sql.delRec("memcat",
                                    where=[("mlc_cono", "=",
                                            self.opts["conum"]),
                                           ("mlc_memno", "=", memno),
                                           ("mlc_type", "=", "B"),
                                           ("mlc_code", "=", data[0])])
                    self.sql.insRec("memcat",
                                    data=[
                                        self.opts["conum"], memno, "B",
                                        data[7], "", self.datew, 0, 0
                                    ])
            cols = [
                "mlc_type", "mlc_code", "mcc_desc", "mcc_freq", "mlc_start",
                "mlc_end", "mlc_last"
            ]
            wher = [("mlc_cono", "=", self.opts["conum"]),
                    ("mlc_memno", "=", memno), ("mlc_start", ">", 0),
                    ("mlc_start", "<=", self.datew), ("mcc_cono=mlc_cono", ),
                    ("mcc_type=mlc_type", ), ("mcc_code=mlc_code", ),
                    ("mcc_freq", "<>", "N")]
            cats = self.sql.getRec(tables=["memcat", "memctc"],
                                   cols=cols,
                                   where=wher,
                                   order="mlc_type, mlc_code")
            for ctyp, code, desc, freq, start, end, last in cats:
                if start > self.datew:
                    # Not yet Started
                    continue
                if last and end and end < self.datew:
                    # Ended
                    continue
                if last and freq == "O":
                    # Once Off
                    continue
                if last and last > self.opts["period"][2][0]:
                    # Already Raised for Next Period in Advance
                    continue
                if not self.ynd and last and freq == "A" and \
                        last >= self.opts["period"][1][0] and \
                        last <= self.opts["period"][2][0]:
                    # Already Raised in Financial Period
                    continue
                self.doRaiseCharge(memno, ctyp, code, start, end, last, freq,
                                   desc)

    def doRaiseCharge(self,
                      memno,
                      ctyp,
                      code,
                      start,
                      end,
                      last,
                      freq,
                      desc,
                      skip=False):
        if freq == "O":
            dte = True
            nxt = False
        else:
            dte = False
            nxt = bool(self.ynd or freq == "M")
            if not last:
                if dateDiff(start, self.nme, "days") > self.ldays:
                    dte = True
                else:
                    nxt = True
        if dte:
            trdt = start
            amt = self.doGetCharge(ctyp, code, trdt)
            if amt:
                self.doUpdateTables(memno, ctyp, code, desc, trdt, amt)
        if not skip and nxt:
            if end and self.datew > end:
                return
            trdt = self.datew
            amt = self.doGetCharge(ctyp, code, trdt)
            if amt:
                self.doUpdateTables(memno, ctyp, code, desc, trdt, amt)

    def doGetCharge(self, ctyp, code, date):
        prc = self.sql.getRec("memctp",
                              where=[("mcp_cono", "=", self.opts["conum"]),
                                     ("mcp_type", "=", ctyp),
                                     ("mcp_code", "=", code),
                                     ("mcp_date", "<=", date)],
                              order="mcp_date desc",
                              limit=1)
        if not prc:
            # No Price
            return
        if prc[5] == "N" or (self.ynd and date == self.datew):
            # Not Pro Rata or End of Financial Year
            amt = CCD(prc[6], "UD", 12.2).work
        else:
            # Extract Pro Rata Rate
            mths = 17 - dateDiff(date, self.opts["period"][2][0], "months")
            if mths < 1:
                mths = 12
            amt = CCD(prc[mths], "UD", 12.2).work
        if not amt:
            # No Charge
            return
        else:
            return amt

    def doUpdateTables(self, memno, ctyp, code, desc, trdt, amt):
        batch = "PROFORM"
        self.refno += 1
        refno = "PF%07i" % self.refno
        curdt = int(trdt / 100)
        # VAT Rate and Amount
        vrte = getVatRate(self.sql, self.opts["conum"], self.taxdf, trdt)
        if vrte is None:
            vrte = 0.0
        vat = CCD(round(((amt * vrte) / 114), 2), "UD", 12.2).work
        # Members Ledger Transaction (memtrn)
        self.sql.insRec("memtrn",
                        data=[
                            self.opts["conum"], memno, 1, refno, batch, trdt,
                            amt, vat, curdt, ctyp, code, desc, self.taxdf, "",
                            self.opts["capnm"], self.sysdtw, 0
                        ],
                        unique="mlt_refno")
        # Members Category Record (memcat)
        self.sql.updRec("memcat",
                        cols=["mlc_last"],
                        data=[trdt],
                        where=[("mlc_cono", "=", self.opts["conum"]),
                               ("mlc_memno", "=", memno),
                               ("mlc_type", "=", ctyp),
                               ("mlc_code", "=", code)])

    def doProcess(self, mlm):
        dmc = self.sql.memmst_col
        tdc = self.form.sql.tpldet_col
        self.memno = mlm[dmc.index("mlm_memno")]
        self.doGetBalTrn()
        if not self.mlt[1]:
            return
        if self.zeros == "N" and not self.tbal:
            return
        elif self.zeros == "C" and not self.tbal and not self.mlt[2]:
            return
        if self.minus == "N" and self.tbal < 0:
            return
        eml = []
        kon = self.sql.getRec(tables=["memctk", "memkon"],
                              cols=["mlk_detail"],
                              where=[("mck_type", "=", "E"),
                                     ("mlk_cono", "=", self.opts["conum"]),
                                     ("mlk_memno", "=", self.memno),
                                     ("mlk_code=mck_code", )])
        for k in kon:
            eml.append(k[0])
        if self.whole == "E" and not eml:
            return
        elif self.whole == "P" and eml:
            return
        for col in dmc:
            d = "%s_C00" % col
            if d in self.form.newdic:
                dat = mlm[dmc.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        self.doHeader(mlm, dmc, tdc)
        mlc, mlt = self.mlt[:2]
        if self.current == "Y":
            amt = 0
            bal = 0
            cdt = int(self.opts["period"][2][0] / 100)
            trn = copy.deepcopy(mlt)
            mlt = []
            while trn:
                rec = trn.pop()
                if rec[mlc.index("mlt_curdt")] < cdt:
                    amt = float(ASD(amt) + ASD(rec[mlc.index("mlt_tramt")]))
                    bal = float(ASD(bal) + ASD(rec[mlc.index("balance")]))
                else:
                    mlt.append(rec)
            if amt or bal:
                rec[mlc.index("mlt_type")] = 3
                rec[mlc.index("mlt_refno")] = "B/FWD"
                rec[mlc.index("mlt_batch")] = ""
                rec[mlc.index("mlt_tramt")] = amt
                rec[mlc.index("balance")] = bal
                rec[mlc.index("mlt_taxamt")] = 0
                rec[mlc.index("mlt_desc")] = "Balance Brought Forward"
                rec[mlc.index("mlt_taxind")] = ""
                rec[mlc.index("mlt_batind")] = ""
                mlt.insert(0, rec)
        if len(mlt) <= self.form.maxlines:
            self.doBody(mlc, mlt, tdc)
        else:
            amt = 0
            bal = 0
            lines = len(mlt) - self.form.maxlines + 1
            for _ in range(lines):
                trn = mlt.pop(0)
                amt = float(ASD(amt) + ASD(trn[mlc.index("mlt_tramt")]))
                bal = float(ASD(bal) + ASD(trn[mlc.index("balance")]))
            trn[mlc.index("mlt_type")] = 3
            trn[mlc.index("mlt_refno")] = "B/FWD"
            trn[mlc.index("mlt_batch")] = ""
            trn[mlc.index("mlt_tramt")] = amt
            trn[mlc.index("balance")] = bal
            trn[mlc.index("mlt_taxamt")] = 0
            trn[mlc.index("mlt_desc")] = "Balance Brought Forward"
            trn[mlc.index("mlt_taxind")] = ""
            trn[mlc.index("mlt_batind")] = ""
            mlt.insert(0, trn)
            self.doBody(mlc, mlt, tdc)
        self.doTotal(tdc)
        self.doTail(tdc)
        if self.repeml[1] == "Y" and not self.emadd:
            self.repeml[2] = eml
            self.doPrint()

    def doGetBalTrn(self):
        if self.paid == "Y":
            trans = "A"
        else:
            trans = "Y"
        bals = Balances(self.opts["mf"], "MEM", self.opts["conum"], self.curdt,
                        (self.memno, ))
        self.obal, self.tbal, self.ages, self.mlt = bals.doAllBals(trans=trans)
        lnk = self.sql.getRec("memlnk",
                              cols=["mll_lnkno"],
                              where=[("mll_cono", "=", self.opts["conum"]),
                                     ("mll_memno", "=", self.memno)])
        if not lnk:
            return
        for l in lnk:
            bals = Balances(self.opts["mf"], "MEM", self.opts["conum"],
                            self.curdt, (l[0], ))
            obal, tbal, ages, mlt = bals.doAllBals(trans=trans)
            for n, d in enumerate(mlt[1]):
                mlt[1][n][mlt[0].index("mlt_desc")] = "%s (%s)" % \
                    (d[mlt[0].index("mlt_desc")][:30],
                    d[mlt[0].index("mlt_memno")])
            self.obal = float(ASD(self.obal) + ASD(obal))
            self.tbal = float(ASD(self.tbal) + ASD(tbal))
            for x in range(5):
                self.ages[x] = float(ASD(self.ages[x]) + ASD(ages[x]))
            self.mlt[1].extend(mlt[1])
            self.mlt[2] += mlt[2]
        # Sort Transaction by Date
        self.mlt[1].sort(key=lambda x: x[mlt[0].index("mlt_trdt")])

    def doHeader(self, mlm, dmc, tdc):
        self.form.add_page()
        if "account_details" in self.form.tptp:
            tit = mlm[dmc.index("mlm_title")]
            sur = mlm[dmc.index("mlm_surname")]
            nam = mlm[dmc.index("mlm_names")]
            ini = ""
            for n, d in enumerate(nam.split()):
                if n < 3:
                    if not ini:
                        ini = d[0].upper()
                    else:
                        ini = "%s %s" % (ini, d[0].upper())
            nad = "%s %s %s" % (tit.strip(), ini.strip(), sur.strip())
            add = self.sql.getRec("memadd",
                                  where=[("mla_cono", "=", self.opts["conum"]),
                                         ("mla_memno", "=", self.memno),
                                         ("mla_type", "=", "P")],
                                  limit=1)
            adc = self.sql.memadd_col
            if add:
                if add[adc.index("mla_add1")]:
                    nad = "%s\n%s" % (nad, add[adc.index("mla_add1")])
                if add[adc.index("mla_add2")]:
                    nad = "%s\n%s" % (nad, add[adc.index("mla_add2")])
                if add[adc.index("mla_add3")]:
                    nad = "%s\n%s" % (nad, add[adc.index("mla_add3")])
                if add[adc.index("mla_city")]:
                    nad = "%s\n%s" % (nad, add[adc.index("mla_city")])
                if add[adc.index("mla_country")]:
                    nad = "%s\n%-s, %-s" % (nad, add[adc.index("mla_code")],
                                            add[adc.index("mla_country")])
                else:
                    nad = "%s\n%s" % (nad, add[adc.index("mla_code")])
            else:
                nad = "\n\n\n\n\n"
            self.form.newdic["account_details_C00"][tdc.index(
                "tpd_text")] = nad
        for key in self.form.newkey:
            nl = copyList(self.form.newdic[key])
            if nl[tdc.index("tpd_place")] != "A":
                continue
            if nl[tdc.index("tpd_detseq")] == "mlm_memno_C00":
                nl[tdc.index("tpd_text")] = self.memno
            self.form.doDrawDetail(nl)

    def doBody(self, mlc, mlt, tdc):
        text = self.form.sql.tpldet_col.index("tpd_text")
        self.tots = [0.0, 0.0, 0.0]
        count = 0
        for trans in mlt:
            if trans[mlc.index("mlt_type")] not in (5, 6) and \
                    trans[mlc.index("mlt_curdt")] == self.curdt:
                if self.paid == "Y":
                    self.tots[1] = float(ASD(self.tots[1]) + \
                        ASD(trans[mlc.index("mlt_tramt")]))
                else:
                    self.tots[1] = float(ASD(self.tots[1]) + \
                        ASD(trans[mlc.index("balance")]))
                if trans[mlc.index("mlt_taxamt")]:
                    self.tots[1] = float(ASD(self.tots[1]) - \
                        ASD(trans[mlc.index("mlt_taxamt")]))
                    self.tots[2] = float(ASD(self.tots[2]) + \
                        ASD(trans[mlc.index("mlt_taxamt")]))
            else:
                self.tots[0] = float(ASD(self.tots[0]) + \
                    ASD(trans[mlc.index("balance")]))
            for cod in self.form.body:
                if cod == "type_code":
                    c = "mlt_type"
                elif self.paid == "N" and cod == "mlt_tramt":
                    c = "balance"
                else:
                    c = cod
                d = "%s_C%02i" % (cod, count)
                if cod == "type_code":
                    ttyp = trans[mlc.index(c)]
                    self.form.newdic[d][text] = mltrtp[ttyp - 1][0]
                else:
                    self.form.newdic[d][text] = trans[mlc.index(c)]
                self.form.doDrawDetail(self.form.newdic[d])
            count += 1
        for x in range(count, self.form.maxlines):
            for cod in self.form.body:
                d = "%s_C%02i" % (cod, x)
                self.form.newdic[d][tdc.index("tpd_text")] = "BLANK"
                self.form.doDrawDetail(self.form.newdic[d])

    def doTotal(self, tdc):
        for c in self.form.total:
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                line = self.form.newdic[d]
                if c == "120_day_balance":
                    line[tdc.index("tpd_text")] = self.ages[4]
                elif c == "90_day_balance":
                    line[tdc.index("tpd_text")] = self.ages[3]
                elif c == "60_day_balance":
                    line[tdc.index("tpd_text")] = self.ages[2]
                elif c == "30_day_balance":
                    line[tdc.index("tpd_text")] = self.ages[1]
                elif c == "current_balance":
                    line[tdc.index("tpd_text")] = self.ages[0]
                elif c == "total_arrears":
                    line[tdc.index("tpd_text")] = self.tots[0]
                elif c == "month_exclusive":
                    line[tdc.index("tpd_text")] = self.tots[1]
                elif c == "month_tax":
                    line[tdc.index("tpd_text")] = self.tots[2]
                elif c == "total_balance":
                    line[tdc.index("tpd_text")] = self.tbal
                self.form.doDrawDetail(line)

    def doTail(self, tdc):
        for c in self.form.tail:
            if c == "message" and not self.mesno:
                continue
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                if d == "message_C00" and self.mesno:
                    mes = self.sql.getRec("ctlmes",
                                          cols=["mss_detail"],
                                          where=[("mss_system", "=", "MEM"),
                                                 ("mss_message", "=",
                                                  self.mesno)],
                                          limit=1)
                    self.form.newdic[d][tdc.index("tpd_text")] = mes[0]
                self.form.doDrawDetail(self.form.newdic[d])

    def doPrint(self):
        if self.repeml[1] == "Y" and not self.emadd:
            key = "%s_%s" % (self.opts["conum"], self.memno)
        else:
            key = "%s_all" % self.opts["conum"]
        pdfnam = getModName(self.opts["mf"].rcdic["wrkdir"],
                            self.__class__.__name__,
                            key,
                            ext="pdf")
        self.form.output(pdfnam, "F")
        doPrinter(mf=self.opts["mf"],
                  conum=self.opts["conum"],
                  pdfnam=pdfnam,
                  header="%s Statement at %s" %
                  (self.opts["conam"], self.dated),
                  fromad=self.fromad,
                  repprt=self.repprt,
                  repeml=self.repeml)
        if self.repeml[1] == "Y":
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()

    def doExit(self):
        self.df.closeProcess()
        if "wait" not in self.opts:
            self.opts["mf"].closeLoop()
Ejemplo n.º 2
0
class cr3080(object):
    def __init__(self, **opts):
        self.opts = opts
        if self.setVariables():
            self.mainProcess()
            if "wait" in self.opts:
                self.df.mstFrame.wait_window()
            else:
                self.opts["mf"].startLoop()

    def setVariables(self):
        gc = GetCtl(self.opts["mf"])
        crsctl = gc.getCtl("crsctl", self.opts["conum"])
        if not crsctl:
            return
        t = time.localtime()
        self.sysdtw = (t[0] * 10000) + (t[1] * 100) + t[2]
        self.sql = Sql(self.opts["mf"].dbm, [
            "ctlctl", "ctlmst", "crsctl", "crsmst", "crstrn", "crsage",
            "gentrn", "tplmst"
        ],
                       prog=self.__class__.__name__)
        if self.sql.error:
            return
        self.glint = crsctl["ctc_glint"]
        self.bestac = crsctl["ctc_bestac"]
        self.besttp = crsctl["ctc_besttp"]
        self.bankac = crsctl["ctc_bankac"]
        self.tplnam = crsctl["ctc_tplnam"]
        self.fromad = crsctl["ctc_emadd"]
        if self.glint == "Y":
            ctlctl = gc.getCtl("ctlctl", self.opts["conum"])
            if not ctlctl:
                return
            if gc.chkRec(self.opts["conum"], ctlctl, ["crs_ctl"]):
                return
            self.crsctl = ctlctl["crs_ctl"]
        acc = self.sql.getRec("crstrn",
                              cols=["max(crt_ref1)"],
                              where=[("crt_cono", "=", self.opts["conum"]),
                                     ("crt_type", "=", 5),
                                     ("crt_ref1", "like", "EFT______")],
                              limit=1)
        try:
            self.cats = int(acc[0][3:])
            self.refs = int(acc[0][3:]) + 1
        except:
            self.cats = 0
            self.refs = 1
        self.etotal = 0
        return True

    def mainProcess(self):
        tpm = {
            "stype":
            "R",
            "tables": ("tplmst", ),
            "cols": (("tpm_tname", "", 0, "Template"), ("tpm_title", "", 0,
                                                        "Title", "Y")),
            "where": [("tpm_type", "=", "R"), ("tpm_system", "=", "CRS")],
            "order":
            "tpm_tname"
        }
        crm = {
            "stype":
            "R",
            "tables": ("crsmst", ),
            "cols":
            (("crm_acno", "", 0, "Acc-Num"), ("crm_name", "", 0, "Name", "Y")),
            "where": [("crm_cono", "=", self.opts["conum"]),
                      ("crm_stat", "<>", "X")]
        }
        r1s = (("Yes", "Y"), ("Range", "R"), ("Singles", "S"))
        r2s = (("Yes", "Y"), ("No", "N"))
        r3s = (("Number", "N"), ("Name", "M"), ("Postal Code", "P"))
        r4s = (("Monthly", "M"), ("Daily", "D"))
        fld = ((("T", 0, 0, 0), "INA", 20, "Template Name", "", self.tplnam,
                "Y", self.doTplNam, tpm, None, None),
               (("T", 0, 1, 0), ("IRB", r1s), 0, "Whole File", "", "S", "Y",
                self.doWhole, None, None, None), [["T", 0, 2, 0], "INA", 7,
                                                  "From Account", "", "", "Y",
                                                  self.doAcc, crm, None, None],
               [["T", 0, 3, 0], "INA", 7, "To   Account", "", "", "Y",
                self.doAcc, crm, None,
                None], (("T", 0, 4, 0), ("IRB", r3s), 0, "Sort Order", "", "N",
                        "Y", self.doSort, None, None, None),
               (("T", 0, 5, 0), ("IRB", r4s), 0, "Terms Base", "", "M", "Y",
                self.doFrequency, None, None,
                None), (("T", 0, 6, 0), ("IRB", r2s), 0, "Exceptions", "", "N",
                        "Y", self.doExcepts, None, None, None),
               (("T", 0, 7, 0), "ID1", 10, "Due Date", "", self.sysdtw, "Y",
                self.doDuedat, None, None, ("efld", )),
               (("T", 0, 8, 0), "ID1", 10, "Payment Date", "", self.sysdtw,
                "Y", self.doPaydat, None, None, ("efld", )))
        tnd = ((self.doEnd, "Y"), )
        txt = (self.doExit, )
        self.df = TartanDialog(self.opts["mf"],
                               eflds=fld,
                               tend=tnd,
                               txit=txt,
                               view=("N", "V"),
                               mail=("B", "Y"))

    def doTplNam(self, frt, pag, r, c, p, i, w):
        acc = self.sql.getRec("tplmst",
                              where=[("tpm_tname", "=", w),
                                     ("tpm_type", "=", "R"),
                                     ("tpm_system", "=", "CRS")],
                              limit=1)
        if not acc:
            return "Invalid Template Name"
        self.tname = w

    def doWhole(self, frt, pag, r, c, p, i, w):
        self.whole = w
        if self.whole in ("Y", "S"):
            self.sacc = ""
            self.eacc = ""
            self.df.loadEntry("T", 0, p + 1, data=self.sacc)
            self.df.loadEntry("T", 0, p + 2, data=self.eacc)
            return "sk2"

    def doAcc(self, frt, pag, r, c, p, i, w):
        chk = self.sql.getRec("crsmst",
                              cols=["crm_name", "crm_stat"],
                              where=[("crm_cono", "=", self.opts["conum"]),
                                     ("crm_acno", "=", w)],
                              limit=1)
        if not chk:
            return "Invalid Creditors Account"
        if not chk[1] == "X":
            return "Invalid Account, Redundant"
        if c == 3:
            self.sacc = w
        else:
            self.eacc = w

    def doSort(self, frt, pag, r, c, p, i, w):
        self.sort = w

    def doFrequency(self, frt, pag, r, c, p, i, w):
        self.freq = w

    def doExcepts(self, frt, pag, r, c, p, i, w):
        self.excepts = w

    def doDuedat(self, frt, pag, r, c, p, i, w):
        self.duedtw = w
        self.duedtd = self.df.t_disp[pag][0][p]

    def doPaydat(self, frt, pag, r, c, p, i, w):
        self.paydtw = w
        self.paydtd = self.df.t_disp[pag][0][p]
        self.curdt = int(self.paydtw / 100)
        self.batno = "E%s" % self.curdt

    def doEnd(self):
        self.df.closeProcess()
        if self.excepts == "Y":
            self.doExceptions()
        self.emadd = self.df.repeml[2]
        if self.bestac:
            self.export = open(
                os.path.join(
                    self.opts["mf"].rcdic["wrkdir"],
                    "best%03d_%s.txt" % (self.opts["conum"], self.paydtw)),
                "w")
            # Header for BEST
            self.export.write("%1s%4s%-40s%8s%1s%8s%-15s%1s%2s%1s%9s%2s%4s"\
                "\r\n" % ("*", self.bestac, self.opts["conam"], self.paydtw,
                "Y", "", "CREDITORS EFT", "+", self.besttp, 0, "", "01",
                "LIVE"))
        else:
            self.export = None
        if self.whole == "S":
            recs = getSingleRecords(self.opts["mf"],
                                    "crsmst", ("crm_acno", "crm_name"),
                                    where=[("crm_cono", "=",
                                            self.opts["conum"]),
                                           ("crm_termsb", "=", self.freq),
                                           ("crm_pyind", "<>", "N"),
                                           ("crm_stat", "<>", "X")])
        else:
            if not self.eacc:
                self.eacc = "zzzzzzz"
            whr = [("crm_cono", "=", self.opts["conum"]),
                   ("crm_acno", "between", self.sacc, self.eacc),
                   ("crm_termsb", "=", self.freq), ("crm_pyind", "<>", "N"),
                   ("crm_stat", "<>", "X")]
            if self.sort == "N":
                odr = "crm_acno"
            elif self.sort == "M":
                odr = "crm_name"
            else:
                odr = "crm_pcod"
            recs = self.sql.getRec("crsmst", where=whr, order=odr)
            if not recs:
                showError(self.opts["mf"].body, "Error",
                          "No Accounts Selected")
        if recs:
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()
            p = ProgressBar(self.opts["mf"].body, mxs=len(recs), esc=True)
            for num, rec in enumerate(recs):
                p.displayProgress(num)
                if p.quit:
                    self.opts["mf"].dbm.rollbackDbase()
                    break
                self.doProcess(rec)
            p.closeProgress()
            if p.quit or not self.form.page:
                pass
            elif self.df.repeml[1] == "N" or self.emadd:
                self.df.repeml[2] = self.emadd
                self.doPrint()
            if self.bestac:
                # Trailer for BEST
                value = int(round((self.etotal * 100), 0))
                self.export.write("%1s%4s%1s%30s%013u%47s\r\n" % \
                    (2, self.bestac, "T", "", value, ""))
                self.export.close()
                if self.glint == "Y" and self.etotal:
                    # Create total transactions in GL
                    data = [
                        self.opts["conum"], self.crsctl, self.curdt,
                        self.paydtw, 2, self.refno, self.batno, self.etotal,
                        0.0,
                        "Payment EFT%06i to EFT%06i" % (self.refs, self.cats),
                        "", "", 0, self.opts["capnm"], self.sysdtw, 0
                    ]
                    self.sql.insRec("gentrn", data=data)
                    data[1] = self.bankac
                    data[7] = float(ASD(0) - ASD(self.etotal))
                    self.sql.insRec("gentrn", data=data)
                self.opts["mf"].dbm.commitDbase(
                    ask=True,
                    mess="""Would you like to commit all elecronic payments?

If you decide to do this, you must remember to upload the BEST file to the Bank otherwise you are NOT going to Reconcile!""",
                    default="no")
        if "wait" not in self.opts:
            self.opts["mf"].closeLoop()

    def doExceptions(self):
        crm = {
            "stype":
            "R",
            "tables": ("crsmst", ),
            "cols":
            (("crm_acno", "", 0, "Acc-Num"), ("crm_name", "", 0, "Name", "Y")),
            "where": [("crm_cono", "=", self.opts["conum"]),
                      ("crm_termsb", "=", self.freq), ("crm_stat", "<>", "X")]
        }
        crt = {
            "stype":
            "R",
            "tables": ("crstrn", ),
            "cols":
            (("crt_ref1", "", 0, "Reference", "Y"), ("crt_type",
                                                     ("XX", crtrtp), 3, "Typ"),
             ("crt_trdt", "", 0, "Date"), ("crt_tramt", "", 0, "    Amount"),
             ("paid", "SD", 13.2,
              "      Paid"), ("balance", "SD", 13.2,
                              "   Balance"), ("crt_paydt", "", 0, "Pay-Date"),
             ("crt_payind", "", 0, "I"), ("crt_payamt", "", 0, "  Pay-Amnt")),
            "wtype":
            "D",
            "where": [],
            "order":
            "crt_ref1"
        }
        types = []
        for x in range(1, len(crtrtp) + 1):
            types.append((x, crtrtp[x - 1][1]))
        typ = {
            "stype": "C",
            "titl": "Select the Required Type",
            "head": ("C", "Type"),
            "data": types
        }
        fld = ((("T", 0, 0,
                 0), "I@crm_acno", 0, "", "", "", "N", self.doExAcNo, crm,
                None, ("notblank", )), (("T", 0, 0, 0), "O@crm_name", 0, ""),
               (("C", 0, 0, 1), "I@crt_type", 0, "", "", "", "N",
                self.doExTrnTyp, typ, None, ("in", (1, 2, 3, 4, 5))),
               (("C", 0, 0, 0), "I@crt_ref1", 0, "", "", "", "N",
                self.doExTrnRef, crt, None, ("notblank", )),
               (("C", 0, 0, 2), "O@crt_trdt", 0, ""), (("C", 0, 0, 3), "OSD",
                                                       13.2, "Balance"),
               (("C", 0, 0, 4), "I@crt_payind", 0, "", "", "", "N",
                self.doExInd, None, None, ("in", ("Y", "N"))),
               (("C", 0, 0, 5), "I@crt_paydt", 0, "", "", "", "N",
                self.doExDte, None, None,
                ("efld", )), (("C", 0, 0, 6), "I@crt_payamt", 0, "", "", "",
                              "N", self.doExAmt, None, None, ("efld", )))
        tnd = ((self.doExEndTop, "n"), )
        txt = (self.doExExitTop, )
        cnd = ((self.doExEndCol, "y"), )
        cxt = (self.doExExitCol, )
        self.ex = TartanDialog(self.opts["mf"],
                               eflds=fld,
                               tend=tnd,
                               txit=txt,
                               cend=cnd,
                               cxit=cxt)
        self.ex.mstFrame.wait_window()

    def doExAcNo(self, frt, pag, r, c, p, i, w):
        acc = self.sql.getRec(
            "crsmst",
            cols=["crm_name", "crm_termsb", "crm_pyind", "crm_stat"],
            where=[("crm_cono", "=", self.opts["conum"]),
                   ("crm_acno", "=", w)],
            limit=1)
        if not acc:
            return "Invalid Account Number"
        if acc[1] != self.freq:
            return "Invalid Terms Base"
        if acc[2] == "N":
            return "Invalid Payment Indicator"
        if acc[3] == "X":
            return "Invalid Account, Redundant"
        self.exacc = w
        self.ex.loadEntry(frt, pag, p + 1, data=acc[0])

    def doExEndTop(self):
        self.ex.focusField("C", 0, 1)

    def doExExitTop(self):
        self.opts["mf"].dbm.commitDbase(
            ask=True, mess="Would you like to commit these exceptions?")
        self.ex.closeProcess()

    def doExTrnTyp(self, frt, pag, r, c, p, i, w):
        self.extyp = w
        data = []
        # Build the data for the F1 choice selection
        col, dat = getTrn(self.opts["mf"].dbm,
                          "crs",
                          whr=[("crt_cono", "=", self.opts["conum"]),
                               ("crt_acno", "=", self.exacc),
                               ("crt_type", "=", w)],
                          zer="N")
        if dat:
            cols = ("crt_ref1", "crt_type", "crt_trdt", "crt_tramt", "paid",
                    "balance", "crt_paydt", "crt_payind", "crt_payamt")
            for d in dat:
                rec = []
                for cc in cols:
                    rec.append(d[col.index(cc)])
                data.append(rec)
        self.ex.colf[0][1][8]["where"] = data

    def doExTrnRef(self, frt, pag, r, c, p, i, w):
        col = ["crt_trdt", "balance", "crt_payind", "crt_paydt", "crt_payamt"]
        whr = [("crt_cono", "=", self.opts["conum"]),
               ("crt_acno", "=", self.exacc), ("crt_type", "=", self.extyp),
               ("crt_ref1", "=", w)]
        c, d = getTrn(self.opts["mf"].dbm, "crs", whr=whr, lim=1)
        if not d:
            return "Invalid Transaction Number"
        if not d[0][c.index("balance")]:
            return "Transaction Has No Balance"
        self.exref = w
        self.exdte = d[0][c.index("crt_paydt")]
        self.examt = d[0][c.index("crt_payamt")]
        for pos, fld in enumerate(col):
            self.ex.loadEntry(frt, pag, p + 1 + pos, data=d[0][c.index(fld)])

    def doExInd(self, frt, pag, r, c, p, i, w):
        self.exind = w
        if self.exind == "N":
            return "nd"

    def doExDte(self, frt, pag, r, c, p, i, w):
        self.exdte = w

    def doExAmt(self, frt, pag, r, c, p, i, w):
        self.examt = w

    def doExEndCol(self):
        # Update Transaction"
        self.sql.updRec("crstrn",
                        cols=["crt_payind", "crt_paydt", "crt_payamt"],
                        data=[self.exind, self.exdte, self.examt],
                        where=[("crt_cono", "=", self.opts["conum"]),
                               ("crt_acno", "=", self.exacc),
                               ("crt_type", "=", self.extyp),
                               ("crt_ref1", "=", self.exref)])
        self.ex.advanceLine(0)

    def doExExitCol(self):
        self.ex.focusField("T", 0, 1)

    def doLoadStatic(self):
        cmc = self.sql.ctlmst_col
        ctm = self.sql.getRec("ctlmst",
                              where=[("ctm_cono", "=", self.opts["conum"])],
                              limit=1)
        for fld in cmc:
            dat = ctm[cmc.index(fld)]
            if fld in self.form.tptp:
                if fld == "ctm_logo":
                    self.form.letterhead(cmc, ctm, fld, dat)
                    continue
                self.form.tptp[fld][1] = dat
        if "letterhead" in self.form.tptp:
            self.form.letterhead(cmc, ctm, "letterhead", None)
        self.form.document_date(self.duedtd)

    def doProcess(self, crm):
        cmc = self.sql.crsmst_col
        tdc = self.form.sql.tpldet_col
        self.acno = crm[cmc.index("crm_acno")]
        eml = crm[cmc.index("crm_acc_email")]
        self.form.account_details("crm", cmc, crm, 1)
        for col in cmc:
            d = "%s_C00" % col
            if d in self.form.newdic:
                dat = crm[cmc.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        jon = "cra_curdt <= %s" % self.curdt
        whr = [("crt_cono", "=", self.opts["conum"]),
               ("crt_acno", "=", self.acno), ("crt_payind", "=", "Y"),
               ("crt_paydt", "<=", self.duedtw)]
        ctc, crt = getTrn(self.opts["mf"].dbm,
                          "crs",
                          jon=jon,
                          whr=whr,
                          zer="N")
        if not crt:
            return
        bal = 0
        self.pay = 0
        for d in crt:
            if d[ctc.index("balance")] < 0:
                d[ctc.index("crt_payamt")] = d[ctc.index("balance")]
            bal = float(ASD(bal) + ASD(d[ctc.index("balance")]))
            self.pay = float(ASD(self.pay) + ASD(d[ctc.index("crt_payamt")]))
        if self.pay > bal:
            self.pay = bal
        if self.pay > 0:
            self.bname = crm[cmc.index("crm_bname")]
            self.bibt = crm[cmc.index("crm_bibt")]
            self.bacc = crm[cmc.index("crm_bacc")]
            if self.bname and self.bibt and self.bacc:
                self.ptype = "E"  # Electronic
                test = False
                while not test:
                    self.cats += 1
                    self.refno = "EFT%06i" % self.cats
                    # Check if Reference Number Already Exists
                    chk = self.sql.getRec("crstrn",
                                          where=[("crt_cono", "=",
                                                  self.opts["conum"]),
                                                 ("crt_acno", "=", self.acno),
                                                 ("crt_type", "=", 5),
                                                 ("crt_ref1", "=", self.refno)
                                                 ])
                    if not chk:
                        test = True
            else:
                self.ptype = "C"  # Cheque
            self.doBody(ctc, crt, tdc)
            self.doTotal(tdc)
            self.doTail(tdc)
            if self.df.repeml[1] == "Y" and not self.emadd:
                self.df.repeml[2] = eml
                self.doPrint()

    def doPrint(self):
        if self.df.repeml[1] == "Y" and not self.emadd:
            key = "%s_%s" % (self.opts["conum"], self.acno)
        else:
            key = "%s_all" % self.opts["conum"]
        pdfnam = getModName(self.opts["mf"].rcdic["wrkdir"],
                            self.__class__.__name__,
                            key,
                            ext="pdf")
        self.form.output(pdfnam, "F")
        doPrinter(mf=self.opts["mf"],
                  conum=self.opts["conum"],
                  pdfnam=pdfnam,
                  header="%s Remittance Advice" % self.opts["conam"],
                  repprt=self.df.repprt,
                  fromad=self.fromad,
                  repeml=self.df.repeml)
        if self.df.repeml[1] == "Y":
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()

    def doBody(self, ctc, crt, tdc):
        page = 0
        count = 0
        text = self.form.sql.tpldet_col.index("tpd_text")
        for trans in crt:
            trbal = trans[ctc.index("balance")]
            payamt = trans[ctc.index("crt_payamt")]
            if payamt > trbal:
                payamt = trbal
            trans[ctc.index("balance")] = payamt
            if not count:
                page += 1
                count = self.doHeader(page)
            if count == self.form.maxlines:
                page = self.doCfwd(page)
                count = self.doHeader(page)
            for cod in self.form.body:
                if cod == "type_code":
                    c = "crt_type"
                elif cod == "line_paid":
                    c = "paid"
                elif cod == "line_balance":
                    c = "balance"
                else:
                    c = cod
                d = "%s_C%02i" % (cod, count)
                if cod == "type_code":
                    ttyp = trans[ctc.index(c)]
                    self.form.newdic[d][text] = crtrtp[ttyp - 1][0]
                else:
                    self.form.newdic[d][text] = trans[ctc.index(c)]
                self.form.doDrawDetail(self.form.newdic[d])
            if self.ptype == "E" and self.bestac:
                trtp = trans[ctc.index("crt_type")]
                ref1 = trans[ctc.index("crt_ref1")]
                # Create Ageing Transaction
                self.sql.insRec("crsage",
                                data=[
                                    self.opts["conum"], self.acno, trtp, ref1,
                                    self.curdt, 5, self.refno, payamt, 0
                                ])
            count += 1
        for x in range(count, self.form.maxlines):
            for cod in self.form.body:
                d = "%s_C%02i" % (cod, x)
                self.form.newdic[d][tdc.index("tpd_text")] = "BLANK"
                self.form.doDrawDetail(self.form.newdic[d])

    def doHeader(self, page):
        tdc = self.form.sql.tpldet_col
        self.form.add_page()
        for key in self.form.newkey:
            nl = copyList(self.form.newdic[key])
            if nl[tdc.index("tpd_place")] != "A":
                continue
            if nl[tdc.index("tpd_detseq")] == "crm_acno_C00":
                nl[tdc.index("tpd_text")] = self.acno
            elif nl[tdc.index("tpd_detseq")] == "page_number_C00":
                nl[tdc.index("tpd_text")] = str(page)
            self.form.doDrawDetail(nl)
        return 0

    def doCfwd(self, page):
        if "carried_forward" in self.form.tptp:
            tdc = self.form.sql.tpldet_col
            line = copyList(self.form.cfwd)
            line[tdc.index("tpd_text")] = "Continued on Page %i" % (page + 1)
            self.form.doDrawDetail(line)
        return page + 1

    def doTotal(self, tdc):
        for c in self.form.total:
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                line = self.form.newdic[d]
                if c == "total_payment":
                    line[tdc.index("tpd_text")] = self.pay
                self.form.doDrawDetail(line)
        if self.ptype == "E" and self.bestac:
            value = int(round((self.pay * 100), 0))
            self.export.write("%1s%4s%06u%-7s%019u%1s%1s%011u%-20s%10s"\
                "%-15s%1s\r\n" % (2, self.bestac, self.bibt, self.acno,
                int(self.bacc), "", "1", value, self.bname, "",
                self.opts["conam"][:15], ""))
            self.etotal = float(ASD(self.etotal) + ASD(self.pay))
            # Create Payment and Ageing Transaction
            p = float(ASD(0) - ASD(self.pay))
            self.sql.insRec("crstrn",
                            data=[
                                self.opts["conum"], self.acno, 5, self.refno,
                                self.batno, self.paydtw, "", p, 0.0, 0.0,
                                self.curdt, 0, "", 0.0, "Electronic Payment",
                                "", "", self.opts["capnm"], self.sysdtw, 0
                            ])
            self.sql.insRec("crsage",
                            data=[
                                self.opts["conum"], self.acno, 5, self.refno,
                                self.curdt, 5, self.refno, p, 0
                            ])

    def doTail(self, tdc):
        for c in self.form.tail:
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                line = self.form.newdic[d]
                if c == "eft_message" and self.ptype == "E" and self.bestac:
                    line[tdc.index("tpd_text")] = "ELECTRONIC TRANSFER AT %s "\
                        "REFERENCE %s" % (self.paydtd, self.refno)
                self.form.doDrawDetail(line)

    def doExit(self):
        self.df.closeProcess()
        if "wait" not in self.opts:
            self.opts["mf"].closeLoop()
Ejemplo n.º 3
0
class rc3060(object):
    def __init__(self, **opts):
        self.opts = opts
        if self.setVariables():
            if "args" in self.opts:
                self.own, self.prm, self.acc, self.datew, self.dated, \
                    self.repprt, self.repeml = self.opts["args"]
                self.emadd = self.repeml[2]
                self.tname = self.stpl
                self.zeros = "Y"
                self.minus = "Y"
                self.mesno = 0
                whr = [("rtn_cono", "=", self.opts["conum"]),
                       ("rtn_owner", "=", self.own),
                       ("rtn_code", "=", self.prm),
                       ("rtn_acno", "=", self.acc)]
                rtn = self.sql.getRec("rcatnm", where=whr, limit=1)
                self.form = DrawForm(self.opts["mf"].dbm,
                                     self.tname,
                                     wrkdir=self.opts["mf"].rcdic["wrkdir"])
                self.doLoadStatic()
                self.form.doNewDetail()
                self.doProcess(rtn)
                if self.form.page and (self.repeml[1] == "N" or self.emadd):
                    self.repeml[2] = self.emadd
                    self.doPrint()
            else:
                self.mainProcess()
                self.opts["mf"].startLoop()

    def setVariables(self):
        self.sql = Sql(self.opts["mf"].dbm, [
            "ctlpwu", "ctlmes", "ctlmst", "rcatnm", "rcatnt", "rcaprm",
            "rcacon", "tplmst"
        ],
                       prog=self.__class__.__name__)
        if self.sql.error:
            return
        gc = GetCtl(self.opts["mf"])
        rcactl = gc.getCtl("rcactl", self.opts["conum"])
        if not rcactl:
            return
        self.stpl = rcactl["cte_tpltnt"]
        usr = self.sql.getRec("ctlpwu",
                              cols=["usr_emadd"],
                              where=[("usr_name", "=", self.opts["capnm"])],
                              limit=1)
        if usr[0]:
            self.fromad = usr[0]
        else:
            self.fromad = rcactl["cte_emadd"]
        t = time.localtime()
        self.sysdtw = (t[0] * 10000) + (t[1] * 100) + t[2]
        return True

    def mainProcess(self):
        tpm = {
            "stype":
            "R",
            "tables": ("tplmst", ),
            "cols": (("tpm_tname", "", 0, "Template"), ("tpm_title", "", 0,
                                                        "Title", "Y")),
            "where": [("tpm_type", "=", "S"), ("tpm_system", "=", "RCA"),
                      ("tpm_sttp", "=", "T")],
            "order":
            "tpm_tname"
        }
        mss = {
            "stype":
            "R",
            "tables": ("ctlmes", ),
            "cols": (("mss_message", "", 0, "Num"), ("mss_detail", "NA", 50,
                                                     "Details")),
            "where": [("mss_system", "=", "RCA")]
        }
        r1s = (("Yes", "Y"), ("No", "N"))
        fld = ((("T", 0, 0, 0), "INA", 20, "Template Name", "", self.stpl, "Y",
                self.doTplNam, tpm, None, None),
               (("T", 0, 1,
                 0), ("IRB", r1s), 0, "Whole File", "", "Y", "Y", self.doWhole,
                None, None, None), (("T", 0, 2, 0), ("IRB", r1s), 0,
                                    "Zero Balances", "Include Zero Balances",
                                    "N", "Y", self.doZeros, None, None, None),
               (("T", 0, 3, 0), ("IRB", r1s), 0, "Minus Balances",
                "Include Minus Balances", "N", "Y", self.doMinus, None, None,
                None), (("T", 0, 4, 0), "IUI", 3, "Message Number", "", "",
                        "Y", self.doMessno, mss, None, ("efld", )),
               (("T", 0, 5, 0), "ID1", 10, "Statement Date", "", self.sysdtw,
                "Y", self.doDat, None, None, ("efld", )))
        tnd = ((self.doEnd, "Y"), )
        txt = (self.doExit, )
        self.df = TartanDialog(self.opts["mf"],
                               eflds=fld,
                               tend=tnd,
                               txit=txt,
                               view=("N", "V"),
                               mail=("B", "Y"))

    def doTplNam(self, frt, pag, r, c, p, i, w):
        acc = self.sql.getRec("tplmst",
                              where=[("tpm_tname", "=", w),
                                     ("tpm_type", "=", "S"),
                                     ("tpm_system", "=", "RCA"),
                                     ("tpm_sttp", "=", "T")],
                              limit=1)
        if not acc:
            return "Invalid Template Name"
        self.tname = w

    def doWhole(self, frt, pag, r, c, p, i, w):
        self.whole = w

    def doZeros(self, frt, pag, r, c, p, i, w):
        self.zeros = w

    def doMinus(self, frt, pag, r, c, p, i, w):
        self.minus = w

    def doMessno(self, frt, pag, r, c, p, i, w):
        if w:
            acc = self.sql.getRec("ctlmes",
                                  cols=["mss_detail"],
                                  where=[("mss_system", "=", "RCA"),
                                         ("mss_message", "=", w)],
                                  limit=1)
            if not acc:
                return "Invalid Message Number"
        self.mesno = w

    def doDat(self, frt, pag, r, c, p, i, w):
        self.datew = w
        self.dated = self.df.t_disp[pag][0][p]

    def doEnd(self):
        self.df.closeProcess()
        self.repprt = self.df.repprt
        self.repeml = self.df.repeml
        self.emadd = self.df.repeml[2]
        if self.whole == "N":
            recs = getSingleRecords(
                self.opts["mf"],
                "rcatnm", ("rtn_owner", "rtn_code", "rtn_acno", "rtn_name"),
                where=[("rtn_cono", "=", self.opts["conum"])])
        else:
            whr = [("rtn_cono", "=", self.opts["conum"])]
            odr = "rtn_owner, rtn_code, rtn_acno"
            recs = self.sql.getRec("rcatnm", where=whr, order=odr)
            if not recs:
                showError(self.opts["mf"].body, "Error",
                          "No Accounts Selected")
        if recs:
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()
            p = ProgressBar(self.opts["mf"].body, mxs=len(recs), esc=True)
            for num, rec in enumerate(recs):
                p.displayProgress(num)
                if p.quit:
                    break
                self.doProcess(rec)
            p.closeProgress()
            if p.quit or not self.form.page:
                pass
            elif self.repeml[1] == "N" or self.emadd:
                self.df.repeml[2] = self.emadd
                self.doPrint()
        self.opts["mf"].closeLoop()

    def doLoadStatic(self):
        cmc = self.sql.ctlmst_col
        ctm = self.sql.getRec("ctlmst",
                              where=[("ctm_cono", "=", self.opts["conum"])],
                              limit=1)
        for fld in cmc:
            dat = ctm[cmc.index(fld)]
            if fld in self.form.tptp:
                if fld == "ctm_logo":
                    self.form.letterhead(cmc, ctm, fld, dat)
                    continue
                self.form.tptp[fld][1] = dat
        if "letterhead" in self.form.tptp:
            self.form.letterhead(cmc, ctm, "letterhead", None)
        self.form.document_date(self.dated)

    def doProcess(self, rtn):
        tmc = self.sql.rcatnm_col
        tdc = self.form.sql.tpldet_col
        self.own = rtn[tmc.index("rtn_owner")]
        self.prm = rtn[tmc.index("rtn_code")]
        self.acc = rtn[tmc.index("rtn_acno")]
        eml = rtn[tmc.index("rtn_email")]
        acc = self.sql.getRec("rcacon",
                              cols=["rcc_cnum", "rcc_deposit", "rcc_status"],
                              where=[("rcc_cono", "=", self.opts["conum"]),
                                     ("rcc_owner", "=", self.own),
                                     ("rcc_code", "=", self.prm),
                                     ("rcc_acno", "=", self.acc)],
                              order="rcc_cnum")
        if acc:
            dps, dep, sta = acc[-1:][0]
        else:
            dps, dep, sta = 0, 0.0, ""
        if sta != "C":
            dep = 0.0
            dos = 0.0
        else:
            dos = dep
        self.tots = [0.0, dep, 0.0, 0.0, 0.0, dep]
        tots = self.sql.getRec(
            "rcatnt",
            cols=["rtu_type", "rtu_mtyp", "round(sum(rtu_tramt), 2)"],
            where=[("rtu_cono", "=", self.opts["conum"]),
                   ("rtu_owner", "=", self.own), ("rtu_code", "=", self.prm),
                   ("rtu_acno", "=", self.acc),
                   ("rtu_trdt", "<=", self.datew)],
            group="rtu_type, rtu_mtyp")
        self.dpp = 0.0
        for t in tots:
            if t[1] == 1:
                self.tots[0] = float(ASD(self.tots[0]) + ASD(t[2]))
                self.tots[5] = float(ASD(self.tots[5]) + ASD(t[2]))
            elif t[1] == 2:
                self.dpp = float(ASD(self.dpp) + ASD(t[2]))
                if dep:
                    dos = float(ASD(dos) + ASD(t[2]))
                    self.tots[1] = float(ASD(self.tots[1]) + ASD(t[2]))
                    self.tots[5] = float(ASD(self.tots[5]) + ASD(t[2]))
            elif t[1] == 3:
                self.tots[2] = float(ASD(self.tots[2]) + ASD(t[2]))
                self.tots[5] = float(ASD(self.tots[5]) + ASD(t[2]))
            elif t[1] in (4, 5):
                self.tots[3] = float(ASD(self.tots[3]) + ASD(t[2]))
                self.tots[5] = float(ASD(self.tots[5]) + ASD(t[2]))
            else:
                self.tots[4] = float(ASD(self.tots[4]) + ASD(t[2]))
                self.tots[5] = float(ASD(self.tots[5]) + ASD(t[2]))
        if not dos and not tots:
            return
        if self.zeros == "N" and not dos and not self.tots[5]:
            return
        if self.minus == "N" and not dos and self.tots[5] < 0:
            return
        prc = self.sql.rcaprm_col
        prm = self.sql.getRec("rcaprm",
                              where=[("rcp_cono", "=", self.opts["conum"]),
                                     ("rcp_owner", "=", self.own),
                                     ("rcp_code", "=", self.prm)],
                              limit=1)
        ttc = [
            "rtu_trdt", "rtu_refno", "rtu_type", "rtu_desc", "rtu_cnum",
            "rtu_mtyp", "balance"
        ]
        rtt = self.sql.getRec("rcatnt", cols=["rtu_trdt",
            "rtu_refno", "rtu_type", "rtu_desc", "rtu_cnum", "rtu_mtyp",
            "round(sum(rtu_tramt), 2)"], where=[("rtu_cono", "=",
            self.opts["conum"]), ("rtu_owner", "=", self.own), ("rtu_code",
            "=", self.prm), ("rtu_acno", "=", self.acc), ("rtu_type", "<>", 5),
            ("rtu_trdt", "<=", self.datew)], group="rtu_trdt, rtu_refno, "\
            "rtu_type, rtu_desc, rtu_cnum, rtu_mtyp", order="rtu_trdt, "\
            "rtu_cnum, rtu_refno")
        if not rtt:
            rtt = []
        if dos > 0:
            rtt.append(
                [self.datew, "Deposit", 4, "Outstanding Deposit", dps, 2, dos])
        elif dos < 0:
            rtt.append(
                [self.datew, "Deposit", 4, "Overpaid Deposit", dps, 2, dos])
        nad = rtn[tmc.index("rtn_name")]
        if rtn[tmc.index("rtn_addr1")]:
            nad = "%s\n%s" % (nad, rtn[tmc.index("rtn_addr1")])
            if rtn[tmc.index("rtn_addr2")]:
                nad = "%s\n%s" % (nad, rtn[tmc.index("rtn_addr2")])
            if rtn[tmc.index("rtn_addr3")]:
                nad = "%s\n%s" % (nad, rtn[tmc.index("rtn_addr3")])
            if rtn[tmc.index("rtn_pcode")]:
                nad = "%s\n%s" % (nad, rtn[tmc.index("rtn_pcode")])
        elif prm[prc.index("rcp_addr1")]:
            nad = "%s\n%s" % (nad, prm[prc.index("rcp_addr1")])
            if prm[prc.index("rcp_addr2")]:
                nad = "%s\n%s" % (nad, prm[prc.index("rcp_addr2")])
            if prm[prc.index("rcp_addr3")]:
                nad = "%s\n%s" % (nad, prm[prc.index("rcp_addr3")])
            if prm[prc.index("rcp_pcode")]:
                nad = "%s\n%s" % (nad, prm[prc.index("rcp_pcode")])
        else:
            nad = "%s\n\n\n\n" % nad
        self.form.newdic["account_details_C00"][tdc.index("tpd_text")] = nad
        for col in tmc:
            d = "%s_C00" % col
            if d in self.form.newkey:
                dat = rtn[tmc.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        for col in prc:
            d = "%s_C00" % col
            if d in self.form.newkey:
                dat = prm[prc.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        if len(rtt) <= self.form.maxlines:
            self.doBody(ttc, rtt, tdc)
        else:
            bal = 0
            lines = len(rtt) - self.form.maxlines + 1
            for _ in range(lines):
                trn = rtt.pop(0)
                if trn[ttc.index("rtu_mtyp")] == 2:
                    continue
                bal = float(ASD(bal) + ASD(trn[ttc.index("balance")]))
            trn[ttc.index("rtu_refno")] = "B/FWD"
            trn[ttc.index("rtu_type")] = 4
            trn[ttc.index("rtu_desc")] = "Balance Brought Forward"
            trn[ttc.index("balance")] = bal
            rtt.insert(0, trn)
            self.doBody(ttc, rtt, tdc)
        self.doTotal(tdc)
        self.doTail(tdc)
        if self.repeml[1] == "Y" and not self.emadd:
            self.repeml[2] = eml
            self.doPrint()

    def doPrint(self):
        if self.repeml[1] == "Y" and not self.emadd:
            key = "%s_%s_%s_%s" % (self.opts["conum"], self.own, self.prm,
                                   self.acc)
        else:
            key = "%s_all_all_all" % self.opts["conum"]
        pdfnam = getModName(self.opts["mf"].rcdic["wrkdir"],
                            self.__class__.__name__,
                            key,
                            ext="pdf")
        self.form.output(pdfnam, "F")
        doPrinter(mf=self.opts["mf"],
                  conum=self.opts["conum"],
                  pdfnam=pdfnam,
                  header="%s Statement at %s" %
                  (self.opts["conam"], self.dated),
                  fromad=self.fromad,
                  repprt=self.repprt,
                  repeml=self.repeml)
        if self.repeml[1] == "Y":
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()

    def doBody(self, ttc, rtt, tdc):
        page = 0
        count = 0
        rbal = 0
        text = tdc.index("tpd_text")
        for trans in rtt:
            if not count:
                page += 1
                count = self.doHeader(page)
            if count == self.form.maxlines:
                page = self.doCfwd(page)
                count = self.doHeader(page)
            for cod in self.form.body:
                if cod == "type_code":
                    c = "rtu_type"
                elif cod == "mvtype_code":
                    c = "rtu_mtyp"
                elif cod == "line_paid":
                    c = "paid"
                elif cod == "line_balance":
                    c = "balance"
                else:
                    c = cod
                d = "%s_C%02i" % (cod, count)
                if cod == "type_code":
                    ttyp = trans[ttc.index(c)]
                    self.form.newdic[d][text] = rctrtp[ttyp - 1][0]
                elif cod == "mvtype_code":
                    ttyp = trans[ttc.index(c)]
                    self.form.newdic[d][text] = rcmvtp[ttyp - 1][0]
                elif cod == "running_balance":
                    self.form.newdic[d][text] = rbal
                else:
                    self.form.newdic[d][text] = trans[ttc.index(c)]
                    if cod == "line_balance":
                        mtp = trans[ttc.index("rtu_mtyp")]
                        if trans[ttc.index("rtu_desc")]:
                            dsc = trans[ttc.index("rtu_desc")].split()[0]
                        else:
                            dsc = ""
                        if mtp == 2 and dsc not in ("Outstanding", "Overpaid"):
                            pass
                        else:
                            rbal = float(ASD(rbal) + ASD(trans[ttc.index(c)]))
                self.form.doDrawDetail(self.form.newdic[d])
            count += 1
        for x in range(count, self.form.maxlines):
            for cod in self.form.body:
                d = "%s_C%02i" % (cod, x)
                self.form.newdic[d][tdc.index("tpd_text")] = "BLANK"
                self.form.doDrawDetail(self.form.newdic[d])

    def doHeader(self, page):
        self.form.add_page()
        tdc = self.form.sql.tpldet_col
        for key in self.form.newkey:
            nl = copyList(self.form.newdic[key])
            if nl[tdc.index("tpd_place")] != "A":
                continue
            if nl[tdc.index("tpd_detseq")] == "rtn_acno_C00":
                nl[tdc.index("tpd_text")] = self.acc
            self.form.doDrawDetail(nl)
        return 0

    def doCfwd(self, page):
        if "carried_forward" in self.form.tptp:
            tdc = self.form.sql.tpldet_col
            line = copyList(self.form.cfwd)
            line[tdc.index("tpd_text")] = "Continued on Page %i" % (page + 1)
            self.form.doDrawDetail(line)
            return page + 1

    def doTotal(self, tdc):
        for c in self.form.total:
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                line = self.form.newdic[d]
                if c == "total_rental":
                    line[tdc.index("tpd_text")] = self.tots[0]
                elif c == "total_receipts":
                    line[tdc.index("tpd_text")] = self.tots[1]
                elif c == "total_payments":
                    line[tdc.index("tpd_text")] = self.tots[2]
                elif c == "total_journals":
                    line[tdc.index("tpd_text")] = self.tots[3]
                elif c == "total_deposit":
                    line[tdc.index("tpd_text")] = self.tots[4]
                elif c == "total_balance":
                    line[tdc.index("tpd_text")] = self.tots[5]
                self.form.doDrawDetail(line)

    def doTail(self, tdc):
        for c in self.form.tail:
            if c == "message" and not self.mesno:
                continue
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                if c == "deposit":
                    dep = float(ASD(0) - ASD(self.dpp))
                    self.form.newdic[d][tdc.index("tpd_text")] = dep
                elif d == "message_C00" and self.mesno:
                    mes = self.sql.getRec("ctlmes",
                                          cols=["mss_detail"],
                                          where=[("mss_system", "=", "STA"),
                                                 ("mss_message", "=",
                                                  self.mesno)],
                                          limit=1)
                    self.form.newdic[d][tdc.index("tpd_text")] = mes[0]
                self.form.doDrawDetail(self.form.newdic[d])

    def doExit(self):
        self.df.closeProcess()
        self.opts["mf"].closeLoop()
Ejemplo n.º 4
0
class bk3070(object):
    def __init__(self, **opts):
        self.opts = opts
        if self.setVariables():
            if "args" in self.opts:
                self.doEnd()
            else:
                self.mainProcess()
                if "wait" in self.opts:
                    self.df.mstFrame.wait_window()
                else:
                    self.opts["mf"].startLoop()

    def setVariables(self):
        gc = GetCtl(self.opts["mf"])
        bkmctl = gc.getCtl("bkmctl", self.opts["conum"])
        if not bkmctl:
            return
        self.stpl = bkmctl["cbk_statpl"]
        self.fromad = bkmctl["cbk_emadd"]
        t = time.localtime()
        self.sysdtw = (t[0] * 10000) + (t[1] * 100) + t[2]
        self.sysdtd = CCD(self.sysdtw, "D1", 10).disp
        self.sql = Sql(self.opts["mf"].dbm,
                       ["ctlmst", "bkmcon", "bkmmst", "bkmtrn", "tplmst"],
                       prog=self.__class__.__name__)
        if self.sql.error:
            return
        return True

    def mainProcess(self):
        tpm = {
            "stype":
            "R",
            "tables": ("tplmst", ),
            "cols": (("tpm_tname", "", 0, "Template"), ("tpm_title", "", 0,
                                                        "Title", "Y")),
            "where": [("tpm_type", "=", "S"), ("tpm_system", "=", "BKM")],
            "order":
            "tpm_tname"
        }
        bkm = {
            "stype":
            "R",
            "tables": ("bkmmst", "bkmcon"),
            "cols": (("bkm_number", "", 0, "Booking"),
                     ("bkc_sname", "", 0, "Surname", "Y"), ("bkc_names", "", 0,
                                                            "Names", "F")),
            "where":
            (("bkm_cono", "=", self.opts["conum"]), ("bkm_state", "<>", "X"),
             ("bkc_cono=bkm_cono", ), ("bkc_ccode=bkm_ccode", ))
        }
        r1s = (("Yes", "Y"), ("Range", "R"), ("Singles", "S"))
        r2s = (("Yes", "Y"), ("No", "N"))
        fld = ((("T", 0, 0, 0), "INA", 20, "Template Name", "", self.stpl, "Y",
                self.doTplNam, tpm, None, None),
               (("T", 0, 1,
                 0), ("IRB", r1s), 0, "Whole File", "", "Y", "N", self.doWhole,
                None, None, None), (("T", 0, 2, 0), "IUI", 7, "From Booking",
                                    "", "", "N", self.doAcc, bkm, None, None),
               (("T", 0, 3, 0), "IUI", 7, "To Booking", "", "", "N",
                self.doAcc, bkm, None, None),
               (("T", 0, 4, 0), ("IRB", r2s), 0, "Include Cancellations", "",
                "Y", "N", self.doCancel, None, None, None))
        tnd = ((self.doEnd, "Y"), )
        txt = (self.doExit, )
        self.df = TartanDialog(self.opts["mf"],
                               eflds=fld,
                               tend=tnd,
                               txit=txt,
                               view=("N", "V"),
                               mail=("B", "Y"))

    def doTplNam(self, frt, pag, r, c, p, i, w):
        acc = self.sql.getRec("tplmst",
                              where=[("tpm_tname", "=", w),
                                     ("tpm_type", "=", "S"),
                                     ("tpm_system", "=", "BKM")],
                              limit=1)
        if not acc:
            return "Invalid Template Name"
        self.tname = w

    def doWhole(self, frt, pag, r, c, p, i, w):
        self.whole = w
        if self.whole in ("Y", "S"):
            self.snum = 0
            self.enum = 0
            self.df.loadEntry("T", 0, p + 1, data="")
            self.df.loadEntry("T", 0, p + 2, data="")
            return "sk2"

    def doAcc(self, frt, pag, r, c, p, i, w):
        if p == 2:
            self.snum = w
        else:
            self.enum = w
        chk = self.sql.getRec("bkmmst",
                              where=[("bkm_cono", "=", self.opts["conum"]),
                                     ("bkm_number", "=", w)],
                              limit=1)
        if not chk:
            return "Invalid Booking Number"
        if p == 2:
            self.df.loadEntry(frt, pag, p + 1, data=self.snum)

    def doCancel(self, frt, pag, r, c, p, i, w):
        self.cancel = w

    def doEnd(self):
        if "args" in self.opts:
            self.cancel = "Y"
            self.tname = self.stpl
            self.whole = "A"
            self.snum = 0
            self.enum = 0
            if len(self.opts["args"]) == 1:
                self.repprt = ["N", "V", "view"]
                self.repeml = ["N", "N", "", "", "Y"]
            else:
                self.repprt = self.opts["args"][1]
                self.repeml = self.opts["args"][2]
        else:
            self.df.closeProcess()
            self.repprt = self.df.repprt
            self.repeml = self.df.repeml
            if not self.enum:
                self.enum = 9999999
        self.emadd = self.repeml[2]
        tab = ["bkmmst"]
        whr = [("bkm_cono", "=", self.opts["conum"])]
        if self.cancel == "N":
            whr.append(("bkm_state", "<>", "X"))
        odr = "bkm_number"
        if self.whole == "A":
            if type(self.opts["args"][0]) == int:
                whr.append(("bkm_number", "=", self.opts["args"][0]))
            else:
                whr.append(("bkm_number", "in", self.opts["args"][0]))
            recs = self.sql.getRec("bkmmst", where=whr, order=odr)
        elif self.whole == "S":
            tab.append("bkmcon")
            col = ["bkm_number", "bkc_sname", "bkc_names"]
            whr.extend([("bkc_cono=bkm_cono", ), ("bkc_ccode=bkm_ccode", )])
            dic = {}
            for c in col:
                for t in tab:
                    d = getattr(self.sql, "%s_dic" % t)
                    if c in d:
                        dic[c] = d[c]
            data = self.sql.getRec(tables=tab, cols=col, where=whr, order=odr)
            data = getSingleRecords(self.opts["mf"],
                                    tab,
                                    col,
                                    dic=dic,
                                    where=data,
                                    ttype="D")
            recs = []
            for dat in data:
                acc = self.sql.getRec("bkmmst",
                                      where=[("bkm_cono", "=",
                                              self.opts["conum"]),
                                             ("bkm_number", "=", dat[0])])
                recs.append(acc[0])
        else:
            if self.whole == "R":
                whr.extend([("bkm_number", ">=", self.snum),
                            ("bkm_number", "<=", self.enum)])
            recs = self.sql.getRec("bkmmst", where=whr, order=odr)
        if not recs:
            showError(self.opts["mf"].body, "Error", "No Bookings Selected")
        else:
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()
            p = ProgressBar(self.opts["mf"].body, mxs=len(recs), esc=True)
            for num, rec in enumerate(recs):
                p.displayProgress(num)
                if p.quit:
                    break
                self.doProcess(rec)
            p.closeProgress()
            if p.quit or not self.form.page:
                pass
            elif self.repeml[1] == "N" or self.emadd:
                self.repeml[2] = self.emadd
                self.doPrint()
        if "wait" not in self.opts:
            self.opts["mf"].closeLoop()

    def doLoadStatic(self):
        cmc = self.sql.ctlmst_col
        ctm = self.sql.getRec("ctlmst",
                              where=[("ctm_cono", "=", self.opts["conum"])],
                              limit=1)
        for fld in cmc:
            dat = ctm[cmc.index(fld)]
            if fld in self.form.tptp:
                if fld == "ctm_logo":
                    self.form.letterhead(cmc, ctm, fld, dat)
                    continue
                self.form.tptp[fld][1] = dat
        if "letterhead" in self.form.tptp:
            self.form.letterhead(cmc, ctm, "letterhead", None)
        self.form.document_date(self.sysdtd)
        self.form.bank_details(cmc, ctm, 0)

    def doProcess(self, bkm):
        bmc = self.sql.bkmmst_col
        ccc = self.sql.bkmcon_col
        btc = self.sql.bkmtrn_col
        tdc = self.form.sql.tpldet_col
        self.num = bkm[bmc.index("bkm_number")]
        self.cod = bkm[bmc.index("bkm_ccode")]
        con = self.sql.getRec("bkmcon",
                              where=[("bkc_cono", "=", self.opts["conum"]),
                                     ("bkc_ccode", "=", self.cod)],
                              limit=1)
        eml = con[self.sql.bkmcon_col.index("bkc_email")]
        if "contact_details" in self.form.tptp:
            dat = "%s %s %s" % (con[ccc.index("bkc_title")],
                                con[ccc.index("bkc_names")],
                                con[ccc.index("bkc_sname")])
            if bkm[bmc.index("bkm_group")]:
                dat = "%s\n%s" % (dat, bkm[bmc.index("bkm_group")][:40])
            for fld in ("addr1", "addr2", "addr3", "pcode"):
                dat = "%1s\n%s" % (dat, con[ccc.index("bkc_%s" % fld)])
            self.form.newdic["contact_details_C00"][tdc.index(
                "tpd_text")] = dat
        for col in bmc:
            d = "%s_C00" % col
            if d in self.form.newdic:
                dat = bkm[bmc.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        bkt = self.sql.getRec("bkmtrn",
                              where=[("bkt_cono", "=", self.opts["conum"]),
                                     ("bkt_number", "=", self.num)],
                              order="bkt_date, bkt_refno")
        if not bkt:
            return
        self.doBody(btc, bkt, tdc)
        self.doTotal(tdc)
        self.doTail()
        if self.repeml[1] == "Y" and not self.emadd:
            self.repeml[2] = eml
            self.doPrint()

    def doPrint(self):
        if self.repeml[1] == "Y" and not self.emadd:
            key = "%s_%s" % (self.opts["conum"], self.num)
        else:
            key = "%s_all" % self.opts["conum"]
        pdfnam = getModName(self.opts["mf"].rcdic["wrkdir"],
                            self.__class__.__name__,
                            key,
                            ext="pdf")
        self.form.output(pdfnam, "F")
        doPrinter(mf=self.opts["mf"],
                  conum=self.opts["conum"],
                  pdfnam=pdfnam,
                  header="%s Statement at %s" %
                  (self.opts["conam"], self.sysdtd),
                  repprt=self.repprt,
                  fromad=self.fromad,
                  repeml=self.repeml)
        if self.repeml[1] == "Y":
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()

    def doBody(self, btc, bkt, tdc):
        page = 0
        count = 0
        self.tbal = 0
        for trans in bkt:
            if not count:
                page += 1
                count = self.doHeader(page)
            if count == self.form.maxlines:
                page = self.doCfwd(page)
                count = self.doHeader(page)
            ldic = {}
            for cod in self.form.body:
                if cod in ("type_code", "line_paid", "line_balance"):
                    continue
                if cod == "bkt_desc":
                    des = self.form.doSplitText("bkt_desc_C00",
                                                trans[btc.index(cod)])
                    if len(des) > 1 and not des[-1]:
                        del des[-1]
                else:
                    ldic[cod] = CCD(trans[btc.index(cod)],
                                    self.form.tptp[cod][0][1],
                                    self.form.tptp[cod][0][2])
            ldic["line_paid"] = 0
            ldic["line_balance"] = ldic["bkt_tramt"].work
            self.tbal = float(ASD(self.tbal) + ASD(ldic["bkt_tramt"].work))
            for n, l in enumerate(des):
                if count == self.form.maxlines:
                    page = self.doCfwd(page)
                    count = self.doHeader(page)
                if n == 0 and len(des) == 1:
                    include = copyList(self.form.body)
                elif n == 0:
                    include = ("bkt_date", "bkt_refno", "type_code")
                elif n + 1 == len(des):
                    include = copyList(self.form.body)
                    include.remove("bkt_date")
                    include.remove("bkt_refno")
                    include.remove("type_code")
                else:
                    include = []
                for code in self.form.body:
                    seq = "%s_C%02i" % (code, count)
                    if code == "bkt_desc":
                        data = l
                    elif code in include:
                        if code == "type_code":
                            data = bktrtp[trans[btc.index("bkt_type")] - 1][0]
                        else:
                            data = ldic[code].work
                    else:
                        data = "BLANK"
                    self.form.newdic[seq][tdc.index("tpd_text")] = data
                    self.form.doDrawDetail(self.form.newdic[seq])
                count += 1
        for x in range(count, self.form.maxlines):
            for cod in self.form.body:
                d = "%s_C%02i" % (cod, x)
                self.form.newdic[d][tdc.index("tpd_text")] = "BLANK"
                self.form.doDrawDetail(self.form.newdic[d])

    def doHeader(self, page):
        self.form.add_page()
        tdc = self.form.sql.tpldet_col
        for key in self.form.newkey:
            nl = copyList(self.form.newdic[key])
            if nl[tdc.index("tpd_place")] != "A":
                continue
            if nl[tdc.index("tpd_detseq")] == "bkm_number_C00":
                nl[tdc.index("tpd_text")] = self.num
            elif nl[tdc.index("tpd_detseq")] == "page_number_C00":
                nl[tdc.index("tpd_text")] = str(page)
            self.form.doDrawDetail(nl)
        return 0

    def doCfwd(self, page):
        if "carried_forward" in self.form.tptp:
            tdc = self.form.sql.tpldet_col
            line = copyList(self.form.cfwd)
            line[tdc.index("tpd_text")] = "Continued on Page %i" % (page + 1)
            self.form.doDrawDetail(line)
        return page + 1

    def doTotal(self, tdc):
        for c in self.form.total:
            if c != "total_balance":
                continue
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                line = self.form.newdic[d]
                line[tdc.index("tpd_text")] = self.tbal
                self.form.doDrawDetail(line)

    def doTail(self):
        for c in self.form.tail:
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[d])

    def doExit(self):
        self.df.closeProcess()
        if "wait" not in self.opts:
            self.opts["mf"].closeLoop()
Ejemplo n.º 5
0
class rt3040(object):
    def __init__(self, **opts):
        self.opts = opts
        if self.setVariables():
            self.mainProcess()
            self.opts["mf"].startLoop()

    def setVariables(self):
        self.sql = Sql(self.opts["mf"].dbm, [
            "ctlmes", "ctlmst", "rtlmst", "rtltrn", "rtlprm", "rtlcon",
            "tplmst"
        ],
                       prog=self.__class__.__name__)
        if self.sql.error:
            return
        gc = GetCtl(self.opts["mf"])
        rtlctl = gc.getCtl("rtlctl", self.opts["conum"])
        if not rtlctl:
            return
        self.stpl = rtlctl["ctr_tplnam"]
        self.fromad = rtlctl["ctr_emadd"]
        t = time.localtime()
        self.sysdtw = (t[0] * 10000) + (t[1] * 100) + t[2]
        self.trtypes = []
        for t in rttrtp:
            self.trtypes.append(t[0])
        return True

    def mainProcess(self):
        tpm = {
            "stype":
            "R",
            "tables": ("tplmst", ),
            "cols": (("tpm_tname", "", 0, "Template"), ("tpm_title", "", 0,
                                                        "Title", "Y")),
            "where": [("tpm_type", "=", "R"), ("tpm_system", "=", "MEM")],
            "order":
            "tpm_tname"
        }
        mss = {
            "stype":
            "R",
            "tables": ("ctlmes", ),
            "cols": (("mss_message", "", 0, "Num"), ("mss_detail", "NA", 50,
                                                     "Details")),
            "where": [("mss_system", "=", "RTL")]
        }
        r1s = (("Yes", "Y"), ("No", "N"))
        fld = ((("T", 0, 0, 0), "INA", 20, "Template Name", "", self.stpl, "Y",
                self.doTplNam, tpm, None, None),
               (("T", 0, 1,
                 0), ("IRB", r1s), 0, "Whole File", "", "Y", "Y", self.doWhole,
                None, None, None), (("T", 0, 2, 0), ("IRB", r1s), 0,
                                    "Zero Balances", "Include Zero Balances",
                                    "N", "Y", self.doZeros, None, None, None),
               (("T", 0, 3, 0), ("IRB", r1s), 0, "Minus Balances",
                "Include Minus Balances", "N", "Y", self.doMinus, None, None,
                None), (("T", 0, 4, 0), "IUI", 3, "Message Number", "", "",
                        "Y", self.doMessno, mss, None, ("efld", )),
               (("T", 0, 5, 0), "ID1", 10, "Statement Date", "", self.sysdtw,
                "Y", self.doDat, None, None, ("efld", )))
        tnd = ((self.doEnd, "Y"), )
        txt = (self.doExit, )
        self.df = TartanDialog(self.opts["mf"],
                               eflds=fld,
                               tend=tnd,
                               txit=txt,
                               view=("N", "V"),
                               mail=("B", "Y"))

    def doTplNam(self, frt, pag, r, c, p, i, w):
        acc = self.sql.getRec("tplmst",
                              where=[("tpm_tname", "=", w),
                                     ("tpm_type", "=", "S"),
                                     ("tpm_system", "=", "RTL")],
                              limit=1)
        if not acc:
            return "Invalid Template Name"
        self.tname = w

    def doWhole(self, frt, pag, r, c, p, i, w):
        self.whole = w

    def doZeros(self, frt, pag, r, c, p, i, w):
        self.zeros = w

    def doMinus(self, frt, pag, r, c, p, i, w):
        self.minus = w

    def doMessno(self, frt, pag, r, c, p, i, w):
        if w:
            self.mes = self.sql.getRec("ctlmes",
                                       cols=["mss_detail"],
                                       where=[("mss_system", "=", "RTL"),
                                              ("mss_message", "=", w)],
                                       limit=1)
            if not self.mes:
                return "Invalid Message Number"
        self.mesno = w

    def doDat(self, frt, pag, r, c, p, i, w):
        self.datew = w
        self.dated = self.df.t_disp[pag][0][p]
        self.curdt = int(w / 100)

    def doEnd(self):
        self.df.closeProcess()
        self.emadd = self.df.repeml[2]
        if self.whole == "N":
            recs = getSingleRecords(self.opts["mf"],
                                    "rtlmst",
                                    ("rtm_code", "rtm_acno", "rtm_name"),
                                    where=[("rtm_cono", "=",
                                            self.opts["conum"])])
        else:
            whr = [("rtm_cono", "=", self.opts["conum"])]
            odr = "rtm_code, rtm_acno"
            recs = self.sql.getRec("rtlmst", where=whr, order=odr)
            if not recs:
                showError(self.opts["mf"].body, "Error",
                          "No Accounts Selected")
        if recs:
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()
            p = ProgressBar(self.opts["mf"].body, mxs=len(recs), esc=True)
            for num, rec in enumerate(recs):
                p.displayProgress(num)
                if p.quit:
                    break
                self.doProcess(rec)
            p.closeProgress()
            if p.quit or not self.form.page:
                pass
            elif self.df.repeml[1] == "N" or not self.emadd:
                self.df.repeml[2] = self.emadd
                self.doPrint()
        self.opts["mf"].closeLoop()

    def doLoadStatic(self):
        cmc = self.sql.ctlmst_col
        ctm = self.sql.getRec("ctlmst",
                              where=[("ctm_cono", "=", self.opts["conum"])],
                              limit=1)
        for fld in cmc:
            dat = ctm[cmc.index(fld)]
            if fld in self.form.tptp:
                if fld == "ctm_logo":
                    self.form.letterhead(cmc, ctm, fld, dat)
                    continue
                self.form.tptp[fld][1] = dat
        if "letterhead" in self.form.tptp:
            self.form.letterhead(cmc, ctm, "letterhead", None)
        self.form.document_date(self.dated)
        self.form.bank_details(cmc, ctm, 0)

    def doProcess(self, rtm):
        rmc = self.sql.rtlmst_col
        rtc = copyList(self.sql.rtltrn_col)
        tdc = self.form.sql.tpldet_col
        self.code = rtm[rmc.index("rtm_code")]
        self.acno = rtm[rmc.index("rtm_acno")]
        eml = rtm[rmc.index("rtm_email")]
        for col in rmc:
            d = "%s_00" % col
            if d in self.form.newdic:
                dat = rtm[rmc.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        prm = self.sql.getRec("rtlprm",
                              where=[("rtp_cono", "=", self.opts["conum"]),
                                     ("rtp_code", "=", self.code)],
                              limit=1)
        if not prm:
            prm = [0, "", "", "", "", "", "", 0, 0]
        prc = self.sql.rtlprm_col
        for col in prc:
            d = "%s_00" % col
            if col in self.form.newdic:
                dat = prm[prc.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        con = self.sql.getRec("rtlcon",
                              where=[("rtc_cono", "=", self.opts["conum"]),
                                     ("rtc_code", "=", self.code),
                                     ("rtc_acno", "=", self.acno)],
                              limit=1)
        coc = self.sql.rtlprm_col
        for col in coc:
            d = "%s_00" % col
            if col in self.form.newdic:
                dat = con[coc.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        self.tots = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
        rtt = self.sql.getRec(
            "rtltrn",
            cols=["round(sum(rtt_tramt), 2)", "round(sum(rtt_taxamt), 2)"],
            where=[("rtt_cono", "=", self.opts["conum"]),
                   ("rtt_code", "=", self.code), ("rtt_acno", "=", self.acno),
                   ("rtt_trdt", "<=", self.datew),
                   ("rtt_curdt", "=", self.curdt)],
            limit=1)
        if rtt[0]:
            self.tots[0] = rtt[0]
            self.tots[2] = rtt[0]
            self.tots[5] = rtt[0]
        if rtt[1]:
            self.tots[0] = float(ASD(self.tots[0]) - ASD(rtt[1]))
            self.tots[1] = rtt[1]
        arr = self.sql.getRec("rtltrn",
                              cols=["round(sum(rtt_tramt), 2)"],
                              where=[("rtt_cono", "=", self.opts["conum"]),
                                     ("rtt_code", "=", self.code),
                                     ("rtt_acno", "=", self.acno),
                                     ("rtt_curdt", "<", self.curdt)],
                              limit=1)
        if arr[0]:
            self.tots[3] = arr[0]
            self.tots[5] = float(ASD(self.tots[5]) + ASD(arr[0]))
        if self.zeros == "N" and not self.tots[5]:
            return
        if self.minus == "N" and self.tots[5] < 0:
            return
        rtc = [
            "rtt_trdt", "rtt_refno", "rtt_type", "rtt_desc", "rtt_cnum",
            "rtt_tramt"
        ]
        rtt = self.sql.getRec(
            "rtltrn",
            cols=[
                "rtt_trdt", "rtt_refno", "rtt_type", "rtt_desc", "rtt_cnum",
                "round(sum(rtt_tramt), 2)"
            ],
            where=[("rtt_cono", "=", self.opts["conum"]),
                   ("rtt_code", "=", self.code), ("rtt_acno", "=", self.acno),
                   ("rtt_type", "<>", 5), ("rtt_trdt", "<=", self.datew)],
            group="rtt_trdt, rtt_refno, rtt_type, rtt_desc, rtt_cnum",
            order="rtt_trdt, rtt_cnum, rtt_refno")
        if not rtt:
            rtt = []
        trans = []
        for trn in rtt:
            tramt = trn[rtc.index("rtt_tramt")]
            if tramt < 0:
                trn.extend([0, tramt])
            else:
                trn.extend([tramt, 0])
            trans.append(trn)
        rtc.extend(["line_debit", "line_credit"])
        self.doHeader(rtm, rmc, prm, prc, tdc)
        if len(trans) <= self.form.maxlines:
            self.doBody(rtc, trans, tdc)
        else:
            bal = 0
            lines = len(trans) - self.form.maxlines + 1
            for _ in range(lines):
                trn = trans.pop(0)
                bal = float(ASD(bal) + ASD(trn[5]))
            trn[rtc.index("rtt_refno")] = "B/FWD"
            trn[rtc.index("rtt_type")] = 3
            trn[rtc.index("rtt_desc")] = "Balance Brought Forward"
            trn[rtc.index("rtt_tramt")] = bal
            if bal < 0:
                trn[rtc.index("line_debit")] = 0
                trn[rtc.index("line_credit")] = bal
            else:
                trn[rtc.index("line_debit")] = bal
                trn[rtc.index("line_credit")] = 0
            trans.insert(0, trn)
            self.doBody(rtc, trans, tdc)
        self.doTotal(tdc)
        self.doTail(tdc)
        if self.df.repeml[1] == "Y" and not self.emadd:
            self.df.repeml[2] = eml
            self.doPrint()

    def doHeader(self, rtm, rmc, prm, prc, tdc):
        self.form.add_page()
        if "account_details" in self.form.tptp:
            nad = rtm[rmc.index("rtm_name")]
            add1 = rtm[rmc.index("rtm_addr1")]
            if add1:
                nad = "%1s\n%1s" % (nad, add1)
                nad = "%1s\n%1s" % (nad, rtm[rmc.index("rtm_addr2")])
                nad = "%1s\n%1s" % (nad, rtm[rmc.index("rtm_addr3")])
                nad = "%1s\n%1s" % (nad, rtm[rmc.index("rtm_pcode")])
            else:
                nad = prm[prc.index("rtp_addr1")]
                nad = "%1s\n%1s" % (nad, prm[prc.index("rtp_addr2")])
                nad = "%1s\n%1s" % (nad, prm[prc.index("rtp_addr3")])
                nad = "%1s\n%1s" % (nad, prm[prc.index("rtp_pcode")])
            self.form.newdic["account_details_C00"][tdc.index(
                "tpd_text")] = nad
        for key in self.form.newkey:
            nl = copyList(self.form.newdic[key])
            if nl[tdc.index("tpd_place")] != "A":
                continue
            if nl[tdc.index("tpd_detseq")] == "rtm_code_C00":
                nl[tdc.index("tpd_text")] = self.code
            elif nl[tdc.index("tpd_detseq")] == "rtm_acno_C00":
                nl[tdc.index("tpd_text")] = self.acno
            self.form.doDrawDetail(nl)

    def doBody(self, rtc, rtt, tdc):
        text = self.form.sql.tpldet_col.index("tpd_text")
        count = 0
        bal = 0
        for trans in rtt:
            for cod in self.form.body:
                if cod == "type_code":
                    c = "rtt_type"
                else:
                    c = cod
                d = "%s_C%02i" % (cod, count)
                if cod == "type_code":
                    ttyp = trans[rtc.index(c)]
                    self.form.newdic[d][text] = rttrtp[ttyp - 1][0]
                elif cod in ("line_debit", "line_credit"):
                    tramt = trans[rtc.index(c)]
                    bal = float(ASD(bal) + ASD(tramt))
                    self.form.newdic[d][text] = trans[rtc.index(c)]
                elif c == "line_balance":
                    self.form.newdic[d][text] = bal
                else:
                    self.form.newdic[d][text] = trans[rtc.index(c)]
                self.form.doDrawDetail(self.form.newdic[d])
            count += 1
        for x in range(count, self.form.maxlines):
            for cod in self.form.body:
                d = "%s_C%02i" % (cod, x)
                self.form.newdic[d][tdc.index("tpd_text")] = "BLANK"
                self.form.doDrawDetail(self.form.newdic[d])

    def doTotal(self, tdc):
        for c in self.form.total:
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                line = self.form.newdic[d]
                if c == "period_rental":
                    line[tdc.index("tpd_text")] = self.tots[0]
                elif c == "period_vat":
                    line[tdc.index("tpd_text")] = self.tots[1]
                elif c == "total_rental":
                    line[tdc.index("tpd_text")] = self.tots[2]
                elif c == "total_arrears":
                    line[tdc.index("tpd_text")] = self.tots[3]
                elif c == "interest":
                    line[tdc.index("tpd_text")] = self.tots[4]
                elif c == "total_balance":
                    line[tdc.index("tpd_text")] = self.tots[5]
                self.form.doDrawDetail(line)

    def doTail(self, tdc):
        for c in self.form.tail:
            if c == "message" and not self.mesno:
                continue
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                if d == "message_C00" and self.mesno:
                    mes = self.sql.getRec("ctlmes",
                                          cols=["mss_detail"],
                                          where=[("mss_system", "=", "STA"),
                                                 ("mss_message", "=",
                                                  self.mesno)],
                                          limit=1)
                    self.form.newdic[d][tdc.index("tpd_text")] = mes[0]
                self.form.doDrawDetail(self.form.newdic[d])

    def doPrint(self):
        if self.df.repeml[1] == "Y" and not self.emadd:
            key = "%s_%s_%s" % (self.opts["conum"], self.code, self.acno)
        else:
            key = "%s_all_all" % self.opts["conum"]
        pdfnam = getModName(self.opts["mf"].rcdic["wrkdir"],
                            self.__class__.__name__,
                            key,
                            ext="pdf")
        self.form.output(pdfnam, "F")
        doPrinter(mf=self.opts["mf"],
                  conum=self.opts["conum"],
                  pdfnam=pdfnam,
                  header="%s Statement at %s" %
                  (self.opts["conam"], self.dated),
                  fromad=self.fromad,
                  repprt=self.df.repprt,
                  repeml=self.df.repeml)
        if self.df.repeml[1] == "Y":
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()

    def doExit(self):
        self.df.closeProcess()
        self.opts["mf"].closeLoop()
Ejemplo n.º 6
0
class dr3080(object):
    def __init__(self, **opts):
        self.opts = opts
        if self.setVariables():
            self.mainProcess()
            self.opts["mf"].startLoop()

    def setVariables(self):
        self.sql = Sql(
            self.opts["mf"].dbm,
            ["ctlmes", "ctlmst", "drschn", "drsmst", "drstrn", "tplmst"],
            prog=self.__class__.__name__)
        if self.sql.error:
            return
        gc = GetCtl(self.opts["mf"])
        drsctl = gc.getCtl("drsctl", self.opts["conum"])
        if not drsctl:
            return
        self.chains = drsctl["ctd_chain"]
        self.stpl = drsctl["ctd_tplnam"]
        self.ageing = drsctl["ctd_ageing"]
        self.fromad = drsctl["ctd_emadd"]
        t = time.localtime()
        self.sysdtw = (t[0] * 10000) + (t[1] * 100) + t[2]
        if self.chains == "N":
            self.schn = 0
            self.echn = 0
        return True

    def mainProcess(self):
        tpm = {
            "stype":
            "R",
            "tables": ("tplmst", ),
            "cols": (("tpm_tname", "", 0, "Template"), ("tpm_title", "", 0,
                                                        "Title", "Y")),
            "where": [("tpm_type", "=", "S"), ("tpm_system", "=", "DRS")],
            "order":
            "tpm_tname"
        }
        drc = {
            "stype": "R",
            "tables": ("drschn", ),
            "cols":
            (("chm_chain", "", 0, "Num"), ("chm_name", "", 0, "Name", "Y")),
            "where": [("chm_cono", "=", self.opts["conum"])]
        }
        drm = {
            "stype":
            "R",
            "tables": ("drsmst", ),
            "cols":
            (("drm_acno", "", 0, "Acc-Num"), ("drm_name", "", 0, "Name", "Y"),
             ("drm_add1", "", 0, "Address Line 1"))
        }
        if self.chains == "Y":
            drm["where"] = [("drm_cono", "=", self.opts["conum"])]
            drm["whera"] = [["C", "drm_chain", 0, 1]]
        else:
            drm["where"] = [("drm_cono", "=", self.opts["conum"]),
                            ("drm_chain", "=", 0)]
        mss = {
            "stype":
            "R",
            "tables": ("ctlmes", ),
            "cols": (("mss_message", "", 0, "Num"), ("mss_detail", "NA", 50,
                                                     "Details")),
            "where": [("mss_system", "=", "STA")]
        }
        r1s = (("Yes", "Y"), ("No", "N"))
        r2s = (("Yes", "Y"), ("Range", "R"), ("Singles", "S"))
        r3s = (("Number", "N"), ("Name", "M"), ("Postal Code", "P"))
        fld = [[["T", 0, 0, 0], "INA", 20, "Template Name", "", self.stpl, "Y",
                self.doTplNam, tpm, None, None],
               [["T", 0, 1, 0], ("IRB", r1s), 0, "Open Item", "", "Y", "Y",
                self.doOItem, None, None, None],
               [["T", 0, 2, 0], "IUI", 2, "Maximum Pages", "", 1, "Y",
                self.doPages, None, None, None],
               [["T", 0, 3, 0], ("IRB", r2s), 0, "Whole File", "", "S", "Y",
                self.doWhole, None, None, None],
               [["T", 0, 4, 0], "IUI", 3, "From Chain", "", "", "Y",
                self.doChn, drc, None, None],
               [["T", 0, 5, 0], "INA", 7, "From Account", "", "", "Y",
                self.doAcc, drm, None, None],
               [["T", 0, 6, 0], "INA", 3, "To Chain", "", "", "Y", self.doChn,
                drc, None, None],
               [["T", 0, 7, 0], "INA", 7, "To Account", "", "", "Y",
                self.doAcc, drm, None, None],
               [["T", 0, 8, 0], ("IRB", r3s), 0, "Sort Order", "", "N", "Y",
                self.doSort, None, None, None],
               [["T", 0, 9, 0], ("IRB", r1s), 0, "Include Zero Balances", "",
                "N", "Y", self.doZeros, None, None, None],
               [["T", 0, 10, 0], ("IRB", r1s), 0, "Include Negative Balances",
                "", "N", "Y", self.doMinus, None, None, None],
               [["T", 0, 11, 0], ("IRB", r1s), 0, "Include Stopped Accounts",
                "", "N", "Y", self.doStops, None, None, None],
               [["T", 0, 12, 0], ("IRB", r1s), 0, "Include Redundant Accounts",
                "", "N", "Y", self.doRedu, None, None, None],
               [["T", 0, 13,
                 0], ("IRB", r1s), 0, "Include Allocated Transactions", "",
                "N", "Y", self.doAlloc, None, None, None],
               [["T", 0, 14, 0], "ID1", 10, "Statement Date", "", self.sysdtw,
                "Y", self.doDat, None, None, ("efld", )],
               [["T", 0, 15, 0], "IUI", 3, "Message Number", "", "", "Y",
                self.doMessno, mss, None, ("efld", )]]
        if self.chains != "Y":
            del fld[6]
            del fld[4]
            for n, f in enumerate(fld):
                fld[n][0][2] = n
        tnd = ((self.doEnd, "Y"), )
        txt = (self.doExit, )
        self.df = TartanDialog(self.opts["mf"],
                               eflds=fld,
                               tend=tnd,
                               txit=txt,
                               view=("N", "V"),
                               mail=("B", "Y"))

    def doTplNam(self, frt, pag, r, c, p, i, w):
        acc = self.sql.getRec("tplmst",
                              where=[("tpm_tname", "=", w),
                                     ("tpm_type", "=", "S"),
                                     ("tpm_system", "=", "DRS")],
                              limit=1)
        if not acc:
            return "Invalid Template Name"
        self.tname = w
        self.sttyp = acc[self.sql.tplmst_col.index("tpm_sttp")]
        if self.sttyp == "N":
            self.oitem = "Y"
            self.pages = 0
            self.df.loadEntry(frt, pag, p + 1, data=self.oitem)
            self.df.loadEntry(frt, pag, p + 2, data=self.pages)
            return "sk2"

    def doOItem(self, frt, pag, r, c, p, i, w):
        self.oitem = w
        if self.oitem == "N":
            self.pages = 0
            self.df.loadEntry(frt, pag, p + 1, data=self.pages)
            return "sk1"

    def doPages(self, frt, pag, r, c, p, i, w):
        self.pages = w

    def doWhole(self, frt, pag, r, c, p, i, w):
        self.whole = w
        if self.whole in ("Y", "S"):
            self.schn = 0
            self.echn = 0
            self.sacc = ""
            self.eacc = ""
            if self.chains == "Y":
                self.df.loadEntry("T", 0, p + 1, data=self.schn)
                self.df.loadEntry("T", 0, p + 2, data=self.sacc)
                self.df.loadEntry("T", 0, p + 3, data=self.echn)
                self.df.loadEntry("T", 0, p + 4, data=self.eacc)
                return "sk4"
            else:
                self.df.loadEntry("T", 0, p + 1, data=self.sacc)
                self.df.loadEntry("T", 0, p + 2, data=self.eacc)
                return "sk2"

    def doChn(self, frt, pag, r, c, p, i, w):
        chk = self.sql.getRec("drschn",
                              cols=["chm_name"],
                              where=[("chm_chain", "=", w)],
                              limit=1)
        if not chk:
            return "Invalid Chain Store"
        if c == 5:
            self.schn = w
        else:
            self.echn = w

    def doAcc(self, frt, pag, r, c, p, i, w):
        if self.chains == "Y" and c == 6:
            chk = self.schn
            self.sacc = w
        elif self.chains == "Y" and c == 8:
            chk = self.echn
            self.eacc = w
        elif c == 5:
            chk = self.schn
            self.sacc = w
        else:
            chk = self.echn
            self.eacc = w
        chk = self.sql.getRec("drsmst",
                              cols=["drm_name"],
                              where=[("drm_cono", "=", self.opts["conum"]),
                                     ("drm_chain", "=", chk),
                                     ("drm_acno", "=", w)],
                              limit=1)
        if not chk:
            return "Invalid Debtors Account"

    def doSort(self, frt, pag, r, c, p, i, w):
        self.sort = w

    def doZeros(self, frt, pag, r, c, p, i, w):
        self.zeros = w

    def doMinus(self, frt, pag, r, c, p, i, w):
        self.minus = w

    def doStops(self, frt, pag, r, c, p, i, w):
        self.stops = w

    def doRedu(self, frt, pag, r, c, p, i, w):
        self.redu = w
        if self.sttyp == "O":
            self.alloc = "Y"
            self.df.loadEntry(frt, pag, p + 1, data=self.alloc)
            return "sk1"

    def doAlloc(self, frt, pag, r, c, p, i, w):
        self.alloc = w

    def doMessno(self, frt, pag, r, c, p, i, w):
        if w:
            acc = self.sql.getRec("ctlmes",
                                  cols=["mss_detail"],
                                  where=[("mss_system", "=", "STA"),
                                         ("mss_message", "=", w)],
                                  limit=1)
            if not acc:
                return "Invalid Message Number"
        self.mess = w

    def doDat(self, frt, pag, r, c, p, i, w):
        self.datew = w
        self.dated = self.df.t_disp[pag][0][p]
        self.curdt = int(w / 100)

    def doEnd(self):
        self.df.closeProcess()
        self.emadd = self.df.repeml[2]
        if not self.echn:
            self.echn = 999
        if not self.eacc:
            self.eacc = "zzzzzzz"
        whr = [("drm_cono", "=", self.opts["conum"]),
               ("drm_chain", ">=", self.schn), ("drm_acno", ">=", self.sacc),
               ("drm_chain", "<=", self.echn), ("drm_acno", "<=", self.eacc)]
        if self.stops == "N":
            whr.append(("drm_stop", "<>", "Y"))
        if self.redu == "N":
            whr.append(("drm_stat", "<>", "X"))
        if self.whole == "S":
            recs = getSingleRecords(self.opts["mf"],
                                    "drsmst",
                                    ("drm_chain", "drm_acno", "drm_name"),
                                    where=whr)
        else:
            if self.sort == "N":
                odr = "drm_chain, drm_acno"
            elif self.sort == "M":
                odr = "drm_name"
            else:
                odr = "drm_pcod"
            recs = self.sql.getRec("drsmst", where=whr, order=odr)
        if not recs:
            showError(self.opts["mf"].body, "Error", "No Accounts Selected")
        else:
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()
            p = ProgressBar(self.opts["mf"].body, mxs=len(recs), esc=True)
            for num, rec in enumerate(recs):
                p.displayProgress(num)
                if p.quit:
                    break
                self.doProcess(rec)
            p.closeProgress()
            if p.quit or not self.form.page:
                pass
            elif self.df.repeml[1] == "N" or self.emadd:
                self.df.repeml[2] = self.emadd
                self.doPrint()
        self.opts["mf"].closeLoop()

    def doLoadStatic(self):
        cmc = self.sql.ctlmst_col
        ctm = self.sql.getRec("ctlmst",
                              where=[("ctm_cono", "=", self.opts["conum"])],
                              limit=1)
        for fld in cmc:
            dat = ctm[cmc.index(fld)]
            if fld in self.form.tptp:
                if fld == "ctm_logo":
                    self.form.letterhead(cmc, ctm, fld, dat)
                    continue
                self.form.tptp[fld][1] = dat
        if "letterhead" in self.form.tptp:
            self.form.letterhead(cmc, ctm, "letterhead", None)
        self.form.document_date(self.dated)
        self.form.bank_details(cmc, ctm, 0)

    def doProcess(self, drm):
        dmc = self.sql.drsmst_col
        dtc = self.sql.drstrn_col
        tdc = self.form.sql.tpldet_col
        self.chn = drm[dmc.index("drm_chain")]
        self.acc = drm[dmc.index("drm_acno")]
        eml = drm[dmc.index("drm_acc_email")]
        self.form.account_details("drm", dmc, drm, 1)
        if drm[dmc.index("drm_stames")]:
            self.mesno = drm[dmc.index("drm_stames")]
        else:
            self.mesno = self.mess
        for col in dmc:
            d = "%s_C00" % col
            if d in self.form.newdic:
                dat = drm[dmc.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        bals = Balances(self.opts["mf"], "DRS", self.opts["conum"], self.curdt,
                        (self.chn, self.acc))
        if self.alloc == "Y":
            tt = "A"
        else:
            tt = "Y"
        obal, self.tbal, self.ages, trns = bals.doAllBals(trans=tt)
        if not trns[1]:
            return
        if self.sttyp == "O":
            self.tots = [0.0, 0.0, 0.0]
            cmth = False
            tran = copyList(trns[1])
            for t in tran:
                if t[dtc.index("drt_type")] not in (2, 6) and \
                        t[dtc.index("drt_curdt")] == self.curdt:
                    self.tots[1] = float(ASD(self.tots[1]) + \
                        ASD(t[dtc.index("drt_tramt")]))
                    if t[dtc.index("drt_taxamt")]:
                        self.tots[1] = float(ASD(self.tots[1]) - \
                            ASD(t[dtc.index("drt_taxamt")]))
                        self.tots[2] = float(ASD(self.tots[2]) + \
                            ASD(t[dtc.index("drt_taxamt")]))
                else:
                    self.tots[0] = float(ASD(self.tots[0]) + \
                        ASD(t[dtc.index("drt_tramt")]))
                if t[dtc.index("drt_curdt")] == self.curdt:
                    cmth = True
                elif self.oitem == "N":
                    trns[1].remove(t)
            if self.zeros == "N" and not self.tbal and not cmth:
                return
            if self.minus == "N" and self.tbal < 0:
                return
            if self.oitem == "N" and obal:
                t[trns[0].index("drt_type")] = 3
                t[trns[0].index("drt_ref1")] = "O/Bal"
                t[trns[0].index("drt_batch")] = ""
                t[trns[0].index("drt_trdt")] = (self.curdt * 100) + 1
                t[trns[0].index("drt_ref2")] = ""
                t[trns[0].index("drt_tramt")] = obal
                t[trns[0].index("drt_taxamt")] = 0
                t[trns[0].index("drt_desc")] = "Opening Balance"
                t[trns[0].index("drt_taxind")] = ""
                t[trns[0].index("drt_batind")] = ""
                trns[1].insert(0, t)
            if len(trns[1]) <= self.form.maxlines:
                self.doBody(trns[0], trns[1], tdc)
            else:
                pages = int(len(trns[1]) / self.form.maxlines)
                if len(trns[1]) % self.form.maxlines:
                    pages += 1
                if pages <= self.pages:
                    self.doBody(trns[0], trns[1], tdc)
                else:
                    bal = 0
                    lines = len(
                        trns[1]) - (self.pages * self.form.maxlines) + 1
                    for _ in range(lines):
                        trn = trns[1].pop(0)
                        bal = float(
                            ASD(bal) + ASD(trn[dtc.index("drt_tramt")]))
                    trn[trns[0].index("drt_type")] = 3
                    trn[trns[0].index("drt_ref1")] = "B/FWD"
                    trn[trns[0].index("drt_batch")] = ""
                    trn[trns[0].index("drt_ref2")] = ""
                    trn[trns[0].index("drt_tramt")] = bal
                    trn[trns[0].index("drt_taxamt")] = 0
                    trn[trns[0].index("drt_desc")] = "Balance Brought Forward"
                    trn[trns[0].index("drt_taxind")] = ""
                    trn[trns[0].index("drt_batind")] = ""
                    trns[1].insert(0, trn)
                    self.doBody(trns[0], trns[1], tdc)
        else:
            if self.zeros == "N" and not self.tbal:
                return
            if self.minus == "N" and self.tbal < 0:
                return
            self.doBody(trns[0], trns[1], tdc)
        self.doTotal(tdc)
        self.doTail(tdc)
        if self.df.repeml[1] == "Y" and not self.emadd:
            self.df.repeml[2] = eml
            self.doPrint()

    def doPrint(self):
        if self.df.repeml[1] == "Y" and not self.emadd:
            key = "%s_%s_%s" % (self.opts["conum"], self.chn, self.acc)
        else:
            key = "%s_all_all" % self.opts["conum"]
        pdfnam = getModName(self.opts["mf"].rcdic["wrkdir"],
                            self.__class__.__name__,
                            key,
                            ext="pdf")
        self.form.output(pdfnam, "F")
        doPrinter(mf=self.opts["mf"],
                  conum=self.opts["conum"],
                  pdfnam=pdfnam,
                  header="%s Statement for %s as at %s" %
                  (self.opts["conam"], self.acc, self.dated),
                  repprt=self.df.repprt,
                  fromad=self.fromad,
                  repeml=self.df.repeml)
        if self.df.repeml[1] == "Y":
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()

    def doBody(self, dtc, drt, tdc):
        page = 0
        count = 0
        rbal = 0
        text = tdc.index("tpd_text")
        for trans in drt:
            if not count:
                page += 1
                count = self.doHeader(page)
            if count == self.form.maxlines:
                page = self.doCfwd(page)
                count = self.doHeader(page)
            for cod in self.form.body:
                if cod == "type_code":
                    c = "drt_type"
                elif cod == "line_paid":
                    c = "paid"
                elif cod == "line_balance":
                    c = "balance"
                else:
                    c = cod
                d = "%s_C%02i" % (cod, count)
                if cod == "type_code":
                    ttyp = trans[dtc.index(c)]
                    self.form.newdic[d][text] = drtrtp[ttyp - 1][0]
                elif self.sttyp == "O" and cod == "running_balance":
                    self.form.newdic[d][text] = rbal
                else:
                    self.form.newdic[d][text] = trans[dtc.index(c)]
                    if self.sttyp == "O" and cod == "drt_tramt":
                        rbal = float(ASD(rbal) + ASD(trans[dtc.index(c)]))
                self.form.doDrawDetail(self.form.newdic[d])
            count += 1
        for x in range(count, self.form.maxlines):
            for cod in self.form.body:
                d = "%s_C%02i" % (cod, x)
                self.form.newdic[d][tdc.index("tpd_text")] = "BLANK"
                self.form.doDrawDetail(self.form.newdic[d])

    def doHeader(self, page):
        self.form.add_page()
        tdc = self.form.sql.tpldet_col
        for key in self.form.newkey:
            nl = copyList(self.form.newdic[key])
            if nl[tdc.index("tpd_place")] != "A":
                continue
            if nl[tdc.index("tpd_detseq")] == "drm_acno_C00":
                nl[tdc.index("tpd_text")] = self.acc
            elif nl[tdc.index("tpd_detseq")] == "page_number_C00":
                nl[tdc.index("tpd_text")] = str(page)
            self.form.doDrawDetail(nl)
        return 0

    def doCfwd(self, page):
        if "carried_forward" in self.form.tptp:
            tdc = self.form.sql.tpldet_col
            line = copyList(self.form.cfwd)
            line[tdc.index("tpd_text")] = "Continued on Page %i" % (page + 1)
            self.form.doDrawDetail(line)
        return page + 1

    def doTotal(self, tdc):
        for c in self.form.total:
            if self.ageing == "N" and c != "total_balance":
                continue
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                line = self.form.newdic[d]
                if c == "120_day_balance":
                    line[tdc.index("tpd_text")] = self.ages[4]
                elif c == "90_day_balance":
                    line[tdc.index("tpd_text")] = self.ages[3]
                elif c == "60_day_balance":
                    line[tdc.index("tpd_text")] = self.ages[2]
                elif c == "30_day_balance":
                    line[tdc.index("tpd_text")] = self.ages[1]
                elif c == "current_balance":
                    line[tdc.index("tpd_text")] = self.ages[0]
                elif c == "total_arrears":
                    line[tdc.index("tpd_text")] = self.tots[0]
                elif c == "month_exclusive":
                    line[tdc.index("tpd_text")] = self.tots[1]
                elif c == "month_tax":
                    line[tdc.index("tpd_text")] = self.tots[2]
                elif c == "total_balance":
                    line[tdc.index("tpd_text")] = self.tbal
                self.form.doDrawDetail(line)

    def doTail(self, tdc):
        for c in self.form.tail:
            if c == "message" and not self.mesno:
                continue
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                if d == "message_C00" and self.mesno:
                    mes = self.sql.getRec("ctlmes",
                                          cols=["mss_detail"],
                                          where=[("mss_system", "=", "STA"),
                                                 ("mss_message", "=",
                                                  self.mesno)],
                                          limit=1)
                    self.form.newdic[d][tdc.index("tpd_text")] = mes[0]
                self.form.doDrawDetail(self.form.newdic[d])

    def doExit(self):
        self.df.closeProcess()
        self.opts["mf"].closeLoop()
Ejemplo n.º 7
0
class ln3040(object):
    def __init__(self, **opts):
        self.opts = opts
        if self.setVariables():
            self.mainProcess()
            self.opts["mf"].startLoop()

    def setVariables(self):
        gc = GetCtl(self.opts["mf"])
        lonctl = gc.getCtl("lonctl", self.opts["conum"])
        if not lonctl:
            return
        self.capb = lonctl["cln_capb"]
        self.capf = lonctl["cln_capf"]
        self.lint = lonctl["cln_last"]
        self.stpl = lonctl["cln_tplnam"]
        self.fromad = lonctl["cln_emadd"]
        t = time.localtime()
        self.sysdtw = (t[0] * 10000) + (t[1] * 100) + t[2]
        self.curdt = int(self.sysdtw / 100)
        self.sper = int(self.opts["period"][1][0] / 100)
        self.eper = int(self.opts["period"][2][0] / 100)
        self.sql = Sql(self.opts["mf"].dbm, [
            "ctlmes", "ctlmst", "ctlynd", "lonmf1", "lonmf2", "lontrn",
            "tplmst"
        ],
                       prog=self.__class__.__name__)
        if self.sql.error:
            return
        return True

    def mainProcess(self):
        tpm = {
            "stype":
            "R",
            "tables": ("tplmst", ),
            "cols": (("tpm_tname", "", 0, "Template"), ("tpm_title", "", 0,
                                                        "Title", "Y")),
            "where": [("tpm_type", "=", "S"), ("tpm_system", "=", "LON")],
            "order":
            "tpm_tname"
        }
        r1s = (("Yes", "Y"), ("No", "N"))
        r2s = (("Number", "A"), ("Name", "N"))
        fld = ((("T", 0, 0, 0), "INA", 20, "Template Name", "", self.stpl, "Y",
                self.doTplNam, tpm, None, None),
               (("T", 0, 1, 0), "Id2", 7, "Start Period", "", self.sper, "N",
                self.doSPer, None, None,
                None), (("T", 0, 2, 0), "ID2", 7, "End Period", "", self.eper,
                        "N", self.doEPer, None, None,
                        None), (("T", 0, 3, 0), ("IRB", r1s), 0, "Whole File",
                                "", "N", "N", self.doWhole, None, None, None),
               (("T", 0, 4, 0), ("IRB", r2s), 0, "Sort Order", "", "A", "N",
                self.doSort, None, None, None),
               (("T", 0, 5, 0), ("IRB", r1s), 0, "Include Zero Balances", "",
                "N", "N", self.doZeros, None, None, None),
               (("T", 0, 6, 0), ("IRB", r1s), 0, "Include Pending Interest",
                "", "N", "N", self.doPend, None, None, None),
               (("T", 0, 7, 0), ("IRB", r1s), 0, "Interest Totals Only", "",
                "Y", "N", self.doITots, None, None, None))
        tnd = ((self.doEnd, "Y"), )
        txt = (self.doExit, )
        self.df = TartanDialog(self.opts["mf"],
                               eflds=fld,
                               tend=tnd,
                               txit=txt,
                               view=("N", "V"),
                               mail=("B", "Y"))

    def doTplNam(self, frt, pag, r, c, p, i, w):
        acc = self.sql.getRec("tplmst",
                              where=[("tpm_tname", "=", w),
                                     ("tpm_type", "=", "S"),
                                     ("tpm_system", "=", "LON")],
                              limit=1)
        if not acc:
            return "Invalid Template Name"
        self.tname = w

    def doSPer(self, frt, pag, r, c, p, i, w):
        self.sperw = w
        self.sperd = self.df.t_disp[pag][0][p]

    def doEPer(self, frt, pag, r, c, p, i, w):
        self.eperw = w
        self.eperd = self.df.t_disp[pag][0][p]
        self.date = CCD(mthendDate((w * 100) + 1), "D1", 10)

    def doWhole(self, frt, pag, r, c, p, i, w):
        self.whole = w

    def doSort(self, frt, pag, r, c, p, i, w):
        self.sort = w

    def doZeros(self, frt, pag, r, c, p, i, w):
        self.zeros = w

    def doPend(self, frt, pag, r, c, p, i, w):
        self.pend = w

    def doITots(self, frt, pag, r, c, p, i, w):
        self.itot = w

    def doEnd(self):
        self.df.closeProcess()
        self.emadd = self.df.repeml[2]
        if self.whole == "N":
            recs = getSingleRecords(self.opts["mf"],
                                    "lonmf1", ("lm1_acno", "lm1_name"),
                                    where=[("lm1_cono", "=",
                                            self.opts["conum"])])
        else:
            whr = [("lm1_cono", "=", self.opts["conum"])]
            if self.sort == "A":
                odr = "lm1_acno"
            else:
                odr = "lm1_name"
            recs = self.sql.getRec("lonmf1", where=whr, order=odr)
        if not recs:
            showError(self.opts["mf"].body, "Error", "No Accounts Selected")
        else:
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()
            p = ProgressBar(self.opts["mf"].body, mxs=len(recs), esc=True)
            for num, rec in enumerate(recs):
                p.displayProgress(num)
                if p.quit:
                    break
                self.doProcess(rec)
            p.closeProgress()
            if p.quit or not self.form.page:
                pass
            elif self.df.repeml[1] == "N" or self.emadd:
                self.df.repeml[2] = self.emadd
                self.doPrint()
        self.opts["mf"].closeLoop()

    def doLoadStatic(self):
        cmc = self.sql.ctlmst_col
        ctm = self.sql.getRec("ctlmst",
                              where=[("ctm_cono", "=", self.opts["conum"])],
                              limit=1)
        for fld in cmc:
            dat = ctm[cmc.index(fld)]
            if fld in self.form.tptp:
                if fld == "ctm_logo":
                    self.form.letterhead(cmc, ctm, fld, dat)
                    continue
                self.form.tptp[fld][1] = dat
        if "letterhead" in self.form.tptp:
            self.form.letterhead(cmc, ctm, "letterhead", None)
        self.form.document_date(self.date.disp)
        self.form.bank_details(cmc, ctm, 0)

    def doProcess(self, lm1):
        l1c = self.sql.lonmf1_col
        tdc = self.form.sql.tpldet_col
        self.acno = lm1[l1c.index("lm1_acno")]
        self.emlto = lm1[l1c.index("lm1_email")]
        self.form.account_details("lm1", l1c, lm1, 1)
        for col in l1c:
            d = "%s_C00" % col
            if d in self.form.newdic:
                dat = lm1[l1c.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        lonmf2 = self.sql.getRec("lonmf2",
                                 where=[("lm2_cono", "=", self.opts["conum"]),
                                        ("lm2_acno", "=", self.acno)],
                                 order="lm2_loan")
        for loan in lonmf2:
            self.doStatement(tdc, loan)

    def doStatement(self, tdc, lm2):
        l2c = self.sql.lonmf2_col
        ltc = copyList(self.sql.lontrn_col)
        self.loan = lm2[l2c.index("lm2_loan")]
        for col in l2c:
            d = "%s_C00" % col
            if d in self.form.newdic:
                dat = lm2[l2c.index(col)]
                self.form.newdic[d][tdc.index("tpd_text")] = dat
        if self.pend == "Y":
            # Raise Pending Interest
            LoanInterest("L",
                         self.opts["mf"].dbm,
                         lm2,
                         update="Y",
                         tdate=self.date.work,
                         batch="Pending",
                         curdt=self.curdt,
                         capnm="")
        # Get Transactions
        whr = [("lnt_cono", "=", self.opts["conum"]),
               ("lnt_acno", "=", self.acno), ("lnt_loan", "=", self.loan),
               ("lnt_curdt", "<=", self.eperw)]
        odr = "lnt_curdt, lnt_trdt, lnt_type, lnt_refno"
        if self.itot == "Y":
            w = whr[:]
            w.append(("lnt_type", "<>", 4))
            trns = self.sql.getRec("lontrn", where=w, order=odr)
            if self.capb == "A":  # Anniversary
                fcap = [lm2[l2c.index("lm2_start")], 0]
                if self.capf == "A":
                    fcap[1] = projectDate(fcap[0], 1, typ="years")
                else:
                    fcap[1] = projectDate(fcap[0], 6, typ="months")
            else:  # Financial
                periods = self.sql.getRec(
                    "ctlynd",
                    cols=["cye_period", "cye_start", "cye_end"],
                    where=[("cye_cono", "=", self.opts["conum"])],
                    order="cye_period")
                fcap = [periods[0][1], periods[0][2]]
                if self.capf == "B":  # Bi-Annual
                    fcap[1] = projectDate(fcap[1], -6, typ="months")
            capdt = [copyList(fcap)]
            while fcap[1] < self.date.work:
                if self.capf == "A":
                    fcap[0] = projectDate(fcap[0], 1, typ="years")
                    fcap[1] = projectDate(fcap[1], 1, typ="years")
                else:
                    fcap[0] = projectDate(fcap[0], 6, typ="months")
                    fcap[1] = projectDate(fcap[1], 6, typ="months")
                if fcap[1] <= self.date.work:
                    capdt.append(copyList(fcap))
            for capd in capdt:
                w = whr[:]
                w.append(("lnt_type", "=", 4))
                w.append(("lnt_trdt", "between", capd[0], capd[1]))
                ints = self.sql.getRec("lontrn", where=w, order=odr)
                if not ints:
                    continue
                ddes = "Dr Int %s to %s"
                cdes = "Cr Int %s to %s"
                dbal = 0
                cbal = 0
                for trn in ints:
                    amt = trn[ltc.index("lnt_tramt")]
                    if amt < 0:
                        cbal = float(ASD(cbal) + ASD(amt))
                    else:
                        dbal = float(ASD(dbal) + ASD(amt))
                if dbal:
                    trn[ltc.index("lnt_tramt")] = dbal
                    trn[ltc.index("lnt_desc")] = ddes % (CCD(
                        int(capd[0] / 100), "D2",
                        7).disp, CCD(int(capd[1] / 100), "D2", 7).disp)
                    trns.append(copyList(trn))
                if cbal:
                    trn[ltc.index("lnt_tramt")] = cbal
                    trn[ltc.index("lnt_desc")] = cdes % (CCD(
                        int(capd[0] / 100), "D2",
                        7).disp, CCD(int(capd[1] / 100), "D2", 7).disp)
                    trns.append(copyList(trn))
            trns = sorted(trns, key=itemgetter(5))
        else:
            trns = self.sql.getRec("lontrn", where=whr, order=odr)
        if not trns:
            return
        self.bal = 0
        self.tots = 0
        if self.sperw:
            obal = 0
            trans = copyList(trns)
            for trn in trans:
                tramt = trn[self.sql.lontrn_col.index("lnt_tramt")]
                if trn[self.sql.lontrn_col.index("lnt_curdt")] < self.sperw:
                    obal = float(ASD(obal) + ASD(tramt))
                    trns.remove(trn)
            trn[ltc.index("lnt_type")] = 3
            trn[ltc.index("lnt_trdt")] = (self.sperw * 100) + 1
            trn[ltc.index("lnt_refno")] = "O/Bal"
            trn[ltc.index("lnt_batch")] = ""
            trn[ltc.index("lnt_tramt")] = obal
            trn[ltc.index("lnt_desc")] = "Opening Balance"
            trn[ltc.index("lnt_batind")] = ""
            trns.insert(0, trn)
        trans = []
        for trn in trns:
            tramt = trn[ltc.index("lnt_tramt")]
            self.tots = float(ASD(self.tots) + ASD(tramt))
            if tramt < 0:
                trn.extend([0, tramt])
            else:
                trn.extend([tramt, 0])
            trans.append(trn)
        ltc.extend(["line_debit", "line_credit"])
        if self.zeros == "N" and not self.tots:
            return
        self.doBody(ltc, trans, tdc)
        self.doTotal(tdc)
        self.doTail(tdc)
        if self.pend == "Y":
            self.opts["mf"].dbm.rollbackDbase()
        if self.df.repeml[1] == "Y" and not self.emadd:
            self.df.repeml[2] = self.emlto
            self.doPrint()

    def doPrint(self):
        if self.df.repeml[1] == "Y" and not self.emadd:
            key = "%s_%s_%s" % (self.opts["conum"], self.acno, self.loan)
        else:
            key = "%s_all_all" % self.opts["conum"]
        pdfnam = getModName(self.opts["mf"].rcdic["wrkdir"],
                            self.__class__.__name__,
                            key,
                            ext="pdf")
        self.form.output(pdfnam, "F")
        doPrinter(mf=self.opts["mf"],
                  conum=self.opts["conum"],
                  pdfnam=pdfnam,
                  header="%s Statement at %s" %
                  (self.opts["conam"], self.date.disp),
                  repprt=self.df.repprt,
                  fromad=self.fromad,
                  repeml=self.df.repeml)
        if self.df.repeml[1] == "Y":
            self.form = DrawForm(self.opts["mf"].dbm,
                                 self.tname,
                                 wrkdir=self.opts["mf"].rcdic["wrkdir"])
            self.doLoadStatic()
            self.form.doNewDetail()

    def doBody(self, ltc, lnt, tdc):
        page = 0
        count = 0
        text = tdc.index("tpd_text")
        for trans in lnt:
            if not count:
                page += 1
                count = self.doHeader(page)
            if count == self.form.maxlines:
                page = self.doCfwd(page)
                count = self.doHeader(page)
            for cod in self.form.body:
                if cod == "type_code":
                    c = "lnt_type"
                elif cod == "line_paid":
                    c = "paid"
                else:
                    c = cod
                d = "%s_C%02i" % (cod, count)
                if cod == "type_code":
                    ttyp = trans[ltc.index(c)]
                    self.form.newdic[d][text] = lntrtp[ttyp - 1][0]
                elif c in ("line_debit", "line_credit"):
                    tramt = trans[ltc.index(c)]
                    self.bal = float(ASD(self.bal) + ASD(tramt))
                    self.form.newdic[d][text] = trans[ltc.index(c)]
                elif c == "line_balance":
                    self.form.newdic[d][text] = self.bal
                else:
                    self.form.newdic[d][text] = trans[ltc.index(c)]
                self.form.doDrawDetail(self.form.newdic[d])
            count += 1
        for x in range(count, self.form.maxlines):
            for cod in self.form.body:
                d = "%s_C%02i" % (cod, x)
                self.form.newdic[d][tdc.index("tpd_text")] = "BLANK"
                self.form.doDrawDetail(self.form.newdic[d])

    def doHeader(self, page):
        self.form.add_page()
        tdc = self.form.sql.tpldet_col
        for key in self.form.newkey:
            nl = copyList(self.form.newdic[key])
            if nl[tdc.index("tpd_place")] != "A":
                continue
            if nl[tdc.index("tpd_detseq")] == "lm1_acno_C00":
                nl[tdc.index("tpd_text")] = self.acno
            elif nl[tdc.index("tpd_detseq")] == "page_number_C00":
                nl[tdc.index("tpd_text")] = str(page)
            self.form.doDrawDetail(nl)
        return 0

    def doCfwd(self, page):
        if "carried_forward" in self.form.tptp:
            tdc = self.form.sql.tpldet_col
            line = copyList(self.form.cfwd)
            line[tdc.index("tpd_text")] = "Continued on Page %i" % (page + 1)
            self.form.doDrawDetail(line)
        return page + 1

    def doTotal(self, tdc):
        for c in self.form.total:
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                line = self.form.newdic[d]
                if c == "total_balance":
                    line[tdc.index("tpd_text")] = self.tots
                self.form.doDrawDetail(line)

    def doTail(self, tdc):
        for c in self.form.tail:
            t = "%s_T00" % c
            if c in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[c])
            elif t in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[t])
            d = "%s_C00" % c
            if d in self.form.newdic:
                self.form.doDrawDetail(self.form.newdic[d])

    def doExit(self):
        self.df.closeProcess()
        self.opts["mf"].closeLoop()