Конструктор отчетов

Ключ
Эта строка удалена.
Это слово было удалено. Это слово было добавлено.
Эта строка добавлена.

Изменения (4)

просмотр истории страницы
and av_inn_master.id is null
{code}

# h5. Отчет формирует информацию за период по следующим полям: входящий баланс абонента, списания за указанный месяц, оплачено, исходящий баланс, НДС 20% от списаний.
Если нет необходимости разделять на типы услуг, то можно убрать поля и скорректировать нахождение "Исходящего остатка"
1
{code}
h5. Упрощенный отчет "Входящий баланс" - "Оплачено" - "Списано" - "Исходящий баланс"
{code}
select distinct
a.contract_number as "№ договора",
(select first 1 login from users where users.abonent_id=a.id) as "Логин",
a.name as "Название/ФИО",
iif(a.company=1, 'Юр. лица','Физ. лица') as "Физ./Юр.",
/*Выполянется проверка, какие данные выводить по входящему балансу, в зависимости от даты создания абонента*/
case
when a.create_date_system between cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date) and
dateadd(1 month to cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date))
then
(select first 1 fost.aa_ostatok/10000000000.00
from finance_operations fost
where fost.storno = 0
and fost.abonent_id = a.id
and (extract(year from fost.period_end_date) = extract(year from cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date)) and
extract(month from fost.period_end_date) = extract(month from cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date)))
order by fost.system_date)

when coalesce(foincome2.summa, 0) != 0
then
foincome2.summa
else round((coalesce(foincome.balance_buh / 10000000000.00, 0.00)), 2)
end as "Входящий остаток",
coalesce(debet.summa, 0.00) as "Оплачено",
round(coalesce(onetime.summa, 0.00), 2) as "Начислено",
round((coalesce(foincome2.summa, 0.00)) + coalesce(debet.summa, 0.00) - coalesce(onetime.summa, 0.00), 2) as "Исходящий остаток"
from abonents a
join tarif t on a.tarif_id=t.id
left join finance_operations foincome on foincome.storno = 0 and a.id = foincome.abonent_id and foincome.op_type = 1 and
dateadd(-1 month to cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date)) = cast(foincome.period_end_date as date)
/*получаем бух.баланс из акта за предыдущий месяц от искомого*/

left join (select round(fo.balance_buh / 10000000000.00, 2) summa, fo.abonent_id abon
from finance_operations fo
where fo.storno = 0 and fo.op_type = 1
and (
extract(year from fo.period_end_date) = extract(year from dateadd(-1 month to cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date))) and
extract(month from fo.period_end_date) = extract(month from dateadd(-1 month to cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date)))
)) foincome2 on a.id = foincome2.abon

/*получаем сумму приходов за искомый месяц*/
left join (select round(sum(fo.op_summa / 10000000000.00), 2) summa,
fo.abonent_id abon
from finance_operations fo
where fo.storno = 0
and fo.op_type = 2
and fo.op_date between cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date) and dateadd(-1 second to dateadd(1 month to cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as timestamp)))
group by fo.abonent_id) debet on a.id = debet.abon
/*cумма списаний за искомый месяц*/
left join (select sum(c.summ) summa, c.abonent_id abon
from counters c
join usluga u on c.usluga_id = u.id
where c.month_number = extract(month from cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date))
and c.year_number = extract(year from cast(':Расчетный период|monthchoice|period_end_date|select[financeoperations]$' as date))
group by c.abonent_id
) onetime on a.id = onetime.abon


where
a.is_folder=0
and a.deleted = 0
and a.id > 0
and a.parent_id not in (2, 244)
{code}


h1. Решение проблем