SELECT * FROM dbo.Note_Jenis AS nj
SELECT * FROM dbo.Note_Terbit_Master WHERE No_Ref IN ('2016.002737', '2015.096049')
SELECT * FROM dbo.Note_Terbit_Master WHERE No_Note = '096049/DN.CL/OG/I/2016'
SELECT * FROM dbo.Note_Terbit_Detail WHERE No_Ref IN ('2016.002737', '2016.096049')

SELECT
  dbo.Note_Lunas_Master.No_Ref,
  dbo.Note_Lunas_Master.Create_Date,
  dbo.Note_Lunas_Master.Tgl_Bayar,
  dbo.Note_Terbit_Master.No_Note,
  dbo.Note_Terbit_Master.Tipe,
  dbo.Polis_Asuransi.No_Proposal,
  dbo.Note_Terbit_Master.Total,
  dbo.Note_Terbit_Master.Jml_Adjust,
  dbo.Note_Lunas_Master.No_Bukti,
  dbo.Note_Lunas_Master.Cicilan_Ke,
  Pelunasan = CASE dbo.Note_Terbit_Master.Write_Off
                WHEN 1 THEN dbo.Note_Terbit_Master.Tipe * SUM(dbo.Note_Terbit_Master.Jml_Adjust)
                ELSE dbo.Note_Terbit_Master.Tipe * SUM(dbo.Note_Lunas_Master.Jumlah)
              END,
  dbo.Note_Terbit_Master.Write_Off
FROM
  dbo.Note_Lunas_Master
  INNER JOIN dbo.Note_Terbit_Master ON dbo.Note_Lunas_Master.No_Ref = dbo.Note_Terbit_Master.No_Ref
  INNER JOIN dbo.Polis_Asuransi ON dbo.Note_Terbit_Master.No_Proposal = dbo.Polis_Asuransi.No_Proposal
  INNER JOIN dbo.Perusahaan ON dbo.Polis_Asuransi.No_Prs = dbo.Perusahaan.No_Prs
  LEFT JOIN dbo.Polis_Rekening AS pr ON dbo.Polis_Asuransi.No_Polis = pr.No_Polis -- add by Pribadi Pamungkas, 2015-09-14. Request via JO no 2015.09.08-001 dari Finance 
  LEFT JOIN dbo.Jenis_Rekening_Note AS jrn ON dbo.Note_Terbit_Master.Jenis = jrn.Jenis_Note
WHERE
  dbo.Note_Terbit_Master.No_Ref IN ('2016.002737', '2015.096049')
GROUP BY
  dbo.Note_Lunas_Master.No_Ref,
  dbo.Note_Lunas_Master.Create_Date,
  dbo.Note_Lunas_Master.Tgl_Bayar,
  dbo.Note_Terbit_Master.No_Note,
  dbo.Note_Terbit_Master.Tipe,
  dbo.Polis_Asuransi.No_Proposal,
  dbo.Note_Terbit_Master.Total,
  dbo.Note_Terbit_Master.Jml_Adjust,
  dbo.Note_Lunas_Master.No_Bukti,
  dbo.Note_Lunas_Master.Rek_No,
  dbo.Note_Lunas_Master.Cicilan_Ke,
  dbo.Note_Terbit_Master.Write_Off,
  pr.Rek_No2, -- add by Pribadi Pamungkas, 2015-09-14. Request via JO no 2015.09.08-001 dari Finance
  pr.Rek_Nama2, -- add by Pribadi Pamungkas, 2015-09-14. Request via JO no 2015.09.08-001 dari Finance
  pr.Bank_Cabang2,  -- add by Pribadi Pamungkas, 2015-09-15. Request via JO no 2015.09.08-001 dari Finance
  dbo.Note_Terbit_Master.Jenis
--------------
-- start: cek rekening cabang
DECLARE 
  @cabang_note VARCHAR(14),
  @no_polis VARCHAR(11),
  @jenis_note SMALLINT,
  @new_rek_no VARCHAR(30) = '',
  @new_rek_nama VARCHAR(50) = '',
  @new_rek_bank VARCHAR(15) = '',
  @new_rek_cabang VARCHAR(60) = ''
SELECT @cabang_note=COALESCE(ntm.Cabang,''), @no_polis=pa.No_Polis, @jenis_note=ntm.Jenis FROM dbo.Note_Terbit_Master AS ntm INNER JOIN dbo.Polis_Asuransi AS pa ON ntm.No_Proposal = pa.No_Proposal WHERE ntm.No_Ref IN ('2016.002737', '2016.096049')-- ada kolom No_Polis sih, tapi lebih aman join ke Polis_Asuransi
PRINT @no_polis
IF (@cabang_note <> '') -- jika di Note Terbit Master didefinisikan cabang, maka
  BEGIN
  PRINT 'A'
    -- start: gunakan rekening cabang
    SELECT 
      rekcab.Rek_No,
      rekcab.Rek_Nama,
      bm.Nama_Pendek,
      rekcab.Bank_Cabang
    FROM 
      dbo.Jenis_Rekening_Note AS jrn CROSS APPLY 
      dbo.func_rek_cabang_note_lunas(@no_polis,jrn.Jenis_Rek,@cabang_note) AS rekcab LEFT JOIN
      dbo.Bank_Master AS bm ON rekcab.Kode_Bank = bm.Kode_Bank
    WHERE
      jrn.Jenis_Note = @jenis_note
    -- end: gunakan rekening cabang
  END
ELSE
  BEGIN
  PRINT 'B'
    -- start: gunakan rekening polis
    SELECT
      rek.Rek_No,
      rek.Rek_Nama,
      bm.Nama_Pendek,
      rek.Bank_Cabang
    FROM
      dbo.Jenis_Rekening_Note AS jrn CROSS APPLY 
	    dbo.func_rek_note_lunas(@no_polis,jrn.Jenis_Rek) AS rek LEFT JOIN
      dbo.Bank_Master AS bm ON rek.Kode_Bank = bm.Kode_Bank
    WHERE
      jrn.Jenis_Note = @jenis_note
    -- end: gunakan rekening polis
  END