0% found this document useful (0 votes)
3 views

query_simpan

The document contains a SQL script that manages transactions for a sales order system. It includes operations such as creating temporary tables, inserting data into the MasterSO and detailso tables, and handling errors with a try-catch block. The script also calculates and updates various fields related to sales orders based on specific conditions and data from other tables.

Uploaded by

Nico Yudianto
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

query_simpan

The document contains a SQL script that manages transactions for a sales order system. It includes operations such as creating temporary tables, inserting data into the MasterSO and detailso tables, and handling errors with a try-catch block. The script also calculates and updates various fields related to sales orders based on specific conditions and data from other tables.

Uploaded by

Nico Yudianto
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*CACHE=0*/ BEGIN TRY BEGIN TRANSACTION if (select

object_id('tempdb..#Kategori')) is not null drop table #Kategori SELECT * INTO


#Kategori FROM Kategori where cabang='006UA/01' declare @tgl datetime,
@pajakposretail money declare @kodenota varchar(50) declare @lastkodenota
varchar(50) declare @lastnomornota int declare @HrgSatuanIncludePPn bit declare
@isCustCome bit set @tgl= dbo.GetTransDate('006UA/01') DECLARE @kodedepan
VARCHAR(30),@cabang VARCHAR(15), @staff VARCHAR(15), @kode_tanggal VARCHAR(5),
@inisial VARCHAR(5) SET @cabang = '006UA/01' SET @staff = '006UA/01/01' SET
@inisial = 'KN' SET @staff = replace(right(@staff,5),'/','') SET @kode_tanggal =
right('00'+cast(datepart(dd, @tgl) as varchar(10)),2) SET @kode_tanggal =
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@ko
de_tanggal, '0', 'X'), '1', 'A'), '2', 'B'), '3', 'C'), '4', 'D'), '5', 'E'), '6',
'F'), '7', 'G'), '8', 'H'), '9', 'I') SET @kodedepan =
@cabang+'/'+@inisial+'/'+@staff+'/'+right('00'+cast(datepart(yy, @tgl) as
varchar(10)),2)+right('00'+cast(datepart(mm, @tgl) as
varchar(10)),2)+'/'+@kode_tanggal select @lastkodenota=max(kodenota) from MasterSO
where kodenota like @kodedepan + '%' set
@lastnomornota=isnull(cast(right(@lastkodenota,4) as int),0) set
@lastnomornota=@lastnomornota+1 set @kodenota=@kodedepan +
right('0000000000'+cast(@lastnomornota as varchar),4) declare @norentobject
varchar(50) set @norentobject='T000001' if (select pkp from pelanggan where
kode='006UA/01/000001')=1 OR (select isPPN from #kategori )=1 begin select
@pajakposretail=pajakposretail from #kategori where cabang='006UA/01' end else
begin set @pajakposretail=0 end select @HrgSatuanIncludePPn=PriceListIncPPn
from #kategori where cabang='006UA/01' set @isCustCome = 1 insert into MasterSO
(KodeNota, Tgl, Cust,ShipTo,Sellto, PPnPersenManual,
MataUang,deliverydate,Keterangan,
Potongan,Dibatalkan,HrgSatuanIncludePPn,Operator, TglEntry, CreateBy, CreateDate,
StatusCetak, JamMulai, JamAKhir, NoRentObject, jml, Sales, GabungRentObject,
StatusRentObject,AsalCust,Keterangan1,NamaKirim,AlamatKirim,TelpKirim,SOVia,KodeAca
k,NoRefDraftSO,StatusDelivery,isBooking,NoRangka,NoPol,isCustCome,eta,etd,lamaKredi
t) values (@kodenota, @tgl, '006UA/01/000001', '006UA/01/000001',
'006UA/01/000001', @pajakposretail, 'IDR', cast(convert(varchar(30), getdate(),
112) as datetime),'','0', 0 , @HrgSatuanIncludePPn, '006UA/01/01', GETDATE(),
'006UA/01/01', GETDATE(), '0', GETDATE(), GETDATE(), @norentobject, 1,
'006UA/01/0001', @kodenota+';'+@norentobject, '0', '0','Bu
vika',NULL,NULL,NULL,'','','',NULL,0,NULL,NULL,@isCustCome,NULL,NULL,0) insert
into detailso (KodeNota, NoItem, Brg, Gudang, Jml, Rasio, HrgSatuan,
DiscValuePre, Disc1, Disc2, Disc3, Disc4, DiscValuePost, Keterangan, JmlPaket,
Paket,JmlBonus,
RasioBonus,WaktuPesan,NoDO,ParentStok,ParentNoItem,point,operatorpesan) values
(@kodenota, '1', '006UA/010000001', '006UA/01/01', 1.4, '1', 29400.0, '0', '0.0',
'0.0', '0.0', 0.0, 0.0, '', 1.0, '_', '0', '1', '2019-07-22
00:23:48.087','',0,0,0.0,'006UA/01/01') update MasterSO set Keterangan1 ='Bu
vika' where kodenota = @kodenota if (select
object_id('tempdb..##lock_cashier__T000001')) is not null drop table
##lock_cashier__T000001 if (select object_id('tempdb..##lock_cashier_T000001')) is
not null drop table ##lock_cashier_T000001 declare @Cycle int, @CycleToday int
select top 1 @cycle = cast (d1.NoItem/1000 as numeric(18,0)) from detailso d1 where
d1.kodenota = @kodenota order by d1.NoItem desc select @CycleToday = count(Cycle)
from ( select Cycle = cast (d1.NoItem/1000 as numeric(18,0)), m.kodenota from
masterso m, detailso d1, #kategori k where d1.kodenota = m.kodenota and m.tgl =
dbo.GetTransDate('006UA/01') and substring(m.kodenota,10,1) = 'K' and
d1.WaktuPesan <= (select top 1 d2.waktuPesan from detailso d2 where d2.kodenota =
@kodenota and cast(d2.NoItem/1000 as numeric(18,0)) = @cycle order by d2.WaktuPesan
desc) and m.kodenota like k.cabang+'%' group by m.kodenota, cast (d1.NoItem/1000
as numeric(18,0)) ) x COMMIT END TRY BEGIN CATCH IF (@@TRANCOUNT > 0)
ROLLBACK DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int SELECT @ErrMsg =
ERROR_MESSAGE(), @ErrSeverity = ERROR_SEVERITY() RAISERROR(@ErrMsg, @ErrSeverity,
1) END CATCH update masterso set Operator = '006UA/01/01',TglEntry =
getdate(),keteranganorder = '' where kodenota =@kodenota if (select
object_id('tempdb..#detailso')) is not null drop table #detailso SELECT * INTO
#detailso FROM detailso WHERE kodenota=@kodenota if (select
object_id('tempdb..#detailsononstok')) is not null drop table #detailsononstok
SELECT * INTO #detailsononstok FROM detailsononstok WHERE kodenota=@kodenota if
(select object_id('tempdb..#masterso')) is not null drop table #masterso SELECT *
INTO #masterso FROM masterso WHERE kodenota=@kodenota select (select
COUNT(kodenota) from #detailso where kodenota=@kodenota AND Noitem>0 AND
noitem<1000),(select COUNT(kodenota) from #detailsononstok where kodenota=@kodenota
AND Noitem>0 AND noitem<1000) ,@kodenota,IsNull(@Cycle,0) as CycleOrder,
IsNull(@CycleToday,0) as CycleToday,(select top 1 statuscetak from #detailso where
kodenota=@kodenota AND Noitem>0 AND noitem<1000 order by statuscetak) as
statuscetak, (select keterangan1 from #masterso where kodenota=@kodenota ) as
keterangan1, (select Cust from #masterso where kodenota=@kodenota ) as kodeCust

You might also like