/****** Object:  Trigger [insertwu]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[insertwu]'))
DROP TRIGGER [dbo].[insertwu]
GO
/****** Object:  Trigger [vougrno]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[vougrno]'))
DROP TRIGGER [dbo].[vougrno]
GO
/****** Object:  Trigger [vou_update]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[vou_update]'))
DROP TRIGGER [dbo].[vou_update]
GO
/****** Object:  Trigger [vou_insert]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[vou_insert]'))
DROP TRIGGER [dbo].[vou_insert]
GO
/****** Object:  Trigger [vou_delete]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[vou_delete]'))
DROP TRIGGER [dbo].[vou_delete]
GO
/****** Object:  Trigger [updltxndt]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[updltxndt]'))
DROP TRIGGER [dbo].[updltxndt]
GO
/****** Object:  Trigger [updatevouhist]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[updatevouhist]'))
DROP TRIGGER [dbo].[updatevouhist]
GO
/****** Object:  Trigger [roomServiceBlobk]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[roomServiceBlobk]'))
DROP TRIGGER [dbo].[roomServiceBlobk]
GO
/****** Object:  Trigger [CashPermission]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[CashPermission]'))
DROP TRIGGER [dbo].[CashPermission]
GO
/****** Object:  Trigger [trg_ins_toutlet]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trg_ins_toutlet]'))
DROP TRIGGER [dbo].[trg_ins_toutlet]
GO
/****** Object:  Trigger [suppcheck]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[suppcheck]'))
DROP TRIGGER [dbo].[suppcheck]
GO
/****** Object:  Trigger [updstbal]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[updstbal]'))
DROP TRIGGER [dbo].[updstbal]
GO
/****** Object:  Trigger [storeminusbalance]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[storeminusbalance]'))
DROP TRIGGER [dbo].[storeminusbalance]
GO
/****** Object:  Trigger [CHKStoreMaster]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[CHKStoreMaster]'))
DROP TRIGGER [dbo].[CHKStoreMaster]
GO
/****** Object:  Trigger [tg_updatehctrlno2]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[tg_updatehctrlno2]'))
DROP TRIGGER [dbo].[tg_updatehctrlno2]
GO
/****** Object:  Trigger [stpurinsert]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[stpurinsert]'))
DROP TRIGGER [dbo].[stpurinsert]
GO
/****** Object:  Trigger [StorehistQTY1]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[StorehistQTY1]'))
DROP TRIGGER [dbo].[StorehistQTY1]
GO
/****** Object:  Trigger [spstupdate]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[spstupdate]'))
DROP TRIGGER [dbo].[spstupdate]
GO
/****** Object:  Trigger [spstdelete]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[spstdelete]'))
DROP TRIGGER [dbo].[spstdelete]
GO
/****** Object:  Trigger [shminusbalance]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[shminusbalance]'))
DROP TRIGGER [dbo].[shminusbalance]
GO
/****** Object:  Trigger [forltxndtupdate]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[forltxndtupdate]'))
DROP TRIGGER [dbo].[forltxndtupdate]
GO
/****** Object:  Trigger [forltxndtdelete]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[forltxndtdelete]'))
DROP TRIGGER [dbo].[forltxndtdelete]
GO
/****** Object:  Trigger [StockPosting]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[StockPosting]'))
DROP TRIGGER [dbo].[StockPosting]
GO
/****** Object:  Trigger [sptbrs1invalidrooms]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[sptbrs1invalidrooms]'))
DROP TRIGGER [dbo].[sptbrs1invalidrooms]
GO
/****** Object:  Trigger [sptbrs1_update_wingno]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[sptbrs1_update_wingno]'))
DROP TRIGGER [dbo].[sptbrs1_update_wingno]
GO
/****** Object:  Trigger [sptbrs1_del]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[sptbrs1_del]'))
DROP TRIGGER [dbo].[sptbrs1_del]
GO
/****** Object:  Trigger [updatereservation]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[updatereservation]'))
DROP TRIGGER [dbo].[updatereservation]
GO
/****** Object:  Trigger [sptbrsUDI]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[sptbrsUDI]'))
DROP TRIGGER [dbo].[sptbrsUDI]
GO
/****** Object:  Trigger [statusdate]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[statusdate]'))
DROP TRIGGER [dbo].[statusdate]
GO
/****** Object:  Trigger [CI]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[CI]'))
DROP TRIGGER [dbo].[CI]
GO
/****** Object:  Trigger [updateposition]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[updateposition]'))
DROP TRIGGER [dbo].[updateposition]
GO
/****** Object:  Trigger [PHOUTLETINS]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[PHOUTLETINS]'))
DROP TRIGGER [dbo].[PHOUTLETINS]
GO
/****** Object:  Trigger [chkposition]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[chkposition]'))
DROP TRIGGER [dbo].[chkposition]
GO
/****** Object:  Trigger [check_date]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[check_date]'))
DROP TRIGGER [dbo].[check_date]
GO
/****** Object:  Trigger [MBInsert]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[MBInsert]'))
DROP TRIGGER [dbo].[MBInsert]
GO
/****** Object:  Trigger [MizUpdate]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[MizUpdate]'))
DROP TRIGGER [dbo].[MizUpdate]
GO
/****** Object:  Trigger [mizaminus]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[mizaminus]'))
DROP TRIGGER [dbo].[mizaminus]
GO
/****** Object:  Trigger [kitprimeins]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[kitprimeins]'))
DROP TRIGGER [dbo].[kitprimeins]
GO
/****** Object:  Trigger [insertkh1]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[insertkh1]'))
DROP TRIGGER [dbo].[insertkh1]
GO
/****** Object:  Trigger [CheckGuest]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[CheckGuest]'))
DROP TRIGGER [dbo].[CheckGuest]
GO
/****** Object:  Trigger [KITINSERT]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[KITINSERT]'))
DROP TRIGGER [dbo].[KITINSERT]
GO
/****** Object:  Trigger [KITHISTDEL]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[KITHISTDEL]'))
DROP TRIGGER [dbo].[KITHISTDEL]
GO
/****** Object:  Trigger [khupdate]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[khupdate]'))
DROP TRIGGER [dbo].[khupdate]
GO
/****** Object:  Trigger [smsdelete]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[smsdelete]'))
DROP TRIGGER [dbo].[smsdelete]
GO
/****** Object:  Trigger [updatecodate]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[updatecodate]'))
DROP TRIGGER [dbo].[updatecodate]
GO
/****** Object:  Trigger [guestcidate]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[guestcidate]'))
DROP TRIGGER [dbo].[guestcidate]
GO
/****** Object:  Trigger [gst_expecteddep]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[gst_expecteddep]'))
DROP TRIGGER [dbo].[gst_expecteddep]
GO
/****** Object:  Trigger [gnewcidate]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[gnewcidate]'))
DROP TRIGGER [dbo].[gnewcidate]
GO
/****** Object:  Trigger [gppinsert]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[gppinsert]'))
DROP TRIGGER [dbo].[gppinsert]
GO
/****** Object:  Trigger [updtbal]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[updtbal]'))
DROP TRIGGER [dbo].[updtbal]
GO
/****** Object:  Trigger [glrpimisert]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[glrpimisert]'))
DROP TRIGGER [dbo].[glrpimisert]
GO
/****** Object:  Trigger [GLPRIMUSERNM]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[GLPRIMUSERNM]'))
DROP TRIGGER [dbo].[GLPRIMUSERNM]
GO
/****** Object:  Trigger [deletecasecade]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[deletecasecade]'))
DROP TRIGGER [dbo].[deletecasecade]
GO
/****** Object:  Trigger [loginupd]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[loginupd]'))
DROP TRIGGER [dbo].[loginupd]
GO
/****** Object:  Trigger [logininsert]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[logininsert]'))
DROP TRIGGER [dbo].[logininsert]
GO
/****** Object:  Trigger [comuserdel]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[comuserdel]'))
DROP TRIGGER [dbo].[comuserdel]
GO
/****** Object:  Trigger [INSCOMPANY]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[INSCOMPANY]'))
DROP TRIGGER [dbo].[INSCOMPANY]
GO
/****** Object:  Trigger [ForCoLogo]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[ForCoLogo]'))
DROP TRIGGER [dbo].[ForCoLogo]
GO
/****** Object:  Trigger [comlogins]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[comlogins]'))
DROP TRIGGER [dbo].[comlogins]
GO
/****** Object:  Trigger [updatechalan]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[updatechalan]'))
DROP TRIGGER [dbo].[updatechalan]
GO
/****** Object:  Trigger [INSERTCHALAN]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[INSERTCHALAN]'))
DROP TRIGGER [dbo].[INSERTCHALAN]
GO
/****** Object:  Trigger [delchal]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[delchal]'))
DROP TRIGGER [dbo].[delchal]
GO
/****** Object:  Trigger [bhinsert]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[bhinsert]'))
DROP TRIGGER [dbo].[bhinsert]
GO
/****** Object:  Trigger [pwinsert]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[pwinsert]'))
DROP TRIGGER [dbo].[pwinsert]
GO
/****** Object:  Trigger [acclog_insert]    Script Date: 07/04/2026 20:30:01 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[acclog_insert]'))
DROP TRIGGER [dbo].[acclog_insert]
GO
/****** Object:  Trigger [dbo].[acclog_insert]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.acclog_insert    Script Date: 21/09/2006 12:14:21 PM ******/ /****** Modified on 06/Oct/2009 : 14 May 2019  Ram *****/  
CREATE  TRIGGER [dbo].[acclog_insert] ON [dbo].[acclog]  FOR INSERT AS declare @ctrlno numeric declare @unm varchar(50) declare @sessionid varchar(100) 
declare @host_name varchar(100) 
declare @username varchar(100)
select top 1 @username=usernm,@host_name=computer from comuser where [host_id]=host_id() 
select @sessionid=isnull(sessionid,''), @ctrlno=ctrlno,@unm=isnull(username,'')  from inserted
--if exists (select top 1 usernm from comuser where [host_id]=host_id()  and usernm is null and isnull(sessionid,'')=@sessionid)     or not exists 
--(select * from comuser where [host_id]=host_id()  and  isnull(sessionid,'')=@sessionid)    
--begin print 'You are not looged in' rollback raiserror('Please loggin and then try',16,-1) return end 	
if ltrim(rtrim(@unm))=''	   
if exists (select top 1 usernm from comuser where [host_id]=host_id()  and isnull(sessionid,'')=@sessionid) 		
begin         		 
update acclog set computer=@host_name, username=(select usernm from comuser where [host_id]=host_id()  and isnull(sessionid,'')=@sessionid) where ctrlno=@ctrlno  
end 		
update acclog set  usernm=@host_name+'-'+(select usernm from comuser where [host_id]=host_id()),computer=@host_name,txndt=getdate() where ctrlno=@ctrlno 		 insert into acclogdup select * from acclog  where ctrlno=@ctrlno     		 if exists (select top 1 usernm from comuser where computer=@host_name and isnull(sessionid,'')=@sessionid)         update acclog set username=(select top 1 usernm from comuser where computer=@host_name and isnull(sessionid,'')=@sessionid) where ctrlno=@ctrlno update acclog set  usernm=@host_name+'-'+username,computer=@host_name,txndt=getdate() where ctrlno=@ctrlno 		 insert into acclogdup select * from acclog  where ctrlno=@ctrlno 
GO
ALTER TABLE [dbo].[acclog] ENABLE TRIGGER [acclog_insert]
GO
/****** Object:  Trigger [dbo].[pwinsert]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[pwinsert] ON [dbo].[BASIC] 
/* with encryption */
FOR INSERT 
AS declare  
@userno numeric declare @ctrlno numeric select @ctrlno = ctrlno,@userno=isnull(userno,0)  from inserted if @userno=0 begin select @userno=max(userno) from basic set @userno = isnull(@userno,0) + 1 update basic set userno=@userno where ctrlno=@ctrlno End
GO
ALTER TABLE [dbo].[BASIC] ENABLE TRIGGER [pwinsert]
GO
/****** Object:  Trigger [dbo].[bhinsert]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.bhinsert    Script Date: 21/09/2006 12:14:20 PM ******/
/****** Object:  Trigger dbo.bhinsert    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.bhinsert    Script Date: 10/08/2004 8:28:15 PM ******/
CREATE TRIGGER [dbo].[bhinsert] ON [dbo].[BILLHIST] 
/* with encryption */
FOR INSERT 
AS
declare @rent money
declare @ctrlno numeric
select @rent=rent,@ctrlno=ctrlno from inserted
update billhist set orent=@rent where ctrlno=@ctrlno
GO
ALTER TABLE [dbo].[BILLHIST] ENABLE TRIGGER [bhinsert]
GO
/****** Object:  Trigger [dbo].[delchal]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.delchal    Script Date: 21/09/2006 12:14:20 PM ******/
/****** Object:  Trigger dbo.delchal    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.delchal    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.delchal    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[delchal] ON [dbo].[CHALAN] 
/* with encryption */
FOR  DELETE 
AS
declare @outlet int
declare @grno int
declare @grno1 int
declare @grno_co1 int
declare @company int
declare @fy int
declare @txndt datetime
declare @retval int
declare @stype int
begin transaction
select @stype=stype,@outlet=outlet,@grno=grno,@grno1=grno_t,@txndt=chldate,@grno_co1=grno_co1 from deleted
select @company=company from toutlet where outlet=@outlet
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
if @grno<>0
begin
exec delgrno @company,@fy,@grno,@txndt,@retval out
if @retval<>0  goto e_error
end
if @grno1<>0
begin
exec delgrno @company,@fy,@grno1,@txndt,@retval out
if @retval<>0  goto e_error
end
if @grno_co1<> 0
begin
set @company = 1
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
exec delgrno @company,@fy,@grno_co1,@txndt,@retval out
if @retval<>0  goto e_error
end
commit transaction
return
e_error:
rollback transaction
raiserror('deletion problem in TKT chalan',16,-1)
return
GO
ALTER TABLE [dbo].[CHALAN] ENABLE TRIGGER [delchal]
GO
/****** Object:  Trigger [dbo].[INSERTCHALAN]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.INSERTCHALAN    Script Date: 21/09/2006 12:14:20 PM ******/
/****** Object:  Trigger dbo.INSERTCHALAN    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.INSERTCHALAN    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.INSERTCHALAN    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[INSERTCHALAN]  ON [dbo].[CHALAN] 
/* with encryption */
FOR INSERT
AS
declare @usernm varchar(10)
declare @chctrlno numeric
select @usernm=usernm from comuser where [host_id]=host_id()
select @chctrlno=chctrlno from inserted
update chalan set user1=@usernm where chctrlno=@chctrlno
return
GO
ALTER TABLE [dbo].[CHALAN] ENABLE TRIGGER [INSERTCHALAN]
GO
/****** Object:  Trigger [dbo].[updatechalan]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.updatechalan    Script Date: 21/09/2006 12:14:20 PM ******/
/****** Object:  Trigger dbo.updatechalan    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.updatechalan    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.updatechalan    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[updatechalan] ON [dbo].[CHALAN] 
/* with encryption */
FOR  UPDATE
AS
if not update(finaldate) return
declare @usernm varchar(10)
declare @chctrlno numeric
select @usernm=usernm from comuser where [host_id]=host_id()
select @chctrlno=chctrlno from inserted
update chalan set user2=@usernm where chctrlno=@chctrlno
return
GO
ALTER TABLE [dbo].[CHALAN] ENABLE TRIGGER [updatechalan]
GO
/****** Object:  Trigger [dbo].[comlogins]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO











/****** Object:  Trigger dbo.comlogins    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.comlogins    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.comlogins    Script Date: 10/08/2004 8:28:15 PM ******/
CREATE TRIGGER [dbo].[comlogins]  ON [dbo].[comlog] 
/* with encryption */
FOR INSERT
AS
declare @details varchar(50)
declare @ctrlno numeric
declare @txntype tinyint
select @ctrlno=ctrlno,@details=details from inserted
set @txntype =  case 
         when substring(@details,1,9) = 'Logged in'   then 3
         when substring(@details,1,10)='Logged out' then 4
         when substring(@details,1,7)='Started'  then 1
         when substring(@details,1,12)='Disconnected' then  2
     end
update comlog set txntype=@txntype where ctrlno=@ctrlno
delete from comlog where datediff(d,txndt,getdate()) > 30











GO
ALTER TABLE [dbo].[comlog] ENABLE TRIGGER [comlogins]
GO
/****** Object:  Trigger [dbo].[ForCoLogo]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  TRIGGER [dbo].[ForCoLogo] ON [dbo].[company] FOR INSERT AS declare @company int SELECT @COMPANY=COMPANY FROM INSERTED insert into companylogo (company) values (@company)
GO
ALTER TABLE [dbo].[company] ENABLE TRIGGER [ForCoLogo]
GO
/****** Object:  Trigger [dbo].[INSCOMPANY]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.INSCOMPANY    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.INSCOMPANY    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.INSCOMPANY    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.INSCOMPANY    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[INSCOMPANY] ON [dbo].[company] 
/* with encryption */
FOR INSERT
AS
DECLARE @COMPANY INT
DECLARE @X INT
SELECT @COMPANY=COMPANY FROM INSERTED
SET @X = 1
WHILE @X <= 10
BEGIN
   INSERT INTO BILLPOSITION (COMPANY,POSITION) VALUES (@COMPANY,@X)
   SELECT  @X = @X + 1
END
GO
ALTER TABLE [dbo].[company] ENABLE TRIGGER [INSCOMPANY]
GO
/****** Object:  Trigger [dbo].[comuserdel]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.comuserdel    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.comuserdel    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.comuserdel    Script Date: 10/08/2004 8:28:15 PM ******/
CREATE TRIGGER [dbo].[comuserdel]  ON [dbo].[comuser] 
/* with encryption */
FOR  DELETE 
AS
declare @rowcount numeric
set @rowcount=@@rowcount
if @rowcount=0 return
if @rowcount > 1 return
insert into comlog (computer,usernm,details) values (isnull((select computer from comuser where [host_id]=host_id()),''),'','Disconnected at ' + convert(varchar(20),getdate()))
GO
ALTER TABLE [dbo].[comuser] ENABLE TRIGGER [comuserdel]
GO
/****** Object:  Trigger [dbo].[logininsert]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.logininsert    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.logininsert    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.logininsert    Script Date: 10/08/2004 8:28:15 PM ******/
CREATE TRIGGER [dbo].[logininsert]  ON [dbo].[comuser] 
/* with encryption */
FOR INSERT
AS
declare @compname varchar(50)
declare @usernm varchar(50)
select @compname=computer from inserted
update comuser set logintime=getdate() where computer=@compname AND [HOST_ID]=HOST_ID()
delete from comuser where datediff(n,logintime,getdate())  > 120
insert into comlog (computer,usernm,details) values 
(isnull((select top 1 computer from comuser where [host_id]=host_id()),''),'','started at ' + convert(varchar(20),getdate()))
GO
ALTER TABLE [dbo].[comuser] ENABLE TRIGGER [logininsert]
GO
/****** Object:  Trigger [dbo].[loginupd]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.loginupd    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.loginupd    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.loginupd    Script Date: 10/08/2004 8:28:15 PM ******/
CREATE TRIGGER [dbo].[loginupd] ON [dbo].[comuser] 
/* with encryption */
FOR  UPDATE AS
declare @computer varchar(50)
declare @usernmin varchar(50)
declare @usernmout varchar(50)
delete from comuser where datediff(d,logintime,getdate())  >6
if update(usernm) 
begin
select @usernmin=usernm,@computer=computer from inserted
select @usernmout=usernm from deleted
if @usernmout<>'' 
insert into comlog (computer,usernm,details) values ((select top 1 computer from comuser where [host_id]=host_id()),@usernmout,'Logged out at ' + convert(varchar(20),getdate()))
if @usernmin<>''
insert into comlog (computer,usernm,details) values ((select top 1 computer from comuser where [host_id]=host_id()),@usernmin,'Logged in at ' + convert(varchar(20),getdate()))
end
GO
ALTER TABLE [dbo].[comuser] ENABLE TRIGGER [loginupd]
GO
/****** Object:  Trigger [dbo].[deletecasecade]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  TRIGGER [dbo].[deletecasecade] ON [dbo].[GLPRIMLD] 
FOR DELETE 
 AS 
if exists (select * from deleted where deletable='N') 
begin 
rollback 
Print 'The account is linked to some other operation..can not delete'
raiserror('',16,-1) 
end

GO
ALTER TABLE [dbo].[GLPRIMLD] ENABLE TRIGGER [deletecasecade]
GO
/****** Object:  Trigger [dbo].[GLPRIMUSERNM]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[GLPRIMUSERNM]
   ON  [dbo].[GLPRIMLD]
   AFTER INSERT
AS 
BEGIN
SET NOCOUNT ON;
DECLARE @USERNM VARCHAR(100)
DECLARE @CTRLNO NUMERIC
IF EXISTS (select usernm from comuser where [host_id]=host_id()) 
SELECT TOP 1  @USERNM=USERNM FROM COMUSER WHERE [host_id]=host_id()
SELECT @CTRLNO=CTRLNO FROM INSERTED
Update glprimld set usernm=@USERNM,doo=getdate() where ctrlno=@CTRLNO
END
GO
ALTER TABLE [dbo].[GLPRIMLD] ENABLE TRIGGER [GLPRIMUSERNM]
GO
/****** Object:  Trigger [dbo].[glrpimisert]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[glrpimisert]  ON [dbo].[GLPRIMLD]  
/* with encryption */
FOR INSERT 
AS
declare @company int
declare @fy int
declare @newfy int
declare @cgp_Desc varchar(50)
declare @asset char(1)
declare @onac char(1)
declare @tdno int
declare @flag int
declare @cgp_actno int
declare @grp int
declare @add1 varchar(50)
declare @add2 varchar(50)
declare @city varchar(50)
declare @phone varchar(50)
declare @fydate datetime
declare @newactno numeric
declare @msg varchar(500)
declare @deletable char(1)
declare @newglacno numeric
declare @gstin nvarchar(50)
declare @pan nvarchar(50)
declare @ctrlno numeric
declare @nextyearfy numeric
--print 'periya'
select @ctrlno=ctrlno,@deletable=deletable,@company=COMPANY,@fy=FY,@cgp_desc=CGP_DESC,@asset=ASSET,@onac=ONAC,
@tdno=TDNO,@flag=FLAG,@cgp_actno=CGP_ACTNO,@grp=GRP,@add1=add1,@add2=add2,@city=city,@phone=phone ,@nextyearfy=nextyearfy
, @gstin=gstin,@pan=pan from inserted
if @nextyearfy=@fy begin set @msg = 'Error in nextyearfy' goto e_error end
select @fydate=finyear.[date]  from finyear where fy=@fy
set @msg = str(@fy) + str(@company)  + convert(varchar(20),@fydate)
--set @fydate= '1 apr ' + str(datepart(yy,@fydate)+1)
set @fydate = dbo.getbofy(dateadd(yy,1,@fydate))

if exists(select * from finyear where datediff(dd,'1/1/80',finyear.[date])=datediff(dd,'1/1/80',@fydate) and company=@company)
begin
	select @newfy=fy from finyear where datediff(dd,'1/1/80',finyear.[date])=datediff(dd,'1/1/80',@fydate) and company=@company
	set @newactno = 0
	if exists(select * from glprimld where company=@company and fy=@newfy and cgp_actno=@cgp_actno)
	begin
		select @newactno=max(cgp_actno) from glprimld where company=@company and fy = @newfy
		set @newactno = @newactno+1
	end
	else
		set @newactno=@cgp_actno


	if not exists(select * from glprimld where cgp_desc=@cgp_desc and company=@company and fy=@newfy)
		begin
			INSERT GLPRIMLD (COMPANY,FY,CGP_DESC,ASSET,ONAC,TDNO,FLAG,CGP_ACTNO,GRP,add1,add2,city,phone,deletable,gstin,PAN) VALUES 
			(@company,@newfy,@cgp_Desc,@asset,@onac,@tdno,@flag,@newactno,@grp,@add1,@add2,@city,@phone,@deletable,@gstin,@pan)
			if @@error <> 0 goto e_error
			update glprimld set nextyearac=@newactno,nextyearfy=@newfy where company=@company and fy=@fy and cgp_actno=@cgp_actno
			if @@error <> 0 goto e_error
		end 
	else
		begin
		set @msg =  'GL Exists ' + @cgp_desc
		select @newglacno=cgp_actno from glprimld where cgp_desc=@cgp_desc and company=@company and fy=@newfy
		update glprimld set nextyearac=@newglacno,nextyearfy=@newfy where cgp_desc=@cgp_desc and fy=@newfy
		--goto e_error
		end
end

return
e_error:
rollback transaction
set @msg = @msg + ' next year failed'
raiserror(@msg,16,-1)
return
GO
ALTER TABLE [dbo].[GLPRIMLD] ENABLE TRIGGER [glrpimisert]
GO
/****** Object:  Trigger [dbo].[updtbal]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.updtbal    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.updtbal    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.updtbal    Script Date: 04/08/2004 4:42:32 PM ******/
/****** Object:  Trigger dbo.updtbal    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE  TRIGGER [dbo].[updtbal] ON [dbo].[GLPRIMLD] 
/* with encryption */
FOR UPDATE 
AS
declare @company numeric
declare @fy numeric
declare @nextyearac numeric
declare @nextyearfy numeric
declare @grp numeric
declare @balance money
declare @prebalance money
declare @difference money
declare @txndt datetime
declare @oldgrp numeric
declare @actnum numeric
declare @flag numeric
declare @oldflag numeric
declare @oldbal money
declare @detached char(1)
declare @status char(1)
---if not update(balance) and not update(status) and not update(grp) return

select @txndt=ltxndt,@company=company,@fy=fy,@nextyearac=isnull(nextyearac,0),@nextyearfy=isnull(nextyearfy,0),@grp=grp,@balance=balance,@actnum=cgp_actno,@flag=flag,@status=status from inserted
select @oldgrp=grp,@prebalance=balance from deleted

--- This script is created on 22-Mar-2014 @ Kottaiyur (came to collect the Lachu Marriage invitation
if update(status)
if @nextyearac<> 0
update glprimld set status=@status where company=@company and fy=@nextyearfy and cgp_actno = @nextyearac

if update(balance) and not update(status) 
if @balance<>0 
update glprimld set status='' where company=@company and fy=@fy and cgp_actno = @actnum
--- Status update end


	select @detached=detached from finyear where fy=@fy
    if @detached='Y' return

---if update(OPGBAL)
IF UPDATE(balance) ----OR UPDATE(BALANCE)

begin
	if not exists(select * from finyear where fy=@nextyearfy) return
	set @difference=  @balance-@prebalance
	if @nextyearfy<> 0
	begin
		select @txndt=finyear.[date] from finyear where company=@company and fy=@nextyearfy

		if @grp<>7
			begin
--rollback transaction
--declare @retval varchar(100)
--set @retval = str(@difference)
--raiserror(@retval,16,-1)
	
			update glprimld set opgbal=@balance,balance=balance+@difference,ltxndt=@txndt where company=@company and fy=@nextyearfy and cgp_actno = @nextyearac
			end
		else
		begin
			declare @plbal money
			select @plbal=sum(balance) from glprimld where fy=@fy and grp=7
			select @oldbal=balance from glprimld where fy=@fy and cgp_actno=52
			update glprimld set opgbal=@plbal+@OLDBAL,ltxndt=@txndt  where company=@company and fy=@nextyearfy and cgp_actno = 52
			exec fillbal @txndt,@company,52,1
		end
	end
end --ends balance updating

if update(grp) 
begin
 if @nextyearac <>0
    if @oldgrp<>@grp 
       begin
       set @difference=  @balance-@prebalance
       select @txndt=finyear.[date] from finyear where company=@company and fy=@nextyearfy
       if @oldgrp<>7 and @grp=7
           begin
               update glprimld set grp=@grp,flag=@flag,opgbal=opgbal-@balance,balance=balance-@balance,ltxndt=@txndt where company=@company and fy=@nextyearfy and cgp_actno = @nextyearac      
               update glprimld set opgbal=opgbal+@balance ,balance=balance-@balance,ltxndt=@txndt where company=@company and fy=@nextyearfy and cgp_actno = 52
           end
      else if @oldgrp=7 and @grp<>7
           begin
               update glprimld set grp=@grp,flag=@flag,opgbal=opgbal+@balance,balance=balance+@balance,ltxndt=@txndt where company=@company and fy=@nextyearfy and cgp_actno = @nextyearac      
               update glprimld set opgbal=opgbal-@balance,balance=balance-@balance,ltxndt=@txndt where company=@company and fy=@nextyearfy and cgp_actno = 52
           end
         end
end
GO
ALTER TABLE [dbo].[GLPRIMLD] ENABLE TRIGGER [updtbal]
GO
/****** Object:  Trigger [dbo].[gppinsert]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO







CREATE TRIGGER [dbo].[gppinsert] ON [dbo].[GPASSPORT] 
/* with encryption */
FOR INSERT
AS
begin transaction
declare @cformno numeric
declare @ctrlno numeric
select @ctrlno=ctrlno from inserted
select @cformno=max(cformno) from gpassport
update gpassport set cformno=@cformno+1 where ctrlno=@ctrlno
commit transaction








GO
ALTER TABLE [dbo].[GPASSPORT] ENABLE TRIGGER [gppinsert]
GO
/****** Object:  Trigger [dbo].[gnewcidate]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  TRIGGER [dbo].[gnewcidate] ON [dbo].[GUEST] 
/* with encryption */
FOR INSERT  AS declare @cidate datetime declare @gno numeric select * into #gnewcid from inserted select @gno=gno,@cidate=ci_date from #gnewcid if abs(datediff(n,@cidate,getdate())) > 20 begin insert into acclog (narration,txntype,gno) values ('Check-In differs by more than 20 minutes-Current Date & Time ' + convert(varchar(20),getdate(),13) + ' Date & Time Recorded ' +   convert(varchar(20),@cidate,13),73,@gno) end update guest set gi=datediff(s,'1 jan 2000', getdate()) where gno=@gno
GO
ALTER TABLE [dbo].[GUEST] ENABLE TRIGGER [gnewcidate]
GO
/****** Object:  Trigger [dbo].[gst_expecteddep]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[gst_expecteddep] ON [dbo].[GUEST] 
/* with encryption */
FOR  UPDATE 
AS
if not update(expecteddep) return 
declare @expecteddep datetime
declare @expold datetime
declare @expnew datetime 
declare @eno numeric 
declare @rno numeric 
declare @gno numeric 
declare @name varchar(200) 
select @expold=isnull(expecteddep,'1 jan 2010')  from deleted 
select @expnew=isnull(expecteddep,'1 jan 2011') from inserted 
if datediff(n,@expold,@expnew)<>0 
begin 
select @gno=gno,@expecteddep=expecteddep, @eno=eno, @rno=rno,@name=name from inserted 
insert into acclog (narration,gno,txntype) values ('Expected Departure Date extended to ' + convert(varchar(20),@expecteddep) + ' from ' + convert(varchar(20),@expold) + ' Guest Name ' + @name + ' Entry No. ' + ltrim(str(@eno))  + ' Room No.' + ltrim(str(@rno)),@gno,203) 
end
GO
ALTER TABLE [dbo].[GUEST] ENABLE TRIGGER [gst_expecteddep]
GO
/****** Object:  Trigger [dbo].[guestcidate]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  TRIGGER [dbo].[guestcidate] ON [dbo].[GUEST] 
/* with encryption */
FOR  UPDATE
AS
declare @ocid datetime
declare @ncid datetime
declare @gno numeric
declare @ogrpname varchar(50)
declare @ngrpname varchar(50)
declare @rno numeric
declare @guest varchar(100)
if update(ci_date)
begin
select * into #gcid1 from deleted
select * into #gcid2 from inserted
select @gno=gno,@ocid=ci_date from #gcid1
select @gno=gno,@ncid=ci_date from #gcid2
if datediff ( n, @ocid,@ncid) <> 0
begin
select @rno=rno,@guest=name from guest where gno=@gno
insert into acclog(narration,txntype,gno) values ('Check-In Date Changed from ' + convert(varchar(20),@ocid,13) + ' to ' + convert(varchar(20),@ncid,13) +  ' Room No.' + ltrim(str(@rno)) + ' Guest Name : ' +  @guest  ,72,@gno)
 End
End
if update(grpname)
begin
select * into #gcid01 from deleted
select * into #gcid02 from inserted
select @gno=gno,@ogrpname=grpname from #gcid01
select @gno=gno,@ngrpname=grpname from #gcid02
if @ogrpname<>@ngrpname
insert into acclog(narration,txntype,gno) values ('Sys.No.'  + ltrim(str(@gno))  + '-Group ID Changed from ' + @ogrpname + ' to ' + @ngrpname ,74,@gno)
End
GO
ALTER TABLE [dbo].[GUEST] ENABLE TRIGGER [guestcidate]
GO
/****** Object:  Trigger [dbo].[updatecodate]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[updatecodate] ON [dbo].[GUEST] 
/* with encryption */
FOR UPDATE
AS
if not update(co_date) return
declare @co_date datetime
declare @ci_date datetime
declare @gno numeric
select @gno=gno,@co_date=co_date,@ci_date=ci_date from inserted
if datediff(s,@ci_date,@co_date) < 0 begin
insert into acclog (narration) values ('sys.no-' + ltrim(str(@gno) ))
print 'This operation changes the checkout date less than the checkin date...so operation aborted - '
rollback transaction
return
end
--print 'periya'
GO
ALTER TABLE [dbo].[GUEST] ENABLE TRIGGER [updatecodate]
GO
/****** Object:  Trigger [dbo].[smsdelete]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO


CREATE TRIGGER  [dbo].[smsdelete] ON [dbo].[infosent] 
FOR INSERT, UPDATE
AS
delete from infosent where datediff(d,txndt,getdate()) > 20


GO
ALTER TABLE [dbo].[infosent] ENABLE TRIGGER [smsdelete]
GO
/****** Object:  Trigger [dbo].[khupdate]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO













/****** Object:  Trigger dbo.khupdate    Script Date: 21/09/2006 12:14:20 PM ******/
/****** Object:  Trigger dbo.khupdate    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.khupdate    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.khupdate    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[khupdate] ON [dbo].[KITHIST] 
FOR  UPDATE
AS
declare @name varchar(50)
if update(name) return
select  @name=name from kitprime where icode=(select icode from kitmast where code=(select icode from inserted))













GO
ALTER TABLE [dbo].[KITHIST] ENABLE TRIGGER [khupdate]
GO
/****** Object:  Trigger [dbo].[KITHISTDEL]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/****** Object:  Trigger dbo.KITHISTDEL    Script Date: 21/09/2006 12:14:20 PM ******/
/****** Object:  Trigger dbo.KITHISTDEL    Script Date: 4/14/2005 10:21:49 PM ******/
/****** Object:  Trigger dbo.KITHISTDEL    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.KITHISTDEL    Script Date: 15/02/2004 6:23:01 PM ******/
/****** Object:  Trigger dbo.KITHISTDEL    Script Date: 1/20/03 11:51:49 AM ******/
CREATE TRIGGER [dbo].[KITHISTDEL] ON [dbo].[KITHIST] 
/* with encryption */
FOR DELETE 
AS
--BEGIN TRANSACTION
DECLARE @M_CODE INT
DECLARE @M_OUTLET INT
DECLARE @M_QTY INT
DECLARE @M_CODE1 INT
DECLARE @M_QTY1 INT
DECLARE @ERR INT
declare @curdate datetime
declare @opgbal float
declare @outlet int
declare @txndt datetime
declare @bno numeric
set @ERR = 0
SELECT @bno=bno,@M_CODE=CODE,@M_QTY=QTY FROM DELETED
select @outlet=outlet from kitmast where code =@m_code
--select @curdate=curdate from outlet where code=@outlet
select @txndt=txndt from kithist1 where bno=@bno
DECLARE KM212 CURSOR FOR SELECT scode,qty FROM KITMAST2 WHERE icode=@M_CODE
OPEN KM212
FETCH NEXT FROM KM212 INTO @M_CODE1,@M_QTY1
WHILE @@FETCH_STATUS=0 
  BEGIN 
  if not exists(select * from mizastock where otype=1 and outlet=@outlet and icode=@m_code1) insert into mizastock(otype,outlet,icode,stock,ltxndt) values (1,@outlet,@m_code1,0,getdate())
  update mizastock set stock=stock+(@m_qty1*@m_qty) where icode=@m_code1 and outlet=@outlet and otype=1

  declare @msicode numeric
  select @msicode=ctrlno from mizastock  where icode=@m_code1 and outlet=@outlet and otype=1

  if not exists (select * from mizstock where OUTLET=@OUTLET AND  icode=@msicode and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)) insert into mizstock (icode,txndt,OUTLET) values (@msicode,@txndt,@OUTLET)
  update mizstock set consumed=consumed - (@m_qty1*@m_qty) where  outlet=@outlet and icode=@msicode and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)

  IF @@ERROR <> 0 GOTO E_ERROR
  FETCH NEXT FROM KM212 INTO @M_CODE1,@M_QTY1
END
GOTO E_CLOSE
E_CLOSE:
CLOSE KM212
DEALLOCATE KM212
IF @ERR<>0
--COMMIT TRANSACTION
--ELSE
BEGIN
ROLLBACK TRANSACTION
RAISERROR('KITMAST2 DELETION FAILED',16,-1)
END
RETURN
E_ERROR:
SELECT @ERR = 1
GOTO E_CLOSE
GO
ALTER TABLE [dbo].[KITHIST] ENABLE TRIGGER [KITHISTDEL]
GO
/****** Object:  Trigger [dbo].[KITINSERT]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/****** Object:  Trigger dbo.KITINSERT    Script Date: 21/09/2006 12:14:20 PM ******/
/****** Object:  Trigger dbo.KITINSERT    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.KITINSERT    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.KITINSERT    Script Date: 15/02/2004 6:23:01 PM ******/
/****** Object:  Trigger dbo.KITINSERT    Script Date: 1/20/03 11:51:49 AM ******/
/****** Object:  Trigger dbo.KITINSERT    Script Date: 10/14/02 1:17:58 PM ******/
/*****  Modified on 27-09-2012 - withdrawn OLSTORE table Items Linking etc....... created new table and new logic ***/
CREATE TRIGGER [dbo].[KITINSERT]  ON [dbo].[KITHIST] 
/* with encryption */
FOR INSERT
AS
--if update(name) return
DECLARE @M_CODE INT
DECLARE @M_OUTLET INT
DECLARE @M_QTY float
DECLARE @M_CODE1 INT
DECLARE @M_QTY1 float 
DECLARE @ERR INT
declare @outlet int
declare @curdate datetime
declare @opgbal float
declare @name varchar(50)
declare @ctrlno numeric
declare @txndt datetime
declare @bno numeric
declare @msicode numeric

set @ERR = 0
SELECT @bno=bno,@CTRLNO=CTRLNO,@M_CODE=CODE,@M_QTY=QTY FROM INSERTED
select @name=name from kitprime  where icode=(select icode from kitmast where code=@m_code)
update kithist set name=@name where ctrlno=@ctrlno
select @txndt=txndt from kithist1 where bno=@bno
--PRINT @NAME
--PRINT @M_CODE
select @outlet=outlet from kitmast_v where code =@m_code
select @curdate=curdate from outlet where code=@outlet
DECLARE KM2 CURSOR FOR SELECT scode,qty FROM KITMAST2 WHERE icode=@M_CODE
OPEN KM2
FETCH NEXT FROM KM2 INTO @M_CODE1,@M_QTY1
WHILE @@FETCH_STATUS=0 
BEGIN 
	if not exists(select * from mizastock where otype=1 and outlet=@outlet and icode=@m_code1) insert into mizastock(otype,outlet,icode,stock,ltxndt) values (1,@outlet,@m_code1,0,getdate())
	update mizastock set stock=stock-(@m_qty1*@m_qty),ltxndt=@curdate where otype=1 and outlet=@outlet and icode=@m_code1
	IF @@ERROR <> 0 GOTO E_ERROR
	
	select @msicode=ctrlno from mizastock  where icode=@m_code1 and outlet=@outlet and otype=1
	
	if not exists 
	(select * from mizstock where OUTLET=@OUTLET AND icode=@MSIcode and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)) 
	insert into mizstock (icode,txndt,OUTLET) values (@msicode,@txndt,@OUTLET)
	update mizstock set consumed=consumed+(@m_qty1*@m_qty) where outlet=@outlet and  icode=@msicode and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)
	
	FETCH NEXT FROM KM2 INTO @M_CODE1,@M_QTY1
END
GOTO E_CLOSE
E_CLOSE:
CLOSE KM2
DEALLOCATE KM2
IF @ERR<>0
BEGIN
ROLLBACK TRANSACTION
RAISERROR('KITMAST2 UPDATION FAILED',16,-1)
END
RETURN
E_ERROR:
SELECT @ERR = 1
GOTO E_CLOSE
GO
ALTER TABLE [dbo].[KITHIST] ENABLE TRIGGER [KITINSERT]
GO
/****** Object:  Trigger [dbo].[CheckGuest]    Script Date: 07/04/2026 20:30:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[CheckGuest]  ON [dbo].[KITHIST1] 
/* with encryption */
FOR INSERT, UPDATE AS 
IF UPDATE(GSTIN) RETURN
IF UPDATE(EINVOICE) RETURN
declare @gno numeric declare @billdt datetime declare @cidate datetime declare @codate datetime declare @mop int select @gno=gno,@billdt=txndt,@mop=pmode from inserted select @cidate=ci_date,@codate=isnull(co_date,getdate()) from guest where gno=@gno if isnull(@gno,0) = 0  return if isnull(@mop,0)<>2 return if     datediff(d,'1/1/90',@billdt) < (datediff(d,'1/1/90',@cidate)+1) and  datediff(d,'1/1/90',@billdt) > datediff(d,'1/1/90',@codate)  begin rollback transaction raiserror('Error in the Bill Date and check-in checkout date - message from kithist1',16,-1) Return End
declare @err varchar(200)
if not exists(select * from hmsfolder where version_no = (select top 1 version_no from comuser where [host_id]=host_id()) ) 
	begin 
	set @err='You are using a wrong version of the Softwre Installed...Please update the version (T1) and try'
	raiserror(@err,16,-1)
	return
	end
GO
ALTER TABLE [dbo].[KITHIST1] ENABLE TRIGGER [CheckGuest]
GO
/****** Object:  Trigger [dbo].[insertkh1]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[insertkh1] ON [dbo].[KITHIST1] /* with encryption */ FOR INSERT AS IF UPDATE(GSTIN) RETURN declare @usernm varchar(10) declare @ctrlno numeric select @usernm=usernm  from comuser where [host_id]=host_id() select @ctrlno=bno  from inserted if isnull(@usernm,'')<>'' update kithist1 set [usernm]=@usernm where bno=@ctrlno
GO
ALTER TABLE [dbo].[KITHIST1] ENABLE TRIGGER [insertkh1]
GO
/****** Object:  Trigger [dbo].[kitprimeins]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[kitprimeins]
   ON  [dbo].[kitprime]
   AFTER INSERT
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    declare @icode numeric 
declare @chr char(1)  
declare @cnt numeric  
declare @shcode numeric 
select * into #dummykp from inserted select @shcode=shcode,@icode=icode,@chr=substring(name,1,1) from #dummykp  
if isnull(@shcode,0) = 0   
begin
   select @cnt=max(shcode) from kitprime where substring(name,1,1)=@chr  
set @cnt=@cnt+1 	 
update kitprime set shcode=@cnt where icode=@icode  
End


END
GO
ALTER TABLE [dbo].[kitprime] ENABLE TRIGGER [kitprimeins]
GO
/****** Object:  Trigger [dbo].[mizaminus]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[mizaminus]
   ON  [dbo].[MIZASTOCK]
   AFTER UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
if exists(select *  from inserted where  stock< 0 ) 
begin
print 'Miza Stock going Minus....transaction aborted'
rollback transaction
raiserror('Miza Stock going Minus....transaction aborted',16,1)
return
end
END
GO
ALTER TABLE [dbo].[MIZASTOCK] ENABLE TRIGGER [mizaminus]
GO
/****** Object:  Trigger [dbo].[MizUpdate]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE   TRIGGER [dbo].[MizUpdate] ON [dbo].[MizStock] 
/* with encryption */
 FOR UPDATE,insert
 AS 
 declare @ctrlno numeric 
 declare @icode numeric 
 declare @closing float 
 declare @txndt datetime 
 declare @dt1 datetime 
 declare @dt2 datetime 
declare @err varchar(2000)
declare @outlet int
 select @ctrlno=ctrlno,@outlet=outlet,@icode=icode,@txndt=txndt,@closing=closing  from inserted 
 if update(received) or update(consumed) 
 begin 
 --update mizstock set opening=isnull((select b.closing from mizstock b where b.icode=@icode and datediff(d,'1/1/90',b.txndt) = datediff(d,'1/1/90',dateadd(d,-1,txndt))),0)
 --,closing=isnull((select b.closing from mizstock b  where b.icode=@icode and datediff(d,'1/1/90',b.txndt) = datediff(d,'1/1/90', dateadd(d,-1,txndt))),0)+received-consumed where ctrlno=@ctrlno 
set @err =  convert(varchar(20),@txndt) + str(@ctrlno) + str(@icode)
--raiserror(@err,16,-1)
 update mizstock set opening=isnull((select b.closing from mizstock b where b.outlet=@outlet and b.icode=@icode and datediff(d,'1/1/90',b.txndt) = datediff(d,'1/1/90',dateadd(d,-1,mizstock.txndt))),0) 
 where ctrlno=@ctrlno 

 update mizstock set closing=opening+received-consumed where ctrlno=@ctrlno

 select @closing=closing from mizstock where ctrlno=@ctrlno 
 set @dt1 = @txndt 
 set @dt2 = dateadd(d,1,@dt1) 
 while @dt2 < @txndt 
 begin 
 if not exists (select * from mizstock where icode=@icode and datediff(d,'1/1/90',txndt) =
  datediff(d,'1/1/90', @dt2)) 
 insert into mizstock (txndt,icode,opening,closing) 
    Values 
    (@dt2,@icode,@closing,@closing) 
     set @dt2 = dateadd(d,1,@dt2)
 End 
 declare c11 cursor for select ctrlno,txndt  from mizstock where icode=@icode and 
 datediff(d,'1/1/90',txndt) > datediff(d,'1/1/90',@txndt) order by datediff(d,'1/1/90',txndt) 
 open c11 
 fetch next from c11 into @ctrlno,@txndt 
 while @@fetch_status=0 
 begin 
 update mizstock  set opening=@closing,closing=@closing+received-consumed where ctrlno=@ctrlno 
 select @closing=closing from mizstock where ctrlno=@ctrlno 
 fetch next from c11 into @ctrlno,@txndt 
 End 
 Close c11 
 deallocate c11 
 End

GO
ALTER TABLE [dbo].[MizStock] ENABLE TRIGGER [MizUpdate]
GO
/****** Object:  Trigger [dbo].[MBInsert]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  TRIGGER [dbo].[MBInsert] ON [dbo].[NECMiniBar]   
FOR INSERT   
AS  
DECLARE @MESSAGE VARCHAR(30)  
declare @extn varchar(20)  
declare @outlet numeric  
declare @items int   
DECLARE @STTABLENO NUMERIC
select @items=items,@outlet=outlet from necmboutlet  
select @STTABLENO=sttableno from outlet where CODE=@outlet
select * into #inserted22 from inserted  
select @message=message,@extn=extn from #inserted22  
set @message=ltrim(rtrim(@message))  
if len(@Message) <> 2  and len(@message) <> 9  and len(@message) <> 6  
	begin  
	update necminibar set acctd ='A' where ctrlno = (select ctrlno from #inserted22)  
	return  
	end  
else
	begin  
	if len(@message)=2  
		begin  
		if @message='00' update room set status=' ' where cast(extn as int)  =  cast(@extn as int)  
		if @message='01' update room set status='A' where cast(extn  as int)  =  cast(@extn  as int)   
		if @message='02' update room set status='B' where cast(extn as int)  =  cast(@extn as int)  
		if @message='03' update room set status='C' where cast(extn as int)  =  cast(@extn as int)  
		if @message='04' update room set status='D' where cast(extn as int)  =  cast(@extn as int)  
		if @message='05' update room set status='E' where cast(extn as int)  =  cast(@extn as int)  
		if @message='06' update room set status='F' where cast(extn as int)  =  cast(@extn as int)  
		if @message='07' update room set status='G' where cast(extn as int)  =  cast(@extn as int) and gno <> 0  
		if @message='11' update room set ci='0' where cast(extn as int)  =  cast(@extn as int)  
		if @message='12' update room set ci='1' where cast(extn as int)  =  cast(@extn as int)  
		if @message='13' update room set ci='5' where cast(extn as int)  = cast(@extn as int)  
		if @message='14' update room set ci='6' where cast(extn as int)  =  cast(@extn as int)  
		update necminibar set acctd ='Y' where ctrlno = (select ctrlno from #inserted22)  
		return  
		end  
	if substring(@message,1,2)='89'  
		begin  
		update room set wu='Y',time=substring(@message,3,4) where cast(extn as int)  =  cast(@extn as int)  
		update necminibar set acctd ='Y' where ctrlno = (select ctrlno from #inserted22)  
		return  
		end  
	if substring(@message,1,2)='99'  
	begin  
		declare @rno numeric  
		declare @gno numeric  
		declare @tax money  

		if not exists(select * from comuser where [host_id]=host_id() and inet=0)
			insert into  comuser (usernm,version_no,userno,control,computer,[host_id]) values ('SYS',(select top 1 version_no from HmsFolder),0,'Y','SYS',host_id())
		else
			update comuser set control='Y' where [host_id]=host_id()

		
		select @rno=rno,@gno=gno from room where cast(extn as int)  =  cast(@extn as int)  
		if  @gno=0  
			update necminibar set acctd ='N' where ctrlno = (select ctrlno from #inserted22)  
		else  
																																									begin                 
			declare @str1 char(7)  
			set @str1 = substring(@message,3,7)  
			select * into #dummykit from dummykit  
			declare @x int  
			declare @ecode int  
			declare @icode numeric  
			declare @rate money  
			declare @qty int  
			DECLARE @GST money
			declare @cess money
			declare @cess2 money
			declare @strate money  
			set @x=1  
			while @x <= @items  
			begin                                 
				set @qty=cast(substring(@message,@x+2,1) as int)                                  
				if @qty<>0  
				begin  
					select @icode=icode from necmbmaster where ecode=@x  
					select @rate=rate,@strate=tax from kitmast_v where code=@icode  
					if @strate <> 0   
						Set @tax = (@rate*@qty)/@strate  
					else  
						set @tax=0  
                                          
					declare @amt money
					declare @bno numeric  
					declare @txndt datetime  
					set  @txndt = getdate()  
					set @amt=@rate*@qty
					exec GetServiceTaxR @amt,@txndt,@gst out,@cess out,@outlet,@sttableno,@cess2 out
					insert into #dummykit values (@icode,@qty,@rate,@strate,@rate*@qty, @tax ,0,1,2,@rno,'',@GST,@cess,0,@cess2)  
				end  
				set @x=@x+1  
			end  
			exec pbpkb1u #dummykit,@outlet,@txndt,2,0,@gno,'','','',@rno,1,0,1,0,0,0,0,@bno out,'EPX',1,0,0 
			insert into necmbrprint (bno,status) values (@bno,'') 
			update necminibar set acctd ='Y' where ctrlno =(select ctrlno from #inserted22)  
		end  --here end
	end  
else  
	update necminibar set acctd ='B' where ctrlno = (select ctrlno from #inserted22)  
end
GO
ALTER TABLE [dbo].[NECMiniBar] ENABLE TRIGGER [MBInsert]
GO
/****** Object:  Trigger [dbo].[check_date]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[check_date] ON [dbo].[PHONE] /* with encryption */
 FOR INSERT, UPDATE, DELETE AS declare @gno numeric declare @billdt datetime declare @cidate datetime declare @codate datetime declare @mop int select @gno=gno,@billdt=txndt,@mop=pmode  from inserted select @cidate=ci_date,@codate=isnull(co_date,getdate()) from guest where gno=@gno if isnull(@gno,0) = 0 return if isnull(@mop,0) <> 2 return if      datediff(d,'1/1/90',@billdt) < (datediff(d,'1/1/90',@cidate)+1) and  datediff(d,'1/1/90',@billdt) > datediff(d,'1/1/90',@codate) begin rollback transaction raiserror('Error in the Bill Date and check-in checkout date - message from Phone',16,-1) Return End
GO
ALTER TABLE [dbo].[PHONE] ENABLE TRIGGER [check_date]
GO
/****** Object:  Trigger [dbo].[chkposition]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[chkposition]
   ON  [dbo].[PHOUTLET]
   AFTER INSERT,UPDATE
AS 
BEGIN
if exists (select billposition1 from inserted where billposition1 > 10) 
begin
print 'exceeds 10 outlets...not allowed'
rollback 
return
end
if exists (select billposition2 from inserted where billposition2 > 10) 
begin
print 'exceeds 10 outlets...not allowed'
rollback 
return
end

END
GO
ALTER TABLE [dbo].[PHOUTLET] ENABLE TRIGGER [chkposition]
GO
/****** Object:  Trigger [dbo].[PHOUTLETINS]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.PHOUTLETINS    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.PHOUTLETINS    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.PHOUTLETINS    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.PHOUTLETINS    Script Date: 15/02/2004 6:23:01 PM ******/
/****** Object:  Trigger dbo.PHOUTLETINS    Script Date: 4/30/2003 4:59:34 PM ******/
/****** Object:  Trigger dbo.PHOUTLETINS    Script Date: 1/20/03 11:51:49 AM ******/
CREATE TRIGGER [dbo].[PHOUTLETINS] ON [dbo].[PHOUTLET] 
/* with encryption */
FOR INSERT
AS
declare @company int
declare @outlet int
declare @selfdeal char(1)
declare @pos int
select @outlet=outlet,@selfdeal=selfdeal,@COMPANY=COMPANY from inserted
if @company=1 begin
    if not exists(select * from billposition where company=1 and servicetype=2 and outlet=@outlet)
BEGIN
		select @pos=position from billposition where company=1 and position=(select max(position) from billposition where company=1 and outlet<>0)
		set @pos=@pos+1
		update billposition set servicetype=2,outlet=@outlet where company=1 and position=@pos
END
    else
	    select @pos=position from billposition where servicetype=2 and outlet=@outlet and company=1

	update phoutlet  set billposition1=@pos where outlet=@outlet
end
else  /* if not company=1*/
if @selfdeal='N'
begin
    if not exists(select * from billposition where company=1 and servicetype=2 and outlet=@outlet)
		BEGIN
		select @pos=position from billposition where company=1 and position=(select max(position) from billposition where company=1 and outlet<>0)
		set @pos=@pos+1
		update billposition set servicetype=2,outlet=@outlet where company=1 and position=@pos
		END
    else
	    select @pos=position from billposition  where servicetype=2 and outlet=@outlet and company=1
	update phoutlet  set billposition1=@pos where outlet=@outlet
end
else
begin

    if not exists(select * from billposition where company=@COMPANY and servicetype=2 and outlet=@outlet)
BEGIN
		select @pos=position from billposition where company=@COMPANY and	position=(select max(position) from billposition where company=1 and outlet<>0)
		set @pos=@pos+1
		update billposition set servicetype=2,outlet=@outlet where company=@COMPANY and position=@pos
END
    else
	    select @pos=position  from billposition where servicetype=2 and outlet=@outlet and company=@COMPANY
	update phoutlet  set billposition2=@pos where outlet=@outlet
end
GO
ALTER TABLE [dbo].[PHOUTLET] ENABLE TRIGGER [PHOUTLETINS]
GO
/****** Object:  Trigger [dbo].[updateposition]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.updateposition    Script Date: 21/09/2006 12:14:21 PM ******/
CREATE TRIGGER [dbo].[updateposition] ON [dbo].[RESTCO] 
/* with encryption */
FOR INSERT
AS
declare @co numeric
declare @channel numeric
declare @pos int
select @co=company , @channel=channel  from INSERTED
if @co=1
	begin
	select @pos=(select max(position) from billposition where company=1 AND outlet<>0)
	set @pos=isnull(@pos,0) + 1
	if @pos > 10 begin print 'The Bill position goes beyond 10... No more inclusions allowed' rollback RAISERROR('ERROR',16,-1) return end
	update billposition set outlet=@channel,servicetype=1 where company=1 and position=@pos
	update restco set billposition1=@pos where channel=@channel
	end
else
	begin
	select @pos=(select max(position) from billposition where company=@co AND outlet<>0)
	set @pos=isnull(@pos,0) + 1
	if @pos > 10 begin print '2-The Bill position goes beyond 10... No more inclusions allowed' +str(@pos)  rollback RAISERROR('ERROR',16,-1) return end
	update billposition set outlet=@channel,servicetype=1 where company=1 and position=@pos
	update restco set billposition1=@pos where channel=@channel
	select @pos=(select max(position) from billposition where company=@co and outlet<>0)
	set @pos=isnull(@pos,0) + 1
	if @pos > 10 begin print '3-The Bill position goes beyond 10... No more inclusions allowed' + str(@pos)  rollback RAISERROR('ERROR',16,-1) return end
	update billposition set outlet=@channel,servicetype=1 where company=@co  and position=@pos
	update restco set billposition2=@pos where channel=@channel
	end











GO
ALTER TABLE [dbo].[RESTCO] ENABLE TRIGGER [updateposition]
GO
/****** Object:  Trigger [dbo].[CI]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  TRIGGER [dbo].[CI]  ON [dbo].[ROOM] FOR  UPDATE  
AS   
IF NOT UPDATE(GNO)  RETURN  
declare @ogno numeric 
DECLARE @RNO NUMERIC  
DECLARE @GNO NUMERIC 
select @ogno=gno from deleted 
SELECT @GNO=GNO, @RNO=RNO FROM INSERTED  
IF @GNO=0 
begin	 
	UPDATE  ROOM SET CI='N' ,ol24gno=@ogno,cistatus='N',wutimeset='',wustatus=' ',OL24CI='N',cismssent=0 WHERE RNO=@RNO 
	if @ogno<>0 and @gno=0 insert into ol24room (rno,gno,[status]) values (@rno,@ogno,' ') 
end  
Else 
	UPDATE ROOM SET CI='Y',sl='Y',OL24CI='Y',OL24GNO=0  WHERE RNO=@RNO Return
GO
ALTER TABLE [dbo].[ROOM] ENABLE TRIGGER [CI]
GO
/****** Object:  Trigger [dbo].[statusdate]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.statusdate    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.statusdate    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.statusdate    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.statusdate    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[statusdate] ON [dbo].[ROOM] 
/* with encryption */
FOR UPDATE
AS
declare @oldstatus  char(1)
declare @status char(1)
declare @rno int
if update(statusdate) return

if update(status)
   begin
   select @RNO=RNO,@status=status from inserted
   select @oldstatus=status from deleted
   if @status<>' '
      if @oldstatus=' '
          update room set statusdate=getdate(),sendstatus='Y'  WHERE RNO=@RNO
  if @status=' '
      if @oldstatus<>' '
          update room set statusdate=null,SENDSTATUS=' ' where rno=@rno 
   end
 if @status='A' update room set hksms=' ' where rno=@rno
GO
ALTER TABLE [dbo].[ROOM] ENABLE TRIGGER [statusdate]
GO
/****** Object:  Trigger [dbo].[sptbrsUDI]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[sptbrsUDI]   ON  [dbo].[sptbrs]  
 AFTER INSERT,DELETE,UPDATE AS BEGIN		
 SET NOCOUNT ON; 
 declare @host_name varchar(100) 
 select @host_name=computer from comuser where [host_id]=host_id() 
 declare @net char(1) 
 declare @err varchar(200) 
 select @net=net from inserted 
 if @net='N' 
 if not exists(select * from hmsfolder where version_no = (select top 1 version_no from comuser where computer=@host_name AND HOST_ID()=[HOST_ID]) )  	begin  	set @err=@host_name + 'You are using a wrong version of the Softwre Installed...Please update the version (T2) and try' 	raiserror(@err,16,-1) return 	end END
GO
ALTER TABLE [dbo].[sptbrs] ENABLE TRIGGER [sptbrsUDI]
GO
/****** Object:  Trigger [dbo].[updatereservation]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.updatereservation    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.updatereservation    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.updatereservation    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.updatereservation    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[updatereservation] ON [dbo].[sptbrs] 
/* with encryption */
FOR UPDATE
AS
declare @str1 varchar(1000)
if not update(advanceadjd)   return
declare @advr money
declare @adva money
declare @advrf money
select @advr=advancerecd,@adva=advanceadjd ,@advrf=refundamount from inserted
set @str1 = 'Not enough amount for advance adjustment' + char(13) + 'Adjusted Already ' +  convert(varchar(20),@adva)  + ' Refunded (including this) ' + convert(varchar(20),@advrf) + char(13) + 'Updation aborted'
--if @advr < @adva BEGIN  ROLLBACK raiserror ( @str1+ 'ERROR IN ADVANCE ADJUSTMENT',16,-1 ) END
if @advr < (@adva+@advrf) BEGIN  ROLLBACK raiserror ( @str1,16,-1 ) END
return
GO
ALTER TABLE [dbo].[sptbrs] ENABLE TRIGGER [updatereservation]
GO
/****** Object:  Trigger [dbo].[sptbrs1_del]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.sptbrs1_del    Script Date: 21/09/2006 12:14:21 PM ******/
CREATE TRIGGER [dbo].[sptbrs1_del] ON [dbo].[sptbrs1] 
/* with encryption */
FOR  delete
AS
declare @myc numeric
select @myc=myctrlno from deleted
delete from sptbrooms where myctrlno=@myc
GO
ALTER TABLE [dbo].[sptbrs1] ENABLE TRIGGER [sptbrs1_del]
GO
/****** Object:  Trigger [dbo].[sptbrs1_update_wingno]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/****** Object:  Trigger dbo.sptbrs1_update_wingno    Script Date: 21/09/2006 12:14:21 PM ******/
CREATE TRIGGER [dbo].[sptbrs1_update_wingno] ON [dbo].[sptbrs1] 
/* with encryption */
FOR INSERT, UPDATE
AS
if update(roomtype) 
update sptbrs1  set wingno=(select wingno from wings where description=(select roomtype from inserted)) 
  where myctrlno=(select myctrlno from inserted)

GO
ALTER TABLE [dbo].[sptbrs1] ENABLE TRIGGER [sptbrs1_update_wingno]
GO
/****** Object:  Trigger [dbo].[sptbrs1invalidrooms]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[sptbrs1invalidrooms]  ON [dbo].[sptbrs1] 
/* with encryption */
FOR UPDATE
AS
declare @tr int
declare @cr int
declare @ar int
select @tr=noofrooms,@cr=cancelledrooms,@ar=arrivedrooms from inserted
if @ar+@cr > @tr 
begin
print 'Incorrect number of rooms'
rollback 
end
return
GO
ALTER TABLE [dbo].[sptbrs1] ENABLE TRIGGER [sptbrs1invalidrooms]
GO
/****** Object:  Trigger [dbo].[StockPosting]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[StockPosting]  
   ON  [dbo].[storebills] 
/* with encryption */

   AFTER DELETE,UPDATE 
AS  
BEGIN 
  SET NOCOUNT ON; 
    declare @txndt datetime 
    declare @freezedate datetime 
   declare @storeno numeric 
   select @txndt=txndt,@storeno=storeno from deleted 
  select @freezedate=freeze from store where storeno=@storeno 
    if datediff(d,@freezedate,@txndt) <= 0  
    begin 
      rollback transaction 
       raiserror('The Transaction Period is freezed',16,-1) 
       return 
 end 
END 
GO
ALTER TABLE [dbo].[storebills] ENABLE TRIGGER [StockPosting]
GO
/****** Object:  Trigger [dbo].[forltxndtdelete]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[forltxndtdelete]
   ON  [dbo].[storehist]
   AFTER delete
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @txndt datetime
	declare @ltxndt datetime
	declare @ctrlno numeric
	select @txndt=txndt,@ctrlno=ctrlno from deleted 
	select @ltxndt=ltxndt from storeitems where ctrlno=@ctrlno
if datediff(d,@ltxndt,@txndt) < 0
update storeitems set ltxndt=@txndt where ctrlno=@ctrlno
END
GO
ALTER TABLE [dbo].[storehist] ENABLE TRIGGER [forltxndtdelete]
GO
/****** Object:  Trigger [dbo].[forltxndtupdate]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[forltxndtupdate]
   ON  [dbo].[storehist]
   AFTER UPDATE
AS 
BEGIN
if exists(select * from inserted where forupdt='Y') return
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @txndt datetime
	declare @ltxndt datetime
	declare @ntxndt datetime
	declare @ctrlno numeric
	select @txndt=txndt,@ctrlno=ctrlno from deleted 
	select @ntxndt=txndt from inserted 
	select @ltxndt=ltxndt from storeitems where ctrlno=@ctrlno
	if datediff(d,@ltxndt,@txndt) < 0
	update storeitems set ltxndt=@txndt where ctrlno=@ctrlno
	if datediff(d,@ltxndt,@ntxndt) < 0
	update storeitems set ltxndt=@txndt where ctrlno=@ctrlno

    -- Insert statements for trigger here

END
GO
ALTER TABLE [dbo].[storehist] ENABLE TRIGGER [forltxndtupdate]
GO
/****** Object:  Trigger [dbo].[shminusbalance]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[shminusbalance]
   ON  [dbo].[storehist]
   AFTER update
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
declare @cnt numeric    --dummy program
    -- Insert statements for trigger here

END
GO
ALTER TABLE [dbo].[storehist] ENABLE TRIGGER [shminusbalance]
GO
/****** Object:  Trigger [dbo].[spstdelete]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.spstdelete    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.spstdelete    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.spstdelete    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.spstdelete    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[spstdelete] ON [dbo].[storehist] 
/* with encryption */
FOR  DELETE 
AS
declare @ctrlno int
declare @qty float
declare @storeno int
declare @oltype int
declare @outlet int
declare @rate money
declare @amttyp char(1)
declare @olctrlno int
declare @bctrlno int
declare @ltxndt datetime
declare @txndt datetime
declare @forupdt char(1)
select @forupdt=forupdt from deleted  
--print @forupdt
--rollback transaction  raiserror('p.e.r.i.y.a',16,-1) return 
if exists(select * from deleted where forupdt='Y') return
--select  @ctrlno=ctrlno,@qty=qty,@amttyp=amttyp,@rate=rate,@oltype=oltype,@outlet=outlet from deleted
if not exists(select * from deleted) return
select  @bctrlno=bctrlno,@ctrlno=ctrlno,@qty=qty,@amttyp=amttyp,@rate=rate,@forupdt=forupdt from deleted

--select @oltype=grno,@outlet=supplier,@txndt=@txndt from storebills where bctrlno=@bctrlno
select @txndt=txndt,@oltype=issuedtotype,@outlet=issuedtocode from storebills where bctrlno=@bctrlno

declare @fy numeric
declare @company int
declare @sql varchar(2000)

select @storeno=storeno from storeitems where ctrlno=@ctrlno
select @company=company from store where storeno=@storeno
 
exec  getfy @txndt,@company,@fy out

if @amttyp='C'
	begin
		update storeitems set stock=stock-@qty,forupdt=@forupdt where ctrlno=@ctrlno
		if @@error<>0 rollback transaction
		update storeprimary set balance=balance - @qty where @fy=fy and ictrlno=@ctrlno
		if @@error<>0 rollback transaction
	end
else
begin
	update storeitems set stock=stock+@qty where ctrlno=@ctrlno
	if @@error<>0 rollback transaction
	update storeprimary set balance=balance + @qty where @fy=fy and ictrlno=@ctrlno
	if @@error<>0 rollback transaction
	if @oltype=1
	--print str(@ctrlno) rollback transaction  raiserror('p.e.r.i.y.a',16,-1) return 
	if exists (select * from kitmast2 where scode=@ctrlno)
	begin
	--print 'periya' rollback transaction  raiserror('p e r i y a',16,-1) return 
		if not exists (select * from mizastock where icode=@ctrlno and otype=@oltype and outlet=@outlet) insert into mizastock (otype,outlet,icode,stock,ltxndt) values (@oltype,@outlet,@ctrlno,0,getdate())
		update mizastock set stock=stock-@qty,ltxndt=@txndt where otype=@oltype and outlet=@outlet and icode=@ctrlno
		declare @msicode numeric
    	select @msicode=ctrlno from mizastock  where icode=@ctrlno and outlet=@outlet and otype=@oltype
		if not exists(select * from mizstock where outlet=@outlet and icode=@msicode and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) )
		insert into mizstock (icode,txndt,outlet) values (@msicode,@txndt,@outlet)
		update mizstock set received=isnull(received,0)-@qty where outlet=@outlet and  icode=@msicode and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
		if @@error<>0 rollback transaction
	end
end

select @ltxndt=txndt from storebills where bctrlno=@bctrlno
GO
ALTER TABLE [dbo].[storehist] ENABLE TRIGGER [spstdelete]
GO
/****** Object:  Trigger [dbo].[spstupdate]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.spstupdate    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.spstupdate    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.spstupdate    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.spstupdate    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[spstupdate] ON [dbo].[storehist] 
/* with encryption */
FOR UPDATE 
AS
IF UPDATE(TXNDT) RETURN
if update(balance) return
if update(forupdt) return
if update(qty1) return
declare @ctrlno int
declare @qty float
declare @storeno int
declare @oltype int
declare @outlet int
declare @rate money
declare @amttyp char(1)
declare @olctrlno int
declare @bctrlno numeric
declare @txndt datetime
declare @ltxndt datetime
declare @forupdt char(1)
declare @balance float
if exists(select * from inserted where forupdt='Y') return
select  @bctrlno=bctrlno,@ctrlno=ctrlno,@qty=qty,@amttyp=amttyp,@rate=rate,@forupdt=forupdt from deleted
select @txndt=txndt,@oltype=issuedtotype,@outlet=issuedtocode from storebills where bctrlno=@bctrlno

declare @fy numeric
declare @company int
select @storeno=storeno from storeitems where ctrlno=@ctrlno
select @company=company from store where storeno=@storeno
exec  getfy @txndt,@company,@fy out

if @amttyp<>'C'
	begin
	update storeprimary set balance=balance + @qty where @fy=fy and ictrlno=@ctrlno
	if @@error<>0 rollback transaction
	if @oltype=1
	if exists (select * from kitmast2 where scode=@ctrlno)
	begin
		if not exists (select * from mizastock where icode=@ctrlno and otype=@oltype and outlet=@outlet) insert into mizastock (otype,outlet,icode,stock,ltxndt) values (@oltype,@outlet,@ctrlno,0,getdate())
		update mizastock set stock=stock-@qty,ltxndt=@txndt where otype=@oltype and outlet=@outlet and icode=@ctrlno
		declare @msicode numeric
    	select @msicode=ctrlno from mizastock  where icode=@ctrlno and outlet=@outlet and otype=@oltype
		if not exists(select * from mizstock where  outlet=@outlet and icode=@msicode and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) )
		insert into mizstock (icode,txndt,outlet) values (@msicode,@txndt,@outlet)
		update mizstock set received=received-@qty where outlet=@outlet and icode=@msicode and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
		if @@error<>0 rollback transaction
	end
	 end
else
	begin
		update storeprimary set balance=balance - @qty where @fy=fy and ictrlno=@ctrlno
	             if @@error<>0 rollback transaction
	end


select  @bctrlno=bctrlno,@ctrlno=ctrlno,@qty=qty,@amttyp=amttyp,@rate=rate from inserted
select @txndt=txndt,@oltype=issuedtotype,@outlet=issuedtocode from storebills where bctrlno=@bctrlno


select @storeno=storeno from storeitems where ctrlno=@ctrlno
select @company=company from store where storeno=@storeno
exec  getfy @txndt,@company,@fy out

/* For updating storeprimary table with new records for new financial year */
if not exists (select * from storeprimary where fy=@fy and ictrlno=@ctrlno)
insert into storeprimary (fy,ictrlno) values (@fy,@ctrlno)

if @amttyp='C'
	begin
		 update storeitems set stock=stock+@qty,lpdate=getdate(),rate=case when ratedate <= @txndt then @rate else rate end,ratedate=case when ratedate < @txndt then @txndt else ratedate end where ctrlno=@ctrlno
	             if @@error<>0 rollback transaction
		update storeprimary set balance=balance + @qty where @fy=fy and ictrlno=@ctrlno
	             if @@error<>0 rollback transaction
	end
else
begin
	update storeitems set stock=stock-@qty where ctrlno=@ctrlno
	if @@error<>0 rollback transaction
	update storeprimary set balance=balance - @qty where @fy=fy and ictrlno=@ctrlno
	if @@error<>0 rollback transaction
	if @oltype=1 
	if exists (select * from kitmast2 where scode=@ctrlno)
	begin
		if not exists (select * from mizastock where icode=@ctrlno and otype=@oltype and outlet=@outlet) insert into mizastock (otype,outlet,icode,stock,ltxndt) values (@oltype,@outlet,@ctrlno,0,getdate())
		update mizastock set stock=stock+@qty ,ltxndt=@txndt where otype=@oltype and outlet=@outlet and icode=@ctrlno
		if not exists(select * from mizstock where  outlet=@outlet and icode=@ctrlno and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) )
		insert into mizstock (icode,txndt,outlet) values (@ctrlno,@txndt,@outlet)
		update mizstock set received=received+@qty where outlet=@outlet and  icode=@ctrlno and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
		if @@error<>0 rollback transaction
	end
end
select @ltxndt=txndt from storebills where bctrlno=@bctrlno
GO
ALTER TABLE [dbo].[storehist] ENABLE TRIGGER [spstupdate]
GO
/****** Object:  Trigger [dbo].[StorehistQTY1]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[StorehistQTY1]
   ON  [dbo].[storehist]
   AFTER INSERT,UPDATE
AS 
BEGIN
if update(qty1) return
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	
	update storehist set qty1=case when amttyp='C' then qty else -qty end where hctrlno2 in (select hctrlno2 from inserted)    
END
GO
ALTER TABLE [dbo].[storehist] ENABLE TRIGGER [StorehistQTY1]
GO
/****** Object:  Trigger [dbo].[stpurinsert]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.stpurinsert    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.stpurinsert    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.stpurinsert    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.stpurinsert    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[stpurinsert] ON [dbo].[storehist] 
/* with encryption */
FOR INSERT 
AS
declare @ctrlno numeric
declare @qty float
declare @storeno int
declare @oltype int
declare @outlet int
declare @rate money
declare @amttyp char(1)
declare @olctrlno int
declare @bctrlno numeric
declare @txndt datetime
declare @ltxndt datetime
declare @forupdt char(1)
declare @hctrlno  numeric
declare @stk float
select  @hctrlno=hctrlno,@bctrlno=bctrlno,@ctrlno=ctrlno,@qty=qty,@amttyp=amttyp,@rate=rate,@forupdt=forupdt from inserted
select @txndt=txndt,@oltype=issuedtotype,@outlet=issuedtocode from storebills where bctrlno=@bctrlno
declare @fy numeric
declare @company int
select @stk=stock,@storeno=storeno from storeitems where ctrlno=@ctrlno
select @company=company from store where storeno=@storeno
exec  getfy @txndt,@company,@fy out
/* For updating storeprimary table with new records for new financial year */
if not exists (select * from storeprimary where fy=@fy and ictrlno=@ctrlno)
insert into storeprimary (fy,ictrlno) values (@fy,@ctrlno)

if @amttyp='C'
	begin
		select @stk=stock from storeitems where ctrlno=@ctrlno
		update storeitems set stock=stock+@qty,forupdt=@forupdt,lpdate=getdate(),rate=case when datediff(d,'1/1/90',ratedate) <= datediff(d,'1/1/90',@txndt) then @rate else rate end,ratedate=case when datediff(d,'1/1/90',ratedate) <= datediff(d,'1/1/90',@txndt) then @txndt else ratedate end,ltxndt=case when datediff(d,'1/1/90',ltxndt) > datediff(d,'1/1/90',@txndt) then @txndt else ltxndt end  where ctrlno=@ctrlno 
		if @@error<>0 rollback transaction
		update storeprimary set balance=balance+@qty where fy=@fy and ictrlno=@ctrlno
		if @@error<>0 rollback transaction
	end
else
	begin
		update storeitems set stock=stock-@qty,ltxndt=case when ltxndt > @txndt then @txndt else ltxndt end where ctrlno=@ctrlno
		if @@error<>0 rollback transaction
		update storeprimary set balance=balance-@qty where fy=@fy and ictrlno=@ctrlno
		if @@error<>0 rollback transaction
		if @oltype=1
		if exists (select * from kitmast2 where scode=@ctrlno)
		begin
			if not exists (select * from mizastock where icode=@ctrlno and otype=@oltype and outlet=@outlet) insert into mizastock (otype,outlet,icode,stock,ltxndt) values (@oltype,@outlet,@ctrlno,0,getdate())
			update mizastock set stock=stock+@qty,ltxndt=@txndt where otype=@oltype and outlet=@outlet and icode=@ctrlno
			declare @msicode numeric
    		select @msicode=ctrlno from mizastock  where icode=@ctrlno and outlet=@outlet and otype=@oltype
			if not exists(select * from mizstock where icode=@msicode and outlet=@outlet and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) )
			insert into mizstock (icode,txndt,outlet) values (@msicode,@txndt,@outlet)
			update mizstock set received=isnull(received,0)+@qty where outlet=@outlet and icode=@msicode and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
			if @@error<>0 rollback transaction
		end
	end
GO
ALTER TABLE [dbo].[storehist] ENABLE TRIGGER [stpurinsert]
GO
/****** Object:  Trigger [dbo].[tg_updatehctrlno2]    Script Date: 07/04/2026 20:30:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[tg_updatehctrlno2] ON [dbo].[storehist] 
/* with encryption */
FOR INSERT
AS
declare @hctrlno numeric
select @hctrlno=hctrlno from inserted
update storehist set forupdt='N',hctrlno2=case when isnull(hctrlno2,0)=0 then hctrlno else hctrlno2 end  where hctrlno=@hctrlno
GO
ALTER TABLE [dbo].[storehist] ENABLE TRIGGER [tg_updatehctrlno2]
GO
/****** Object:  Trigger [dbo].[CHKStoreMaster]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[CHKStoreMaster] 
   ON  [dbo].[storeitems]
   AFTER UPDATE
AS 
BEGIN
	if not update(luperhu) return
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @ctrlno numeric
	declare @ovalue numeric
	declare @nvalue numeric
	select @ctrlno=ctrlno,@nvalue=luperhu from inserted
	select @ovalue=luperhu from deleted	
	if @ovalue<>@nvalue
	if exists(select * from storehist where ctrlno=@ctrlno)
		begin
		print 'You can not alter the ''Lower Unit Per Higher Unit Value'' after entering issues and purchases for a Store Item'
		rollback transaction
		raiserror('Error in updating Store Master',16,-1) 
		return
		end
END
GO
ALTER TABLE [dbo].[storeitems] ENABLE TRIGGER [CHKStoreMaster]
GO
/****** Object:  Trigger [dbo].[storeminusbalance]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[storeminusbalance]    ON  [dbo].[storeitems]    AFTER UPDATE AS BEGIN return if not update(stock) return 		SET NOCOUNT ON; declare @stock decimal(18,4) declare @ctrlno numeric declare @iname varchar(200) declare @msg varchar(200) select @stock=stock,@ctrlno=ctrlno from inserted select @iname=iname from storeitems where ctrlno=@ctrlno if @stock < 0 	begin 	set @msg =  'Negative balance in ' + @iname 	rollback transaction  	raiserror (@msg,16,-1) 	return 	end END 
GO
ALTER TABLE [dbo].[storeitems] ENABLE TRIGGER [storeminusbalance]
GO
/****** Object:  Trigger [dbo].[updstbal]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[updstbal]  ON [dbo].[StorePrimary] /* with encryption */
FOR UPDATE 
AS 
if update(opgbal)  return
if update(balance) return
if not update(balance) return declare @fy numeric declare @ictrlno numeric
declare @balance decimal declare @balance1 decimal declare @diff  decimal  declare @ctrlno numeric 
declare @name varchar(200) 

select @fy=fy,@ictrlno=ictrlno,@balance=balance from inserted 
select @balance1=balance from deleted 
select @name=iname from storeitems where ctrlno=@ictrlno 
if @balance < 0 begin print 'Negative Balance-' + rtrim(@name) + ' Deleted ' +  convert(varchar(20),@balance1) + ' updated ' + convert(varchar(20),@balance)
if exists(select * from setting where g_storeminus = 0) begin rollback transaction  return  end end  set @diff = @balance1-@balance  if exists( select ctrlno from storeprimary where ictrlno=@ictrlno and fy=(select min(fy) from storeprimary where ictrlno=@ictrlno and fy > @fy)) begin select @ctrlno=ctrlno from storeprimary where ictrlno=@ictrlno and fy=(select min(fy) from storeprimary where ictrlno=@ictrlno and fy > @fy) update storeprimary set opgbal=@balance,balance=balance-@diff where ctrlno=@ctrlno end 

GO
ALTER TABLE [dbo].[StorePrimary] ENABLE TRIGGER [updstbal]
GO
/****** Object:  Trigger [dbo].[suppcheck]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO











/****** Object:  Trigger dbo.suppcheck    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.suppcheck    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.suppcheck    Script Date: 10/08/2004 8:28:15 PM ******/
CREATE TRIGGER [dbo].[suppcheck] ON [dbo].[suppliers] 
/* with encryption */
FOR DELETE 
AS
if exists (select * from storebills where amttyp='C' and supplier=(select sid from deleted)) 
begin
print 'Supplier can not be deleted...since there are some purchases from this supplier'
rollback
raiserror('',16,-1)
end











GO
ALTER TABLE [dbo].[suppliers] ENABLE TRIGGER [suppcheck]
GO
/****** Object:  Trigger [dbo].[trg_ins_toutlet]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.trg_ins_toutlet    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.trg_ins_toutlet    Script Date: 4/14/2005 10:21:48 PM ******/
/****** Object:  Trigger dbo.trg_ins_toutlet    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.trg_ins_toutlet    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE TRIGGER [dbo].[trg_ins_toutlet] ON [dbo].[toutlet] 
/* with encryption */
FOR INSERT
AS
declare @company int
declare @outlet int
declare @selfdeal char(1)
declare @pos int
--print 'periya'
--if update(billposition1) return
--if update(billposition2) return
select @outlet=outlet,@selfdeal=selfdeal,@COMPANY=COMPANY from inserted
SET @POS = 0
select @outlet=outlet,@selfdeal=selfdeal,@COMPANY=COMPANY from inserted
if @company=1 begin
	select @pos=position from billposition where company=1 and position=(select max(position) from billposition where company=1 and outlet<>0)
	set @pos=@pos+1
	if @pos > 10 begin rollback transaction print 'You have reached the Maximum Number of Outlets for this company' raiserror('Error',16,-1) return end
	update billposition set servicetype=3,outlet=@outlet where company=1 and position=@pos
	update toutlet  set billposition1=@pos where outlet=@outlet
end
else  /* if not company=1*/
if @selfdeal='N'
begin
	select @pos=position from billposition where company=1 and position=(select max(position) from billposition where company=1 and outlet<>0)
	set @pos=@pos+1
	if @pos > 10 begin rollback transaction print 'You have reached the Maximum Number of Outlets for this company' raiserror('Error',16,-1) return end
	update billposition set servicetype=3,outlet=@outlet where company=1 and position=@pos
	update toutlet  set billposition1=@pos where outlet=@outlet
end
else
begin
	select @pos=position from billposition where company=@company and position=(select max(position) from billposition where company=@company and outlet<>0)
	set @pos=@pos+1
	if @pos > 10 begin rollback transaction print 'You have reached the Maximum Number of Outlets for this company' raiserror('Error',16,-1) return end
	update billposition set servicetype=3,outlet=@outlet where company=@company and position=@pos
	update toutlet  set billposition2=@pos where outlet=@outlet
end
GO
ALTER TABLE [dbo].[toutlet] ENABLE TRIGGER [trg_ins_toutlet]
GO
/****** Object:  Trigger [dbo].[CashPermission]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger [dbo].[CashPermission]    Script Date: 12/04/2017 14:37:56 ******/ 
CREATE trigger [dbo].[CashPermission] on [dbo].[VOUCHERS] /* with encryption */ 

FOR INSERT, UPDATE, DELETE  AS  
declare @err varchar(500) 
declare @userno numeric 
 declare @error varchar(1000)  
 declare @control char(1)  
 select @userno=userno,@control=control  from comuser where [host_id]=host_id() 
  if @control<>'Y'  
  if exists(select * from setting where g_cashentries=0)  
  if exists(select * from servicepermission where userno=@userno and permission='N' and serviceno=813)  	
  begin      	
  rollback      	
  set @error =  'You are not permitted to make any CASH Transactions'      	
  raiserror(@error,16,1)      	
  Return  	
  End  
  
  if not update(amt)  return 
  if @control<>'Y'if exists(select * from  comuser where [host_id]=host_id())	
  if not exists(select * from hmsfolder where version_no = (select TOP 1 version_no from comuser where [host_id]=host_id())) 	 	
  begin 	 	
  set @err='Voucher Posting- CashPermission -> You are using a wrong version of the Softwre Installed...Please update the version (T3) and try'	 	
  raiserror(@err,16,-1) 	 	
  return 		
  end
GO
ALTER TABLE [dbo].[VOUCHERS] ENABLE TRIGGER [CashPermission]
GO
/****** Object:  Trigger [dbo].[roomServiceBlobk]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[roomServiceBlobk] 
   ON  [dbo].[VOUCHERS]
   AFTER INSERT,UPDATE
AS 
BEGIN
	SET NOCOUNT ON;
	declare  @company int
	declare @actnum numeric
	declare @amttyp char(1)
	declare @partic varchar(300)
	select @company=company,@actnum=actnum,@amttyp=amttyp,@partic=partic from inserted
	if @actnum=24 and @amttyp='D'
		begin
			if @partic like '%adv.r.no%' 
				begin
					rollback transaction
					raiserror('Error in [Room Service Receivable a/c posting].Entry not allowed',16,-1)
					return
				end
		end
END
GO
ALTER TABLE [dbo].[VOUCHERS] ENABLE TRIGGER [roomServiceBlobk]
GO
/****** Object:  Trigger [dbo].[updatevouhist]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE TRIGGER [dbo].[updatevouhist]
   ON  [dbo].[VOUCHERS]
   AFTER DELETE
AS 
BEGIN
declare @txnno numeric(18,0)
select @txnno=txnno from inserted
declare @flds varchar(5000)
declare @sql varchar(8000)
--set @flds = 'COMPANY,FY,ACTNUM,TXNNO,TXNDT,AMTTYP,PARTIC,AMT,[USER],BALANCE,RECON,GRNO,FDRSNO,CHANNELCODE,REFNO,tmstmp,ltxndt,markedfordeletion,payee,mvchno,TXNNO2,txntype,recondate,EXApproved,EXDate,EXby,sessionid,outlet,seller,billno,billdate,chlno,taxtype,TOTAMT,PERCENTAGE,ACTNO'
--the autoselection of feilds created on 8 April 2020
select @flds=columns from (Select table_SCHEMA, table_name     , Stuff(        (        Select ', [' +  C.COLUMN_NAME + ']'       From INFORMATION_SCHEMA.COLUMNS As C        Where C.TABLE_SCHEMA = T.TABLE_SCHEMA            And C.TABLE_NAME = T.TABLE_NAME        Order By C.ORDINAL_POSITION        For Xml Path('')        ), 1, 2, '') As Columns From INFORMATION_SCHEMA.TABLES as T  where table_name='vouchers' ) a
select * into #dum from deleted
set @sql = 'insert into vouhist (' + @flds + ') select top 1 ' + @flds + ' from #dum'
--insert into vouhist (COMPANY,FY,ACTNUM,TXNNO,TXNDT,AMTTYP,PARTIC,AMT,[USER],BALANCE,RECON,GRNO,FDRSNO,CHANNELCODE,REFNO,tmstmp,ltxndt,markedfordeletion,payee,mvchno,TXNNO2,txntype,recondate,EXApproved,EXDate,EXby,sessionid,outlet,seller,billno,billdate,chlno,taxtype,TOTAMT,PERCENTAGE,ACTNO) select top 1 COMPANY,FY,ACTNUM,TXNNO,TXNDT,AMTTYP,PARTIC,AMT,[USER],BALANCE,RECON,GRNO,FDRSNO,CHANNELCODE,REFNO,tmstmp,ltxndt,markedfordeletion,payee,mvchno,TXNNO2,txntype,recondate,EXApproved,EXDate,EXby,sessionid,outlet,seller,billno,billdate,chlno,taxtype,TOTAMT,PERCENTAGE,ACTNO from deleted
exec(@sql)
END
GO
ALTER TABLE [dbo].[VOUCHERS] ENABLE TRIGGER [updatevouhist]
GO
/****** Object:  Trigger [dbo].[updltxndt]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.updltxndt    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.updltxndt    Script Date: 4/14/2005 10:21:49 PM ******/
/****** Object:  Trigger dbo.updltxndt    Script Date: 10/08/2004 8:28:15 PM ******/
CREATE  TRIGGER [dbo].[updltxndt] ON [dbo].[VOUCHERS] 
/* with encryption */
FOR INSERT, UPDATE,delete 
AS
IF UPDATE(PAYEE) RETURN
IF UPDATE(BALANCE) RETURN
if update(recon) return
if update(recondate) return
IF UPDATE(FDRSNO) RETURN
IF UPDATE(TOTAMT_BALANCE) RETURN
declare @txndt datetime
declare @actnum numeric
declare @company int
declare @fy int
declare @userno numeric
declare @vpdays numeric
declare @vpdays2 numeric
declare @control char(1)
declare @error varchar(2000)
declare @sessionid varchar(200)
declare @host_name varchar(200)
declare @totamt_balance money
declare @amt money
declare @amttyp char(1)
declare @txnno numeric
declare @seller varchar(100)
declare @billdate_closedate datetime
declare @taxtype int
select @host_name=computer from comuser where [host_id]=host_id() 
select @txnno=txnno,@amttyp=amttyp, @sessionid=sessionid,@txndt=txndt,@actnum=actnum,@company=company,@fy=fy,@amt=amt,@totamt_balance=TOTAMT_BALANCE,@seller=seller,@billdate_closedate=billdate_closedate,@taxtype=taxtype  from inserted


if exists (select * from inserted where (actnum >=104 and actnum <110 ) and (amttyp='D') and (seller is null  or billno is null or billdate_closedate is null or taxtype is null))
	begin
	print 'Input Tax Details not given - updltxndt'
	rollback 
	return
	end
	

if exists (select * from inserted where actnum =104 and (amttyp='C') and (seller is null  or billno is null or billdate_closedate is null or taxtype is null))
	begin
	print 'TDS Details not given - updltxndt'  + @seller  + ' ' + convert(varchar(20),@billdate_closedate)
	rollback 
	return
	end


select @host_name=computer from comuser where [host_id]=host_id() 
select @txnno=txnno,@amttyp=amttyp, @sessionid=sessionid,@txndt=txndt,@actnum=actnum,@company=company,@fy=fy,@amt=amt,@totamt_balance=TOTAMT_BALANCE  from inserted
declare @asset char(1)

--select @asset=asset from glprimld where fy=@fy and cgp_actno=@actnum
--if (@asset='D' and @amttyp='D')  or (@asset='C' and @amttyp='C')
--if  not @totamt_balance=@amt
--begin
--print 'SUB-GL Account Reverese exceeds the Original Amount  1 '  + str(@totamt_balance) + ' ' + str(@amt)  + str(@totamt_balance-@amt)
--rollback 
--return
--end 



if exists(select * from glprimld where fy=@fy and cgp_actno=@actnum and asset='D')
if  @totamt_balance < 0   OR @TOTAMT_BALANCE > @AMT 
begin
print 'SUB-GL Account Reverese exceeds the Original Amount  1 '  + str(@totamt_balance) + ' ' + str(@amt)  + str(@totamt_balance-@amt)
rollback 
return
end 

if exists(select * from glprimld where fy=@fy and cgp_actno=@actnum and asset='C')
if (@totamt_balance > @amt  or @totamt_balance  < 0 )  
begin
print 'SUB-GL Account Reverse exceeds the Original Amount  2  '  + convert(varchar(20),@totamt_balance) + '  ' + convert(varchar(20),@amt)
rollback 
return
end 


set @sessionid=isnull(@sessionid,'')
select @userno=userno,@control=control from comuser where [host_id]=host_id() and sessionid=@sessionid
set @control = isnull(@control,'Y')
if isnull(@control,'Y')<>'Y' 
begin
	if isnull(@userno,0)=0 begin rollback set @error = '33Invalid User Info (User Number)' + str(isnull(@userno,0)) +  @host_name + @sessionid +  '  Found..Please Login Again Control ' + @control + ' sessionid=' + @sessionid raiserror(@error,16,-1) return end
	select @vpdays=vpdays,@vpdays2=vpdays from basic where userno=@userno
	if @vpdays=null begin rollback set @error = 'Invalid User Info (Days) Found' raiserror(@error,16,-1) return end
	---if @vpdays<>0 
	begin
		if exists (select * from inserted)
			begin
			select @txndt=txndt  from inserted
			end
		if  datediff(d,@txndt,getdate())  > @vpdays  begin rollback set @error= 'You dont have permission to delete/modfy transactions beyond ' + ltrim(str(@vpdays)) +  ' day(s) (1)' + convert(varchar(20),@txndt)  raiserror(@error,16,-1) return end
		if exists (select * from deleted)
			begin
			select @txndt=txndt from deleted
			end
		if datediff(d,@txndt,getdate()) > @vpdays  begin rollback set @error =  'You dont have permission to delete/modfy transactions beyond ' + ltrim(str(@vpdays)) + ' days (2)' + convert(varchar(20),@txndt)  raiserror(@error,16,1)   return end
	end
end
if update(balance) return
if update(txnno2) return
if update(fdrsno) return
if update([user]) return
if update(payee) return
if exists (select * from inserted)
begin
select @txndt=txndt,@actnum=actnum,@company=company,@fy=fy from inserted
update glprimld set ltxndt= case when datediff(d,'1/1/90',ltxndt) >= datediff(d,'1/1/90',@txndt) then @txndt else ltxndt end where cgp_actno=@actnum and company=@company and fy=@fy
end
if exists (select * from deleted)
begin
select @txndt=txndt,@actnum=actnum,@company=company,@fy=fy from deleted
update glprimld set ltxndt= case when datediff(d,'1/1/90',ltxndt) >= datediff(d,'1/1/90',@txndt) then @txndt else ltxndt end  where cgp_actno=@actnum and company=@company and fy=@fy
end
GO
ALTER TABLE [dbo].[VOUCHERS] ENABLE TRIGGER [updltxndt]
GO
/****** Object:  Trigger [dbo].[vou_delete]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.vou_delete    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.VCH_DEL_TRG    Script Date: 4/14/2005 10:21:49 PM ******/
/****** Object:  Trigger dbo.VCH_DEL_TRG    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.VCH_DEL_TRG    Script Date: 15/02/2004 6:23:02 PM ******/
/****** Object:  Trigger dbo.VCH_DEL_TRG    Script Date: 1/20/03 11:51:49 AM ******/
/****** Object:  Trigger dbo.VCH_DEL_TRG    Script Date: 10/14/02 1:17:58 PM ******/
/****** Object:  Trigger dbo.VCH_DEL_TRG    Script Date: 9/13/02 9:52:11 PM ******/
/****** Object:  Trigger dbo.VCH_DEL_TRG    Script Date: 7/28/02 9:35:57 AM ******/
CREATE  TRIGGER [dbo].[vou_delete] ON [dbo].[VOUCHERS] 
/* /* with encryption */*/
FOR  DELETE 
AS
--SET XACT_ABORT ON
DECLARE @M_AMT MONEY
DECLARE @M_AMTTYP CHAR(1)
DECLARE @M_TXNDT DATETIME
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_ACTNUM INT
DECLARE @M_FDRSNO INT
DECLARE @M_TXNNO INT
DECLARE @M_TDNO INT
DECLARE @M_ASSET CHAR(1)
DECLARE @M_ONAC CHAR(1)
DECLARE @M_ONACBAL MONEY
DECLARE @M_FDRBAL MONEY
DECLARE @M_FDRAMT MONEY
DECLARE @M_AMT1 MONEY
DECLARE @M_1 MONEY
DECLARE @M_2 MONEY
DECLARE @M_OLDTXNDT DATETIME
DECLARE @M_CLOSDATE DATETIME
DECLARE @EMSG VARCHAR(200)
declare @con_id char(20)
declare @amount money
declare @txndt datetime
declare @amttyp char(1)
declare @actnum numeric
declare @company int
declare @fy int
declare @txnno numeric
--declare @pcon varchar(100)
declare @usernm varchar(50)
declare @amt money
declare @mfd char(1)
declare @host_name varchar(200)
declare @totamt_balance numeric(18,2) 
declare @chlno_rtxnno numeric
declare @orgamt numeric(18,2) 
select @host_name=computer from comuser where [host_id]=host_id()
SET @M_FDRSNO = @@ROWCOUNT
SET @EMSG =  'VOUCHER DELETION TRIGGER FAILED' + str(@m_fdrsno) 
IF @M_FDRSNO = 0 RETURN
select @mfd=markedfordeletion from deleted
if @mfd='Y' return

IF @M_FDRSNO > 1 begin
RAISERROR('MULTIPLE DELETION NOT ALLOWED',20,-1) WITH LOG 
end
set nocount on
select * into #deleted3 from deleted


--blockdelete
select @txndt=txndt,@fy=fy from deleted
if exists(select * from finyear where fy=@fy and blocked='Y')
begin
print 'Financial Year Blocked - No Transactions Alllowed'
rollback transaction
raiserror('Financial Year Blocked',16,-1)
end
if exists(select * from finyear where fy=@fy and datediff(d,'1/1/80',@txndt) <= datediff(d,'1/1/80',blockdate))
begin
print 'Transactions blocked for this date - No Transactions Alllowed'
rollback transaction
raiserror('Financial Year Blocked',16,-1)
end

SET @EMSG =  'Periya0001'


--ram
--cashbookd
select @usernm=usernm from comuser where computer=@host_name
     select @txnno=txnno,@actnum=actnum,@amt=amt,@amttyp=amttyp,@txndt=txndt,@usernm=[user],@totamt_balance=TOTAMT_BALANCE,@chlno_rtxnno=chlno_RTXNNO    from #deleted3
     if @actnum=1 
     begin
		delete from cashboxhist where txnno=@txnno 
		if @amttyp = 'D'
			begin
			update cashbook set amount=amount - @amt where cname=@host_name and uname=@usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
			end
		else
			begin
			update cashbook set amount=amount + @amt where cname=@host_name and uname=@usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
			end



-- This portion is inserted for maintaining Cash Box 03-01-2009 Saturday FGP2

declare @cashboxuser char(1)
declare @cashboxno  numeric
declare @cashbox int
declare @cbtxndt datetime
select @usernm=usernm,@cashboxuser=cashbox from comuser where computer=@host_name
if @cashboxuser='Y'
begin
  select @cashbox=g_cashbox from setting 
  if @cashbox=1
	begin
		if exists(select * from cashboxuser where computername=@host_name and usernm=@usernm and status<>'N' )
		begin
			select @cashboxno=cashboxno from cashboxuser where computername=@host_name and usernm=@usernm and status<>'N'
			---print 'periya' + str(@cashboxno)  + str(@txnno) goto e_error
			delete from cashboxhist where txnno=@txnno and cashboxno=@cashboxno
			if @@error<>0 goto e_error
		end
		else
		begin
			IF exists(select * from cashboxuser where computername=@host_name and usernm<>@usernm and status<>'N' )
			BEGIN
					print 'The Cash Box is used by another user'
					SET @EMSG =  'The Cash Box is used by another user'
			END
			else
				if not exists(select * from cashboxuser where computername=@host_name and  status<>'N' )
				BEGIN
					print 'The Cash Box is not opened..No Cash Transactions allowed from this terminal'
					SET @EMSG = 		'The Cash Box is not opened..No Cash Transactions allowed from this terminal'
					goto e_error
					return
				END
			end
	end
	else
	begin
		if exists(select * from cashboxuser where computername=@host_name and usernm<>@usernm and status<>'N' )
			begin
			select @cashboxno=cashboxno from cashboxuser where computername=@host_name and status<>'N'
			select @cbtxndt=txndt from cashbox where ctrlno=@cashboxno
			if datediff(d,@txndt,@cbtxndt) >=0   
				begin
				print 'The Cash Box is used by another user..No cash Transactions allowed'
				set @emsg = 'The Cash Box is used by another user..No cash Transactions allowed'
				goto e_error
				end
			end
	end

end
------- cashbox maintanence ends here



     end
-- vc_delete
if @@rowcount > 1 return
--original delete 
SET @EMSG =  'Periya0002'

declare @partic varchar(100)
SELECT @partic=partic,@M_AMT=AMT,@M_AMTTYP=AMTTYP,@M_TXNDT=TXNDT,@M_COMPANY=COMPANY,@M_FY=FY,@M_ACTNUM=ACTNUM,@M_FDRSNO=FDRSNO,@M_TXNNO=TXNNO FROM #DELETED3
if @amttyp='C'  and (@actnum=1 or @actnum=51)
insert into acclog (narration) values ('Cash Payment voucher deleted - Amount : ' + convert(varchar(20),@amt) + ' Details-' + @partic )
set @m_amt1 = @m_amt
IF @M_AMTTYP='C'
SET @M_AMT=@M_AMT*-1
---
UPDATE GLPRIMLD SET BALANCE=BALANCE+@M_AMT,LTXNDT= CASE WHEN datediff(d,'1/1/90',@M_TXNDT)<=datediff(d,'1/1/90',ltxndt)  THEN @M_TXNDT  ELSE LTXNDT END ,
DEBIT1  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=1  THEN DEBIT1 -(@M_AMT1) ELSE DEBIT1 END ELSE DEBIT1   END,
DEBIT2  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=2  THEN DEBIT2 -(@M_AMT1) ELSE DEBIT2 END ELSE DEBIT2   END,
DEBIT3  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=3  THEN DEBIT3 -(@M_AMT1) ELSE DEBIT3 END ELSE DEBIT3   END,
DEBIT4  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=4  THEN DEBIT4 -(@M_AMT1) ELSE DEBIT4 END ELSE DEBIT4   END,
DEBIT5  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=5  THEN DEBIT5 -(@M_AMT1) ELSE DEBIT5 END ELSE DEBIT5   END,
DEBIT6  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=6  THEN DEBIT6 -(@M_AMT1) ELSE DEBIT6 END ELSE DEBIT6   END,
DEBIT7  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=7  THEN DEBIT7 -(@M_AMT1) ELSE DEBIT7 END ELSE DEBIT7    END,
DEBIT8  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=8  THEN DEBIT8 -(@M_AMT1) ELSE DEBIT8 END ELSE DEBIT8    END,
DEBIT9  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=9  THEN DEBIT9 -(@M_AMT1) ELSE DEBIT9 END ELSE DEBIT9    END,
DEBIT10 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=10 THEN DEBIT10-(@M_AMT1) ELSE DEBIT10 END ELSE DEBIT10  END,
DEBIT11 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=11 THEN DEBIT11-(@M_AMT1) ELSE DEBIT11 END ELSE DEBIT11  END,
DEBIT12 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=12 THEN DEBIT12-(@M_AMT1) ELSE DEBIT12 END ELSE DEBIT12  END,
CREDIT1  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=1  THEN CREDIT1- (@M_AMT1) ELSE CREDIT1 END ELSE CREDIT1   END,
CREDIT2  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=2  THEN CREDIT2- (@M_AMT1) ELSE CREDIT2 END ELSE CREDIT2   END,
CREDIT3  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=3  THEN CREDIT3- (@M_AMT1) ELSE CREDIT3 END ELSE CREDIT3   END,
CREDIT4  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=4  THEN CREDIT4- (@M_AMT1) ELSE CREDIT4 END ELSE CREDIT4   END,
CREDIT5  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=5  THEN CREDIT5- (@M_AMT1) ELSE CREDIT5 END ELSE CREDIT5   END,
CREDIT6  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=6  THEN CREDIT6- (@M_AMT1) ELSE CREDIT6 END ELSE CREDIT6   END,
CREDIT7  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=7  THEN CREDIT7- (@M_AMT1) ELSE CREDIT7 END ELSE CREDIT7    END,
CREDIT8  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=8  THEN CREDIT8- (@M_AMT1) ELSE CREDIT8 END ELSE CREDIT8    END,
CREDIT9  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=9  THEN CREDIT9- (@M_AMT1) ELSE CREDIT9 END ELSE CREDIT9    END,
CREDIT10 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=10 THEN CREDIT10-(@M_AMT1) ELSE CREDIT10 END ELSE CREDIT10  END,
CREDIT11 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=11 THEN CREDIT11-(@M_AMT1) ELSE CREDIT11 END ELSE CREDIT11  END,
CREDIT12 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=12 THEN CREDIT12-(@M_AMT1) ELSE CREDIT12 END ELSE CREDIT12  END
WHERE COMPANY=@M_COMPANY AND FY = @M_FY AND CGP_ACTNO=@M_ACTNUM
/**/
IF @@ERROR <> 0 GOTO E_ERROR
SET @EMSG =  'Periya0003-Voucher Deleting Error'

set @m_amt1 = 0
IF @M_AMTTYP='C'
SET @M_AMT=@M_AMT*-1
SELECT @M_TDNO=TDNO,@M_ASSET=ASSET,@M_ONAC=ONAC  FROM GLPRIMLD WHERE COMPANY=@M_COMPANY AND FY = @M_FY AND CGP_ACTNO=@M_ACTNUM

IF (@M_ASSET='D' and @m_amttyp='D')  OR ( @M_ASSET='C' and @m_amttyp='C')
	begin
	if @totamt_balance = 0 
	BEGIN 
		print  'Sub-GL account seems already reversed fROM VOUCHERS - delete 1 ' +  convert(varchar(20),@totamt_balance) + ' '+  convert(varchar(20),@amt) 
		SET @EMSG = 'Sub-GL account seems already reversed fROM VOUCHERS - delete 1 ' + @MFD +   convert(varchar(20),@totamt_balance) + ' '+  convert(varchar(20),@amt) 
		GOTO E_ERROR
	END
	end
SET @EMSG =  'Periya0004'



IF (@M_ASSET='D' AND @M_AMTTYP ='C') OR (@M_ASSET='C' AND @M_AMTTYP='D')
	BEGIN

   select @totamt_balance=totamt_balance,@orgamt=amt,@m_fdrsno=fdrsno from vouchers where company=@m_company and txnno=@chlno_rtxnno 

   if @totamt_balance > @orgamt 
   begin set @emsg = 'The transaction amount exceeds the Vouchers Amount - Voucher - delete' + convert(varchar(20),@totamt_balance) + convert(varchar(20),@orgamt)  goto e_error 
   end
        END        --------------

if (@m_asset='D' and @m_amttyp='C') or (@m_asset='C' and @m_amttyp='D')
	BEGIN
		UPDATE VOUCHERS SET  TOTAMT_BALANCE=totamt_balance+@amt,billdate_CloseDate='1 jan 1900' ,chlno_RTXNNO=0 WHERE TXNNO=@chlno_rtxnno
		IF @@ERROR <> 0 GOTO E_ERROR
	END
RETURN
E_ERROR:
RAISERROR ( @emsg, 16,-1)
ROLLBACK
RETURN
GO
ALTER TABLE [dbo].[VOUCHERS] ENABLE TRIGGER [vou_delete]
GO
/****** Object:  Trigger [dbo].[vou_insert]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.vou_insert    Script Date: 21/09/2006 12:14:21 PM ******/
/****** Object:  Trigger dbo.VOU_U_TRG    Script Date: 4/14/2005 10:21:49 PM ******/
/****** Object:  Trigger dbo.VOU_U_TRG    Script Date: 10/08/2004 8:28:15 PM ******/
/****** Object:  Trigger dbo.VOU_U_TRG    Script Date: 15/02/2004 6:23:02 PM ******/
CREATE    TRIGGER [dbo].[vou_insert] ON [dbo].[VOUCHERS] 
/* with encryption */
FOR INSERT AS
DECLARE @M_AMT MONEY
declare @m_amt1 money
DECLARE @M_AMTTYP CHAR(1)
DECLARE @M_TXNDT DATETIME
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_ACTNUM INT
DECLARE @M_TDNO INT
DECLARE @M_ASSET CHAR(1)
DECLARE @M_PARTIC VARCHAR(500)
DECLARE @M_FDRSNO INT
DECLARE @M_ONAC CHAR(1)
DECLARE @M_TXNNO INT
DECLARE @M_OLDTXNDT DATETIME
declare @actno numeric
declare @code numeric
declare @error varchar(2000)
select @code=rate5 from stdisd3 where sno=21
if @code=17 
begin
goto e_error
end
set nocount on
declare @fy int
declare @usernm varchar(50)
declare @txndt datetime
declare @amttyp char(1)
declare @amt money
declare @actnum int
declare @chlno_rtxnno numeric
declare @host_name varchar(100)
select @host_name=computer from comuser where [host_id]=host_id()
set @error = 'Insert Trigger Failed in Voucher Table'
if exists(select * from setting where g_finaccount=0) begin raiserror('Attempting post vouchers - PowerHms-Lite',16,-1) return end
select *  into #inserted   from inserted
--block insert
select @txndt=txndt,@fy=fy from #inserted
if exists(select * from finyear where fy=@fy and blocked='Y')
begin
set @error= 'Financial Year Blocked - No Transactions Allowed'
rollback transaction
raiserror(@error,16,-1)
end


if exists(select * from finyear where fy=@fy and datediff(d,'1/1/80',@txndt) <= datediff(d,'1/1/80',blockdate))
begin
set @error =  'Transactions blocked for this date - No Transactions Allowed'
rollback transaction
raiserror(@error,16,-1)
end


-- vc_insert
declare @con_id char(20)
declare @amount money
declare @company int
declare @txnno numeric
declare @partic_con varchar(100)
declare @partic varchar(100)
select @partic=partic,@txnno=txnno,@amount=amt, @txndt=txndt, @amttyp=amttyp, @actnum=actnum, @company=company,@fy=fy,@chlno_rtxnno=chlno_RTXNNO  from #inserted
if @amount < 0  begin rollback raiserror('Amount Invalid',16,-1) end

--cashbooki
select @usernm=usernm from comuser where [host_id]=host_id()
select @actnum=actnum,@amt=amt,@amttyp=amttyp,@txndt=txndt from #inserted


if @actnum=1 
begin
     if not exists(select * from cashbook where cname = @host_name and uname = @usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) )
        begin
        insert into cashbook (cname,uname,txndt,amount) values (@host_name,@usernm,@txndt,0)        
       end
     if @amttyp = 'D'
        begin
        update cashbook set amount=amount + @amt where cname=@host_name and uname=@usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
        end
     else
        begin
        update cashbook set amount=amount -  @amt where cname=@host_name and uname=@usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
        end


-- This portion is inserted for maintaining Cash Box 03-01-2009 Saturday FGP2
	declare @cashboxuser char(1)
	declare @cashbox int
	declare @cashboxno numeric
	declare @cbtxndt datetime 

	select @usernm=usernm,@cashboxuser=cashbox from comuser where [host_id]=host_id()

	select @cashbox=g_cashbox from setting 
	if @cashbox = 1 
	if @cashboxuser='Y'
	begin
			if exists(select * from cashboxuser where computername=@host_name and usernm=@usernm and status<>'N' )
			begin
				select @cashboxno=cashboxno from cashboxuser where computername=@host_name and usernm=@usernm and status<>'N'
				insert into cashboxhist (txndt,cashboxno,amttyp,amt,partic,txnno,computername,usernm) values (@txndt,@cashboxno,@amttyp,@amount,@partic,@txnno,@host_name,@usernm)
			end
			else
				begin
					if exists(select * from cashboxuser where computername=@host_name and usernm<>@usernm and status<>'N' )
						set @error =  'The Cash Box is used by another user'
					else
					if not exists(select * from cashboxuser where computername=@host_name and  status<>'N' )
						set @error =  'The Cash Box is not opened..No cash Transactions allowed from this terminal'	

						goto e_error
					return
				end
	end  ---222
	else
		begin
			if exists(select * from cashboxuser where computername=@host_name and usernm<>@usernm and status<>'N' )
				begin
				select @cashboxno=cashboxno from cashboxuser where computername=@host_name and status<>'N'
				select @cbtxndt=txndt from cashbox where ctrlno=@cashboxno
				if datediff(d,@txndt,@cbtxndt) >=0   
				set @error =  'The Cash Box is used by another user..No cash Transactions allowed'
				goto e_error
				end
			else
			   begin
				   set @error = 'Cash Box is not opened ..No cash Transactions Allowed'
				   goto e_error
			   end
		end
	------- cashbox maintanence ends here

end
     
 
     
SELECT @M_AMT=AMT,@M_AMTTYP=AMTTYP,@M_TXNDT=TXNDT,@M_COMPANY=COMPANY,@M_FY=FY,@M_ACTNUM=ACTNUM,@M_PARTIC=PARTIC,@M_FDRSNO=FDRSNO,@M_TXNNO=TXNNO FROM #INSERTED
set @actno=@m_actnum
IF @M_AMT<=0 RETURN
set @m_amt1 = @m_amt

declare @amt_gl numeric(18,2)
set @amt_gl = @m_amt
IF @M_AMTTYP='D'
SELECT @amt_gl=@M_AMT*-1

select @usernm=usernm from comuser where [host_id]=host_id()
if @usernm='not logged in' 
begin
set @error='You are not logged in'
goto e_error
end

 UPDATE VOUCHERS SET [USER] = case when [user] is null then @usernm else  case when [user]='NULL' then @USERNM else [user] end end ,txnno2=case when isnull(txnno2,0)=0 then @m_txnno else txnno2 end WHERE TXNNO = @M_TXNNO

IF NOT EXISTS(SELECT * FROM GLPRIMLD WHERE COMPANY=@M_COMPANY AND FY = @M_FY AND CGP_ACTNO=@M_ACTNUM) 
BEGIN
set @error =  'GL A/C NUMBER GIVEN ' + LTRIM(STR(@M_ACTNUM)) + ' IS NOT FOUND IN THE MASTER FILE...CONTACT POWERBRAIN' + STR(@COMPANY) + STR(@FY)
goto e_error
END

UPDATE GLPRIMLD SET BALANCE=BALANCE+@AMT_GL,LTXNDT=case when datediff(d,'1/1/90',ltxndt) >= datediff(d,'1/1/90',@m_txndt) then @m_txndt else ltxndt end,TXNNO=@M_TXNNO,
DEBIT1  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=1  THEN DEBIT1 +(@M_AMT1) ELSE DEBIT1 END ELSE DEBIT1   END,
DEBIT2  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=2  THEN DEBIT2 +(@M_AMT1) ELSE DEBIT2 END ELSE DEBIT2   END,
DEBIT3  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=3  THEN DEBIT3 +(@M_AMT1) ELSE DEBIT3 END ELSE DEBIT3   END,
DEBIT4  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=4  THEN DEBIT4 +(@M_AMT1) ELSE DEBIT4 END ELSE DEBIT4   END,
DEBIT5  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=5  THEN DEBIT5 +(@M_AMT1) ELSE DEBIT5 END ELSE DEBIT5   END,
DEBIT6  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=6  THEN DEBIT6 +(@M_AMT1) ELSE DEBIT6 END ELSE DEBIT6   END,
DEBIT7  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=7  THEN DEBIT7 +(@M_AMT1) ELSE DEBIT7 END ELSE DEBIT7    END,
DEBIT8  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=8  THEN DEBIT8 +(@M_AMT1) ELSE DEBIT8 END ELSE DEBIT8    END,
DEBIT9  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=9  THEN DEBIT9 +(@M_AMT1) ELSE DEBIT9 END ELSE DEBIT9    END,
DEBIT10 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=10 THEN DEBIT10+(@M_AMT1) ELSE DEBIT10 END ELSE DEBIT10  END,
DEBIT11 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=11 THEN DEBIT11+(@M_AMT1) ELSE DEBIT11 END ELSE DEBIT11  END,
DEBIT12 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=12 THEN DEBIT12+(@M_AMT1) ELSE DEBIT12 END ELSE DEBIT12  END,
CREDIT1  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=1  THEN CREDIT1+ (@M_AMT1) ELSE CREDIT1 END ELSE CREDIT1   END,
CREDIT2  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=2  THEN CREDIT2+ (@M_AMT1) ELSE CREDIT2 END ELSE CREDIT2   END,
CREDIT3  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=3  THEN CREDIT3+ (@M_AMT1) ELSE CREDIT3 END ELSE CREDIT3   END,
CREDIT4  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=4  THEN CREDIT4+ (@M_AMT1) ELSE CREDIT4 END ELSE CREDIT4   END,
CREDIT5  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=5  THEN CREDIT5+ (@M_AMT1) ELSE CREDIT5 END ELSE CREDIT5   END,
CREDIT6  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=6  THEN CREDIT6+ (@M_AMT1) ELSE CREDIT6 END ELSE CREDIT6   END,
CREDIT7  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=7  THEN CREDIT7+ (@M_AMT1) ELSE CREDIT7 END ELSE CREDIT7    END,
CREDIT8  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=8  THEN CREDIT8+ (@M_AMT1) ELSE CREDIT8 END ELSE CREDIT8    END,
CREDIT9  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=9  THEN CREDIT9+ (@M_AMT1) ELSE CREDIT9 END ELSE CREDIT9    END,
CREDIT10 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=10 THEN CREDIT10+(@M_AMT1) ELSE CREDIT10 END ELSE CREDIT10  END,
CREDIT11 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=11 THEN CREDIT11+(@M_AMT1) ELSE CREDIT11 END ELSE CREDIT11  END,
CREDIT12 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=12 THEN CREDIT12+(@M_AMT1) ELSE CREDIT12 END ELSE CREDIT12  END
WHERE COMPANY=@M_COMPANY AND FY = @M_FY AND CGP_ACTNO=@M_ACTNUM
IF @@ERROR <> 0 GOTO E_ERROR

SELECT @M_TDNO=TDNO,@M_ASSET=ASSET,@M_ONAC=ONAC  FROM GLPRIMLD WHERE COMPANY=@M_COMPANY AND FY = @M_FY AND CGP_ACTNO=@M_ACTNUM



IF @M_ASSET='D'  AND @M_AMTTYP ='D' 
BEGIN
UPDATE VOUCHERS SET TOTAMT_BALANCE=amt   WHERE TXNNO = @M_TXNNO
END


IF @M_ASSET='C'  AND @M_AMTTYP ='C' 
BEGIN
UPDATE VOUCHERS SET FDRSNO = 0 WHERE TXNNO = @M_TXNNO
UPDATE VOUCHERS SET TOTAMT_BALANCE=amt   WHERE TXNNO = @M_TXNNO
END


IF (@M_ASSET='D' AND @M_AMTTYP ='C') OR (@M_ASSET='C' AND @M_AMTTYP='D')
BEGIN
--'	IF @M_ONAC='S'
--		BEGIN
		  if isnull(@chlno_rtxnno ,0) <> 0 
			  begin
				  update vouchers set totamt_balance=totamt_balance-@m_amt,billdate_CloseDate=@M_TXNDT,chlno_RTXNNO=@M_TXNNO   WHERE txnno=@chlno_rtxnno 
				  update vouchers set chlno_rtxnno=@chlno_rtxnno,BILLDATE_CLOSEDATE=@M_TXNDT where txnno=@m_txnno
			  end 
		  else
			  UPDATE VOUCHERS SET TOTAMT_BALANCE=amt   WHERE TXNNO = @M_TXNNO
--END

/*
IF @M_ONAC='O'
	BEGIN
		update vouchers set totamt_balance=totamt_balance-@m_amt,billdate_CloseDate=@M_TXNDT,chlno_RTXNNO=@M_TXNNO   WHERE txnno=@chlno_rtxnno 
		update vouchers set chlno_rtxnno=@chlno_rtxnno where txnno=@m_txnno
		IF @@ERROR <> 0 GOTO E_ERROR
	END
*/
END     
RETURN
E_ERROR:
ROLLBACK 
RAISERROR(@error,16,-1)
RETURN
GO
ALTER TABLE [dbo].[VOUCHERS] ENABLE TRIGGER [vou_insert]
GO
/****** Object:  Trigger [dbo].[vou_update]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/****** Object:  Trigger dbo.vou_update    Script Date: 21/09/2006 12:14:21 PM ******/
CREATE   TRIGGER [dbo].[vou_update] ON [dbo].[VOUCHERS] 
/* with encryption */
FOR  UPDATE 
AS
declare @error varchar(2000)

if update(txnno2) return
if update(fdrsno) return
if update([user]) return
--if update(balance) return
if update(ltxndt) return
--if update(recon) return
--if update(recondate) return
--if update (markedfordeletion)  return
--if update (totamt_balance) return 
IF UPDATE (CHLNO_RTXNNO) RETURN
--IF UPDATE (BILLDATE_CLOSEDATE) RETURN 

declare @host_name varchar(200)
DECLARE @M_AMT MONEY
DECLARE @M_AMTTYP CHAR(1)
DECLARE @M_TXNDT DATETIME
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_ACTNUM INT
DECLARE @M_FDRSNO INT
DECLARE @M_TXNNO INT
DECLARE @M_TDNO INT
DECLARE @M_ASSET CHAR(1)
DECLARE @M_ONAC CHAR(1)
DECLARE @M_ONACBAL MONEY
DECLARE @M_FDRBAL MONEY
DECLARE @M_FDRAMT MONEY
DECLARE @M_AMT1 MONEY
DECLARE @M_1 MONEY
DECLARE @M_2 MONEY
DECLARE @M_OLDTXNDT DATETIME
DECLARE @M_CLOSDATE DATETIME
declare @m_fillbalano int
DECLARE @M_TESTAMT MONEY
declare @actno numeric
declare @sql varchar(50)
if not exists(select * from deleted) return
select * into #deleted2 from deleted
select * into #inserted2 from inserted
select @host_name=computer from comuser where [host_id]=host_id()

set @m_actnum = @@rowcount
set @sql = str(@m_actnum) + ' record count'
set @error = 'Update Trigger Failed in Voucher Table'

--print 'ennai kaapavan periyan-avan mel enakku niraya nambikkai undu....en kaalathirku piraku yaar in the msg pathaalum belive it-26 Jan 2003'
set nocount on
declare @err varchar(500)
declare @con_id char(20)
declare @amount money
declare @txndt datetime
declare @amttyp char(1)
declare @actnum numeric
declare @company int
declare @fy int
declare @txnno numeric
declare @partic_con varchar(100)
declare  @partic_con1 varchar(100)
declare @txnno1 numeric
declare @amount1 money
declare @con_id1 char(20)
declare @txndt1 datetime
declare @amttyp1 char(1)
declare @actnum1 numeric
declare @company1 int
declare @fy1 int
declare @grno numeric
--if @@rowcount > 1 return
declare @cashboxuser char(1)
declare @cashbox numeric
declare @cashboxno numeric
declare @cbtxndt datetime
declare @partic varchar(200)
declare @usernm2 varchar(20)
declare @chlno_rtxnno numeric
declare @chlno_rtxnno1 numeric
declare @totamt_balance1 numeric(18,2)
declare @totamt_balance numeric(18,2)
select @txnno=txnno,@amount=amt,  @txndt=txndt, @amttyp=amttyp, @actnum=actnum, @company=company,@fy=fy,@chlno_rtxnno=chlno_RTXNNO,@totamt_balance=totamt_balance   from #deleted2
SELECT @M_TDNO=TDNO,@M_ASSET=ASSET,@M_ONAC=ONAC  FROM GLPRIMLD WHERE COMPANY=@COMPANY AND FY = @FY AND CGP_ACTNO=@M_ACTNUM
if (@m_asset='D' or @m_asset='C') and @totamt_balance<>@amount
begin
set @error='The SUBGL account seems already Reversed - from vouchers - new for credits/debits (update)'
goto e_error
end

select @usernm2=[user],@grno=grno,@txnno1=txnno, @amount1=amt , @txndt1=txndt, @amttyp1=amttyp, @actnum1=actnum, @company1=company,@fy1=fy,@partic=partic,@chlno_rtxnno1=chlno_RTXNNO,@totamt_balance1=totamt_balance from #inserted2
if isnull(@fy1,0)=0 begin set @error='Error on Updating Vouchers' goto e_error end




if exists(select  * from vouchers where company=@COMPANY and txnno=@chlno_rtxnno1 and amt<>@amount1) 
	begin
	set @error='Invalid amount for that entry to reverse - in vouchers' 
	goto e_error
	end

if @totamt_balance1 < 0 begin set @error='posted voucher amount is not matching the balance' goto e_error end
update vouchers set ltxndt=getdate() where  fy=@fy1 and txndt=@txndt1 and grno=@grno
if @amount1 < 0  begin rollback raiserror('Amount Invalid',16,-1) return end
if @amount < 0  begin rollback raiserror('Amount Invalid',16,-1) return end
---- block update
select @txndt=txndt,@fy=fy from #deleted2


if exists(select * from finyear where fy=@fy and blocked='Y')
begin
set @error =  'Financial Year Blocked - No Transactions Alllowed'
rollback transaction
raiserror(@error,16,-1)
end
if exists(select * from finyear where fy=@fy and datediff(d,'1/1/80',@txndt) <= datediff(d,'1/1/80',blockdate))
begin
set @error =  'Transactions blocked for this date - No Transactions Alllowed'
rollback transaction
raiserror(@error,16,-1)
end



select @txndt=txndt,@fy=fy from #inserted2
if exists(select * from finyear where fy=@fy and blocked='Y')
begin
set @error =  'Financial Year Blocked - No Transactions Alllowed'
rollback transaction
raiserror(@error,16,-1)
end
if exists(select * from finyear where fy=@fy and datediff(d,'1/1/80',@txndt) <= datediff(d,'1/1/80',blockdate))
begin
set @error = 'Transactions blocked for this date - No Transactions Alllowed'
rollback transaction
raiserror(@error,16,-1)
end
--cashboook update
declare @usernm varchar(50)
declare @amt money
declare @msg varchar(500)
select @usernm=usernm from comuser where [host_id]=host_id()

     select @actnum=actnum,@amt=amt,@amttyp=amttyp,@txndt=txndt from #deleted2


     if @actnum= 1 
		 begin
			 if @amttyp = 'D'
				begin
					update cashbook set amount=amount - @amt where cname=@host_name and uname=@usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
				end
			 else
				begin
					update cashbook set amount=amount + @amt where cname=@host_name and uname=@usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
				end
		 end
     select @actnum=actnum,@amt=amt,@amttyp=amttyp,@txndt=txndt from #inserted2
	if @actnum= 1 
		begin
			if not exists(select * from cashbook where cname = @host_name and uname = @usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) )
				begin
				insert into cashbook (cname,uname,txndt,amount) values (@host_name,@usernm,@txndt,0)        
				end
			if @amttyp = 'D'
				begin
				update cashbook set amount=amount + @amt where cname=@host_name and uname=@usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
				end
			else
				begin
				update cashbook set amount=amount -  @amt where cname=@host_name and uname=@usernm and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) 
				end

-- This portion is inserted for maintaining Cash Box 03-01-2009 Saturday FGP2

	select @usernm=usernm,@cashboxuser=cashbox from comuser where computer=@host_name
    select @cashboxno=cashboxno from cashboxuser where usernm=@usernm and status='Y'
	select @cashbox=g_cashbox from setting 
	if @cashbox = 1 
		if exists(select * from cashboxhist where cashboxno=@cashboxno and txnno=@txnno and usernm=@usernm2) 
			begin
			update cashboxhist set amt=@amount1 where txnno=@txnno and cashboxno=@cashboxno
			end
		else
	if @cashboxuser='Y'
		begin
			if exists(select * from cashboxuser where computername=@host_name and usernm=@usernm and status<>'N' )
				begin
				select @cashboxno=cashboxno from cashboxuser where computername=@host_name and usernm=@usernm and status<>'N'
					if @amttyp='D'
						if not exists(select * from cashboxhist where txnno=@txnno and cashboxno=@cashboxno) 
							insert into cashboxhist (txndt,cashboxno,amttyp,amt,partic,txnno,computername,usernm) values (@txndt,@cashboxno,@amttyp,@amount,@partic,@txnno,@host_name,@usernm)
						else
							update cashboxhist set amt=@amount1 where txnno=@txnno and cashboxno=@cashboxno
					else
							update cashboxhist set amt=@amount1 where txnno=@txnno and cashboxno=@cashboxno
				end
			else
				if exists(select * from cashboxuser where computername=@host_name and usernm<>@usernm and status<>'N' )
					set @error =  'The Cash Box is used by another user'
				else
					if not exists(select * from cashboxuser where computername=@host_name and  status<>'N' )
					begin
					set @error =  'The Cash Box is not opened..No cash Transactions allowed from this terminal'	
					goto e_error
					return
					end
		end  ---222
		else
				begin
				if exists(select * from cashboxuser where computername=@host_name and usernm<>@usernm and status<>'N' )
				begin
					select @cashboxno=cashboxno from cashboxuser where computername=@host_name and status<>'N'
					select @cbtxndt=txndt from cashbox where ctrlno=@cashboxno
					if datediff(d,@txndt,@cbtxndt) >=0
						begin   
						set @error =  'The Cash Box is used by another user..No cash Transactions allowed'
						goto e_error
						end
				end
				else
					begin
					if exists(select * from cashboxhist where usernm=@usernm2 and txnno=@txnno)
					begin
					select	@cashboxno=cashboxno from cashboxhist where usernm=@usernm2 and txnno=@txnno
						update cashboxhist set amt=@amount1 where txnno=@txnno and cashboxno=@cashboxno

					end
					else
					begin
					set @error = 'Cash Box is not opened ..No cash Transactions Allowed'
					goto e_error
					end
					end
				end

		------- cashbox maintanence ends here
end

SELECT @M_AMT=AMT,@M_AMTTYP=AMTTYP,@M_TXNDT=TXNDT,@M_COMPANY=COMPANY,@M_FY=FY,@M_ACTNUM=ACTNUM,@M_FDRSNO=FDRSNO,@M_TXNNO=TXNNO FROM #DELETED2
SET @ACTNO=@M_ACTNUM
IF @M_ACTNUM=0 RETURN
IF @M_AMT<=0 GOTO LABELUPDATE
set @m_fillbalano = @m_actnum
----
set @m_amt1 = @m_amt 
IF @M_AMTTYP='C'
SELECT @M_AMT=@M_AMT*-1
---
UPDATE GLPRIMLD SET BALANCE=BALANCE+@M_AMT,LTXNDT= CASE WHEN datediff(d,'1/1/90',@M_TXNDT)<=datediff(d,'1/1/90',ltxndt)  THEN @M_TXNDT ELSE LTXNDT END ,
DEBIT1  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=1  THEN DEBIT1 -(@M_AMT1) ELSE DEBIT1 END ELSE DEBIT1   END,
DEBIT2  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=2  THEN DEBIT2 -(@M_AMT1) ELSE DEBIT2 END ELSE DEBIT2   END,
DEBIT3  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=3  THEN DEBIT3 -(@M_AMT1) ELSE DEBIT3 END ELSE DEBIT3   END,
DEBIT4  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=4  THEN DEBIT4 -(@M_AMT1) ELSE DEBIT4 END ELSE DEBIT4   END,
DEBIT5  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=5  THEN DEBIT5 -(@M_AMT1) ELSE DEBIT5 END ELSE DEBIT5   END,
DEBIT6  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=6  THEN DEBIT6 -(@M_AMT1) ELSE DEBIT6 END ELSE DEBIT6   END,
DEBIT7  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=7  THEN DEBIT7 -(@M_AMT1) ELSE DEBIT7 END ELSE DEBIT7    END,
DEBIT8  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=8  THEN DEBIT8 -(@M_AMT1) ELSE DEBIT8 END ELSE DEBIT8    END,
DEBIT9  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=9  THEN DEBIT9 -(@M_AMT1) ELSE DEBIT9 END ELSE DEBIT9    END,
DEBIT10 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=10 THEN DEBIT10-(@M_AMT1) ELSE DEBIT10 END ELSE DEBIT10  END,
DEBIT11 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=11 THEN DEBIT11-(@M_AMT1) ELSE DEBIT11 END ELSE DEBIT11  END,
DEBIT12 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=12 THEN DEBIT12-(@M_AMT1) ELSE DEBIT12 END ELSE DEBIT12  END,
CREDIT1  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=1  THEN CREDIT1- (@M_AMT1) ELSE CREDIT1 END ELSE CREDIT1   END,
CREDIT2  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=2  THEN CREDIT2- (@M_AMT1) ELSE CREDIT2 END ELSE CREDIT2   END,
CREDIT3  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=3  THEN CREDIT3- (@M_AMT1) ELSE CREDIT3 END ELSE CREDIT3   END,
CREDIT4  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=4  THEN CREDIT4- (@M_AMT1) ELSE CREDIT4 END ELSE CREDIT4   END,
CREDIT5  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=5  THEN CREDIT5- (@M_AMT1) ELSE CREDIT5 END ELSE CREDIT5   END,
CREDIT6  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=6  THEN CREDIT6- (@M_AMT1) ELSE CREDIT6 END ELSE CREDIT6   END,
CREDIT7  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=7  THEN CREDIT7- (@M_AMT1) ELSE CREDIT7 END ELSE CREDIT7    END,
CREDIT8  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=8  THEN CREDIT8- (@M_AMT1) ELSE CREDIT8 END ELSE CREDIT8    END,
CREDIT9  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=9  THEN CREDIT9- (@M_AMT1) ELSE CREDIT9 END ELSE CREDIT9    END,
CREDIT10 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=10 THEN CREDIT10-(@M_AMT1) ELSE CREDIT10 END ELSE CREDIT10  END,
CREDIT11 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=11 THEN CREDIT11-(@M_AMT1) ELSE CREDIT11 END ELSE CREDIT11  END,
CREDIT12 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=12 THEN CREDIT12-(@M_AMT1) ELSE CREDIT12 END ELSE CREDIT12  END
WHERE COMPANY=@M_COMPANY AND FY = @M_FY AND CGP_ACTNO=@M_ACTNUM
/**/
IF @@ERROR <> 0 GOTO E_ERROR

SELECT @M_TDNO=TDNO,@M_ASSET=ASSET,@M_ONAC=ONAC  FROM GLPRIMLD WHERE COMPANY=@M_COMPANY AND FY = @M_FY AND CGP_ACTNO=@M_ACTNUM

--PRINT 'DONE' -- @@ROWCOUNT
---repairing done 19 apr 2020
set @m_amt1 = 0
IF @M_AMTTYP='C'
SELECT @M_AMT=@M_AMT*-1

SELECT @M_TDNO=TDNO,@M_ASSET=ASSET,@M_ONAC=ONAC  FROM GLPRIMLD WHERE COMPANY=@M_COMPANY AND FY = @M_FY AND CGP_ACTNO=@M_ACTNUM


if not (update(totamt_balance)  or update(chlno_rtxnno) or update(billdate_closedate))

LABELUPDATE:
DECLARE @M_PARTIC VARCHAR(100)
-------*************
SELECT @M_AMT=AMT,@M_AMTTYP=AMTTYP,@M_TXNDT=TXNDT,@M_COMPANY=COMPANY,@M_FY=FY,@M_ACTNUM=ACTNUM,@M_PARTIC=PARTIC,@M_FDRSNO=FDRSNO,@M_TXNNO=TXNNO FROM #INSERTED2
IF @M_AMT<=0 RETURN
set @m_fillbalano = @m_actnum
set @m_amt1 = @m_Amt
IF @M_AMTTYP='D'
SELECT @M_AMT=@M_AMT*-1

set @fy=@m_fy
IF NOT EXISTS(SELECT * FROM GLPRIMLD WHERE COMPANY=@M_COMPANY AND FY = @FY AND CGP_ACTNO=@M_ACTNUM) 
BEGIN
	set @error=  'GL A/C NUMBER GIVEN ' + str(isnull(@m_actnum,0)) + ' ' + LTRIM(STR(@M_ACTNUM)) + ' IS NOT FOUND IN THE MASTER FILE...CONTACT POWERBRAIN'
    goto e_error
END

UPDATE GLPRIMLD SET BALANCE=BALANCE+@M_AMT,LTXNDT=case when datediff(d,'1/1/90',ltxndt) >= datediff(d,'1/1/90',@m_txndt) then dateadd(d,-2,@m_txndt) else ltxndt end,TXNNO=@M_TXNNO,
DEBIT1  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=1  THEN DEBIT1 +(@M_AMT1) ELSE DEBIT1 END ELSE DEBIT1   END,
DEBIT2  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=2  THEN DEBIT2 +(@M_AMT1) ELSE DEBIT2 END ELSE DEBIT2   END,
DEBIT3  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=3  THEN DEBIT3 +(@M_AMT1) ELSE DEBIT3 END ELSE DEBIT3   END,
DEBIT4  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=4  THEN DEBIT4 +(@M_AMT1) ELSE DEBIT4 END ELSE DEBIT4   END,
DEBIT5  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=5  THEN DEBIT5 +(@M_AMT1) ELSE DEBIT5 END ELSE DEBIT5   END,
DEBIT6  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=6  THEN DEBIT6 +(@M_AMT1) ELSE DEBIT6 END ELSE DEBIT6   END,
DEBIT7  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=7  THEN DEBIT7 +(@M_AMT1) ELSE DEBIT7 END ELSE DEBIT7    END,
DEBIT8  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=8  THEN DEBIT8 +(@M_AMT1) ELSE DEBIT8 END ELSE DEBIT8    END,
DEBIT9  = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=9  THEN DEBIT9 +(@M_AMT1) ELSE DEBIT9 END ELSE DEBIT9    END,
DEBIT10 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=10 THEN DEBIT10+(@M_AMT1) ELSE DEBIT10 END ELSE DEBIT10  END,
DEBIT11 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=11 THEN DEBIT11+(@M_AMT1) ELSE DEBIT11 END ELSE DEBIT11  END,
DEBIT12 = CASE WHEN  @M_AMTTYP='D' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=12 THEN DEBIT12+(@M_AMT1) ELSE DEBIT12 END ELSE DEBIT12  END,
CREDIT1  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=1  THEN CREDIT1+ (@M_AMT1) ELSE CREDIT1 END ELSE CREDIT1   END,
CREDIT2  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=2  THEN CREDIT2+ (@M_AMT1) ELSE CREDIT2 END ELSE CREDIT2   END,
CREDIT3  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=3  THEN CREDIT3+ (@M_AMT1) ELSE CREDIT3 END ELSE CREDIT3   END,
CREDIT4  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=4  THEN CREDIT4+ (@M_AMT1) ELSE CREDIT4 END ELSE CREDIT4   END,
CREDIT5  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=5  THEN CREDIT5+ (@M_AMT1) ELSE CREDIT5 END ELSE CREDIT5   END,
CREDIT6  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=6  THEN CREDIT6+ (@M_AMT1) ELSE CREDIT6 END ELSE CREDIT6   END,
CREDIT7  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=7  THEN CREDIT7+ (@M_AMT1) ELSE CREDIT7 END ELSE CREDIT7    END,
CREDIT8  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=8  THEN CREDIT8+ (@M_AMT1) ELSE CREDIT8 END ELSE CREDIT8    END,
CREDIT9  = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=9  THEN CREDIT9+ (@M_AMT1) ELSE CREDIT9 END ELSE CREDIT9    END,
CREDIT10 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=10 THEN CREDIT10+(@M_AMT1) ELSE CREDIT10 END ELSE CREDIT10  END,
CREDIT11 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=11 THEN CREDIT11+(@M_AMT1) ELSE CREDIT11 END ELSE CREDIT11  END,
CREDIT12 = CASE WHEN  @M_AMTTYP='C' THEN CASE WHEN DATEPART(mm,@M_TXNDT)=12 THEN CREDIT12+(@M_AMT1) ELSE CREDIT12 END ELSE CREDIT12  END
WHERE COMPANY=@M_COMPANY AND FY = @M_FY AND CGP_ACTNO=@M_ACTNUM
IF @@ERROR <> 0 GOTO E_ERROR

return  
E_ERROR:
ROLLBACK
raiserror(@error,16,-1)
GO
ALTER TABLE [dbo].[VOUCHERS] ENABLE TRIGGER [vou_update]
GO
/****** Object:  Trigger [dbo].[vougrno]    Script Date: 07/04/2026 20:30:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[vougrno]    ON  [dbo].[VOUCHERS]   
AFTER INSERT AS 
BEGIN 	
SET NOCOUNT ON; 	
declare @grno numeric 	
select @grno=grno from inserted 
if isnull(@grno,0)=0 
begin 
rollback transaction 
raiserror ('Error in Voucher Posting : Zero GRNO',16,-1) 
return 
end END
GO
ALTER TABLE [dbo].[VOUCHERS] ENABLE TRIGGER [vougrno]
GO
