dbo.f_mutasi_bp

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Null on Null Input

False

Schema Bound

False

Assembly

Parameters

Name Data Type Length Description
@tgstart date 3
@tgend date 3
@tipebahan varchar 50

SQL Script

SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO

CREATE FUNCTION dbo.f_mutasi_bp
( 
 @tgstart date, @tgend date, @tipebahan varchar(50) 
)
RETURNS  @mutasibj table (
    part_no varchar(100) null
    ,part_name varchar(100) null
    ,stock_awal float null
    ,qty_in float null
    ,qty_out float null
    ,sisa float null
    ,unit varchar(100) null
    ,qty_in_by_date float null
    ,qty_out_by_date float null
    ,sisa_by_date float null
    ,stock_opname  float null
    ,adj  float null
    
    )
AS
begin



declare @datesaldo date
declare @stsstockopname int, @dtsto date

set @datesaldo = '2013-01-01'
set @stsstockopname = 0

if (
    select count(*) from (
  select count(*) as cnt from [dbo].[tbl_stock_opname] a

      where a.[date_stock_opname] = (select max([date_stock_opname]) from [dbo].[tbl_stock_opname] b
      where b.[date_stock_opname] <= @tgstart and b.typebahan  = 'BP' 
      )   and a.typebahan  = 'BP'
      group by part_name_customs
  ) as x

) > 0 
begin
 set @stsstockopname = 1 
 set   @dtsto =  (select max(dt) from (
  select [date_stock_opname] as dt from [dbo].[tbl_stock_opname] a

      where a.[date_stock_opname] = (select max([date_stock_opname]) from [dbo].[tbl_stock_opname] b
      where b.[date_stock_opname] <= @tgstart and b.typebahan  = 'BP'
      )  and a.typebahan  = 'BP'
      group by part_name_customs, [date_stock_opname]
  ) as x)
 set @datesaldo=dateadd(day, 1, @dtsto)  
end

declare @tgstartx date
if (select count(*) from  [tbl_stock_opname] where typebahan  = 'BP' and [date_stock_opname] between @tgstart and @tgend) > 0
begin
 set @tgstartx = (
  select max(date_stock_opname) from  [tbl_stock_opname] where typebahan  = 'BP' and [date_stock_opname] between @tgstart and @tgend
 )
 if (@tgend <> @tgstartx)
 begin
  set @tgstart = dateadd(day, 1, @tgstartx)  
    end
end

   ;with
    ct_assy_list(
     part_no,part_name, unit
     ) as
     (
     SELECT 
       part_no2
        ,part_name
       ,UOM
           
      FROM [dbo].[tbl_data_bahan_penolong] where aktif='aktif'
      group by 
       part_no2
        ,part_name,UOM
     ),
      ctestokawal(part_no, qty) as (
    SELECT 
        [part_no2]
        ,isnull(sum([saldo_awal]),0)
     
     FROM [dbo].[tbl_data_bahan_penolong] where aktif='aktif'
     group by 
     [part_no2]
     ),
     ctestok_trxawal(
     part_no, qty_in
     ) as
     (
     --select part_no, sum(abs(qty_in)), unit_in
     select part_no2, sum(qty_in)
     from v_data_bp_in
     where status='APPROVED' and v_data_bp_in.doc_in_date between @tgstart and @tgend
     group by  part_no2
     ),
     ctest_trxoutawal(part_no, qty_out) as (
     --SELECT
     --dbo.tbl_bahanpenolong_out.part_no,
     --ISNULL(sum(dbo.tbl_bahanpenolong_out.qty),0)
     --FROM
     --dbo.tbl_bahanpenolong_out
     --where status_data='APPROVED' and doc_date between @tgstart and @tgend
     --GROUP BY
     --dbo.tbl_bahanpenolong_out.part_no

     --union all
     --SELECT tbl_data_bahan_penolong.part_no, ISNULL(sum(v_oracle_bp.Qty),0)
     --FROM tbl_data_bahan_penolong LEFT OUTER JOIN
     -- v_oracle_bp ON tbl_data_bahan_penolong.part_name_customs = v_oracle_bp.Description
     --where RegisterDate between @tgstart and @tgend
     --group by tbl_data_bahan_penolong.part_no

     select  part_no, sum(qty) from (
      SELECT
          dbo.tbl_bahanpenolong_out.part_no,
          ISNULL(sum(dbo.tbl_bahanpenolong_out.qty),0) as qty
          FROM
          dbo.tbl_bahanpenolong_out
          where status_data='APPROVED' and doc_date  between @tgstart and @tgend
          GROUP BY
          dbo.tbl_bahanpenolong_out.part_no

          union all
          SELECT        tbl_data_bahan_penolong.part_no, ISNULL(sum(v_oracle_bp.Qty),0)
          FROM            tbl_data_bahan_penolong LEFT OUTER JOIN
                 v_oracle_bp ON tbl_data_bahan_penolong.part_name_customs = v_oracle_bp.Description
          where RegisterDate between @tgstart and @tgend
          group by tbl_data_bahan_penolong.part_no

     ) x
     group by  x.part_no

     
     ),
    
      ctestok_in_date(
     part_no,  qty_in
     
     ) as
     (
     select part_no2, sum(qty_in)
     from v_data_bp_in
     where status='APPROVED' and v_data_bp_in.doc_in_date between @datesaldo and dateadd(day, -1, @tgstart)
     group by  part_no2

     ),
     ctest_trxout_bydate(part_no, qty_out) as (
     --SELECT
     --dbo.tbl_bahanpenolong_out.part_no,
     --ISNULL(sum(dbo.tbl_bahanpenolong_out.qty),0)
     --FROM
     --dbo.tbl_bahanpenolong_out
     --where status_data='APPROVED' and doc_date between @datesaldo and dateadd(day, -1, @tgstart)
     --GROUP BY
     --dbo.tbl_bahanpenolong_out.part_no
     --union all
     --SELECT tbl_data_bahan_penolong.part_no, ISNULL(sum(v_oracle_bp.Qty),0)
     --FROM tbl_data_bahan_penolong LEFT OUTER JOIN
     -- v_oracle_bp ON tbl_data_bahan_penolong.part_name_customs = v_oracle_bp.Description
     --where RegisterDate between @datesaldo and dateadd(day, -1, @tgstart)
     --group by tbl_data_bahan_penolong.part_no

     select  part_no, sum(qty) from (
      SELECT
          dbo.tbl_bahanpenolong_out.part_no,
          ISNULL(sum(dbo.tbl_bahanpenolong_out.qty),0) as qty
          FROM
          dbo.tbl_bahanpenolong_out
          where status_data='APPROVED' and doc_date  between  @datesaldo and dateadd(day, -1, @tgstart)
          GROUP BY
          dbo.tbl_bahanpenolong_out.part_no

          union all
          SELECT        tbl_data_bahan_penolong.part_no, ISNULL(sum(v_oracle_bp.Qty),0)
          FROM            tbl_data_bahan_penolong LEFT OUTER JOIN
                 v_oracle_bp ON tbl_data_bahan_penolong.part_name_customs = v_oracle_bp.Description
          where RegisterDate between  @datesaldo and dateadd(day, -1, @tgstart)
          group by tbl_data_bahan_penolong.part_no

     ) x
     group by  x.part_no

     
     
     ),
    cte_stock_opname_awal(
    part_no, qty    
    ) as (
   
    select part_no, sum(qty) from [dbo].[tbl_stock_opname] a

     where a.[date_stock_opname] = (select max([date_stock_opname]) from [dbo].[tbl_stock_opname] b
     where b.[date_stock_opname] < @tgstart and b.typebahan  = 'BP'
     )  and a.typebahan = 'BP'
      group by part_no
   
    ),
     cte_stock_opname_akhir(
    part_no, qty    
    ) as (
   
     select part_no, sum(qty) from [dbo].[tbl_stock_opname] a

     where a.[date_stock_opname] = (select max([date_stock_opname]) from [dbo].[tbl_stock_opname] b
     where b.[date_stock_opname] <= @tgend and b.typebahan  = 'BP'
     )  and a.typebahan = 'BP'
     group by part_no
   
    ),
        cte_stock_opname_count(
    part_no, cnt, [date_stock_opname]   
    ) as (
   
    select part_no, count(*) AS cnt, [date_stock_opname]  from [dbo].[tbl_stock_opname] a

     where a.[date_stock_opname] = (select max([date_stock_opname]) from [dbo].[tbl_stock_opname] b
     where b.[date_stock_opname] < @tgstart and b.typebahan  = 'BP'
     )  and a.typebahan = 'BP'
      group by part_no, [date_stock_opname]
   
    ),
     cte_stock_opname_tgl_akhir(
    part_no, qty    
    ) as (
   
     select part_no, sum(qty) from [dbo].[tbl_stock_opname] a

     where a.[date_stock_opname] = (select max([date_stock_opname]) from [dbo].[tbl_stock_opname] b
     where b.[date_stock_opname] = @tgend and b.typebahan  = 'BP'
     )  and a.typebahan = 'BP'
     group by part_no
   
    )
   

    
      
       insert into @mutasibj (part_no,part_name,stock_awal,qty_in,qty_out,sisa,unit,qty_in_by_date,qty_out_by_date,sisa_by_date,stock_opname,adj 
                  )
    SELECT 
    
     ct_assy_list.part_no
     , ct_assy_list.part_name
    
    --,isnull(ctestokawal.qty,0) + (isnull(ctestok_in_date.qty_in, 0) - isnull(ctest_trxout_bydate.qty_out,0)) as stok_awal
    ,
    case when isnull(cte_stock_opname_count.cnt,0) = 0 then  
     (isnull(ctestokawal.qty,0) + (isnull(ctestok_in_date.qty_in,0)) -  (isnull(ctest_trxout_bydate.qty_out,0) ))
     else 
     case when cte_stock_opname_count.date_stock_opname=dateadd(day, -1, @tgstart) then 
 
     isnull(cte_stock_opname_awal.qty,0) 
     else  
     --((isnull(ct_brg_in_awal_periode.qty,0) + isnull(cte_inject_in_rm_periode.qty,0)) - (isnull(ct_brg_out_awal_periode.qty,0) + isnull(cte_inject_out_rm_periode.qty,0) ))
     (isnull(cte_stock_opname_awal.qty,0) + (isnull(ctestok_in_date.qty_in,0) -  (isnull(ctest_trxout_bydate.qty_out,0) ))) 
     end 
     end  as stok_awal

    ,isnull(ctestok_trxawal.qty_in, 0) as qty_in
    
    ,isnull(ctest_trxoutawal.qty_out, 0) as qty_out
    
    ,(isnull(ctestokawal.qty,0) + (isnull(ctestok_trxawal.qty_in,0)) -  isnull(ctest_trxoutawal.qty_out, 0) ) as sisa
    
    ,ct_assy_list.unit as unit --(isnull(ctestok_trxawal.unit, 'PCS')) as unit
    ,isnull(ctestok_in_date.qty_in,0) as qty_in_by_date
    ,isnull(ctest_trxout_bydate.qty_out,0) as qty_out_by_date
    
    --,(isnull(ctestokawal.qty,0) + ( ( isnull(ctestok_in_date.qty_in,0) - isnull(ctest_trxout_bydate.qty_out,0)) ) + (isnull(ctestok_trxawal.qty_in, 0) - isnull(ctest_trxoutawal.qty_out, 0))) as sisa_by_date
    ,
    (
    (case when  isnull(cte_stock_opname_count.cnt,0)= 0 then  
     (isnull(ctestokawal.qty,0) + (isnull(ctestok_in_date.qty_in,0) -  isnull(ctest_trxout_bydate.qty_out,0) ))
     else 
     case when cte_stock_opname_count.date_stock_opname=dateadd(day, -1, @tgstart) then 
     isnull(cte_stock_opname_awal.qty,0) 
     else  
     (isnull(cte_stock_opname_awal.qty,0) + (isnull(ctestok_in_date.qty_in,0)  -  (isnull(ctest_trxout_bydate.qty_out,0) ))) 
     end 
     end
      + 
     ((isnull(ctestok_trxawal.qty_in,0))) - (isnull(ctest_trxoutawal.qty_out,0)))
    ) as saldo_akhir
    ,
    isnull(cte_stock_opname_akhir.qty,0) as sto
    ,
    case when cte_stock_opname_tgl_akhir.qty is null then 0 else 
    cte_stock_opname_tgl_akhir.qty -
    (
    (case when  isnull(cte_stock_opname_count.cnt,0)= 0 then  
     (isnull(ctestokawal.qty,0) + (isnull(ctestok_in_date.qty_in,0) -  isnull(ctest_trxout_bydate.qty_out,0) ))
     else 
     case when cte_stock_opname_count.date_stock_opname=dateadd(day, -1, @tgstart) then 
     isnull(cte_stock_opname_awal.qty,0) 
     else  
     (isnull(cte_stock_opname_awal.qty,0) + (isnull(ctestok_in_date.qty_in,0)  -  (isnull(ctest_trxout_bydate.qty_out,0) ))) 
     end 
     end
      + 
     ((isnull(ctestok_trxawal.qty_in,0))) - (isnull(ctest_trxoutawal.qty_out,0)))
    ) 

    end
    
    from ct_assy_list 
     left outer join ctestok_trxawal on ct_assy_list.part_no = ctestok_trxawal.part_no
     left outer join ctest_trxoutawal on ct_assy_list.part_no=ctest_trxoutawal.part_no
     left outer join ctestokawal on ct_assy_list.part_no=ctestokawal.part_no
     left outer join ctestok_in_date on ct_assy_list.part_no=ctestok_in_date.part_no
     left outer join ctest_trxout_bydate on ct_assy_list.part_no=ctest_trxout_bydate.part_no
     left outer join cte_stock_opname_awal on ct_assy_list.part_no = cte_stock_opname_awal.part_no
     left outer join cte_stock_opname_akhir on ct_assy_list.part_no = cte_stock_opname_akhir.part_no  
    left outer join cte_stock_opname_count on ct_assy_list.part_no = cte_stock_opname_count.part_no  
     left outer join cte_stock_opname_tgl_akhir on ct_assy_list.part_no = cte_stock_opname_tgl_akhir.part_no
    return
end

GO

Used By

No items found