def findT4VT(aasta): akpv = date(aasta, 1, 1) lkpv = date(aasta, 12, 31) vts = Varatehing.objects.all().filter(vaartuspaev__gte=akpv, vaartuspaev__lte=lkpv).order_by('vaartuspaev') vtx = [] for vt in vts: cnt = 0 if vt.tyyp != 'H': ks = Kanne.objects.all().filter(Q(konto__kontonumber='117')|Q(konto__kontonumber='118')| Q(konto__kontonumber='1191')|Q(konto__kontonumber='115')|Q(konto__kontonumber='1132'), tehing__maksepaev=vt.vaartuspaev, summa=vt.eur_summa) cnt = ks.count() else: vh = VaraHaldur() dr = abs(vh.getReservDiff(vt.vara.id, vt.vaartuspaev)) if dr != 0: ks = Kanne.objects.all().filter(Q(konto__kontonumber='117')|Q(konto__kontonumber='118')| Q(konto__kontonumber='1191')|Q(konto__kontonumber='264'), tehing__maksepaev=vt.vaartuspaev, summa=dr) cnt = ks.count() else: cnt = -1 if cnt > 0: vtx.append(vt) return vtx
def getReservD(vp_id, deebet, kpv): vh = VaraHaldur() l_val = float(vh.getReservDiff(vp_id, kpv)) if deebet == 'K' and l_val < 0 or deebet == 'D' and l_val > 0: l_val = 0 if deebet == 'D' and l_val < 0: l_val = -1 * l_val return l_val
def assettrans_action_handler(request): context = {} template = 'assettrans_edit.html' if request.method == 'POST' and request.POST.has_key('action'): if request.POST['action'] == 'Add': pk = request.POST['pk'] v = Vara.objects.get(pk=pk) form = VaratehingForm() form.vara=v context = {'form': form, 'pk': pk, 'ipk': 0} elif request.POST['action'] == 'Add_H': pk = request.POST['pk'] summa = request.POST['summa'] vh = VaraHaldur() pr = vh.getActivePR() vkpv = date(pr.aasta, 12, 31) kogus = vh.getEndCount(pk) vh.reprize(pk, vkpv, kogus, summa, 'EUR') elif request.POST['action'] == 'Update': pk = request.POST['pk'] ipk = request.POST['ipk'] doc=Varatehing.objects.get(pk=ipk) form = VaratehingForm(instance=doc) context = {'form': form, 'pk': pk, 'ipk': ipk} elif request.POST['action'] == 'Delete': ipk = request.POST['ipk'] Varatehing.objects.all().filter(pk=ipk).delete() elif request.POST['action'] == 'Save': pk = request.POST['pk'] ipk = request.POST['ipk'] if ipk != '0': doc=Varatehing.objects.get(pk=ipk) form = VaratehingForm(request.POST, instance=doc) else: form = VaratehingForm(request.POST) if form.is_valid(): form.save() else: context = {'form': form, 'pk': pk, 'ipk': ipk} elif request.POST['action'] == 'Cancel': pass if len(context) > 0: result = render_to_response(template, context, context_instance=RequestContext(request)) else: result = redirect(assets) return result
def assets(request): a = Vara.objects.all().order_by('vp_tyyp', 'nimetus') vh = VaraHaldur() assets = [] for i in a: if vh.getEndCount(i.id) > 0.0 or vh.getLastDealYear(i.id) == vh.getActivePR().aasta: assets.append(i) count = len(assets) stat = [] statb = vh.statusReport(date(vh.getActivePR().aasta-1, 12, 31)) stat.append(statb) for i in range(0, 11): stat.append(vh.statusReport(date(vh.getActivePR().aasta, 1, 31) + relativedelta(months=+i) )) state = vh.statusReport(date(vh.getActivePR().aasta, 12, 31)) stat.append(state) context = Context({'assets': assets, 'count': count, 'statb':statb, 'state':state, 'stat':stat }) return render_to_response('assets.html', context, context_instance=RequestContext(request))
def reports(request): if request.GET.has_key('rt'): if request.GET['rt'] == 'kvt': prs = Pearaamat.objects.all().order_by('aasta') pr_count = prs.count() if request.POST.has_key('year'): pr = Pearaamat.objects.get(aasta=request.POST['year']) else: pr = prs.reverse()[:1][0] vtx = findKVT(pr.aasta) vh = VaraHaldur() data = vh.reservMovementReport(pr) x = vh.assetReport(pr) asst = x[0] sums = x[1] context = Context({'prs':prs, 'pr_count':pr_count, 'year':pr.aasta, 'vtx':vtx, 'data':data, 'asst':asst, 'sums':sums}) return render_to_response('reports_kvt.html', context, context_instance=RequestContext(request)) if request.GET['rt'] == 'bil': prs = Pearaamat.objects.all().order_by('aasta') pr_count = prs.count() if request.POST.has_key('year'): pr = Pearaamat.objects.get(aasta=request.POST['year']) else: pr = prs.reverse()[:1][0] kvartal = 0 if request.POST.has_key('kvartal'): kvartal = int(request.POST['kvartal']) vtx = findBIL(pr, kvartal) context = Context({'prs':prs, 'pr_count':pr_count, 'year':pr.aasta, 'kvartal':kvartal, 'vtx':vtx[0], 'tot':vtx[1]}) return render_to_response('reports_bil.html', context, context_instance=RequestContext(request)) if request.GET['rt'] == 'kas': prs = Pearaamat.objects.all().order_by('aasta') pr_count = prs.count() if request.POST.has_key('year'): pr = Pearaamat.objects.get(aasta=request.POST['year']) else: pr = prs.reverse()[:1][0] kvartal = 0 if request.POST.has_key('kvartal'): kvartal = int(request.POST['kvartal']) vtx = kasumiAruanne(pr, kvartal) context = Context({'prs':prs, 'pr_count':pr_count, 'year':pr.aasta, 'kvartal':kvartal, 'vtx':vtx[0], 'fin':vtx[1], 'tot':vtx[2]}) return render_to_response('reports_kas.html', context, context_instance=RequestContext(request)) if request.GET['rt'] == 'rva': prs = Pearaamat.objects.all().order_by('aasta') pr_count = prs.count() if request.POST.has_key('year'): pr = Pearaamat.objects.get(aasta=request.POST['year']) else: pr = prs.reverse()[:1][0] kvartal = 0 if request.POST.has_key('kvartal'): kvartal = int(request.POST['kvartal']) rva = rahavoogudeAruanne(pr, kvartal) context = Context({'prs':prs, 'pr_count':pr_count, 'year':pr.aasta, 'kvartal':kvartal, 'rva':rva }) return render_to_response('reports_rva.html', context, context_instance=RequestContext(request)) elif request.POST.has_key('action'): if request.POST['action'] == u'Kustuta erinevustega tehingud': prs = Pearaamat.objects.all().order_by('aasta') pr_count = prs.count() if request.POST.has_key('year'): pr = Pearaamat.objects.get(aasta=request.POST['year']) else: pr = prs.reverse()[:1][0] vtx = findKVT(pr.aasta) lkpv = date(pr.aasta, 12, 31) vh = VaraHaldur() data = vh.reservMovementReport(pr) for r in data: print r if float(r['dk']) != float(r['dv']): print '-- erinevus!' T = Tehing.objects.get(pk=r['t']) if not T.on_manual: pts = Pangakirje.objects.all().filter(tehing=T) for pt in pts: pt.arvestatud = 'N' pt.tehing = None pt.save() Kanne.objects.all().filter(tehing=T).delete() print '-- kustutan tehingu ', T.sisu vtt = Varatehing.objects.all().filter(tehing_id=T.id) for u in vtt: u.tehing_id = None u.save() T.delete() if request.POST['action'] == u'Genereeri ümberhindlused': prs = Pearaamat.objects.all().order_by('aasta') pr_count = prs.count() if request.POST.has_key('year'): pr = Pearaamat.objects.get(aasta=request.POST['year']) else: pr = prs.reverse()[:1][0] vtx = findKVT(pr.aasta) print '..1 vtx.length = ', len(vtx) lkpv = date(pr.aasta, 12, 31) vh = VaraHaldur() for vt in vtx: if vt.tyyp == 'H': dr = vh.getReservDiff(vt.vara.id, lkpv) if dr != 0: tt = Tehingutyyp.objects.get(kirjeldus=u'Käsitsi sisestatud tehing') if vt.vara.vp_tyyp == 'A': l_sisu = u'Aktsia ümberhindamine ' l_konto = '117' elif vt.vara.vp_tyyp == 'V': l_sisu = u'Võlakirja ümberhindamine ' l_konto = '118' elif vt.vara.vp_tyyp == 'I': l_sisu = u'Alt.investeeringu ümberhindamine ' l_konto = '1191' l_sisu = l_sisu + '(' + vt.vara.nimetus + ')' t = Tehing.objects.create(pearaamat=pr, tehingutyyp=tt, sisu=l_sisu, tehingupaev=lkpv, maksepaev=lkpv, on_manual=True) vpk = Konto.objects.get(pk=l_konto) rsk = Konto.objects.get(pk='264') vt.tehing_id = t.id vt.save() else: print '... reserv_diff = 0: ', vt.vara.nimetus if dr > 0: Kanne.objects.create(tehing=t, konto=vpk, on_deebet=True, summa=dr, on_manual=True) Kanne.objects.create(tehing=t, konto=rsk, on_deebet=False, summa=dr, on_manual=True) elif dr < 0: Kanne.objects.create(tehing=t, konto=vpk, on_deebet=False, summa=abs(dr), on_manual=True) Kanne.objects.create(tehing=t, konto=rsk, on_deebet=True, summa=abs(dr), on_manual=True) vtx = findT4VT(pr.aasta) print '..2 vtx.length = ', len(vtx) for vt in vtx: if vt.tyyp == 'H': tt = Tehingutyyp.objects.get(kirjeldus=u'Käsitsi sisestatud tehing') if vt.vara.vp_tyyp == 'A': l_sisu = u'Aktsia ümberhindamine ' l_konto = '117' elif vt.vara.vp_tyyp == 'V': l_sisu = u'Võlakirja ümberhindamine ' l_konto = '118' elif vt.vara.vp_tyyp == 'I': l_sisu = u'Alt.investeeringu ümberhindamine ' l_konto = '1191' l_sisu = l_sisu + '(' + vt.vara.nimetus + ')' t = Tehing.objects.all().filter(pearaamat=pr, tehingutyyp=tt, sisu=l_sisu, tehingupaev=lkpv, maksepaev=lkpv, on_manual=True) if len(t) > 0: vt.tehing_id = t[0].id vt.save() else: '... no ', l_sisu context = Context({}) return render_to_response('pd_rp_home.html', context, context_instance=RequestContext(request))
def asset_action_handler(request): prs = Pearaamat.objects.all().order_by('aasta') if request.POST.has_key('year'): pr = Pearaamat.objects.get(aasta=request.POST['year']) else: pr = prs.reverse()[:1][0] template = 'asset_edit.html' go_home = False count = 0 context = {'icount': count} if request.method == 'POST' and request.POST.has_key('action'): if request.POST['action'] == 'Loe varad': if request.FILES.has_key('cvs'): request.encoding = 'utf-8' wf = request.FILES['cvs'] f = codecs.EncodedFile(File(wf), 'utf8') kl = AssetLoader() kl.setYear(pr.aasta) kl.import_assets(f) go_home = True elif request.POST['action'] == 'Kirjuta varad faili': f = codecs.open('assets.txt', mode="w", encoding="utf8") kl = AssetLoader() if kl.export_assets(date(pr.aasta, 12, 31), f): f.close() f = codecs.open('assetdeals.csv', mode="w", encoding="utf8") kl = AssetLoader() if kl.export_assetdeals(f): f.close() go_home = True elif request.POST['action'] == u'Kustuta tehingud': Varatehing.objects.all().delete() go_home = True elif request.POST['action'] == u'Kustuta varad': Vara.objects.all().delete() go_home = True elif request.POST['action'] == u'Ümberarvutus': vh = VaraHaldur() vh.analyse() vs = Vara.objects.all() for v in vs: vh.recalc(v.id) go_home = True else: form = VaraForm() pk = request.POST['pk'] if pk != '': doc = Vara.objects.get(pk=pk) items = Varatehing.objects.all().filter(vara=doc).order_by('vaartuspaev','-tyyp','-kogus') count = items.count() else: count = 0 context = {'pk': pk, 'items': items, 'icount': count} if request.POST['action'] == 'Add': pk = '' context = {'form': form, 'pk': pk, 'icount': 0} elif request.POST['action'] == 'Update': form = VaraForm(instance=doc) context = {'form': form, 'pk': pk, 'items': items, 'icount': count} elif request.POST['action'] == 'Delete': items.delete() doc.delete() go_home = True elif request.POST['action'] == 'Save': if pk != '': doc = Vara.objects.get(pk=pk) form = VaraForm(request.POST, instance=doc) else: form = VaraForm(request.POST) if form.is_valid(): form.save() go_home = True else: context = {'form': form, 'pk': pk, 'items': items, 'icount': count} elif request.POST['action'] == 'Cancel': go_home = True else: go_home = True if go_home: result = redirect(assets) else: result = render_to_response(template, context, context_instance=RequestContext(request)) return result
def getReserv(vp_id, kpv): vh = VaraHaldur() l_val = vh.getReservDiff(vp_id, kpv) return float(l_val)
def amt(vp_id, kpv): # leiab vara maksumuse muutuse vh = VaraHaldur() vp_sum = vh.getTHDiff(vp_id, kpv) return float(vp_sum)
def vp(ss): vh = VaraHaldur() vp_id = vh.identify(ss) return vp_id
def import_assets(self, csv_file): vh = VaraHaldur() vrec = {} # {nimetus, aldsaldo, id} trec = [] # (*tehing*) v_id = -1 v = None delta = 0 if self.jooksev_aasta >= 2012: delta = 2 for l in csv_file: if len(l.rstrip()) > 0: r = l.split(';') for i in range(len(r)): r[i] = r[i].strip('\"') print r[12+delta], r[12+delta].decode('utf8') if len(r) < 17: # vanemat tüüpi formaadiga fail delta = 0 if r[0] == 'Tehingu ref.': # faili päis pass elif r[0].isdigit() and v_id >= 0: # tehingu kirje print '>>> tehing' if float(r[13+delta]) != 0: if re.search('\+',r[1]): tyyp = 'O' elif re.search('-',r[1]): tyyp = 'M' elif re.search('%',r[1]): tyyp = 'H' elif float(r[13+delta]) > 0: tyyp = 'O' elif float(r[13+delta]) < 0: tyyp = 'M' s = r[2].split('.') vkpv = date(int(s[2]), int(s[1]), int(s[0])) summa = float(r[9+delta]) valuuta = r[10+delta] kogus = float(r[13+delta]) trec.append({u'tüüp':tyyp, 'vkpv':vkpv, 'summa':summa, 'valuuta':valuuta, 'kogus':kogus}) print ' * ', trec elif r[0] == '' and r[12+delta] == 'Lõppsaldo' and v_id >= 0: # lõppsaldo print '>>> lõpp ', vrec['algsaldo'], ' -> ', r[13+delta] add_vt = False if v_id == 0 and add_vt and float(r[13+delta]) > 0 and float(vrec['algsaldo']) > 0: print 'WARNING: Missing previous data for ', vrec['nimetus'], vrec['algsaldo'], ' -> ', r[13+delta] if v_id > 0: add_vt = True else: for t in trec: add_vt = add_vt or t['vkpv'].year >= self.jooksev_aasta if v_id == 0 and float(vrec['algsaldo']) == 0: lyh = vrec['lyhend'] if vrec['lyhend'] != '' else '?' typ = vrec[u'tüüp'] if vrec[u'tüüp'] != '' else 'A' v = Vara.objects.create(nimetus=vrec['nimetus'], vp_tyyp=typ, lyhend=lyh) v_id = v.id add_vt = True print ' ** ', len(trec) if add_vt: for t in trec: if t[u'tüüp'] == 'O': vh.buy(v.id, t['vkpv'], t['vkpv'], t['kogus'], t['summa'], t['valuuta']) elif t[u'tüüp'] == 'M': vh.sell(v.id, t['vkpv'], t['vkpv'], t['kogus'], t['summa'], t['valuuta']) elif t[u'tüüp'] == 'H': vh.reprize(v.id, t['vkpv'], t['kogus'], t['summa'], t['valuuta']) else: print ' # Not added: ', vrec['nimetus'] elif r[12+delta] == 'Algsaldo': # vara nimetus ja algsaldo print '>>> algus' try: trec = [] v = Vara.objects.get(nimetus=r[0]) v_id = v.id if r[8] == '': r[8] = v.vp_tyyp if r[10] == '': r[10] = v.lyhend print ' # Found: ', r[0] except: print ' # Not found, will create: ', r[0] v = Vara.objects.create(nimetus=r[0], vp_tyyp=r[8], lyhend=r[10]) v_id = v.id vrec = {'nimetus':r[0], 'algsaldo':r[13+delta], 'id':v_id, u'tüüp':r[8], 'lyhend':r[10]} print vrec return True