Закрытие счета 10/13

ed1=zzFirstDateOfMonth(acnt.s.date)
ed2=zzLastDateOfMonth(acnt.s.date)
keyPrim=u"#закрытие_месяца#расчет10/13"

mbp_in_use=const.mbp_in_use
mbp_cnt_cr=const.mbp_cnt_cr
date=ed2
cdate=dtoc(date)
rnd=const.rnd

zzCursor("""delete 
			FROM move 
			where date>='%(ed1)s' and date<='%(ed2)s' and cr_cnt='%(mbp_cnt_cr)s' and prim like '%(keyPrim)s'
			""" % locals())

m_mbp_in_use=num(zzCursor("""select 
						sum(
								case 
									when _db_cnt='%(mbp_in_use)s'=_cr_cnt='%(mbp_in_use)s' then 0 
									when _db_cnt='%(mbp_in_use)s' then summa 
									else -summa 
								end
							) as summa
				from move0 
				where date<='%(date)s' and '%(mbp_in_use)s' in (_db_cnt,_cr_cnt)
				""" % locals()).r.summa)

m_mbp_cnt_cr=num(zzCursor("""select 
						sum(
								-case 
									when _db_cnt='%(mbp_cnt_cr)s'=_cr_cnt='%(mbp_cnt_cr)s' then 0 
									when _db_cnt='%(mbp_cnt_cr)s' then summa 
									else -summa 
								end
							) as summa
				from move0 
				where date<='%(date)s' and '%(mbp_cnt_cr)s' in (_db_cnt,_cr_cnt) 
				""" % locals()).r.summa)


t_mbp_in_use=num(zzCursor("""select sum(round(qt*price,%(rnd)s)) as summa
									from infs
									where date<='%(date)s' and cnt='%(mbp_in_use)s'
									""" % locals()).r.summa)

if m_mbp_in_use!=t_mbp_in_use:
	zzMess(u"""Сальдо на конец периода (<b>%(cdate)s</b>)<br>
					по балансу (<b>%(m_mbp_in_use)s</b>)<br>
					и <br>
					по учету ТМЦ (<b>%(t_mbp_in_use)s</b>)<br>
					<font color=red size=+2>Не совпадают!!!</font> <br>
					Для расчета будут использованы данные баланса!
				""" % locals())
must_be=round(m_mbp_in_use/2,rnd)
delta=must_be-m_mbp_cnt_cr

if not insert("move",{"SUMMA":delta,"DATE":ed2,"DB_CNT":"26/","CR_CNT":mbp_cnt_cr,"PRIM":keyPrim}):
	wasError=u"Ошибка при записи результата расчета: %s" % lastError()