import BP, cPickle, time exe = BP.BankPerfectExePath() def DaysOfMonth(y, m): if m == 2 and (y % 4 == 0) and (y % 100 != 0 or y % 400 == 0): return 29 else: return [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][m - 1] def AddD(date, delta): s = time.mktime(date + (3, 0, 0, 0, 0, 0)) return time.gmtime(s + 86400 * delta)[:3] def AddM(date, delta): y, m, d = date KeepLastDay = DaysOfMonth(y, m) == d m += delta if delta > 0: while m > 12: y += 1; m -= 12 elif delta < 0: while m < 1: y -= 1; m += 12; LastDay = DaysOfMonth(y, m) if KeepLastDay or d > LastDay: return y, m, LastDay else: return y, m, d def Add2Date(d, delta, unit): if unit == 0: return AddD(d, delta) elif unit == 1: return AddM(d, delta) else: return d[0] + delta, d[1], d[2] def Reduce2Month(Vals): d = {} for v in Vals: k = (v[0][0], v[0][1]) d[k] = (v[1], d.get(k, (0, 0))[1] + v[2]) K = d.keys() K.sort() return [((k[0], k[1], DaysOfMonth(k[0], k[1])), d[k][0], d[k][1]) for k in K] def Cumul(Vals): t = sum([BP.OperationAmount[Acc][i] for i in BP.VisibleLines()]) last_date = max([(int(d[6:]), int(d[3:5]), int(d[:2])) for d in BP.OperationDate[Acc]]) l = [(last_date, "Solde actuel", t, t)] Max = abs(t) for v in Vals: if v[0] > last_date: t += v[2] if abs(t) > Max: Max = abs(t) l.append( (v[0], v[1], v[2], t) ) return [v + (int(1000 * v[3] / Max), ) for v in l] def ApplyRec(Vals, Rec, Acc, Until): End, n = Rec["enddate"], "nextdate" if End != (0, 0, 0) and End < Until: Until = End while Rec[n] <= Until: Vals.append((Rec[n], Rec["tiers"], Rec["montant"])) Rec[n] = Add2Date(Rec[n], Rec["delta"], Rec["deltaunit"]) def bar(v, p): w = p / 10 if w == 0: clr = "dddddd" elif v < 0: clr = "cc0000" else: clr = "007700" head = '' foot = '
' if w == 100 or w == 0: return '%s%s' %(head, clr, foot) else: return '%s%s' %(head, clr, w, 100 - w, foot) def Export(Sender): s = "\n".join(['%.2d %s %.4d%s%.2f%.2f%s' %(v[0][2], mois[v[0][1] - 1], v[0][0], v[1], v[2], v[3], bar(v[2], v[3])) for v in Vals]) s = '

Tableau prévisionnel du compte %s

\n\n%s\n
DateMontantSoldeProgression
' %(path, s) open("%sexport.htm" %exe, "w").write(s) BP.ShellExecute("open", "%sexport.htm" %exe, "", 1) def draw(Sender, ACol, ARow, R, State): cv = Sender.Canvas cv.Font.Color = 0x00000000 if "gdSelected" in State: cv.Brush.Color = 0x00dec5b9 elif ARow > 0 and ARow % 2 == 0: cv.Brush.Color = 0x00f0f0f0 cv.FillRect(R) try: if ARow == 0: s = ["Date", "Tiers", "Montant", "Solde", "Progression"][ACol] cv.Font.Style = ["fsBold"] else: v = Vals[ARow - 1] if ACol == 0: s = "%.2d %s %.4d" %(v[0][2], mois[v[0][1] - 1], v[0][0]) elif ACol == 1: s = str(v[1]) elif ACol in [2, 3]: s = "%.2f" %v[ACol] if v[ACol] < 0: cv.Font.Color = 0x000000CC else: cv.Font.Color = 0x00003300 else: s = "" w = int(abs(v[4]) * (R.Right - R.Left - 6) / 1000.0) R.Left, R.Right, R.Top, R.Bottom = R.Left + 3, R.Left + w, R.Top + 6, R.Bottom - 6 if R.Right <= R.Left: R.Right = R.Left + 1 cv.Brush.Color = 0x00000000 cv.FillRect(R) R.Left, R.Right, R.Top, R.Bottom = R.Left + 1, R.Right - 1, R.Top + 1, R.Bottom - 1 if v[4] < 0: cv.Brush.Color = 0x000000CC else: cv.Brush.Color = 0x00007700 cv.FillRect(R) except: s = "" if s != "": Y = (R.Bottom - R.Top - dY) / 2 if Y < 3: Y = 3 if ACol == 0 or ARow == 0: X = (R.Right - R.Left - cv.TextWidth(s)) / 2 if X < 3: X = 3 X = R.Left + X elif ACol in [2, 3]: X = R.Right - 3 - cv.TextWidth(s) else: X = R.Left + 3 cv.TextRect(R, X, R.Top + Y, s) cv.Brush.Style = 1 def Resize(S): Grid.DefaultColWidth = (Grid.Width - 40) / 5 f = CreateComponent("TForm", None) f.SetProps(Position="poMainFormCenter", Width=280, Height=205, BorderStyle="bsSingle", BorderIcons=["biSystemMenu","biMinimize"], Caption="Prévisions") f.Font.Name = "Tahoma" L1 = CreateComponent("TLabel", f) L1.SetProps(Parent=f, Left=30, Top=30, Width=201, Caption="Calculer le tableau prévisionnel jusqu'au :") Dt1 = CreateComponent("TDateTimePicker", f) Dt1.SetProps(Parent=f, Left=30, Top=50, Width=214, Height=20, Format="dddd dd MMMM yyyy") now = time.localtime()[:3] now = (now[0] + 1, now[1], now[2]) Dt1.Date = time.mktime(now + (1, 0, 0, 0, 0, 0)) / 86400 + 25569 ChkEnd = CreateComponent("TCheckBox", f) ChkEnd.SetProps(Parent=f, Left=30, Top=80, Width=214, Caption="Afficher des soldes mensuels") BCnl = CreateComponent("TButton", f) BCnl.SetProps(Parent=f, Left=48, Top=120, Width=90, Height=25, Caption="Annuler", Cancel=1, ModalResult=2) BOK = CreateComponent("TButton", f) BOK.SetProps(Parent=f, Left=144, Top=120, Width=90, Height=25, Caption="OK", Default=1, ModalResult=1) try: fn = open("%sScripts\\Scheduler\\scheduler.dat" %exe, "rb") files = cPickle.load(fn) fn.close() except: files = {} AccNames = BP.AccountName; path = BP.BankPerfectFileName() while path.find("\\") > -1: path = path[path.find("\\") + 1:] if files.has_key(path): LenAcc = BP.AccountCount() f.ShowModal() if f.ModalResult == 1: Until = time.gmtime((int(Dt1.Date) - 25569) * 86400)[:3] Vals = [] Acc = BP.AccountCurrent() for Rec in files[path]: if Rec["account"] == Acc: ApplyRec(Vals, Rec, Acc, Until) Vals.sort() if ChkEnd.Checked: Vals = Reduce2Month(Vals) Vals = Cumul(Vals) mois = ["jan", "fév", "mar", "avr", "mai", "jun", "jul", "aoû", "sep", "oct", "nov", "déc"] f = CreateComponent("TForm", None) f.SetProps(Position="poMainFormCenter", Width=600, Height=400, Caption="Prévisions", OnResize=Resize) Grid = CreateComponent("TDrawGrid", f) Grid.SetProps(Parent=f, DefaultColWidth=100, RowCount=len(Vals) + 1, Anchors=["akLeft", "akRight", "akTop", "akBottom"], OnDrawCell=draw, Left=30, Top=30, Width=540, Height=310, FixedCols = 0, FixedRows = 1, ColCount=5, Options = ["goFixedHorzLine", "goVertLine", "goDrawFocusSelected", "goRowSelect", "goThumbTracking"]) BExport = CreateComponent("TButton", f) BExport.SetProps(Parent=f, Left=380, Top=350, Width=90, Height=25, Anchors=["akRight","akBottom"], Caption="Export HTML", OnClick=Export) BOK = CreateComponent("TButton", f) BOK.SetProps(Parent=f, Left=480, Top=350, Width=90, Height=25, Anchors=["akRight","akBottom"], Caption="OK", Default=1, Cancel=1, ModalResult=1) dY = Grid.Canvas.TextHeight('A') Grid.DefaultRowHeight = 20 f.ShowModal() else: BP.MsgBox("Il n'y a aucune échéance enregistrée", 64)