create proc dbo.%PROC% @owner_id numeric, -- фонд @agent_dog_id numeric, -- агентский договор @pay_owner_id numeric, -- пайщик @out_doc_id numeric, -- документ-основание снятия (заявка,переход прав,обмен) @operday datetime, -- дата проведения операции @paydate datetime, -- дата цены пая @qty_all .PIF_PAY_TYPE, -- общее кол-во для погашения @is_manual_tax integer, -- = 1 - сумма налога вводится вручную @tax_percent money, -- % налога установленный вручную, @tax_sum money, -- сумма налога, установленная вручную @dop_sum_ed money, -- расходы (доп.или подтвержденные), введенные вручную @dop_sum_type numeric, -- тип расчета расходов (спр.5051) @is_operation integer, -- = 0 - без проводок @oper_doc_id numeric, -- id поручения, @oper_action_id numeric, -- id метода поручения @oper_action_history_id numeric, @p_sum_all money out, -- вся сумма оплаты (вычетом комиссии и налога) @comis_all money out, -- вся скидка @tax_all money out, -- вся сумма налога @out_sum money out, -- списанная балансовая сумма @dop_sum_all money out, -- все доп.расходы @partdate_last datetime out, -- дата посл.партии @qty_all_res .PIF_PAY_TYPE out -- количество as declare @calc_type int -- 1 - по изменению %, иначе - по каждой партии select @calc_type=1 declare @plan_id numeric, @acc_id numeric, @acc7641_id numeric, @acc7642_id numeric, @subc_type_id numeric, @part_subc_type_id numeric, @pp_id numeric, @get_date datetime, @pays_acc_mode int declare @part_id numeric, @common_part_id numeric, @new_part_id numeric, @new_pay_owner_id numeric, @partdate datetime, @part_sum money, @part_sum_op money, @part_qty_bal money, @part_qty_op money, @part_qty_op_cred money, @part_qty_calc .PIF_PAY_TYPE, @all_sum_part money, -- вся сумма по партии, зачисленная на 86.1 @dop_sum_part money, -- доп.расходы по партии @dop_sum_more money, -- доп.расходы, введенные вне учета @dop_sum_op money, @qty_all_bal money, @qty_bal money, @qty_ost money, @part_price decimal(36,16), @oper_doc_type varchar(255), @out_doc_name varchar(255), @oper_id numeric, @cred_id numeric declare @calc_qty .PIF_PAY_TYPE, @calc_sum money, @calc_sum_all money, @stock_price money, @p_sum money, @comis money, @comis_proc money, @comis_proc_last money, @for_tax_sum money, @tax_city_id numeric declare @op_pay_owner_id numeric, @op_new_pay_owner_id numeric select @plan_id=.ARG_PLAN('PIF_ACCOUNT') select @acc_id=.SD_ACCID_FROM_EFFNO(@plan_id,'96.1') select @acc7641_id=.SD_ACCID_FROM_EFFNO(@plan_id,'76.4.1') select @acc7642_id=.SD_ACCID_FROM_EFFNO(@plan_id,'76.4.2') select @subc_type_id=.SUBCTYPE_FROM_CODE('partners') select @part_subc_type_id=.SUBCTYPE_FROM_CODE('doc_item') select @get_date=dateadd(dd,1,(.DATE_NORM(@operday,B))) select @pp_id= .PLAN2PARTNER_ID(@owner_id,@plan_id) select @pays_acc_mode=isnull(pays_acc_mode,1) from td_pay_fond where id=@owner_id select @qty_all_bal=isnull((.PAYS_QTY_BAL(@qty_all,@owner_id)),0) select @new_pay_owner_id= @pay_owner_id select @oper_doc_type=upper(.ITEM_TYPE_CODE(@oper_doc_id)) select @out_doc_name=.ITEM_NAME(@out_doc_id) if @oper_doc_type='PIF_PAY_REPORT' select @cred_id=@acc7642_id if @oper_doc_type='TP_PIF_PAYS_MOVES' begin select @cred_id=@acc_id select @new_pay_owner_id=d_pay_owner_id from tp_pif_queries where id=@oper_doc_id end -- новая партия select @common_part_id=.SD_COMMON_PART4PAYS if @common_part_id is null exec ap_insert_common_part4pays select @common_part_id=.SD_COMMON_PART4PAYS if @cred_id=@acc_id if @pays_acc_mode=1 select @new_part_id=@common_part_id else select @new_part_id=@out_doc_id --.CHECK_DEFAULT_PIF_PARTNER(@owner_id,@pay_owner_id,@op_pay_owner_id) --.CHECK_DEFAULT_PIF_PARTNER(@owner_id,@new_pay_owner_id,@op_new_pay_owner_id) select @op_pay_owner_id=@pay_owner_id select @op_new_pay_owner_id=@new_pay_owner_id -- проверка на наличие select @qty_bal=isnull((select sum(isnull(ol.qty_sum*ol.deb_or_cred*(-1),0)) from t_oper_subconto os, t_oper_list ol, t_operations o where o.partner2plan_id=@pp_id and ol.head_id=o.id and ol.acc_id=@acc_id and os.id=ol.id and os.subconto_type_id=@subc_type_id and o.op_date<@get_date and os.item_id=@op_pay_owner_id),0) if @is_operation=1 and (@qty_bal < @qty_all_bal or @qty_bal=0) .EXIT_MESSAGE_PARM('Не хватает паев на балансе',@out_doc_name) if @qty_bal < @qty_all_bal select @qty_all_bal=@qty_bal -- партии select @p_sum_all=0, @comis_all=0, @out_sum=0, @dop_sum_all=0, @comis_proc_last=null, @qty_ost=@qty_all_bal if @pays_acc_mode in (1,2) declare parts cursor for select osp.item_id, max(d.in_date), sum(isnull(ol.sum_bal*ol.deb_or_cred*(-1),0)), sum(isnull(ol.qty_sum*ol.deb_or_cred*(-1),0)) from t_oper_subconto os, t_oper_subconto osp, t_oper_list ol, t_operations o, td_depo_docs d where o.partner2plan_id=@pp_id and ol.head_id=o.id and ol.acc_id=@acc_id and os.id=ol.id and os.subconto_type_id=@subc_type_id and osp.id=ol.id and osp.subconto_type_id=@part_subc_type_id and o.op_date<@get_date and os.item_id=@op_pay_owner_id and osp.item_id=d.id group by osp.item_id, d.in_date, d.in_no having sum(isnull(ol.qty_sum*ol.deb_or_cred*(-1),0))>0 order by d.in_date, d.in_no for read only if @pays_acc_mode=3 declare parts cursor for select osp.item_id, max(d.in_date), sum(isnull(ol.sum_bal*ol.deb_or_cred*(-1),0)), sum(isnull(ol.qty_sum*ol.deb_or_cred*(-1),0)) from t_oper_subconto os, t_oper_subconto osp, t_oper_list ol, t_operations o, td_depo_docs d where o.partner2plan_id=@pp_id and ol.head_id=o.id and ol.acc_id=@acc_id and os.id=ol.id and os.subconto_type_id=@subc_type_id and osp.id=ol.id and osp.subconto_type_id=@part_subc_type_id and o.op_date<@get_date and os.item_id=@op_pay_owner_id and osp.item_id=d.id group by osp.item_id, d.in_date, d.in_no having sum(isnull(ol.qty_sum*ol.deb_or_cred*(-1),0))>0 order by d.in_date desc, d.in_no desc for read only open parts fetch parts into @part_id, @partdate, @part_sum, @part_qty_bal while .CURSOR_STATE=0 and @qty_ost>0 begin if @part_qty_bal <= @qty_ost -- вся партия begin select @part_qty_op = @part_qty_bal select @part_sum_op = @part_sum end else -- часть партии begin select @part_qty_op = @qty_ost select @part_price = convert(decimal(36,16),convert(float,1.0)*convert(decimal(36,16),@part_sum) / convert(decimal(36,16),@part_qty_bal)) select @part_sum_op = convert(money,round((convert(decimal(36,16),@part_qty_op) * @part_price),2)) end select @qty_ost = @qty_ost - @part_qty_op select @part_qty_calc = .PAYS_QTY_CALC(@part_qty_op,@owner_id) if @oper_doc_type='TP_PIF_PAYS_MOVES' select @part_qty_op_cred=@part_qty_op else select @part_qty_op_cred=0 -- доп.расходы по партии if isnull(@oper_doc_type,'') not in ('TP_PIF_PAYS_MOVES') and @dop_sum_type in (1,2,3) and @pay_owner_id=@op_pay_owner_id begin select @all_sum_part=null, @dop_sum_part=null, @dop_sum_more=null if @part_id=@common_part_id -- по общей партии begin -- вся сумма, зачисленная на 86.1 по этому пайщику на общую партию select @all_sum_part=(select sum(isnull(ol.sum_bal,0)) from t_oper_subconto os, t_oper_subconto osp, t_oper_list ol, t_operations o where o.partner2plan_id=@pp_id and ol.head_id=o.id and ol.acc_id=@acc_id and ol.deb_or_cred=-1 and os.id=ol.id and os.subconto_type_id=@subc_type_id and osp.id=ol.id and osp.subconto_type_id=@part_subc_type_id and o.op_date<@get_date and os.item_id=@pay_owner_id and osp.item_id=@part_id) -- надбавка = вся сумма, оплаченная по заявкам общей партии этого пайщика и переведенная на нее с др.пайщиков - @all_sum_part if @dop_sum_type in (1,2) select @dop_sum_part=(select sum(isnull(ol.sum_bal,0)) from t_oper_subconto os, t_oper_list ol, t_operations o where o.partner2plan_id=@pp_id and ol.head_id=o.id and (ol.acc_id=@acc7641_id -- сумма оплаты по заявкам or (ol.acc_id=@acc_id and ol.deb_or_cred=-1 and upper(.ITEM_TYPE_CODE(o.item_id)) in ('TP_PIF_PAYS_MOVES','PIF_START_PART')) -- по передаче паев, начальному зачислению ) and os.id=ol.id and os.subconto_type_id=@subc_type_id and os.item_id=@pay_owner_id and o.item_id in (select o1.item_id from t_oper_subconto os1, t_oper_subconto osp1, t_oper_list ol1, t_operations o1 where o1.partner2plan_id=@pp_id and ol1.head_id=o1.id and ol1.acc_id=@acc_id and os1.id=ol1.id and os1.subconto_type_id=@subc_type_id and osp1.id=ol1.id and osp1.subconto_type_id=@part_subc_type_id and o1.op_date<@get_date and os1.item_id=@pay_owner_id and osp1.item_id=@part_id) ) - @all_sum_part end else begin -- сумма по 86.1 в этом документе-партии select @all_sum_part=(select sum(isnull(ol.sum_bal,0)) from t_oper_subconto os, t_oper_list ol, t_operations o where o.partner2plan_id=@pp_id and ol.head_id=o.id and ol.acc_id=@acc_id and o.item_id=@part_id and os.id=ol.id and os.subconto_type_id=@subc_type_id and os.item_id=@pay_owner_id) -- надбавка = сумма по 76.4.1 в этом документе-партии (т.е. вся оплата по этой партии) - @all_sum_part if @dop_sum_type in (1,2) select @dop_sum_part=(select sum(isnull(ol.sum_bal*ol.deb_or_cred,0)) from t_oper_subconto os, t_oper_list ol, t_operations o where o.partner2plan_id=@pp_id and ol.head_id=o.id and ol.acc_id=@acc7641_id and o.item_id=@part_id and os.id=ol.id and os.subconto_type_id=@subc_type_id and os.item_id=@pay_owner_id) - @all_sum_part end -- отдельные доп.расходы if @dop_sum_type in (1,3) begin select @dop_sum_more=(select sum(e.exp_sum) from td_pif_pay_expenses e where e.fund_id=@owner_id and e.doc_id=@part_id and e.partner_id=@pay_owner_id and e.exp_type=2 and .ITEMS_EXISTS(e.id)) if isnull(@dop_sum_more,0)>0 -- указаны реальные расходы по партии select @dop_sum_part=@dop_sum_more else -- указаны доп.расходы по партии select @dop_sum_part=isnull(@dop_sum_part,0) + isnull((select sum(e.exp_sum) from td_pif_pay_expenses e where e.fund_id=@owner_id and e.doc_id=@part_id and e.partner_id=@pay_owner_id and e.exp_type=1 and .ITEMS_EXISTS(e.id)),0) end -- отдельные доп.расходы if @part_sum_op < @all_sum_part -- списывается часть партии begin select @dop_sum_op=convert(money,round(convert(decimal(36,16),@dop_sum_part)*(convert(decimal(36,16),(convert(float,1.0)*convert(decimal(36,16),@part_sum_op)/convert(decimal(36,16),@all_sum_part)))),2)) if isnull(@dop_sum_more,0)>0 select @dop_sum_op=@dop_sum_op - @part_sum_op end else begin if isnull(@dop_sum_more,0)>0 select @dop_sum_op=@dop_sum_part - @part_sum_op else select @dop_sum_op=@dop_sum_part end end -- проводка по списанию if @is_operation=1 begin .OPERATION(@owner_id,@plan_id,@operday,@acc_id,@cred_id,@part_sum_op,'RUR',@operday,@part_qty_op,@part_qty_op_cred,@out_doc_name,N,@oper_id,@oper_doc_id,@oper_action_id,@oper_action_history_id) .UPDATE_SUBC(D,'partners',@op_pay_owner_id,@oper_id) .UPDATE_SUBC(D,'doc_item',@part_id,@oper_id) if @cred_id=@acc7642_id begin .UPDATE_SUBC(K,'partners',@new_pay_owner_id,@oper_id) .UPDATE_SUBC(K,'pifdocs',@out_doc_id,@oper_id) end if @cred_id=@acc_id begin .UPDATE_SUBC(K,'partners',@op_new_pay_owner_id,@oper_id) .UPDATE_SUBC(K,'doc_item',@new_part_id,@oper_id) end .SUBC_CHECK(@oper_id) end -- списанная балансовая сумма select @out_sum=@out_sum + @part_sum_op -- списанные расходы select @dop_sum_all=@dop_sum_all + isnull(@dop_sum_op,0) -- расчет select @comis_proc=.PIF_AGENT_PROC4PARTION(@agent_dog_id,@operday,@partdate) if isnull(@calc_type,0)=1 and @comis_proc_last is not null and isnull(@comis_proc,0)=@comis_proc_last -- та же комиссия select @calc_qty=@calc_qty+@part_qty_calc, @calc_sum=isnull(@calc_sum,0)+@part_sum_op+isnull(@dop_sum_op,0) else select @calc_qty=@part_qty_calc, @calc_sum=@part_sum_op+isnull(@dop_sum_op,0), @p_sum_all=@p_sum_all+isnull(@p_sum,0), @comis_all=@comis_all+isnull(@comis,0) select @calc_sum_all=isnull(@calc_sum_all,0)+@part_sum_op+isnull(@dop_sum_op,0) select @p_sum=0, @comis=0, @comis_proc_last=isnull(@comis_proc,0) exec ap_pifpay_out_new @calc_qty, @calc_sum, @pay_owner_id, @agent_dog_id, @owner_id, @operday, @paydate, @partdate, @comis_proc, @stock_price out, @p_sum out, @comis out, @comis_proc out if @@error != 0 begin if @@trancount<>0 rollback tran return end fetch parts into @part_id, @partdate, @part_sum, @part_qty_bal end close parts .DEALLOCATE parts select @p_sum_all=@p_sum_all+isnull(@p_sum,0), @comis_all=@comis_all+isnull(@comis,0), @partdate_last=@partdate, @qty_all_res=.PAYS_QTY_CALC(@qty_all_bal,@owner_id) --kashuba_av begin Сделано для ММБ. --Считает комиссию по формуле: цена без скидки*общее количество-цено с оскидкой*общее количество if rtrim(ltrim(.GET_CONST('CLIENT_CODE'))) = 'MMB' begin declare @stock_price_calc decimal(36,16) select @stock_price_calc = round(@stock_price*(1-isnull(@comis_proc,0)/100.0),2) select @comis_all=round((@stock_price*@qty_all_res-@stock_price_calc*@qty_all_res),2) end --kashuba_av end -- доп.расходы, введенные вручную if @dop_sum_type in (1,2,3,4) begin select @dop_sum_all=isnull(@dop_sum_all,0) + isnull(@dop_sum_ed,0) select @calc_sum_all=isnull(@calc_sum_all,0) + isnull(@dop_sum_ed,0) end -- подтвержденные расходы, введенные вручную if @dop_sum_type=5 begin select @dop_sum_all=isnull(@dop_sum_ed,0)-@out_sum select @calc_sum_all=isnull(@dop_sum_ed,0) end -- расчет налога if @is_manual_tax=0 begin select @for_tax_sum = @p_sum_all - isnull(@calc_sum_all,0) if @for_tax_sum < 0 select @for_tax_sum=0 if @tax_percent is not null select @tax_all = @for_tax_sum * @tax_percent / 100.0 else begin .CALC_TAX('INCOME_TAX',@pay_owner_id,@for_tax_sum,@operday,@tax_all,@tax_city_id,@tax_percent) end end else select @tax_all = isnull(@tax_sum,0) select @tax_all = round(@tax_all,0) select @p_sum_all=@p_sum_all-isnull(@tax_all,0)