Example #1
0
 def import_ledger(self, csv_file):
     t_id = -1
     mt = None
     for l in csv_file:
         if len(l.rstrip()) > 0:
             r = l.split(';')
             print r
             if r[0] == 'T': # tehing
                 if len(r) == 8 and r[2].rstrip() != '':
                     t = None
                     try:
                         pr = Pearaamat.objects.get(aasta=int(r[1]))
                     except:
                         pr = Pearaamat()
                         pr.aasta = int(r[1])
                         pr.on_avatud = False
                         pr.save()
                     try:
                         tt = Tehingutyyp.objects.get(kirjeldus=r[2])
                     except:
                         tt = Tehingutyyp()
                         tt.kirjeldus = r[2]
                         tt.save()
                     try:
                         d = r[5].split('.')
                         vkpv = date(int(d[2]), int(d[1]), int(d[0]))
                         d = r[4].split('.')
                         tkpv = date(int(d[2]), int(d[1]), int(d[0]))
                         t = Tehing.objects.get(pearaamat=pr, tehingutyyp=tt, maksepaev=vkpv, sisu=r[3])
                         t.tehingupaev = tkpv
                         t.on_manual = r[6] == 'M'
                         Kanne.objects.all().filter(tehing=t).delete()
                     except:
                         t = Tehing(pearaamat=pr, tehingutyyp=tt, sisu=r[3], tehingupaev=tkpv, maksepaev=vkpv, on_manual=(r[6]=='M'))
                     finally:
                         t.save()
                         t_id = t.id
                         mt = t
             elif r[0] == 'K' and t_id > 0: # kanne    
                 k = Konto.objects.get(pk=r[1])
                 Kanne.objects.create(tehing=mt, konto=k, on_deebet=(r[2]=='D'), summa=float(r[3]), on_manual=(r[4]=='M'))
     return True