dbo.sp_nonhib_insert

Description

Properties

Name Value
ANSI Nulls ON

True

Quoted Identifier ON

True

Encrypted

False

Execute As
Assembly

Parameters

Name Data Type Length Description
@datanoinv varchar -1
@typebc varchar 5
@nobl varchar 50
@tgbl date 3

SQL Script

SET QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
CREATE PROCEDURE dbo.sp_nonhib_insert
(@datanoinv as varchar(max), @typebc as varchar(5), @nobl varchar(50), @tgbl date )
AS
BEGIN
 declare @tbl_listinv_bl table  ( id_inv_hdr bigint,noinv varchar(60))
 declare @sts varchar(2), @desc varchar(50)
 
 
 declare @idata varchar(50)
 DECLARE a_cursor CURSOR FOR 
 select data from  dbo.Split( @datanoinv, CHAR(124))
 OPEN a_cursor
 FETCH NEXT FROM a_cursor INTO @idata
 WHILE @@FETCH_STATUS = 0 
 BEGIN -- begin here
  
  insert into @tbl_listinv_bl (id_inv_hdr, noinv) values((select data from  dbo.Split( @idata, ',') where ID = 1), (select data from  dbo.Split( @idata, ',') where ID = 2))
 FETCH NEXT FROM a_cursor INTO @idata
 END
 CLOSE a_cursor
 DEALLOCATE a_cursor
 
 if (select COUNT(*) as cnt  from dbo.tbl_bc_in_hdr where dbo.tbl_bc_in_hdr.nobl = @nobl and dbo.tbl_bc_in_hdr.tglbl = @tgbl ) > 0
 begin
  set @sts = '04'
  set @desc = 'Data Sudah Ada di data BC Lakukan Recalculate jika ingin menggabungkan ke no nomor '+@nobl+' tgl '+cast(@tgbl as varchar)
  goto msg  
 end 
 
 declare @ihdr bigint, @inoinv varchar(50)
 
 DECLARE i_cursor CURSOR FOR 
 select id_inv_hdr, noinv from @tbl_listinv_bl
 OPEN i_cursor
 FETCH NEXT FROM i_cursor INTO @ihdr, @inoinv 
 WHILE @@FETCH_STATUS = 0 
 BEGIN -- begin here
  if (select COUNT(*) as cnt from dbo.tbl_nonhib where nobl = @nobl and id_nonhib_hdr=@ihdr) = 0
  begin
   set @sts = '01'
   set @desc = 'Data Not Found '+@nobl+' idhdr= '+cast(@ihdr as varchar)+' noinv= '+@inoinv
   goto msg   
  end 
     
 FETCH NEXT FROM i_cursor INTO @ihdr, @inoinv
 END
 CLOSE i_cursor
 DEALLOCATE i_cursor
 
 
 
 declare @jmldetil int
 set @jmldetil = (SELECT COUNT(*) AS cnt
  FROM         tbl_nonhib INNER JOIN
                      v_nonhib_inv_dtl ON tbl_nonhib.id_nonhib_hdr = v_nonhib_inv_dtl.id_nonhib_hdr
  WHERE     (tbl_nonhib.nobl = @nobl) AND (tbl_nonhib.tgbl = @tgbl))

     if (select COUNT(*) 
   FROM       v_nonhib_inv_dtl INNER JOIN
      tbl_nonhib ON v_nonhib_inv_dtl.id_nonhib_hdr = tbl_nonhib.id_nonhib_hdr LEFT OUTER JOIN
     tbl_data_part_hs ON v_nonhib_inv_dtl.partnumberx = tbl_data_part_hs.part_no
  WHERE     (tbl_nonhib.nobl = @nobl) AND (tbl_nonhib.tgbl = @tgbl) and tbl_data_part_hs.kode_barang is null
  
  GROUP BY
     dbo.v_nonhib_inv_dtl.partname,
     dbo.v_nonhib_inv_dtl.UOMPFX,
     dbo.tbl_data_part_hs.hs_code,tbl_data_part_hs.kode_barang) >0
 begin
  set @sts = '01'
   set @desc = ' Partnumber tidak ditemukan, silahkan isi dahulu (bisa bertanya ke bagian part)...'
   goto msg   
 end    

 BEGIN TRY  
 BEGIN TRANSACTION
  declare @now date
  set @now = GETDATE()
  declare @ndpdm float
  set @ndpdm = (
   select top 1 nilai from dbo.tblKurs where (TgAkhir >= @now and TgAwal <= @now) and KdKurs = 'USD'  
  )
  -- insert header
  insert into dbo.tbl_bc_in_hdr (
  [CAR]
  ,statusdok 
  ,typedok
  ,bc_in_from_in
  ,KdKpbc
  ,Tujuan
  ,JnsBarang
  ,TujuanKirim
  ,PasokNama
  ,PasokAlmt
  ,PasokNeg
  ,AngkutNama
  ,AngkutFl
  ,Moda
  ,KdVal
  ,NilInv
  ,KdHrg
  ,JmBrg
  ,Registrasi
  ,UsahaNama
  ,UsahaAlmt
  ,UsahaStatus
  ,KdTPB
  ,ApiKd
  ,ApiNo
  ,KdKpbcBongkar
  ,KdKpbcAwas
  ,Namattd
  ,KotaTtd
  ,nobl
  ,tglbl
  ,Ndpbm
  ) 
 select top 1
  '-'
  ,'DATA BARU'
  ,@typebc
  ,'INV NON HIB'
  ,'150300'
  ,'1'
  ,'01'
  ,'02'
  ,(select top 1 supplier_name from dbo.tbl_data_supplier where subcode_as400 = tbl_nonhib.vendor)
  ,(select top 1 supplier_address from dbo.tbl_data_supplier where subcode_as400 = tbl_nonhib.vendor)
  ,(select top 1 supplier_country from dbo.tbl_data_supplier where subcode_as400 = tbl_nonhib.vendor)
  ,' '
  ,'1'
  ,''
  ,'USD'
  ,0
        ,'1'
        ,@jmldetil
        ,(select top 1 Registrasi from tblImportir)
        ,(select top 1 impnama from tblImportir)
        ,(select top 1 impalmt from tblImportir)
        ,(select top 1 usahastatus from tblImportir)
        ,(select top 1 KdTPB from tblImportir)
        ,(select top 1 ApiKd from tblImportir)
        ,(select top 1 ApiNo from tblImportir)
        ,'150300'
        ,'150300'
        ,(select top 1 Namattd from tblImportir)
        ,(select top 1 KotaTtd from tblImportir)
        ,tbl_nonhib.nobl
        ,tbl_nonhib.tgbl
        ,@ndpdm
  
 from tbl_nonhib where nobl = @nobl and tgbl = @tgbl
    declare @idhdr bigint
    set @idhdr=@@IDENTITY
    
    
     -- detil inv begin here
 insert into dbo.tbl_bc_in_dtl (
  [CAR]
  ,Serial
  ,BrgUrai
  ,DNilInv
  ,NoHs
  ,id_bc_in_hdr
  ,JnsBarangDtl
  ,TujuanKirimDtl
  ,SeriTrp
  ,Merk
  ,Tipe
  ,SpfLain
  ,KdBrg
  ,JmlSat
  ,KdSat
  ,BrgAsal
  ,Penggunaan
  ,KemasJn
  ,KemasJm
 ) select 
   '-'
   ,row_number() over(order by (select 0)) 
   ,dbo.v_nonhib_inv_dtl.partname
   ,SUM(v_nonhib_inv_dtl.hargacif * dbo.v_nonhib_inv_dtl.INVQTY)/1000
   ,REPLACE(dbo.tbl_data_part_hs.hs_code,'.','')
   ,@idhdr
   ,'01'
   ,'02'
   ,'0'
   ,'-'
   ,'-'
   ,'-'
   ,isnull((tbl_data_part_hs.kode_barang) , '-')
   ,SUM(dbo.v_nonhib_inv_dtl.INVQTY)
   ,(ISNULL((select top 1 KD_SAT_BC from dbo.tbl_satuan_bc where KD_SAT_INTERNAL = dbo.v_nonhib_inv_dtl.UOMPFX), dbo.v_nonhib_inv_dtl.UOMPFX))
   ,(select top 1 dbo.tbl_bc_in_hdr.PasokNeg from dbo.tbl_bc_in_hdr where dbo.tbl_bc_in_hdr.id_bc_in_hdr=@idhdr )
   ,'1'
   ,'BX'
   , SUM(dbo.v_nonhib_inv_dtl.PACK)
 
  FROM         v_nonhib_inv_dtl INNER JOIN
      tbl_nonhib ON v_nonhib_inv_dtl.id_nonhib_hdr = tbl_nonhib.id_nonhib_hdr LEFT OUTER JOIN
     tbl_data_part_hs ON v_nonhib_inv_dtl.partnumberx = tbl_data_part_hs.part_no
  WHERE     (tbl_nonhib.nobl = @nobl) AND (tbl_nonhib.tgbl = @tgbl)
  
  GROUP BY
     dbo.v_nonhib_inv_dtl.partname,
     dbo.v_nonhib_inv_dtl.UOMPFX,
     dbo.tbl_data_part_hs.hs_code,tbl_data_part_hs.kode_barang
 -- update dokumen

 insert into dbo.tbl_bc_in_dok (
  CAR
  ,DokKd
  ,DokNo
  ,DokTg  
     ,id_bc_in_hdr
 )select 
   '-'
   ,'380'
   ,tbl_nonhib.noinv
   ,tbl_nonhib.tglinv
   ,@idhdr
 FROM         tbl_nonhib INNER JOIN
      v_nonhib_inv_dtl ON tbl_nonhib.id_nonhib_hdr = v_nonhib_inv_dtl.id_nonhib_hdr
 WHERE     (tbl_nonhib.nobl = @nobl) AND (tbl_nonhib.tgbl = @tgbl)
 group by 
  tbl_nonhib.noinv,
  tbl_nonhib.tglinv
 
 
 insert into dbo.tbl_bc_in_dok (
  CAR
  ,DokKd
  ,DokNo
  ,DokTg  
     ,id_bc_in_hdr
 )select 
   '-'
   ,'705'
   ,tbl_nonhib.nobl
   ,tbl_nonhib.tgbl
   ,@idhdr
 FROM         tbl_nonhib
 WHERE     (tbl_nonhib.nobl = @nobl) AND (tbl_nonhib.tgbl = @tgbl)
 group by
 tbl_nonhib.nobl,
 tbl_nonhib.tgbl
 
 --cek update pemasok
 if (select count(*) from dbo.tbl_bc_in_dok where id_bc_in_hdr=@idhdr and DokKd='380' and DokNo like '%SXC%') > 0
 begin
  update tbl_bc_in_hdr set PasokNama='PT SURABAYA AUTOCOMP INDONESIA',
  PasokAlmt='JL. NGORO INDUSTRI PERSADA KAV.7.1 MOJOKERTO JAWATIMUR'
  where  id_bc_in_hdr=@idhdr 
 end 
 if (select count(*) from dbo.tbl_bc_in_dok where id_bc_in_hdr=@idhdr and DokKd='380' and DokNo like '%MXD%') > 0
 begin
  update tbl_bc_in_hdr set PasokNama='PT SEMARANG AUTOCOMP MFG INDONESIA',
  PasokAlmt='JL. WALISONGO KM.98 SEMARANG 50151 JAWA TENGAH'
  where  id_bc_in_hdr=@idhdr 
 end
 if (select count(*) from dbo.tbl_bc_in_dok where id_bc_in_hdr=@idhdr and DokKd='380' and DokNo like '%JXD%') > 0
 begin
  update tbl_bc_in_hdr set PasokNama='PT JATIM AUTOCOMP INDONESIA',
  PasokAlmt='JL. RAYA WONOAYU NO.26 BELAKANG GEMPOL PASURUAN JATIM'
  where  id_bc_in_hdr=@idhdr 
 end
 --

 -- update kontener
 insert into dbo.tbl_bc_in_con (
    [id_bc_in_hdr]
      ,[CAR]
      ,[ContNo]
      ,[ContUkur]
      ,[ContTipe]
 )select 
   @idhdr
   ,'-'
   ,dbo.v_nonhib_inv_dtl.CONTNO
   ,'40'
   ,'F'
 FROM tbl_nonhib INNER JOIN
    v_nonhib_inv_dtl ON tbl_nonhib.id_nonhib_hdr = v_nonhib_inv_dtl.id_nonhib_hdr
 WHERE     (tbl_nonhib.nobl = @nobl) AND (tbl_nonhib.tgbl = @tgbl)
 GROUP BY
   dbo.v_nonhib_inv_dtl.CONTNO      
 
 -- insert to partname detil bc in
    insert into tbl_bc_in_dtl_partnumber (
       [inv_dtl]
      ,[inv_hdr_id]
      ,[id_bc_in_hdr]
      ,[noinv]
      ,[car]
       ,part_no2
      ,[part_number]
      ,part_name
      ,[part_name_customs]
      ,[bm]
      ,[qty]
      ,[unit]
      ,[harga]
      ,[kdval]
      ,[box]
      ,[pack]
      ,hscode
      ,id_partnumber
      ,seri_detil
    
    ) select
       dbo.v_nonhib_inv_dtl.id_nonhib_dtl
      ,dbo.v_nonhib_inv_dtl.id_nonhib_hdr
      ,@idhdr
      ,dbo.v_nonhib_inv_dtl.INVOICE_NO
      ,'-'
      ,(select top 1 dbo.tbl_data_part_hs.part_no2 from tbl_data_part_hs where tbl_data_part_hs.part_no = dbo.v_nonhib_inv_dtl.partnumberx )
      ,dbo.v_nonhib_inv_dtl.partnumberx
      ,v_nonhib_inv_dtl.partname
      ,isnull(dbo.v_nonhib_inv_dtl.partnamecustoms,'-UNKNOWN' + replace(convert(varchar(255) , newid()), '-','')) as part_name_customs
      ,(isnull((select top 1 dbo.tbl_data_part_hs.bm from tbl_data_part_hs where tbl_data_part_hs.part_name = dbo.v_nonhib_inv_dtl.partnumberx ), 0) ) as bm
      ,dbo.v_nonhib_inv_dtl.INVQTY
      ,(ISNULL((select top 1 KD_SAT_BC from dbo.tbl_satuan_bc where KD_SAT_INTERNAL = dbo.v_nonhib_inv_dtl.UOMPFX), dbo.v_nonhib_inv_dtl.UOMPFX))
      ,(isnull(v_nonhib_inv_dtl.hargacif, 0)) as cekprice -- i dont know from where
      ,'USD' -- i dont know from where
      ,0 -- i dont know from where
      ,dbo.v_nonhib_inv_dtl.PACK 
      ,(isnull((select top 1 dbo.tbl_data_part_hs.hs_code from tbl_data_part_hs where tbl_data_part_hs.part_name = dbo.v_nonhib_inv_dtl.partnumberx ), '000000000') ) as hscode
      ,(isnull((select top 1 dbo.tbl_data_part_hs.id from tbl_data_part_hs where tbl_data_part_hs.part_no = dbo.v_nonhib_inv_dtl.partnumberx ), 0) ) as idpart
      ,(select top 1 Serial from tbl_bc_in_dtl where id_bc_in_hdr = @idhdr and BrgUrai= v_nonhib_inv_dtl.partnamecustoms )
    FROM tbl_nonhib INNER JOIN
    v_nonhib_inv_dtl ON tbl_nonhib.id_nonhib_hdr = v_nonhib_inv_dtl.id_nonhib_hdr
 WHERE     (tbl_nonhib.nobl = @nobl) AND (tbl_nonhib.tgbl = @tgbl)   
  
 
 --update
 update tbl_bc_in_hdr set NilInv=(select SUM(DNilInv) as asum from tbl_bc_in_dtl where id_bc_in_hdr=@idhdr)  where  id_bc_in_hdr=@idhdr
 update dbo.tbl_nonhib set dbo.tbl_nonhib.status = 'DATA PROCESSED' where dbo.tbl_nonhib.nobl = @nobl and dbo.tbl_nonhib.tgbl = @tgbl 
 INSERT INTO tbl_bc_in_hdr_h_status
                      (id_bc_in_hdr, statusdok, status_date)
 VALUES     (@idhdr, (select top 1 tbl_bc_in_hdr.statusdok from tbl_bc_in_hdr where tbl_bc_in_hdr.inv_hdr_id = @idhdr), GETDATE()) 
  
  
 COMMIT
END TRY 
BEGIN CATCH
-- Whoops, there was an error
IF @@TRANCOUNT > 0
 ROLLBACK

-- Raise an error with the details of the exception
DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
Set @ErrMsg = ERROR_MESSAGE()
set @ErrSeverity = ERROR_SEVERITY()
select @ErrMsg as status,@ErrSeverity as description
RAISERROR(@ErrMsg, @ErrSeverity, 1)
END CATCH 

set @sts = '00'
 set @desc = 'Data Processed'
 goto msg

msg:
  select @sts as status, @desc as description   
 
END
GO

Used By

No items found