home *** CD-ROM | disk | FTP | other *** search
- #Version 1.5
-
- import BP, cPickle, time
- global modes, files, inserted_lines, refused_lines
-
- inserted_lines = []
- transfer_other_mode = {0: 8, 1: 6, 2: 7, 3: 8, 4: 8, 5: 8, 6: 1, 7: 2, 8: 3}
- data_path = "%sScripts\\Scheduler\\scheduler.dat" %BP.BankPerfectExePath()
- modes = ["Carte", "Retrait DAB", "ChΦque Θmis", "PrΘlΦvement", "Virement Θmis", "TIP", "Versement", "DΘp⌠t de chΦque", "Virement reτu"]
-
- def DaysOfMonth(y, m):
- is_leap_year = (y % 4 == 0) and (y % 100 != 0 or y % 400 == 0)
- days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
- if m == 2 and is_leap_year: return 29
- else: return days[m - 1]
-
- def add_years(date, delta):
- y, m, d = date
- return y + delta, m, d
-
- def AddDays(dt, delta):
- y, m, d = dt
- d += delta
- if delta > 0:
- while d > DaysOfMonth(y, m):
- d -= DaysOfMonth(y, m)
- m += 1
- if m == 13: y += 1; m = 1
- else:
- while d < 1:
- m -= 1
- d += DaysOfMonth(y, m)
- if m == 0: y -= 1; m = 12
- return y, m, d
-
- def AddMonths(date, delta):
- y, m, d = date
- is_last_day = 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;
-
- last_day = DaysOfMonth(y, m)
- if is_last_day or d > last_day: return y, m, last_day
- else: return y, m, d
-
- def add_to_date(date, delta, unit):
- if unit == 0: #jours
- return AddDays(date, delta)
- elif unit == 1: #mois
- return AddMonths(date, delta)
- else: #annΘes
- return add_years(date, delta)
-
- def date_to_str(date):
- if date == (0, 0, 0): y, m, d = time.localtime()[:3]
- else: y, m, d = date
- return "%.02d/%.02d/%.04d" %(d, m, y)
-
- def extract_file_name(path):
- while path.find("\\") > -1:
- path = path[path.find("\\") + 1:]
- return path
-
- def record_to_str(record):
- return "Compte %s : ΘchΘance \"%s\" du %s (montant : %.2f)" %(account_names[record["account"]], record["tiers"], date_to_str(record["nextdate"]), record["montant"])
-
- def Easter(Y):
- C = Y / 100
- H = (19 * (Y % 19) + C - (C / 4) - ((8 * C + 13) / 25) + 15) % 30
- I = ((H / 28) * (29 / (H + 1)) * ((21 - (Y % 19)) / 11) - 1) * (H / 28) + H
- R = 28 + I - (((Y / 4 + Y) + I + 2 + (C / 4) - C) % 7)
- if R <= 31: return (Y, 3, R)
- else: return (Y, 4, R - 31)
-
- def DayOfWeek(y, m, d):
- t = time.mktime((y, m, d, 12, 0, 0, 0, 0, 0))
- return time.localtime(t)[6]
-
- def NonWorkingDay(dt, include): #include: DSF, D=dim, S=sam, F=FΘriΘ
- y, m, d = dt
- dw = DayOfWeek(y, m, d)
- if "D" in include and dw == 6: return 1
- if "S" in include and dw == 5: return 1
- if "F" in include:
- if (m, d) in [(1, 1), (5, 1), (8, 1), (7, 14), (8, 15), (11, 1), (11, 11), (12, 25)]: return 1
- E = Easter(y)
- #Lundi de pΓques, Jeudi ascension, Lundi de Pentecote
- if (y, m, d) in [AddDays(E, 1), AddDays(E, 39), AddDays(E, 50)]: return 1
- return 0
-
- def NextWorkingDate(dt, include):
- while NonWorkingDay(dt, include): dt = AddDays(dt, 1)
- return dt
-
- def apply_record(record):
- global inserted_lines, refused_lines
- until = time.localtime()[:3]
- enddate = record["enddate"]
- if enddate != (0, 0, 0) and enddate < until: until = enddate
-
- account_number1 = record["account"]
- account_number2 = record.get("transfer", -1)
- max_account_number = BP.AccountCount() - 1
- if account_number1 > max_account_number or account_number2 > max_account_number:
- refused_lines.append(record_to_str(record) + "... compte inexistant")
- else:
- while record["nextdate"] <= until:
- DateInsert = record["nextdate"]
- if record.get("Postpone", ""): DateInsert = NextWorkingDate(DateInsert, record["Postpone"])
- date = date_to_str(DateInsert)
- count1 = BP.OperationCount[account_number1]
- ctg = record["categ"]
- if ctg >= CntCtg: ctg = -1
- if account_number2 > -1:
- #Transfert de compte α compte
- n1 = account_names[account_number1]
- n2 = account_names[account_number2]
- mode1 = record["mode"]
- mode2 = transfer_other_mode[mode1]
- if mode1 in [0, 1, 2, 3, 4, 5]:
- direction1 = "vers"
- direction2 = "depuis"
- else:
- direction1 = "depuis"
- direction2 = "vers"
- BP.LineAdd(account_number1, date, modes[mode1], "Transfert %s le compte %s" %(direction1, n2), \
- record["details"], ctg, record["montant"], 0)
- BP.LineAdd(account_number2, date, modes[mode2], "Transfert %s le compte %s" %(direction2, n1), \
- record["details"], ctg, -record["montant"], 0)
- else:
- #OpΘration standard
- BP.LineAdd(account_number1, date, modes[record["mode"]], "[auto] " + record["tiers"], \
- record["details"], ctg, record["montant"], 0)
-
- count2 = BP.OperationCount[account_number1]
- if count1 == count2: refused_lines.append(record_to_str(record))
- else: inserted_lines.append(record_to_str(record))
- record["nextdate"] = add_to_date(record["nextdate"], record["delta"], record["deltaunit"])
-
-
-
-
- try:
- f = open(data_path, "rb")
- files = cPickle.load(f)
- f.close()
- except:
- files = {}
-
- account_names = BP.AccountName;
- inserted_lines = []
- refused_lines = []
- filename = extract_file_name(BP.BankPerfectFileName())
-
- if files.has_key(filename):
- CntCtg = BP.CategCount()
- records = files[filename]
-
- lastaccount = BP.AccountCount() - 1
- for record in records:
- if record.get("autoinsert", 1) and record["account"] <= lastaccount:
- apply_record(record)
-
- if len(inserted_lines) + len(refused_lines) > 0:
- f = open(data_path, "wb")
- cPickle.dump(files, f, 1)
- f.close()
-
- s = "EchΘancier :\r\n\r\n"
- if len(inserted_lines) > 0: s += "Lignes insΘrΘes :\r\n%s\r\n" %"\r\n".join(inserted_lines)
- if len(refused_lines) > 0: s += "Lignes refusΘes :\r\n%s" %"\r\n".join(refused_lines)
- BP.MsgBox(s, 0)
-
- BP.AccountRefreshScreen()
-