/****** Object:  StoredProcedure [dbo].[updtSttableR]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updtSttableR]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updtSttableR]
GO
/****** Object:  StoredProcedure [dbo].[updtSttable]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updtSttable]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updtSttable]
GO
/****** Object:  StoredProcedure [dbo].[updtrentperiod]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updtrentperiod]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updtrentperiod]
GO
/****** Object:  StoredProcedure [dbo].[updtrentmaster]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updtrentmaster]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updtrentmaster]
GO
/****** Object:  StoredProcedure [dbo].[updtfy]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updtfy]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updtfy]
GO
/****** Object:  StoredProcedure [dbo].[updatexxx123]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updatexxx123]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updatexxx123]
GO
/****** Object:  StoredProcedure [dbo].[updatevattabler]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updatevattabler]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updatevattabler]
GO
/****** Object:  StoredProcedure [dbo].[UPDATEstaahINV]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UPDATEstaahINV]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[UPDATEstaahINV]
GO
/****** Object:  StoredProcedure [dbo].[updaterentaccount]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updaterentaccount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updaterentaccount]
GO
/****** Object:  StoredProcedure [dbo].[UPDATELTONTARIFF]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UPDATELTONTARIFF]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[UPDATELTONTARIFF]
GO
/****** Object:  StoredProcedure [dbo].[updatelic]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updatelic]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updatelic]
GO
/****** Object:  StoredProcedure [dbo].[updateledgerforVB6]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updateledgerforVB6]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[updateledgerforVB6]
GO
/****** Object:  StoredProcedure [dbo].[UPDATEINPUTAC]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UPDATEINPUTAC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[UPDATEINPUTAC]
GO
/****** Object:  StoredProcedure [dbo].[UpdateGLCRDR]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UpdateGLCRDR]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[UpdateGLCRDR]
GO
/****** Object:  StoredProcedure [dbo].[TTSplit]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TTSplit]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TTSplit]
GO
/****** Object:  StoredProcedure [dbo].[TallyGLPRIMLD1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TallyGLPRIMLD1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TallyGLPRIMLD1]
GO
/****** Object:  StoredProcedure [dbo].[TallyGLPRIMLD]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TallyGLPRIMLD]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TallyGLPRIMLD]
GO
/****** Object:  StoredProcedure [dbo].[ta_updtperiodtable]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ta_updtperiodtable]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ta_updtperiodtable]
GO
/****** Object:  StoredProcedure [dbo].[ta_packagev]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ta_packagev]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ta_packagev]
GO
/****** Object:  StoredProcedure [dbo].[TA_ItineraryInsert]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TA_ItineraryInsert]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TA_ItineraryInsert]
GO
/****** Object:  StoredProcedure [dbo].[TA_EditAdvance]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TA_EditAdvance]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TA_EditAdvance]
GO
/****** Object:  StoredProcedure [dbo].[ta_deletebill]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ta_deletebill]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ta_deletebill]
GO
/****** Object:  StoredProcedure [dbo].[TA_DELENTRY]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TA_DELENTRY]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TA_DELENTRY]
GO
/****** Object:  StoredProcedure [dbo].[TA_DelAdvance]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TA_DelAdvance]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TA_DelAdvance]
GO
/****** Object:  StoredProcedure [dbo].[TA_billing]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TA_billing]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TA_billing]
GO
/****** Object:  StoredProcedure [dbo].[ta_advvp]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ta_advvp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ta_advvp]
GO
/****** Object:  StoredProcedure [dbo].[TA_AdvanceReceipt]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TA_AdvanceReceipt]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[TA_AdvanceReceipt]
GO
/****** Object:  StoredProcedure [dbo].[super1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[super1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[super1]
GO
/****** Object:  StoredProcedure [dbo].[super]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[super]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[super]
GO
/****** Object:  StoredProcedure [dbo].[stttda]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[stttda]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[stttda]
GO
/****** Object:  StoredProcedure [dbo].[ststipdate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ststipdate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ststipdate]
GO
/****** Object:  StoredProcedure [dbo].[storeupdate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[storeupdate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[storeupdate]
GO
/****** Object:  StoredProcedure [dbo].[storenewyear]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[storenewyear]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[storenewyear]
GO
/****** Object:  StoredProcedure [dbo].[storeinitfy]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[storeinitfy]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[storeinitfy]
GO
/****** Object:  StoredProcedure [dbo].[storecreateac]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[storecreateac]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[storecreateac]
GO
/****** Object:  StoredProcedure [dbo].[StaahRateUpdate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[StaahRateUpdate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[StaahRateUpdate]
GO
/****** Object:  StoredProcedure [dbo].[spupsgla]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spupsgla]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spupsgla]
GO
/****** Object:  StoredProcedure [dbo].[spupdusr]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spupdusr]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spupdusr]
GO
/****** Object:  StoredProcedure [dbo].[spupdde]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spupdde]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spupdde]
GO
/****** Object:  StoredProcedure [dbo].[spupdd]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spupdd]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spupdd]
GO
/****** Object:  StoredProcedure [dbo].[spudob]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spudob]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spudob]
GO
/****** Object:  StoredProcedure [dbo].[spttSplit]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spttSplit]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spttSplit]
GO
/****** Object:  StoredProcedure [dbo].[sptted1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptted1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptted1]
GO
/****** Object:  StoredProcedure [dbo].[spttdc]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spttdc]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spttdc]
GO
/****** Object:  StoredProcedure [dbo].[spttar]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spttar]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spttar]
GO
/****** Object:  StoredProcedure [dbo].[spttap]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spttap]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spttap]
GO
/****** Object:  StoredProcedure [dbo].[sptt3vpt]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt3vpt]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt3vpt]
GO
/****** Object:  StoredProcedure [dbo].[SPTT3VPC]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPTT3VPC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPTT3VPC]
GO
/****** Object:  StoredProcedure [dbo].[sptt3crv]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt3crv]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt3crv]
GO
/****** Object:  StoredProcedure [dbo].[sptt3cl]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt3cl]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt3cl]
GO
/****** Object:  StoredProcedure [dbo].[sptt3]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt3]
GO
/****** Object:  StoredProcedure [dbo].[SPTT2VPT]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPTT2VPT]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPTT2VPT]
GO
/****** Object:  StoredProcedure [dbo].[SPTT2VPC]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPTT2VPC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPTT2VPC]
GO
/****** Object:  StoredProcedure [dbo].[sptt2E]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2E]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2E]
GO
/****** Object:  StoredProcedure [dbo].[sptt2crv]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2crv]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2crv]
GO
/****** Object:  StoredProcedure [dbo].[sptt2cl]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2cl]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2cl]
GO
/****** Object:  StoredProcedure [dbo].[sptt2canrv]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2canrv]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2canrv]
GO
/****** Object:  StoredProcedure [dbo].[sptt2canpv]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2canpv]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2canpv]
GO
/****** Object:  StoredProcedure [dbo].[sptt2candelete]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2candelete]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2candelete]
GO
/****** Object:  StoredProcedure [dbo].[sptt2canchgsrv]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2canchgsrv]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2canchgsrv]
GO
/****** Object:  StoredProcedure [dbo].[sptt2canchgsr]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2canchgsr]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2canchgsr]
GO
/****** Object:  StoredProcedure [dbo].[sptt2canchgspv]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2canchgspv]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2canchgspv]
GO
/****** Object:  StoredProcedure [dbo].[sptt2canchgsp]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2canchgsp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2canchgsp]
GO
/****** Object:  StoredProcedure [dbo].[sptt2can]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2can]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2can]
GO
/****** Object:  StoredProcedure [dbo].[sptt21]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt21]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt21]
GO
/****** Object:  StoredProcedure [dbo].[sptt2]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt2]
GO
/****** Object:  StoredProcedure [dbo].[SPTT1VPT]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPTT1VPT]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPTT1VPT]
GO
/****** Object:  StoredProcedure [dbo].[SPTT1VPC]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPTT1VPC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPTT1VPC]
GO
/****** Object:  StoredProcedure [dbo].[sptt1crv]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt1crv]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt1crv]
GO
/****** Object:  StoredProcedure [dbo].[sptt1cl]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt1cl]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt1cl]
GO
/****** Object:  StoredProcedure [dbo].[sptt11]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt11]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt11]
GO
/****** Object:  StoredProcedure [dbo].[sptt1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptt1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptt1]
GO
/****** Object:  StoredProcedure [dbo].[sptcac_single]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptcac_single]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptcac_single]
GO
/****** Object:  StoredProcedure [dbo].[sptcac]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptcac]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptcac]
GO
/****** Object:  StoredProcedure [dbo].[sptbru]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbru]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbru]
GO
/****** Object:  StoredProcedure [dbo].[sptbrsblock]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbrsblock]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbrsblock]
GO
/****** Object:  StoredProcedure [dbo].[SPTBROOMSUpdate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPTBROOMSUpdate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPTBROOMSUpdate]
GO
/****** Object:  StoredProcedure [dbo].[SPTBRF]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPTBRF]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPTBRF]
GO
/****** Object:  StoredProcedure [dbo].[sptbos]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbos]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbos]
GO
/****** Object:  StoredProcedure [dbo].[SPTBNS]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPTBNS]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPTBNS]
GO
/****** Object:  StoredProcedure [dbo].[sptbcc_editns]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbcc_editns]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbcc_editns]
GO
/****** Object:  StoredProcedure [dbo].[SPTBCC]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPTBCC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPTBCC]
GO
/****** Object:  StoredProcedure [dbo].[sptbbqE]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbbqE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbbqE]
GO
/****** Object:  StoredProcedure [dbo].[sptbbqc]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbbqc]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbbqc]
GO
/****** Object:  StoredProcedure [dbo].[sptbbq]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbbq]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbbq]
GO
/****** Object:  StoredProcedure [dbo].[sptbbbe]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbbbe]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbbbe]
GO
/****** Object:  StoredProcedure [dbo].[sptbbbd]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbbbd]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbbbd]
GO
/****** Object:  StoredProcedure [dbo].[sptbbb]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbbb]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbbb]
GO
/****** Object:  StoredProcedure [dbo].[sptbadvd]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbadvd]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbadvd]
GO
/****** Object:  StoredProcedure [dbo].[sptbadv]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sptbadv]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sptbadv]
GO
/****** Object:  StoredProcedure [dbo].[SpStRewriteSTOpgbal]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SpStRewriteSTOpgbal]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SpStRewriteSTOpgbal]
GO
/****** Object:  StoredProcedure [dbo].[spstpure]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spstpure]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spstpure]
GO
/****** Object:  StoredProcedure [dbo].[spstpuradjnew]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spstpuradjnew]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spstpuradjnew]
GO
/****** Object:  StoredProcedure [dbo].[spstpuradj]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spstpuradj]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spstpuradj]
GO
/****** Object:  StoredProcedure [dbo].[spstpur]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spstpur]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spstpur]
GO
/****** Object:  StoredProcedure [dbo].[spstporE]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spstporE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spstporE]
GO
/****** Object:  StoredProcedure [dbo].[spstporC]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spstporC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spstporC]
GO
/****** Object:  StoredProcedure [dbo].[spstisse]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spstisse]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spstisse]
GO
/****** Object:  StoredProcedure [dbo].[spstissdir]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spstissdir]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spstissdir]
GO
/****** Object:  StoredProcedure [dbo].[spstiss]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spstiss]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spstiss]
GO
/****** Object:  StoredProcedure [dbo].[spsthrewrite]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spsthrewrite]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spsthrewrite]
GO
/****** Object:  StoredProcedure [dbo].[SPSTAdj]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPSTAdj]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPSTAdj]
GO
/****** Object:  StoredProcedure [dbo].[spshrm]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spshrm]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spshrm]
GO
/****** Object:  StoredProcedure [dbo].[sprsup1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sprsup1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sprsup1]
GO
/****** Object:  StoredProcedure [dbo].[sprsup]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sprsup]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sprsup]
GO
/****** Object:  StoredProcedure [dbo].[sprstent]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sprstent]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sprstent]
GO
/****** Object:  StoredProcedure [dbo].[sprda]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sprda]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sprda]
GO
/****** Object:  StoredProcedure [dbo].[spradve]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spradve]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spradve]
GO
/****** Object:  StoredProcedure [dbo].[spradv]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spradv]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spradv]
GO
/****** Object:  StoredProcedure [dbo].[spputstock]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spputstock]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spputstock]
GO
/****** Object:  StoredProcedure [dbo].[spppc]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spppc]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spppc]
GO
/****** Object:  StoredProcedure [dbo].[sponacm]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sponacm]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sponacm]
GO
/****** Object:  StoredProcedure [dbo].[sponacdel]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sponacdel]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sponacdel]
GO
/****** Object:  StoredProcedure [dbo].[sponac]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sponac]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sponac]
GO
/****** Object:  StoredProcedure [dbo].[spocg]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spocg]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spocg]
GO
/****** Object:  StoredProcedure [dbo].[SPNSED]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPNSED]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPNSED]
GO
/****** Object:  StoredProcedure [dbo].[spnsdel]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spnsdel]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spnsdel]
GO
/****** Object:  StoredProcedure [dbo].[spmiscolln]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spmiscolln]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spmiscolln]
GO
/****** Object:  StoredProcedure [dbo].[spltp2]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spltp2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spltp2]
GO
/****** Object:  StoredProcedure [dbo].[spltp]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spltp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spltp]
GO
/****** Object:  StoredProcedure [dbo].[spkblp_rsdiscount]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spkblp_rsdiscount]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spkblp_rsdiscount]
GO
/****** Object:  StoredProcedure [dbo].[spkblp]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spkblp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spkblp]
GO
/****** Object:  StoredProcedure [dbo].[spkblnewsingle]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spkblnewsingle]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spkblnewsingle]
GO
/****** Object:  StoredProcedure [dbo].[spkblnew]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spkblnew]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spkblnew]
GO
/****** Object:  StoredProcedure [dbo].[spkbl]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spkbl]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spkbl]
GO
/****** Object:  StoredProcedure [dbo].[SPHSNOCODE]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPHSNOCODE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SPHSNOCODE]
GO
/****** Object:  StoredProcedure [dbo].[spglno]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spglno]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spglno]
GO
/****** Object:  StoredProcedure [dbo].[spexroRes]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spexroRes]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spexroRes]
GO
/****** Object:  StoredProcedure [dbo].[spexro]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spexro]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spexro]
GO
/****** Object:  StoredProcedure [dbo].[speptr]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[speptr]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[speptr]
GO
/****** Object:  StoredProcedure [dbo].[spedmask]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spedmask]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spedmask]
GO
/****** Object:  StoredProcedure [dbo].[spedaac]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spedaac]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spedaac]
GO
/****** Object:  StoredProcedure [dbo].[spdrs]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spdrs]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spdrs]
GO
/****** Object:  StoredProcedure [dbo].[spdlpur]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spdlpur]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spdlpur]
GO
/****** Object:  StoredProcedure [dbo].[spdeg]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spdeg]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spdeg]
GO
/****** Object:  StoredProcedure [dbo].[spde]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spde]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spde]
GO
/****** Object:  StoredProcedure [dbo].[spcu]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spcu]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spcu]
GO
/****** Object:  StoredProcedure [dbo].[spchnnob]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spchnnob]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spchnnob]
GO
/****** Object:  StoredProcedure [dbo].[spchnno]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spchnno]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spchnno]
GO
/****** Object:  StoredProcedure [dbo].[spbls1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spbls1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spbls1]
GO
/****** Object:  StoredProcedure [dbo].[spbls]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spbls]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spbls]
GO
/****** Object:  StoredProcedure [dbo].[spautobill]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[spautobill]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[spautobill]
GO
/****** Object:  StoredProcedure [dbo].[sp_grd]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_grd]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_grd]
GO
/****** Object:  StoredProcedure [dbo].[sp_gco]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_gco]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_gco]
GO
/****** Object:  StoredProcedure [dbo].[sp_gcb]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_gcb]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_gcb]
GO
/****** Object:  StoredProcedure [dbo].[sp_cbdummy]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_cbdummy]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_cbdummy]
GO
/****** Object:  StoredProcedure [dbo].[sp_cb_test]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_cb_test]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_cb_test]
GO
/****** Object:  StoredProcedure [dbo].[sp_cb_fast]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_cb_fast]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_cb_fast]
GO
/****** Object:  StoredProcedure [dbo].[sp_cb]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_cb]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_cb]
GO
/****** Object:  StoredProcedure [dbo].[smsinfo]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[smsinfo]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[smsinfo]
GO
/****** Object:  StoredProcedure [dbo].[shiftf12]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[shiftf12]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[shiftf12]
GO
/****** Object:  StoredProcedure [dbo].[setserialno]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[setserialno]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[setserialno]
GO
/****** Object:  StoredProcedure [dbo].[SETOPGBAL]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SETOPGBAL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SETOPGBAL]
GO
/****** Object:  StoredProcedure [dbo].[setcat]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[setcat]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[setcat]
GO
/****** Object:  StoredProcedure [dbo].[ServicePermissionUpdate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ServicePermissionUpdate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ServicePermissionUpdate]
GO
/****** Object:  StoredProcedure [dbo].[servicebvouching]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[servicebvouching]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[servicebvouching]
GO
/****** Object:  StoredProcedure [dbo].[servicebdeleting]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[servicebdeleting]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[servicebdeleting]
GO
/****** Object:  StoredProcedure [dbo].[ServiceAVouching]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ServiceAVouching]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ServiceAVouching]
GO
/****** Object:  StoredProcedure [dbo].[serviceadeleting]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[serviceadeleting]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[serviceadeleting]
GO
/****** Object:  StoredProcedure [dbo].[seradj]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[seradj]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[seradj]
GO
/****** Object:  StoredProcedure [dbo].[saleadj2]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[saleadj2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[saleadj2]
GO
/****** Object:  StoredProcedure [dbo].[saleadj111]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[saleadj111]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[saleadj111]
GO
/****** Object:  StoredProcedure [dbo].[saleadj11]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[saleadj11]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[saleadj11]
GO
/****** Object:  StoredProcedure [dbo].[saleadj1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[saleadj1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[saleadj1]
GO
/****** Object:  StoredProcedure [dbo].[SALEADJ]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SALEADJ]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[SALEADJ]
GO
/****** Object:  StoredProcedure [dbo].[REWRITE_new]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[REWRITE_new]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[REWRITE_new]
GO
/****** Object:  StoredProcedure [dbo].[REWRITE]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[REWRITE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[REWRITE]
GO
/****** Object:  StoredProcedure [dbo].[RemoveSplit]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RemoveSplit]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[RemoveSplit]
GO
/****** Object:  StoredProcedure [dbo].[removeduplicatesfromlinker]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[removeduplicatesfromlinker]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[removeduplicatesfromlinker]
GO
/****** Object:  StoredProcedure [dbo].[RemoveColumnWithDefaultConstraints]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RemoveColumnWithDefaultConstraints]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[RemoveColumnWithDefaultConstraints]
GO
/****** Object:  StoredProcedure [dbo].[RANDFILE]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[RANDFILE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[RANDFILE]
GO
/****** Object:  StoredProcedure [dbo].[QRNO]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[QRNO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[QRNO]
GO
/****** Object:  StoredProcedure [dbo].[qcheckres]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[qcheckres]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[qcheckres]
GO
/****** Object:  StoredProcedure [dbo].[PrePaidStockPostingProc]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PrePaidStockPostingProc]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PrePaidStockPostingProc]
GO
/****** Object:  StoredProcedure [dbo].[PrePaidStockPosting]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PrePaidStockPosting]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PrePaidStockPosting]
GO
/****** Object:  StoredProcedure [dbo].[pproomdue]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pproomdue]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pproomdue]
GO
/****** Object:  StoredProcedure [dbo].[PermitKOt]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PermitKOt]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PermitKOt]
GO
/****** Object:  StoredProcedure [dbo].[PbVC11]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PbVC11]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PbVC11]
GO
/****** Object:  StoredProcedure [dbo].[PbVC]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PbVC]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PbVC]
GO
/****** Object:  StoredProcedure [dbo].[pbspeb]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbspeb]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbspeb]
GO
/****** Object:  StoredProcedure [dbo].[pbsgetsac_NEW]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbsgetsac_NEW]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbsgetsac_NEW]
GO
/****** Object:  StoredProcedure [dbo].[pbsgetsac]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbsgetsac]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbsgetsac]
GO
/****** Object:  StoredProcedure [dbo].[pbpv1u]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpv1u]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpv1u]
GO
/****** Object:  StoredProcedure [dbo].[pbpt3_0503]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpt3_0503]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpt3_0503]
GO
/****** Object:  StoredProcedure [dbo].[pbpt0503]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpt0503]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpt0503]
GO
/****** Object:  StoredProcedure [dbo].[pbpsdelcomm]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpsdelcomm]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpsdelcomm]
GO
/****** Object:  StoredProcedure [dbo].[pbpscommpay]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpscommpay]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpscommpay]
GO
/****** Object:  StoredProcedure [dbo].[pbpscommEdit]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpscommEdit]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpscommEdit]
GO
/****** Object:  StoredProcedure [dbo].[pbpsb]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpsb]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpsb]
GO
/****** Object:  StoredProcedure [dbo].[pbpp1u_co]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpp1u_co]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpp1u_co]
GO
/****** Object:  StoredProcedure [dbo].[pbpp1u]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpp1u]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpp1u]
GO
/****** Object:  StoredProcedure [dbo].[pbpp1508]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpp1508]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpp1508]
GO
/****** Object:  StoredProcedure [dbo].[pbpp1106]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpp1106]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpp1106]
GO
/****** Object:  StoredProcedure [dbo].[pbpp0503]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpp0503]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpp0503]
GO
/****** Object:  StoredProcedure [dbo].[pbpma2]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpma2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpma2]
GO
/****** Object:  StoredProcedure [dbo].[pbpma1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpma1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpma1]
GO
/****** Object:  StoredProcedure [dbo].[pbpltNEW]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpltNEW]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpltNEW]
GO
/****** Object:  StoredProcedure [dbo].[pbplt2015]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbplt2015]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbplt2015]
GO
/****** Object:  StoredProcedure [dbo].[pbplt]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbplt]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbplt]
GO
/****** Object:  StoredProcedure [dbo].[pbpkb2uko_multi]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpkb2uko_multi]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpkb2uko_multi]
GO
/****** Object:  StoredProcedure [dbo].[pbpkb2uko]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpkb2uko]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpkb2uko]
GO
/****** Object:  StoredProcedure [dbo].[pbpkb1u_co]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpkb1u_co]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpkb1u_co]
GO
/****** Object:  StoredProcedure [dbo].[pbpkb1u]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpkb1u]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpkb1u]
GO
/****** Object:  StoredProcedure [dbo].[pbpk1Bill]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpk1Bill]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpk1Bill]
GO
/****** Object:  StoredProcedure [dbo].[pbpk1508]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpk1508]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpk1508]
GO
/****** Object:  StoredProcedure [dbo].[pbpk1106]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpk1106]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpk1106]
GO
/****** Object:  StoredProcedure [dbo].[pbpk0503]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpk0503]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpk0503]
GO
/****** Object:  StoredProcedure [dbo].[pbpga7]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga7]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpga7]
GO
/****** Object:  StoredProcedure [dbo].[pbpga6]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga6]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpga6]
GO
/****** Object:  StoredProcedure [dbo].[pbpga5]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga5]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpga5]
GO
/****** Object:  StoredProcedure [dbo].[pbpga3]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga3]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpga3]
GO
/****** Object:  StoredProcedure [dbo].[pbpga2]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga2]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpga2]
GO
/****** Object:  StoredProcedure [dbo].[pbpga1new]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga1new]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpga1new]
GO
/****** Object:  StoredProcedure [dbo].[pbpga1_hourly]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga1_hourly]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpga1_hourly]
GO
/****** Object:  StoredProcedure [dbo].[pbpga1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpga1]
GO
/****** Object:  StoredProcedure [dbo].[pbpga]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpga]
GO
/****** Object:  StoredProcedure [dbo].[pbpe1u]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpe1u]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpe1u]
GO
/****** Object:  StoredProcedure [dbo].[PBPChargeCard]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[PBPChargeCard]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[PBPChargeCard]
GO
/****** Object:  StoredProcedure [dbo].[pbpc4dco]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpc4dco]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpc4dco]
GO
/****** Object:  StoredProcedure [dbo].[pbpc3cm]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpc3cm]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpc3cm]
GO
/****** Object:  StoredProcedure [dbo].[pbpc2igUSELESS]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpc2igUSELESS]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpc2igUSELESS]
GO
/****** Object:  StoredProcedure [dbo].[pbpc2ig]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpc2ig]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpc2ig]
GO
/****** Object:  StoredProcedure [dbo].[pbpc1o]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpc1o]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpc1o]
GO
/****** Object:  StoredProcedure [dbo].[pbpc1iUSELESS]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpc1iUSELESS]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpc1iUSELESS]
GO
/****** Object:  StoredProcedure [dbo].[pbpc1iq]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpc1iq]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpc1iq]
GO
/****** Object:  StoredProcedure [dbo].[pbpc1i]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpc1i]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpc1i]
GO
/****** Object:  StoredProcedure [dbo].[pbpa1u]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpa1u]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpa1u]
GO
/****** Object:  StoredProcedure [dbo].[pbpa1508]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpa1508]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpa1508]
GO
/****** Object:  StoredProcedure [dbo].[pbpa1106]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpa1106]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[pbpa1106]
GO
/****** Object:  StoredProcedure [dbo].[OTACommEntry]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OTACommEntry]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[OTACommEntry]
GO
/****** Object:  StoredProcedure [dbo].[Online24Posting]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Online24Posting]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Online24Posting]
GO
/****** Object:  StoredProcedure [dbo].[onetimeupdateforoutletnumering]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[onetimeupdateforoutletnumering]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[onetimeupdateforoutletnumering]
GO
/****** Object:  StoredProcedure [dbo].[ONACREVulta]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ONACREVulta]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ONACREVulta]
GO
/****** Object:  StoredProcedure [dbo].[ONACREV]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ONACREV]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ONACREV]
GO
/****** Object:  StoredProcedure [dbo].[ONACAutoReverse]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ONACAutoReverse]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ONACAutoReverse]
GO
/****** Object:  StoredProcedure [dbo].[OL24Posting]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OL24Posting]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[OL24Posting]
GO
/****** Object:  StoredProcedure [dbo].[newlic]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[newlic]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[newlic]
GO
/****** Object:  StoredProcedure [dbo].[miscashbox]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[miscashbox]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[miscashbox]
GO
/****** Object:  StoredProcedure [dbo].[initsuppglac]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[initsuppglac]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[initsuppglac]
GO
/****** Object:  StoredProcedure [dbo].[initguest]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[initguest]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[initguest]
GO
/****** Object:  StoredProcedure [dbo].[gupernewdesign]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[gupernewdesign]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[gupernewdesign]
GO
/****** Object:  StoredProcedure [dbo].[gupernew]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[gupernew]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[gupernew]
GO
/****** Object:  StoredProcedure [dbo].[guper]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[guper]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[guper]
GO
/****** Object:  StoredProcedure [dbo].[grpred]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[grpred]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[grpred]
GO
/****** Object:  StoredProcedure [dbo].[grpdetails]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[grpdetails]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[grpdetails]
GO
/****** Object:  StoredProcedure [dbo].[gptemp]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[gptemp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[gptemp]
GO
/****** Object:  StoredProcedure [dbo].[GLOPGBAL]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GLOPGBAL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GLOPGBAL]
GO
/****** Object:  StoredProcedure [dbo].[GetST]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetST]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetST]
GO
/****** Object:  StoredProcedure [dbo].[GetServiceTaxR1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetServiceTaxR1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetServiceTaxR1]
GO
/****** Object:  StoredProcedure [dbo].[GetServiceTaxR]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetServiceTaxR]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetServiceTaxR]
GO
/****** Object:  StoredProcedure [dbo].[GetServiceASno]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetServiceASno]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetServiceASno]
GO
/****** Object:  StoredProcedure [dbo].[GETOPGBAL]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GETOPGBAL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GETOPGBAL]
GO
/****** Object:  StoredProcedure [dbo].[GetNettRent]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetNettRent]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetNettRent]
GO
/****** Object:  StoredProcedure [dbo].[getkotfy]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getkotfy]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[getkotfy]
GO
/****** Object:  StoredProcedure [dbo].[GETGRPVOU]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GETGRPVOU]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GETGRPVOU]
GO
/****** Object:  StoredProcedure [dbo].[getgrprooms1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getgrprooms1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[getgrprooms1]
GO
/****** Object:  StoredProcedure [dbo].[getgrprooms]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getgrprooms]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[getgrprooms]
GO
/****** Object:  StoredProcedure [dbo].[getgrpadvr]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getgrpadvr]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[getgrpadvr]
GO
/****** Object:  StoredProcedure [dbo].[GETGRP]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GETGRP]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GETGRP]
GO
/****** Object:  StoredProcedure [dbo].[GETFY]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GETFY]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GETFY]
GO
/****** Object:  StoredProcedure [dbo].[GETEDITBALANCE1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GETEDITBALANCE1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GETEDITBALANCE1]
GO
/****** Object:  StoredProcedure [dbo].[GETEDITBALANCE]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GETEDITBALANCE]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GETEDITBALANCE]
GO
/****** Object:  StoredProcedure [dbo].[Getdobphones]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Getdobphones]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Getdobphones]
GO
/****** Object:  StoredProcedure [dbo].[GetDetails1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetDetails1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[GetDetails1]
GO
/****** Object:  StoredProcedure [dbo].[getbno]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getbno]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[getbno]
GO
/****** Object:  StoredProcedure [dbo].[FindRent]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FindRent]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[FindRent]
GO
/****** Object:  StoredProcedure [dbo].[findclash]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[findclash]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[findclash]
GO
/****** Object:  StoredProcedure [dbo].[fillstock1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fillstock1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[fillstock1]
GO
/****** Object:  StoredProcedure [dbo].[fillstock]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fillstock]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[fillstock]
GO
/****** Object:  StoredProcedure [dbo].[fillfyforlinker]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fillfyforlinker]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[fillfyforlinker]
GO
/****** Object:  StoredProcedure [dbo].[fillfg]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fillfg]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[fillfg]
GO
/****** Object:  StoredProcedure [dbo].[FILLBALold]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FILLBALold]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[FILLBALold]
GO
/****** Object:  StoredProcedure [dbo].[fillbal_all]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fillbal_all]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[fillbal_all]
GO
/****** Object:  StoredProcedure [dbo].[FILLBAL]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FILLBAL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[FILLBAL]
GO
/****** Object:  StoredProcedure [dbo].[FDRonetime]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FDRonetime]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[FDRonetime]
GO
/****** Object:  StoredProcedure [dbo].[dropXXXtables]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dropXXXtables]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[dropXXXtables]
GO
/****** Object:  StoredProcedure [dbo].[dropfile]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dropfile]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[dropfile]
GO
/****** Object:  StoredProcedure [dbo].[dropcolumn]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dropcolumn]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[dropcolumn]
GO
/****** Object:  StoredProcedure [dbo].[dootacommvouching]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dootacommvouching]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[dootacommvouching]
GO
/****** Object:  StoredProcedure [dbo].[DELGRNO1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DELGRNO1]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[DELGRNO1]
GO
/****** Object:  StoredProcedure [dbo].[DELGRNO]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DELGRNO]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[DELGRNO]
GO
/****** Object:  StoredProcedure [dbo].[deleteglac]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[deleteglac]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[deleteglac]
GO
/****** Object:  StoredProcedure [dbo].[delcursor]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[delcursor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[delcursor]
GO
/****** Object:  StoredProcedure [dbo].[DELCOUT]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DELCOUT]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[DELCOUT]
GO
/****** Object:  StoredProcedure [dbo].[CRSrateupdate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CRSrateupdate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CRSrateupdate]
GO
/****** Object:  StoredProcedure [dbo].[CreateTable]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CreateTable]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CreateTable]
GO
/****** Object:  StoredProcedure [dbo].[createproforma]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[createproforma]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[createproforma]
GO
/****** Object:  StoredProcedure [dbo].[createbillGST]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[createbillGST]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[createbillGST]
GO
/****** Object:  StoredProcedure [dbo].[createbill2GST]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[createbill2GST]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[createbill2GST]
GO
/****** Object:  StoredProcedure [dbo].[createbill]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[createbill]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[createbill]
GO
/****** Object:  StoredProcedure [dbo].[cospldc]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[cospldc]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[cospldc]
GO
/****** Object:  StoredProcedure [dbo].[CODate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CODate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CODate]
GO
/****** Object:  StoredProcedure [dbo].[CloseAndDeallocateCursor]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CloseAndDeallocateCursor]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CloseAndDeallocateCursor]
GO
/****** Object:  StoredProcedure [dbo].[chkforgrno]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[chkforgrno]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[chkforgrno]
GO
/****** Object:  StoredProcedure [dbo].[checksystems]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[checksystems]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[checksystems]
GO
/****** Object:  StoredProcedure [dbo].[CheckLTONTARIFF]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CheckLTONTARIFF]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CheckLTONTARIFF]
GO
/****** Object:  StoredProcedure [dbo].[CheckKOTDate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CheckKOTDate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CheckKOTDate]
GO
/****** Object:  StoredProcedure [dbo].[checkblock]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[checkblock]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[checkblock]
GO
/****** Object:  StoredProcedure [dbo].[CHARTTOGL]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CHARTTOGL]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CHARTTOGL]
GO
/****** Object:  StoredProcedure [dbo].[ChartNew]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ChartNew]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ChartNew]
GO
/****** Object:  StoredProcedure [dbo].[Channel2Channel]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Channel2Channel]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[Channel2Channel]
GO
/****** Object:  StoredProcedure [dbo].[ChangeKOTDate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ChangeKOTDate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[ChangeKOTDate]
GO
/****** Object:  StoredProcedure [dbo].[CelVou]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CelVou]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[CelVou]
GO
/****** Object:  StoredProcedure [dbo].[BatchPostingB]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BatchPostingB]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[BatchPostingB]
GO
/****** Object:  StoredProcedure [dbo].[BatchPostingA]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BatchPostingA]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[BatchPostingA]
GO
/****** Object:  StoredProcedure [dbo].[BanqBillNOUpdate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BanqBillNOUpdate]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[BanqBillNOUpdate]
GO
/****** Object:  StoredProcedure [dbo].[avrrentold]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[avrrentold]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[avrrentold]
GO
/****** Object:  StoredProcedure [dbo].[avrrent]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[avrrent]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[avrrent]
GO
/****** Object:  StoredProcedure [dbo].[addrooms]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[addrooms]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[addrooms]
GO
/****** Object:  UserDefinedFunction [dbo].[vouddet]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[vouddet]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[vouddet]
GO
/****** Object:  UserDefinedFunction [dbo].[VatEligible]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[VatEligible]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[VatEligible]
GO
/****** Object:  UserDefinedFunction [dbo].[TTSTType]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TTSTType]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[TTSTType]
GO
/****** Object:  UserDefinedFunction [dbo].[SPKBLNEWFunction]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SPKBLNEWFunction]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[SPKBLNEWFunction]
GO
/****** Object:  UserDefinedFunction [dbo].[shfrooms]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[shfrooms]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[shfrooms]
GO
/****** Object:  UserDefinedFunction [dbo].[pwcheck]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pwcheck]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[pwcheck]
GO
/****** Object:  UserDefinedFunction [dbo].[pbpga4]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[pbpga4]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[pbpga4]
GO
/****** Object:  UserDefinedFunction [dbo].[mop]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[mop]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[mop]
GO
/****** Object:  UserDefinedFunction [dbo].[getusername]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getusername]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getusername]
GO
/****** Object:  UserDefinedFunction [dbo].[getttstrate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getttstrate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getttstrate]
GO
/****** Object:  UserDefinedFunction [dbo].[getsttabler]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getsttabler]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getsttabler]
GO
/****** Object:  UserDefinedFunction [dbo].[getstrate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getstrate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getstrate]
GO
/****** Object:  UserDefinedFunction [dbo].[GetStockRate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetStockRate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetStockRate]
GO
/****** Object:  UserDefinedFunction [dbo].[GetStockNew]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetStockNew]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetStockNew]
GO
/****** Object:  UserDefinedFunction [dbo].[GetStockFast2]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetStockFast2]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetStockFast2]
GO
/****** Object:  UserDefinedFunction [dbo].[GetStockFast]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetStockFast]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetStockFast]
GO
/****** Object:  UserDefinedFunction [dbo].[GetStock]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetStock]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetStock]
GO
/****** Object:  UserDefinedFunction [dbo].[getrnos1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getrnos1]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getrnos1]
GO
/****** Object:  UserDefinedFunction [dbo].[getrnos]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getrnos]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getrnos]
GO
/****** Object:  UserDefinedFunction [dbo].[GetRentminuscommn]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetRentminuscommn]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetRentminuscommn]
GO
/****** Object:  UserDefinedFunction [dbo].[GetRent]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetRent]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetRent]
GO
/****** Object:  UserDefinedFunction [dbo].[GetPreference]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetPreference]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetPreference]
GO
/****** Object:  UserDefinedFunction [dbo].[GetPmode]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetPmode]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetPmode]
GO
/****** Object:  UserDefinedFunction [dbo].[getpax]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getpax]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getpax]
GO
/****** Object:  UserDefinedFunction [dbo].[getonedayrent]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getonedayrent]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getonedayrent]
GO
/****** Object:  UserDefinedFunction [dbo].[GetOB]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetOB]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetOB]
GO
/****** Object:  UserDefinedFunction [dbo].[Getnschgs]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Getnschgs]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[Getnschgs]
GO
/****** Object:  UserDefinedFunction [dbo].[GetNoshowGSTRate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetNoshowGSTRate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetNoshowGSTRate]
GO
/****** Object:  UserDefinedFunction [dbo].[GetNettRentF]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetNettRentF]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetNettRentF]
GO
/****** Object:  UserDefinedFunction [dbo].[getmopgl]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getmopgl]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getmopgl]
GO
/****** Object:  UserDefinedFunction [dbo].[getmopdetails]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getmopdetails]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getmopdetails]
GO
/****** Object:  UserDefinedFunction [dbo].[getmin]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getmin]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getmin]
GO
/****** Object:  UserDefinedFunction [dbo].[GetLTRate]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetLTRate]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetLTRate]
GO
/****** Object:  UserDefinedFunction [dbo].[getlic3]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getlic3]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getlic3]
GO
/****** Object:  UserDefinedFunction [dbo].[getlic2]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getlic2]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getlic2]
GO
/****** Object:  UserDefinedFunction [dbo].[getlic1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getlic1]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getlic1]
GO
/****** Object:  UserDefinedFunction [dbo].[getkots]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getkots]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getkots]
GO
/****** Object:  UserDefinedFunction [dbo].[GetHeads]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetHeads]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetHeads]
GO
/****** Object:  UserDefinedFunction [dbo].[getgstrater]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getgstrater]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getgstrater]
GO
/****** Object:  UserDefinedFunction [dbo].[getgstbillnoservice1]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getgstbillnoservice1]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getgstbillnoservice1]
GO
/****** Object:  UserDefinedFunction [dbo].[getgstbillnoService]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getgstbillnoService]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getgstbillnoService]
GO
/****** Object:  UserDefinedFunction [dbo].[getgstbillno]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getgstbillno]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getgstbillno]
GO
/****** Object:  UserDefinedFunction [dbo].[geteofy]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[geteofy]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[geteofy]
GO
/****** Object:  UserDefinedFunction [dbo].[getenos]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getenos]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getenos]
GO
/****** Object:  UserDefinedFunction [dbo].[getdetails]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getdetails]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getdetails]
GO
/****** Object:  UserDefinedFunction [dbo].[getbofy]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getbofy]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getbofy]
GO
/****** Object:  UserDefinedFunction [dbo].[GETBNQGSTRATEforbanquetcharges]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GETBNQGSTRATEforbanquetcharges]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GETBNQGSTRATEforbanquetcharges]
GO
/****** Object:  UserDefinedFunction [dbo].[GETBNQGSTRATE]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GETBNQGSTRATE]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GETBNQGSTRATE]
GO
/****** Object:  UserDefinedFunction [dbo].[GetBlockstatus]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetBlockstatus]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetBlockstatus]
GO
/****** Object:  UserDefinedFunction [dbo].[GetBlocks]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetBlocks]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetBlocks]
GO
/****** Object:  UserDefinedFunction [dbo].[GetBlocked]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetBlocked]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetBlocked]
GO
/****** Object:  UserDefinedFunction [dbo].[getbillqty]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getbillqty]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[getbillqty]
GO
/****** Object:  UserDefinedFunction [dbo].[GETBALANCEFAST]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GETBALANCEFAST]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GETBALANCEFAST]
GO
/****** Object:  UserDefinedFunction [dbo].[GetBalance]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[GetBalance]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[GetBalance]
GO
/****** Object:  UserDefinedFunction [dbo].[fn_Split]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn_Split]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn_Split]
GO
/****** Object:  UserDefinedFunction [dbo].[findrentSIMPLE]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[findrentSIMPLE]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[findrentSIMPLE]
GO
/****** Object:  UserDefinedFunction [dbo].[findrents]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[findrents]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[findrents]
GO
/****** Object:  UserDefinedFunction [dbo].[FindRentForOneDay]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FindRentForOneDay]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FindRentForOneDay]
GO
/****** Object:  UserDefinedFunction [dbo].[findperiodvalid]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[findperiodvalid]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[findperiodvalid]
GO
/****** Object:  UserDefinedFunction [dbo].[FindLT]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[FindLT]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[FindLT]
GO
/****** Object:  UserDefinedFunction [dbo].[findfy]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[findfy]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[findfy]
GO
/****** Object:  UserDefinedFunction [dbo].[findactnum]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[findactnum]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[findactnum]
GO
/****** Object:  UserDefinedFunction [dbo].[encoder]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[encoder]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[encoder]
GO
/****** Object:  UserDefinedFunction [dbo].[dateOnly]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dateOnly]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[dateOnly]
GO
/****** Object:  UserDefinedFunction [dbo].[DatabaseExists]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DatabaseExists]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[DatabaseExists]
GO
/****** Object:  UserDefinedFunction [dbo].[counter]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[counter]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[counter]
GO
/****** Object:  UserDefinedFunction [dbo].[CompareRent]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CompareRent]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[CompareRent]
GO
/****** Object:  UserDefinedFunction [dbo].[CHKITINERARY]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CHKITINERARY]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[CHKITINERARY]
GO
/****** Object:  UserDefinedFunction [dbo].[BillPreFix]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BillPreFix]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[BillPreFix]
GO
/****** Object:  UserDefinedFunction [dbo].[BillAmtCNO]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BillAmtCNO]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[BillAmtCNO]
GO
/****** Object:  UserDefinedFunction [dbo].[billamtbcno]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[billamtbcno]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[billamtbcno]
GO
/****** Object:  UserDefinedFunction [dbo].[BillAmt]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BillAmt]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[BillAmt]
GO
/****** Object:  UserDefinedFunction [dbo].[BanquetBillno]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BanquetBillno]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[BanquetBillno]
GO
/****** Object:  UserDefinedFunction [dbo].[advrecpt]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[advrecpt]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[advrecpt]
GO
/****** Object:  UserDefinedFunction [dbo].[advancemop]    Script Date: 08/04/2026 16:48:12 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[advancemop]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[advancemop]
GO
/****** Object:  UserDefinedFunction [dbo].[advancemop]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[advancemop] (@mop int)  
RETURNS varchar(20)  
/* with encryption */
AS  
BEGIN 
return
case when @mop=1 then 'Cash'
when @mop=2 then 'Credit card'
when @mop=3 then 'DD/Cheque'
when @mop=4 then 'GL Account'
else ' '
end
END



GO
/****** Object:  UserDefinedFunction [dbo].[advrecpt]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[advrecpt]( @cno numeric  )
RETURNS varchar(1500) 
/* with encryption */
AS  
BEGIN 
declare @ano numeric
declare @str1 varchar(1500)
set @str1=''
declare c1 cursor for select ano from advance where gno in (select gno from guest where brno in (select brno from billhist1 where cno=@cno))
open c1
fetch next from c1 into @ano
while @@fetch_status=0
begin
set @str1 = @str1 + ltrim(rtrim(@ano)) +','
fetch next from c1 into @ano
END
close c1
deallocate c1
if len(@str1) > 0 
set @str1 = substring(@str1,1,len(@str1)-1)
return @str1

end



GO
/****** Object:  UserDefinedFunction [dbo].[BanquetBillno]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[BanquetBillno]
(
  @company int,@bnqno numeric,@m_bno numeric ,@m_billdate datetime 
)
RETURNS varchar(50)
AS
BEGIN
declare @sdt datetime
declare @edt datetime
declare @bno1 varchar(10)
declare @year varchar(10)
declare @prefixco varchar(10)
declare @prefixbnq varchar(10)
DECLARE @BILLNO VARCHAR(50)
declare @cbbillyear int
SELECT @CBBILLYEAR=BILLYEAR FROM SETTING

select @prefixco = billprefix from company where company=@company  
select @prefixbnq= isnull(billprefix,'BNQ') from banqmaster where bnqno=@bnqno
set @sdt = dbo.getbofy(@m_billdate) 
set @edt = dbo.geteofy(@m_billdate)
set @bno1 = ltrim(rtrim(str(@m_bno)))
if @cbbillyear=1
	begin
	set @year= ' /' + substring(ltrim(rtrim(str(Year(@sdt)))),3,2) + '-' + substring(ltrim(rtrim(str(year(@edt)))),3,2)
	set @year= '/' + substring(ltrim(rtrim(str(Year(@sdt)))),3,2)
	if exists(select doubleyear from setting where doubleyear='1')
	set @year = @year + '-' + substring(ltrim(rtrim(str(year(@edt)))),3,2)
	end
else
set @year=''

SET @BILLNO =  @PREFIXCO + @PREFIXBNQ + '/' + @BNO1 + @YEAR
RETURN @BILLNO
END
GO
/****** Object:  UserDefinedFunction [dbo].[BillAmt]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[BillAmt]  
(
	@brno numeric,@type int=0
)
RETURNS money
/* with encryption */
AS
BEGIN
	declare @money money
if @type=0
	select @money = sum(total) - 
	(select sum(spldiscount) from billhist1  where brno=@brno and split in (' ' ,'N','Y'))
	+ (select sum(foodbill) from billhist1 where brno=@brno and split in (' ' ,'N','Y'))
	from billhist where cno in (select cno from billhist1 where
	brno=@brno and split in (' ' ,'N','Y'))
else
	select @money = sum(total) - 
	(select sum(spldiscount) from billhist1  where brno=@brno and split in (' ' ,'N','Y'))
	+ (select sum(foodbill) from billhist1 where brno=@brno and split in (' ' ,'N','Y'))
	from billhist where cno in (select cno from billhist1 where
	brno=@brno and split not in (' ' ,'N','Y'))
	return @money
END



GO
/****** Object:  UserDefinedFunction [dbo].[billamtbcno]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[billamtbcno]  
(
@brno numeric,@company numeric,@nsplit int )
RETURNS money
/* with encryption */
AS
BEGIN
declare @billamt numeric
declare @cno numeric
if @nsplit=0
select @cno=cno from billhist1 where brno=@brno and company=@company and split in ('N',' ') 
else
select @cno=cno from billhist1 where brno=@brno and company=@company and split in ('Y') 

select @billamt=dbo.billamtcno(@cno)
return @billamt
END
GO
/****** Object:  UserDefinedFunction [dbo].[BillAmtCNO]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 14 May 2013
-- Description:	Specialy for MassRent ( This total is not included with OtherRoomChgs
-- =============================================
CREATE FUNCTION [dbo].[BillAmtCNO]
(
@cno numeric
)
RETURNS numeric(18,2)
/* with encryption */
AS
BEGIN
declare @money numeric(18,2)
	select @money= (select sum(total) from billhist where cno=@cno) - spldiscount + foodbill
    from billhist1 where cno=@cno
   return @money
END



GO
/****** Object:  UserDefinedFunction [dbo].[BillPreFix]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[BillPreFix] 
(
	@co int, @bdt datetime  
)
RETURNS varchar(20)
/* with encryption */
AS
BEGIN
	-- Declare the return variable here
	DECLARE @prefix varchar(20) 
	declare @bofy datetime
	declare @eofy datetime
	SELECT @prefix = billprefix from company where company=@co
if exists (select * from setting where billyear=1) 
	set @prefix = @prefix --+ substring( ltrim( str(year(dbo.getbofy(@bdt))) ) ,3,2) + substring( ltrim( str(year(dbo.geteofy(@bdt))) ) ,3,2)  
		-- Return the result of the function
	RETURN @prefix

END
GO
/****** Object:  UserDefinedFunction [dbo].[CHKITINERARY]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[CHKITINERARY] 
(
@CTRLNO numeric)
RETURNS int
/* with encryption */
AS
BEGIN
declare @cnt int
set @cnt=0
if exists (select * from chalan where ino=@ctrlno) set @cnt=1
if exists (select * from ta_chalan where ino=@ctrlno) set @cnt=1
RETURN @cnt
END
GO
/****** Object:  UserDefinedFunction [dbo].[CompareRent]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[CompareRent](@amt1 money,@amt2 money,@txndt datetime)
RETURNS int
/* with encryption */
AS
BEGIN
	DECLARE @ResultVar int
	declare @lrate1 money
	declare @lrate2 money
	set @lrate1 = dbo.getltrate(@amt1,@txndt)
	set @lrate2 = dbo.getltrate(@amt2,@txndt)
if @lrate1<>@lrate2 
set @resultvar=1
else
set @resultvar = 0

	RETURN @ResultVar

END
GO
/****** Object:  UserDefinedFunction [dbo].[counter]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[counter] ( )
RETURNS numeric 
/* with encryption */
AS  
BEGIN 
declare @sno numeric
exec setserialno
select @sno=cnt from serialno
return @sno
END



GO
/****** Object:  UserDefinedFunction [dbo].[DatabaseExists]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[DatabaseExists](@dbname nvarchar(128))
RETURNS int
/* with encryption */
AS
BEGIN
    declare @result int
    SELECT @result = CAST(
        CASE WHEN db_id(@dbname) is not null THEN 1 
        ELSE 0 
        END 
    AS int)
    return @result
END
GO
/****** Object:  UserDefinedFunction [dbo].[dateOnly]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[dateOnly]
(
@dateInput datetime
)
RETURNS varchar (10)
/* with encryption */
AS
BEGIN

declare @tempRes as varchar(10)
declare @tempYear as varchar(4)
declare @tempMonth as varchar(2)
declare @tempDay as varchar(2)

set @tempYear=(select cast ((datepart(yyyy,@dateInput)) as varchar(4)))
set @tempMonth=(select cast ((datepart(MM,@dateInput)) as varchar(4)))
set @tempDay=(select cast ((datepart(dd,@dateInput)) as varchar(4)))

set @tempRes=@tempYear+'-'+@tempMonth+'-'+@tempDay

RETURN @tempRes

END

GO
/****** Object:  UserDefinedFunction [dbo].[encoder]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[encoder] (@str varchar(200))  
RETURNS varchar(200) 
/* with encryption */
AS  
BEGIN 
declare @x int
declare @y int
declare @str1 varchar(200)
declare @chr char(3)
set @x = len(@str)
set @y=1
set @str1=''
while @y <= @x
begin
if ascii(substring(@str,@y,1)) <> 32
select @chr=col002 from encode where col001=substring(@str,@y,1)
else
select @chr='%20'

set @str1 = @str1 + @chr
set @y=@y+1
end
return @str1
END



GO
/****** Object:  UserDefinedFunction [dbo].[findactnum]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[findactnum](@actnum numeric , @fy numeric,@txndt datetime)
RETURNS numeric 
/* with encryption */
AS  
begin
declare @company numeric
declare @ofy numeric
declare @oactnum numeric
declare @nfy numeric
declare @sno numeric
declare @a1 numeric
declare @f1 numeric
set @oactnum=@actnum
set @sno = 0

select @company=company from finyear where fy=@fy
set @ofy=@fy
set @nfy = dbo. findfy(@company,@txndt)
while @ofy <= @nfy
begin
set @sno=@sno+1
select @a1=nextyearac, @f1=nextyearfy from glprimld where fy=@ofy and cgp_actno=@oactnum
if @a1 is null break
if @f1 is null break
if @ofy=@nfy break
if @sno=50  break
set @ofy =@f1
set @oactnum = @a1
end

return @oactnum
END



GO
/****** Object:  UserDefinedFunction [dbo].[findfy]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[findfy]( @company numeric,@txndt datetime)
RETURNS numeric
/* with encryption */
AS  
begin
declare @fy numeric
DECLARE @M_COUNT INT
DECLARE @M_YEAR INT
DECLARE @M_TDATE DATETIME
DECLARE @M_DESC CHAR(30)

set @m_tdate = dbo.getbofy(@txndt)
--IF DATEPART(mm,@TXNDT) >=  1 AND DATEPART(mm,@TXNDT) <= 3
--     SET @M_YEAR = DATEPART(yy,@TXNDT)-1
--ELSE
--     SET @M_YEAR = DATEPART(yy,@TXNDT)
set @m_year = datepart(yy,@m_tdate)

SELECT @M_DESC = 'FIN.YEAR ' + LTRIM(STR(@m_YEAR)) + '-' + LTRIM(STR(@m_YEAR+1))

SELECT @FY=FY FROM FINYEAR WHERE  DATEPART(yy,[DATE])=@M_YEAR AND COMPANY=@COMPANY
SELECT @M_COUNT= @@ROWCOUNT
if @m_count=0 set @fy=0  

return @fy
END



GO
/****** Object:  UserDefinedFunction [dbo].[FindLT]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[FindLT]  (@amt money,@txndt datetime , @adroom int=1,@rno numeric=0,@rent money =0 ,@head int = 1 ,@discount money = 0 , @dup int = 0 ,@renttype int=1)

RETURNS money  
/* with encryption */
AS  
BEGIN 
declare @ltamt money
declare @tno int
declare @sql varchar(5000)
declare @lrate money
declare @samt money
declare @eamt money
declare @famt money
declare @amt2 money
declare @ltontariff char(1)
declare @taxsystem int
select @taxsystem=g_taxsystem from setting
SET @LTAMT = 0
declare @ltamttemp money
declare @amttemp money
declare @amttemp1 money
declare @tariff money
declare @ltrateontariff char(1)
set @ltamttemp=0
select @ltontariff=ltontariff,@ltrateontariff=ltrateontariff from ltontariff where datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt) 
if @ltontariff='N'
	begin
		set @amttemp=@amt
		set @amttemp1=@amt
	end
else
	begin
		if @rent<>0 
			begin
				select @tariff=dbo.getrent(@renttype,@rno,@txndt,@head)
				if @rent-@discount < @tariff set @amttemp=@amt-@rent+@discount + @tariff else set @amttemp=@amt
			end
		else
			set @amttemp=@amt
		set @amttemp1=@amttemp
		set @amt=@amttemp1
		set @adroom=1 --since there is no use in additional rooms
	end
if @adroom=0 set @adroom=1

select @tno=tno from ltmaster where datediff(d,'1/1/90',@txndt)  between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)
if @taxsystem=1    -- slabsystem
begin
	declare cur_lttable cursor for select lrate,samt,eamt,famt from lttable where tno = @tno
	open cur_lttable
	fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
	while @@fetch_status=0 and @amt > 0
	begin
		if @eamt > (@amt/@adroom)  set @amt2 = @amt   else   set @amt2 = @eamt
		if @lrate<>0 and (@amt2+@famt) <> 0 
			set @ltamt = round(@ltamt + ((@amt2 + @famt) * @lrate / 100),2)
		
		set @amt = @amt - @amt2
		if @eamt > @amttemp set @amt2=@amttemp else set @amt2 = @eamt
		if @lrate<>0 and (@amt2+@famt) <> 0 
			set @ltamttemp = round(@ltamttemp+((@amt2+@famt)*@lrate/100),2)
		set @amttemp=@amttemp-@amt2
		fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
	end
	close cur_lttable
	deallocate cur_lttable
end
else
begin
	if @ltrateontariff='Y'
		declare ct1 cursor for select lrate from lttable where tno=@tno  and  @TARIFF  between samt and eamt
	else
		declare ct1 cursor for select lrate from lttable where tno=@tno  and  (@amt/@adroom)  between samt and eamt
	open ct1
	fetch next from ct1 into @lrate
	if @@fetch_status=0
	begin
	set  @ltamt = round(@amt * @lrate /100,2)
	end
end

return @ltamt

END



GO
/****** Object:  UserDefinedFunction [dbo].[findperiodvalid]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[findperiodvalid] (@dt1 datetime,@dt2 datetime,@dt3 datetime,@dt4 datetime)  
RETURNS numeric  
/* with encryption */
AS  
BEGIN 
declare @retval numeric
if (datediff(d,'1/1/90',@dt1) between datediff(d,'1/1/90',@dt3) and datediff(d,'1/1/90',@dt4))  or
(datediff(d,'1/1/90',@dt2) between datediff(d,'1/1/90',@dt3) and datediff(d,'1/1/90',@dt4)) or
(datediff(d,'1/1/90',@dt3) between datediff(d,'1/1/90',@dt1) and datediff(d,'1/1/90',@dt2)) or
(datediff(d,'1/1/90',@dt4) between datediff(d,'1/1/90',@dt1) and datediff(d,'1/1/90',@dt2))
set @retval = 1
else
set @retval=0
return @retval
END



GO
/****** Object:  UserDefinedFunction [dbo].[FindRentForOneDay]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 15 August 2015
-- Description:	to correct the infosent program
-- =============================================
CREATE FUNCTION [dbo].[FindRentForOneDay]
(
@gno numeric,@txndt datetime,@getdate datetime 
)
RETURNS numeric
/* with encryption */
AS
BEGIN
declare @rent numeric(19,2)
declare @lttype int
declare @renttype int
declare @scrate numeric(18,2)
declare @drate money
declare @sc money
declare @discount numeric(18,2)
declare @rno numeric
declare @ontariff int
declare @heads int
declare @nettrent numeric(18,2)
declare @lt money
declare @strate money
declare @st money
declare @baserent money
declare @allinclusive char(1)

select @heads=male+female,@rno=rno,@rent=rent,@nettrent=nettrent,@lttype=lttype,@renttype=renttype1,@scrate=isnull(sc,0),@drate=isnull(discount,0),@allinclusive=allinclusive from guest where gno=@gno
set @baserent=@nettrent
if exists(select * from grent where gno=@gno)
begin
select @rno=rno,@heads=male+female,@drate=discount,@nettrent=rent from grent where gno=@gno and @txndt between cI_date and co_Date 
end
else
begin
	if @lttype=1
	select @nettrent=dbo.getnettrentF(@nettrent,@heads,@rno,@ontariff,@txndt,@scrate,@renttype ,0,0,@getdate)

	else
	select @nettrent=nettrent from guest where gno=@gno
end
if @drate > 0
select @discount = @baserent * @drate/100
else
select @discount=0

select @lt = dbo.findlt( @nettrent,@txndt,1,@rno, @nettrent,@heads,@drate,0,@renttype)
select @strate=dbo.getstrate (@nettrent,@txndt)
select @st = @nettrent*(@strate/100)
select @sc = @baserent*(@scrate/100)

return @nettrent + @lt + @st +@sc - @discount

END
GO
/****** Object:  UserDefinedFunction [dbo].[findrents]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[findrents]
(
@amt money,@txndt datetime
)
RETURNS money
AS
BEGIN

declare @tno int
declare @lrate money
declare @samt money
declare @eamt money
declare @famt money
select @tno=tno from ltmaster where datediff(d,'1/1/90',@txndt)  between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)
select @lrate=LRATE from LTTABLE where TNO= @TNO AND  @amt between samt AND EAMT

RETURN @AMT + (@AMT * @LRATE/100)

END
GO
/****** Object:  UserDefinedFunction [dbo].[findrentSIMPLE]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[findrentSIMPLE]
(
@amt money,@txndt datetime
)
RETURNS money
AS
BEGIN

declare @tno int
declare @lrate money
declare @samt money
declare @eamt money
declare @famt money
select @tno=tno from ltmaster where datediff(d,'1/1/90',@txndt)  between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)
select @lrate=LRATE from LTTABLE where TNO= @TNO AND  @amt between samt AND EAMT

RETURN @AMT + (@AMT * @LRATE/100)

END
GO
/****** Object:  UserDefinedFunction [dbo].[fn_Split]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[fn_Split](@sText varchar(8000), @sDelim varchar(20) = ' ')
RETURNS @retArray TABLE (idx smallint Primary Key, value varchar(8000))
/* with encryption */
AS
BEGIN
DECLARE @idx smallint,
@value varchar(8000),
@bcontinue bit,
@iStrike smallint,
@iDelimlength tinyint

IF @sDelim = 'Space'
BEGIN
SET @sDelim = ' '
END

SET @idx = 0
SET @sText = LTrim(RTrim(@sText))
SET @iDelimlength = DATALENGTH(@sDelim)
SET @bcontinue = 1

IF NOT ((@iDelimlength = 0) or (@sDelim = 'Empty'))
BEGIN
WHILE @bcontinue = 1
BEGIN

--If you can find the delimiter in the text, retrieve the first element and
--insert it with its index into the return table.

IF CHARINDEX(@sDelim, @sText)>0
BEGIN
SET @value = SUBSTRING(@sText,1, CHARINDEX(@sDelim,@sText)-1)
BEGIN
INSERT @retArray (idx, value)
VALUES (@idx, @value)
END

--Trim the element and its delimiter from the front of the string.
--Increment the index and loop.
SET @iStrike = DATALENGTH(@value) + @iDelimlength
SET @idx = @idx + 1
SET @sText = LTrim(Right(@sText,DATALENGTH(@sText) - @iStrike))

END
ELSE
BEGIN
--If you can't find the delimiter in the text, @sText is the last value in
--@retArray.
SET @value = @sText
BEGIN
INSERT @retArray (idx, value)
VALUES (@idx, @value)
END
--Exit the WHILE loop.
SET @bcontinue = 0
END
END
END
ELSE
BEGIN
WHILE @bcontinue=1
BEGIN
--If the delimiter is an empty string, check for remaining text
--instead of a delimiter. Insert the first character into the
--retArray table. Trim the character from the front of the string.
--Increment the index and loop.
IF DATALENGTH(@sText)>1
BEGIN
SET @value = SUBSTRING(@sText,1,1)
BEGIN
INSERT @retArray (idx, value)
VALUES (@idx, @value)
END
SET @idx = @idx+1
SET @sText = SUBSTRING(@sText,2,DATALENGTH(@sText)-1)

END
ELSE
BEGIN
--One character remains.
--Insert the character, and exit the WHILE loop.
INSERT @retArray (idx, value)
VALUES (@idx, @sText)
SET @bcontinue = 0 
END
END

END

RETURN
END



GO
/****** Object:  UserDefinedFunction [dbo].[GetBalance]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/** Date : 26-01-2011 Republic Day **/
CREATE FUNCTION [dbo].[GetBalance](@company as numeric,@actnum numeric,@dt datetime)
RETURNS decimal(16,2)  
/* with encryption */
AS  
BEGIN 
declare @balance as decimal(16,2)
declare @fy int
set @fy = dbo.findfy(@company,@dt)

if exists(select  *  from vouchers 
where actnum=@actnum and 
fy=@fy
)
select @balance= balance from 
(select top 1 *  from vouchers 
where actnum=@actnum and 
fy=@fy
and datediff(d,txndt,@dt) >= 0
order by datediff(d,'1 jan 2000',txndt) desc ,txnno2 desc )  a
else
select @balance=opgbal from glprimld where cgp_actno=@actnum and fy=@fy

set @balance = isnull(@balance,0)

return @balance
END



GO
/****** Object:  UserDefinedFunction [dbo].[GETBALANCEFAST]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Rm.Ramanathan
-- Create date: 02 Nov 2017
-- Description:	Faster retrieve of ledger s and balance
-- =============================================
CREATE FUNCTION [dbo].[GETBALANCEFAST] 
(
@ACTNO NUMERIC,@TXNDT DATETIME,@fy numeric
)
RETURNS money
/* with encryption */
AS
BEGIN
	declare @balance money
	declare @opgbal money
	declare @cr money
	declare @dr money
	declare @bofy datetime
	set @bofy = dbo.getbofy(@txndt) 

	select @opgbal=isnull(opgbal,0) from glprimld where  fy=@fy and cgp_actno=@actno
	select @CR= sum(case when amttyp ='C' then amt else -amt end )  from vouchers where fy=@fy and actnum=@actno and datediff(d,'1/1/2000',txndt) between datediff(d,'1/1/2000',@bofy) and datediff(d,'1/1/2000',@txndt)
	--select @CR=SUM(amt1)  from vouchers where actnum=@actno and datediff(d,'1/1/2000',txndt) between datediff(d,'1/1/2000',@bofy) and datediff(d,'1/1/2000',@txndt)
	set @balance = @opgbal + isnull(@cr,0)   
return @balance
END

GO
/****** Object:  UserDefinedFunction [dbo].[getbillqty]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getbillqty]
(
@ctrlno numeric,@bctrlno numeric
)
RETURNS numeric(18,4)
/* with encryption */
AS
BEGIN
declare @qty numeric(18,4) 
declare @balance numeric(18,4)
select @balance=min(balance) from storehist where bctrlno=@bctrlno and ctrlno=@ctrlno
set @balance=isnull(@balance,0) 
select @qty=sum(qty) from storehist where bctrlno=@bctrlno
set @qty = isnull(@qty,0) 
return @balance+@qty
END
GO
/****** Object:  UserDefinedFunction [dbo].[GetBlocked]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[GetBlocked] (@rno numeric,@dt datetime)
RETURNS varchar(5000) 
/* with encryption */
AS  
BEGIN 
declare @return varchar(5000)
declare @ctrlno numeric
declare @adate datetime
declare @ddate 	datetime
declare @name varchar(200)
declare @company varchar(200)
declare @nor numeric
declare @myctrlno numeric
--declare c1 cursor for select top 5  noofrooms - (arrivedrooms+cancelledrooms), name,company,arrivaldate,departuredate,ctrlno_o,myctrlno from sptbrooms_v where rno = @rno and status1<> 'Y'  and (datediff(d,'1/1/90',@dt) between datediff(d,'1/1/90',arrivaldate) and datediff(d,'1/1/90',departuredate) and datediff(d,@dt,arrivaldate) >=0) order by arrivaldate
declare c1 cursor for select top 5  noofrooms - (arrivedrooms+cancelledrooms), name,company,arrivaldate,departuredate,ctrlno_o,myctrlno from sptbrooms_v where rno = @rno and status1<> 'Y'   and datediff(d,@dt,arrivaldate) >=0 order by arrivaldate
set @return = ''
open c1
fetch next from c1 into @nor,@name,@company,@adate,@ddate,@ctrlno,@myctrlno
while @@fetch_status=0
begin
if @return='' 
	set @return=@name +'/' + isnull(ltrim(rtrim(@company)),'FIT') + '-'  + convert(varchar(20),@adate) + ' To ' + convert(varchar(20),@ddate) + ' Res.Ref.No. ' + ltrim(str(@ctrlno))  + ' NOR : ' + ltrim(str(@nor)) + char(13) +  replace(dbo.getblocks(@myctrlno),',','.')
else
	set @return=@return +','+ @name +  '/' + isnull(ltrim(rtrim(@company)),'FIT') +  '-' + convert(varchar(20),@adate) + ' To ' + convert(varchar(20),@ddate) + ' Res.Ref.No. ' +  ltrim(str(@ctrlno))   + ' NOR : ' + ltrim(str(@nor))+ char(13) +  replace(dbo.getblocks(@myctrlno),',','.')

fetch next from c1 into @nor, @name,@company,@adate,@ddate,@ctrlno,@myctrlno
end
close c1
deallocate c1
return @return
END



GO
/****** Object:  UserDefinedFunction [dbo].[GetBlocks]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[GetBlocks]  (@myctrlno numeric )
RETURNS varchar(5000) 
/* with encryption */
AS  
BEGIN 
declare @return varchar(5000)
declare @rno int
declare @gno numeric
declare c1 cursor for select rno from sptbrooms where myctrlno = @myctrlno order by rno
set @return = ''
open c1
fetch next from c1 into @rno
while @@fetch_status=0
begin
if @return='' 
	set @return=@return + rtrim(ltrim(str(@rno)))
else
	set @return=@return + ','+ rtrim(ltrim(str(@rno)))

fetch next from c1 into @rno
end
close c1
deallocate c1
return @return
END



GO
/****** Object:  UserDefinedFunction [dbo].[GetBlockstatus]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[GetBlockstatus] (@rno numeric,@dt datetime)
RETURNS char(1) 
/* with encryption */
AS  
BEGIN 
declare @return char(1)
if exists(select * from sptbrooms_v where rno=@rno and (noofrooms-(arrivedrooms+cancelledrooms)) > 0 and  datediff(d,arrivaldate,@dt)=0) select @return='Y' else select @return='N'  
return @return
END



GO
/****** Object:  UserDefinedFunction [dbo].[GETBNQGSTRATE]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GETBNQGSTRATE]( @BNQNO INT, @DT DATETIME)
RETURNS MONEY
/* with encryption */
AS
BEGIN
	Declare @sttableno int
	declare @rate money
	select @sttableno=sttableno from banqmaster where bnqno=@bnqno
	select @rate=rate from sttabler where sttableno=@sttableno and datediff(d,'1/1/2000',@dt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
	RETURN @rate
END
GO
/****** Object:  UserDefinedFunction [dbo].[GETBNQGSTRATEforbanquetcharges]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GETBNQGSTRATEforbanquetcharges]( @BNQNO INT, @DT DATETIME)
RETURNS MONEY
/* with encryption */
AS
BEGIN
	Declare @sttableno int
	declare @rate money
	select @sttableno=bnqsttableno from banqmaster where bnqno=@bnqno
	select @rate=rate from sttabler where sttableno=@sttableno and datediff(d,'1/1/2000',@dt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
	RETURN @rate
END
GO
/****** Object:  UserDefinedFunction [dbo].[getbofy]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<PowerBrain Software (India)>
-- Create date: <21-Dec-2011, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getbofy]
(
	-- Add the parameters for the function here
	@dt datetime
)
RETURNS datetime
/* with encryption */
AS
BEGIN
	-- Declare the return variable here
	declare @finmm varchar(20)
	declare @finstart tinyint
	declare @year numeric
	declare @bofy datetime
	select @finstart=g_finstart from setting

	-- Add the T-SQL statements to compute the return value here
	if @finstart=0 
	begin
			if datepart(mm,@dt) >=  1 and datepart(mm,@dt) <= 3
				set @year = datepart(yy,@dt)-1
			else
				set @year = datepart(yy,@dt)
			set @bofy = '1 Apr ' + str(@year)
	end
	else
		begin
	    set @year = datepart(yy,@dt)
		set @bofy = 'January 1,' + str(@year)
		end
	
	-- Return the result of the function
	RETURN @bofy

END



GO
/****** Object:  UserDefinedFunction [dbo].[getdetails]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[getdetails]( @gno numeric)
RETURNS varchar(200) 
/* with encryption */
AS  
BEGIN 
declare @det varchar(100)
declare @detf varchar(500)
set @detf=''
declare c1 cursor for select details from guest_memo where ctrlno_ref=(select top 1 ctrlno from gm_master where gno=@gno order by ctrlno desc)
open c1
fetch next from c1 into @det
while @@fetch_status=0 begin
set @detf=@detf+@det
fetch next from c1 into @det
end
close c1
deallocate c1
return(isnull(@detf,''))
END



GO
/****** Object:  UserDefinedFunction [dbo].[getenos]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[getenos]  (@brno numeric  )
RETURNS varchar(5000) 
/* with encryption */
AS  
BEGIN 
declare @return varchar(5000)
declare @rno int
declare @gno numeric
-- The RNO variable is used for Entry NO field (eno) - do not get confused
declare c1 cursor for select eno,gno from guest where brno = @brno and opentoall<>'N'
set @return = ''
open c1
fetch next from c1 into @rno,@gno
while @@fetch_status=0
begin
if @return='' 
	set @return=@return + rtrim(ltrim(str(@rno)))
else
	set @return=@return + ','+ rtrim(ltrim(str(@rno)))

declare c2 cursor for select rno from addroom where gno=@gno
open c2
fetch next from c2 into @rno
while @@fetch_status=0
begin
if @return='' 
	set @return=@return + rtrim(ltrim(str(@rno)))
else
	set @return=@return + ','+ rtrim(ltrim(str(@rno)))
fetch next from c2 into @rno
end
close c2
deallocate c2

fetch next from c1 into @rno,@gno
end
close c1
deallocate c1
return @return
END



GO
/****** Object:  UserDefinedFunction [dbo].[geteofy]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<PowerBrain Software (India)>
-- Create date: <December 2011>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[geteofy]
(
	-- Add the parameters for the function here
	@dt datetime
)
RETURNS datetime
/* with encryption */
AS
BEGIN
	-- Declare the return variable here
	DECLARE @bofy datetime
	declare @eofy datetime
	declare @finstart tinyint
	select @finstart=g_finstart from setting
	set @bofy = dbo.getbofy(@dt)
	if @finstart=0 
	   set @eofy = '31 Mar ' + str(datepart(yy,@bofy)+1)
	else
	   set @eofy = '31 dec ' + str(datepart(yy,@bofy))

	RETURN @eofy

END



GO
/****** Object:  UserDefinedFunction [dbo].[getgstbillno]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getgstbillno] ( @m_company numeric,@m_bno  numeric, @m_billdate Datetime, @m_split  char)
RETURNS varchar(100)
AS
BEGIN
declare @Retval varchar(100)
declare @G_BillPreFIX  varchar(10)
declare @cot1 varchar(109)
declare @year varchar(10)
select @g_billprefix=billprefix,@cot1=[COT] from company where company=@m_company
declare @sdt datetime
declare @edt datetime
declare @bno1 varchar(100)
DECLARE @CBBILLYEAR AS INT
set @sdt = dbo.getbofy(@m_billdate) 
set @edt = dbo.geteofy(@m_billdate)
set @bno1 = ltrim(rtrim(str(@m_bno)))
--set @year= '/' + substring(ltrim(rtrim(str(Year(@sdt)))),3,2)
--+ '-' + substring(ltrim(rtrim(str(year(@edt)))),3,2)
        --If ltrim(rtrim(@G_BillPreFix))  <> '' 

set @year= '/' + substring(ltrim(rtrim(str(Year(@sdt)))),3,2)
if exists(select doubleyear from setting where doubleyear='1')
set @year = @year + '-' + substring(ltrim(rtrim(str(year(@edt)))),3,2)


SELECT @CBBILLYEAR=BILLYEAR FROM SETTING
IF @CBBILLYEAR=1 --set @year=''
			begin
            If @M_split = 'N' Or @M_split = ' ' 
                set @RetVal = @G_BillPreFix + @cot1 + '/' + @bno1  + @year
            else 
			If @M_split = 'Y' 
			set @RetVal = @G_BillPreFix + 'SPT' + '/' + @bno1 + @year 
            Else 
			set @RetVal = @G_BillPreFix + 'COM' + '/' + @bno1 + @year
            End
Else
	begin
			begin
            If @M_split = 'N' Or @M_split = ' ' 
                set @RetVal = @G_BillPreFix + @cot1 + '/' + @bno1 
            else 
			If @M_split = 'Y' 
			begin
				set @RetVal = @G_BillPreFix + 'SPT' + '/' + @bno1 
			end
            Else 
			set @RetVal = @G_BillPreFix + 'COM' + '/' + @bno1 
            End
--                set @RetVal = @G_BillPreFix + @cot1 + '/' + @bno1 
	end
    Return @retval
END
GO
/****** Object:  UserDefinedFunction [dbo].[getgstbillnoService]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author: Ramanathan
-- Create date: <04 March 2021
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getgstbillnoService] (@company numeric, @m_bno numeric,@m_billdate datetime,@channel numeric,@outlet numeric)
RETURNS varchar(100)
AS
BEGIN
declare @retval varchar(100)
declare @g_billprefix varchar(10)
declare @billprefix  varchar(10)
declare @cot varchar(10)
declare @bno1 varchar(100)
declare @stype int
select @g_billprefix=billprefix from company where company=@company
declare @sql varchar(2000)
set @bno1 = ltrim(rtrim(Str(@m_bno)))


if @channel=1 
  select @billprefix=billprefix from outlet where code= @outlet
else if @channel=2
  select @billprefix=billprefix from phoutlet where outlet=@outlet
else if @channel=3
  select @billprefix=billprefix from banqmaster where bnqno=@outlet

declare @sdt datetime
declare @edt datetime
declare @year varchar(50)

set @sdt = dbo.getbofy(@m_billdate) 
set @edt = dbo.geteofy(@m_billdate)
set @bno1 = ltrim(rtrim(str(@m_bno)))
set @year = ''
if exists(select doubleyear from setting where billyear='1')
	begin
		set @year= '/' + substring(ltrim(rtrim(str(Year(@sdt)))),3,2)
		if exists(select doubleyear from setting where doubleyear='1')
		set @year = @year + '-' + substring(ltrim(rtrim(str(year(@edt)))),3,2)
	end
set @retval= @g_billprefix + @billprefix + '/' + @bno1 + @year
return @retval

END
GO
/****** Object:  UserDefinedFunction [dbo].[getgstbillnoservice1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getgstbillnoservice1] (@company numeric, @m_bno numeric,@m_billdate datetime,@channel numeric,@outlet numeric,@chctrlno numeric)
RETURNS varchar(100)
AS
BEGIN
declare @retval varchar(100)
declare @g_billprefix varchar(10)
declare @billprefix  varchar(10)
declare @cot varchar(10)
declare @bno1 varchar(100)
declare @stype int
select @g_billprefix=billprefix from company where company=@company
declare @sql varchar(2000)
set @bno1 = ltrim(rtrim(Str(@m_bno)))


if @channel=1 
  begin
  select @billprefix=billprefix from outlet where code= @outlet
  select @bno1=ltrim(rtrim(str(sno))) from kithist1 where bno=@m_bno
  end
else if @channel=2
  begin
  select @billprefix=billprefix from phoutlet where outlet=@outlet
    select @bno1=ltrim(rtrim(str(sno))) from phone where cno=@m_bno
  end
else if @channel=3
  select @billprefix=billprefix from banqmaster where bnqno=@outlet
else if @channel=4
  select @stype=stype from chalan where chctrlno=@chctrlno
  if @stype=1
  select @billprefix=substring(billprefix,1,2) + 'C' from toutlet where outlet=@outlet
  else 
  select @billprefix=substring(billprefix,1,2) + 'T' from toutlet where outlet=@outlet
declare @sdt datetime
declare @edt datetime
declare @year varchar(50)

set @sdt = dbo.getbofy(@m_billdate) 
set @edt = dbo.geteofy(@m_billdate)
 --set @year= '/' + substring(ltrim(rtrim(str(Year(@sdt)))),3,2) + '-' + substring(ltrim(rtrim(str(year(@edt)))),3,2)
set @year=''
if exists(select * from setting where billyear=1)
set @year= '/' + substring(ltrim(rtrim(str(Year(@sdt)))),3,2)
if exists(select doubleyear from setting where doubleyear='1')
set @year = @year + '-' + substring(ltrim(rtrim(str(year(@edt)))),3,2)

	set @retval= @g_billprefix + @billprefix + '/' + @bno1 + @year
return @retval

END

GO
/****** Object:  UserDefinedFunction [dbo].[getgstrater]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getgstrater](@outlet int,@amt money,@dt datetime,@opt int=0) RETURNS money
/* with encryption */
AS
BEGIN
declare @sttableno  int 
declare @strate money
declare @strate1 money
declare @stamt money
declare @lamt money
declare @discount money
if @opt=0
select @sttableno=sttableno from outlet where code=@outlet
else
select @sttableno=sttableno from phoutlet where outlet=@outlet

select @strate=rate,@discount=discount ,@lamt=lamt from sttabler where sttableno = @sttableno
and @amt between samt and eamt
and datediff(d,'1/1/2000',@dt) between datediff(d,'1/1/2000',sdt)  and datediff(d,'1/1/2000',edt) 

if @discount > 0 
set @stamt = (100 - @DISCOUNT ) * @STRATE/100
else
set @stamt = 100 * @strate/100

return @stamt
END
GO
/****** Object:  UserDefinedFunction [dbo].[GetHeads]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[GetHeads] (@gno numeric)
RETURNS varchar(1500) 
/* with encryption */
AS  
BEGIN 
declare @str1 varchar(1500)
declare @male int
declare @female int
declare @child int
select @child=child from guest where gno=@gno

if exists(select  * from grent where gno=@gno)
select @male=male ,@female=female from grent where gno=@gno and datediff(d,'1/1/2010',ci_Date)= (select max(datediff(d,'1/1/2010',ci_date)) from grent where gno=@gno)
else
select @male=male,@female=female from guest where gno=@gno

set @str1='M' + ltrim(str(@male)) + 'F' + ltrim(str(@female)) + 'C' + ltrim(str(@child))

return @str1
END



GO
/****** Object:  UserDefinedFunction [dbo].[getkots]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[getkots] (@bno numeric )
RETURNS varchar(100) 
/* with encryption */
AS  
BEGIN 
declare @kot numeric
declare @found tinyint
declare @rv varchar(100)
set @found=0
set @rv=''
declare ckot1 cursor for select distinct kot from kithist where bno=@bno
open ckot1
fetch next from ckot1 into @kot
while @@fetch_status=0
begin
set @found=1
set @rv=@rv + ltrim(str(@kot)) + ','
fetch next from ckot1 into @kot
end
if @found=1 set @rv=substring(@rv,1,len(@rv)-1)
return @rv
END



GO
/****** Object:  UserDefinedFunction [dbo].[getlic1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getlic1] 
(
@company numeric,@dt datetime
)
RETURNS varchar(100)
/* with encryption */
AS
BEGIN
declare @lic1 varchar(100)
select @lic1=gstin from company where company=@company
if @lic1<>'' 
return ''  --ltrim(rtrim(@lic1))

if exists(select * from lic where company=@company and datediff(d,'1/1/2000',@dt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt))
select @lic1=lic1 from lic where company=@company and datediff(d,'1/1/2000',@dt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
else
select @lic1=lic1 from company where company=1

return ltrim(rtrim(@lic1))
END
GO
/****** Object:  UserDefinedFunction [dbo].[getlic2]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getlic2] 
(
@company numeric,@dt datetime
)
RETURNS varchar(100)
/* with encryption */
AS
BEGIN
declare @lic2 varchar(100)
if exists(select * from lic where company=@company and datediff(d,'1/1/2000',@dt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt))
select @lic2=lic2 from lic where company=@company and datediff(d,'1/1/2000',@dt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
else
select @lic2=lic2 from company where company=1

return ltrim(rtrim(@lic2))
END
GO
/****** Object:  UserDefinedFunction [dbo].[getlic3]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getlic3] 
(
@company numeric,@dt datetime
)
RETURNS varchar(100)
/* with encryption */
AS
BEGIN
declare @lic3 varchar(100)
if exists(select * from lic where company=@company and datediff(d,'1/1/2000',@dt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt))
select @lic3=lic3 from lic where company=@company and datediff(d,'1/1/2000',@dt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
else
select @lic3=lic2 from company where company=1

return ltrim(rtrim(@lic3))
END

GO
/****** Object:  UserDefinedFunction [dbo].[GetLTRate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GetLTRate](@amt money,@txndt datetime)
RETURNS money
/* with encryption */
AS
BEGIN
	-- Declare the return variable here
	DECLARE @ResultVar money
	declare @tno int 
    set @amt= ceiling(@amt)
	select @tno=tno from ltmaster where datediff(d,'1/1/90',@txndt)  between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)
	select @ResultVar=lrate from lttable where tno = @tno and @amt between samt and eamt
	RETURN @ResultVar
END
GO
/****** Object:  UserDefinedFunction [dbo].[getmin]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[getmin] (@time varchar(10))  
RETURNS numeric 
/* with encryption */
AS  
BEGIN 
declare @hh int
declare @mm int
set @hh = cast(substring(@time,1,2) as numeric) * 60
set @mm = cast(substring(@time,4,2) as numeric)
return @hh+ @mm
END



GO
/****** Object:  UserDefinedFunction [dbo].[getmopdetails]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[getmopdetails] (@cno numeric(18,0))  
RETURNS varchar(50)  
/* with encryption */
AS  
BEGIN 
declare @gl varchar(50)
declare @name varchar(40)
declare @mop int
select @mop=mop,@name=crname from billhist1 where cno=@cno
set @gl=
case when @mop=0 then 'Cash'
when @mop=1 then 'Credit card'
when @mop=2 then 'DD/Cheque'
when @mop=3 then 'GL'
when @mop=4 then 'Room Account'
else 'Unknown'
end
if @mop=3  set @gl = @gl + '-' + ltrim(@name)
return @gl
END



GO
/****** Object:  UserDefinedFunction [dbo].[getmopgl]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getmopgl] (@cno integer) 
RETURNS varchar(200)
AS
BEGIN
	DECLARE @GLDESC VARCHAR(200)
	decLare @GLAC int
	SELECT @GLAC=GLACTNO FROM BILLHIST1 WHERE CNO=@CNO
    SELECT @GLDESC=CGP_dESC FROM GLPRIMLD WHERE  COMPANY=1 AND CGP_ACTNO=@GLAC
	RETURN @GLDESC
END
GO
/****** Object:  UserDefinedFunction [dbo].[GetNettRentF]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GetNettRentF] 
(
@brent decimal(18,2) , @heads int,@rno int,@Ontariff tinyint=0,@dt datetime='1 jan 1900',@pscrate decimal(18,4)=0,@mrenttype int=1 ,@m_adroom  int=1,@NREST int=0,@dtnow datetime=getdate
)
RETURNS numeric(18,2)
/* with encryption */
AS
BEGIN
declare @nettrent  decimal(18,2)
declare @prent decimal(18,4)
declare @mrent decimal(18,4)
declare @tariff1 decimal(18,4)
declare @tariff2 decimal(18,4)
declare @tariff decimal(18,4)
declare @strate decimal(18,4)
declare @p_ltontariff int
declare @p_STontariff varchar(5)
declare @g_scnettrent int
declare @lrate decimal(18,4)
declare @g_sc_ltrules int 
declare @trent decimal(18,4)
declare @errmsg varchar(5000)
declare @rent1 numeric(18,2)
declare @rent2 numeric(18,2)
declare @renti1 numeric(18,2)
declare @renti2 numeric(18,2)

if @dt='1 JAN 1900'  set @dt=@dtnow
if @brent=0 
	begin
		set @nettrent = 0
		return 0
	end

set @m_adroom = case when @m_adroom <=0 then 1 else @m_adroom end

select @p_ltontariff = case when ltontariff='Y' then 1 else 0 end from ltontariff where @dt between sdt and edt
select @p_stontariff=upper(ontariff) from sttable where @dt between sdt and edt
--exec [FindRent] @mrenttype,@rno , @heads, @dt ,0,@tariff out

select @rent1=rent1,@rent2=rent2,@renti1=renti1,@renti2=renti2 from rentmaster where rno=@rno and
rtypectrlno= (select ctrlno from rentperiod where rtype=@mrenttype and datediff(d,'1/1/2000',@dt)  between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt))
if @heads=1 
set @tariff=@rent1
else
set @tariff=@rent2




select @g_scnettrent=g_scnettrent ,@g_sc_ltrules=g_sc_ltrules from setting
select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
set @trent = @brent
set @brent=@brent/@m_adroom

if @p_stontariff='YES'
	set @strate = dbo.getstrate(@tariff,@dt)
else
	set @strate = dbo.getstrate(@brent,@dt)

if @NREST=1 SET @STRATE=0


--set @errmsg = convert(Varchar(20),@brent) goto e_Error 
--set @errmsg =  convert(Varchar(20),@strate) goto e_Error 
--set @errmsg =   convert(Varchar(20),@p_ltontariff) goto e_error
if @p_ltontariff=1   --YES
	begin
		if @brent <= @tariff
		begin
			if @g_scnettrent=0
				begin
				if @p_stontariff='YES'
					begin				
--					set @strate = dbo.getstrate(@tariff,@dt)
					set @mrent= @BRent - ((@lrate + @strate) / 100)
					end
				else  --p_stontariff='No'
					begin
						set @strate = dbo.GetStRate(@brent, @dt)
						set @mrent = @brent - ((@lrate) / 100 * @tariff)
						set @mrent = @mrent * 100 / (@strate + 100)
					end
				end
			else -- g_scnetrent=1
				begin
					set @MRent = @BRent - (((@lrate + @pSCRate) / 100) * @Tariff)
					If @P_STonTariff = 'YES' 
						begin
							If @G_SC_LTRULES=1 
								set @MRent = @BRent - ((@lrate + @pSCRate + @STRate) / 100 * @Tariff)
							Else
								begin
									set @MRent = @BRent - (@lrate / 100 * @Tariff)
									set @MRent = @MRent - ((@STRate / 100) * @Tariff)
									set @MRent = @MRent * (100 / (@pSCRate + 100))
								end
						end
					Else --' stontariff ='NO'
						begin
						If @G_SC_LTRULES=1
							begin
								set @MRent = @bRent - ((@lrate + @pSCRate) / 100 * @Tariff)
								if @p_stontariff='YES'
									set @strate = dbo.getstrate(@tariff,@dt)
								else
									set @strate = dbo.getstrate(@mrent,@dt)

								If @STRate > 0 set @MRent = @MRent * 100 / (@STRate + 100)
							end
						Else
							begin
								set @MRent = @BRent - (@lrate / 100 * @Tariff)
								If @pSCRate + @STRate > 0 set @MRent = @MRent * 100 / (@pSCRate + @STRate + 100)
							end
						end
				end
		end
		else -- rent > tariff
		begin
			select TOP 1 @lrate=lrate from lttable where EAMT >= @brent   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
			If @G_SCNettRent=0 
				If @P_STonTariff = 'YES'
					begin
						set @MRent = @BRent - (((@lrate + @STRate) / 100) * @BRent)
						If @MRent < @Tariff
							begin
								select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff  AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
								set @MRent = @BRent - (((@lrate + @STRate) / 100) * @Tariff)
							End
					end
				Else  --' if ston tariff = 'NO'
					begin
						set @MRent = @BRent * 100 / (@lrate + @STRate + 100)
						If @MRent < @Tariff
							begin
								select TOP 1 @lrate=lrate from lttable where EAMT >= @tariff  AND tno =  (select tno from ltmaster where @dt between sdt and edt)
								set @MRent = @BRent - (((@lrate) / 100) * @Tariff)
								set @MRent = @MRent * 100 / (@STRate + 100)
							end
					end
			else  --g_scnettrent=1
				begin
				If @P_STonTariff = 'YES'
					begin
						If @G_SC_LTRULES =1
							begin
								set @MRent = @BRent - (((@lrate + @STRate + @pSCRate) / 100) * @BRent)
								If @MRent < @Tariff

									begin
										select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff  AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
										set @MRent = @BRent - (((@lrate + @STRate + @pSCRate) / 100) * @Tariff)
									End
							end			
						Else
							begin
								set @MRent = @BRent * 100 / (@lrate + @STRate + 100)
								set @MRent = @MRent * 100 / (@pSCRate + 100)
								If @MRent < @Tariff
									begin 
										select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff  AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
										set @MRent = @BRent - (((@lrate + @STRate) / 100) * @Tariff)
										set @MRent = @MRent * 100 / (@pSCRate + 100)
									End 
							End 
					end
					Else  -- if ston tariff = 'NO'
					begin
						set @MRent = @BRent * 100 / (@lrate + @STRate + @pSCRate + 100)
						If @MRent < @Tariff 
							begin
								select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
								set @MRent = @BRent - (((@lrate+@pscrate) / 100) * @Tariff)
								set @MRent = @MRent * 100 / (@STRate + 100)
							End 
					End 
				end
		end
	end
else -- ltontariff=no
	begin
		select TOP 1 @lrate=lrate from lttable where EAMT >= @BRent AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
		If @G_SCNettRent=0
			begin
			                If @P_STonTariff = 'YES'
					begin
						set  @MRent = @BRent - (@STRate * @Tariff / 100)
						set @MRent = 100 / (100 + @lrate) * @MRent
					end
				Else
					set @MRent = (100 / (100 + @lrate + @STRate)) * @BRent
			end
	            Else
			If @P_STonTariff= 'YES' 
				begin
					set @MRent = @Tariff
					If @STRate <> 0
						set @MRent = @BRent - (@Tariff * @STRate / 100)
					Else
						set @MRent = @BRent
					set @MRent = (100 / (100 + @lrate + @pSCRate)) * @MRent
				end
			else
				begin
--PRINT 'PERIYA' + CONVERT(VARCHAR(20),@lrate) + ' ' + convert(varchar(20),@pscrate) + convert(varchar(20),@strate)   GOTO E_ERROR
					If @pSCRate <> 0 set @MRent = (100 / (100 + @pSCRate)) * @BRent   ELSE SET @MRENT=@BRENT
					set @PRent = @MRent
					select TOP 1 @lrate=lrate from lttable where EAMT >= @MRent AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
					set @MRent = (100 / (100 + @lrate + @pSCRate + @STRate)) * @BRent

-- this was included on 15-12-2013
declare @tlrate numeric(18,2)
select TOP 1 @tlrate=lrate from lttable where EAMT >= @MRent AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
--print convert(varchar(20),@tlrate) + ' ' +  convert(varchar(20),@lrate) goto e_error
--print convert(varchar(20),(@mrent/100 * (@lrate+@pscrate+@strate) ) + @mrent) goto e_error
if @tlrate <> @lrate
begin
set @lrate=@tlrate
while (@mrent/100 * (@lrate+@pscrate+@strate) ) + @mrent < @brent
begin
set @mrent = @mrent+1
set @strate = dbo.getstrate(@mrent,@dt)
select TOP 1 @tlrate=lrate from lttable where EAMT >= @MRent AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
end
end
---upto this 15-12-2013

					If @MRent  < @PRent
						begin
							select TOP 1 @lrate=lrate from lttable where EAMT >= @mRent  AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
							If @lrate = 0 And @StRate = 0 
								begin
									set @MRent = @BRent
									return 0
								End 
						end
				End 
	end	
if @mrent < 0 set @mrent=0
set @mrent = @mrent * @m_adroom
if abs((@mrent*100)-(@trent*100)) <=5  set @mrent=@trent

set @NettRent = case when @MRent < 0 then  0 else  @MRent end
return @nettrent
end
GO
/****** Object:  UserDefinedFunction [dbo].[GetNoshowGSTRate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GetNoshowGSTRate]
(
@dt datetime
)
RETURNS money
/* with encryption */
AS
BEGIN
declare @rate money
declare @sttableno int
select @sttableno=sttableno_noshow from setting 
select @rate=rate from sttabler where sttableno=@sttableno and datediff(d,'1/1/2000',@dt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
RETURN @rate
END
GO
/****** Object:  UserDefinedFunction [dbo].[Getnschgs]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[Getnschgs]  (@myctrlno numeric )
RETURNS money 
/* with encryption */
AS  
BEGIN 
declare @return money
declare @amt money
declare c1 cursor for select sum(amount) amount from sptbrs_noshow where rsmyctrlno = @myctrlno 
set @return = 0
open c1
fetch next from c1 into @amt
while @@fetch_status=0
begin
set @return=@return+@amt
fetch next from c1 into @amt
end
close c1
deallocate c1
return @return
END



GO
/****** Object:  UserDefinedFunction [dbo].[GetOB]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[GetOB] (@m_company int,@m_txndt datetime,@m_actnum numeric,@M_FY INT)
RETURNS decimal(18,2)
/* with encryption */
AS  
BEGIN 
declare @m_opgbal decimal(18,2)
DECLARE @M_FSDATE DATETIME
DECLARE @M_YEAR INT
DECLARE @M_BOFY DATETIME
DECLARE @M_OB AS MONEY
declare @m_oldtxndt as datetime
IF DATEPART(mm,@M_TXNDT) >=  1 AND DATEPART(mm,@M_TXNDT) <= 3
     SET @M_YEAR = DATEPART(yy,@M_TXNDT)-1
ELSE
     SET @M_YEAR = DATEPART(yy,@M_TXNDT)
SELECT @M_BOFY = '1 APR ' + STR(@M_YEAR)
SELECT @M_FSDATE = @M_TXNDT-1
SELECT @M_OB=0
SELECT @m_oldtxndt=ltxndt,@M_OB=OPGBAL FROM GLPRIMLD WHERE COMPANY=@M_COMPANY AND FY=@M_FY AND CGP_ACTNO= @M_ACTNUM
IF EXISTS (SELECT BALANCE FROM VOUCHERS WHERE TXNNO2=
    (SELECT TOP 1 TXNNO2  FROM VOUCHERS  WHERE  COMPANY=@M_COMPANY AND FY=@M_FY AND ACTNUM=@M_ACTNUM AND 
    DATEDIFF(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@m_fsdate)
    ORDER BY TXNNO2 DESC))
BEGIN
SELECT @M_OB = BALANCE FROM VOUCHERS WHERE TXNNO2=
    (SELECT TOP 1 TXNNO2  FROM VOUCHERS  WHERE  COMPANY=@M_COMPANY AND FY=@M_FY AND ACTNUM=@M_ACTNUM AND
    DATEDIFF(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@m_fsdate)
    ORDER BY TXNNO2 DESC)
END
ELSE
BEGIN

WHILE NOT EXISTS (SELECT BALANCE FROM VOUCHERS WHERE TXNNO2=
  (SELECT TOP 1 TXNNO2  FROM VOUCHERS  WHERE  COMPANY=@M_COMPANY AND FY=@M_FY AND ACTNUM=@M_ACTNUM AND
  DATEDIFF(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@m_fsdate)
  ORDER BY TXNNO2 DESC))
 BEGIN
    SELECT @M_FSDATE = @M_FSDATE -1
--print convert(varchar(20),@m_fsdate)
    IF EXISTS (SELECT BALANCE FROM VOUCHERS WHERE COMPANY=@M_COMPANY AND FY=@M_FY AND ACTNUM=@M_ACTNUM AND TXNNO2=
        (SELECT TOP 1 TXNNO2  FROM VOUCHERS  WHERE  COMPANY=@M_COMPANY AND FY=@M_FY AND ACTNUM=@M_ACTNUM AND
        DATEDIFF(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@m_fsdate)
        ORDER BY TXNNO2 desc))
       BEGIN 
         SELECT @M_OB=BALANCE FROM VOUCHERS WHERE COMPANY=@M_COMPANY AND FY=@M_FY AND ACTNUM=@M_ACTNUM AND TXNNO2=
         (SELECT TOP 1 TXNNO2  FROM VOUCHERS  WHERE  COMPANY=@M_COMPANY AND FY=@M_FY AND ACTNUM=@M_ACTNUM AND
         DATEDIFF(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@m_fsdate)
         ORDER BY TXNNO2 desc)
         BREAK
       END
   IF @M_FSDATE < @M_BOFY BREAK
   END

END
SELECT @m_OPGBAL=@M_OB
RETURN @m_opgbal
return 0
END



GO
/****** Object:  UserDefinedFunction [dbo].[getonedayrent]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Ramanathan
-- Create date: 05 Feb 2016
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getonedayrent] 
(
@crno int,
@head int,
@rent money,
@scrate money,
@renttype int,
@txndt datetime
)
RETURNS money
/* with encryption */
AS
BEGIN
	declare @ltrateontariff char(1)
	declare @ltontariff char(1)
	declare @ltamttemp money
	declare @amttemp money
	declare @amttemp1 money
	declare @g_ltonsc tinyint
	declare @g_disc_ltrules int
	declare @amt money
	declare @sc money
	declare @tno int
	declare @taxsystem int
	declare @sql varchar(8000)
	declare @tariff money
	set @ltamttemp=0
	set @amttemp=@rent
	select @ltontariff=ltontariff,@ltrateontariff=ltrateontariff from ltontariff where datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)
	select  top 1  @g_disc_ltrules=g_disc_ltrules ,@g_ltonsc=g_ltonsc from setting
	if @rent<>0
	set @amttemp = dbo.getrent(@renttype,@crno,@txndt,@head)
	set @tariff = @amttemp

	if @ltrateontariff='Y'
		set @amttemp1=case when @rent > @amttemp then @rent else @amttemp end
	else
		set @amttemp1 = @rent

	set @amttemp = @amttemp1
	set @amt = @rent
	if @ltontariff='Y' 	set @amt = @amttemp
	set @amt= isnull(@amt,0)

	if @scrate > 0 
		set @sc	= @amt * @scrate/100
	else
		set @sc =0

	if @g_ltonsc=1 set @amt=@amt+@sc
	select @tno=tno from ltmaster where convert(datetime,@txndt,106)  between sdt and edt
	declare @taxed money
	declare @nontaxable money
	declare @samt money
	declare @eamt money
	declare @famt money
	declare @amt2 money
	declare @ltamt money
	declare @lrate money

	set @taxed=0
	set @nontaxable=0
	--set @taxsystem=1
	select top 1 @taxsystem=g_taxsystem from setting
	if @taxsystem=1 
	begin
		declare cur_lttable cursor for select lrate,samt,eamt,famt from lttable where tno = @tno
		open cur_lttable
		fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
		set @amttemp = @amt
		while @@fetch_status=0 and @amt > 0
			begin
			      if @eamt > @amt  set @amt2 = @amt   else   set @amt2 = @eamt
			--if @amt2<>499 begin  print convert(varchar(20),@amt2) goto e_error end
			      set @ltamt = round(@ltamt + ((@amt2 + @famt) * @lrate / 100),2)
			   if @lrate > 0 set @taxed=@taxed + @amt2  else set @nontaxable=@nontaxable+@amt2

			      set @amt = @amt - @amt2
			      if @eamt > @amttemp set @amt2=@amttemp else set @amt2 = @eamt
			if @ltontariff='Y'
			      set @ltamttemp =  round(@ltamttemp+((@amt2+@famt)*@lrate/100),2)
			else
			      set @ltamttemp =  @ltamttemp+round(((@amt2+@famt)*@lrate/100),2)
			      set @amttemp=@amttemp-@amt2
			      fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
			end
			close cur_lttable
			deallocate cur_lttable
	end
	else
	begin
		if isnull(@amt,0) <> 0
		begin
			if @ltrateontariff='Y'
				declare ct1 cursor for select lrate from lttable where tno=@tno and case when @tariff > @amt then isnull(@tariff,0) else isnull(@amt,0) end   between samt and eamt
			else
				declare ct1 cursor for select lrate from lttable where tno=@tno and isnull(@amt,0)  between samt and eamt
			open ct1
			fetch next from ct1 into @lrate
			if @@fetch_status=0
			begin
	            set @ltamttemp =  round(@amt*@lrate/100,2)
			end
			close ct1
			deallocate ct1
		end
	end
	set @ltamt = @ltamttemp
	if @ltamt < 0 set @ltamt=0

declare @cessonwhat tinyint
declare @cessonwhat2 tinyint
declare @stontariff varchar(10)
declare @strate money
declare @stdiscount money
declare @stlessamt money
declare @cessrate money
declare @cessrate2 money
declare @st money
declare @cess money		
declare @cess2 money
	if @rent<>0
	begin
	select top 1  @stontariff=ontariff from sttable where (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
	
	if upper(@stontariff)='YES'
			begin
				set @tariff = dbo.getrent(@renttype,@crno,@txndt,@head)
				set @amttemp=case when @rent > @tariff then @rent else @tariff end
			end
		else
			set @amttemp=@rent
			set @strate = 0
		if exists(select * from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)))
       	select @cessonwhat2=cessonwhat2,@cessonwhat=cessonwhat,@strate=rate,@stdiscount=discount,@stlessamt=lamt,@cessrate=cess,@cessrate2=cess2  from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		set @strate = isnull(@strate,0)
		set @stdiscount = isnull(@stdiscount,0)
		set @stlessamt=isnull(@stlessamt,0)
		set @cessrate = isnull(@cessrate,0)
		set @cessrate2 = isnull(@cessrate2,0)

		set @amttemp = @amttemp - @stlessamt
		set @st=0
		set @cess=0
		set @cess2=0

		if @amttemp>0 
			begin
			if @strate<>0
				set @st = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@strate/100)) as decimal(12,2))
			else
				set @st=0

				if @cessonwhat=0
							begin
								if @cessrate > 0 
									set @cess = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@cessrate/100)) as decimal(12,2))
							end
							else
							begin
								if @st > 0 
									if @cessrate > 0 
										set @cess = @st * @cessrate / 100
							end
				if @cessonwhat2=0
							begin
								if @cessrate2 > 0 
									set @cess2 = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@cessrate2/100)) as decimal(12,2))
							end
							else
							begin
								if @st > 0 
									if @cessrate2 > 0 
										set @cess2 = @st * @cessrate2 / 100
							end

			end
	end

return @rent+@sc+@ltamt+@st+@cess+@cess2

END
GO
/****** Object:  UserDefinedFunction [dbo].[getpax]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[getpax]  (@brno numeric  )
RETURNS varchar(5000) 
/* with encryption */
AS  
BEGIN 
declare @return varchar(5000)
declare @rno int
declare @gno numeric
declare @m numeric
declare @f numeric
declare @c numeric
declare @mt numeric
declare @ft numeric
declare @ct numeric
set @mt=0
set @ft=0
set @ct=0

-- The RNO variable is used for Entry NO field (eno) - do not get confused
declare c1 cursor for select male,female,child,gno from guest where brno = @brno and opentoall<>'N'
set @return = ''
open c1
fetch next from c1 into @m,@f,@c,@gno
while @@fetch_status=0
begin
set @mt = @mt + @m
set @ft = @ft + @f
set @ct = @ct + @c
fetch next from c1 into @m,@f,@c,@gno
end
close c1
deallocate c1
set @return = 'M' + ltrim(rtrim(@mt)) + 'F' + ltrim(rtrim(@ft)) + 'C' + ltrim(rtrim(@ct))
return @return
END



GO
/****** Object:  UserDefinedFunction [dbo].[GetPmode]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 10-Aug-2013
-- Description:	For Restaurant Mode of Payment
-- =============================================
CREATE FUNCTION [dbo].[GetPmode] 
(
@mop int
)
RETURNS varchar(50)
/* with encryption */
AS
BEGIN
	DECLARE @ResultVar varchar(50)
	select @resultvar=name from restpmode where pmode=@mop
	return @resultvar
END

GO
/****** Object:  UserDefinedFunction [dbo].[GetPreference]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[GetPreference]( @str2 varchar(1500))
RETURNS varchar(50) 
/* with encryption */
AS  
BEGIN 
declare @cnt numeric
declare @str varchar(50)
set @cnt = 0
if @str2<>''  select @cnt=count(*) from  dbo.fn_split(@str2,'***')
set @str = replicate('N',@cnt+2)
return @str
END



GO
/****** Object:  UserDefinedFunction [dbo].[GetRent]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GetRent] 
(
	-- Add the parameters for the function here
	@rtype int,@rno numeric,@dt datetime,@heads int
)
RETURNS numeric(18,2)
/* with encryption */
AS
BEGIN

declare @rent decimal
declare @rent1 decimal
declare @rent2 decimal
declare @renti1 decimal
declare @renti2 decimal

select @rent1=rent1,@rent2=rent2,@renti1=renti1,@renti2=renti2 from rentmaster where rno=@rno and
rtypectrlno= (select ctrlno from rentperiod where rtype=@rtype and datediff(d,'1/1/2000',@dt)  between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt))


	if @heads=1 
	set @rent=@rent1
	else
	set @rent=@rent2
return @rent

END



GO
/****** Object:  UserDefinedFunction [dbo].[GetRentminuscommn]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GetRentminuscommn]
(
   @gno int
)
RETURNS money
AS
BEGIN
declare @rent money
declare @commctrlno numeric(18)
declare @commrate money
declare @gstrate money
declare @tdsrate money
declare @tcsrate money
declare @commission money
declare @gst money
declare @tcs money
DECLARE @TDS MONEY
declare @netamt money
declare @returnamount money
declare @txndt datetime
declare @tno numeric
declare @lrate money
declare @lt money
declare @nettrent money
select @rent=rent,@txndt=ci_date,@commctrlno=otacommctrlno,@nettrent=nettrent  from guest where gno= @gno

select @tno=tno from ltmaster where @txndt between sdt and edt
select @lrate=lrate from lttable where tno=@tno and @rent between samt and eamt

set @lt = @rent * @lrate/100

set @netamt=@rent+@lt

select @commrate=commrate, @gstrate=gstrate,@tcsrate=tcsrate,@tdsrate=tdsrate from otacommission where ctrlno=@commctrlno

set @commission= @rent * @commrate/100

set @gst=@commission * @gstrate/100

set @tds = @rent * @tdsrate/100

set @tcs = @rent * @tcsrate/100

set @returnamount = @nettrent-(@commission +@gst+@tcs+@tds)
return isnull(@returnamount ,0)



END
GO
/****** Object:  UserDefinedFunction [dbo].[getrnos]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[getrnos]  (@brno numeric  )
RETURNS varchar(5000) 
/* with encryption */
AS  
BEGIN 
declare @return varchar(5000)
declare @rno int
declare @gno numeric
declare c1 cursor for select rno,gno from guest where brno = @brno and opentoall<>'N'
set @return = ''
open c1
fetch next from c1 into @rno,@gno
while @@fetch_status=0
begin
if @return='' 
	set @return=@return + rtrim(ltrim(str(@rno)))
else
	set @return=@return + ','+ rtrim(ltrim(str(@rno)))

declare c2 cursor for select rno from addroom where gno=@gno
open c2
fetch next from c2 into @rno
while @@fetch_status=0
begin
if @return='' 
	set @return=@return + rtrim(ltrim(str(@rno)))
else
	set @return=@return + ','+ rtrim(ltrim(str(@rno)))
fetch next from c2 into @rno
end
close c2
deallocate c2

fetch next from c1 into @rno,@gno
end
close c1
deallocate c1
return @return
END



GO
/****** Object:  UserDefinedFunction [dbo].[getrnos1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[getrnos1]  (@brno numeric  )
RETURNS numeric 
/* with encryption */
AS  
BEGIN 
declare @return numeric
declare @rno int
declare @gno numeric
declare c1 cursor for select rno,gno from guest where brno = @brno and opentoall<>'N'
set @return = 0
open c1
fetch next from c1 into @rno,@gno
while @@fetch_status=0
begin
set @return = @return  + 1
declare c2 cursor for select rno from addroom where gno=@gno
open c2
fetch next from c2 into @rno
while @@fetch_status=0
begin
set @return = @return  + 1
fetch next from c2 into @rno
end
close c2
deallocate c2

fetch next from c1 into @rno,@gno
end
close c1
deallocate c1
return @return
END



GO
/****** Object:  UserDefinedFunction [dbo].[GetStock]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[GetStock] ( @ctrlno numeric=0,@txndt datetime)
RETURNS numeric 
/* with encryption */
AS  
BEGIN 
declare @opgbal numeric
set @opgbal=0
select
@opgbal=case when amttyp='D' then @opgbal-qty else @opgbal+qty end from storehist
where hctrlno in (select top 100000 hctrlno from storehist where ctrlno=@ctrlno 
and datediff(d,@txndt,txndt) < 0)
return @opgbal
END



GO
/****** Object:  UserDefinedFunction [dbo].[GetStockFast]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GetStockFast]
( @ctrlno numeric,@txndt datetime,@fy numeric=0)
RETURNS numeric(18,4)
/* with encryption */
AS
BEGIN
declare @balance numeric
declare @sdt datetime
declare @cr numeric
declare @dr numeric
declare @stock numeric
    select @sdt=date from finyear where fy=@fy
	select @balance=isnull(opgbal,0) from storeprimary where ictrlno=@ctrlno and fy=@fy
	--select 
	--@cr=sum(case when amttyp='C' then qty else 0 end),
	--@dr=sum(case when amttyp='D'then qty else 0 end)
	--from storehist where ctrlno=@ctrlno and datediff(d,'1/1/2000',txndt) 
	--between datediff(d,'1/1/2000',@sdt) and datediff(d,'1/1/2000',@txndt)

	select 
	@cr=SUM(QTY1)
	from storehist where ctrlno=@ctrlno and datediff(d,'1/1/2000',txndt) 
	between datediff(d,'1/1/2000',@sdt) and datediff(d,'1/1/2000',@txndt)

	RETURN isnull(isnull(@balance,0)+isnull(@cr,0),0)

END
GO
/****** Object:  UserDefinedFunction [dbo].[GetStockFast2]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GetStockFast2]
( @ctrlno numeric,@txndt datetime,@fy numeric=0,@hctrlno2 numeric)
RETURNS numeric(18,4)
/* with encryption */
AS
BEGIN
declare @balance numeric
declare @sdt datetime
declare @cr numeric
declare @dr numeric
declare @stock numeric
    select @sdt=date from finyear where fy=@fy
	select @balance=isnull(opgbal,0) from storeprimary where ictrlno=@ctrlno and fy=@fy
	select 
	@cr=sum(case when amttyp='C' then qty else 0 end),
	@dr=sum(case when amttyp='D'then qty else 0 end)
	from storehist where ctrlno=@ctrlno and datediff(d,'1/1/2000',txndt) 
	between datediff(d,'1/1/2000',@sdt) and datediff(d,'1/1/2000',@txndt) 
	and hctrlno2 not in (
	select hctrlno2 from storehist where ctrlno=100 and datediff(d,txndt,@txndt)=0 and ( amttyp='D' 
	or (amttyp='C' and hctrlno2 < @hctrlno2))
	)

	RETURN @balance+isnull(@cr,0)-isnull(@dr,0)

END

GO
/****** Object:  UserDefinedFunction [dbo].[GetStockNew]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[GetStockNew] ( @ctrlno numeric,@txndt datetime,@fy numeric=0,@hctrlno2 numeric=0 )
RETURNS numeric 
/* with encryption */
AS  
BEGIN 
declare @balance numeric
declare @company numeric
declare @bofy datetime
declare @eofy datetime
declare @hctrlno2b numeric
set @balance=0

	--select @company=company from store where storeno = (select storeno from storeitems where ctrlno=@ctrlno)
	--set @fy = dbo.findfy(@company,@txndt)
	--set @bofy = dbo.getbofy(@txndt)
	--set @eofy = dbo.geteofy(@txndt)

	--dbo.spsthrewritefunction(@ctrlno, @fy, @bofy,@eofy)

	select @balance=opgbal from storeprimary where ictrlno=@ctrlno and fy=@fy
	if exists(select * from storehist where ctrlno=@ctrlno and datediff(d,txndt,@txndt)=0)
		select @balance=balance,@hctrlno2b=hctrlno2 from storehist where ctrlno=@ctrlno and datediff(d,txndt,@txndt)=0
		and hctrlno2=(select top 1 hctrlno2 from storehist where ctrlno=@ctrlno and datediff(d,txndt,@txndt)=0 order by txndt desc,amttyp desc,hctrlno2 desc)
	else
		if exists(select * from storehist where ctrlno=@ctrlno and datediff(d,txndt,@txndt)>0)
			begin
			select  @balance=balance,@hctrlno2b=hctrlno2 from storehist where ctrlno=@ctrlno and datediff(d,txndt,@txndt)>0 
			and hctrlno2=(select top 1 hctrlno2 from storehist where ctrlno=@ctrlno and datediff(d,txndt,@txndt)>0 order by txndt desc,amttyp desc,hctrlno2 desc)
			end

if @hctrlno2 <> 0 
begin
	declare @bal money
	declare @htl2 numeric
	declare @found int
	set @found = 0
	declare c1 cursor for select hctrlno2,balance from storehist 
	where ctrlno=@ctrlno and hctrlno2 > @hctrlno2b 
	order by datediff(d,'1/1/2000',txndt),amttyp ,hctrlno2 
	open c1 
	fetch next from c1 into @htl2,@bal
	while @@fetch_status=0 and @found=0
	begin
		if @htl2=@hctrlno2 
		begin
		set @found=1
		end
		else
		set @balance=@bal

	fetch next from c1 into @htl2,@bal
	end
	close c1
	deallocate c1
end

return @balance
end





GO
/****** Object:  UserDefinedFunction [dbo].[GetStockRate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[GetStockRate] 
(
	-- Add the parameters for the function here
	@ctrlno numeric, @txndt datetime
)
RETURNS numeric(18,2)
/* with encryption */
AS
BEGIN
	-- Declare the return variable here
	DECLARE @ResultVar numeric(18,2)

select top 1 @resultvar=rate from storehist where adjust ='N' and amttyp='C' and ctrlno=@ctrlno and datediff(d,@txndt,txndt) < = 0
order by txndt desc

	-- Return the result of the function
	RETURN @ResultVar

END

GO
/****** Object:  UserDefinedFunction [dbo].[getstrate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[getstrate] (@tariff numeric(18,2),@dt datetime='1 jan 2010'  )
RETURNS decimal(18,4) 
/* with encryption */
AS  
BEGIN 
	declare @strate decimal(18,4)
	declare @rate decimal(18,4)
	declare @cess decimal (18,4)
	declare @cess2 decimal (18,4)
	declare @discount decimal(18,4)
	declare @cessamt decimal(18,4)
	declare @cessamt2 decimal(18,4)
	declare @cessonwhat tinyint 
	declare @cessonwhat2 tinyint 
	select @cessonwhat=cessonwhat,@rate=isnull(rate,0) ,@discount=isnull(discount,0),@cess=isnull(cess,0),@cess2=isnull(cess2,0) from sttable 
	where @tariff  between samt and eamt and CONVERT(varchar(20),@dt,106) between sdt and edt
	set @cessamt = 0
	set @strate = 0
    if @cessonwhat=0
		begin
			set @rate = @rate + @cess + @cess2
			If @discount <> 0
				set @strate = (@rate - (@Rate * @discount / 100))
			Else
				set @strate = @Rate 
		end
	else
		begin
			If @discount <> 0
				set @strate = (@rate - (@Rate * @discount / 100))
			Else
				set @strate = @Rate 
			set @cessamt =  @strate * ((@cess+@cess2)/100)
		end



	set @strate = isnull(@strate,0) + isnull(@cessamt,0) 
	return @strate
end


GO
/****** Object:  UserDefinedFunction [dbo].[getsttabler]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getsttabler] 
(
  @tableno int,@amt decimal(18,2),@dt datetime
)
RETURNS int
AS
BEGIN
declare @rate int
select @rate=rate from sttabler where sttableno=@tableno 
and @dt between sdt and edt
and @amt between samt and eamt

	RETURN @rate
END
GO
/****** Object:  UserDefinedFunction [dbo].[getttstrate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getttstrate](@outlet int,@dt datetime,@stype int)
RETURNS money
/* with encryption */
AS
BEGIN
declare @rate money
declare @sttableno int
select @sttableno=sttableno from ta_servicetaxsetting where outlet=@outlet and servicetype=@stype
select @rate=rate from sttabler where sttableno=@sttableno and datediff(d,'1/1/2000',@dt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
RETURN @rate
END
GO
/****** Object:  UserDefinedFunction [dbo].[getusername]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[getusername]() 
RETURNS varchar(100)
/* with encryption */
AS
BEGIN
declare @usernm varchar(100)
select @usernm=usernm from comuser where sessionid='' and [host_id]=host_id()
return @usernm
END
GO
/****** Object:  UserDefinedFunction [dbo].[mop]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[mop] (@mop int)  
RETURNS varchar(20)  
/* with encryption */
AS  
BEGIN 
return
case when @mop=0 then '1Cash'
when @mop=1 then 'Credit card'
when @mop=2 then 'DD/Cheque'
when @mop=3 then 'GL Account'
when @mop=4 then 'Room Account'
else 'Unknown'
end

end

GO
/****** Object:  UserDefinedFunction [dbo].[pbpga4]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/*PowerBrain - 21-Nov-2006
 test for category wise room status 24-feb-2006 Rm.Ramanathan
*/
CREATE FUNCTION  [dbo].[pbpga4](@dt datetime,@dt2 datetime ,@category varchar(50))
RETURNS numeric 
/* with encryption */
AS  
BEGIN 
declare @ar numeric
exec pbpga2 @dt ,@dt2,@category ,@ar out
return @ar
END



GO
/****** Object:  UserDefinedFunction [dbo].[pwcheck]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[pwcheck] (@ServiceNo numeric , @Outlet numeric = 0,@outletType Integer = 0,@userno numeric = 0)
RETURNS int 
/* with encryption */
AS  
BEGIN 
declare @defaultpermittedto int
declare @cnt numeric
declare @ok int
select @defaultpermittedto=defaultpermittedto from servicenumbers where serviceno=@serviceno
If @defaultpermittedto = 1  return 0
If @userno = 0 return 2
If @serviceno = 0 return 3

If @OutletType = 0 
	begin
		SELECT  @cnt=count(*)  FROM SERVICEPERMISSION WHERE USERNO = @userno  AND SERVICENO = @ServiceNo
		If isnull(@cnt,0) > 1 
			begin
				if exists(select top 1 * FROM SERVICEPERMISSION WHERE USERNO = @userno  AND SERVICENO = @ServiceNo  and permission='Y') 
				set @ok = 0
				else
				set @ok=1
			end
		else
			begin
				if exists(SELECT  * FROM SERVICEPERMISSION WHERE USERNO = @userno AND SERVICENO = @ServiceNo  and permission='Y') 
					set @ok=0
				else
					set @ok=1
			End 
	end
Else
	begin
		if exists(select * from servicepermission where userno = @userno  and serviceno = @ServiceNo and outlet = @Outlet  and outlettype = @OutletType)
		set @ok=0
		else
		set @ok=1
	end
return @ok
end



GO
/****** Object:  UserDefinedFunction [dbo].[shfrooms]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[shfrooms] (@cno numeric)  
RETURNS varchar(1500) 
/* with encryption */
AS  
BEGIN 
declare @cnt numeric
declare @rno numeric
declare @str1 varchar(1500)
declare @first tinyint
set @first=0
set @str1 = ''
select @cnt=count(*) from guest where brno in (select brno from billhist1 where cno=@cno)
if @cnt > 1 return ''
select @cnt=gno from guest where brno in (select brno from billhist1 where cno=@cno)
declare c1 cursor for select rno from groom where gno=@cnt  order by ci_date desc
open c1

fetch next from c1 into @rno
while @@fetch_status=0
begin
if patindex('%' + ltrim(rtrim(str(@rno))) + '%',@str1)=0 
if @first<>0 set @str1 = @str1 + ltrim(rtrim(str(@rno))) + ','
set @first=1
fetch next from c1 into @rno
end

close c1
deallocate c1
if len(@str1) > 1 set @str1 =  substring(@str1,1,len(@str1)-1)
return @str1
END



GO
/****** Object:  UserDefinedFunction [dbo].[SPKBLNEWFunction]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[SPKBLNEWFunction] 
(
@outlet numeric,@type int,@code varchar(50),@opt int
)
RETURNS  decimal(18,2)
/* with encryption */
AS
BEGIN
Declare @amt decimal(18,2)
declare @servicetax numeric(18,2)
declare @cess numeric(18,2)
declare @servicetax1 numeric(18,2)
declare @cess1 numeric(18,2)
declare @cess2 numeric(18,2)
declare @cess21 numeric(18,2)
declare @sql varchar(5000)
declare @couponamt numeric(18,2)
declare @txndt datetime
declare @g_staxincl char(1)
declare @vatonst int 
declare @bofy datetime
declare @year numeric
declare @drate numeric(18,2) 
declare @sttableno numeric
declare @test numeric
declare @rv numeric(18,2)
declare @rtot numeric(18,2)
declare @rtax numeric(18,2)
declare @rsc numeric(18,2)
declare @rst numeric(18,2)

set @test = 999999
			set @servicetax=0
			set @cess = 0
			set @servicetax1=0
			set @cess1 = 0
			set @cess2 = 0
			set @cess21 = 0

set @drate = 0 
set @couponamt = 0
				set @couponamt = isnull(@couponamt,0)
				set @bofy = dbo.getbofy(@txndt)
				set @year = datepart(yy,@bofy)
declare @scset int
declare @stset int
declare @scrate numeric(18,2)
declare @sc_table int
declare @sc_room int
declare @sc_door int
declare @servicetaxeligible char(1)
declare @selfdeal char(10)
declare @showsc int
declare @total numeric(18,2)
				select @total= 0

declare @saleamt numeric(18,2)
declare @sctotal numeric(18,2)
declare @disctotal numeric(18,2)
declare @taxorgst int
declare @itemcode numeric
declare @tax numeric(18,2)
declare @qty numeric
declare @rate numeric(18,2)
declare @disc numeric(18,2)
declare @scamt numeric(18,2)
declare @g_scvat int
declare @cscrate numeric(18,2)
declare @scadd  numeric(18,2)
declare @stadd  numeric(18,2)
declare @stonsc int
declare @strate  numeric(18,2)

---- for getservicetax funtion replacement
declare @cessonwhat int
declare @cessonwhat2 int
declare @discount numeric(18,2)
declare @strate1 numeric(18,2)
declare @lamt numeric(18,2)
declare @cessrate numeric(18,2)
declare @cess2rate numeric(18,2)
declare @dt datetime
declare @cessamt numeric(18,2)
declare @stamt numeric(18,2)
declare @base numeric(18,2)
DECLARE @CNT INT
DECLARE @VAT MONEY
SET @CNT=0
set @rv  = 0
set @rtot = 0 
set @rtax= 0
set @rsc = 0
set @rst = 0
				set @saleamt = 0
				set @sctotal = 0
				set @disctotal = 0
select @g_scvat=g_scvat from (select top 1 * from setting) a
declare tempcur1 cursor for select distinct outlet from currentkot where pend_type=@type and pend_code=@code
open tempcur1
fetch next from tempcur1 into @outlet
while @@FETCH_STATUS=0
begin
				select @txndt=curdate,@g_staxincl=g_staxincl,@vatonst=vatonst,@sttableno=sttableno from outlet where code=@outlet
				select @scset=scset,@stset=stset,@selfdeal=selfdeal,@scrate=scrate,
				@sc_table=sc_table,@sc_room=sc_room,@sc_door=sc_door,@servicetaxeligible=servicetax,@vatonst=vatonst,
				@showsc=showsc
				from outlet  where code = @outlet

				declare tempcur cursor for select code,sum(qty) qty ,max(rate) rate,max(strate) strate,max(scrate) scrate from currentkot where outlet=@outlet and pend_type=@type and pend_code=@code group by code
				open tempcur
				fetch next from tempcur into @itemcode,@qty,@rate,@VAT,@Cscrate
				while @@fetch_status=0
				begin
				-----
				select @taxorgst=taxorgst from kitprime where icode=(select icode from kitmast where code=@itemcode)
				select @tax=0
				select @amt = @qty*@rate
				select @disc= @amt*@drate/100
				select @saleamt = @saleamt + @amt
				select @disctotal = @disctotal + @disc

				set @scamt=0
				--if (@type=1 and @sc_table=1) or (@type=2 and @sc_room=1) or (@type=3 and @sc_door=1)
				if @showsc=1 set @scrate=@cscrate
				if @scset=0
					select @scamt=round(@amt*@scrate/100,2)
				else
					select @scamt=round((@amt-@disc)*@scrate/100,2)


				set @sctotal = @sctotal + @scamt

				if @g_scvat=1 
					set @scadd = @scamt
				else
				set @scadd = 0
				set @cess21 = 0
				set @servicetax1 = 0
				set @cess1 = 0
				set @stadd=0
				declare @stable money
				if @taxorgst=0
					begin
						if @servicetaxeligible = 'Y'
							begin
								select @stonsc=g_stonsc from setting
								if @stonsc=1 
										select @stable = @amt + @scamt - @disc
								else
										select @stable = @amt  - @disc 

								------------------------
								------------------------

								set @dt=@txndt
								select @cessonwhat=cessonwhat,@cessonwhat2=cessonwhat2,@strate1=rate,@cessrate=cess, @discount=discount,@lamt=lamt,@cess2rate=cess2  from sttabler where  
								sttableno=@sttableno and(@stable >= samt and @stable <= eamt ) and datediff(d,'1/1/2000',@dt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)

								set @cessamt=0
								set @stamt = 0
								if @discount > 0 
									set @base = @stable -  (@stable * @discount /100)
								else
									set @base = @stable

								if @strate1 > 0 
									set @stamt = @base * @strate1/100
									else
									set @stamt = 0
								if @cessonwhat=0
									begin
									if @cess > 0
											set @cessamt = @base * @cess/100
										else
											set @cessamt = 0
									end
								else
									begin
										if @CESS > 0 
											if @stamt > 0 
												set @cessamt = @stamt * @cess/100
									end
								set @cess2=0
								if @cessonwhat2=0
									begin
									if @cess2rate > 0
											set @cess21 = @base * @cess2rate/100
										else
											set @cess21 = 0
									end
								else
									begin
										if @CESS2rate > 0 
											if @stamt > 0 
												set @cess21 = @stamt * @cess2rate/100
									end

								set @servicetax1 = round(@stamt,2,1)
								set @cess1 = round(@cessamt,2,1)
								set @cess21 = round(@cess21,2,1)
								------------------------
								------------------------

								set @servicetax = @servicetax + @servicetax1
								set @cess = @cess + @cess1
								set @cess2 = @cess2 + @cess21
							end
						if @vatonst= 1 
							set @stadd = @servicetax1+@cess1
						else
							set @stadd = 0
					end
				else
					begin
					set @test = 7777777
								select @stable = @scamt + @amt
								------------------------
								------------------------
								set @dt=@txndt
								select @cessonwhat=cessonwhat,@cessonwhat2=cessonwhat2,@strate1=rate,@cessrate=cess, @discount=discount,@lamt=lamt,@cess2rate=cess2  from sttabler where  
								sttableno=@sttableno and(@stable >= samt and @stable <= eamt ) and datediff(d,'1/1/2000',@dt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)

								set @cessamt=0
								set @stamt = 0
								if @discount > 0 
									set @base = @stable -  (@stable * @discount /100)
								else
									set @base = @stable

								if @strate1 > 0 
									set @stamt = @base * @strate1/100
									else
									set @stamt = 0

								if @cessonwhat=0
									begin
									if @cess > 0
											set @cessamt = @base * @cess/100
										else
											set @cessamt = 0
									end
								else
									begin
										if @CESS > 0 
											if @stamt > 0 
												set @cessamt = @stamt * @cess/100
									end
								set @cess2=0
								if @cessonwhat2=0
									begin
									if @cess2rate > 0
											set @cess21 = @base * @cess2rate/100
										else
											set @cess21 = 0
									end
								else
									begin
										if @CESS2rate > 0 
											if @stamt > 0 
												set @cess21 = @stamt * @cess2rate/100
									end

								set @servicetax1 = round(@stamt,2,1)
								set @cess1 = round(@cessamt,2,1)
								set @cess21 = round(@cess21,2,1)
								------------------------
								------------------------

								set @servicetax = @servicetax + @servicetax1
								set @cess = @cess + @cess1
								set @cess2 = @cess2 + @cess21
					end
					
				if @stset=0
					select @tax=round((@amt + @scadd + @stadd)*@VAT/100,2)
				else
					select @tax=round(((@amt + @scadd + @stadd) - @disc)*@VAT/100,2)
				
				/* Service Charges Calculation according to the Mode of the bill modified on 27/07/2009   further modified on 13 Marc 2018***/
				select @total = ( @total + @amt + @tax + @scamt + @servicetax1 + @cess1 + @cess21) - @disc
				set @rtax = @rtax + @tax
				set @rsc = @rsc + @scamt
				set @rst = @rst + @servicetax1 + @cess1 + @cess21
			SET @CNT=@CNT+1

			fetch next from tempcur into @itemcode,@qty,@rate,@VAT,@showsc
				end
				close tempcur
				deallocate tempcur
				set @rtot = @rtot + @total 
				set @total = 0 
				fetch next from tempcur1 into @outlet
				end
				close tempcur1
				deallocate tempcur1
	-- Return the result of the function
	SElecT @RV = Case 
	when @opt=0 THEN  @rtot
	when @opt=1 THEN  @rtax
	when @opt=2 THEN  @rsc
	when @opt=3 THEN @RST
	END 
	return @rv

END

GO
/****** Object:  UserDefinedFunction [dbo].[TTSTType]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [dbo].[TTSTType]
(
@outlet int,@servicetype int
)
RETURNS int
/* with encryption */
AS
BEGIN
declare @method int
select @method=method from ta_servicetaxsetting where outlet=@outlet and servicetype=@servicetype-1
return isnull(@method,0)

END
GO
/****** Object:  UserDefinedFunction [dbo].[VatEligible]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 22 Aug 2017
-- Description:	Returns  Vat Eligibility
-- =============================================
CREATE FUNCTION [dbo].[VatEligible] 
(
@outlet int  ,@txndt datetime
)
RETURNS int
/* with encryption */
AS
BEGIN
declare @mt integer
select @mt=vat from vattabler where outlet=@outlet and
datediff(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
return isnull(@mt,0)
END
GO
/****** Object:  UserDefinedFunction [dbo].[vouddet]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[vouddet] (@grno numeric,@txnno numeric)  
RETURNS varchar(8000) 
/* with encryption */
AS  
BEGIN
declare @fy numeric
declare @txndt datetime
declare @return varchar(8000)
declare @actnum numeric
declare @amttyp char(1)
declare @amt money
declare @cusname varchar(50)
declare @sb char(1)
declare @eb char(1)
set @return = ''
select @txndt=txndt,@fy=fy from vouchers where txnno=@txnno
declare c1 cursor for select actnum,amttyp,amt from vouchers where fy=@fy and grno=@grno and datediff(d,txndt,@txndt) =0 and txnno <> @txnno order by amttyp
open c1
fetch next from c1 into @actnum,@amttyp,@amt
while @@fetch_status=0 begin
select @cusname=cgp_Desc from glprimld where fy=@fy and cgp_actno=@actnum
if @amttyp='D' set @sb='(' else set @sb= ''
if @amttyp='D' set @eb=')' else set @eb = ''
set @return = @return + ltrim(rtrim(@sb + ltrim(rtrim(@cusname)) + '=' + ltrim(rtrim(convert(varchar(20),@amt))) + @eb))+ ' '
fetch next from c1 into @actnum,@amttyp,@amt
end
return @return
END
/*
CREATE FUNCTION  pbpga4(@dt datetime,@dt2 datetime ,@category varchar(50))
RETURNS numeric AS  
BEGIN 
declare @ar numeric
exec pbpga2 @dt ,@dt2,@category ,@ar out
return @ar
END
*/



GO
/****** Object:  StoredProcedure [dbo].[addrooms]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- 0503CM
-- 1106E
-- 1508D
--test 13 Oct 2015
/* PowerBrain Software (India) New Delhi*/
CREATE PROCEDURE [dbo].[addrooms] @gno numeric,@rnostring varchar(50) out 
/* with encryption */
as
declare @rno numeric
declare tcar cursor for select rno from addroom where gno = @gno
open tcar
fetch next from tcar into @rno
while @@fetch_status=0 begin
 set @rnostring = @rnostring + ',' + ltrim(str(@rno))
 fetch next from tcar into @rno
end
close tcar
deallocate tcar




GO
/****** Object:  StoredProcedure [dbo].[avrrent]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[avrrent] @dt1 datetime,@dt2 datetime ,@temp varchar(50)
/* with encryption */
AS
--if cursor_status('global','c123') > 0
--begin
--close c123
--deallocate c123
--end

declare @dt datetime
declare @dt3 datetime
declare @cnt numeric
declare @totrent money
declare @avg numeric
declare @table varchar(40)
declare @sql varchar(2000)
declare @days numeric
set @days = datediff(d,@dt1,@dt2)+1
set @dt = @dt1
set nocount on 

select ctrlno,gno,txndt,rent+otherchgs rent ,discount into #temp1 from billhist where datediff(d,'1/1/2004',txndt)  between datediff(d,'1/1/2004', @dt1) and datediff(d,'1/1/2004',@dt2) and rent<>0

set @sql = 'insert into #temp1 select rno,txndt,rent+otherchgs,discount  from ' + @temp +  ' where datediff(d,''1/1/2004'',txndt)  between datediff(d,''1/1/2004'',''' +  convert(varchar(20),@dt1)  +''') and datediff(d,''1/1/2004'','''+ convert(varchar(20),@dt2)  + ''') and rent<>0'
exec(@sql)

delete from  #temp1 where ctrlno in (
select min(ctrlno) ctrlno  from #temp1 where gno in (select gno from guest where datediff(d,dateadd(d,1,@dt2),ci_Date) = 0)  and datediff(d,txndt,@dt2)=0 )

select ctrlno='000000' ,dt=min(txndt),nor=count(*),rent=sum(rent-discount),arr=sum(rent-discount)/count(*)  into #temp from 
#temp1 group by datediff(d,'1/1/2004',txndt)

declare @x numeric
set @x=0
declare c1 cursor for select dt from #temp order by dt
open c1
fetch next  from c1 into @dt
while @@fetch_status=0
begin
set @x = @x+1
update #temp set ctrlno=ltrim(rtrim(str(@x))) where dt=@dt
fetch next  from c1 into @dt
end
close c1
deallocate c1
select @cnt=sum(nor) ,@totrent=sum(rent),@avg=sum(arr) from #temp
--insert into #temp values ('Total ' , '31 dec 3999',@cnt,@totrent,@avg/@days)
insert into #temp values ('Total ' , '31 dec 3999',@cnt,@totrent,@totrent/@cnt)
set nocount off
select ctrlno [S.No] ,convert(varchar(20),dt,106)  [Txn.Date],nor [No.of Rooms],cast(rent  as numeric(12,2)) [Total Rent],round(cast(arr as numeric(12,2)),2) [Average Rent] from #temp order by dt




GO
/****** Object:  StoredProcedure [dbo].[avrrentold]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[avrrentold] @dt1 datetime,@dt2 datetime 
/* with encryption */
AS
declare @dt datetime
declare @dt3 datetime
declare @cnt numeric
declare @totrent money
declare @avg numeric
declare @table varchar(40)
declare @sql varchar(2000)
declare @days numeric
set @days = datediff(d,@dt1,@dt2)+1
set @dt = @dt1
set nocount on 
create table #temp ( ctrlno numeric identity , dt varchar(20) , nor int, rent numeric(9,2), arr numeric(9,2)) 

while datediff(d,@dt,@dt2) >=0
begin
set @dt3 = dateadd(d,-1,@dt)

select @cnt=count(*),@totrent=sum(rent-discount),@avg=sum(rent-discount)/count(*)   from 
(
select gno,eno,name,rno,rent,discount,
nett,lt,c1,c2 from ( select gno,eno=(select eno from guest where gno=billhist.gno),
name=(select name from guest where gno=billhist.gno) ,rno=(select rno from guest where
gno=billhist.gno),rent,discount,rent-discount nett,lt,0 c1,0 c2 from billhist 
where datediff(d,txndt,@dt)=0 ) a where gno not in (select gno from 
guest where datediff(d,@dt,ci_date) > 0) 

union all  

select gno,eno=(select eno from guest where 
gno=billhist.gno),name=(select name from guest where gno=billhist.gno) ,
rno=(select rno from guest where gno=billhist.gno),rent,discount,rent-discount nett,
lt,0 c1,0 c2 from billhist where datediff(d,txndt,@dt3)=0 and gno  in 
(select gno from guest where datediff(d,@dt,ci_date) = 0) 

union all 

select a.gno,eno,name,a.rno,rent, rent * discount/100 discount, rent- (rent*discount/100) nett,0,0
 c1 ,0 c2  from room_guest a 
--join MIS8YSA6544Z b on a.rno=b.rno  
where 
datediff(d,'1/1/90',ci_date) <= datediff(d,'1/1/90',@dt) 

union all 

select a.gno,eno,name,a.rno,rent,(rent*discount/100) discount, rent-(rent*discount/100)
 nett,0,0 c1 ,0 c2  from room_guest a 
--join MIS8YSA6544Z b on a.rno=b.rno  
where datediff(d,ci_date,@dt)=0 and datediff(n,convert(varchar(20),@dt,6) +   ' 00:00',ci_date) 
< 420

) A where nett <> 0  

insert into #temp values (convert(varchar(20),@dt,6) ,@cnt,@totrent,@avg)
set @dt = dateadd(d,1,@dt)
end

select @cnt=sum(nor) ,@totrent=sum(rent),@avg=sum(arr) from #temp
insert into #temp values ('Total ' ,@cnt,@totrent,@avg/@days)

set nocount off
select ctrlno [S.No] ,dt [Txn.Date],nor [No.of Rooms],rent [Total Rent],arr [Average Rent]
 from #temp




GO
/****** Object:  StoredProcedure [dbo].[BanqBillNOUpdate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Rm.Ramanathan
-- Create date: 8 June 2018  
-- Description:	'yesterday Anna Thanam at Solkettan Vinayakar Aalyam by me
-- This is one time run program to create banqbillno table and update it with existing last numbers
-- =============================================
CREATE PROCEDURE [dbo].[BanqBillNOUpdate]
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @bofy datetime
	declare @eofy datetime
	declare @company numeric
	declare @fy numeric 
	declare @billno numeric
	declare @bnqno numeric

	declare c1 cursor for select bnqno,company from banqmaster
	open c1
	fetch next from c1 into @bnqno,@company
	while @@fetch_Status = 0
	begin
		declare cfy cursor for select fy,date from finyear where company=@company
		open cfy
		fetch next from cfy into @fy,@bofy
		while @@fetch_status=0
		begin
			select @eofy=dbo.geteofy(@bofy) 
			select @billno=max(billno) from banquet where bnqno=@bnqno and datediff(d,'1/1/2000',billdate) between datediff(d,'1/1/2000',@bofy) and datediff(d,'1/1/2000',@eofy) 
			set @billno = isnull(@billno,0)
			if not exists(select * from banqbillno where bnqno=@bnqno and fy=@fy)
			insert into banqbillno (bnqno,fy,billno) values (@bnqno,@fy,@billno)
			else
			update banqbillno set billno=@billno where bnqno=@bnqno and fy=@fy
			fetch next from cfy into @fy,@bofy
		end
		close cfy
		deallocate cfy
		fetch next from c1 into @bnqno,@company
	end
	close c1
	deallocate c1


END

GO
/****** Object:  StoredProcedure [dbo].[BatchPostingA]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 20 Aug 2017
-- Description:	Batch Posting
-- =============================================
CREATE PROCEDURE [dbo].[BatchPostingA]
@outlet int,@mop int,@sdt datetime,@edt datetime,@checkall int=1
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @txndt datetime
	declare @grnocash numeric
	declare @grnors numeric
	declare @grnobr numeric
	declare @grnogl numeric
	declare @company int
	declare @fy int
	declare @grno numeric
	declare @retval int
	declare @usernm varchar(50)
	declare @ctrlno numeric
	declare @bno numeric
	declare @msg varchar(2000)
	declare @selfdeal char(1)
	declare @co1grno numeric
	declare @htl_receivable numeric
	declare @hno_hotelbook numeric
	declare @actno numeric
	declare @fy1 numeric
	declare @testamt money
	declare @oname varchar(100)
	IF @MOP <=0 OR @MOP >=5 begin RAISERROR('MOP is wrong',16,-1) return end
	
	select @company=company,@htl_receivable=htl_receivable from restco where channel=(select channel from outlet where code=@outlet)
	select @oname=name,@selfdeal=selfdeal,@co1grno=co1grno,@hno_hotelbook=hno_htlbook from outlet where code=@outlet
	exec @fy=getfy @sdt,@company,@fy out

	if @checkall=1  --new
		begin
		select ctrlno,txndt,usernm,grnocash,grnors,grnocc,grnogl into #dummy  from serviceagrno where outlet=@outlet 
		and datediff(d,'1/1/2000',txndt) in( select datediff(d,'1/1/2000', txndt) 	from (select grno,MIN(TXNDT) TXNDT, sum(case when amttyp='D' then amt else 0 end) dr,sum(case when amttyp='C' then amt else 0 end) cr
		from vouchers where fy=@fy and  datediff(d,'1/1/2000',txndt) BETWEEN  DATEDIFF(d,'1/1/2000',@sdt) AND DATEDIFF(d,'1/1/2000',@edt)  group by datediff(d,'1/1/2000',txndt),grno) a where dr<>cr)
		declare c1 cursor for select ctrlno,txndt,usernm,grnocash,grnors,grnocc,grnogl from #dummy
		end
	else		--old
		begin
		select ctrlno,txndt,usernm,grnocash,grnors,grnocc,grnogl into #dummy1 from serviceagrno where outlet=@outlet 
		and datediff(d,'1/1/2000',txndt) between datediff(d,'1/1/2000',@sdt) and datediff(d,'1/1/2000',@edt)
		declare c1 cursor for select ctrlno,txndt,usernm,grnocash,grnors,grnocc,grnogl from #dummy1
		end

	open c1
	fetch next from c1 into @ctrlno,@txndt,@usernm,@grnocash,@grnors,@grnobr,@grnogl
	while @@fetch_status=0
		begin
		exec @fy=getfy @txndt,@company,@fy out
		exec @fy1=getfy @txndt,1,@fy1 out

		if  @mop=1 
			exec delgrno @company,@FY,@grnocash,@txndt,@retval out  
		else if @mop=2
			BEGIN 
				exec delgrno @company,@FY,@grnors,@txndt,@retval out  
				if @company<>1 and @selfdeal='N'
				begin

					set @actno=@htl_receivable
-- this is important line for the vouchers posted before 18/sep/2017
if exists (select * from vouchers where company=1 and fy=@fy1 and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@co1grno and amttyp='D' and [user]=@usernm and outlet=0) 
update vouchers set amt=0 where company=1 and fy=@fy1 and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@co1grno and amttyp='D' and [user]=@usernm and outlet=0

					update vouchers set amt=0
					where company=1 and fy=@fy1 and datediff(d,txndt,@txndt)=0 and 
					actnum=@actno and grno=@co1grno and amttyp='D' and [user]=@usernm and outlet=@outlet
					set @actno=@hno_hotelbook
-- this is important line for the vouchers posted before 18/sep/2017
if exists (select * from vouchers where company=1 and fy=@fy1 and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@co1grno and amttyp='C' and [user]=@usernm and outlet=0) 
update vouchers set amt=0 where company=1 and fy=@fy1 and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@co1grno and amttyp='C' and [user]=@usernm and outlet=0
					update vouchers set amt=0
					where company=1 and fy=@fy1 and datediff(d,txndt,@txndt)=0 and 
					actnum=@actno and grno=@co1grno and amttyp='C' and [user]=@usernm and outlet=@outlet


				end
			END
		else if @mop=3 
			exec delgrno @company,@FY,@grnobr,@txndt,@retval out  
		else if @mop=4
			exec delgrno @company,@FY,@grnogl,@txndt,@retval out  

		declare c2 cursor for select bno from kithist1 where datediff(d,txndt,@txndt)=0 and usernm=@usernm and outlet=@outlet
		and pmode=@mop
		open c2
		fetch next from c2 into @bno
		while @@fetch_status=0
			begin
			--print @usernm
			--print @txndt
			set @msg = @usernm +  ' ' + str(@bno)
			exec ServiceAVouching @bno
			if @@error<>0 raiserror(@msg,16,-1)
			fetch next from c2 into @bno
			end
		close c2
		deallocate c2 
		fetch next from c1 into @ctrlno,@txndt,@usernm,@grnocash,@grnors,@grnobr,@grnogl
		end
	close c1
	deallocate c1
END

GO
/****** Object:  StoredProcedure [dbo].[BatchPostingB]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 20 Aug 2017
-- Description:	Batch Posting For Service B
-- =============================================
CREATE PROCEDURE [dbo].[BatchPostingB]
@outlet int,@mop int,@sdt datetime,@edt datetime
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @txndt datetime
	declare @grnocash numeric
	declare @grnors numeric
	declare @grnobr numeric
	declare @grnogl numeric
	declare @grnocc numeric
	declare @company int
	declare @fy int
	declare @grno numeric
	declare @retval int
	declare @usernm varchar(50)
	declare @ctrlno numeric
	declare @bno numeric
	IF @MOP <=0 OR @MOP > 5 begin RAISERROR('MOP is wrong',16,-1) return end
	select @company=company from phoutlet where outlet=@outlet
	begin transaction 
	declare c1 cursor for select ctrlno,txndt,usernm,grnocash,grnors,grnobr,grnogl,grnocc from servicebgrno where outlet=@outlet 
	and datediff(d,'1/1/2000',txndt) between datediff(d,'1/1/2000',@sdt) and datediff(d,'1/1/2000',@edt)
	open c1
	fetch next from c1 into @ctrlno,@txndt,@usernm,@grnocash,@grnors,@grnobr,@grnogl,@grnocc
	while @@fetch_status=0
	begin
	exec @fy=getfy @txndt,@company,@fy out

		if  @mop=1 
			exec delgrno @company,@FY,@grnocash,@txndt,@retval out  
		else if @mop=2 
			exec delgrno @company,@FY,@grnors,@txndt,@retval out  
		else if @mop=3 
			exec delgrno @company,@FY,@grnobr,@txndt,@retval out  
		else if @mop=4
			exec delgrno @company,@FY,@grnogl,@txndt,@retval out  
		else if @mop=5
			exec delgrno @company,@FY,@grnocc,@txndt,@retval out  


	declare c2 cursor for select cno from phone where datediff(d,txndt,@txndt)=0 and usernm=@usernm 
	and pmode=@mop and outlet=@outlet
	open c2
	fetch next from c2 into @bno
	while @@fetch_status=0
	begin
	exec ServiceBVouching @bno
	fetch next from c2 into @bno
	end
	close c2
	deallocate c2 
	fetch next from c1 into @ctrlno,@txndt,@usernm,@grnocash,@grnors,@grnobr,@grnogl,@grnocc
	end
	close c1
	deallocate c1
	commit transaction
END

GO
/****** Object:  StoredProcedure [dbo].[CelVou]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		PowerBrain
-- Create date: 8 December 2015 
-- Description:	Celebrations Vouching
-- =============================================
CREATE PROCEDURE [dbo].[CelVou] 
@ctrlno as integer 
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @company numeric
	declare @glac numeric
	declare @txndt datetime 
	declare @actno numeric
	declare @amt decimal(18,2)
	declare @amttyp char(1)
	declare @desc varchar(180)
	declare @tno numeric
	declare @fy numeric
	declare @grno numeric
	declare @mop numeric
	declare @dactno numeric
	declare @glactno numeric
	declare @usernm varchar(50)
	declare @sessionid varchar(100)
	declare @fname varchar(100)
	select @company=company,@glac=glac,@fname=fname from celmaster where fno=(select fno from celtickets where ctrlno=@ctrlno)
	select @txndt=dob,@amt=amount,@tno=tno,@mop=mop,@glactno=case when crnum='' then 0 else cast(crnum as numeric) end,@usernm=usernm,@sessionid=sessionid from celtickets where ctrlno=@ctrlno
	if @mop=4 return 
	Begin Transaction 
	exec getgrp @txndt,@company,@grno out
	if @@error <>0 goto e_error

	exec @fy=getfy @txndt,@company,@fy out
	if @@error<>0 goto e_error

	select @dactno = case
	when @mop=1 then 1  			--cash sales
	when @mop=2 then @GLActno 		-- GL
	when @mop=3 then 22 			-- credir card
	when @mop=4 then 0 				-- complementary - no vouching
	end

	set @ACTNO = @glac
	set @AMTTYP = 'C'
	set @DESC = 'By Sales - Ticket No.' + ltrim(rtrim(str(@tno)))  + ' - ' + @fname
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
	(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
	if @@error <> 0 goto e_error
	set @ACTNO = @dactno
	set @AMTTYP = 'D'
	set @DESC = 'To Sales - Ticket No.' + ltrim(rtrim(str(@tno)))
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
	(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
	if @@error <> 0 goto e_error

	update celtickets set grno=@grno where ctrlno=@ctrlno
    commit transaction
	return
e_error:
rollback transaction
raiserror('Error in Celebrations Voucher Posting',16,-1)
return
END

GO
/****** Object:  StoredProcedure [dbo].[ChangeKOTDate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ChangeKOTDate] @bno numeric,@txndt datetime
	-- Add the parameters for the stored procedure here
/* with encryption */
AS
BEGIN
declare @mop int
declare @billgno numeric
declare @sql varchar(5000)
declare @accesslog varchar(100)
declare @usernm varchar(50)
declare @olddate datetime
declare @finalamt numeric(18,2)
declare @olname varchar(50)
declare @company numeric
declare @outlet numeric
declare @channel numeric
declare @m_split char(1)
declare @lux char(1)
declare @userno numeric
declare @sno numeric
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
begin transaction
	select @sno=sno,@m_split=split,@finalamt=finalamt,@mop=pmode,@billgno=gno,@olddate=txndt,@outlet=outlet from kithist1 where bno=@bno
	select @channel=channel,@olname=name,@lux=lux from outlet where code=@outlet
	select @company=company from restco where channel=@channel
	exec serviceadeleting @bno
	if @@error <> 0 goto e_error

	update kithist1 set txndt=@txndt,txntime=substring(convert(varchar(20),@txndt,8),1,5)  where bno=@bno

	exec serviceavouching @bno
	if @@error <> 0 goto e_error
    -- Insert statements for procedure here
set @userno=1
if @mop=2
   begin
   set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalamt) + ',0,' + str(@company) + ',' + str(100+@outlet) + ',''' + convert(varchar(20),@olddate) + ''',''' + @m_split + ''',0,0,''X'','+str(@userno)+','''+@lux+''''
   exec (@sql)
   set @sql = 'exec delcout ' + str(@billgno) + ',0,0,' +  str(@company) + ',' + str(100+@outlet) + ',''' + convert(varchar(20),@txndt) + ''',''' + @m_split + ''',0,' + convert(varchar(20),@finalamt) + ',''' + @M_SPLIT +''','+str(@userno)+','''+@lux+''''
   exec (@sql)
   if @@error <> 0  goto e_error
   end
goto final
FINAL:

select @usernm=usernm from comuser where [host_id]=host_id()
---print str(@m_sno) + ' '+ convert(varchar(20),@m_txndt,6) + ' ' + convert(varchar(20),@oldamt)
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, 
ltrim(rtrim(@olname))+ ' Bill No ' + ltrim(str(@sno)) +  ' Date Modified- Old Date = '+ convert(varchar(20), @olddate) + ' ,  New Date = '+ convert(varchar(20),@txndt),2,@billgno)

commit transaction
return
e_error:
rollback transaction
raiserror('Error in changing date',16,-1)
END




GO
/****** Object:  StoredProcedure [dbo].[Channel2Channel]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Channel2Channel] @sc int,@tc int, @so int 
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
declare @cnt numeric 
declare @icode numeric
declare @rate1 money 
declare @rate2 money 
declare @rate money 
declare @st money 
declare @grp int
declare @class int
declare @sclass int
declare @iname varchar(200)
declare @norate char(1)
declare @pref varchar(500)
declare @unit varchar(100)
declare @shcode varchar(20)
declare @status char(1)
declare @tax money 
declare @outlet int 
begin transaction 
select @grp=min(code) from kitgroup where rcode=@tc
select @class=min(code) from kitclass where rcode=@tc
select @sclass=min(code) from kitclass_2 where rcode=@tc

declare c1 cursor for select icode,name,norate,preference,unit,shcode from kitprime where rcode=@sc  order by icode
open c1
fetch next from c1 into @icode,@iname,@norate,@pref,@unit,@shcode
while @@fetch_status=0
begin
if not exists(select * from kitprime where rcode=@tc and name=@iname) 
	begin 
		insert into kitprime (rcode,name,grp,class,subclass,norate,preference,unit,shcode) 
		values (@TC,rtrim(@iname),@grp,@class,@sclass,@norate,@pref,@unit,@shcode)
		if @@error<>0 goto e_error
		select @cnt=@@identity
		select @rate=rate,@rate1=rate1,@rate2=rate2,@tax=tax,@status=status from kitmast where icode=@icode and outlet=@so
		declare c2 cursor for select code from outlet where channel=@tc
		open c2
		fetch next from c2 into @outlet
		while @@fetch_status=0
			begin
			if not exists(select code from kitmast where outlet=@outlet and icode=@cnt)
			insert into kitmast (outlet,icode,rate,rate1,rate2,tax,status) values
			(@outlet,@cnt,@rate,@rate1,@rate2,@tax,@status)
			if @@error<>0 goto e_error
			fetch next from c2 into @outlet
			end
		close c2
		deallocate c2
	end
	fetch next from c1 into @icode,@iname,@norate,@pref,@unit,@shcode
end
close c1
deallocate c1
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[ChartNew]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[ChartNew]
@desc varchar(100),@flag numeric,@grp numeric ,@opt int=1
/* with encryption */
AS
BEGIN
	Begin Transaction
	SET NOCOUNT ON;
    declare @actno  numeric
	declare @err varchar(6000)
	set @err = 'Error in opening new chart of account'
    if @opt=1
		begin
		select @actno=max(cgp_Actno) from chart where cgp_actno > 500
		set @actno=isnull(@actno,500) + 1
			if @actno > 1000
				begin
				set @err = 'You have reached the maximum limit for user-defined Chart of Account'
				goto e_error
				end
		end
    else
		begin
		select @actno=max(cgp_Actno) from chart where cgp_actno <= 500
		set @actno=isnull(@actno,500) + 1
			if @actno > 500
				begin
				set @err = 'You have reached the maximum limit for Default Chart of Account'
				goto e_error
				end
		end
		insert into chart (cgP_desc,cgp_actno,flag,grp) values (@desc,@actno,@flag,@grp)
commit transaction
return
e_error:
rollback transaction
raiserror(@err,16,-1)
END




GO
/****** Object:  StoredProcedure [dbo].[CHARTTOGL]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.CHARTTOGL    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE  PROCEDURE [dbo].[CHARTTOGL]  @M_COMPANY INT,@M_FY INT /* with encryption */
AS
BEGIN TRAN CHARTGL
DECLARE @M_ACTNUM INT
DECLARE @M_DESC CHAR(50)
DECLARE @M_FLAG INT
DECLARE @M_TDNO INT
DECLARE @M_ASSET CHAR(1)
DECLARE @M_ONAC CHAR(1)
DECLARE @M_FYDATE DATETIME
DECLARE @P_FYDATE DATETIME
DECLARE @P_FY INT
declare @balance as money
declare @add1 varchar(50)
declare @add2 varchar(50)
declare @add3 varchar(50)
declare @city varchar(50)
declare @email varchar(100)
declare @plbal money
declare @cgp_actno numeric
declare @cgp_desc varchar(50)
declare @phone varchar(50)
declare @grp numeric
declare @flag numeric
declare @tdno numeric
declare @asset char(1)
declare @onac char(1)
declare @deletable char(1)
declare @subgrp numeric
declare @permit char(1)
declare @allowcredit char(1)
declare @nextyearac numeric
declare @checkac numeric
declare @usernm varchar(50) 
declare @doo datetime 
declare @gstin nvarchar(50)
declare @pan nvarchar(50)
declare @err varchar(1000)
declare @flds varchar(5000)
declare @sql Nvarchar(4000)
declare @sql1 Nvarchar(4000)
declare @fldsnew varchar(5000)
print 'periya'
select @flds=columns from (Select table_SCHEMA, table_name     , Stuff(        (        Select ',[' +  ltrim(rtrim(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='glprimld' ) a
select @fldsnew=columns from (Select table_SCHEMA, table_name     , Stuff(        (        Select ',isnull([' +  ltrim(rtrim(C.COLUMN_NAME)) + '],'+ case when isnull(c.numeric_precision,'')='' then '''''' else '0' end  + ')'    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='glprimld' ) a
set @flds = '['+@flds
set @fldsnew = 'i'+@fldsnew
set @flds = replace(@flds,'[ctrlno],','')
set @flds = upper(@flds)
set @fldsnew = upper(@fldsnew)
set @fldsnew = replace(@fldsnew,'ISNULL([CTRLNO],0),','')
--print @flds
--print @fldsnew 
SELECT @M_FYDATE=[DATE] FROM FINYEAR WHERE COMPANY = @M_COMPANY AND FY = @M_FY
SELECT @P_FYDATE= DATEADD(YEAR,-1,@M_FYDATE)
IF EXISTS (SELECT * FROM FINYEAR WHERE COMPANY=@M_COMPANY AND datediff(d,'1/1/90',[DATE] )= datediff(d,'1/1/90',@P_FYDATE))
	begin
	set @err= '2111'
	update glprimld set opgbal=0 where fy=@m_fy
	SELECT @P_FY=FY FROM FINYEAR WHERE COMPANY=@M_COMPANY AND datediff(d,'1/1/90',[DATE] )= datediff(d,'1/1/90',@P_FYDATE)
	set @err= '2112'
	declare cursorgl cursor for select balance,cgp_actno,cgp_desc,add1,add2,city,phone,flag,grp,tdno,asset,onac,deletable,subgrp,permit,allowcredit,email,nextyearac,usernm,doo,gstin,pan from glprimld where fy=@p_fy
	open cursorgl
	fetch next from cursorgl into @balance,@cgp_actno,@cgp_desc,@add1,@add2,@city,@phone,@flag,@grp,@tdno,@asset,@onac,@deletable,@subgrp,@permit,@allowcredit,@email,@nextyearac,@usernm,@doo,@gstin,@pan
		while @@fetch_status=0
			begin	
			set @err= '2113'
			set @Checkac =  isnull(@nextyearac,@cgp_actno)

			if not exists(select * from glprimld where fy=@m_fy and cgp_actno=@checkac)
				begin
				if not exists(select * from glprimld where fy=@m_fy and CGP_DESC=@cgp_Desc)
					begin
					set @err= 'Test ' + @cgp_desc
					--insert into glprimld  (company,fy,opgbal,balance,cgp_actno,cgp_desc,add1,add2,city,phone,flag,grp,tdno,asset,onac,deletable,subgrp,permit,allowcredit,email,ltxndt,usernm,doo,gstin,pan)
					--values 
					--(@m_company,@m_fy,case when @grp=7 then 0 else  @balance end,case when @grp=7 then 0 else  @balance end,@checkac,@cgp_desc,@add1,@add2,@city,@phone,@flag,@grp,@tdno,@asset,@onac,@deletable,@subgrp,@permit,@allowcredit,@email,'1 jan 2010',isnull(@usernm,'NYR'),ISNULL(@doo,getdate()),@gstin,@pan)
					set @fldsnew = replace(@fldsnew,'[Fy]',str(@m_fy))
					if @grp=7 set @balance=0 
					set @fldsnew = replace(@fldsnew,'[opgbal]',convert(varchar(20),@balance))
					set @fldsnew = replace(@fldsnew,'[balance]',convert(varchar(20),@balance))
					set @fldsnew = replace(@fldsnew,'[','')
					set @fldsnew = replace(@fldsnew,']','')
					if @m_fy<>(select max(fy) from finyear where company=@m_company) 
					set @fldsnew = replace(@fldsnew,'ISNULL(NEXTYEARFY,0)',0)

					set @sql = 'insert into glprimld (' + @flds + ') '  
					set @sql1 = 'select  ' + @fldsnew +' from glprimld where fy=' + str(@p_fy) + ' and cgp_actno=' + str(@cgp_actno)
					print @sql + @sql1 + 'Test'
					exec (@sql + @sql1)
					--if @m_fy<>(select max(fy) from finyear where company=@m_company) 
					   -- begin
						--print 'okok'
 						update glprimld set nextyearac=cgp_actno,nextyearfy=@m_fy where fy=@p_fy and cgp_actno=@cgp_Actno
						--end
					end
				end
			else
				begin
				if @grp<>7 BEGIN update glprimld set opgbal=@balance where cgp_actno=@checkac and fy=@m_fy END
				end
		fetch next from cursorgl into @balance,@cgp_actno,@cgp_desc,@add1,@add2,@city,@phone,@flag,@grp,@tdno,@asset,@onac,@deletable,@subgrp,@permit,@allowcredit,@email,@nextyearac,@usernm,@doo,@gstin,@pan
		end
		close cursorgl
		deallocate cursorgl

	select @plbal=sum(balance) from glprimld where grp=7 and company=@m_company and fy=@p_fy group by company
	update glprimld set opgbal=opgbal+@plbal where cgp_actno = 52 and company=@m_company and fy=@m_fy
	IF @@ERROR <> 0 GOTO EERROR
	END
	ELSE
BEGIN
print @p_fydate
print '3'

declare S1 cursor for select CGP_ACTNO,cgp_desc from chart
open s1

fetch next from s1 into @cgp_actno,@cgp_desc
 while @@fetch_status=0
 begin
 if not exists (select * from glprimld where fy=@m_Fy and CGP_ACTNO=@cgp_actno and cgp_desc=@cgp_Desc) 
   begin
   print @cgp_Desc
	INSERT GLPRIMLD (COMPANY,FY,CGP_ACTNO,CGP_DESC,LTXNDT,TDNO,ASSET,ONAC,GRP,flag) 
	SELECT @M_COMPANY,@M_FY,CGP_ACTNO,CGP_DESC,'1 APR 1999',TDNO,ASSET,ONAC,GRP,flag FROM CHART where cgp_actno=@cgP_actno 
	IF @@ERROR <> 0 GOTO EERROR
   end
  fetch next from s1 into @cgp_actno,@cgp_desc
 end
 close s1
 deallocate s1

end




COMMIT TRAN CHARTGL
RETURN
EERROR:
ROLLBACK TRAN CHARTGL
set @err= @err + '-CHATTOGL FAILED'
RAISERROR (@err,16,-1)





GO
/****** Object:  StoredProcedure [dbo].[checkblock]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[checkblock] /* with encryption */
AS
declare @cnt int
if exists (
select * from sptbrooms_v  where  datediff(d,getdate(),arrivaldate) >= 0 and  datediff(s,'1/1/90',getdate()) between 
datediff(s,'1/1/90',arrivaldate) and datediff(s,'1/1/90',departuredate)  and 
rno in (select rno from room where gno<>0)  and status1='N'
)
set @cnt=1
else
set @cnt=0
select cnt=@cnt




GO
/****** Object:  StoredProcedure [dbo].[CheckKOTDate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[CheckKOTDate] 
@outlet int ,  @retval int=0 out,@err varchar(5000) out
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @txndt datetime
declare @g_kotfinaltime int 
declare @g_autokot char(1)
select @txndt=curdate,@g_autokot=autokot from outlet where code=@outlet
select @g_kotfinaltime=g_kotfinaltime from setting 
set @err = 'Error in entering KOT'
set @err = convert(varchar(20),@txndt)
while datediff(day,@txndt,getdate()) > 0 
begin
if datediff(day,@txndt,getdate())=1 
	begin
		if  datediff(Minute, convert(varchar(20),getdate(),101),getdate())  > @g_kotfinaltime
			begin
				if exists(select * from currentkot where outlet=@outlet)
					begin
					set @err='KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
					set @retval=0
					return
					end   
				else
					begin
					update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
					set @retval=1
					return
					end
			end
		else 
		begin
		set @retval=1
		return
		end

	end
else
	begin
set @err='Step11'
			if exists(select * from currentkot where outlet=@outlet)
				begin
				    set @err= 'KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
					set @retval=0
					return
				end   
			else
				begin
				update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
					set @retval=1
					return
				end
	end
	   select @txndt=curdate from outlet where code=@outlet
	   set @retval=1
	   return
end --- new
set @retval=1
return
END

GO
/****** Object:  StoredProcedure [dbo].[CheckLTONTARIFF]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[CheckLTONTARIFF]
/* with encryption */
AS
BEGIN
SET NOCOUNT ON;
declare @sdt datetime
declare @edt datetime
declare @nextsdt datetime
declare @first  int
set @first=1
declare c1 cursor for select sdt,edt  from ltontariff order by sdt
open c1
fetch next from c1 into @sdt,@edt
while @@fetch_status=0
begin
if @first <> 1 
   if datediff(d,@sdt,@nextsdt) <> 0 
	begin
      raiserror('Error in LTONTARIFF TABLE',16,-1)
	  return
	end
	set @first=0
    set @nextsdt = dateadd(d,1,@edt)
fetch next from c1 into @sdt,@edt
end
close c1
deallocate c1
if datediff(d,getdate(),@nextsdt) <= 30 
begin 
      raiserror('Error in LTONTARIFF TABLE',16,-1)
return
end


END

GO
/****** Object:  StoredProcedure [dbo].[checksystems]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO

CREATE PROCEDURE [dbo].[checksystems] @name varchar(100),@g_allowed int 
/* with encryption */
as
set nocount on 
declare @cnt int
if @name<>''
begin
if not exists(select * from systems where cname=@name) 
	begin 
		select @cnt=count(*) from systems  
		if @cnt < @g_allowed 
			begin 
			insert into systems (cname,ltxndt) values (@name,getdate()) 
			select @cnt=0 
			end 
		else if  @cnt >= @g_allowed
		    select @cnt=2
		else
			select @cnt=1 
	end
else 
	begin 
	update systems set ltxndt=getdate() where cname=@name 
	select @cnt=0 
	end
end
else
set @cnt=1

set nocount off
select @cnt as cnt

GO
/****** Object:  StoredProcedure [dbo].[chkforgrno]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<PowerBrain>
-- Create date: <08 Oct 2014>
-- Description:	<For ALmondz>
-- =============================================
CREATE PROCEDURE [dbo].[chkforgrno]
	-- Add the parameters for the stored procedure here
@bno numeric
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
begin transaction
	SET NOCOUNT ON;

    -- Insert statements for procedure here

declare @grno numeric 
declare @grnocash numeric
declare @outlet int 
declare @usernm varchar(50) 
declare @txndt datetime
declare @channel int
declare @company int
if not exists(select * from vouchers where refno=@bno )
	begin
	print 'The entry voucher not found' goto e_error 
	end
else 
	begin
	select @grno=grno from vouchers where refno=@bno 
	end
select @outlet=outlet,@usernm=usernm,@txndt=txndt from kithist1 where bno=@bno
if not exists(select * from serviceagrno where datediff(d,txndt,@txndt)=0 and usernm=@usernm and outlet=@outlet) 
	begin
	-- For Room Service
	insert into serviceagrno (outlet,txndt,usernm,grnors) values (@outlet,@txndt,@usernm,@grno) 
	-- For Cash Txn
	if exists (select * from vouchers where datediff(d,txndt,@txndt)=0 and [user]=@usernm and amttyp='C' and txntype='SER-A' and actnum=(select actnum from outlet where code=@outlet) and grno<>@grno)
		begin
		select @grnocash=grno from vouchers where datediff(d,txndt,@txndt)=0 and [user]=@usernm and amttyp='C' and txntype='SER-A' and actnum=(select actnum from outlet where code=@outlet) and grno<>@grno
		end
	else
		begin
		select @channel=channel from outlet where code=@outlet
		select @company=company from restco where channel=@channel
		exec getgrp @txndt,@company,@grnocash out
		if @@error <>0 goto e_error
		update serviceagrno set grnocash=@grnocash where  datediff(d,txndt,@txndt)=0 and outlet=@outlet
		end
	end
else
	begin
	print 'This Entry is existing'
	goto e_error
	end
commit transaction 
return
E_error:
rollback transaction
raiserror('Error',16,-1)
return
END


GO
/****** Object:  StoredProcedure [dbo].[CloseAndDeallocateCursor]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[CloseAndDeallocateCursor]
    @cursorName NVARCHAR(80)
AS
BEGIN
    IF CURSOR_STATUS('global', @cursorName) >= -1
    BEGIN
        DECLARE @SQL NVARCHAR(91)
        IF CURSOR_STATUS('global', @cursorName) > -1
        BEGIN
            SET @SQL = N'CLOSE ' + @cursorName
            EXEC sp_executeSQL @SQL
        END
        SET @SQL = N'DEALLOCATE ' + @cursorName
        EXEC sp_executeSQL @SQL
    END
END
GO
/****** Object:  StoredProcedure [dbo].[CODate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CODate]  @dt datetime,@gno numeric=0,@grpname varchar(500)='' /* with encryption */
AS
declare @rno numeric
declare @refno numeric
declare @ed datetime
declare @find  int
declare @dt1 datetime
declare @dt2 datetime
declare @oed datetime
if @gno<>0 
begin
	if not exists(select * from room where gno=@gno) begin raiserror('The selected room is already checked out',16,-1)  return end
	select @rno=rno,@ed=expecteddep,@refno=reserve_ref from guest where gno=@gno
	set @oed = @ed
	set @find=0
	if datediff(d,getdate(),@ed) < 0  set @ed= getdate()
if datediff(d,@ed,@dt)=0 and datediff(s,@ed,@dt) < 0 
set @find= 0 
else
begin

	--set @ed = @ed + '11:59:00'
	while datediff(HH,@ed,@dt)>=0
	begin
		---print convert(varchar(20),@dt) + ' ' + convert(varchar(20),@ed)  + str( datediff(d,@ed,@dt))
		if  exists (select * from sptbrooms_v where myctrlno<>@refno and   rno=  @rno 
		and datediff(d,arrivaldate,getdate()) <= 0 
		and datediff(s,'1/1/90',@ed) between datediff(s,'1/1/90',arrivaldate) and datediff(s,'1/1/90',departuredate)) 
			begin	    
			set @find=1
			break
			end
		set @ed = dateadd(HH,1,@ed)
	end
end
	if @find=1 
		raiserror('This room is blocked for the period during which the expected departure of the current guest is extended',16,-1)
		else
		update guest set expecteddep=@dt where gno=@gno
		
		set @dt1= getdate()
		if @dt > @oed
			begin
			set @dt1 = @oed
			set @dt2 = @dt
			end
		else
			begin
			set @dt1 = @dt
			set @dt2 = @oed
			end		
		--print convert(varchar(20),@dt1) + ' ' + convert(varchar(20),@dt2) + ' ' + convert(varchar(20),@dt) + ' ' + convert(varchar(20),@oed) raiserror('error',16,-1) return
		While DateDiff(d,@dt1,@dt2) >= 0
		begin
			if not exists (select * from staahinv where status='' and datediff(d,txndt,@dt1) = 0) insert into staahinv (txndt) values (@DT1)
			if not exists (select * from crsinv where status='' and datediff(d,txndt,@dt1) = 0) insert into crsinv (txndt,status,message) values (@DT1,'','')
			set @dt1 = dateadd(d,1,@dt1)
		end
end

if @grpname<>''
begin
	begin transaction
	declare c1 cursor for select rno,gno from room where grpname=@grpname
	open c1
	fetch next from c1 into @rno,@gno
	while @@fetch_status=0
		begin
		select @ed=expecteddep,@refno=reserve_ref,@gno=gno from guest where gno=@gno
		set @oed = @ed
		set @find=0
		if datediff(d,getdate(),@ed) < 0  set @ed= getdate()

		if datediff(d,@ed,@dt)=0 and datediff(s,@dt,@ed) >  0 
			begin
			--raiserror(@grpname,16,-1)
			set @find= 0 
			end
		else
			begin
			while datediff(HH,@ed,@dt)>=0
				begin
					--print convert(varchar(20),@dt) + ' ' + convert(varchar(20),@ed)  + str( datediff(d,@ed,@dt)) raiserror('error',16,-1) return
					if not exists (select * from room where gno=@gno)
						begin
						set @grpname = 'Room No: ' +  ltrim(rtrim(str(@rno))) + ' already checked out'
						rollback transaction
						raiserror(@grpname,16,-1)
						return
						end 
					if  exists (select * from sptbrooms_v where myctrlno<>@refno and   rno=  @rno 
					and datediff(s,'1/1/90',@ed) between datediff(s,'1/1/90',arrivaldate) and datediff(s,'1/1/90',departuredate)) 
						begin	    
						set @find=1
						break
						end
					set @ed = dateadd(HH,1,@ed)
				end
			end
		if @find=1 
			begin		
				set @grpname='Room No: ' +  ltrim(rtrim(str(@rno))) + ' is blocked for the period' + char(13) + 'during which the expected departure of the current guest is extended'
				raiserror(@grpname,16,-1)
				rollback transaction
				return
			end
		else
			begin
			--raiserror(@grpname,16,-1)
				update guest set expecteddep=@dt where gno=@gno

				set @dt1= getdate()
				if @dt > @oed
					begin
					set @dt1 = @oed
					set @dt2 = @dt
					end
				else
					begin
					set @dt1 = @dt
					set @dt2 = @oed
					end		

				While DateDiff(d,@dt1,@dt2) >= 0
				begin
					if not exists (select * from staahinv where status='' and datediff(d,txndt,@dt1) = 0) insert into staahinv (txndt) values (@DT1)
					if not exists (select * from crsinv where status='' and datediff(d,txndt,@dt1) = 0) insert into crsinv (txndt) values (@DT1)
					set @dt1 = dateadd(d,1,@dt1)
				end
			end
			fetch next from c1 into @rno,@gno
	end
	commit transaction
end




GO
/****** Object:  StoredProcedure [dbo].[cospldc]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.cospldc    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[cospldc] @cno int, @spldiscount  money,@foodbill money,@gno numeric  /* with encryption */
AS
declare @t1 varchar(50)
declare @t2 varchar(50)
declare @rv varchar(50)
declare @sql varchar(5000)
declare @sql1 varchar(5000)
declare @split char(1)
DECLARE @BCOMPANY INT
DECLARE @SECTION CHAR(1)
DECLARE @MAINBILLDATE DATETIME
DECLARE @GRNO numeric
DECLARE @RET_VAL numeric
DECLARE @FY int
declare @crname varchar(50)
declare @crnum varchar(50)
declare @crtype varchar(50)
declare @mop int
declare @glactno int
declare @bno numeric
declare @eno numeric
declare @guestname varchar(100)
declare @usernm varchar(20)
declare @ospd money
declare @ofb money
--declare @foodbill money
set nocount on
EXEC RANDFILE @RV OUT
begin transaction
/*
select @t1 = 'bill' + ltrim(@RV)
set @t2 = 'fbpm' + ltrim(@RV)
SET @SQL1 = 'if exists (select name,xtype from sysobjects where name = ''' + @T2 +''' and xtype = ''U'') drop table ' + @T2
EXEC (@SQL1)
IF @@ERROR <> 0 GOTO E_ERROR
SET @SQL = 'SELECT * INTO ' + @T2 + '  FROM  DUMMYPYMT'
EXEC (@SQL)
IF @@ERROR <> 0 GOTO E_ERROR
*/
SELECT @mop=mop,@glactno=glactno,@crname=crname,@crnum=crnum,@crtype=crtype,@GRNO=GRNO,@SPLIT=SPLIT,@BCOMPANY=COMPANY,@MAINBILLDATE=TXNDT,@ospd=spldiscount,@ofb=foodbill,@bno=bno  FROM BILLHIST1 WHERE CNO=@CNO
select @eno=eno,@guestname=name from guest_view1 where gno=@gno
EXEC @FY= GETFY  @mainbilldate,@bCOMPANY,@FY OUT
EXEC DELGRNO @bCOMPANY,@FY,@GRNO,@mainbilldate,@RET_VAL OUT
IF @@ERROR <> 0
begin
print 'Sub GL Account seems to be reversed..no modifications allowed'
GOTO E_ERROR
end
UPDATE BILLHIST1 SET foodbill=@foodbill,spldiscount=@spldiscount,MOP=@MOP,CRNAME=@CRNAME,CRNUM=@CRNUM,CRTYPE=@CRTYPE,@GRNO=GRNO  WHERE CNO=@CNO
exec pbvc @cno,@cno
if @@error <> 0  goto e_error
select @usernm=usernm from comuser where [host_id]=host_id()
set @sql =  'Final Bill Special Discount Modified Bill No.' + ltrim(str(isnull(@bno,0))) + ' dated ' + convert(varchar(20),@mainbilldate,3) + ' GRNO ' +ltrim(str(isnull(@eno,0))) + ' Guest Name-' +ltrim(rtrim( isnull(@guestname,' NIL '))) + ' Old Spl.Disc ' + convert(varchar(20),isnull(@ospd,0)) + ' New ' + convert(varchar(20),isnull(@spldiscount,0)) +  '  Old Ex-Food Bill ' + convert(varchar(20),isnull(@ofb,0)) + ' New ' + convert(varchar(20),isnull(@foodbill,0))
insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm,@sql ,0,@gno)
if @@error<>0 
begin
print 'Error in updating accesslog'
goto e_error
end
--SET @SQL1 = 'if exists (select name,xtype from sysobjects where name = ''' + @T2 +''' and xtype = ''U'') drop table ' + @T2
--EXEC (@SQL1)
commit transaction
return 
e_error:
rollback transaction
raiserror('cospldc failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[createbill]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Rm.Ramanathan>
-- Create date: <16 Dec 2015>
-- Description:	<Creating Bills Ledger Type>
-- =============================================
CREATE PROCEDURE [dbo].[createbill] @cno numeric,@opt int=0,@fname varchar(50)=''
/* with encryption */
AS

BEGIN
	SET NOCOUNT ON;

    declare @split char(1)
	declare @company numeric
	declare @gno numeric
	declare @txndt datetime
	declare @rent numeric(18,2)
	declare @otherchgs numeric(18,2)
	declare @discount numeric(18,2)
	declare @lt numeric(18,2)
	declare @sc numeric(18,2)
	declare @st numeric(18,2)
	declare @cess numeric(18,2)
	declare @cess2 numeric(18,2)
	declare @outlet numeric
	declare @olname varchar(100)
	declare @sno numeric
	declare @amt money 
	declare @drate money
	declare @lrate money
	declare @rno numeric
	declare @strate money
	declare @cessrate money
	declare @cessrate2 money
	declare @crcardbank varchar(100)
	declare @crcardnum varchar(100)
	declare @crcardname varchar(100)
	declare @mop tinyint
	declare @nar varchar(200)
	declare @codate datetime 
	declare @maxdate datetime
	declare @cessname varchar(50)
	declare @cessname2 varchar(50)
	declare @billdate datetime
	declare @msg varchar(500)
	declare @extrabed money
	declare @exbedgst money
	declare @food money
	declare @foodgst  money
	declare @gStname varchar(100)

	declare @gstrate money
	declare @sttableno int 
	declare @servicetype int
	declare @sac varchar(15) 
	declare @gstin varchar(50)
	declare @err varchar(5000) 
	declare @adv varchar(20)
	declare @igst tinyint
	declare @hotelgststate varchar(20)
	
	select @billdate = txndt,@gstin=gstin from billhist1 where cno=@cno
	select @hotelgststate=gststate from setting
	if substring(@gstin,1,2) = substring(@hotelgststate,1,2)
	   set @igst = 1
    else
	   set @igst =0

    select @adv=g_advance  from setting

	select @maxdate=max(txndt) from billhist where cno=@cno
	select @cessname=cessname,@cessname2=cessname2 from sttable where @maxdate between sdt and edt 

	create table #dl 
 
	(
	ctrlno numeric identity(1,1) not null,
	txndt  datetime,
	[HSN CODE] VARCHAR(10),
	narration varchar(100),
	amttyp char(1),
	amt	 money,
	btype int
	)

	select @company=company,@split=split,@codate=txndt from billhist1 where cno=@cno
    
	declare @dtdiff numeric
	declare @firstdate int
	set @firstdate = 0
	set @dtdiff = 1000000000

	--declare c1 cursor for select min(txndt),min(gno),sum(rent) rent,sum(otherchgs) otherchgs,sum(discount) discount,sum(sc) sc,sum(lt) lt,sum(st) st, sum(cess) cess, sum(cess2) cess2,max(lrate),min(rno)  from billhist  where cno=@cno group by datediff(d,'1/1/2000',txndt),lrate 
	declare c1 cursor for select min(txndt),min(gno),sum(rent) rent,sum(otherchgs) otherchgs,sum(discount) discount,sum(sc) sc,sum(lt) lt,sum(st) st, sum(cess) cess, sum(cess2) cess2,max(lrate) lrate,min(rno) rno,sum(extrabed) extrabed,sum(exbedgst) gstexbed,sum(food) food,sum(foodgst) foodgst  from billhist  where cno=@cno group by datediff(d,'1/1/2000',txndt),lrate   order by datediff(d,'1/1/2000',txndt),lrate
	open c1
	--fetch next from c1 into @txndt,@gno,@rent,@otherchgs,@discount,@sc,@lt,@st,@cess,@cess2,@lrate,@rno
	fetch next from c1 into @txndt,@gno,@rent,@otherchgs,@discount,@sc,@lt,@st,@cess,@cess2,@lrate,@rno,@extrabed,@exbedGST,@FOOD,@FOODGST
	while @@fetch_status=0
		begin
			if datediff(d,'1/1/2000',@txndt) = @dtdiff set @firstdate = 1 else set @firstdate= 0
			set @dtdiff = datediff(d,'1/1/2000',@txndt)
        	exec GetST  @txndt,@rno,10000,0,@strate out,@cessrate out,1,@cessrate2 out
			set @strate = @strate/100
			set @cessrate = @cessrate/100
			set @cessrate2= @cessrate2/100
			--if @rent<>0	
			insert into #dl  (txndt,narration,amttyp,amt,btype,[HSN CODE]) values (@txndt,'To Rent','C',@rent,1,'996311')

			if @extrabed <> 0
			insert into #dl  (txndt,narration,amttyp,amt,btype,[HSN CODE]) values (@txndt,'To Extra Bed Chg ' ,'C',@extrabed,1,'996311')
			if @food <> 0
			insert into #dl  (txndt,narration,amttyp,amt,btype,[HSN CODE]) values (@txndt,'To Food ','C',@food,1,'')

			if @otherchgs <> 0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To otherchgs','C',@otherchgs,1)
			if @discount<>0 
			begin 
				set @drate = (@discount/@rent) * 100
				insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Discount @' + ltrim(convert(varchar(20),@drate)) + '%','D',@discount,1)
			end

			declare @ltname varchar(100) 
			select @ltname=ltname from ltmaster where @txndt between sdt and edt
			set @igst=0
			if ltrim(rtrim(@gstin)) = ''
			   set @igst =1
			else
				if substring(@gstin,1,2) = substring(@hotelgststate,1,2)
				   set @igst = 1
				else
				   set @igst =0
set @err =  substring(@gstin,1,2) + '  and ' + substring(@hotelgststate,1,2)
--goto e_error
				   set @err = @igst
				   set @igst=1
			if datediff(d,'30 jun 2017',@TXNDT) >0 
			begin
			if @igst=0
			begin
				if @lt<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To IGST @' + ltrim(convert(varchar(20),@lrate)) + '%','C',(@lt+@exbedgst+@foodgst),1)
            end
			else
			    begin
				if @lt<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To CGST @' + ltrim(convert(varchar(20),@lrate/2)) + '%','C',(@lt+@exbedgst+@foodgst)/2,1)
				if @lt<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To SGST @' + ltrim(convert(varchar(20),@lrate/2)) + '%','C',(@lt+@exbedgst+@foodgst)/2,1)
				end
			end
			else
			if @lt<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @ltname + ' @' + ltrim(convert(varchar(20),@lrate)) + '%','C',@lt,1)

			if @sc<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Service Charges','C',@sc,1)
			if @st<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Service Tax @' + ltrim(convert(varchar(20),@strate)) + '%','C',@st,1)
			if @cess<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @cessname + ' CESS @' + ltrim(convert(varchar(20),@cessrate)) + '%','C',@cess,1)
			if @cess2<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @cessname2 + ' CESS @' + ltrim(convert(varchar(20),@cessrate2)) + '%','C',@cess2,1)

if @firstdate=0
begin
			declare c2 cursor for 
			select outlet,finalamt,sno from kithist1 where outlet in (select code from outlet where  selfdeal='Y' and channel in (select channel from restco where company=@company) ) and datediff(d,txndt,@txndt)=0 and  gno in (select gno from billhist where cno=@cno)  and split=@split  order by outlet
			open c2
			fetch next from c2 into @outlet,@amt,@sno
			while @@FETCH_STATUS=0
				begin
					select @olname=name from outlet where code=@outlet
					set @olname = ltrim(rtrim(@olname)) 
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,2)
					fetch next from c2 into @outlet,@amt,@sno
				end
				close c2
				deallocate c2

			-- For no selfdeeal  in Service A
				if @company=1
				begin
							declare c2 cursor for 
							select outlet,finalamt,sno from kithist1 where outlet in (select code from outlet where  selfdeal='N' and channel in (select channel from restco where company<>@company) ) and datediff(d,txndt,@txndt)=0 and  gno in (select gno from billhist where cno=@cno)  and split=@split  order by outlet
							open c2
							fetch next from c2 into @outlet,@amt,@sno
							while @@FETCH_STATUS=0
								begin
									select @olname=name from outlet where code=@outlet
									set @olname = ltrim(rtrim(@olname)) 
									insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,2)
									fetch next from c2 into @outlet,@amt,@sno
								end
								close c2
								deallocate c2
				end

			declare c2 cursor for 
			select outlet,finalamt,sno from phone where outlet in (select outlet from phoutlet where  company=@company and selfdeal='Y') and datediff(d,txndt,@txndt)=0 and gno in (select gno from billhist where cno=@cno) and split=@split  order by outlet
			open c2
			fetch next from c2 into @outlet,@amt,@sno
			while @@FETCH_STATUS=0
				begin
					select @olname=name from phoutlet where outlet=@outlet
					set @olname = ltrim(rtrim(@olname)) 
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,3)
					fetch next from c2 into @outlet,@amt,@sno
				end
				close c2
				deallocate c2

			-- For no selfdeeal  in Service B
			if @company=1
			begin
						declare c2 cursor for 
						select outlet,finalamt,sno from phone where outlet in (select outlet from phoutlet where  company<>@company and selfdeal<>'Y') and datediff(d,txndt,@txndt)=0 and  gno in (select gno from billhist where cno=@cno)  and split=@split  order by outlet
						open c2
						fetch next from c2 into @outlet,@amt,@sno
						while @@FETCH_STATUS=0
							begin
								select @olname=name from phoutlet where outlet=@outlet
								set @olname = ltrim(rtrim(@olname)) 
								insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,3)
								fetch next from c2 into @outlet,@amt,@sno
							end
							close c2
							deallocate c2
			end

			declare c2 cursor for 
			select outlet,finalamt,chlno from tt_chalan_ol  where company=@company and selfdeal='Y' and datediff(d,finaldate,@txndt)=0 and  gno in (select gno from billhist where cno=@cno)  and split=@split  order by outlet 
			open c2
			fetch next from c2 into @outlet,@amt,@sno
			while @@FETCH_STATUS=0
				begin
					select @olname=name from toutlet where outlet=@outlet
					set @olname = ltrim(rtrim(@olname)) 
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,4)
					fetch next from c2 into @outlet,@amt,@sno
				end
				close c2
				deallocate c2

			if @company=1
			begin
						declare c2 cursor for 
						select outlet,finalamt,chlno from tt_chalan_ol  where company<>@company and selfdeal<>'Y' and datediff(d,finaldate,@txndt)=0 and  gno in (select gno from billhist where cno=@cno)  and split=@split  order by outlet 
						open c2
						fetch next from c2 into @outlet,@amt,@sno
						while @@FETCH_STATUS=0
							begin
								select @olname=name from toutlet where outlet=@outlet
								set @olname = ltrim(rtrim(@olname)) 
								insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,4)
								fetch next from c2 into @outlet,@amt,@sno
							end
							close c2
							deallocate c2
			end

			declare c2 cursor for 
			select finalamt=amount,ano,mop,crcardbank,crcardnum,crcardname from advance  where company=@company and datediff(d,txndt,@txndt)=0 and gno in (select gno from billhist where cno=@cno) and split=@split 
			open c2
			fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
			while @@FETCH_STATUS=0
				begin
					select @nar= case when @mop=1 then 'Cash Payment'
					when @mop=2 then 'Cr.Card' + rtrim(ltrim(@crcardbank)) + '...' + substring(@crcardnum,16,4)
					when @mop=3 then 'Chq.No' + @crcardnum
					when @mop=4 then 'GL A/C ' +  @crcardname end 

					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'By ' + substring(@adv,1,3) + ' Rec.No.' + rtrim(ltrim(str(@sno))) + '-' + @nar,'D',@amt,5)
					fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
				end
				close c2
				deallocate c2
				
			declare c2 cursor for 
			select finalamt=amount,(select bno from billhist1 where cno=roomdues.cno) as sno from roomdues where company=@company and datediff(d,txndt,@txndt)=0 and gno in (select gno from billhist where cno=@cno) and split=@split 
			open c2
			fetch next from c2 into @amt,@sno
			while @@FETCH_STATUS=0
				begin
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Other Room Dues Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,6)
					fetch next from c2 into @amt,@sno
				end
				close c2
				deallocate c2
end -- end of @firstdate=0
		--fetch next from c1 into @txndt,@gno,@rent,@otherchgs,@discount,@sc,@lt,@st,@cess,@cess2,@lrate,@rno
		fetch next from c1 into @txndt,@gno,@rent,@otherchgs,@discount,@sc,@lt,@st,@cess,@cess2,@lrate,@rno,@extrabed,@exbedGST,@FOOD,@FOODGST
		end
----------------**************
	close c1
	deallocate c1

	declare @cdt datetime
	set @cdt = dateadd(d,1,@maxdate)
	if datediff(d,@codate,@maxdate)<>0
    while datediff(d,@cdt,@codate) >=0 
	begin
            set @txndt = @cdt
			declare c2 cursor for 
			select finalamt=amount,ano,mop,crcardbank,crcardnum,crcardname from advance  where company=@company and datediff(d,txndt,@txndt)=0 and gno in (select gno from billhist where cno=@cno) and split=@split 
			open c2
			fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
			while @@FETCH_STATUS=0
				begin
					select @nar= case when @mop=1 then 'Cash Payment'
					when @mop=2 then 'Cr.Card' + rtrim(ltrim(@crcardbank)) + '...' + substring(@crcardnum,16,4)
					when @mop=3 then 'Chq.No' + @crcardnum
					when @mop=4 then 'GL A/C ' +  @crcardname end 
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'By ' + substring(@adv,1,3) + '.Rec.No.' + rtrim(ltrim(str(@sno))) + '-' + @nar,'D',@amt,5)
					fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
				end
				close c2
				deallocate c2

			declare c2 cursor for 
			select finalamt=amount,(select bno from billhist1 where cno=roomdues.cno) as sno from roomdues where company=@company and datediff(d,txndt,@txndt)=0 and gno in (select gno from billhist where cno=@cno) and split=@split 
			open c2
			fetch next from c2 into @amt,@sno
			while @@FETCH_STATUS=0
				begin
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Other Room Dues Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,6)
					fetch next from c2 into @amt,@sno
				end
				close c2
				deallocate c2

			set @cdt = dateadd(d,1,@cdt)
	end
	declare @foodbill money
	declare @spldiscount money 
	declare @fbnarration varchar(100)
	select @txndt=txndt,@foodbill=isnull(foodbill,0),@spldiscount=isnull(spldiscount,0) from billhist1 where cno=@cno
	if @foodbill<>0 
		begin
		select top 1 @fbnarration=foodbillnarration from setting 
		insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + rtrim(@fbnarration) ,'C',@foodbill,7)
		end
	if @spldiscount<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Special Discount','D',@spldiscount,7)
if @opt=0
	SELECT * from #dl  order by txndt,btype
else
	begin
    declare @sql varchar(1000)
	set @sql= 'select * from (SELECT min(txndt) txndt, case when btype=1 and amttyp=''C'' then ''Room Rent ''  when btype=1 and amttyp=''D'' then ''Discount'' when btype=2 then ''Service A Bills'' when btype=3 then ''Service B Bills'' when btype=4 then ''Travel Desk'' when btype=5 then ''Less Advance'' when btype=6 then ''Other Room Dues'' when btype=7 and amttyp=''D'' then ''Special Discount'' when btype=7 and amttyp=''C'' then (select top 1 foodbillnarration from setting) else ''Unknown'' end as  narration,btype,amttyp,sum(amt) amt  from #dl  group by btype,amttyp) a where amt <> 0'
	set nocount off
	exec (@sql) 
	end
END
return
e_error:
raiserror(@ERR,16,-1)



GO
/****** Object:  StoredProcedure [dbo].[createbill2GST]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 05 Sep 2017
-- Description:	Bills2 GST Format
-- =============================================
CREATE PROCEDURE [dbo].[createbill2GST] @cno numeric,@opt int=0,@fname varchar(50)=''
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;

    declare @split char(1)
	declare @company numeric
	declare @gno numeric
	declare @txndt datetime
	declare @rent numeric(18,2) 
	declare @otherchgs numeric(18,2)
	declare @discount numeric(18,2)
	declare @lt numeric(18,2)
	declare @sc numeric(18,2)
	declare @st numeric(18,2)
	declare @cess numeric(18,2)
	declare @cess2 numeric(18,2)
	declare @outlet numeric
	declare @olname varchar(100)
	declare @sno numeric
	declare @amt money 
	declare @drate money
	declare @lrate money
	declare @rno numeric
	declare @strate money
	declare @cessrate money
	declare @cessrate2 money
	declare @crcardbank varchar(100)
	declare @crcardnum varchar(100)
	declare @crcardname varchar(100)
	declare @mop tinyint
	declare @nar varchar(200)
	declare @codate datetime 
	declare @maxdate datetime
	declare @cessname varchar(50)
	declare @cessname2 varchar(50)
	declare @billdate datetime
	declare @msg varchar(500)
	declare @extrabed money
	declare @exbedgst money
	declare @food money
	declare @foodgst  money
	declare @gStname varchar(100)
	declare @advance money

	declare @item1 money
	declare @item2 money
	declare @item3 money
	declare @item4 money
	declare @item5 money
	declare @item6 money
	declare @item7 money
	declare @item8 money
	declare @item9 money
	declare @item10 money
	declare @header1 varchar(100)
	declare @header2 varchar(100)
	declare @header3 varchar(100)
	declare @header4 varchar(100)
	declare @header5 varchar(100)
	declare @header6 varchar(100)
	declare @header7 varchar(100)
	declare @header8 varchar(100)
	declare @header9 varchar(100)
	declare @header10 varchar(100)
	declare @spldiscount money



declare @gstrate money
declare @sttableno int 
declare @servicetype int
declare @sac varchar(15) 
declare @gstin varchar(50)

	select @company=1,@split='N',@codate=billdate,@gstin=gstin,@advance=advance, 
	@header1=header1,@header2=header2,@header3=header3,@header4=header4,@header5=header5,
	@header6=header6,@header7=header7,@header8=header8,@header9=header9,@header10=header10,
	@spldiscount=spldiscount
	from guest2 where ctrlno=@cno

	select @maxdate=max(txndt) from bill2 where cno=@cno
	select @cessname=cessname,@cessname2=cessname2 from sttable where @maxdate between sdt and edt 

	create table #dl 

	(
	ctrlno numeric identity(1,1) not null,
	txndt  datetime,
	[HSN CODE] VARCHAR(10),
	narration varchar(100),
	amttyp char(1),
	AMT	 money not null default 0 ,
	CGST money not null default 0 ,
	SGST money not null default 0 ,
	AMT1 money not null default 0 ,
	SGST1 money not null default 0 ,
	CGST1 money not null default 0 ,
	IGST1 money not null default 0 ,
	Discount money not null default 0 ,
	Advance money not null default 0 ,
	lrate money not null default 0,
	gstrate money not null default 0 ,
	btype int
	)
    
	declare c1 cursor for select txndt,rent,discount,sc,lt,servicetax,cess,cess2,lrate,
	item1,item2,item3,item4,item5,item6,item7,item8,item9,item10
	 from bill2  where cno=@cno 
	open c1
	fetch next from c1 into @txndt,@rent,@discount,@sc,@lt,@st,@cess,@cess2,@lrate
	,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10
	while @@fetch_status=0
		begin
        	exec GetST  @txndt,@rno,10000,0,@strate out,@cessrate out,1,@cessrate2 out
			set @strate = @strate/100
			set @cessrate = @cessrate/100
			set @cessrate2= @cessrate2/100
			if @split='N' and @company=1
			begin
			set @gstname ='' 
			if @lrate<>0  set @gstname = '( CGST @' + ltrim(convert(varchar(20),@lrate/2)) + '%' + ' SGST @' + ltrim(convert(varchar(20),@lrate/2)) + '%)' else set @gstname=''
			insert into #dl  (txndt,narration,amttyp,amt,cgst,sgst,amt1,advance,btype,[HSN CODE],lrate) values (@txndt,'To Rent ' +@gstname,'C',@rent,@lt/2,@lt/2 ,0,0,1,'996311',@lrate)
			if @discount<>0 
				begin 
					set @drate = (@discount/@rent) * 100
					insert into #dl  (txndt,narration,amttyp,discount,btype) values (@txndt,'To Discount @' + ltrim(convert(varchar(20),@drate)) + '%','D',@discount,1)
				end
			if @sc<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Service Charges','C',@sc,1)
			if @st<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Service Tax @' + ltrim(convert(varchar(20),@strate)) + '%','C',@st,1)
			if @cess<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @cessname + ' CESS @' + ltrim(convert(varchar(20),@cessrate)) + '%','C',@cess,1)
			if @cess2<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @cessname2 + ' CESS @' + ltrim(convert(varchar(20),@cessrate2)) + '%','C',@cess2,1)
			if isnull(@item1,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header1 ,'C',@item1,1)
			if isnull(@item2,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header2 ,'C',@item2,1)
			if isnull(@item3,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header3 ,'C',@item3,1)
			if isnull(@item4,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header4 ,'C',@item4,1)
			if isnull(@item5,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header5 ,'C',@item5,1)
			if isnull(@item6,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header6 ,'C',@item6,1)
			if isnull(@item7,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header7 ,'C',@item7,1)
			if isnull(@item8,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header8 ,'C',@item8,1)
			if isnull(@item9,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header9 ,'C',@item9,1)
			if isnull(@item10,0)<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @header10 ,'C',@item10,1)

            end

			fetch next from c1 into @txndt,@rent,@discount,@sc,@lt,@st,@cess,@cess2,@lrate
			,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10
		end
	close c1
	deallocate c1

	if @advance <> 0				insert into #dl  (txndt,narration,amttyp,advance,btype) values (@txndt,'By Advance','D',@advance,5)
	if @spldiscount<>0 insert into #dl  (txndt,narration,amttyp,discount,btype) values (@txndt,'To Special Discount','D',@spldiscount,7)

if @opt=0
	SELECT *,@company company,@gstin gstin from #dl  order by txndt,btype
else
	begin
    declare @sql varchar(1000)
	set @sql= 'select * from (SELECT min(txndt) txndt, case when btype=1 and amttyp=''C'' then ''Room Rent and Taxes''  when btype=1 and amttyp=''D'' then ''Discount'' when btype=2 then ''Service A Bills'' when btype=3 then ''Service B Bills'' when btype=4 then ''Travel Desk'' when btype=5 then ''Less Advance'' when btype=6 then ''Other Room Dues'' when btype=7 and amttyp=''D'' then ''Special Discount'' when btype=7 and amttyp=''C'' then (select top 1 foodbillnarration from setting) else ''Unknown'' end as  narration,btype,amttyp,sum(amt) amt  from #dl  group by btype,amttyp) a where amt <> 0'
	set nocount off
	exec (@sql) 
	end
END



GO
/****** Object:  StoredProcedure [dbo].[createbillGST]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[createbillGST] @cno numeric,@opt int=0,@fname varchar(50)=''
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;

    declare @split varchar(10)
	declare @company numeric
	declare @gno numeric
	declare @txndt datetime
	declare @rent numeric(18,2) 
	declare @otherchgs numeric(18,2)
	declare @discount numeric(18,2)
	declare @lt numeric(18,2)
	declare @sc numeric(18,2)
	declare @st numeric(18,2)
	declare @cess numeric(18,2)
	declare @cess2 numeric(18,2)
	declare @outlet numeric
	declare @olname varchar(100)
	declare @sno numeric
	declare @amt money 
	declare @drate money
	declare @lrate money
	declare @rno numeric
	declare @strate money
	declare @cessrate money
	declare @cessrate2 money
	declare @crcardbank varchar(100)
	declare @crcardnum varchar(100)
	declare @crcardname varchar(100)
	declare @mop tinyint
	declare @nar varchar(200)
	declare @codate datetime 
	declare @maxdate datetime
	declare @cessname varchar(50)
	declare @cessname2 varchar(50)
	declare @billdate datetime
	declare @msg varchar(500)
	declare @extrabed money
	declare @exbedgst money
	declare @food money
	declare @foodgst  money
	declare @gStname varchar(100)
	declare @hotelgststate varchar(20)
	declare @igst tinyint
declare @gstrate money
declare @sttableno int 
declare @servicetype int
declare @sac varchar(15) 
declare @gstin varchar(50)
declare @err varchar(5000) 
declare @adv varchar(20)
declare @hisgstin as varchar(50)
declare @ourgstin as varchar(50)
set @err = 'error'
select @adv=g_advance from setting
	select @billdate = txndt,@gstin=gstin from billhist1 where cno=@cno
	select @hotelgststate=gststate from setting
	if substring(@gstin,1,2) = substring(@hotelgststate,1,2)
	   set @igst =0
    else
	   set @igst =1

select @err =substring(@gstin,1,2) + ' ' + substring(@hotelgststate,1,2)
	--select @maxdate=max(txndt) from billhist where cno=@cno
select @maxdate=min(ci_date) from guest where gno in (select gno from billhist where cno=@cno)
	select @cessname=cessname,@cessname2=cessname2 from sttable where @maxdate between sdt and edt 
	create table #dl 

	(
	ctrlno numeric identity(1,1) not null,
	txndt  datetime,
	[HSN CODE] VARCHAR(10),
	narration varchar(100),
	amttyp char(1),
	AMT	 money not null default 0 ,
	CGST money not null default 0 ,
	SGST money not null default 0 ,
	AMT1 money not null default 0 ,
	SGST1 money not null default 0 ,
	CGST1 money not null default 0 ,
   	IGST1 money not null default 0,
	Discount money not null default 0 ,
	Advance money not null default 0 ,
	lrate money not null default 0,
	gstrate money not null default 0 ,
	btype int
	)
	select @company=company,@split=split,@codate=txndt from billhist1 where cno=@cno
    
	declare @dtdiff numeric
	declare @firstdate int
	set @firstdate = 0
	set @dtdiff = 1000000000
	IF CURSOR_STATUS('global','c1') >= -1 
	begin
	close c1
    deallocate c1 
	end

	declare c1 cursor for select min(txndt),min(gno),sum(rent) rent,sum(otherchgs) otherchgs,sum(discount) discount,sum(sc) sc,sum(lt) lt,sum(st) st, sum(cess) cess, sum(cess2) cess2,max(lrate) lrate,min(rno) rno,sum(extrabed) extrabed,sum(exbedgst) gstexbed,sum(food) food,sum(foodgst) foodgst  from billhist  where cno=@cno group by datediff(d,'1/1/2000',txndt),lrate   order by datediff(d,'1/1/2000',txndt),lrate
	open c1
	fetch next from c1 into @txndt,@gno,@rent,@otherchgs,@discount,@sc,@lt,@st,@cess,@cess2,@lrate,@rno,@extrabed,@exbedGST,@FOOD,@FOODGST
	while @@fetch_status=0
		begin
			if datediff(d,'1/1/2000',@txndt) = @dtdiff set @firstdate = 1 else set @firstdate= 0
			set @dtdiff = datediff(d,'1/1/2000',@txndt)
        	exec GetST  @txndt,@rno,10000,0,@strate out,@cessrate out,1,@cessrate2 out
			set @strate = @strate/100
			set @cessrate = @cessrate/100
			set @cessrate2= @cessrate2/100
			--if @rent<>0	
			if @split='N' and @company=1
			begin

			if @lrate<>0  set @gstname = '( CGST @' + ltrim(convert(varchar(20),@lrate/2)) + '%' + ' SGST @' + ltrim(convert(varchar(20),@lrate/2)) + '%)' else set @gstname=''
			set @gstname ='' 
	if substring(@gstin,1,2) = substring(@hotelgststate,1,2)
	   set @igst =0
    else
	   set @igst =1
--if len(ltrim(rtrim(@gstin))) < 10
--begin
--set @err = len(@gstin)
----goto e_error
--			insert into #dl  (txndt,narration,amttyp,amt,cgst,sgst,amt1,advance,btype,[HSN CODE],lrate) values (@txndt,'To Rent ' +@gstname,'C',@rent,@lt/2,@lt/2 ,0,0,1,'996311',@lrate)
--end
--else
--begin
set @igst=0

			if  @igst=1
			begin
						insert into #dl  (txndt,narration,amttyp,amt,cgst,sgst,amt1,advance,btype,[HSN CODE],lrate) values (@txndt,'To Rent ' +@gstname,'C',@rent,0,@lt ,0,0,1,'996311',@lrate)
			end
			else
			begin
						insert into #dl  (txndt,narration,amttyp,amt,cgst,sgst,amt1,advance,btype,[HSN CODE],lrate) values (@txndt,'To Rent ' +@gstname,'C',@rent,@lt/2,@lt/2 ,0,0,1,'996311',@lrate)
			end
			if @extrabed <> 0
			insert into #dl  (txndt,narration,amttyp,amt,cgst,sgst,amt1,advance,btype,[HSN CODE],lrate) values (@txndt,'To Extra Bed Chg ' + @gstname,'C',@extrabed,@exbedgst/2,@exbedgst/2 ,0,0,1,'996311',@lrate)
			if @food <> 0
			insert into #dl  (txndt,narration,amttyp,amt,cgst,sgst,amt1,advance,btype,[HSN CODE],lrate) values (@txndt,'To Food ' + @gstname,'C',@food,@foodgst/2,@foodgst/2 ,0,0,1,'996311',@lrate)

			if @otherchgs <> 0 
			insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To otherchgs','C',@otherchgs,1)
			if @discount<>0 
			begin 
				set @drate = (@discount/@rent) * 100
				insert into #dl  (txndt,narration,amttyp,discount,btype) values (@txndt,'To Discount @' + ltrim(convert(varchar(20),@drate)) + '%','D',@discount,1)
			end
			if @sc<>0 insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To Service Charges','C',@sc,1)
			if @st<>0 insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To Service Tax @' + ltrim(convert(varchar(20),@strate)) + '%','C',@st,1)
			if @cess<>0 insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To ' + @cessname + ' CESS @' + ltrim(convert(varchar(20),@cessrate)) + '%','C',@cess,1)
			if @cess2<>0 insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To ' + @cessname2 + ' CESS @' + ltrim(convert(varchar(20),@cessrate2)) + '%','C',@cess2,1)
            end
			--from kithist1 where outlet in (select code from outlet where  selfdeal='Y' and channel in (select channel from restco where company=@company) ) and datediff(d,txndt,@txndt)=0 and  gno in (select gno from billhist where cno=@cno)  and split=@split  group by outlet order by outlet

declare @sp varchar(100)
set @igst=0
-----------
select @ourgstin=gstin from company where company=@company
IF @FIRSTDATE = 0 
BEGIN 
	IF CURSOR_STATUS('global','c2') >= -1 
	begin
	close c2
    deallocate c2
	end
	if exists(select * from  tempdb.sys.tables where name like '#dummy2%') drop table #dummy2
	        select * into #dummy2 from kithist1 where pmode=2 and gno in (select gno from billhist where cno=@cno) 
				if exists(select * from  tempdb.sys.tables where name like '#dummy1%') drop table #dummy1
	select * into #dummy1 from #dummy2 where datediff(d,txndt,@txndt)=0 and  split=@split

				if exists(select * from  tempdb.sys.tables where name like '#dummyx%') drop table #dummyx 
			select * into #dummyx from kithist where bno in (select bno from #dummy1)

     		declare c2 cursor for 
			select outlet,finalamt-ST FINALAMT ,ST GST,sno,split,gstin    FROM 
			(select  kithist1.gstin,kithist1.sno,kithist1.split,finalamt, (select sum(st) from #dummyx where bno=kithist1.bno) st,outlet from  #dummy1 kithist1 where outlet in  (select code from outlet where  selfdeal='Y' and  channel in  (select channel from restco where company=@company) ) ) A
			 order by outlet 
			open c2
			fetch next from c2 into @outlet,@amt,@st,@sno,@split,@hisgstin

			while @@FETCH_STATUS=0
				begin
					select @olname=name,@sttableno=sttableno,@sac=sac from outlet where code=@outlet
					select @gstrate=rate from sttabler where sttableno=@sttableno and  datediff(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
					set @olname = ltrim(rtrim(@olname))
if @IGST=0
					insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,igst1,gstrate,btype,[HSN CODE]) values (@txndt,'To '+@olname + ' B.No ' + dbo.getgstbillnoservice(@company,@sno,@txndt,1,@outlet),'C',@amt,@st/2,@st/2,0,@gstrate,2,@sac)
else
					insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,igst1,gstrate,btype,[HSN CODE]) values (@txndt,'To '+@olname + ' B.No ' + dbo.getgstbillnoservice(@company,@sno,@txndt,1,@outlet),'C',@amt,0,0,@st,@gstrate,2,@sac)

					fetch next from c2 into @outlet,@amt,@st,@sno,@split,@hisgstin
				end
				close c2
				deallocate c2
-------------------
		-- For no selfdeeal  in Service A

		if @company=1
		begin
			--declare c2 cursor for 
			--select outlet,sum(finalamt) finalamt from kithist1 where outlet in (select code from outlet where  selfdeal='N' and channel in (select channel from restco where company<>@company) ) and datediff(d,txndt,@txndt)=0 and  gno in (select gno from billhist where cno=@cno)  and split=@split  group by outlet order by outlet

			declare c2 cursor for 
			select outlet,finalamt-ST ,ST,sno,split,gstin  FROM 
			(select  kithist1.gstin,kithist1.split,kithist1.sno,kithist1.bno,finalamt, (select sum(st) from #dummyx where bno=kithist1.bno) st,outlet from    #dummy1  kithist1 where outlet in  (select code from outlet where  selfdeal='N' and  channel in  (select channel from restco where company<>1) ) ) A
			 order by outlet 
			open c2
			fetch next from c2 into @outlet,@amt,@st,@sno,@split,@hisgstin
			while @@FETCH_STATUS=0
				begin
			    select @olname=name,@sttableno=sttableno,@sac=sac from outlet where code=@outlet

				select @gstrate=rate from sttabler where sttableno=@sttableno and  datediff(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
				set @olname = ltrim(rtrim(@olname)) 
				--insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,gstrate,btype) values (@txndt,'To '+@olname + ' Bill(s)','C',@amt,@st/2,@st/2,@gstrate,2)

if @IGST=0
				insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,igst1,gstrate,btype,[HSN CODE]) values (@txndt,'To '+@olname + ' B.No ' + dbo.getgstbillnoservice(@company,@sno,@txndt,1,@outlet),'C',@amt,@st/2,@st/2,0,@gstrate,2,@sac)
else
				insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,igst1,gstrate,btype,[HSN CODE]) values (@txndt,'To '+@olname + ' B.No ' + dbo.getgstbillnoservice(@company,@sno,@txndt,1,@outlet),'C',@amt,0,0,@st,@gstrate,2,@sac)

				fetch next from c2 into @outlet,@amt,@st,@sno,@split,@hisgstin
				end
			close c2
			deallocate c2
		end
--44--------------------------------
		declare c2 cursor for 
		select outlet,finalamt,stamt stamt,sno,split,gstin from phone where outlet in (select outlet from phoutlet where  company=@company and selfdeal='Y') and datediff(d,txndt,@txndt)=0 and gno in (select gno from billhist where cno=@cno) and split=@split   order by outlet
		open c2
		fetch next from c2 into @outlet,@amt,@st,@sno,@split,@hisgstin
		while @@FETCH_STATUS=0
		begin
			select @olname=name,@sttableno=sttableno,@sac=sac from phoutlet where outlet=@outlet
			select @gstrate=rate from sttabler where sttableno=@sttableno and  datediff(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
			set @olname = ltrim(rtrim(@olname)) 
			set @err= convert(varchar(20),@amt)  
			--raiserror(@err,16,-1) return
			--insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To '+@olname + ' Bill(s)','C',@amt,3)
if @IGST=0
			insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,igst1,gstrate,btype) values (@txndt,'To '+@olname + ' B.No ' + dbo.getgstbillnoservice(@company,@sno,@txndt,2,@outlet),'C',@amt-@st,@st/2,@st/2,0,@gstrate,2)
else
			insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,igst1,gstrate,btype) values (@txndt,'To '+@olname + ' B.No ' + dbo.getgstbillnoservice(@company,@sno,@txndt,2,@outlet),'C',@amt-@st,0,0,@st,@gstrate,2)

			fetch next from c2 into @outlet,@amt,@st,@sno,@split,@hisgstin
		end
		close c2
		deallocate c2

			-- For no selfdeeal  in Service B
		if @company=1
		begin
			declare c2 cursor for 
			select sno,split,outlet,finalamt, stamt,gstin from phone where outlet in (select outlet from phoutlet where  company<>1 and selfdeal<>'Y') and datediff(d,txndt,@txndt)=0 and gno in (select gno from billhist where cno=@cno) and split=@split  order by outlet
			open c2
			fetch next from c2 into @outlet,@amt,@st,@sno,@split,@hisgstin
			while @@FETCH_STATUS=0
			begin
				select @olname=name,@sttableno=sttableno,@sac=sac from phoutlet where outlet=@outlet
				select @gstrate=rate from sttabler where sttableno=@sttableno and  datediff(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
				set @olname = ltrim(rtrim(@olname)) 
				--insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To '+@olname + ' Bill(s)','C',@amt,3)
				--insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,gstrate,btype) values (@txndt,'To '+@olname + ' Bill(s)','C',@amt-@st,@st/2,@st/2,@gstrate,2)
if @IGST=0 --SUBSTRING(@hisgstin,1,2) <> SUBSTRING(@ourgstin,1,2) 
				insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,igst1,gstrate,btype,[HSN CODE]) values (@txndt,'To '+@olname + ' B.No ' + dbo.getgstbillnoservice(@company,@sno,@txndt,2,@outlet),'C',@amt,@st/2,@st/2,0,@gstrate,3,@sac)
		else
				insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,igst1,gstrate,btype,[HSN CODE]) values (@txndt,'To '+@olname + ' B.No ' + dbo.getgstbillnoservice(@company,@sno,@txndt,2,@outlet),'C',@amt,0,0,@st,@gstrate,3,@sac)
				fetch next from c2 into @outlet,@amt,@st,@sno,@split
			end
			close c2
			deallocate c2
		end
--		----------------------------55
			declare c2 cursor for 
			select outlet,sum(finalbasic-discount) finalamt,sum(finaltax) gst,stype    from tt_chalan_ol  where company=@company and selfdeal='Y' and datediff(d,finaldate,@txndt)=0 and  gno in (select gno from billhist where cno=@cno)  and split=@split  group by outlet,stype order by outlet,stype 
			open c2
			fetch next from c2 into @outlet,@amt,@st,@servicetype
			while @@FETCH_STATUS=0
				begin
					select @olname=name,@sac=sac from toutlet where outlet=@outlet
					select @sttableno=sttableno from TA_ServiceTaxSetting where outlet=@outlet and servicetype=@servicetype 
					select @gstrate=rate from sttabler where sttableno=@sttableno and  datediff(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
					set @olname = ltrim(rtrim(@olname)) 
					--insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To '+@olname + ' Bill(s)','C',@amt,3)
					--insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,gstrate,btype) values (@txndt,'To '+@olname + ' Bill(s)','C',@amt-@st,@st/2,@st/2,@gstrate,2)
					insert into #dl  (txndt,narration,amttyp,amt1,cgst1,sgst1,gstrate,btype,[HSN CODE]) values (@txndt,'To '+@olname + ' Bill(s)','C',@amt,@st/2,@st/2,@gstrate,3,@sac)
					fetch next from c2 into @outlet,@amt,@st,@servicetype
				end
				close c2
				deallocate c2

			if @company=1
			begin
						declare c2 cursor for 
						select outlet,sum(finalamt) finalamt  from tt_chalan_ol  where company<>@company and selfdeal<>'Y' and datediff(d,finaldate,@txndt)=0 and  gno in (select gno from billhist where cno=@cno)  and split=@split group by outlet order by outlet 
						open c2
						fetch next from c2 into @outlet,@amt
						while @@FETCH_STATUS=0
							begin
								select @olname=name,@sac=sac from toutlet where outlet=@outlet
								set @olname = ltrim(rtrim(@olname)) 
								--insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To '+@olname + ' Bill(s)','C',@amt,3)
								insert into #dl  (txndt,narration,amttyp,amt1,btype,[HSN CODE]) values (@txndt,'To '+@olname + ' Bill(s)','C',@amt,4,@sac)
								fetch next from c2 into @outlet,@amt
							end
							close c2
							deallocate c2
			end
			/*
			declare c2 cursor for 
			select finalamt=amount,ano,mop,crcardbank,crcardnum,crcardname from advance  where company=@company and datediff(d,txndt,@txndt)=0 and gno in (select gno from billhist where cno=@cno) and split=@split 
			open c2
			fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
			while @@FETCH_STATUS=0
				begin
					select @nar= case when @mop=1 then 'Cash Payment'
					when @mop=2 then 'Cr.Card' + rtrim(ltrim(@crcardbank)) + '...' + substring(@crcardnum,16,4)
					when @mop=3 then 'Chq.No' + @crcardnum
					when @mop=4 then 'GL A/C ' +  @crcardname end 

					insert into #dl  (txndt,narration,amttyp,advance,btype) values (@txndt,'By Adv.Rec.No.' + rtrim(ltrim(str(@sno))) + '-' + @nar,'D',@amt,5)
					fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
				end
				close c2
				deallocate c2
				
			declare c2 cursor for 
			select finalamt=amount,(select bno from billhist1 where cno=roomdues.cno) as sno from roomdues where company=@company and datediff(d,txndt,@txndt)=0 and gno in (select gno from billhist where cno=@cno) and split=@split 
			open c2
			fetch next from c2 into @amt,@sno
			while @@FETCH_STATUS=0
				begin
					insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To Other Room Dues Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,6)
					fetch next from c2 into @amt,@sno
				end
				close c2
				deallocate c2
				*/
		END   -- THIS END IS FOR FIRSTDATE=0
		fetch next from c1 into @txndt,@gno,@rent,@otherchgs,@discount,@sc,@lt,@st,@cess,@cess2,@lrate,@rno,@extrabed,@exbedgst,@food,@foodgst
		end
	close c1
	deallocate c1



	declare @cdt datetime
	set @cdt=@maxdate
--if @split='Y'
--begin
    while datediff(d,@cdt,@codate) >=0 
	begin
            set @txndt = @cdt
			declare c2 cursor for 
			select finalamt=amount,ano,mop,crcardbank,crcardnum,crcardname from advance  where company=@company and
			datediff(d,txndt,@txndt)=0 and 
			gno in (select gno from billhist where cno=@cno) and split=@split 
			open c2
			fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
			while @@FETCH_STATUS=0
				begin
					select @nar= case when @mop=1 then 'Cash Payment'
					when @mop=2 then 'Cr.Card' + rtrim(ltrim(@crcardbank)) + '...' + substring(@crcardnum,16,4)
					when @mop=3 then 'Chq.No' + @crcardnum
					when @mop=4 then 'GL A/C ' +  @crcardname end 

					insert into #dl  (txndt,narration,amttyp,advance,btype) values (@txndt,'By ' + substring(@adv,1,3) +'.Rec.No.' + rtrim(ltrim(str(@sno))) + '-' + @nar,'D',@amt,5)
					fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
				end
				close c2
				deallocate c2

			declare c2 cursor for 
			select finalamt=amount,(select bno from billhist1 where cno=roomdues.cno) as sno from roomdues where company=@company and datediff(d,txndt,@txndt)=0 and gno in (select  gno from billhist where cno=@cno) and split=@split 
			open c2
			fetch next from c2 into @amt,@sno
			while @@FETCH_STATUS=0
				begin
					insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To Other Room Dues Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,6)
					fetch next from c2 into @amt,@sno
				end
				close c2
				deallocate c2

			set @cdt = dateadd(d,1,@cdt)
	end
--end
	declare @foodbill money
	declare @spldiscount money 
	declare @fbnarration varchar(100)
	select @txndt=txndt,@foodbill=isnull(foodbill,0),@spldiscount=isnull(spldiscount,0) from billhist1 where cno=@cno

	if @foodbill<>0 
		begin
		select top 1 @fbnarration=foodbillnarration from setting 
		insert into #dl  (txndt,narration,amttyp,amt1,btype) values (@txndt,'To ' + rtrim(@fbnarration) ,'C',@foodbill,7)
		end
	if @spldiscount<>0 insert into #dl  (txndt,narration,amttyp,discount,btype) values (@txndt,'To Special Discount','D',@spldiscount,7)

    if @opt=0
	SELECT *,@company company,@gstin gstin from #dl  order by txndt,btype
else
	begin
    declare @sql varchar(1000)
	set @sql= 'select * from (SELECT min(txndt) txndt, case when btype=1 and amttyp=''C'' then ''Room Rent and Taxes''  when btype=1 and amttyp=''D'' then ''Discount'' when btype=2 then ''Service A Bills'' when btype=3 then ''Service B Bills'' when btype=4 then ''Travel Desk'' when btype=5 then ''Less Advance'' when btype=6 then ''Other Room Dues'' when btype=7 and amttyp=''D'' then ''Special Discount'' when btype=7 and amttyp=''C'' then (select top 1 foodbillnarration from setting) else ''Unknown'' end as  narration,btype,amttyp,sum(amt) amt  from #dl  group by btype,amttyp) a where amt <> 0'
	set nocount off
	exec (@sql) 

	end
END
return
e_error:
raiserror(@ERR,16,-1)



GO
/****** Object:  StoredProcedure [dbo].[createproforma]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[createproforma] @gno varchar(3000),@company varchar(20),@split char(1), @txndt datetime,@tempfile varchar(50),@opt int=0
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @rent numeric(18,2)
	declare @otherchgs numeric(18,2)
	declare @discount numeric(18,2)
	declare @lt numeric(18,2)
	declare @sc numeric(18,2)
	declare @st numeric(18,2)
	declare @cess numeric(18,2)
	declare @cess2 numeric(18,2)
	declare @outlet numeric
	declare @olname varchar(100)
	declare @sno numeric
	declare @amt money 
	declare @drate money
	declare @lrate money
	declare @rno numeric
	declare @strate money
	declare @cessrate money
	declare @cessrate2 money
	declare @crcardbank varchar(100)
	declare @crcardnum varchar(100)
	declare @crcardname varchar(100)
	declare @mop tinyint
	declare @nar varchar(200)
	declare @codate datetime 
	declare @maxdate datetime
	declare @cessname varchar(50)
	declare @cessname2 varchar(50)
	declare @sql varchar(5000) 
	declare @err varchar(3000)
	--select @gno = replace(@gno,'-',',')
	set @err = 'Error'
	set @sql = 'select * into tempbillhist from ' + @tempfile
    exec(@sql) 
	select * into #tempbillhist from tempbillhist 
	drop table tempbillhist

	select @maxdate=getdate() 
	select @cessname=cessname,@cessname2=cessname2 from sttable where @maxdate between sdt and edt 
	
	create table #dl 
 
	(
	ctrlno numeric identity(1,1) not null,
	txndt  datetime,
	[HSN CODE] VARCHAR(10),
	narration varchar(100),
	amttyp char(1),
	amt	 money,
	btype int
	)


    
	declare c1 cursor for select min(txndt),sum(rent) rent,sum(otherchgs) otherchgs,sum(discount) discount,sum(sc) sc,sum(lt) lt,sum(st) st, sum(cess) cess, sum(cess2) cess2,max(lrate),min(rno)  from #tempbillhist    group by datediff(d,'1/1/2000',txndt)
	open c1
	fetch next from c1 into @txndt,@rent,@otherchgs,@discount,@sc,@lt,@st,@cess,@cess2,@lrate,@rno
	while @@fetch_status=0
		begin
        	exec GetST  @txndt,@rno,10000,0,@strate out,@cessrate out,1,@cessrate2 out
			set @strate = @strate/100
			set @cessrate = @cessrate/100
			set @cessrate2= @cessrate2/100
			--if @rent<>0	
			insert into #dl  (txndt,narration,amttyp,amt,btype,[HSN CODE]) values (@txndt,'To Rent','C',@rent,1,'00441070')
			if @otherchgs <> 0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To otherchgs','C',@otherchgs,1)
			if @discount<>0 
			begin 
				set @drate = (@discount/@rent) * 100
				insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Discount @' + ltrim(convert(varchar(20),@drate)) + '%','D',@discount,1)
			end
			
			declare @ltname varchar(100) 
			select @ltname=ltname from ltmaster where @txndt between sdt and edt
			if datediff(d,'30 jun 2017',@TXNDT) >0 
			begin
				if @lt<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To CGST @' + ltrim(convert(varchar(20),@lrate/2)) + '%','C',@lt/2,1)
				if @lt<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To SGST @' + ltrim(convert(varchar(20),@lrate/2)) + '%','C',@lt/2,1)
			end
			else
			if @lt<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @ltname + ' @' + ltrim(convert(varchar(20),@lrate)) + '%','C',@lt,1)



			if @sc<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Service Charges','C',@sc,1)
			if @st<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Service Tax @' + ltrim(convert(varchar(20),@strate)) + '%','C',@st,1)
			if @cess<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @cessname + ' CESS @' + ltrim(convert(varchar(20),@cessrate)) + '%','C',@cess,1)
			if @cess2<>0 insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To ' + @cessname2 + ' CESS @' + ltrim(convert(varchar(20),@cessrate2)) + '%','C',@cess2,1)
			
			--declare c2 cursor for 
			--select outlet,finalamt,sno from kithist1 where outlet in (select code from outlet where  selfdeal='Y' and channel in (select channel from restco where company=@company) ) and datediff(d,txndt,@txndt)=0 and  gno in (@gno)  and split=@split  order by outlet
			set @sql = 	'declare c2 cursor for select outlet,finalamt,sno from kithist1 where outlet in (select code from outlet where  selfdeal=''Y'' and channel in (select channel from restco where company=' + @company + ') ) and datediff(d,txndt,''' + convert(varchar(20),@txndt,1) + ''')=0 and  gno in (' + @gno + ')  and split=''' + @split + ''' order by outlet'
			exec(@sql)

			open c2
			fetch next from c2 into @outlet,@amt,@sno
			while @@FETCH_STATUS=0
				begin
					select @olname=name from outlet where code=@outlet
					set @olname = ltrim(rtrim(@olname)) 
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,2)
					fetch next from c2 into @outlet,@amt,@sno
				end
				close c2
				deallocate c2


			-- For no selfdeeal  in Service A
			if @company=1
				begin
					set @sql = 'declare c2 cursor for select outlet,finalamt,sno from kithist1 where outlet in (select code from outlet where  selfdeal=''N'' and channel in (select channel from restco where company<>' + @company + ') ) and datediff(d,txndt,''' + convert(varchar(20),@txndt) + ''')=0 and  gno in (' + @gno + ')  and split=''' + @split + ''' order by outlet'
					exec (@sql)
					open c2
					fetch next from c2 into @outlet,@amt,@sno
					while @@FETCH_STATUS=0
						begin
							select @olname=name from outlet where code=@outlet
							set @olname = ltrim(rtrim(@olname)) 
							insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,2)
							fetch next from c2 into @outlet,@amt,@sno
						end
					close c2
					deallocate c2
				end

			set @sql = 'declare c2 cursor for select outlet,finalamt,sno from phone where outlet in (select outlet from phoutlet where  company=' + @company + ' and selfdeal=''Y'') and datediff(d,txndt,''' + convert(varchar(20),@txndt) + ''')=0 and gno in (' + @gno + ') and split=''' + @split  + ''' order by outlet'
			exec (@sql)
			open c2
			fetch next from c2 into @outlet,@amt,@sno
			while @@FETCH_STATUS=0
				begin
					select @olname=name from phoutlet where outlet=@outlet
					set @olname = ltrim(rtrim(@olname)) 
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,3)
					fetch next from c2 into @outlet,@amt,@sno
				end
				close c2
				deallocate c2

			-- For no selfdeeal  in Service B
			if @company=1
			begin
						set @sql = 'declare c2 cursor for select outlet,finalamt,sno from phone where outlet in (select outlet from phoutlet where  company<>' + @company + ' and selfdeal<>''Y'') and datediff(d,txndt,'''  + convert(varchar(20),@txndt) + ''')=0 and  gno in (' + @gno  + ')  and split=''' + @split  + ''' order by outlet'
						exec (@sql)
						open c2
						fetch next from c2 into @outlet,@amt,@sno
						while @@FETCH_STATUS=0
							begin
								select @olname=name from phoutlet where outlet=@outlet
								set @olname = ltrim(rtrim(@olname)) 
								insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,3)
								fetch next from c2 into @outlet,@amt,@sno
							end
							close c2
							deallocate c2
			end
			
			set @sql = 'declare c2 cursor for select outlet,finalamt,chlno from tt_chalan_ol  where company=' + @company + ' and selfdeal=''Y'' and datediff(d,finaldate,''' + convert(varchar(20),@txndt,1) + ''')=0 and  gno in (' +  @gno  + ')  and split=''' + @split + '''  order by outlet '
			exec(@sql)
			open c2
			fetch next from c2 into @outlet,@amt,@sno
			while @@FETCH_STATUS=0
				begin
					select @olname=name from toutlet where outlet=@outlet
					set @olname = ltrim(rtrim(@olname)) 
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,4)
					fetch next from c2 into @outlet,@amt,@sno
				end
				close c2
				deallocate c2

			if @company=1
			begin
						set @sql = ' declare c2 cursor for select outlet,finalamt,chlno from tt_chalan_ol  where company<>' + @company + ' and selfdeal<>''Y'' and datediff(d,finaldate,''' + convert(varchar(20),@txndt) + ''') =0 and  gno in (' + @gno + ' )  and split=''' + @split + ''' order by outlet '
						exec(@sql)
						open c2
						fetch next from c2 into @outlet,@amt,@sno
						while @@FETCH_STATUS=0
							begin
								select @olname=name from toutlet where outlet=@outlet
								set @olname = ltrim(rtrim(@olname)) 
								insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To '+@olname + ' Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,4)
								fetch next from c2 into @outlet,@amt,@sno
							end
							close c2
							deallocate c2
			end

			set @sql = 'declare c2 cursor for select finalamt=amount,ano,mop,crcardbank,crcardnum,crcardname from advance  where company=' + @company  + ' and datediff(d,txndt,''' + convert(varchar(20),@txndt,1) +''')=0 and gno in (' + @gno + ') and split=''' + @split + ''''
			exec(@sql)
			open c2
			fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
			while @@FETCH_STATUS=0
				begin
					select @nar= case when @mop=1 then 'Cash Payment'
					when @mop=2 then 'Cr.Card' + rtrim(ltrim(@crcardbank)) + '...' + substring(@crcardnum,16,4)
					when @mop=3 then 'Chq.No' + @crcardnum
					when @mop=4 then 'GL A/C ' +  @crcardname end 

					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'By Adv.Rec.No.' + rtrim(ltrim(str(@sno))) + '-' + @nar,'D',@amt,5)
					fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
				end
				close c2
				deallocate c2
				
			set @sql = 'declare c2 cursor for select finalamt=amount,(select bno from billhist1 where cno=roomdues.cno) as sno from roomdues where company=' + @company + ' and datediff(d,txndt,''' + convert(varchar(20),@txndt) + ''')=0 and gno in ( ' + @gno + '  ) and split=''' + @split + ''''
			exec(@sql)
			open c2
			fetch next from c2 into @amt,@sno
			while @@FETCH_STATUS=0
				begin
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Other Room Dues Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,6)
					fetch next from c2 into @amt,@sno
				end
				close c2
				deallocate c2

		fetch next from c1 into @txndt,@rent,@otherchgs,@discount,@sc,@lt,@st,@cess,@cess2,@lrate,@rno
		end
	close c1
	deallocate c1

	declare @cdt datetime
	set @cdt = dateadd(d,1,@maxdate)
	if datediff(d,@codate,@maxdate)<>0
    while datediff(d,@cdt,@codate) >=0 
	begin
            set @txndt = @cdt
			set @sql = 'declare c2 cursor for select finalamt=amount,ano,mop,crcardbank,crcardnum,crcardname from advance  where company=' + @company + ' and datediff(d,txndt,''' + convert(varchar(20),@txndt) + ''')=0 and gno in (' + @gno + ') and split=''' + @split +''''
			exec(@sql)
			open c2
			fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
			while @@FETCH_STATUS=0
				begin
					select @nar= case when @mop=1 then 'Cash Payment'
					when @mop=2 then 'Cr.Card' + rtrim(ltrim(@crcardbank)) + '...' + substring(@crcardnum,16,4)
					when @mop=3 then 'Chq.No' + @crcardnum
					when @mop=4 then 'GL A/C ' +  @crcardname end 

					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'By Adv.Rec.No.' + rtrim(ltrim(str(@sno))) + '-' + @nar,'D',@amt,5)
					fetch next from c2 into @amt,@sno,@mop,@crcardbank,@crcardnum,@crcardname
				end
				close c2
				deallocate c2
				

			set @sql = 'declare c2 cursor for select finalamt=amount,(select bno from billhist1 where cno=roomdues.cno) as sno from roomdues where company=' + @company + ' and datediff(d,txndt,''' + convert(varchar(20),@txndt) + ''')=0 and gno in (' + @gno + ' ) and split=''' + @split +''''
			exec(@sql)
			open c2
			fetch next from c2 into @amt,@sno
			while @@FETCH_STATUS=0
				begin
					insert into #dl  (txndt,narration,amttyp,amt,btype) values (@txndt,'To Other Room Dues Bill No.' + rtrim(ltrim(str(@sno))),'C',@amt,6)
					fetch next from c2 into @amt,@sno
				end
				close c2
				deallocate c2

			set @cdt = dateadd(d,1,@cdt)
	end
	--SELECT * from #dl  order by txndt,btype

if @opt=0
	SELECT * from #dl  order by txndt,btype
else
	begin
	set @sql= 'select * from (SELECT min(txndt) txndt, case when btype=1 and amttyp=''C'' then ''Room Rent and Taxes''  when btype=1 and amttyp=''D'' then ''Discount'' when btype=2 then ''Service A Bills'' when btype=3 then ''Service B Bills'' when btype=4 then ''Travel Desk'' when btype=5 then ''Less Advance'' when btype=6 then ''Other Room Dues'' when btype=7 and amttyp=''D'' then ''Special Discount'' when btype=7 and amttyp=''C'' then (select top 1 foodbillnarration from setting) else ''Unknown'' end as  narration,btype,amttyp,sum(amt) amt  from #dl  group by btype,amttyp) a where amt <> 0'
	set nocount off
	exec (@sql) 
	end


return
e_error:
raiserror(@err,16,-1)

END

GO
/****** Object:  StoredProcedure [dbo].[CreateTable]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[CreateTable] @Para1 int 
/* with encryption */
AS

if @para1 = 1  -- Creates dummybill and dummbill1
begin
		if exists (select * from dbo.sysobjects where id = object_id(N'[DUMMYBILL]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
		drop table [DUMMYBILL]
		
		CREATE TABLE [DUMMYBILL] (
			[TXNDT] [datetime] NULL ,
			[RENT] [money] NULL CONSTRAINT [DF_DUMMYBILL_RENT] DEFAULT (0),
			[OtherChgs] [money] NULL ,
			[DISCOUNT] [money] NULL CONSTRAINT [DF_DUMMYBILL_DISCOUNT] DEFAULT (0),
			[ITEM1] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM1] DEFAULT (0),
			[ITEM2] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM2] DEFAULT (0),
			[ITEM3] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM3] DEFAULT (0),
			[ITEM4] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM4] DEFAULT (0),
			[ITEM5] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM5] DEFAULT (0),
			[ITEM6] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM6] DEFAULT (0),
			[ITEM7] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM7] DEFAULT (0),
			[ITEM8] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM8] DEFAULT (0),
			[ITEM9] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM9] DEFAULT (0),
			[ITEM10] [money] NULL CONSTRAINT [DF_DUMMYBILL_ITEM10] DEFAULT (0),
			[LT] [money] NULL CONSTRAINT [DF_DUMMYBILL_LT] DEFAULT (0),
			[SC] [money] NULL CONSTRAINT [DF_DUMMYBILL_SC] DEFAULT (0),
			[ST] [money] NULL ,
			[CESS] [money] NULL ,
			[CESS2] [money] NULL ,
			[TOTAL] [money] NULL CONSTRAINT [DF_DUMMYBILL_TOTAL] DEFAULT (0),
			[BILLORDER] [int] NULL CONSTRAINT [DF_DUMMYBILL_BILLORDER] DEFAULT (0),
			[SECTION] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF_DUMMYBILL_SECTION] DEFAULT (' '),
			[OUTLET1] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET1] DEFAULT (0),
			[OUTLET2] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET2] DEFAULT (0),
			[OUTLET3] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET3] DEFAULT (0),
			[OUTLET4] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET4] DEFAULT (0),
			[OUTLET5] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET5] DEFAULT (0),
			[OUTLET6] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET6] DEFAULT (0),
			[OUTLET7] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET7] DEFAULT (0),
			[OUTLET8] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET8] DEFAULT (0),
			[OUTLET9] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET9] DEFAULT (0),
			[OUTLET10] [int] NULL CONSTRAINT [DF_DUMMYBILL_OUTLET10] DEFAULT (0),
			[ctrlno] [numeric](18, 0) NULL ,
			[taxable] [money] NULL CONSTRAINT [DF_DUMMYBILL_taxable] DEFAULT (0),
			[head] [numeric](18, 0) NULL CONSTRAINT [DF_DUMMYBILL_head] DEFAULT (0),
			[rno] [numeric](18, 0) NULL CONSTRAINT [DF_DUMMYBILL_rno] DEFAULT (0),
			[lrate] [numeric](9, 0) NULL
		) ON [PRIMARY]
		
		if exists (select * from dbo.sysobjects where id = object_id(N'[DUMMYBILL1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
		drop table [DUMMYBILL1]
		
		
		CREATE TABLE [DUMMYBILL1] (
			[rno] [numeric](18, 0) NULL CONSTRAINT [DF__DUMMYBILL1__rno__05EEBAAE] DEFAULT (0),
			[gno] [numeric](18, 0) NULL CONSTRAINT [DF__DUMMYBILL1__gno__06E2DEE7] DEFAULT (0),
			[gname] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL CONSTRAINT [DF__DUMMYBILL__gname__07D70320] DEFAULT (''),
			[total] [money] NULL CONSTRAINT [DF__DUMMYBILL__total__08CB2759] DEFAULT (0),
			[advance] [money] NULL CONSTRAINT [DF__DUMMYBILL__advan__09BF4B92] DEFAULT (0),
			[rent] [money] NULL CONSTRAINT [DF__DUMMYBILL1__rent__0AB36FCB] DEFAULT (0),
			[otherchgs] [money] NULL ,
			[discount] [money] NULL CONSTRAINT [DF__DUMMYBILL__disco__0BA79404] DEFAULT (0),
			[lt] [money] NULL CONSTRAINT [DF__DUMMYBILL1__lt__0C9BB83D] DEFAULT (0),
			[sc] [money] NULL CONSTRAINT [DF__DUMMYBILL1__sc__0D8FDC76] DEFAULT (0),
			[service1] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__0E8400AF] DEFAULT (0),
			[service2] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__0F7824E8] DEFAULT (0),
			[service3] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__106C4921] DEFAULT (0),
			[service4] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__11606D5A] DEFAULT (0),
			[service5] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__12549193] DEFAULT (0),
			[service6] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__1348B5CC] DEFAULT (0),
			[service7] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__143CDA05] DEFAULT (0),
			[service8] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__1530FE3E] DEFAULT (0),
			[service9] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__16252277] DEFAULT (0),
			[service10] [money] NULL CONSTRAINT [DF__DUMMYBILL__servi__171946B0] DEFAULT (0)
		) ON [PRIMARY]

if exists (select * from dbo.sysobjects where id = object_id(N'[DUMMYRENT]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [DUMMYRENT]

CREATE TABLE [DUMMYRENT] (
	[txndt] [datetime] NULL ,
	[rent] [money] NULL ,
	[otherchgs] [money] NULL ,
	[discount] [money] NULL ,
	[rno] [numeric](18, 0) NULL ,
	[lt] [money] NULL ,
	[sc] [money] NOT NULL CONSTRAINT [DF__dummyrent__sc__74AFE73B] DEFAULT (0),
	[st] [money] NULL ,
	[cess] [money] NULL 
) ON [PRIMARY]


if not exists (select * from dbo.sysobjects where id = object_id(N'[rentmaster]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)

CREATE TABLE [rentmaster] (
	[ctrlno] [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
	[rtypectrlno] [numeric](18, 0) NOT NULL ,
	[rno] [numeric](18, 0) NOT NULL ,
	[rent1] [numeric](18, 0) NOT NULL ,
	[rent2] [numeric](18, 0) NOT NULL ,
	[renti1] [numeric](18, 0) NOT NULL ,
	[renti2] [numeric](18, 0) NOT NULL ,
	CONSTRAINT [PK_rentmaster] PRIMARY KEY  CLUSTERED 
	(
		[ctrlno]
	) WITH  FILLFACTOR = 90  ON [PRIMARY] 
) ON [PRIMARY]

end




GO
/****** Object:  StoredProcedure [dbo].[CRSrateupdate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[CRSrateupdate]
@renttype int,
@sdt datetime,@edt datetime
/* with encryption */
AS
BEGIN
SET NOCOUNT ON;
declare @rentTable int
declare @tedt datetime
declare @doit as int
declare @csdt as datetime
declare @staahroomtype as varchar(50)
declare @roomtype varchar(100)
declare @rno int
declare @rate decimal
declare @ssdt varchar(20)
declare @sedt varchar(20)
declare @renttypename varchar(100)
set @csdt = @sdt
select @renttypename=name from renttype where ctrlno=@renttype
set @doit = 0
while @doit = 0
	begin
	select @renttable=ctrlno,@tedt=edt from rentperiod where rtype=@renttype and 
	datediff(d,'1/1/2000',@csdt)
	between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
    --print convert(varchar(20),@edt) + ' ' + convert(varchar(20),@tedt)
	if datediff(d,@edt,@tedt) > 0 
	begin
	set @tedt = @edt
	set @doit= 1 
    end

	declare c1 cursor for 
	select distinct ltrim(rtrim(roomtype)) roomtype  from room
	open c1 
	fetch next from c1 into @roomtype
	while @@fetch_status=0
	begin
	    select top 1 @rno=rno from room where roomtype=@roomtype
		select @rate=rent1 from rentmaster where rtypectrlno=@renttable and rno=@Rno
			if exists (select * from crsrates where 
			datediff(d,@csdt,sdt)=0 and datediff(d,@tedt,edt)=0 
			and roomtype=@roomtype and renttype=@renttype)
			begin
				delete from crsrates where 
				datediff(d,@csdt,sdt)=0 and datediff(d,@tedt,edt)=0 
				and roomtype=@roomtype and renttype=@renttype
			end
			set @ssdt = convert(varchar(20),@csdt) --ltrim(rtrim(str(year(@csdt))))+ '-'+ right('00' + ltrim(rtrim(str(month(@csdt)))),2)+'-'+right('00'+ltrim(rtrim(str(day(@csdt)))),2)
			set @sedt = convert(varchar(20),@tedt) -- ltrim(rtrim(str(year(@tedt))))+ '-'+ right('00' + ltrim(rtrim(str(month(@tedt)))),2)+'-'+right('00'+ltrim(rtrim(str(day(@tedt)))),2)
			
			insert into crsrates 
			(sdt,edt,renttype,roomtype,rent,renttypename)	values
			(@ssdt,@sedt,@renttype,@roomtype,@rate,@renttypename)
		fetch next from c1 into @roomtype
	end	
	close c1
	deallocate c1
    set @csdt = dateadd(day,1,@tedt)
 
	end
END


GO
/****** Object:  StoredProcedure [dbo].[DELCOUT]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.DELCOUT    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.DELCOUT    Script Date: 1/20/03 11:51:49 AM ******/
CREATE PROCEDURE [dbo].[DELCOUT] @gno  int, @amt  money, @namt money, @company  int, @type_ol  int,@billdt  datetime,@split  char(1),
@amt1 money=0,@namt1 money=0, @split1 char(1)='X',@userno numeric = 0,@lux char(1)='N',@checkuser int = 0
/* with encryption */
AS
declare @mainbilldate datetime
declare @sql1 varchar(5000)
declare @grno int
declare @fy int
declare @txndt datetime
declare @outlet as int
declare @ret_val int
declare @t1 varchar(50)
declare @t2 varchar(50)
declare @sql varchar(5000)
declare @rent money
declare @discount money
declare @item1 money
declare @item2 money
declare @item3 money
declare @item4 money
declare @item5 money
declare @item6 money
declare @item7 money
declare @item8 money
declare @item9 money
declare @item10 money
declare @outlet1 int
declare @outlet2 int
declare @outlet3 int
declare @outlet4 int
declare @outlet5 int
declare @outlet6 int
declare @outlet7 int
declare @outlet8 int
declare @outlet9 int
declare @outlet10 int
declare @sc money
declare @lt money
declare @total money
declare @bp  int
declare @bp1 int
declare @bp2 int
declare @selfdeal char(1)
declare @bcompany int
declare @section char(1)
DECLARE @MOP INT
DECLARE @CRNAME VARCHAR(50)
DECLARE @CRNUM VARCHAR(20)
DECLARE @CRTYPE VARCHAR(50)
DECLARE @GLACTNO INT
declare @channel int
DECLARE @CNO INT
DECLARE @RV AS VARCHAR(10)
declare @dtfound int
DECLARE @OLNM1 CHAR(10)
DECLARE @OLNM2 CHAR(10)
DECLARE @OLNM3 CHAR(10)
DECLARE @OLNM4 CHAR(10)
DECLARE @OLNM5 CHAR(10)
DECLARE @OLNM6 CHAR(10)
DECLARE @OLNM7 CHAR(10)
DECLARE @OLNM8 CHAR(10)
DECLARE @OLNM9 CHAR(10)
DECLARE @OLNM10 CHAR(10)
declare @m_txndt datetime
declare @m_fy int
declare @bno int
declare @m_grno int
declare @m_year int
declare @m_bofy datetime
DECLARE @MBDATE DATETIME 
declare @spldisc money
declare @taxable money
declare @ltamt money
declare @lt1 money
declare @totrooms int
declare @notax char(1)
declare @oldlt money
declare @foodbill money
declare @chol numeric
declare @ltrno numeric
declare @ltrent money
declare @head int
declare @ctrlno numeric
declare @grpname varchar(50)
declare @con_id char(20)
declare @partic_con varchar(100)
declare @grpcounter int 
declare @brno numeric
declare @cno1 numeric
declare @grno1 numeric
declare @errmsg varchar(500)
set @errmsg = ''
set nocount on
SET @OLNM1 = ' '
SET @OLNM2 = ' '
SET @OLNM3 = ' '
SET @OLNM4 = ' '
SET @OLNM5 = ' '
SET @OLNM6 = ' '
SET @OLNM7 = ' '
SET @OLNM8 = ' '
SET @OLNM9 = ' '
SET @OLNM10 = ' '
set @taxable = 0
--EXEC RANDFILE @RV OUT
if exists (select * from room where gno=@gno)
   return
begin transaction
if @checkuser=0 begin
if @userno=0
   begin
   print 'unathourised access...deletion denied'
   goto e_error
   end

if @type_ol > 100 and @type_ol < 200
	begin  
		if @split='X'
			begin
			if not exists(select * from servicepermission where userno=@userno and serviceno=125 and outlet=@type_ol-100 and outlettype=1 and permission='Y')
				begin
				set @errmsg =  'You are not permitted to delete Bill of this Outlet after the guest checked out'
				goto e_error
				end
			end
		else
			begin
			if not exists(select * from servicepermission where userno=@userno and serviceno in (127,128,129,130) and outlet=@type_ol-100 and outlettype=1 and permission='Y')
				begin
				set @errmsg = 'You are not permitted to Split/Convert to Main/Complement the Service A Bill of this Outlet after the guest checked out'
				goto e_error
				end
			end		
	end
if @type_ol > 200 and @type_ol < 300
	begin
	if @split='X'
		begin
		if not exists(select * from servicepermission where userno=@userno and serviceno=137 and outlet=@type_ol-200 and outlettype=2 and permission='Y')
			begin
			set @errmsg = 'You are not permitted to delete Bill of this Outlet after the guest checked out'
			goto e_error
			end
		end
	else
		begin
		if not exists(select * from servicepermission where userno=@userno and serviceno in (137,138,139,140) and outlet=@type_ol-200 and outlettype=2 and permission='Y')
			begin
			set @errmsg = 'You are not permitted to Split/Convert to Main/Complement the Service B  Bills of this Outlet after the guest checked out'
			goto e_error
			end
		end	
	end
end --- end of checking user
/*
select @t1 = 'DBL' + ltrim(@RV)
SET @SQL = 'SELECT * INTO ' + @T1 + '  FROM  DUMMYBILL'
EXEC (@SQL)
IF @@ERROR <> 0 GOTO E_ERROR
set @t2 = 'dfbpm' + ltrim(@RV)
SET @SQL = 'SELECT * INTO ' + @T2 + '  FROM  DUMMYPYMT'
EXEC (@SQL)
IF @@ERROR <> 0 GOTO E_ERROR
*/
if @type_ol > 200 and @type_oL  < 300
begin
   set @outlet = @type_ol - 200
   set @chol = @type_ol
   select @bp1=billposition1,@bp2=billposition2,@bcompany=company,@selfdeal=selfdeal  from phoutlet where outlet = @outlet
   if @bcompany=1 
       set @bp = @bp1
   else
        if @selfdeal='N'
             begin
               set @bp=@bp1
  set @bcompany = 1
              end
        else 
              begin 
  set @bp = @bp2
              end
end

if @type_ol > 300 
begin
   set @outlet = @type_ol - 300
   set @chol = @type_ol
   select @bp1=billposition1,@bp2=billposition2,@bcompany=company,@selfdeal=selfdeal  from toutlet where outlet = @outlet
   if @bcompany=1 
       set @bp = @bp1
   else
        if @selfdeal='N'
             begin
              set @bp=@bp1
	  set @bcompany = 1
             end
        else 
              begin 
		  set @bp = @bp2
              end
end
if @type_ol < 200
begin
   set @outlet = @type_ol - 100
   select @channel=channel,@selfdeal=selfdeal from outlet where code=@outlet 
   set @chol = 100+@channel
   select @bp1=billposition1,@bp2=billposition2 ,@bcompany=company from restco where channel = @channel
   if @bcompany=1 
       set @bp = @bp1
   else
        if @selfdeal='N'
             begin
				 set @bp=@bp1
				 set @bcompany = 1
             end
        else 
              begin 
				 set @bp = @bp2
              end
end
declare @cidate datetime
declare @codate datetime
select @brno=brno,@cidate=ci_date,@codate=co_date from guest where gno=@gno
select @cno=cno,@grno=grno,@mainbilldate=txndt from billhist1 where  brno=@brno and company=@bcompany and split=@split
if not exists(select * from billhist where cno=@cno and datediff(d,txndt,@billdt)=0)
if not (datediff(d,'1/1/90',@billdt) between datediff(d,'1/1/90',@cidate)-1 and datediff(d,'1/1/90',@codate)) 
begin set @errmsg = 'The new bill is beyond the stay period of this guest...updation aborted' goto e_error end
set @cno=isnull(@cno,0)
if @cno<> 0   -- new begin on 16-01-2007 Maatu pongal
begin
if @amt + @namt <> 0 and @brno<> 0
   begin
         SET @MBDATE = @MAINBILLDATE
         EXEC @FY= GETFY  @mainbilldate,@bCOMPANY,@FY OUT
         EXEC DELGRNO @bCOMPANY,@FY,@GRNO,@mainbilldate,@RET_VAL OUT
         IF @@ERROR <> 0 GOTO E_ERROR
   end
select @ctrlno=ctrlno from billhist where ctrlno = (select top 1 ctrlno from billhist where datediff(d,'1/1/80',txndt) =  str(datediff(d,'1/1/80',@billdt))  and cno =  str(@cno)  and gno = str(@gno) and company = str(@bcompany) ) 

if isnull(@ctrlno,0) <> 0
begin
	if @lux = 'N'
	   begin
	      set @sql = 'update billhist set outlet' + ltrim(str(@bp)) +  ' = ' + str(@chol)+ ' , item' + ltrim(str(@bp)) + ' = ( item' + ltrim(str(@bp))  + ' + ' + convert(varchar(20),@namt) + ' ) - '  + convert(varchar(20),@amt)  + ', total = total - ' + convert(varchar(20),@amt) + ' + '  + convert(varchar(20),@namt)+ ' where ctrlno = ' + str(@ctrlno)
	      exec (@sql)
	      if @@error <> 0  goto e_error
	   end
	else
	   begin
	   if @bcompany = 1 and @split='N'
	      begin
	        set @sql = 'update billhist set outlet' + ltrim(str(@bp)) +  ' = ' + str(@chol)+ ' , item' + ltrim(str(@bp)) + ' = ( item' + ltrim(str(@bp))  + ' + ' + convert(varchar(20),@namt) + ' ) - '  + convert(varchar(20),@amt)  + ', total = total - ' + convert(varchar(20),@amt) + ' + '  + convert(varchar(20),@namt)+',taxable= taxable + ' + convert(varchar(20),@namt) + '-' + convert(varchar(20),@amt) +  ' where ctrlno = ' + str(@ctrlno)
	        exec (@sql)
	        if @@error <> 0  goto e_error
	        select @notax=lt,@head=male+female from guest where gno=@gno
	        if @notax='Y'
	           select @ltamt = rent+taxable-discount from billhist  where datediff(d,'1/1/80',txndt) =  str(datediff(d,'1/1/80',@billdt))  and cno = str(@cno)
	        else
	           select @ltamt = taxable from billhist  where datediff(d,'1/1/80',txndt) =  str(datediff(d,'1/1/80',@billdt))  and cno = str(@cno)
	      
	        set @totrooms = 1
	        select @totrooms=count(*) from addroom where gno = @gno
	        if @totrooms < 1 set @totrooms = 1
	
	        select @ltrno=rno,@ltrent=rent,@discount=discount from billhist where   datediff(d,'1/1/80',txndt) =  str(datediff(d,'1/1/80',@billdt))  and cno = str(@cno)
	        exec spltp @ltamt,@billdt,@lt1 out ,@totrooms,@ltrno,@ltrent,@head,@discount
	        if @@error <> 0  goto e_error
	        select @oldlt=lt from billhist where company=@bcompany and cno=@cno and gno=@gno and datediff(dd,'1/1/80',txndt)=datediff(dd,'1/1/80',@billdt)
	        set @sql = 'update billhist set lt = ' + convert(varchar(20),@lt1) +   ',total=total + ' + convert(varchar(20),@lt1) + ' - '+convert(varchar(20),@oldlt) +   ' where datediff(d,''1/1/80'',txndt) = ' + str(datediff(d,'1/1/80',@billdt)) + ' and cno = ' + str(@cno) + ' and gno = ' + str(@gno) + ' and company = ' + str(@bcompany)
	        exec (@sql)
	        if @@error <> 0  goto e_error
	      end
   else
      begin
      set @sql = 'update billhist set outlet' + ltrim(str(@bp)) +  ' = ' + str(@chol)+ ' , item' + ltrim(str(@bp)) + ' = ( item' + ltrim(str(@bp))  + ' + ' + convert(varchar(20),@namt) + ' ) - '  + convert(varchar(20),@amt)  + ', total = total - ' + convert(varchar(20),@amt) + ' + '  + convert(varchar(20),@namt)+ ' where datediff(d,''1/1/80'',txndt) = ' + str(datediff(d,'1/1/80',@billdt))+ ' and cno = ' + str(@cno) + ' and gno = ' + str(@gno) + ' and company = ' + str(@bcompany)
      exec (@sql)
        if @@error <> 0  goto e_error
     end
   end
END
-- new insertion on 29 dec 2007 for calculating the service charges on all services (for classic hotel)

declare @scforall tinyint
declare @scrate money
set @scrate=0
declare @g_sc_afterdisc int
select @scforall=scforall,@g_sc_afterdisc=g_sc_afterdisc from setting
select @scrate=sc from guest where gno=@gno
if @scforall=1   -- these codes will work only when the scforall is set to 1 in the setting table   (date of insertion 07 December 2007) for Hotel Classic
	if @scrate<>0
		begin
			if @g_sc_afterdisc=0
				set @sql = 'update billhist set sc=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10) * ' + str(@scrate)  + '/100 where  cno = ' + str(@cno) + ' and gno = ' + str(@gno) + ' and company = ' + str(@bcompany)
			else
				set @sql = 'update billhist set sc=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-discount) * ' + str(@scrate)  + '/100 where  cno = ' + str(@cno) + ' and gno = ' + str(@gno) + ' and company = ' + str(@bcompany)
			exec(@sql)
			set @sql = 'update billhist set total=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10+lt+isnull(sc,0)-discount+ isnull(st,0) + isnull(cess,0)+ isnull(cess2,0) )  where  cno = ' + str(@cno) + ' and gno = ' + str(@gno) + ' and company = ' + str(@bcompany)
			exec(@sql)
		end

-- this code ends new 29-12-2007

set @cno1=@cno
set @grno1 = @grno
exec pbvc @cno,@cno
if @@error <> 0  goto e_error
end
------------------------------------------                HERE STARTS THE NEW FuNDA
--print 'periya' + @split1
--goto e_error
IF @SPLIT1<> 'X'
BEGIN
EXEC RANDFILE @RV OUT
SET @SPLIT=@SPLIT1
SET @AMT=@AMT1
SET @NAMT=@NAMT1
SET @CNO = 0 
EXEC RANDFILE @RV OUT
set @grno=0
--print convert(varchar(20),@amt) + ' ' + convert(varchar(20),@namt)
--goto e_error
SELECT @CNO=ISNULL(CNO,0),@GRNO=ISNULL(GRNO,0) ,@mainbilldate=ISNULL(txndt,@MBDATE)  FROM BILLHIST1 WHERE brno=@brno AND COMPANY=@bCOMPANY AND SPLIT=@SPLIT
set @dtfound = 0
if @namt<> 0
	BEGIN
		SELECT @CNO=ISNULL(CNO,0),@GRNO=ISNULL(GRNO,0) ,@mainbilldate=ISNULL(txndt,@MBDATE)  FROM BILLHIST1 WHERE brNO=@brNO AND COMPANY=@bCOMPANY AND SPLIT=@SPLIT
		if ISNULL(@grno,0)<>0
		begin
		        EXEC @FY= GETFY  @mainbilldate,@bCOMPANY,@FY OUT
		        EXEC DELGRNO @bCOMPANY,@FY,@GRNO,@mainbilldate,@RET_VAL OUT
		        IF @@ERROR <> 0 GOTO E_ERROR
		end
	END
if exists(select * from billhist where gno=@gno and cno=@cno and datediff(d,'1/1/90',@billdt)=datediff(d,'1/1/90',txndt) ) set @dtfound=1
if @type_ol < 200
   begin
   if @bp1=1
      select @olnm1  = shname from restco where channel = @outlet
   if @bp1=2
      select @olnm2  = shname from restco where channel = @outlet
   if @bp1=3
      select @olnm3  = shname from restco where channel = @outlet
   if @bp1=4
      select @olnm4  = shname from restco where channel = @outlet
   if @bp1=5
      select @olnm5  = shname from restco where channel = @outlet
   if @bp1=6
      select @olnm6  = shname from restco where channel = @outlet
   if @bp1=7
      select @olnm7  = shname from restco where channel = @outlet
   if @bp1=8
      select @olnm8  = shname from restco where channel = @outlet
   if @bp1=9
      select @olnm9  = shname from restco where channel = @outlet
   if @bp1=10
      select @olnm10  = shname from restco where channel = @outlet
   end  
else
if @type_ol > 200 and @type_ol < 300
  begin
      if @bp=1
         select @olnm1 = shname from phoutlet  where outlet = @outlet
      if @bp=2
         select @olnm2 = shname from phoutlet  where outlet = @outlet
      if @bp=3
         select @olnm3 = shname from phoutlet  where outlet = @outlet
      if @bp=4
         select @olnm4 = shname from phoutlet  where outlet = @outlet
      if @bp=5
         select @olnm5 = shname from phoutlet  where outlet = @outlet
      if @bp=6
         select @olnm6 = shname from phoutlet  where outlet = @outlet
      if @bp=7
         select @olnm7 = shname from phoutlet  where outlet = @outlet
      if @bp=8
         select @olnm8 = shname from phoutlet  where outlet = @outlet
      if @bp=9
         select @olnm9 = shname from phoutlet  where outlet = @outlet
      if @bp=10
         select @olnm10 = shname from phoutlet  where outlet = @outlet
   end
else
if @type_ol > 300
  begin
      if @bp=1
         select @olnm1 = shname from toutlet  where outlet = @outlet
      if @bp=2
         select @olnm2 = shname from toutlet  where outlet = @outlet
      if @bp=3
         select @olnm3 = shname from toutlet  where outlet = @outlet
      if @bp=4
         select @olnm4 = shname from toutlet  where outlet = @outlet
      if @bp=5
         select @olnm5 = shname from toutlet  where outlet = @outlet
      if @bp=6
         select @olnm6 = shname from toutlet  where outlet = @outlet
      if @bp=7
         select @olnm7 = shname from toutlet  where outlet = @outlet
      if @bp=8
         select @olnm8 = shname from toutlet  where outlet = @outlet
      if @bp=9
         select @olnm9 = shname from toutlet  where outlet = @outlet
      if @bp=10
         select @olnm10 = shname from toutlet  where outlet = @outlet
   end
IF @@ERROR <> 0 GOTO E_ERROR
if @dtfound=1
begin
	select @ctrlno=ctrlno from billhist where ctrlno = (select top 1 ctrlno from billhist where datediff(d,'1/1/80',txndt) =  str(datediff(d,'1/1/80',@billdt))  and cno =  str(@cno)  and gno = str(@gno) and company = str(@bcompany) ) 
	if @lux='Y' and @bcompany=1 and @split='N' 
	begin
	set @sql = 'update billhist set outlet' + ltrim(str(@bp)) +  ' = ' + str(@chol)+ ' , item' + ltrim(str(@bp)) + ' = (item' + ltrim(str(@bp))  +' + '  + convert(varchar(20),@namt-@amt) +'), total = total + '  + convert(varchar(20),@namt-@amt)+ ',taxable=taxable + ' + convert(varchar(20),@namt-@amt)+ ' where ctrlno=' + str(@ctrlno)
	exec (@sql)
	if @@error <> 0  goto e_error
	end
	else
	begin
	set @sql = 'update billhist set outlet' + ltrim(str(@bp)) +  ' = ' + str(@chol)+ ' , item' + ltrim(str(@bp)) + ' = (item' + ltrim(str(@bp))  +' + '  + convert(varchar(20),@namt-@amt)  + '), total = total + '  + convert(varchar(20),@namt-@amt)+ ' where ctrlno=' + str(@ctrlno)
	exec (@sql)
	end
	IF @@ERROR <> 0 GOTO E_ERROR
	 if @bp = 1
	     update billhist1 set olnm1 = @olnm1 where cno = @cno and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	 if @bp = 2
	     update billhist1 set olnm2 = @olnm2 where cno = @cno and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	 if @bp = 3
	     update billhist1 set olnm3 = @olnm3 where cno = @cno and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	 if @bp = 4
	     update billhist1 set olnm4 = @olnm4 where cno = @cno and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	 if @bp = 5
	     update billhist1 set olnm5 = @olnm5 where cno = @cno and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	 if @bp = 6    
	     update billhist1 set olnm6 = @olnm6 where cno = @cno and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	 if @bp = 7
	     update billhist1 set olnm7 = @olnm7 where cno = @cno and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	 if @bp = 8
	     update billhist1 set olnm8 = @olnm8 where cno = @cno and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	 if @bp = 9
	     update billhist1 set olnm9 = @olnm9 where cno = @cno and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	 if @bp = 10
	     update billhist1 set olnm10 = @olnm10 where cno = @cno  -- and  datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@billdt)
	     select @crname=crname,@crnum=crnum,@crtype=crtype,@glactno=glactno,@mop=mop from billhist1 where cno=@cno --  gno=@gno and company=@bcompany and split=@split
	  end
else
begin
	IF @CNO=0
	 BEGIN
		select @mainbilldate=co_date from guest where  gno=@gno
		set @m_txndt = @mainbilldate
		EXEC @M_FY= GETFY  @M_TXNDT,@bCOMPANY,@M_FY OUT
		IF @@ERROR <> 0 GOTO E_ERROR
		EXEC  GETGRP @M_TXNDT,@bCOMPANY,@M_GRNO OUT
		IF @@ERROR <> 0 GOTO E_ERROR
		IF DATEPART(mm,@M_TXNDT) >=  1 AND DATEPART(mm,@M_TXNDT) <= 3
			SET @M_YEAR = DATEPART(yy,@M_TXNDT)-1
		ELSE
			SET @M_YEAR = DATEPART(yy,@M_TXNDT)
		SELECT @M_BOFY = '1 APR ' + STR(@M_YEAR)
	 
		IF @SPLIT='N'
			BEGIN
				UPDATE COMPANY SET BNO=CASE WHEN BDT <> @M_BOFY THEN 1 ELSE BNO+1 END , BDT= CASE  WHEN BDT <> @M_BOFY THEN @M_BOFY ELSE BDT END WHERE COMPANY = @bCOMPANY
				IF @@ERROR <> 0 GOTO E_ERROR
				SELECT @BNO=BNO FROM COMPANY WHERE COMPANY= @bCOMPANY
			END
/*
		ELSE IF @SPLIT='Y'
			BEGIN
				UPDATE COMPANY SET BNO1=CASE WHEN BDT1 <> @M_BOFY THEN 1 ELSE BNO1+1 END , BDT1= CASE  WHEN BDT1 <> @M_BOFY THEN @M_BOFY ELSE BDT1 END WHERE COMPANY = @bCOMPANY
				IF @@ERROR <> 0 GOTO E_ERROR
				SELECT @BNO=BNO1 FROM COMPANY WHERE COMPANY= @bCOMPANY
			END
		ELSE
			BEGIN
				UPDATE COMPANY SET BNO2=CASE WHEN BDT2 <> @M_BOFY THEN 1 ELSE BNO2+1 END , BDT2= CASE  WHEN BDT2 <> @M_BOFY THEN @M_BOFY ELSE BDT2 END WHERE COMPANY = @bCOMPANY
				IF @@ERROR <> 0 GOTO E_ERROR
				SELECT @BNO=BNO2 FROM COMPANY WHERE COMPANY= @bCOMPANY
			END
*/
			ELSE IF @SPLIT='Y'
				BEGIN
						update finyear set bno1=bno1+1 where fy=@m_fy
						if @@error<>0 goto e_error
						select @bno=bno1 from finyear where fy=@m_fy
				END
			ELSE
				BEGIN
						update finyear set bno2=bno2+1 where fy=@m_fy
						if @@error<>0 goto e_error
						select @bno=bno2 from finyear where fy=@m_fy
				END



	
		INSERT INTO BILLHIST1 (brNO,COMPANY,SPLIT,BNO,TXNDT,ADVANCE,CRNAME,CRNUM,CRTYPE,GLACTNO,MOP,GRNO,
		OLNM1,OLNM2,OLNM3,OLNM4,OLNM5,OLNM6,OLNM7,OLNM8,OLNM9,OLNM10)
		VALUES (@brno,@BCOMPANY,@SPLIT,@BNO,@mainbilldate,0,'','','',0,0, @m_grno ,
		@OLNM1,@OLNM2,@OLNM3,@OLNM4,@OLNM5,@OLNM6,@OLNM7,@OLNM8,@OLNM9,@OLNM10)
		IF @@ERROR <> 0 GOTO E_ERROR
		set @cno=@@identity
	 end
	 select @cno=cno,@crname=crname,@crnum=crnum,@crtype=crtype,@glactno=glactno,@mop=mop from billhist1 where cno=@cno --gno=@gno and company=@bcompany and split=@split
	 INSERT INTO BILLHIST(GNO,TXNDT,DISCOUNT,RENT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,
	 OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10,SC,LT,TOTAL,COMPANY,HOSTID,cno,taxable,st,cess,cess2,lrate)
	 VALUES(@GNO,@billdt,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,@bcompany,'0',@cno,0,0,0,0,0)
	 IF @@ERROR <> 0 GOTO E_ERROR
	 set @ctrlno=@@identity
	if @lux='Y' and @bcompany=1 and @split='N'
		begin
		set @sql = 'update billhist set outlet' + ltrim(str(@bp)) +  ' = ' + str(@chol)+ ' , item' + ltrim(str(@bp)) + ' = ( item' + ltrim(str(@bp))  + ' + '  + convert(varchar(20),@namt-@amt)  + '), total = total + '  + convert(varchar(20),@namt-@amt)+ ',taxable=taxable + ' + convert(varchar(20),@namt-@amt)+ ' where ctrlno=' + str(@ctrlno)
		exec (@sql)
		end
	else
		begin
		set @sql = 'update billhist set outlet' + ltrim(str(@bp)) +  ' = ' + str(@chol)+ ' , item' + ltrim(str(@bp)) + ' = ( item' + ltrim(str(@bp))  + ' + '  + convert(varchar(20),@namt-@amt)  + '), total = total + '  + convert(varchar(20),@namt-@amt)+ ' where ctrlno=' + str(@ctrlno)
		exec (@sql)
		end
end
   if @lux='Y' and @bcompany=1 and @split='N'
   if @bcompany = 1 and @split='N'
      begin
        select @notax=lt,@head=male+female from guest where gno=@gno
        if @notax='Y'
          select @ltamt = rent+taxable-discount from billhist  where datediff(d,'1/1/80',txndt) =  str(datediff(d,'1/1/80',@billdt))  and cno = str(@cno)
        else
           select @ltamt = taxable from billhist  where datediff(d,'1/1/80',txndt) =  str(datediff(d,'1/1/80',@billdt))  and cno = str(@cno)
        set @totrooms = 1
        select @totrooms=count(*) from addroom where gno = @gno
        if @totrooms < 1 set @totrooms = 1
        select @ltrno=rno,@ltrent=rent,@discount=discount from billhist where   datediff(d,'1/1/80',txndt) =  str(datediff(d,'1/1/80',@billdt))  and cno = str(@cno)
        exec spltp @ltamt,@billdt,@lt1 out ,@totrooms,@ltrno,@ltrent ,@head,@discount
        if @@error <> 0  goto e_error
        select @oldlt=lt from billhist where company=@bcompany and cno=@cno and gno=@gno and datediff(dd,'1/1/80',txndt)=datediff(dd,'1/1/80',@billdt)
        if @@error <> 0  goto e_error
        set @sql = 'update billhist set lt = ' + convert(varchar(20),@lt1) +   ',total=total + ' + convert(varchar(20),@lt1) + ' - '+convert(varchar(20),@oldlt) +   ' where ctrlno=' + str(@ctrlno)
        exec (@sql)
        if @@error <> 0  goto e_error
      end
---print str(@grno1) + ' ' + str(@m_grno)   + ' ' + str(@cno1) +'  '+ STR(@cno) goto e_error
-- new insertion on 29 dec 2007 for calculating the service charges on all services (for classic hotel)

set @scrate=0
select @scforall=scforall,@g_sc_afterdisc=g_sc_afterdisc from setting
select @scrate=sc from guest where gno=@gno
if @scforall=1   -- these codes will work only when the scforall is set to 1 in the setting table   (date of insertion 07 December 2007) for Hotel Classic
	if @scrate<>0
		begin
		if @g_sc_afterdisc=0
		set @sql = 'update billhist set sc=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10) * ' + str(@scrate)  + '/100 where  cno = ' + str(@cno) + ' and gno = ' + str(@gno) + ' and company = ' + str(@bcompany)
		else
		set @sql = 'update billhist set sc=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-discount) * ' + str(@scrate)  + '/100 where  cno = ' + str(@cno) + ' and gno = ' + str(@gno) + ' and company = ' + str(@bcompany)
		exec(@sql)
		set @sql = 'update billhist set total=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10+lt+isnull(sc,0)-discount+isnull(st,0)+isnull(cess,0)+isnull(cess2,0))  where  cno = ' + str(@cno) + ' and gno = ' + str(@gno) + ' and company = ' + str(@bcompany )
		exec(@sql)
		end

-- this code ends new 29-12-2007


exec pbvc @cno,@cno
if @@error <> 0  goto e_error
END
commit transaction
return 
E_ERROR:
rollback transaction
set @errmsg = 'DelCout Failed' + @errmsg 
raiserror(@errmsg,16,-1)
return



GO
/****** Object:  StoredProcedure [dbo].[delcursor]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
create PROCEDURE [dbo].[delcursor]
@cursorname varchar(100)	-- Add the parameters for the stored procedure here
AS
BEGIN
    IF CURSOR_STATUS('global', @cursorName) >= -1
    BEGIN
        DECLARE @SQL NVARCHAR(91)
        IF CURSOR_STATUS('global', @cursorName) > -1
        BEGIN
            SET @SQL = N'CLOSE ' + @cursorName
            EXEC sp_executeSQL @SQL
        END
        SET @SQL = N'DEALLOCATE ' + @cursorName
        EXEC sp_executeSQL @SQL
    END
END
GO
/****** Object:  StoredProcedure [dbo].[deleteglac]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[deleteglac]  @actnum numeric, @fy numeric, @allow int, @multiple int
/* with encryption */
AS
declare @nac numeric
declare @nfy numeric
declare @tfy numeric
declare @company numeric
declare @err varchar(2000)
declare @desc varchar(200)
begin transaction
set @err= 'No error'
select @company=company,@desc=cgp_desc from glprimld where fy=@fy and cgp_actno=@actnum

if exists (select  * from vouchers where company=@company and actnum=@actnum) 
    begin
  	set @err = 'There are transactions in this account ' + ltrim(str(@actnum))
	goto e_error
	end
if exists(select * from billhist1 where glactno=@actnum) 
	Begin 
	set @err='There are checkouts in this gl account..no deletion allowed - ' + @desc
	goto e_error
	end

if exists(select * from kithist1 where pmode=4 and gno=@actnum AND outlet in (select outlet from restco where company=@company))
	Begin 
	set @err='There are KOT bills made against this gl account..no deletion allowed - ' + @desc
	goto e_error
	end

if exists(select * from kithist1 where pmode1=4 and gno1=@actnum AND outlet in (select outlet from restco where company=@company))
	Begin 
	set @err='There are KOT bills made against this gl account..no deletion allowed - ' + @desc
	goto e_error
	end

if @allow=1 update glprimld set deletable='Y' where COMPANY=@COMPANY and cgp_actno=@actnum

delete from glprimld where company=@company and cgp_actno=@actnum
if @@error<>0  begin set @err='Error in deleting'  goto e_error end

commit transaction 
return

e_error:
rollback transaction
raiserror(@err,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[DELGRNO]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) New Delhi*/
/******reviewed by me on 03 Aug 2006******/
/****** Object:  Stored Procedure dbo.DELGRNO    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.DELGRNO    Script Date: 1/20/03 11:51:48 AM ******/
/****** Object:  Stored Procedure dbo.DELGRNO    Script Date: 10/14/02 1:17:58 PM ******/
CREATE PROCEDURE [dbo].[DELGRNO] @M_COMPANY INT,@M_FY INT, @M_GRNO INT, @M_TXNDT DATETIME, @RET_VAL INT OUT  
/* with encryption */
AS
DECLARE @M_TXNNO INT
DECLARE @MYERROR INT
DECLARE @SQL VARCHAR(5000)
declare @m_actnum numeric
declare @err varchar(5000)
set nocount on
set @err = 'GRNo deletion failed'
BEGIN TRANSACTION
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 (1) and try'
  --goto final
end
if not exists (select * from vouchers where fy=@m_fy and datediff(dd,txndt,@m_txndt)=0 and grno=@m_grno and channelcode='Y')
begin
insert into vchdeleted select * from vouchers where fy=@m_fy and datediff(dd,txndt,@m_txndt)=0 and grno=@m_grno
---insert into acclog (narration) values ('Journal Entry No-' + ltrim(str(@m_grno)) + ' Company-' + ltrim(str(@m_company)) + ',' + convert(varchar(20),@m_txndt) + ' deleted')
end
--DECLARE DELGR CURSOR FOR SELECT TXNNO,actnum FROM VOUCHERS WHERE  COMPANY=@M_COMPANY AND FY = @M_FY AND datediff(dd,TXNDT,@M_TXNDT)=0 AND GRNO = @M_GRNO  order by txnno desc


declare delgr cursor for select txnno,actnum from vouchers where  fy=@m_fy and datediff(dd,TXNDT,@M_TXNDT)=0 AND GRNO = @M_GRNO  order by txnno desc
open delgr
fetch next from delgr into @m_txnno,@m_actnum
while (@@fetch_status=0)
begin
delete from vouchers where  txnno=@m_txnno  --current of delgr
if @@error<>0 goto final
update glprimld set ltxndt = case when datediff(d,@m_txndt,ltxndt) > 0 then @m_txndt else ltxndt end where fy=@m_fy and cgp_actno=@m_actnum
--exec fillbal @m_txndt,@m_company,@m_actnum
if @@error <>0  goto final
fetch next from delgr into @m_txnno,@m_actnum
end
close delgr
deallocate delgr
commit transaction
select @ret_val=0
return (0)
final:
rollback transaction
--deallocate delgr
select @ret_val=1
raiserror(@err,16,-1)
return(1)





GO
/****** Object:  StoredProcedure [dbo].[DELGRNO1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) New Delhi*/
/******reviewed by me on 03 Aug 2006******/
/****** Object:  Stored Procedure dbo.DELGRNO    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.DELGRNO    Script Date: 1/20/03 11:51:48 AM ******/
/****** Object:  Stored Procedure dbo.DELGRNO    Script Date: 10/14/02 1:17:58 PM ******/
/*** creeated on 3- sep 2006 ****/ 
CREATE PROCEDURE [dbo].[DELGRNO1] @M_COMPANY INT,@M_FY INT, @M_GRNO INT, @M_TXNDT DATETIME, @actnum numeric  
/* with encryption */
AS
DECLARE @M_TXNNO INT
DECLARE @MYERROR INT
DECLARE @SQL VARCHAR(5000)
declare @m_actnum int
set nocount on
BEGIN TRANSACTION
if not exists(select * from hmsfolder where version_no = (select version_no from comuser where [host_id]=host_id()) ) 
  begin 
  print 'You are using a wrong version of the Softwre Installed...Please update the version (2) and try'
  goto final
end
if not exists (select * from vouchers where company=@m_company and fy=@m_fy and datediff(dd,'1/1/90',txndt) = datediff(dd,'1/1/90',@m_txndt) and grno=@m_grno and channelcode='Y')
begin
insert into vchdeleted select * from vouchers where company=@m_company and fy=@m_fy and datediff(dd,'1/1/90',txndt) = datediff(dd,'1/1/90',@m_txndt) and grno=@m_grno and actnum<>@actnum
---insert into acclog (narration) values ('Journal Entry No-' + ltrim(str(@m_grno)) + ' Company-' + ltrim(str(@m_company)) + ',' + convert(varchar(20),@m_txndt) + ' Partially deleted by cb2' )
end
DECLARE DELGR CURSOR FOR SELECT TXNNO,actnum FROM VOUCHERS WHERE  COMPANY=@M_COMPANY AND FY = @M_FY AND datediff(dd,'1/1/80',TXNDT) = datediff(dd,'1/1/80',@M_TXNDT) AND GRNO = @M_GRNO  and actnum <> @actnum order by txnno desc
OPEN DELGR
FETCH NEXT FROM DELGR INTO @M_TXNNO,@m_actnum
WHILE (@@FETCH_STATUS=0)
BEGIN
delete from vouchers where  txnno=@m_txnno  --current of delgr
IF @@ERROR <> 0 GOTO FINAL   

update glprimld set ltxndt = case when datediff(d,@m_txndt,ltxndt) > 0 then @m_txndt else ltxndt end where fy=@m_fy and cgp_actno=@m_actnum
--exec fillbal @m_txndt,@m_company,@m_actnum
if @@error <>0  goto final
FETCH NEXT FROM DELGR INTO @M_TXNNO,@m_actnum
END
CLOSE DELGR
DEALLOCATE DELGR
COMMIT TRANSACTION
RETURN (0)
FINAL:
ROLLBACK  TRANSACTION
DEALLOCATE DELGR
RAISERROR ('GRNO DELETION FAILED - ROLLED BACK',16,-1)
RETURN (1)





GO
/****** Object:  StoredProcedure [dbo].[dootacommvouching]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[dootacommvouching] @commctrlno numeric
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @cno numeric
	select @cno=cno from billhist1 where company=1 and split='N' and  brno = (select top 1 brno from guest where otacommctrlno=@commctrlno)
    set @cno =isnull(@cno,0)
	if @cno<>0
	exec pbvc @cno,@cno
END

GO
/****** Object:  StoredProcedure [dbo].[dropcolumn]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[dropcolumn] @tablename varchar(100),@column varchar(100)
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;


Declare @CONSTRAINT varchar(max)
                    set @CONSTRAINT ='ALTER TABLE '+@TABLENAME+' DROP CONSTRAINT '
                    set @CONSTRAINT = @CONSTRAINT + (select SYS_OBJ.name as CONSTRAINT_NAME
                    from sysobjects SYS_OBJ
                    join syscomments SYS_COM on SYS_OBJ.id = SYS_COM.id
                    join sysobjects SYS_OBJx on SYS_OBJ.parent_obj = SYS_OBJx.id 
                    join sysconstraints SYS_CON on SYS_OBJ.id = SYS_CON.constid
                    join syscolumns SYS_COL on SYS_OBJx.id = SYS_COL.id
                    and SYS_CON.colid = SYS_COL.colid
                    where
                    SYS_OBJ.uid = user_id() and SYS_OBJ.xtype = 'D'
                    and SYS_OBJx.name=@TABLENAME and SYS_COL.name=@COLUMN)
                    exec(@CONSTRAINT)

set @CONSTRAINT = 'alter table ' +  @tablename + ' drop column ' + @column 

exec(@CONSTRAINT)

declare @constraintname nvarchar(200)
SELECT @ConstraintName = Name FROM SYS.DEFAULT_CONSTRAINTS WHERE PARENT_OBJECT_ID = OBJECT_ID('__TableName__') AND PARENT_COLUMN_ID = (SELECT column_id FROM sys.columns WHERE NAME = N'__ColumnName__' AND object_id = OBJECT_ID(N'__TableName__'))
IF @ConstraintName IS NOT NULL
    EXEC('ALTER TABLE ' + @TableName + ' DROP CONSTRAINT ' + @ConstraintName)
 
 
END

GO
/****** Object:  StoredProcedure [dbo].[dropfile]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[dropfile] @t1 varchar(200)
/* with encryption */
AS
declare @sql varchar(5000)
set @sql = 'if exists(select * from sysobjects where name=''' + @t1 + '''and xtype=''U'') drop table ' +  @t1
exec(@sql)




GO
/****** Object:  StoredProcedure [dbo].[dropXXXtables]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  PROCEDURE [dbo].[dropXXXtables]
AS
BEGIN
	SET NOCOUNT ON;
	declare @name varchar(100)
	declare @sql varchar(500)
	declare c1 cursor for select name from sys.objects  where name like 'XX%' and type='U'  order by name
	open c1 
	fetch next from c1 into @name 
	while @@fetch_status = 0
	begin
	set @sql = 'drop table ' + @name
	print @name
	exec (@sql)
	fetch next from c1 into @name
	end
	close c1 
	deallocate c1
END

GO
/****** Object:  StoredProcedure [dbo].[FDRonetime]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[FDRonetime] @asset char(1)='D'
AS
BEGIN
	SET NOCOUNT ON;
	declare @company numeric
	declare @tdno numeric
	declare @amount money
	declare @balance money
	declare @txnno numeric
	declare @fdrsno numeric
	declare @closdate datetime 
	declare @totcnt numeric
	declare @cnt numeric
	declare @err varchar(1000)
	declare @bof datetime
	declare @amttyp char(1)
	declare @cnt2 numeric
	set @cnt2 =0 
update vouchers set amt=round(amt,2) where amt <> round(amt,2)
if @asset= 'D' set @amttyp = 'C' else set @amttyp = 'D'
	set @bof = getdate()
	set @cnt=0
	--select @totcnt=count(*) from fdr
	select @totcnt=count(*) from fdr where tdno in (select distinct tdno from glprimld where asset=@asset and onac='S')	and isnull(updated,'N') = 'N'

	declare c1 cursor for select company,tdno,amount,balance,txnno,fdrsno,closdate from fdr where tdno in (select distinct tdno from glprimld where asset=@asset and onac='S') and isnull(updated,'N')= 'N' order by fdrsno desc
	open c1
	fetch next from c1 into @company,@tdno,@amount,@balance,@txnno,@fdrsno,@closdate
	while  @@fetch_status=0
	begin
	if exists(select * from vouchers where txnno2=@txnno and amt >= @balance)
begin
	set @cnt=@cnt+1
	set @err = ltrim(str(@totcnt)) + '/' + ltrim(str(@cnt))
	raiserror (@err,0,1) with nowait
   	update vouchers set totamt_balance=@balance,billdate_CloseDate= @closdate where txnno2=@txnno
    update vouchers set chlno_RTXNNO=@txnno,totamt_balance=0 where fdrsno=@fdrsno and amttyp=@amttyp
	update fdr set updated='Y' where FDRSNO=@fdrsno
end
else
begin
	set @cnt2=@cnt2+1
	set @err = ltrim(str(@cnt2))
	raiserror (@err,0,1) with nowait
end
	fetch next from c1 into @company,@tdno,@amount,@balance,@txnno,@fdrsno,@closdate
	end
	close c1
	deallocate c1


--	declare @cgp_actno numeric
--	declare @cgp_desc varchar(100)
--	declare c1 cursor for select company,cgp_Actno,cgp_desc from glprimld where asset='D' and onac='O' and fy=(select max(fy) from finyear where company=glprimld.company)
--	open c1
--	fetch next from c1 into @company,@cgp_actno,@cgp_desc
--	while @@fetch_status=0
--		begin
--		raiserror (@cgp_desc,0,1) with nowait
--		update vouchers set TOTAMT_BALANCE=amt where company=@company and actnum=@cgp_actno and amttyp='D'
--     	fetch next from c1 into @company,@cgp_actno,@cgp_desc
--		end
--close c1
--deallocate c1

declare @cgp_desc varchar(100)
declare @cgp_actno numeric

declare c2 cursor for select company from company 
open c2
fetch next from c2 into @company
while @@fetch_status=0
begin 
--	declare c1 cursor for select distinct company,cgp_Actno,cgp_desc from glprimld where asset='D' and onac='O' and company=@company and cgp_actno in (select actnum from vouchers where actnum in (select cgp_actno from glprimld where company=@company and onac='O'))  order by cgp_desc
--	open c1
--	fetch next from c1 into @company,@cgp_actno,@cgp_desc  
--	while @@fetch_status=0
--		begin
--		raiserror (@cgp_desc,0,1) with nowait
--		update vouchers set TOTAMT_BALANCE=amt,billdate_CloseDate=null,chlno_rtxnno=0  where company=@company and actnum=@cgp_actno 
--     	fetch next from c1 into @company,@cgp_actno,@cgp_desc
--		end
--close c1
--deallocate c1
   update vouchers set totamt_balance=amt where company=@company and actnum in (select cgp_actno from glprimld where company=@company and asset=@asset and onac='O')
   update vouchers set totamt_balance=amt where company=@company and actnum in (select cgp_actno from glprimld where company=@company and asset=@asset and onac='O')
fetch next from c2 into @company
end
close c2
deallocate c2

END


GO
/****** Object:  StoredProcedure [dbo].[FILLBAL]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.FILLBAL    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.FILLBAL    Script Date: 1/20/03 11:51:48 AM ******/
CREATE PROCEDURE [dbo].[FILLBAL] @M_TXNDT DATETIME,@M_COMPANY INT, @M_ACTNUM INT , @m_putbal int = 0 
/* with encryption */
AS
DECLARE @M_FY INT
DECLARE @M_AMTTYP CHAR(1)
DECLARE @M_AMT MONEY 
DECLARE @M_BALANCE MONEY
DECLARE @M_TDT DATETIME
DECLARE @M_TXNNO INT
DECLARE @M_NEXTYEARAC INT
DECLARE @M_NEXTYEARFY INT
DECLARE @M_GROUP INT
DECLARE @M_FSDATE DATETIME
DECLARE @M_YEAR INT
DECLARE @M_OB MONEY
DECLARE @M_BOFY DATETIME
declare @m_ob1 varchar(20)
declare @ltxndt datetime
DECLARE @OPGBALCON numeric(18,2)
declare @txnno1 numeric
declare @sdt datetime
declare @opgbal numeric(18,2)
declare @fy numeric
declare @actnum numeric
declare @debits numeric(18,2)
declare @credits numeric(18,2)
declare @sql varchar(4000)
set nocount on

IF @M_COMPANY IS NULL  OR @M_ACTNUM IS NULL
BEGIN
RAISERROR('EMPTY COMPANY CODE',16,-1)
RETURN
END

EXEC GETFY @M_TXNDT,@M_COMPANY,@M_FY OUT

--EXEC GETFY @M_TXNDT,@M_COMPANY,@M_FY OUT

--select @m_opgbal=dbo.getob(@m_company,@m_txndt,@m_actnum,@m_fy)
--if @m_putbal = 1 UPDATE GLPRIMLD SET BALANCE=0  WHERE   FY = @M_FY AND CGP_ACTNO = @M_ACTNUM
select @opgbal=isnull(opgbal,0) from glprimld where fy=@m_fy and cgp_actno=@m_actnum 

begin transaction 

--select @debits=sum(case when amttyp='D' then amt else 0 end)  ,@credits=sum(case when amttyp='C' then amt else 0 end)  
--from vouchers where fy=@m_fy and actnum=@m_actnum
--if @m_putbal = 1    UPDATE GLPRIMLD SET BALANCE=@M_OPGBAL+@credits-@debits  WHERE   FY = @M_FY AND CGP_ACTNO = @M_ACTNUM
declare @msg varchar(5000)

declare @x int
set @x=1
while @x <= 12 
begin
select @debits=sum(case when amttyp='D' then amt else 0 end)  ,@credits=sum(case when amttyp='C' then amt else 0 end)  
from vouchers where fy=@m_fy and actnum=@m_actnum and month(txndt)=@x
set @sql = 'update glprimld set credit' + ltrim(rtrim(str(@x))) + '=' + convert(varchar(20),isnull(@credits,0)) + ',debit' + ltrim(rtrim(str(@x))) + '=' + convert(varchar(20),isnull(@debits,0)) + ' where fy=' + str(@m_fy)+' and cgp_actno=' + str(@m_actnum)
exec(@sql)
set @x=@x+1
end 

--set @msg = convert(varchar(20),@credit1)
--raiserror(@msg,16,-1) return 


/*
DECLARE FILLBAL_CUR CURSOR FOR SELECT AMTTYP,AMT,BALANCE,TXNDT,TXNNO2,txnno 
FROM VOUCHERS WHERE 
FY=@M_FY AND ACTNUM=@M_ACTNUM 
and datediff(d,@m_txndt,txndt)>=0
ORDER BY datediff(dd,'1/1/80',txndt),TXNNO2

OPEN FILLBAL_CUR
FETCH NEXT FROM FILLBAL_CUR INTO @M_AMTTYP,@M_AMT,@M_BALANCE,@M_TDT,@M_TXNNO,@txnno1
WHILE  @@FETCH_STATUS=0 BEGIN
IF @M_AMTTYP='D'
SET @M_OPGBAL = @M_OPGBAL - @M_AMT
ELSE
SET @M_OPGBAL = @M_OPGBAL+ @M_AMT

UPDATE vouchers SET BALANCE= @M_OPGBAL WHERE txnno=@txnno1
set @ltxndt = @m_tdt

FETCH NEXT FROM FILLBAL_CUR INTO @M_AMTTYP,@M_AMT,@M_BALANCE,@M_TDT,@M_TXNNO,@txnno1
END
close fillbal_cur
DEALLOCATE FILLBAL_CUR

   UPDATE GLPRIMLD SET LTXNDT=isnull(@LTXNDT,getdate())  WHERE  FY = @M_FY AND CGP_ACTNO = @M_ACTNUM
*/

if @m_putbal = 1
   UPDATE GLPRIMLD SET BALANCE=@OPGBAL
   +credit1+credit2+credit3+credit4+credit5+credit6+credit7+credit8+credit9+credit10+credit11+credit12
   +debit1+debit2+debit3+debit4+debit5+debit6+debit7+debit8+debit9+debit10+debit11+debit12
     WHERE   FY = @M_FY AND CGP_ACTNO = @M_ACTNUM

commit transaction


return

e_error:
rollback transaction 
raiserror('error',16,-1)
return


GO
/****** Object:  StoredProcedure [dbo].[fillbal_all]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[fillbal_all]  @fy numeric
/* with encryption */
 AS
declare @opgbal money
declare @actnum numeric

declare c1 cursor for select cgp_actno from glprimld where fy=@fy
open c1
fetch next from c1 into @actnum
while @@fetch_status=0
begin
print @actnum
begin
select @opgbal=opgbal from glprimld where cgp_actno=@actnum and fy=@fy
select * into #dummy11 from vouchers where fy=@fy and actnum=@actnum order by datediff(d,'1/1/2000',txndt), txnno2 
update #dummy11 set  @opgbal=case when amttyp='D' then @opgbal-amt when amttyp='C' then @opgbal+amt end ,balance=@opgbal
update vouchers set balance=(select balance from #dummy11 where #dummy11.txnno2=vouchers.txnno2) where fy=@fy and actnum=@actnum
update glprimld set balance=@opgbal where fy=@fy and cgp_actno=@actnum
if exists(select name,xtype from tempdb.dbo.sysobjects where name like '#dummy11%'  and xtype = 'U') drop table #dummy11
end

fetch next from c1 into @actnum
end
close c1
deallocate c1



GO
/****** Object:  StoredProcedure [dbo].[FILLBALold]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.FILLBAL    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.FILLBAL    Script Date: 1/20/03 11:51:48 AM ******/
CREATE PROCEDURE [dbo].[FILLBALold] @M_TXNDT DATETIME,@M_COMPANY INT, @M_ACTNUM INT , @m_putbal int = 0 
/* with encryption */
AS
DECLARE @M_FY INT
DECLARE @M_OPGBAL MONEY
DECLARE @M_AMTTYP CHAR(1)
DECLARE @M_AMT MONEY 
DECLARE @M_BALANCE MONEY
DECLARE @M_TDT DATETIME
DECLARE @M_TXNNO INT
DECLARE @M_NEXTYEARAC INT
DECLARE @M_NEXTYEARFY INT
DECLARE @M_GROUP INT
DECLARE @M_FSDATE DATETIME
DECLARE @M_YEAR INT
DECLARE @M_OB MONEY
DECLARE @M_BOFY DATETIME
declare @m_ob1 varchar(20)
declare @ltxndt datetime
DECLARE @OPGBALCON MONEY
declare @txnno1 numeric
set nocount on
set @m_txndt = datename(dd,@m_txndt) + ' ' + datename(mm,@m_txndt) + ' ' + datename(yy,@m_txndt)
IF @M_COMPANY IS NULL  OR @M_ACTNUM IS NULL
BEGIN
RAISERROR('EMPTY COMPANY CODE',16,-1)
RETURN
END
EXEC GETFY @M_TXNDT,@M_COMPANY,@M_FY OUT
SET @M_OPGBAL = 0
EXEC @M_OPGBAL = GETOPGBAL @M_COMPANY,@M_TXNDT,@M_ACTNUM , @M_OB1 out 
set @m_opgbal = convert(money,@m_ob1)
SET @OPGBALCON=@M_OPGBAL
DECLARE FILLBAL_CUR CURSOR FOR SELECT AMTTYP,AMT,BALANCE,TXNDT,TXNNO2,txnno FROM VOUCHERS WHERE 
FY=@M_FY AND ACTNUM=@M_ACTNUM AND datediff(dd,TXNDT,@M_TXNDT) <=  0 ORDER BY datediff(dd,'1/1/80',txndt),TXNNO2
OPEN FILLBAL_CUR
FETCH NEXT FROM FILLBAL_CUR INTO @M_AMTTYP,@M_AMT,@M_BALANCE,@M_TDT,@M_TXNNO,@txnno1
WHILE  @@FETCH_STATUS=0 BEGIN
IF @M_AMTTYP='D'
SET @M_OPGBAL = @M_OPGBAL - @M_AMT
ELSE
SET @M_OPGBAL = @M_OPGBAL+ @M_AMT
UPDATE VOUCHERS SET BALANCE= @M_OPGBAL WHERE txnno=@txnno1
set @ltxndt = @m_tdt
update comuser set chktime=getdate() where [host_id]=host_id()
FETCH NEXT FROM FILLBAL_CUR INTO @M_AMTTYP,@M_AMT,@M_BALANCE,@M_TDT,@M_TXNNO,@txnno1
END
close fillbal_cur
DEALLOCATE FILLBAL_CUR
   UPDATE GLPRIMLD SET LTXNDT=isnull(@LTXNDT,getdate())  WHERE  FY = @M_FY AND CGP_ACTNO = @M_ACTNUM
if @m_putbal = 1
   UPDATE GLPRIMLD SET BALANCE=@M_OPGBAL  WHERE   FY = @M_FY AND CGP_ACTNO = @M_ACTNUM
SET @m_opgbal=@opgbalcon




GO
/****** Object:  StoredProcedure [dbo].[fillfg]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[fillfg] @fn varchar(50),@dt datetime 
/* with encryption */
AS
declare @x int
declare @sql varchar(8000)
declare @gno numeric
declare @amt money
set nocount on
set @sql = 'update ' + @fn + ' set c1=0,c2=0,c3=0,c4=0,c5=0,c6=0,c7=0,c8=0,c9=-0,c10=0'
exec(@sql) 
if @@error<>0 goto e_error
set @sql = 'declare c1 cursor for select gno  from ' + @fn
exec (@sql)
if @@error<>0 goto e_error
open c1
fetch next from c1 into @gno
while @@fetch_status=0
begin
	set @x = 1
	while @x <= 10
	begin
		select @amt=sum(finalamt) from kithist1 where outlet in 
		(select code from outlet where channel=(select channel from restco where billposition1=@x))
		and gno=@gno and datediff(d,txndt,@dt)=0
		set @sql = 'update ' + @fn +  ' set c' + ltrim(str(@x)) + '=c' + ltrim(str(@x))   +  ' +' + convert(varchar(20),@amt) + '   where gno=' + str(@gno)
		exec(@sql)
if @@error<>0 goto e_error
		set @x = @x + 1
	end

	set @x = 1
	while @x <= 10
	begin
		select @amt=sum(amount) from phhist
		where  
		cno in (select cno from phone where gno=@gno and datediff(d,txndt,@dt)=0 and
		outlet in (select outlet from phoutlet where billposition1=@x))
		set @sql = 'update ' + @fn +  ' set c' + ltrim(str(@x)) + '=c' + ltrim(str(@x))   +  ' +' + convert(varchar(20),@amt) + '   where gno=' + str(@gno)
		exec(@sql)
if @@error<>0 goto e_error
		set @x = @x + 1
	end
/*
	declare @mop varchar(50)
	select @mop=
	case 
	when mop=1 then 'Cash'
	when mop=2 then 'Credit Card'
	when mop=3 then 'DD/Cheque'
	when mop=4 then 'GL Account'
	end
	from advance where gno=@gno and cno=(select max(cno) from advance where gno=@gno)
	set @sql = ' update ' + @fn + ' set settlement=''' + @mop + ''' where gno=' + str(@gno)
*/
set @sql = ' update ' + @fn + '   set settlement = 
case when 
(select grpname from room_guest where gno=' + @fn + '.gno)= '''' then
(select top 1 mop from advance where gno=' + @fn + '.gno order by ctrlno)
else
(select top 1 mop from advance where gno in 
(select gno from room_guest where grpname=(select grpname from room_guest where gno=' + @fn + '.gno)) 
order by ctrlno)
end
where gno in (select gno from room_guest)'
	exec(@sql)
	if @@error<>0 goto e_error
	fetch next from c1 into @gno
end
close c1
deallocate c1
set @sql = 'update ' + @fn + ' set settlement=case when ltrim(rtrim(settlement))=''1'' then ''Cash'' when settlement=''2'' then ''Credit Card'' when settlement=''3'' then ''DD/Cheque'' when settlement=''4'' then ''GL Account'' else settlement end'
exec(@sql)
if @@error<>0 goto e_error
---set @sql = ' update ' + @fn + ' set settlement=case when exists (select * from crcardhold where crcardhold.gno=' + @fn + '.gno) then ''Credit Card Hold'' else settlement end'

set @sql = ' update ' + @fn + '   set settlement = 
case when (select grpname from room_guest where gno=' + @fn +'.gno)= '''' then
case when exists (select * from crcardhold where gno=' + @fn + '.gno) then ''Cr.Card Hold'' else '''' end 
else
case when exists (select * from crcardhold where gno in
(select gno from room_guest where grpname=(select grpname from room_guest where gno=' + @fn + '.gno)))
then ''Grp.Card Hold'' else '''' end 
end
where gno in (select gno from room_guest) and settlement is null '
--print @sql goto e_error
exec(@sql)
if @@error<>0 goto e_error
set @sql = 'update ' + @fn + ' set [Final Amount]=(rent+[Lux.Tax] +c1+c2+c3+c4+c5+c6+c7+c8+c9+c10)-discount ' --where gno=' + str(@gno)
exec(@sql)
if @@error<>0 goto e_error

set @sql = 'update ' + @fn + ' set settlement=case when exists(select * from guest where co_date is not null and gno=' + @fn + '.gno) then (select dbo.getmopdetails( (select cno from billhist1 where brno=(select brno from guest where gno=' + @fn + '.gno ) and company=1 and split=''N''))) Else settlement End'
exec(@sql)
set @sql = 'update ' + @fn + ' set settlement=''No advance'' where settlement='''' or settlement is null'
exec(@sql)
set nocount off
return
e_error:
set nocount off

raiserror('Error',16,1)




GO
/****** Object:  StoredProcedure [dbo].[fillfyforlinker]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
CREATE PROCEDURE [dbo].[fillfyforlinker] @dt datetime 
/* with encryption */
AS
declare @fy numeric
declare @company int
delete from linker where fy is null
declare tc cursor for select company from company
declare @bofy datetime
open tc
fetch next from tc into @company
while @@fetch_status=0 begin
set @fy=0

if exists (select * from finyear where company=@company and datediff(d,[date],dbo.getbofy(@dt)) = 0)
begin
exec @fy=getfy @dt,@company, @fy out
if @@error<>0 set @fy=0
end
update linker set fy=@fy where company=@company
fetch next from tc into @company
end
close tc
deallocate tc





GO
/****** Object:  StoredProcedure [dbo].[fillstock]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.fillstock    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[fillstock] @ctrlno numeric ,@txndt datetime='23 Nov 1955' , @putbal int=0 
/* with encryption */
AS
declare @hctrlno numeric
declare @opgbal float
declare @amttyp char(1)
declare @qty numeric
declare @txndt1 datetime 
begin transaction
select @opgbal=0
set @opgbal=0

update storehist
set @opgbal=case when amttyp='D' then @opgbal-qty else @opgbal+qty end
where hctrlno in (select top 1000000 hctrlno from storehist where ctrlno=@ctrlno order by txndt,hctrlno)
if @putbal = 1 UPDATE storeitems SET stock=@opgbal,ltxndt=getdate()  WHERE ctrlno=@ctrlno
commit transaction
return
e_error:
rollback transaction
raiserror('Error in stock updation-stage2',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[fillstock1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.fillstock    Script Date: 15/02/2004 6:23:00 PM ******/
/**** created on 02 Aug 2008 *****/
CREATE PROCEDURE [dbo].[fillstock1] @ctrlno numeric , @txndt datetime
/* with encryption */
AS
declare @hctrlno numeric
declare @opgbal float
declare @amttyp char(1)
declare @qty numeric
declare @txndt1 datetime 
begin transaction
set @opgbal=0
set @opgbal=0

select top 1 @txndt1=txndt,@opgbal=balance from storehist where ctrlno=@ctrlno and txndt < @txndt  order by txndt desc,hctrlno2 desc
set @opgbal = isnull(@opgbal,0)
declare  cstk cursor for select hctrlno,qty,amttyp from storehist where ctrlno=@ctrlno and txndt > @txndt1 order by txndt ,amttyp, hctrlno2
open cstk
fetch next from cstk into @hctrlno,@qty,@amttyp
while @@fetch_status=0
	begin
	if @amttyp='D' set @opgbal=@opgbal - @qty else set @opgbal=@opgbal+@qty
	update storehist set balance=@opgbal where hctrlno=@hctrlno
	fetch next from cstk into @hctrlno,@qty,@amttyp
	end
close cstk
deallocate cstk
commit transaction
return
e_error:
rollback transaction
raiserror('Error in stock updation-stage2',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[findclash]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[findclash]
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

declare @dt datetime
declare @g_12noonstr varchar(20)
select top 1 @g_12noonstr=g_12noonstr from setting
create table #dummy (cnt int , dt datetime ,rno int)

declare @x int
set @x = 0
declare c1 cursor for 
select distinct(convert(varchar(20),arrivaldate,6)) dt from  sptbrs where datediff(d,'1/1/2000',arrivaldate) 
between datediff(d,'1/1/2000',dateadd(d,1,getdate()))  and datediff(d,'1/1/2000',dateadd(d,180,getdate()))  order by dt
open c1
fetch next from c1 into @dt
while @@fetch_status=0
	begin
set @x = @x +1
	print str(@x) + convert(varchar(20),@dt)
	set @dt = convert(varchar(20),@dt,6) + ' ' +  @g_12noonstr
	insert into #dummy (rno,cnt,dt) 
	select rno,cnt,@dt from (
	select rno,count(*) cnt from sptbrooms_v 
	where datediff(s,'1/1/2000',@dt) between 
	datediff(s,'1/1/2000',arrivaldate) and
	datediff(s,'1/1/2000',departuredate) group by rno
	) a where cnt > 1 
	fetch next from c1 into @dt
end
close c1
deallocate c1
select * from #dummy
END

GO
/****** Object:  StoredProcedure [dbo].[FindRent]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[FindRent] @rtype numeric,@rno varchar(20), @heads numeric, @dt datetime,@inet int=0,@rent decimal out
/* with encryption */
AS
declare @rent1 decimal
declare @rent2 decimal
declare @renti1 decimal
declare @renti2 decimal
declare @rno1 int
select @rno1=rno from room where rnoa=@rno
select @rent1=rent1,@rent2=rent2,@renti1=renti1,@renti2=renti2 from rentmaster where rno=@rno1 and
rtypectrlno= (select ctrlno from rentperiod where rtype=@rtype and datediff(d,'1/1/2000',@dt)  between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt))


if @inet=0
	begin
	if @heads=1 
	set @rent=@rent1
	else
	set @rent=@rent2
	end
else
	begin
	if @heads=1 
	set @rent=@renti1
	else
	set @rent=@renti2
	end




GO
/****** Object:  StoredProcedure [dbo].[getbno]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 09 Jan 2013
-- Description:	To Generate Bill numbers
-- =============================================
CREATE PROCEDURE [dbo].[getbno] 
@txndt datetime,@bno numeric out
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
declare @fy numeric
set @fy= dbo.findfy(1,@txndt)
update finyear set bno=isnull(bno,0)+1 where company=1 and fy=@fy
select @bno=bno from finyear where company=1 and fy=@fy
END




GO
/****** Object:  StoredProcedure [dbo].[GetDetails1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE  [dbo].[GetDetails1] @grpname varchar(50) , @brno numeric = 0 
/* with encryption */
AS
--print 'Periya'
declare @company int
declare @gno numeric
declare @rno int
declare @split char(1)
declare @sql varchar(8000)
declare @stype int
declare @outlet int
declare @x int
declare @s1 int
declare @amt money
select * into #dummyg from dummygdetails
declare co1 cursor for select company from company order by company
open co1
fetch next from co1 into @company
while @@fetch_status=0
begin
	if @brno=0
	declare c1 cursor for select gno,rno from guest where grpname=@grpname
	else
	declare c1 cursor for select gno,rno from guest where brno=@brno
	open c1
	fetch next from c1 into @gno,@rno
	while @@fetch_status=0
	begin
   		set @split='N'
		insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
		declare s1 cursor for select servicetype,outlet,position from billposition where company = @company and outlet <> 0  order by position
		open s1
		fetch next from s1 into @stype,@outlet,@x
		while @@fetch_status=0
		begin
			if @stype=1
			begin
			if exists(	select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
				from kithist1 where gno=@gno and outlet=@outlet   and split=@split
				)
				 begin
					select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
					from kithist1 where gno=@gno and outlet=@outlet   and split=@split) a
					set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
					exec(@sql)
				end
			end
	
			if @stype=2
			begin
			if exists (select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@outlet
				) begin
					select @amt=sum(amt) from ( select  (select sum(amount) from phhist where cno=phone.cno) - damt amt from phone where gno=@gno and split=@split and outlet=@outlet) a
					set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
					exec(@sql)
				end
			end
	
			if @stype=3
			begin
			if exists (select sum(finalamt) from chalan  where gno=gno and split=@split and outlet=@outlet
				) begin
					select sum(finalamt) from chalan  where gno=gno and split=@split and outlet=@outlet
					set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
					exec(@sql)
				end
			end
			fetch next from s1 into @stype,@outlet,@x
		end
		close s1
		deallocate s1
		if @company=1
		begin
		 	set @split='Y'
    		insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
			declare s1 cursor for select servicetype,outlet,position from billposition order by position
			open s1
			fetch next  from s1 into @stype,@outlet,@x
			while @@fetch_status=0
			begin
				if @stype=1
				begin
				if exists(	select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
					from kithist1 where gno=@gno and outlet=@outlet   and split=@split
					)
					 begin
						select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
						from kithist1 where gno=@gno and outlet=@outlet   and split=@split) a
						set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
						exec(@sql)
					end
				end
		
				if @stype=2
				begin
				if exists (select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@outlet
					) begin
						select @amt=sum(amt) from ( select  (select sum(amount) from phhist where cno=phone.cno) - damt amt from phone where gno=@gno and split=@split and outlet=@outlet) a
						set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
						exec(@sql)
					end
				end
				if @stype=3
				begin
				if exists (select sum(finalamt) from chalan  where gno=gno and split=@split and outlet=@outlet
					) begin
						select sum(finalamt) from chalan  where gno=gno and split=@split and outlet=@outlet
						set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
						exec(@sql)
					end
				end
				fetch next from s1 into @stype,@outlet,@x
			end
			close s1
			deallocate s1
		end
   		set @split='C'
		insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
		declare s1 cursor for select servicetype,outlet,position from billposition order by position
		open s1
		fetch next from s1 into @stype,@outlet,@x
		while @@fetch_status=0
		begin
			if @stype=1
			begin
			if exists(	select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
				from kithist1 where gno=@gno and outlet=@outlet   and split=@split
				)
				 begin
					select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
					from kithist1 where gno=@gno and outlet=@outlet   and split=@split) a
					set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
					exec(@sql)
				end
			end
	
			if @stype=2
			begin
			if exists (select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@outlet
				) begin
					select @amt=sum(amt) from ( select  (select sum(amount) from phhist where cno=phone.cno) - damt amt from phone where gno=@gno and split=@split and outlet=@outlet) a
					set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
					exec(@sql)
				end
			end
			if @stype=3
			begin
			if exists (select sum(finalamt) from chalan  where gno=gno and split=@split and outlet=@outlet
				) begin
					select sum(finalamt) from chalan  where gno=gno and split=@split and outlet=@outlet
					set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
					exec(@sql)
				end
			end
			fetch next from s1 into @stype,@outlet,@x
		end
		close s1
		deallocate s1
   		set @split='F'
		insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
		declare s1 cursor for select servicetype,outlet,position from billposition order by position
		open s1
		fetch next from s1 into @stype,@outlet,@x
		while @@fetch_status=0
		begin
			if @stype=1
			begin
			if exists(	select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
				from kithist1 where gno=@gno and outlet=@outlet   and split=@split
				)
				 begin
					select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
					from kithist1 where gno=@gno and outlet=@outlet   and split=@split) a
					set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
					exec(@sql)
				end
			end
	
			if @stype=2
			begin
			if exists (select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@outlet
				) begin
					select @amt=sum(amt) from ( select  (select sum(amount) from phhist where cno=phone.cno) - damt amt from phone where gno=@gno and split=@split and outlet=@outlet) a
					set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
					exec(@sql)
				end
			end
			if @stype=3
			begin
			if exists (select sum(finalamt) from chalan  where gno=gno and split=@split and outlet=@outlet
				) begin
					select sum(finalamt) from chalan  where gno=gno and split=@split and outlet=@outlet
					set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
					exec(@sql)
				end
			end
			fetch next  from s1 into @stype,@outlet,@x
		end
		close s1
		deallocate s1
	
		fetch next from c1 into @gno,@rno
	end
	close c1
	deallocate c1
	fetch next from co1 into @company
end
close co1
deallocate co1
--select * from #dummyg
select  Company= (select rtrim(name) from company where company=#dummyg.company ) + char(13) + case 
when split='N' then 'Main Bill'
when split='Y' then 'Supp Bill'
when split='F' then 'Free Bill - not booked'
when split='C' then 'Comp Bill - booked'
end,
[Guest Name & R.No]='R.No.' + ltrim(str(rno)) + '- '+(select rtrim(name) from guestad where cno=(select cno from guest where gno=#dummyg.gno)),item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,total=(item1+item2+item3+item4+item5+item6+item7+item8+item9+item10)
from #dummyg
where item1+item2+item3+item4+item5+item6+item7+item8+item9+item10 <> 0
order by rno
return
e_error:
raiserror ('Error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[Getdobphones]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		RM.Ramanathan 
-- Create date: 04 Sep 2015
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[Getdobphones]  @cnt int
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @sql varchar(6000)
	declare @ptype int
	declare @name varchar(200)
	declare @phone varchar(200)
	declare @email varchar(200)
	declare @dt datetime
	declare @j int
	declare @id numeric
	declare @address varchar(10)
	declare @dobsmssent datetime	

	create table #dumsms (
	ctrlno int  NOT NULL,
	ptype int,
	name varchar(200),
	phone varchar(200),
	email varchar(200),
	dt datetime,
	id numeric,
	addr varchar(10),
	dobsmssent datetime
	)

    if @cnt=1
    set @sql = 'declare c1 cursor for select  id, ptype,name,phone,dt,email,address,dobsmssent from (
		        select gno id,ptype=1,name,phone,ci_date dt,email,address,dobsmssent from guest    where dobm=month(getdate()) and dobd=day(getdate()) and phone<>'''' and datediff(d,dobsmssent,getdate())<>0 union 
                select ctrlno,2,name,phone,bookingdate     ,email,address,dobsmssent from sptbrs   where dobm=month(getdate()) and dobd=day(getdate()) and phone<>'''' and datediff(d,dobsmssent,getdate())<>0 union 
                select ctrlno,3,name,phone,ltxndt          ,email='''','''',dobsmssent from doorcust where dobm=month(getdate()) and dobd=day(getdate()) and phone<>'''' and datediff(d,dobsmssent,getdate())<>0 
				) a where datediff(d,dt,getdate()) <= 1095'
    if @cnt=2
    set @sql = 'declare c1 cursor for select  id,ptype,name,phone,dt,email,address,dobsmssent from (
		        select gno id,ptype=1,name,phone,ci_date dt,email,address,dobsmssent from guest    where domm=month(getdate()) and domd=day(getdate()) and phone<>'''' and datediff(d,domsmssent,getdate())<>0 union 
                select ctrlno,2,name,phone,bookingdate     ,email,address,dobsmssent from sptbrs   where domm=month(getdate()) and domd=day(getdate()) and phone<>'''' and datediff(d,domsmssent,getdate())<>0 union 
                select ctrlno,3,name,phone,ltxndt          ,email='''','''',dobsmssent from doorcust where domm=month(getdate()) and domd=day(getdate()) and phone<>'''' and datediff(d,domsmssent,getdate())<>0 
				) a where datediff(d,dt,getdate()) <= 1095'
	if @cnt=3
    set @sql = 'declare c1 cursor for select  id,ptype,name,phone,dt,email,address,dobsmssent from (
		        select gno id,ptype=1,name,phone,ci_date dt,email,address,dobsmssent from guest    where dobm=month(getdate()) and dobd=day(getdate()) and email<>'''''' and datediff(d,dobemailsent,getdate())<>0 union 
                select ctrlno,2,name,phone,bookingdate     ,email,address,dobsmssent from sptbrs   where dobm=month(getdate()) and dobd=day(getdate()) and email<>'''''' and datediff(d,dobemailsent,getdate())<>0
				) a where email <>'''' and datediff(d,dt,getdate()) <= 1095'
    if @cnt=4
    set @sql = 'declare c1 cursor for select  id,ptype,name,phone,dt,email,address,dobsmssent from (
		        select gno id,ptype=1,name,phone,ci_date dt,email,address,dobsmssent from guest    where domm=month(getdate()) and domd=day(getdate()) and email<>'''''' and datediff(d,domemailsent,getdate())<>0 union 
                select ctrlno,2,name,phone,bookingdate     ,email,address,dobsmssent from sptbrs   where domm=month(getdate()) and domd=day(getdate()) and email<>'''''' and datediff(d,domemailsent,getdate())<>0 
				) a where email <> '''' and  datediff(d,dt,getdate()) <= 1095'


set @j = 0
exec(@sql)
open c1
fetch next from c1 into @id,@ptype,@name,@phone,@dt,@email,@address ,@dobsmssent
while @@fetch_status=0
begin
    set @j=@j+1
	print @j
	if @cnt=1 or @cnt=3
	if not exists(select * from #dumsms where phone=@phone)
	insert into #dumsms (ctrlno,id,ptype,name,phone,dt,email,addr,dobsmssent) values (@j,@id,@ptype,@name,@phone,@dt,@email,@address,@dobsmssent)

	if @cnt=2 or @cnt=4
	if not exists(select * from #dumsms where email=@email)
	insert into #dumsms (ctrlno,id,ptype,name,phone,dt,email,addr,dobsmssent) values (@j,@id,@ptype,@name,@phone,@dt,@email,@address,@dobsmssent)
	fetch next from c1 into @id,@ptype,@name,@phone,@dt,@email,@address,@dobsmssent
end
close c1
deallocate c1
set nocount off
select distinct phone,id, ptype,name,dt,email,addr,dobsmssent from #dumsms
END

GO
/****** Object:  StoredProcedure [dbo].[GETEDITBALANCE]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GETEDITBALANCE] @CTRLNO AS NUMERIC, @QTY AS NUMERIC , @SDT  AS DATETIME , @CNT INT OUT
/* with encryption */
AS
BEGIN
DECLARE @DT DATETIME
DECLARE @MFY INT
DECLARE @COMPANY INT
declare @luperhu numeric
	SET NOCOUNT ON;
select @luperhu=luperhu from storeitems where ctrlno=@ctrlno
SELECT @COMPANY=COMPANY FROM STORE WHERE STORENO=(SELECT STORENO FROM STOREITEMS WHERE  CTRLNO=@CTRLNO)
SELECT @DT = @SDT
SELECT @CNT=0

declare @bal numeric
WHILE DATEDIFF(d,@DT,getdate()) >=0 
	BEGIN 
	EXEC GETFY @DT,@COMPANY,@MFY OUT 
	SELECT @bal=DBO.GETSTOCKFAST (@CTRLNO,@DT,@MFY) 
	select @bal = @bal/@luperhu 
	IF @bal- @QTY < 0 BEGIN SELECT @CNT=1 RETURN END
	SELECT @DT = DATEADD(D,1,@DT)
	END
END

GO
/****** Object:  StoredProcedure [dbo].[GETEDITBALANCE1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GETEDITBALANCE1] @CTRLNO AS NUMERIC,@OQTY as numeric, @QTY AS NUMERIC , @SDT  AS DATETIME , @CNT INT OUT,@OPT as int=0
/* with encryption */
AS
BEGIN
DECLARE @DT DATETIME
DECLARE @MFY INT
DECLARE @COMPANY INT
declare @luperhu numeric
	SET NOCOUNT ON;
select @luperhu=luperhu from storeitems where ctrlno=@ctrlno
SELECT @COMPANY=COMPANY FROM STORE WHERE STORENO=(SELECT STORENO FROM STOREITEMS WHERE  CTRLNO=@CTRLNO)
SELECT @DT = @SDT
SELECT @CNT=0

declare @bal numeric
WHILE DATEDIFF(d,@DT,getdate()) >=0 
	BEGIN 
	EXEC GETFY @DT,@COMPANY,@MFY OUT 
	SELECT @bal=DBO.GETSTOCKFAST (@CTRLNO,@DT,@MFY) 
	select @bal = @bal/@luperhu 
	if @opt=0
	    begin
		IF (@bal+@oqty) - @QTY < 0 BEGIN SELECT @CNT=1 RETURN END
		end
	else
	    begin
		IF (@bal-@oqty) + @QTY < 0 BEGIN SELECT @CNT=1 RETURN END
		end

	SELECT @DT = DATEADD(D,1,@DT)
	END
END


GO
/****** Object:  StoredProcedure [dbo].[GETFY]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.GETFY    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.CHARTTOGL    Script Date: 1/20/03 11:51:46 AM ******/
CREATE PROCEDURE [dbo].[GETFY] @M_TXNDT DATETIME, @M_COMPANY INT, @M_FY INT OUTPUT ,@days int = 10
/* with encryption */
AS
--SET XACT_ABORT ON
BEGIN TRAN
DECLARE @M_COUNT numeric
DECLARE @M_YEAR INT
DECLARE @M_TDATE DATETIME
DECLARE @M_DESC CHAR(30)
declare @finstart tinyint
declare @bofy datetime
declare @abofy datetime

set nocount on
select @finstart=g_finstart from setting
set @bofy = dbo.getbofy(@m_txndt)
set @m_year = DATEPART(yy,@bofy)

if @finstart=0 
begin
	--IF DATEPART(mm,@M_TXNDT) >=  1 AND DATEPART(mm,@M_TXNDT) <= 3
	--	 SET @M_YEAR = DATEPART(yy,@M_TXNDT)-1
	--ELSE
	--	 SET @M_YEAR = DATEPART(yy,@M_TXNDT)
	SET @M_DESC = 'FIN.YEAR ' + LTRIM(STR(@M_YEAR)) + '-' + LTRIM(STR(@M_YEAR+1))
	--set @finmm = 'April 1,'
end
else
	begin
		SET @M_YEAR = DATEPART(yy,@M_TXNDT)
		SET @M_DESC = 'FIN.YEAR ' + LTRIM(STR(@M_YEAR)) 
		--set @finmm = 'January 1,'
	end


/*
old version (before 25 march 2009)
SELECT @M_FY=FY FROM FINYEAR WHERE  DATEPART(yy,[DATE])=@M_YEAR AND COMPANY=@M_COMPANY
SELECT @M_COUNT= @@ROWCOUNT
*/
if exists(select * from finyear where datepart(yy,[date])=@m_year and company=@m_company)
begin
select @m_fy=fy from finyear where datepart(yy,[date])=@m_year and company=@m_company
end
else
BEGIN
	SELECT @M_TDATE=MAX([DATE]) FROM FINYEAR WHERE COMPANY=@M_COMPANY
	SELECT @M_FY=MAX(FY) FROM FINYEAR
	SET @M_FY=  ISNULL(@M_FY,0)
	SET @M_FY = @M_FY + 1
	IF @M_TDATE is NULL
	BEGIN
		if @m_year > datepart(yy,getdate()) + 1  goto eerror 
		INSERT INTO FINYEAR (COMPANY,[DATE],DESCRIPTION,FY) VALUES (@M_COMPANY, @bofy,@M_DESC,@M_FY)
		SELECT @M_FY=FY FROM FINYEAR WHERE  DATEPART(yy,[DATE])=@M_YEAR AND COMPANY=@M_COMPANY
		IF @@ERROR <> 0 GOTO EERROR
			EXEC CHARTTOGL @M_COMPANY,@M_FY
		IF @@ERROR <> 0 GOTO EERROR
			exec storenewyear @m_fy
		if @@error<>0 goto eerror
	END
	ELSE
	BEGIN
		IF @M_YEAR <  DATEPART(yy,@M_TDATE)
		BEGIN
			RAISERROR('Financial year less than the exisiting Fin.account not allowed',15,1)
			SET  @M_FY=0
		END
		ELSE
		BEGIN
		if @m_year > datepart(yy,getdate()) +1  goto eerror 
		if datediff(dd,dateadd(yy,1,@m_tdate),@bofy) <> 0  begin print 'You can not open financial year for future years' goto eerror end
		if datediff(d,getdate(),@bofy) > @days begin print 'New Financial Year Opening is allowed only ' + ltrim(str(@days)) + ' days before the starting of that financial year' + char(13)+ 'Financial Year Openng Failed' goto eerror end
		INSERT INTO FINYEAR (COMPANY,[DATE],DESCRIPTION,FY) VALUES (@M_COMPANY,@bofy,@M_DESC,@M_FY)
		SELECT @M_FY=FY FROM FINYEAR WHERE  DATEPART(yy,[DATE])=@M_YEAR AND COMPANY=@M_COMPANY
		IF @@ERROR <> 0 GOTO EERROR
		EXEC CHARTTOGL @M_COMPANY,@M_FY
		IF @@ERROR <> 0 GOTO EERROR
		declare @g_Storeminus int
		select @g_storeminus=g_storeminus from setting 
		update setting set g_storeminus=1
		exec storenewyear @m_fy
		update setting set g_storeminus=@g_storeminus
		if @@error<>0 goto eerror
		END
	END
END

COMMIT TRAN
RETURN @M_FY
EERROR:
SELECT @M_FY=0
ROLLBACK TRAN
RAISERROR('NEW FIN.YEAR OPENING FAILED',16,-1)
RETURN 0



GO
/****** Object:  StoredProcedure [dbo].[GETGRP]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[GETGRP]
    @M_TXNDT   DATETIME,
    @M_COMPANY INT,
    @RESULT    INT OUTPUT,
    @otype     INT = 0
AS
BEGIN
    SET NOCOUNT ON;
    SET XACT_ABORT ON;

    DECLARE @StartedTran BIT;
    DECLARE @grno INT;

    SET @StartedTran = 0;

    IF @@TRANCOUNT = 0
    BEGIN
        BEGIN TRAN;
        SET @StartedTran = 1;
    END

    BEGIN TRY

        -- Lock row or key range
        IF EXISTS (
            SELECT 1
            FROM txnno2 WITH (UPDLOCK, HOLDLOCK)
            WHERE company = @M_COMPANY
              AND otype   = @otype
              AND txndt  >= CONVERT(DATETIME, CONVERT(DATE, @M_TXNDT))
              AND txndt  <  DATEADD(DAY, 1, CONVERT(DATE, @M_TXNDT))
        )
        BEGIN
            UPDATE txnno2
            SET grno = grno + 1
            WHERE company = @M_COMPANY
              AND otype   = @otype
              AND txndt  >= CONVERT(DATETIME, CONVERT(DATE, @M_TXNDT))
              AND txndt  <  DATEADD(DAY, 1, CONVERT(DATE, @M_TXNDT));

            SELECT @grno = grno
            FROM txnno2
            WHERE company = @M_COMPANY
              AND otype   = @otype
              AND txndt  >= CONVERT(DATETIME, CONVERT(DATE, @M_TXNDT))
              AND txndt  <  DATEADD(DAY, 1, CONVERT(DATE, @M_TXNDT));
        END
        ELSE
        BEGIN
            SET @grno = 1001;

            INSERT INTO txnno2 (txndt, grno, company, otype)
            VALUES (CONVERT(DATE, @M_TXNDT), @grno, @M_COMPANY, @otype);
        END

        SET @RESULT = @grno;

        IF @StartedTran = 1
            COMMIT TRAN;

    END TRY
    BEGIN CATCH

        IF XACT_STATE() <> 0 AND @StartedTran = 1
            ROLLBACK TRAN;

        DECLARE @ErrMsg NVARCHAR(4000), @ErrSeverity INT;
        SELECT @ErrMsg = ERROR_MESSAGE(),
               @ErrSeverity = ERROR_SEVERITY();

        RAISERROR (@ErrMsg, @ErrSeverity, 1);
    END CATCH
END
GO
/****** Object:  StoredProcedure [dbo].[getgrpadvr]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE  [dbo].[getgrpadvr]  @cno numeric,@ano varchar(5000) out
/* with encryption */
 AS
declare @ano1 numeric
declare @fs  int
declare @str varchar(10) 
declare @add1 int
declare @cnt1 int
declare @brno numeric
declare @company int
declare @split char(1)
set @ano=' Rec.No:'
select  @brno=brno,@company=company,@split=split  from billhist1 where cno=@cno
select ano into #dummy from (select ano,company,split  from advance  where gno in (select gno from billhist where cno=@cno) )  a where company=@company and split=@split
declare c1 cursor for select ano from #dummy
open c1
fetch next from c1 into  @ano1
--set @fs = @@fetch_status
set @add1=0
set @cnt1=0
while  @@fetch_status=0  -- @fs=0
begin
set @str =  ltrim(rtrim(str(@ano1)))
      set @ano= @ano + case when @add1=1 then ',' else '' end  
      if @cnt1=10 begin  set @ano=@ano+ ' ' set @cnt1=0 end
      set @ano=@ano+@str
      set @add1=1
      set @cnt1=@cnt1+1
fetch next from c1 into  @ano1
--set @fs =  @@fetch_status
end
close c1
deallocate c1




GO
/****** Object:  StoredProcedure [dbo].[getgrprooms]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE  [dbo].[getgrprooms]  @cno numeric,@eno varchar(5000) out,@rno varchar(5000) out,@rnoa varchar
(5000) = ''  out 
/* with encryption */
 AS
declare @eno1 numeric
declare @rno1 numeric

declare @fs  int
declare @str varchar(10) 
declare @add1 int
declare @add2 int
declare @cnt1 int
declare @cnt2 int
declare @brno numeric
declare @gno numeric
declare @grpname varchar(100)
set @eno=' '
set @rno=' '
set @rnoa = ''
select @brno=brno from billhist1 where cno=@cno

select top 1 @grpname=grpname from guest where brno=@brno
if @grpname='' begin 
select top 1 @eno1=eno from guest where brno=@brno
end

select  @brno=brno from billhist1 where cno=@cno
declare c1 cursor for select rno,eno,gno from guest where brno=@brno and opentoall<>'N' order by eno
open c1
fetch next from c1 into  @rno1,@eno1,@gno
set @fs = @@fetch_status
set @add1=0
set @add2=0
set @cnt1=0
set @cnt2=0
while  @fs=0
begin
	(select @str=ltrim(rtrim(rnoa)) from room where rno=@rno1)
	--if patindex('%' +@str + '%,',@rno) = 0 
	--	begin 
			--set @rno= @rno + ','   --case when @add1=1 then ',' else '' end  
			if @cnt1=22 begin  set @rno=@rno+ ' ' set @cnt1=0 end
			set @rno=@rno+@str + ','
			set @rnoa=@rnoa + '''' + ltrim(rtrim(@str)) + ''','
			set @add1=1
			set @cnt1=@cnt1+1
	--	end
declare c2 cursor for select rno from addroom where gno=@gno and rno not in (select rno from guest where brno=@brno)
open c2
fetch next  from c2 into @rno1
while @@fetch_status=0
begin
	 select @str=ltrim(rtrim(rnoa)) from room where rno=@rno1
--	if patindex('%' +@str + '%,',@rno) = 0 
--		begin 
			--set @rno= @rno + ','   --case when @add1=1 then ',' else '' end  
			if @cnt1=22 begin  set @rno=@rno+ ' ' set @cnt1=0 end
			set @rno=@rno+@str + ','
			set @rnoa=@rnoa + '''' + ltrim(rtrim(@str)) + ''','
            set @add1=1
			set @cnt1=@cnt1+1
--		end
fetch next  from c2 into @rno1
end
close c2
deallocate c2

	set @str =  ltrim(rtrim(str(@eno1)))
print @str + ' ' + @eno
	if patindex('%'+@str+'%',@eno)=0  
		begin  
			--set @eno = @eno + case when @add2=1 then ',' else '' end   
			--if @cnt2=22 begin  set @eno1=@eno1+ ' ' set @cnt2=0 end
			set @eno=@eno+@str+','
			set @add2=1 
			set @cnt2=@cnt2+1
		end
	fetch next from c1 into  @rno1,@eno1,@gno
	set @fs =  @@fetch_status
end
close c1
deallocate c1

if len(@rno) > 0 
begin
set @rno = substring(@rno,1,len(@rno)-1)
set @eno = substring(@eno,1,len(@eno)-1)
set @rnoa = substring(@rnoa,1,len(@rnoa)-1) 
end
else
begin
set @rno = 'NIL'
set @eno = 'NIL'
set @rnoa = 'NIL'
end
return
e_error:

raiserror('error',16,-1)
return


GO
/****** Object:  StoredProcedure [dbo].[getgrprooms1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO





CREATE PROCEDURE  [dbo].[getgrprooms1]  @grpname varchar(100),@eno varchar(5000) out,@rno varchar(5000) out,@rnoa varchar
(5000) = ''  out 
/* with encryption */
 AS
declare @eno1 numeric
declare @rno1 numeric

declare @fs  int
declare @str varchar(10) 
declare @add1 int
declare @add2 int
declare @cnt1 int
declare @cnt2 int
declare @brno numeric
declare @gno numeric
set @eno=' '
set @rno=' '
set @rnoa = ''
if @grpname=''
begin
set @rno=''
set @rnoa=''
set @eno = ''
return
end
declare c1 cursor for select rno,eno,gno from guest where grpname=@grpname and opentoall<>'N' order by eno
open c1
fetch next from c1 into  @rno1,@eno1,@gno
set @fs = @@fetch_status
set @add1=0
set @add2=0
set @cnt1=0
set @cnt2=0
while  @fs=0
begin
	(select @str=rtrim(rnoa) from room where rno=@rno1)
	--if patindex('%' +@str + '%,',@rno) = 0 
	--	begin 
			--set @rno= @rno + ','   --case when @add1=1 then ',' else '' end  
			if @cnt1=22 begin  set @rno=@rno+ ' ' set @cnt1=0 end
			set @rno=@rno+@str + ','
set @rnoa=@rnoa + '''' + @str + ''','
			set @add1=1
			set @cnt1=@cnt1+1
	--	end

declare c2 cursor for select rno from addroom where gno=@gno and rno not in (select rno from guest where brno=@brno)
open c2
fetch next  from c2 into @rno1
while @@fetch_status=0
begin
	 select @str=rnoa from room where rno=@rno1
--	if patindex('%' +@str + '%,',@rno) = 0 
--		begin 
			--set @rno= @rno + ','   --case when @add1=1 then ',' else '' end  
			if @cnt1=22 begin  set @rno=@rno+ ' ' set @cnt1=0 end
			set @rno=@rno+@str + ','
set @rnoa=@rnoa + '''' + @str + ''','
            set @add1=1
			set @cnt1=@cnt1+1
--		end
fetch next  from c2 into @rno1
end
close c2
deallocate c2


	set @str =  ltrim(rtrim(str(@eno1)))
	if patindex('%'+@str+'%',@eno)=0  
		begin  
			--set @eno = @eno + case when @add2=1 then ',' else '' end   
			if @cnt2=22 begin  set @eno=@eno+ ' ' set @cnt2=0 end
			set @eno=@eno+@str+','
			set @add2=1 
			set @cnt2=@cnt2+1
		end
	
	fetch next from c1 into  @rno1,@eno1,@gno
	set @fs =  @@fetch_status
end
close c1
deallocate c1
if len(@rno) > 0 
begin
set @rno = substring(@rno,1,len(@rno)-1)
set @eno = substring(@eno,1,len(@eno)-1)
set @rnoa = substring(@rnoa,1,len(@rnoa)-1)

end
else
begin
set @rno = 'NIL'
set @eno = 'NIL'
end





GO
/****** Object:  StoredProcedure [dbo].[GETGRPVOU]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) New Delhi*/
/* 28 July 2004*/
CREATE PROCEDURE [dbo].[GETGRPVOU] @M_TXNDT DATETIME, @M_COMPANY INT,@RESULT INT OUTPUT ,@otype int=1
/* with encryption */
AS
declare @grno numeric
declare @startno int
declare @endno int
declare @err varchar(500)
set @err='error'
set @grno = 0 
if @otype=1
	begin
	set @startno=1
	set @endno =250
	end
else
	begin
	set @startno=251
	set @endno =500
	end
begin transaction [v2]
set nocount on
if @grno <= 0 set @grno=@startno
if exists(select GRNO from txnno2 where company=@m_company and otype=@otype and datediff(d,txndt,@m_txndt)=0 )
begin
    select @grno=max(grno) from txnno2  where company=@m_company  and otype=@otype and datediff(d,txndt,@m_txndt)=0
    update txnno2 set grno=@grno+1,@otype=@otype where company=@m_company and otype=@otype and datediff(d,txndt,@m_txndt)=0 
    select @grno=max(grno) from txnno2 where company=@m_company  and otype=@otype and datediff(d,txndt,@m_txndt)=0 
end
else
    insert txnno2 values (@m_txndt,@grno,@m_company,@otype)
    set @result= @grno 

commit transaction   [v2]
return
e_error:
raiserror(@ERR,16,-1)
rollback transaction  [v2]
return




GO
/****** Object:  StoredProcedure [dbo].[getkotfy]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[getkotfy] @outlet int,@fy int out
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	declare @company int 
	declare @txndt datetime
	select @company=company from restco where channel=(select channel from outlet where code=@outlet)
    select @txndt = curdate from outlet where code=@outlet
	exec getfy @txndt,@company,@fy out
	SET NOCOUNT ON;
    -- Insert statements for procedure here
END

GO
/****** Object:  StoredProcedure [dbo].[GetNettRent]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[GetNettRent] 
--@NREST is nettrentexcludingSetrviceTax
@brent decimal(18,2) , @heads int,@rno varchar(20),@Ontariff tinyint=0,@dt datetime='1 jan 1900',@pscrate decimal(18,4)=0,@mrenttype int=1 ,@nettrent decimal(18,4) out,@m_adroom  int=1,@NREST int=0
/* with encryption */
AS
declare @prent decimal(18,4)
declare @mrent decimal(18,4)
declare @tariff1 decimal(18,4)
declare @tariff2 decimal(18,4)
declare @tariff decimal(18,4)
declare @strate decimal(18,4)
declare @p_ltontariff int
declare @p_STontariff varchar(5)
declare @g_scnettrent int
declare @lrate decimal(18,4)
declare @g_sc_ltrules int 
declare @trent decimal(18,4)
declare @errmsg varchar(5000)
declare @amt money	
declare @ltrateontariff as char(1)
if @dt='1 JAN 1900'  set @dt=getdate()
if @brent=0 
	begin
		set @nettrent = 0
		return
	end

set @m_adroom = case when @m_adroom <=0 then 1 else @m_adroom end
set @dt = convert(varchar(20),@dt,106)
select @p_ltontariff = case when ltontariff='Y' then 1 else 0 end from ltontariff where @dt between sdt and edt
select @p_stontariff=upper(ontariff) from sttable where @dt between sdt and edt
exec [FindRent] @mrenttype,@rno , @heads, @dt ,0,@tariff out

select @g_scnettrent=g_scnettrent ,@g_sc_ltrules=g_sc_ltrules from setting

select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
set @trent = @brent
set @brent=@brent/@m_adroom
if @p_stontariff='YES'
	set @strate = dbo.getstrate(@tariff,@dt)
else
	set @strate = dbo.getstrate(@brent,@dt)

if @NREST=1 SET @STRATE=0


--set @errmsg = convert(Varchar(20),@brent) goto e_Error 
--set @errmsg =  convert(Varchar(20),@strate) goto e_Error 
--set @errmsg =   convert(Varchar(20),@p_ltontariff) goto e_error
if @p_ltontariff=1   --YES
	begin
		if @brent <= @tariff
		begin
			if @g_scnettrent=0
				begin
				if @p_stontariff='YES'
					begin				
--					set @strate = dbo.getstrate(@tariff,@dt)
					set @mrent= @BRent - ((@lrate + @strate) / 100)
					end
				else  --p_stontariff='No'
					begin
						set @strate = dbo.GetStRate(@brent, @dt)
						set @mrent = @brent - ((@lrate) / 100 * @tariff)
						set @mrent = @mrent * 100 / (@strate + 100)
					end
				end
			else -- g_scnetrent=1
				begin
					set @MRent = @BRent - (((@lrate + @pSCRate) / 100) * @Tariff)
					If @P_STonTariff = 'YES' 
						begin
							If @G_SC_LTRULES=1 
								set @MRent = @BRent - ((@lrate + @pSCRate + @STRate) / 100 * @Tariff)
							Else
								begin
									set @MRent = @BRent - (@lrate / 100 * @Tariff)
									set @MRent = @MRent - ((@STRate / 100) * @Tariff)
									set @MRent = @MRent * (100 / (@pSCRate + 100))
								end
						end
					Else --' stontariff ='NO'
						begin
						If @G_SC_LTRULES=1
							begin
								set @MRent = @bRent - ((@lrate + @pSCRate) / 100 * @Tariff)
								if @p_stontariff='YES'
									set @strate = dbo.getstrate(@tariff,@dt)
								else
									set @strate = dbo.getstrate(@mrent,@dt)

								If @STRate > 0 set @MRent = @MRent * 100 / (@STRate + 100)
							end
						Else
							begin
								set @MRent = @BRent - (@lrate / 100 * @Tariff)
								If @pSCRate + @STRate > 0 set @MRent = @MRent * 100 / (@pSCRate + @STRate + 100)
							end
						end
				end
		end
		else -- rent > tariff
		begin
	        set @amt = case when (@brent * 100) > (floor(@brent) * 100) then floor(@brent)+1 else @brent end

			select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
			If @G_SCNettRent=0 
				If @P_STonTariff = 'YES'
					begin
						set @MRent = @BRent - (((@lrate + @STRate) / 100) * @BRent)
						If @MRent < @Tariff
							begin
								select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff  AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
								set @MRent = @BRent - (((@lrate + @STRate) / 100) * @Tariff)
							End
					end
				Else  --' if ston tariff = 'NO'
					begin
						set @MRent = @BRent * 100 / (@lrate + @STRate + 100)
						If @MRent < @Tariff
							begin
								select TOP 1 @lrate=lrate from lttable where EAMT >= @tariff  AND tno =  (select tno from ltmaster where @dt between sdt and edt)
								set @MRent = @BRent - (((@lrate) / 100) * @Tariff)
								set @MRent = @MRent * 100 / (@STRate + 100)
							end
					end
			else  --g_scnettrent=1
				begin
				If @P_STonTariff = 'YES'
					begin
						If @G_SC_LTRULES =1
							begin
								set @MRent = @BRent - (((@lrate + @STRate + @pSCRate) / 100) * @BRent)
								If @MRent < @Tariff

									begin
										select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff  AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
										set @MRent = @BRent - (((@lrate + @STRate + @pSCRate) / 100) * @Tariff)
									End
							end			
						Else
							begin
								set @MRent = @BRent * 100 / (@lrate + @STRate + 100)
								set @MRent = @MRent * 100 / (@pSCRate + 100)
								If @MRent < @Tariff
									begin 
										select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff  AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
										set @MRent = @BRent - (((@lrate + @STRate) / 100) * @Tariff)
										set @MRent = @MRent * 100 / (@pSCRate + 100)
									End 
							End 
					end
					Else  -- if ston tariff = 'NO'
					begin
						set @MRent = @BRent * 100 / (@lrate + @STRate + @pSCRate + 100)
						If @MRent < @Tariff 
							begin
								select TOP 1 @lrate=lrate from lttable where EAMT >= @Tariff AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
								set @MRent = @BRent - (((@lrate+@pscrate) / 100) * @Tariff)
								set @MRent = @MRent * 100 / (@STRate + 100)
							End 
					End 
				end
		end
	end
else -- ltontariff=no
	begin
	select @ltrateontariff=ltrateontariff from ltontariff where datediff(d,'1/1/2000',@dt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)

    set @amt = case when (@brent * 100) > (floor(@brent) * 100) then floor(@brent)+1 else @brent end

if @ltrateontariff='Y'
		select TOP 1 @lrate=lrate from lttable where case when @tariff > @amt then @tariff else @amt end  between samt and eamt AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
else
		select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)

--set @errmsg = @LTRATEONTARIFF + ' ' +  convert(varchar(20),@lrate) +  ' ' +  convert(varchar(20),@TARIFF) goto e_error

		If @G_SCNettRent=0
			begin
                If @P_STonTariff = 'YES'
					begin
						set  @MRent = @BRent - (@STRate * @Tariff / 100)
						set @MRent = 100 / (100 + @lrate) * @MRent
					end
				Else
					set @MRent = (100 / (100 + @lrate + @STRate)) * @BRent
			end
	            Else
			If @P_STonTariff= 'YES' 
				begin
					set @MRent = @Tariff
					If @STRate <> 0
						set @MRent = @BRent - (@Tariff * @STRate / 100)
					Else
						set @MRent = @BRent
					set @MRent = (100 / (100 + @lrate + @pSCRate)) * @MRent
				end
			else
				begin
--------start
					If @pSCRate <> 0 set @MRent = (100 / (100 + @pSCRate)) * @BRent   ELSE SET @MRENT=@BRENT
					set @PRent = @MRent
					set @amt = case when (@mrent * 100) > (floor(@mrent) * 100) then floor(@mrent)+1 else @mrent end
					if @ltrateontariff='Y'
						select TOP 1 @lrate=lrate from lttable where case when @tariff > @amt then @tariff else @amt end  between samt and eamt AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
					else	
						select TOP 1 @lrate=lrate from lttable where @amt  between samt and eamt  AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
--print convert(varchar(20),@amt) + convert(varchar,@lrate) + 'Periya1' goto e_error

						--select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
					set @MRent = (100 / (100 + @lrate + @pSCRate + @STRate)) * @BRent
					-- this was included on 15-12-2013
					declare @tlrate numeric(18,2)

	        set @amt = case when (@mrent * 100) > (floor(@mrent) * 100) then floor(@mrent)+1 else @mrent end
			if @ltrateontariff='Y'
			    if @tariff > @amt 
					select TOP 1 @lrate=lrate from lttable where EAMT >= @tariff   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
				else
					select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
			else	
				select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)

if @tlrate <> @lrate
begin
set @lrate=@tlrate
while (@mrent/100 * (@lrate+@pscrate+@strate) ) + @mrent < @brent
begin
set @mrent = @mrent+1
set @strate = dbo.getstrate(@mrent,@dt)
	        set @amt = case when (@mrent * 100) > (floor(@mrent) * 100) then floor(@mrent)+1 else @mrent end
--			select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
			if @ltrateontariff='Y'
			    if @tariff > @amt 
					select TOP 1 @lrate=lrate from lttable where EAMT >= @tariff   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
				else
					select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
			else	
				select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)


--select TOP 1 @tlrate=lrate from lttable where EAMT >= @MRent AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
end
end
---upto this 15-12-2013
--print convert(varchar(20),@mrent) + convert(varchar(20),@prent) +  ' Periya' goto e_error
					If @MRent  < @PRent
						begin
							select TOP 1 @lrate=lrate from lttable where EAMT >= @mRent  AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
							set @amt = case when (@mrent * 100) > (floor(@mrent) * 100) then floor(@mrent)+1 else @mrent end
							select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
							if @ltrateontariff='Y'
								if @tariff > @amt 
									select TOP 1 @lrate=lrate from lttable where EAMT >= @tariff   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
								else
									select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)
							else	
								select TOP 1 @lrate=lrate from lttable where EAMT >= @amt   AND tno =  (select tno from ltmaster where @dt  between sdt and edt)

							If @lrate = 0 And @StRate = 0 
								begin
									SET @ERRMSG= 'The nett effect can not be carried our for this amount as it goes below the bottom limit for LT/ST where no tax will be calculated'
									set @MRent = @BRent
									set @nettrent=0
									raiserror(@errmsg,16,-1)
								End 
						end
				End 
				------------end
	end	
if @mrent < 0 set @mrent=0
set @mrent = @mrent * @m_adroom
if abs((@mrent*100)-(@trent*100)) <=5  set @mrent=@trent

set @NettRent = case when @MRent < 0 then  0 else  @MRent end
return
e_error:
raiserror(@errmsg,16,-1)

GO
/****** Object:  StoredProcedure [dbo].[GETOPGBAL]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/****** Object:  Stored Procedure dbo.GETOPGBAL    Script Date: 10/14/02 1:17:57 PM ******/
CREATE PROCEDURE [dbo].[GETOPGBAL] @M_COMPANY INT,@M_TXNDT DATETIME , @M_ACTNUM INT, @M_OPGBAL varchar(50)  OUT  
/* with encryption */
AS
DECLARE @M_FY INT
DECLARE @M_FSDATE DATETIME
DECLARE @M_YEAR INT
DECLARE @M_BOFY DATETIME
DECLARE @M_OB AS decimal(18,2)
declare @m_oldtxndt as datetime
declare @txnno1 numeric
set nocount on
EXEC  GETFY @M_TXNDT,@M_COMPANY,@M_FY OUT
SELECT @M_OPGBAL = ''
SELECT @M_FSDATE = @M_TXNDT-1
SELECT @M_OB=0
SELECT @m_oldtxndt=ltxndt,@M_OB=OPGBAL FROM GLPRIMLD WHERE  FY=@M_FY AND CGP_ACTNO= @M_ACTNUM
IF EXISTS (SELECT TXNNO2  FROM VOUCHERS  WHERE FY=@M_FY AND ACTNUM=@M_ACTNUM AND 
    DATEDIFF(dd,txndt,@m_fsdate)=0)
		BEGIN
		SELECT @M_OB = BALANCE,@txnno1=txnno FROM VOUCHERS WHERE TXNNO2=
		    (SELECT TOP 1 TXNNO2  FROM VOUCHERS  WHERE  FY=@M_FY AND ACTNUM=@M_ACTNUM AND
		    DATEDIFF(dd,txndt,@m_fsdate)=0
		    ORDER BY TXNNO2 DESC)
		END
ELSE
	BEGIN
	if exists(select  * from vouchers where fy=@m_fy and actnum=@m_actnum and 
	datediff(d,txndt,@m_txndt) > 0)
begin 
           select @m_ob=balance,@txnno1=txnno from vouchers where txnno2=
	(select top 1 txnno2 from vouchers where fy=@m_fy and actnum=@m_actnum and
	datediff(d,txndt,@m_txndt) > 0 order by datediff(d,'1/1/90',txndt) desc, txnno2 desc)
end
	END
SELECT @M_OPGBAL=convert(varchar(50),@M_OB)
RETURN (@M_Ob/10000000)
--return 100




GO
/****** Object:  StoredProcedure [dbo].[GetServiceASno]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/** PowerBrain Software (India) - 07-May-2012 **/
CREATE PROCEDURE [dbo].[GetServiceASno] @outlet as numeric,@txndt datetime,@sno numeric out   
/* with encryption */
AS
declare @numbering char(1)
declare @diff numeric
declare @billdate datetime
declare @fy1 numeric
select @numbering=g_numbering from setting
exec getfy @txndt,1,@fy1 out
begin transaction 
if @numbering='Y'
	begin
		if not exists (select * from serviceASNo where fy=@fy1 and outlet=@outlet) insert into serviceASNo (outlet,fy,sno) values (@outlet,@fy1,0)
		update serviceASNo set sno=sno+1 where fy=@fy1 and outlet=@outlet
		select @sno=sno from serviceASNo where outlet=@outlet and fy=@fy1
	end
else
	begin
		select @billdate=billdate from outlet where code=@outlet
		set @diff = datediff(d,@billdate,@txndt)
		if datediff(d,getdate(),@txndt) >=0
			begin
--				UPDATE OUTLET SET BNO=CASE WHEN @diff<>0 THEN 1 ELSE isnull(BNO,0)+1 END , BILLDATE= CASE  WHEN datediff(d,'1/1/90',BILLDATE) <> datediff(d,'1/1/90',@m_txndt) THEN @m_txndt ELSE BILLDATE END WHERE CODE = @M_OUTLET
				update outlet set 
				bno=case when @diff <>0 then 1 else isnull(bno,0)+1 end,
				billdate=case when datediff(d,'1/1/90',billdate) <> datediff(d,'1/1/90',@txndt) then @txndt else billdate end 
				where code=@outlet
				select @sno=bno from outlet where code=@outlet
			end
		else
				select @sno=isnull(max(sno),0)+1 from kithist1 where datediff(d,txndt,@txndt)=0
	end
commit transaction




GO
/****** Object:  StoredProcedure [dbo].[GetServiceTaxR]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[GetServiceTaxR]  ( @amt money,@dt datetime=getdate ,@rettaxamt money out ,@retcessamt money out,@outlet int,@sttableno int=1000,@cess2 money out)
/* with encryption */
AS
declare @strate money
declare @cess   money
declare @cess2rate money
declare @samt   money
declare @eamt   money
declare @lamt   money
declare @discount  money

declare @base  money
declare @stamt  decimal(14,2)
declare @cessamt decimal(14,2)
declare @cessonwhat tinyint
declare @cessonwhat2 tinyint
--declare @sttableno int
declare @err varchar(6000)
if @sttableno=1000
	if @outlet=0
		set @sttableno=0
	else
		select @sttableno=sttableno from outlet where code=@outlet


select @cessonwhat=count(*) from sttabler where  sttableno=@sttableno and
(@amt >= samt and @amt <= eamt ) and datediff(d,'1/1/2000',@dt) between
datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)

--print 'Table No ' + convert(varchar(20),@sttableno) + ' Count ' + convert(varchar(20),@cessonwhat)
--+ 'Amount '  + convert(varchar(20),@amt)
---- + ' Date  '  + convert(varchar(20),@dt)
--raiserror('error',16,0) return


if @cessonwhat <> 1 
begin
set @err= 'There is discripancy in your servicetax table' + str(@sttableno)
raiserror(@err ,16,-1)
return
end

select @cessonwhat=cessonwhat,@cessonwhat2=cessonwhat2,@strate=rate,@cess=cess, @discount=discount,@lamt=lamt,@cess2rate=cess2  from sttabler where  sttableno=@sttableno and
(@amt >= samt and @amt <= eamt ) and datediff(d,'1/1/2000',@dt) between
datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
/*
print convert(varchar(20),@strate)
print convert(varchar(20),@cess)
print convert(varchar(20),@discount)
*/
--print @cess2rate begin raiserror('eerror',6,-1) return end
set @cessamt=0
set @stamt = 0
if @discount > 0 
	set @base = @amt -  (@amt * @discount /100)
else
	set @base = @amt

if @strate > 0 
	set @stamt = @base * @strate/100
	else
	set @stamt = 0
	--print 'CEss On What ' + convert(varchar(20),@cessonwhat) + ' ' + convert(varchar(20),@base) + ' ' + convert(varchar(20),@cess)

if @cessonwhat=0
	begin
	if @cess > 0
			set @cessamt = @base * @cess/100
		else
			set @cessamt = 0
	end
else
	begin
		if @CESS > 0 
			if @stamt > 0 
				set @cessamt = @stamt * @cess/100
	end
set @cess2=0
if @cessonwhat2=0
	begin
	if @cess2rate > 0
			set @cess2 = @base * @cess2rate/100
		else
			set @cess2 = 0
	end
else
	begin
		if @CESS2rate > 0 
			if @stamt > 0 
				set @cess2 = @stamt * @cess2rate/100
	end



set @retcessamt = round(@cessamt,2,1)
set @rettaxamt = round(@stamt,2,1)
set @cess2 = round(@cess2,2,1)

return

GO
/****** Object:  StoredProcedure [dbo].[GetServiceTaxR1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[GetServiceTaxR1]  ( @amt money,@dt datetime=getdate ,@rettaxamt money out ,@retcessamt money out,@outlet int,@sttableno int=1000,@cess2 money out,@amt1 money out)
/* with encryption */
AS
declare @strate money
declare @cess   money
declare @cess2rate money
declare @samt   money
declare @eamt   money
declare @lamt   money
declare @discount  money

declare @base  money
declare @stamt  decimal(14,2)
declare @cessamt decimal(14,2)
declare @cessonwhat tinyint
declare @cessonwhat2 tinyint
--declare @sttableno int

if @sttableno=1000
	if @outlet=0
		set @sttableno=0
	else
		select @sttableno=sttableno from outlet where code=@outlet


select @cessonwhat=count(*) from sttabler where  sttableno=@sttableno and
(@amt >= samt and @amt <= eamt ) and datediff(d,'1/1/2000',@dt) between
datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)

--print 'Table No ' + convert(varchar(20),@sttableno) + ' Count ' + convert(varchar(20),@cessonwhat)
--+ 'Amount '  + convert(varchar(20),@amt)
---- + ' Date  '  + convert(varchar(20),@dt)
--raiserror('error',16,0) return


if @cessonwhat <> 1 
begin
raiserror('There is discripancy in your servicetax table' ,16,-1)
return
end

select @cessonwhat=cessonwhat,@cessonwhat2=cessonwhat2,@strate=rate,@cess=cess, @discount=discount,@lamt=lamt,@cess2rate=cess2  from sttabler where  sttableno=@sttableno and
(@amt >= samt and @amt <= eamt ) and datediff(d,'1/1/2000',@dt) between
datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
/*
print convert(varchar(20),@strate)
print convert(varchar(20),@cess)
print convert(varchar(20),@discount)
*/



--print @cess2rate begin raiserror('eerror',6,-1) return end
set @cessamt=0
set @stamt = 0
if @discount > 0 
	set @base = @amt -  (@amt * @discount /100)
else
	set @base = @amt


declare @amt100 money
declare @base1 money
declare @stamt1 money 
declare @cessamta money
declare @cessamtb money
set @amt100 = 100
if @discount > 0 
	set @base1 = @amt100 -  (@amt100 * @discount /100)
else
	set @base1 = @amt100

if @strate > 0 
	set @stamt1 = @base1 * @strate/100
	else
	set @stamt1 = 0

if @cessonwhat=0
	begin
	if @cess > 0
			set @cessamta = @base1 * @cess/100
		else
			set @cessamta = 0
	end
else
	begin
		if @CESS > 0 
			if @stamt1 > 0 
				set @cessamta = @stamt1 * @cess/100
	end

set @cessamtb=0
if @cessonwhat2=0
	begin
	if @cess2rate > 0
			set @cessamtb = @base1 * @cess2rate/100
		else
			set @cessamtb = 0
	end
else
	begin
		if @CESS2rate > 0 
			if @stamt1 > 0 
				set @cessamtb = @stamt1 * @cess2rate/100
	end
--print 'periya'
--print @stamt1
--print @cessamta
--print @cessamtb
--print @amt
set @amt1 = @amt -  (((@stamt1+@cessamta+@cessamtb) * @amt)/ (@stamt1+@cessamta+@cessamtb+100))
--print @stamt1+@cessamta+@cessamtb
--print @amt1
set @base = @amt1

if @strate > 0 
	set @stamt = @base * @strate/100
	else
	set @stamt = 0
	--print 'CEss On What ' + convert(varchar(20),@cessonwhat) + ' ' + convert(varchar(20),@base) + ' ' + convert(varchar(20),@cess)

if @cessonwhat=0
	begin
	if @cess > 0
			set @cessamt = @base * @cess/100
		else
			set @cessamt = 0
	end
else
	begin
		if @CESS > 0 
			if @stamt > 0 
				set @cessamt = @stamt * @cess/100
	end
set @cess2=0
if @cessonwhat2=0
	begin
	if @cess2rate > 0
			set @cess2 = @base * @cess2rate/100
		else
			set @cess2 = 0
	end
else
	begin
		if @CESS2rate > 0 
			if @stamt > 0 
				set @cess2 = @stamt * @cess2rate/100
	end



set @retcessamt = round(@cessamt,2,1)
set @rettaxamt = round(@stamt,2,1)
set @cess2 = round(@cess2,2,1)

return

GO
/****** Object:  StoredProcedure [dbo].[GetST]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[GetST] @txndt datetime,@rno varchar(20) ,@rent money,@rentdiscount money,@st money out,@cess money out,@nor int=1,@cess2 money out
/* with encryption */
AS
declare @allinclusive char(1)
declare @stontariff char(3)
declare @amttemp money
declare @strate money
declare @cessrate money
declare @cessrate2 money
declare @stdiscount money
declare @stlessamt money
declare @head numeric
declare @tariff money
declare @cessonwhat tinyint
declare @cessonwhat2 tinyint
set @nor = case when @nor <=0 then 1 else @nor end
set @rent = @rent/@nor
set @rentdiscount = @rentdiscount/@nor

	if @rent<>0
	begin
		select top 1  @stontariff=ontariff from sttable where (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		
		if @stontariff='YES'
			begin
				set @tariff=(select case when @head=1 then rent1 else rent2 end  from room where rno=@rno)
				set @amttemp=case when @rent > @tariff then @rent else @tariff end
			end
		else
			set @amttemp=@rent-@rentdiscount


		select @strate=rate,@stdiscount=discount,@stlessamt=lamt,@cessrate=cess,@cessonwhat=cessonwhat,@cessrate2=cess2,@cessonwhat2=cessonwhat2  from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))

		set @rent = @rent * @nor
		
		if @stontariff='YES'
			begin
				set @tariff=(select case when @head=1 then rent1 else rent2 end  from room where rnoa=@rno)
				set @amttemp=case when @rent > @tariff then @rent else @tariff end
			end
		else
			set @amttemp=@rent-@rentdiscount


		
		set @amttemp = @amttemp - @stlessamt
		set @st=0
		set @cess=0
		set @cess2=0
		if @amttemp>0 
			begin
			if @strate<>0
				set @st = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@strate/100)) as decimal(12,2))
			else
				set @st=0

		if @cessonwhat=0
			begin
				if @cessrate > 0 
					set @cess = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@cessrate/100)) as decimal(12,2))
			end
		else
			begin
					if @st > 0 
						if @cessrate > 0 
							set @cess = @st * @cessrate / 100
			end		

		if @cessonwhat2=0
			begin
				if @cessrate2 > 0 
					set @cess2 = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@cessrate2/100)) as decimal(12,2))
			end
		else
			begin
					if @st > 0 
						if @cessrate2 > 0 
							set @cess2 = @st * @cessrate2 / 100
			end		


	end
		--print convert(varchar(20),@st) + ' ' + convert(varchar(20),@cessrate) + ' ' + convert(varchar(20),@cess)
		--raiserror('error',16,-1)

end




GO
/****** Object:  StoredProcedure [dbo].[GLOPGBAL]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		RM.Ramanathan
-- Create date: 13 Nov 2017
-- Description:	For Updating Opening Balance in GLPRIMLD for all Companys and all Financial Years
-- =============================================
CREATE PROCEDURE [dbo].[GLOPGBAL]
/* with encryption */
AS
BEGIN
SET NOCOUNT ON;
declare @fy numeric
declare @fydate datetime
declare @acno numeric
declare @pfy as numeric
declare @opgbal money
declare @cr money
declare @dr money
declare @grp money
declare @plbal money
declare @company numeric
declare @pfacbal money
alter table glprimld disable TRIGGER updtbal

declare c0 cursor for select company from company order by company 
open c0
fetch next from c0 into @company
while @@FETCH_STATUS=0
begin
print @company 
set @pfy = 0
	declare c1 cursor for select fy,date from finyear where company=@company order by fy
	open c1 
	fetch next from c1 into @fy,@fydate
	while @@fetch_status=0
		begin
		if @pfy=0 
			BEGIN
			update glprimld set balance=0  where fy=@fy
			END
		else
			BEGIN
			update glprimld set opgbal=0,balance=0  where fy=@fy
			END
		print @fydate
			-- First updating opening balance from the previous year balance
			if @pfy<>0 
				begin
					update glprimld set opgbal=isnull((select isnull(balance,0) from glprimld b where b.fy=@pfy and b.nextyearac=glprimld.cgp_actno),0) where fy=@fy and grp<>7
					select @plbal=sum(isnull(balance,0)) from glprimld where fy=@pfy and grp=7
					select @pfacbal=balance from glprimld where fy=@pfy and cgp_actno=52
					update glprimld set opgbal=isnull(@pfacbal,0)+isnull(@plbal,0) where fy=@fy and cgp_actno=52
				end

			declare c2 cursor for select cgp_actno,opgbal,grp from glprimld  where fy=@fy order by cgp_actno
			open c2
			fetch next  from c2 into @acno,@opgbal,@grp
			while @@fetch_status=0
				begin
				set @dr=0
				set @cr=0
				select 
				@dr=sum(case when amttyp='D' then amt else 0 end), 
				@cr=sum(case when amttyp='C' then amt else 0 end) 
				from vouchers where actnum=@acno and fy=@fy
				update glprimld set balance=isnull(opgbal,0)+isnull(@cr,0)-isnull(@dr,0) where cgp_actno=@acno and fy=@fy
				fetch next from c2 into @acno,@opgbal,@grp
				end
			close c2
			deallocate c2 
			set @pfy=@fy
			fetch next from c1 into @fy,@fydate
		end
		close c1
		deallocate c1

		fetch next from c0 into @company
	end
	close c0
	deallocate c0

alter table glprimld enable TRIGGER updtbal

END

GO
/****** Object:  StoredProcedure [dbo].[gptemp]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[gptemp] @m_temp varchar(200) 
/* with encryption */
as
declare @cnt numeric(10)
declare @userno numeric(10) 
declare @outlet numeric(10)  
declare @sql varchar(4000)
set @cnt=0
set @sql =  'declare c1 cursor for select userno,outlet from ' + @m_temp
exec(@sql)
open c1 fetch next  from c1 into @userno,@outlet 
while @@fetch_status=0 begin  select @cnt=@cnt+1 
set @sql = 'update ' +  @m_temp + ' set srno=' + str(@cnt) + '  where isnull(outlet,0)=' + STR(isnull(@outlet,0)) + '  and userno=' + str(@userno) 
exec(@sql)
fetch next from c1 into @userno,@outlet 
end 
close c1 
deallocate c1




GO
/****** Object:  StoredProcedure [dbo].[grpdetails]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[grpdetails] @grpname varchar(50),@brno numeric =0 
/* with encryption */
AS
--print 'periya'
declare @gno numeric
declare @rno numeric
declare @company int
declare @amt money
declare @split char(1)
declare @x int
declare @sql varchar(8000)
declare @s1 numeric
select * into #dummyg from dummygdetails
--For Company=1
set @company=1
	set @split='N'
	set @x=1
	if @brno=0
	declare c1 cursor for select gno,rno from guest where grpname=@grpname
	else
	declare c1 cursor for select gno,rno from guest where brno=@brno
	open c1
	fetch next from c1 into @gno,@rno
	while @@fetch_status=0
	begin
		insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
		declare s1 cursor for select channel  from restco  where company=@company  order by channel
		open s1
		fetch next from s1 into @s1
		while @@fetch_status=0
		begin
		if exists(	select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1  )   and split=@split
			)
			 begin
				select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
				from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 )   and split=@split) a
				set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
				exec(@sql)
			end
			set @x=@x+1
			fetch next from s1 into @s1
		end
		close s1
		deallocate s1
		declare s1 cursor for select outlet from phoutlet where company=@company order by outlet
		open s1
		fetch next from s1 into @s1
		while @@fetch_status=0
		begin
		if exists (select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@s1
			) begin
				select @amt=sum(amt) from ( select  (select sum(amount) from phhist where cno=phone.cno) - damt amt from phone where gno=@gno and split=@split and outlet=@s1) a
				set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
				exec(@sql)
			end
			set @x=@x+1
			fetch next from s1 into @s1
		end
		close s1
		deallocate s1
		fetch next from c1 into @gno,@rno
	end
close c1
deallocate c1
set @split='Y'
set @x=1
if @brno=0
declare c1 cursor for select gno,rno from guest where grpname=@grpname
else
declare c1 cursor for  select gno,rno from guest where  brno=@brno
open c1
fetch next from c1 into @gno,@rno
while @@fetch_status=0
begin
	insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
	declare s1 cursor for select channel  from restco  where company=@company order by channel
	open s1
	fetch next from s1 into @s1
	while @@fetch_status=0
	begin
	if exists(	select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
		from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 )   and split=@split
		)
		 begin
			select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 )   and split=@split) a
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
		end
		set @x=@x+1
		fetch next from s1 into @s1
	end
	close s1
	deallocate s1
	declare s1 cursor for select outlet from phoutlet where company=@company order by outlet
	open s1
	fetch next from s1 into @s1
	while @@fetch_status=0
	begin
		if exists (select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@s1
			) begin
				select @amt=sum(amt) from ( select  (select sum(amount) from phhist where cno=phone.cno) - damt amt from phone where gno=@gno and split=@split and outlet=@s1) a
				set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
				exec(@sql)
			end
		set @x=@x+1
		fetch next from s1 into @s1
	end
	close s1
	deallocate s1
	fetch next from c1 into @gno,@rno
end
close c1
deallocate c1
set @split='C'
set @x=1
if @brno=0
declare c1 cursor for select gno,rno from guest where grpname=@grpname
else
declare c1 cursor for  select gno,rno from guest where  brno=@brno
open c1
fetch next from c1 into @gno,@rno
while @@fetch_status=0
begin
	insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
	declare s1 cursor for select channel  from restco  where company=@company order by channel
	open s1
	fetch next from s1 into @s1
	while @@fetch_status=0
	begin
	if exists(	select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
		from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 )   and split=@split
		)
		 begin
			select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 )   and split=@split) a
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
		end
		set @x=@x+1
		fetch next from s1 into @s1
	end
	close s1
	deallocate s1
	declare s1 cursor for select outlet from phoutlet where company=@company order by outlet
	open s1
	fetch next from s1 into @s1
	while @@fetch_status=0
	begin
	if exists (select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@s1
		) begin
			select @amt=sum(amt) from ( select  (select sum(amount) from phhist where cno=phone.cno) - damt amt from phone where gno=@gno and split=@split and outlet=@s1) a
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
		end
		set @x=@x+1
		fetch next from s1 into @s1
	end
	close s1
	deallocate s1
	fetch next from c1 into @gno,@rno
end
close c1
deallocate c1
set @split='F'
set @x=1
if @brno=0
declare c1 cursor for select gno,rno from guest where grpname=@grpname
else
declare c1 cursor for  select gno,rno from guest where  brno=@brno
open c1
fetch next from c1 into @gno,@rno
while @@fetch_status=0
begin
	insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
	declare s1 cursor for select channel  from restco  where company=@company order by channel
	open s1
	fetch next from s1 into @s1
	while @@fetch_status=0
	begin
	if exists(	select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
		from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 )   and split=@split
		)
		 begin
			select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 )   and split=@split) a
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
		end
		set @x=@x+1
		fetch next from s1 into @s1
	end
	close s1
	deallocate s1
	declare s1 cursor for select outlet from phoutlet where company=@company order by outlet
	open s1
	fetch next from s1 into @s1
	while @@fetch_status=0
	begin
	if exists (select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@s1
		) begin
			select @amt=sum(amt) from ( select  (select sum(amount) from phhist where cno=phone.cno) - damt amt from phone where gno=@gno and split=@split and outlet=@s1) a
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
		end
		set @x=@x+1
		fetch next from s1 into @s1
	end
	close s1
	deallocate s1
	fetch next from c1 into @gno,@rno
end
close c1
deallocate c1
--- company 1 ends
declare co1 cursor for select company from company where company > 1
open co1
fetch next from co1 into @company
while @@fetch_status=0
begin
	set @split='N'
	set @x=1
	if @brno=0
	declare c1 cursor for select gno,rno from guest where grpname=@grpname
	else
	declare c1 cursor for select gno,rno from guest where brno=@brno
	open c1
	fetch next from c1 into @gno,@rno
	while @@fetch_status=0
	begin
--print str(@gno)
		insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
		declare s1 cursor for select channel  from restco  where company=@company  order by channel
		open s1
		fetch next from s1 into @s1
		while @@fetch_status=0
		begin
if exists(			select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 and selfdeal='Y')   and split=@split
) begin
			select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 and selfdeal='Y')   and split=@split) a
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
end
			set @x=@x+1
			fetch next from s1 into @s1
		end
		close s1
		deallocate s1
		declare s1 cursor for select outlet from phoutlet where company=@company order by outlet
		open s1
		fetch next from s1 into @s1
		while @@fetch_status=0
		begin
if exists (			select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@s1
) begin
			select @amt=sum(amt) from ( select  (select sum(amount) from phhist where cno=phone.cno) - damt amt from phone where gno=@gno and split=@split and outlet=@s1) a
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
end
			set @x=@x+1
			fetch next from s1 into @s1
		end
		close s1
		deallocate s1
		fetch next from c1 into @gno,@rno
	end
close c1
deallocate c1
	set @split='C'
	set @x=1
	if @brno=0
	declare c1 cursor for select gno,rno from guest where grpname=@grpname
	else
	declare c1 cursor for  select gno,rno from guest where  brno=@brno
	open c1
	fetch next from c1 into @gno,@rno
	while @@fetch_status=0
	begin
		insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
		declare s1 cursor for select channel  from restco  where company=@company order by channel
		open s1
		fetch next from s1 into @s1
		while @@fetch_status=0
		begin
if exists(			select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 and selfdeal='Y')   and split=@split
) begin
			select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 and selfdeal='Y')   and split=@split) a
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
end
			set @x=@x+1
			fetch next from s1 into @s1
		end
		close s1
		deallocate s1
		declare s1 cursor for select outlet from phoutlet where company=@company order by outlet
		open s1
		fetch next from s1 into @s1
		while @@fetch_status=0
		begin
if exists (			select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@s1
) begin
			select @amt = (select sum(amount) from phhist where cno=phone.cno) - damt from phone where gno=@gno and split=@split and outlet=@s1
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
end
			set @x=@x+1
			fetch next from s1 into @s1
		end
		close s1
		deallocate s1
		fetch next from c1 into @gno,@rno
	end
close c1
deallocate c1
	set @split='F'
	set @x=1
	if @brno=0
	declare c1 cursor for select gno,rno from guest where grpname=@grpname
	else
	declare c1 cursor for  select gno,rno from guest where  brno=@brno
	open c1
	fetch next from c1 into @gno,@rno
	while @@fetch_status=0
	begin
		insert into #dummyg(gno,rno,company,split,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10) values (@gno,@rno,@company,@split,0,0,0,0,0 ,0,0,0,0,0)
		declare s1 cursor for select channel  from restco  where company=@company order by channel
		open s1
		fetch next from s1 into @s1
		while @@fetch_status=0
		begin
if exists(			select (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 and selfdeal='Y')   and split=@split
) begin
			select @amt=sum(amt) from (select  (select sum((qty*rate)-discount+Tax) from kithist where bno=kithist1.bno)+roamt+scamt  amt
			from kithist1 where gno=@gno and outlet in (select code from outlet where channel = @s1 and selfdeal='Y')   and split=@split) a
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
end
			set @x=@x+1
			fetch next from s1 into @s1
		end
		close s1
		deallocate s1
		declare s1 cursor for select outlet from phoutlet where company=@company order by outlet
		open s1
		fetch next from s1 into @s1
		while @@fetch_status=0
		begin
if exists (			select  (select sum(amount) from phhist where cno=phone.cno) - damt  amt from phone where gno=@gno and split=@split and outlet=@s1
) begin
			select @amt = (select sum(amount) from phhist where cno=phone.cno) - damt from phone where gno=@gno and split=@split and outlet=@s1
			set @sql = 'update #dummyg set item' + ltrim(str(@x)) + '=' + convert(varchar(20),@amt) + '  where company=' + str(@company) + ' and split=''' + @split + ''' and gno=' + STR(@gno)
			exec(@sql)
end
			set @x=@x+1
			fetch next from s1 into @s1
		end
		close s1
		deallocate s1
		fetch next from c1 into @gno,@rno
	end
	close c1
	deallocate c1
	fetch next from co1 into @company
end
close co1
deallocate co1
select  Company= (select name from company where company=#dummyg.company ),
[Bill Type]=case 
when split='N' then 'Main Bill'
when split='Y' then 'Supp Bill'
when split='F' then 'Free Bill - not booked'
when split='C' then 'Comp Bill - booked'
end,
[Room Number] = rno,[Guest Name]=(select name from guestad where cno=(select cno from guest where gno=#dummyg.gno)),item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,total=(item1+item2+item3+item4+item5+item6+item7+item8+item9+item10)
from #dummyg
where item1+item2+item3+item4+item5+item6+item7+item8+item9+item10 <> 0
order by rno
--select * from #dummyg
return
e_error:
raiserror('Error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[grpred]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) - Procedure created on 01 March 2007 */
CREATE PROCEDURE [dbo].[grpred] @brno numeric, @fname varchar(50) ,@delete int = 0   
/* with encryption */
AS
begin transaction
declare @gno numeric
declare @bno numeric
declare @cno numeric
declare @sql varchar(8000)
declare @male int
declare @female int
declare @child int
declare @gnostr varchar(1500)
declare @usernm varchar(100)
declare @userno int
select @usernm=usernm,@userno=userno from comuser where [host_id]=host_id()
set @gnostr = ''
set @male = 0
set @female = 0
set @child = 0
set @sql = 'declare g1 cursor for select gno from ' + @fname 
exec(@sql)
open g1
fetch next from g1 into @gno
while @@fetch_status=0
begin
if @gnostr=''
set @gnostr = @gnostr + ltrim(str(@gno)) 
else
set @gnostr = ',' + @gnostr + ltrim(str(@gno)) 

	declare c1 cursor for select bno from kithist1 where gno=@gno
	open c1
	fetch next  from c1 into @bno
	while @@fetch_status=0
		begin
			exec pbpk1508 @bno,'',1,1
			if @@error<>0 goto e_error
			fetch next  from c1 into @bno
			--print 'Periya'
		end
	close c1
	deallocate c1
	declare d1 cursor for select cno from phone where gno=@gno
	open d1
	fetch next from d1 into @bno
	while @@fetch_status=0
		begin
			exec pbpp1508 @bno,'',1,1
			if @@error<>0 goto e_error
			fetch next from d1 into @bno
		end
	close d1
	deallocate d1

	declare d1 cursor for select CNO from ADVANCE where gno=@gno
	open d1
	fetch next from d1 into @bno
	while @@fetch_status=0
		begin
            exec pbpa1508 @bno,@usernm,@userno
			if @@error<>0 goto e_error
			fetch next from d1 into @bno
		end
	close d1
	deallocate d1



	delete from billhist where gno=@gno
	if @@error<>0 goto e_error
	if @delete=1 
		begin
		delete from grent where gno=@gno
		if @@error<>0 goto e_error
		delete from gsplit where gno=@gno
		if @@error<>0 goto e_error
		delete from groom where gno=@gno
		if @@error<>0 goto e_error
		delete from guest where gno=@gno
		if @@error<>0 goto e_error
		end
	else
		begin
			update guest set opentoall='N' where gno=@gno
			if @@error<>0 goto e_error
		end
--	select @male=@male + male ,@female=@female+female,@child=@child+child from guest where gno=@gno
	fetch next from g1 into @gno
end
close g1
deallocate g1
declare a1 cursor for select cno from billhist1 where brno=@brno
open a1
fetch next from a1 into @cno
while @@fetch_status=0
	begin
		exec pbvc @cno
		fetch next from a1 into @cno
	end
close a1
deallocate a1
set @male = isnull(@male,0)
set @female = isnull(@female,0)
set @child=isnull(@child,0)
insert into acclog(narration,txntype) values ('G.No: ' + @gnostr + ' Group Details Modified',175)
---select top 1 @gno=gno from guest where brno=@brno and opentoall<>'N'
---update guest set male=male+@male,female=female+@female,child=child+@child where gno=@gno
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[guper]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.guper    Script Date: 15/02/2004 6:23:00 PM ******/
/****** GuperNew was created on 28-May-2009*****/
CREATE PROCEDURE [dbo].[guper] @userno numeric , @t1 varchar(50),@opt tinyint = 0   
/* with encryption */
AS
declare @name varchar(100)
declare @number numeric
declare @sql varchar(7000)
declare @olname varchar(100)
declare @code numeric
declare @serviceno numeric
declare @outlettype int
declare @outlet int
declare @permission char(1)
declare @level int
set @sql= 'truncate table ' + @t1
exec(@sql)


/*
declare tempcur cursor for select servicename,serviceno,defaultpermittedto,outlettype 
from servicenumbers -- where outlettype=0 or outlettype=4 or outlettype=6 or outlettype = 10
open tempcur
fetch next from tempcur into @name,@number,@level,@outlettype
while @@fetch_status=0
begin
set @sql = 'insert into ' + @t1 + ' (servicename,serviceno,defaultlevel,outlettype,outlet) values (''' + @name + ''',' + ltrim(str(@number)) + ',' + str(@level) +','+ str(@outlettype) + ',0)'
exec(@sql)
fetch next from tempcur into @name,@number,@level,@outlettype
end
close tempcur
deallocate tempcur
*/

set @sql = 'insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename)),1,100),
serviceno,outlettype,0,
(select permission from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno  and outlettype=
servicenumbers.outlettype) ,defaultpermittedto
from servicenumbers where outlettype not in (1,2,5)'

exec(@sql)

/*
declare tempcur cursor for select servicename,serviceno,[name],[code],defaultpermittedto from servicenumbers,outlet where outlettype=1 
open tempcur
fetch next from tempcur into @name,@number,@olname,@code,@level
while @@fetch_status=0
begin
set @sql = 'insert into ' + @t1 + ' (servicename,serviceno,outlet,OUTLETTYPE,defaultlevel) values (''' + rtrim(ltrim(@name)) + '-' + ltrim(rtrim(@olname))  + ''',' + ltrim(str(@number)) + ',' + str(@code) + ',1,' + str(@level) + ')'
exec(@sql)
fetch next from tempcur into @name,@number,@olname,@code,@level
end
close tempcur
deallocate tempcur
*/
set @sql = 'insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename))
 +''-''+ltrim(rtrim([name])),1,100),serviceno,1,[code],
(select permission from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.code and outlettype=
servicenumbers.outlettype) ,defaultpermittedto
from servicenumbers,outlet b where outlettype=1'

exec(@sql)
/*
declare tempcur cursor for select servicename,serviceno,[name],[OUTLET],defaultpermittedto from servicenumbers,PHoutlet where outlettype=2 
open tempcur
fetch next from tempcur into @name,@number,@olname,@code,@level
while @@fetch_status=0
begin
set @sql = 'insert into ' + @t1 + ' (servicename,serviceno,outlet,OUTLETTYPE,defaultlevel) values (''' + rtrim(ltrim(@name)) + '-' + ltrim(rtrim(@olname))  + ''',' + ltrim(str(@number)) + ',' + str(@code) + ',2,' + str(@level) + ')'
exec(@sql)
fetch next from tempcur into @name,@number,@olname,@code,@level
end
close tempcur
deallocate tempcur
*/
set @sql ='insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename)) +''-''+
ltrim(rtrim([name])),1,100),serviceno,2,[outlet],
(select permission from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.outlet and outlettype=
servicenumbers.outlettype) ,defaultpermittedto
from servicenumbers,phoutlet b where outlettype=2'

exec(@sql)

/*
declare tempcur cursor for select servicename,serviceno,[name],[company],defaultpermittedto from servicenumbers,company where outlettype=5
open tempcur
fetch next from tempcur into @name,@number,@olname,@code,@level
while @@fetch_status=0
begin
set @sql = 'insert into ' + @t1 + ' (servicename,serviceno,outlet,OUTLETTYPE,defaultlevel) values (''' + rtrim(ltrim(@name)) + '-' + ltrim(rtrim(@olname))  + ''',' + ltrim(str(@number)) + ',' + str(@code) + ',5,' + str(@level) + ')'
exec(@sql)
fetch next from tempcur into @name,@number,@olname,@code,@level
end
close tempcur
deallocate tempcur
*/

set @sql ='insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename)) +''-''+
ltrim(rtrim([name])),1,100),serviceno,5,[company],
(select permission from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.company and outlettype=
servicenumbers.outlettype) ,defaultpermittedto
from servicenumbers,company b where outlettype=5'

exec(@sql)

if @opt=1
begin
set @sql='update ' + @t1 + ' set permission='''''
exec(@sql)
set @sql = 'update ' + @t1 + ' set permission=''Y'' where serviceno in (select serviceno from servicenumbersdefault)'
exec(@sql)
end
/*
set @sql = 'declare tempcur cursor for select serviceno,permission,outlettype,outlet from ' + @t1
exec( @sql)
open tempcur
fetch next from tempcur into @serviceno,@permission,@outlettype,@outlet
while @@fetch_status=0
begin
if exists(select * from servicepermission where userno=@userno and serviceno=@serviceno and outlettype=@outlettype and outlet=@outlet)
  begin
  select @permission=permission from servicepermission where userno=@userno and serviceno=@serviceno and outlettype=@outlettype and outlet=@outlet
  print str(@serviceno) + '  ' + @permission
  set @sql = 'update ' + @t1 + ' set permission=''' + @permission + ''' where current of tempcur'
  print @sql
  exec (@sql)
  end
fetch next from tempcur into @serviceno,@permission,@outlettype,@outlet
end
close tempcur
deallocate tempcur
*/




GO
/****** Object:  StoredProcedure [dbo].[gupernew]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.guper    Script Date: 15/02/2004 6:23:00 PM ******/
/****** GuperNew was created on 28-May-2009*****/
CREATE PROCEDURE [dbo].[gupernew] @userno numeric , @t1 varchar(50),@opt tinyint = 0 
/* with encryption */
AS
declare @name varchar(100)
declare @number numeric
declare @sql varchar(7000)
declare @olname varchar(100)
declare @code numeric
declare @serviceno numeric
declare @outlettype int
declare @outlet int
declare @permission char(1)
declare @level int
update servicepermission set preper=permission where preper is null
set @sql= 'truncate table ' + @t1
exec(@sql)
set @sql = 'insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename)),1,100),
serviceno,outlettype,0,
(select permission from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno  and outlettype=
servicenumbers.outlettype) ,defaultpermittedto,
(select preper from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno  and outlettype=
servicenumbers.outlettype)
from servicenumbers where outlettype not in (1,2,5)'
exec(@sql)
set @sql = 'insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename))
 +''-''+ltrim(rtrim([name])),1,100),serviceno,1,[code],
(select top 1 permission from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.code and outlettype=
servicenumbers.outlettype) ,defaultpermittedto,
(select top 1 preper from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.code and outlettype=
servicenumbers.outlettype)
from servicenumbers,outlet b where outlettype=1'
exec(@sql)
set @sql ='insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename)) +''-''+
ltrim(rtrim([name])),1,100),serviceno,2,[outlet],
(select top 1 permission from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.outlet and outlettype=
servicenumbers.outlettype) ,defaultpermittedto,
(select top 1 preper from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.outlet and outlettype=
servicenumbers.outlettype)
from servicenumbers,phoutlet b where outlettype=2'
exec(@sql)
set @sql ='insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename)) +''-''+
ltrim(rtrim([name])),1,100),serviceno,5,[company],
(select top 1 permission from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.company and outlettype=
servicenumbers.outlettype) ,defaultpermittedto,
(select top 1 preper from servicepermission where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.company and outlettype=
servicenumbers.outlettype)
from servicenumbers,company b where outlettype=5'
exec(@sql)
if @opt=1
	begin
	set @sql='update ' + @t1 + ' set permission='''''
	exec(@sql)
	set @sql = 'update ' + @t1 + ' set permission=''Y'',preper=''Y'' where serviceno in (select serviceno from servicenumbersdefault)'
	exec(@sql)
	end




GO
/****** Object:  StoredProcedure [dbo].[gupernewdesign]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** GuperNewDesign was created on 12-09-2009*****/
CREATE PROCEDURE [dbo].[gupernewdesign] @userno numeric , @t1 varchar(50),@opt tinyint = 0 
/* with encryption */
AS
declare @name varchar(100)
declare @number numeric
declare @sql varchar(7000)
declare @olname varchar(100)
declare @code numeric
declare @serviceno numeric
declare @outlettype int
declare @outlet int
declare @permission char(1)
declare @level int
update servicepermissiond set preper=permission where preper is null
set @sql= 'truncate table ' + @t1
exec(@sql)
set @sql = 'insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename)),1,100),
serviceno,outlettype,0,
(select permission from servicepermissiond where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno  and outlettype=
servicenumbers.outlettype) ,defaultpermittedto,
(select preper from servicepermissiond where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno  and outlettype=
servicenumbers.outlettype)
from servicenumbers where outlettype not in (1,2,5)'
exec(@sql)

if exists (select name,xtype from sysobjects where name = 'outlet' and xtype = 'U') 
begin
	set @sql = 'insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename))
	 +''-''+ltrim(rtrim([name])),1,100),serviceno,1,[code],
	(select permission from servicepermissiond where userno=' + str(@userno) + ' and 
	serviceno=servicenumbers.serviceno and outlet=b.code and outlettype=
	servicenumbers.outlettype) ,defaultpermittedto,
	(select preper from servicepermissiond where userno=' + str(@userno) + ' and 
	serviceno=servicenumbers.serviceno and outlet=b.code and outlettype=
	servicenumbers.outlettype)
	from servicenumbers,outlet b where outlettype=1'
	exec(@sql)
end
if exists (select name,xtype from sysobjects where name = 'phoutlet' and xtype = 'U') 
begin
set @sql ='insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename)) +''-''+
ltrim(rtrim([name])),1,100),serviceno,2,[outlet],
(select permission from servicepermissiond where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.outlet and outlettype=
servicenumbers.outlettype) ,defaultpermittedto,
(select preper from servicepermissiond where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.outlet and outlettype=
servicenumbers.outlettype)
from servicenumbers,phoutlet b where outlettype=2'
exec(@sql)
end
set @sql ='insert into ' + @t1 + '  select substring(ltrim(rtrim(servicename)) +''-''+
ltrim(rtrim([name])),1,100),serviceno,5,[company],
(select permission from servicepermissiond where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.company and outlettype=
servicenumbers.outlettype) ,defaultpermittedto,
(select preper from servicepermissiond where userno=' + str(@userno) + ' and 
serviceno=servicenumbers.serviceno and outlet=b.company and outlettype=
servicenumbers.outlettype)
from servicenumbers,company b where outlettype=5'
exec(@sql)
if @opt=1
	begin
	set @sql='update ' + @t1 + ' set permission='''''
	exec(@sql)
	set @sql = 'update ' + @t1 + ' set permission=''Y'',preper=''Y'' where serviceno in (select serviceno from servicenumbersdefault)'
	exec(@sql)
	end




GO
/****** Object:  StoredProcedure [dbo].[initguest]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[initguest] @ddt datetime 
/* with encryption */
AS

begin transaction
delete from groom where gno in (select gno from guest where datediff(d,@ddt,co_date) <=0) 
delete from grent where gno in (select gno from guest where datediff(d,@ddt,co_date) <=0)
delete from gsplit where gno in (select gno from guest where datediff(d,@ddt,co_date) <=0)
delete from kithist where bno in (select bno from kithist1 where gno in (select gno from guest where datediff(d,@ddt,co_date) <=0))
delete from kithist1 where gno in (select gno from guest where datediff(d,@ddt,co_date) <=0) 
delete from phhist where cno in (select cno from phone where gno in (select gno from guest where datediff(d,@ddt,co_date) <=0)) 
delete from phone where gno in (select gno from guest where datediff(d,@ddt,co_date) <=0) 
--delete from chalan where gno in (select gno from guest where datediff(d,@ddt,co_date) <=0) 
delete from billhist where gno in (select gno from guest where datediff(d,@ddt,co_date) <=0)  
delete from billhist1 where brno in (select brno from guest where datediff(d,@ddt,co_date) <=0) 
delete from guest where datediff(d,@ddt,co_date) <=0
commit transaction
return
eh:
rollback transaction
raiserror('Error initialising guest database',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[initsuppglac]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[initsuppglac]  @fy numeric 
/* with encryption */
AS
declare @fy1 numeric
declare @ac numeric
declare @ac1 numeric
declare @supp numeric
BEGIN TRANSACTION
print 'Error in re-making suppliers account'
declare c1 cursor for select glactno,supplier from suppglac where fy=@fy
if @@error<>0 goto e_error
open c1
if @@error<>0 goto e_error
fetch next from c1 into @ac,@supp
if @@error<>0 goto e_error
while @@fetch_status=0
begin
	select @fy1=nextyearfy,@ac1=nextyearac from glprimld where fy=@fy and cgp_actno=@ac
	if @@error<>0 goto e_error
	if @fy1 is not null
	update suppglac set fy=@fy1,glactno=@ac1 where supplier=@supp and fy=@fy and glactno=@ac
	if @@error<>0 goto e_error
	fetch next from c1 into @ac,@supp
end
close c1
deallocate c1
COMMIT TRANSACTION
RETURN
E_ERROR:
rollback transaction
RAISERROR('ERROR',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[miscashbox]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[miscashbox] @dt datetime ,@dt1 datetime
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

select billno=cashboxno,name=usernm + ' ' + computername,roomservice=0,cash=case when amttyp='D' then amt else -amt end,creditcard=0,glaccts=0,COMPLEMENTARY=0, BILLREC=0,details=case when amttyp='D' then 'Income-'+Computername+' ' + usernm else 'Expenses-' +Computername+' ' + usernm end from (
select amttyp ,cashboxno,min(computername) computername,min(usernm) usernm, sum(amt) amt from cashboxhist where txnno=0 and                                datediff(d,'1/1/2000',txndt) between datediff(d,'1/1/2000',@dt) and datediff(d,'1/1/2000',@dt1) group by cashboxno,amttyp
union  
select amttyp ,cashboxno,min(computername) computername,min(usernm) usernm, sum(amt) amt from cashboxhist where txnno<>0 and partic like '%Pur.Chln%' and  datediff(d,'1/1/2000',txndt) between datediff(d,'1/1/2000',@dt) and datediff(d,'1/1/2000',@dt1) group by cashboxno,amttyp
) a

END


GO
/****** Object:  StoredProcedure [dbo].[newlic]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[newlic]
/* with encryption */
AS
BEGIN
SET NOCOUNT ON;
declare @company numeric
declare @lic1 varchar(50)
declare @lic2 varchar(50)
if exists(select* from lic) return
declare c1 cursor for select company,lic1,lic2 from company order by company
open c1
fetch next  from c1 into @company,@lic1,@lic2
while @@fetch_status=0
begin
insert into lic (company,sdt,edt,lic1,lic2) values (@company,'1 jan 2000','30 jun 2017','LT No','ST No')
insert into lic (company,sdt,edt,lic1,lic2) values (@company,'1 jul 2017','30 jun 2999',@lic1,@lic2)
fetch next  from c1 into @company,@lic1,@lic2
end
close c1
deallocate c1

END

GO
/****** Object:  StoredProcedure [dbo].[OL24Posting]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:	 PowerBrain Software (India)
-- Create date: 14 June 2012
-- Description:	For 24 Online Posting Request
-- =============================================
CREATE PROCEDURE [dbo].[OL24Posting]
@gno numeric,@SeqNo as integer,@amt numeric(18,2),@txndt datetime
/* with encryption */
AS
BEGIN
declare @outlet int
declare @sql varchar(6000)
declare @rv varchar(100)
declare @t1 varchar(100)

EXEC RANDFILE @RV OUT
select @t1 = 'INET' + ltrim(@RV)
SET @SQL = 'SELECT * INTO ' + @T1 + '  FROM  DUMMYph'
EXEC (@SQL)

select @outlet = inetoutlet from setting 



    -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
insert into ol24 (gno,amount,seqno,txndt) values 
(@gno,@amt,@seqno,@txndt)

if @amt <> 0 
begin
	set @sql = 'insert into ' + @t1 + ' (detail,amount) values (''WiFi Usage Charges'', ' + convert(varchar(20),@amt) + ')'
	exec(@sql)
	set @sql =  'declare @ocno int exec pbpp1u  ''' + @t1 + ''' ,' + str(@outlet) + ',''' + convert(varchar(20),getdate()) + ''',2,0, ' + str(@gno) + ','''','''','''',0, @ocno out'
	exec(@sql)
end

END




GO
/****** Object:  StoredProcedure [dbo].[ONACAutoReverse]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ONACAutoReverse] @company int,@asset char(1)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @cgp_actno numeric
	declare @osamt numeric(18,2)
	declare @remainder numeric(18,2)
	declare @adjamt numeric(18,2) 
	declare @totamt_balance numeric(18,2) 
	declare @rtxnno numeric
	declare @txnno numeric
	declare @cgp_Desc varchar(100)
declare @txndt datetime
declare @dt varchar(20) 
declare @cnt numeric
declare @totcnt numeric
declare @err varchar(1000)
set @cnt=0
--   update vouchers set totamt_balance=amt where company=1 and actnum in (select cgp_actno from glprimld where company=1 and asset=@asset and onac='O')d
declare @amttyp char(1)
declare @amttyp1 char(1)
set @amttyp  = @asset
set @amttyp1 = case when @asset='D' then 'C' else 'D' end

    select @totcnt= count(*) from (select distinct cgp_actno,company,cgp_desc from glprimld where company=@company and asset=@asset and onac='O' AND CGP_ACTNO IN (SELECT ACTNUM FROM VOUCHERS WHERE COMPANY=@COMPANY AND ACTNUM IN (SELECT CGP_ACTNO FROM GLPRIMLD WHERE ONAC='O' AND COMPANY=@COMPANY))) a
	--declare c1 cursor for select distinct cgp_actno,company,cgp_desc from glprimld where asset=@asset and onac='O' order by cgp_Desc
	declare c1 cursor for select distinct cgp_actno,company,cgp_desc from glprimld where company=@company and asset=@asset and onac='O' AND CGP_ACTNO IN (SELECT ACTNUM FROM VOUCHERS WHERE COMPANY=@COMPANY AND ACTNUM IN (SELECT CGP_ACTNO FROM GLPRIMLD WHERE ONAC='O' AND COMPANY=@COMPANY))
	open c1
	fetch next from c1 into @cgp_actno,@company,@cgp_desc
	while @@fetch_status=0
	begin
	set @cnt=@cnt+1
	set @err = ltrim(str(@totcnt)) + '/' + ltrim(str(@cnt))
	raiserror (@err,0,1) with nowait

	set @remainder = 0

		declare c2 cursor for select txnno,totamt_balance,txndt from vouchers where company=@company and actnum=@cgp_actno and amttyp=@amttyp1 and totamt_balance<>0 order by txndt
		open c2 
		fetch next from c2 into @txnno,@osamt ,@txndt
		while @@fetch_status=0
			begin
				set @remainder=@remainder + @osamt
				if @remainder > 0 
				begin 
						declare c3 cursor for select txnno,totamt_balance from vouchers where company=@company and actnum=@cgp_actno and amttyp=@amttyp and totamt_balance <> 0 order by txndt
						open c3
						fetch next from c3 into @rtxnno,@totamt_balance
						while @@fetch_status=0
							begin 
							if @remainder > 0 
							begin
								set @err = ltrim(str(@txnno))
								raiserror (@err,0,1) with nowait
								set @adjamt = case when @totamt_balance >= @remainder then @remainder  when @totamt_balance < @remainder then @totamt_balance  end
								set @remainder = @remainder - @adjamt 
								update vouchers set totamt_balance=totamt_balance-@adjamt,billdate_CloseDate=getdate() where txnno=@rtxnno
								update vouchers set totamt_balance=totamt_balance-@adjamt,billdate_CloseDate=getdate() where txnno=@txnno  
							end
							if @remainder = 0  break 
							fetch next from c3 into @rtxnno,@totamt_balance
							end
						close c3
						deallocate c3
				end
				fetch next from c2 into @txnno,@osamt  ,@txndt
			end	
		close c2
		deallocate c2
		fetch next from c1 into @cgp_actno,@company,@cgp_desc
	end
	close c1
	deallocate c1
END


GO
/****** Object:  StoredProcedure [dbo].[ONACREV]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.ONACREV    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.ONACREV    Script Date: 1/20/03 11:51:46 AM ******/
/****** Object:  Stored Procedure dbo.ONACREV    Script Date: 10/14/02 1:17:57 PM ******/
CREATE PROCEDURE [dbo].[ONACREV]  @M_TABLE VARCHAR(50) , @M_COMPANY AS INT,@M_TDNO INT  
/* with encryption */
AS
DECLARE @SQL VARCHAR(5000)
DECLARE @M_FDRSNO INT
DECLARE @M_AMT MONEY
DECLARE @M_TXNAMT MONEY
DECLARE @M_ONACBAL MONEY
DECLARE @M_NAME VARCHAR(500)
DECLARE @M_XTYPE CHAR(1)
IF @M_TDNO=0
BEGIN 
SELECT @SQL = 'DROP TABLE ' +  @M_TABLE
EXEC( @SQL)
RETURN (1)
END
DECLARE TEMPCUR CURSOR FOR select name,xtype from sysobjects where name =  @M_TABLE   and xtype = 'U'
OPEN TEMPCUR
FETCH NEXT FROM TEMPCUR INTO @M_NAME ,@M_XTYPE
IF @@FETCH_STATUS <> 0
BEGIN
CLOSE TEMPCUR
DEALLOCATE TEMPCUR
GOTO EERROR
END
CLOSE TEMPCUR
DEALLOCATE TEMPCUR
IF @@ERROR <> 0 goto eerror
DECLARE TEMPCUR CURSOR FOR SELECT ONACBAL FROM TDMASTER WHERE TDNO =  @M_TDNO and company = @m_company
OPEN TEMPCUR
FETCH NEXT FROM TEMPCUR INTO @M_ONACBAL
CLOSE TEMPCUR
DEALLOCATE TEMPCUR
IF @@ERROR <> 0 goto eerror
if @m_onacbal = 0 
BEGIN 
SELECT @SQL = 'DROP TABLE ' +  @M_TABLE
EXEC( @SQL)
RETURN (1)
END
EXEC ('DECLARE TEMPCUR CURSOR FOR SELECT TOT  = SUM (AMT) FROM ' +   @M_TABLE)
OPEN TEMPCUR
FETCH NEXT FROM TEMPCUR INTO @M_TXNAMT
CLOSE TEMPCUR
DEALLOCATE TEMPCUR
EXEC(@SQL)
IF @@ERROR <> 0 goto eerror
if @m_txnamt = 0
BEGIN 
SELECT @SQL = 'DROP TABLE ' +  @M_TABLE
EXEC( @SQL)
RETURN (1)
END
IF @M_TXNAMT > @M_ONACBAL  RAISERROR('ON ACCOUNT BALANCE IS LESS THAN THE SELECTED TOTAL',11,-1)
SELECT @SQL =  'DECLARE TONAC CURSOR  FOR SELECT FDRSNO,AMT FROM  ' +  @M_TABLE
EXEC( @SQL)
IF @@ERROR <> 0 goto eerror
BEGIN TRANSACTION
OPEN TONAC
FETCH NEXT FROM TONAC INTO @M_FDRSNO,@M_AMT
IF @@ERROR <> 0 goto eerror
WHILE @@FETCH_STATUS=0
BEGIN 
--UPDATE FDR SET BALANCE=BALANCE-@M_AMT,CLOSDATE = CASE WHEN BALANCE=0 THEN GETDATE()  ELSE NULL END WHERE COMPANY = @M_COMPANY AND TDNO = @M_TDNO AND FDRSNO = @M_FDRSNO
UPDATE FDR SET BALANCE=BALANCE-@M_AMT  WHERE COMPANY = @M_COMPANY AND TDNO = @M_TDNO AND FDRSNO = @M_FDRSNO
IF @@ERROR <> 0 GOTO EERROR
UPDATE FDR SET  CLOSDATE = CASE WHEN BALANCE=0 THEN GETDATE()  ELSE NULL END WHERE COMPANY = @M_COMPANY AND TDNO = @M_TDNO AND FDRSNO = @M_FDRSNO
IF @@ERROR <> 0 GOTO EERROR
FETCH NEXT FROM TONAC INTO @M_FDRSNO,@M_AMT
END
CLOSE TONAC
DEALLOCATE TONAC
UPDATE TDMASTER SET ONACBAL= ONACBAL - @M_TXNAMT  WHERE TDNO = @M_TDNO  AND COMPANY = @M_COMPANY
IF @@ERROR <> 0 GOTO EERROR
SELECT @SQL = 'DROP TABLE ' +  @M_TABLE
EXEC( @SQL)
IF @@ERROR <> 0 GOTO EERROR
COMMIT TRANSACTION
RETURN (0)
EERROR:
ROLLBACK TRANSACTION
RAISERROR('ON ACCOUNT UPDATION FAILED',16,-1)
SELECT @SQL = 'DROP TABLE ' +  @M_TABLE
EXEC( @SQL)
RETURN (1)



GO
/****** Object:  StoredProcedure [dbo].[ONACREVulta]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.ONACREV    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.ONACREV    Script Date: 1/20/03 11:51:46 AM ******/
/****** Object:  Stored Procedure dbo.ONACREV    Script Date: 10/14/02 1:17:57 PM ******/
CREATE PROCEDURE [dbo].[ONACREVulta]  @M_TABLE VARCHAR(50) , @M_COMPANY AS INT,@M_TDNO INT  
/* with encryption */
AS
DECLARE @SQL VARCHAR(5000)
DECLARE @M_FDRSNO INT
DECLARE @M_AMT MONEY
DECLARE @M_TXNAMT MONEY
DECLARE @M_ONACBAL MONEY
DECLARE @M_NAME VARCHAR(500)
DECLARE @M_XTYPE CHAR(1)
IF @M_TDNO=0
BEGIN 
SELECT @SQL = 'DROP TABLE ' +  @M_TABLE
EXEC( @SQL)
RETURN (1)
END
DECLARE TEMPCUR CURSOR FOR select name,xtype from sysobjects where name =  @M_TABLE   and xtype = 'U'
OPEN TEMPCUR
FETCH NEXT FROM TEMPCUR INTO @M_NAME ,@M_XTYPE
IF @@FETCH_STATUS <> 0
BEGIN
CLOSE TEMPCUR
DEALLOCATE TEMPCUR
GOTO EERROR
END
CLOSE TEMPCUR
DEALLOCATE TEMPCUR
IF @@ERROR <> 0 goto eerror
DECLARE TEMPCUR CURSOR FOR SELECT ONACBAL FROM TDMASTER WHERE TDNO =  @M_TDNO and company = @m_company
OPEN TEMPCUR
FETCH NEXT FROM TEMPCUR INTO @M_ONACBAL
CLOSE TEMPCUR
DEALLOCATE TEMPCUR
IF @@ERROR <> 0 goto eerror
EXEC ('DECLARE TEMPCUR CURSOR FOR SELECT TOT = SUM (AMT) FROM ' +   @M_TABLE)
OPEN TEMPCUR
FETCH NEXT FROM TEMPCUR INTO @M_TXNAMT
CLOSE TEMPCUR
DEALLOCATE TEMPCUR
EXEC(@SQL)
IF @@ERROR <> 0 goto eerror
if @m_txnamt = 0
BEGIN 
SELECT @SQL = 'DROP TABLE ' +  @M_TABLE
EXEC( @SQL)
RETURN (1)
END
SELECT @SQL =  'DECLARE TONAC CURSOR  FOR SELECT FDRSNO,AMT FROM  ' +  @M_TABLE
EXEC( @SQL)
IF @@ERROR <> 0 goto eerror
BEGIN TRANSACTION
OPEN TONAC
FETCH NEXT FROM TONAC INTO @M_FDRSNO,@M_AMT
IF @@ERROR <> 0 goto eerror
WHILE @@FETCH_STATUS=0
BEGIN 
--UPDATE FDR SET BALANCE=BALANCE-@M_AMT,CLOSDATE = CASE WHEN BALANCE=0 THEN GETDATE()  ELSE NULL END WHERE COMPANY = @M_COMPANY AND TDNO = @M_TDNO AND FDRSNO = @M_FDRSNO
UPDATE FDR SET BALANCE=BALANCE+@M_AMT  WHERE COMPANY = @M_COMPANY AND TDNO = @M_TDNO AND FDRSNO = @M_FDRSNO
IF @@ERROR <> 0 GOTO EERROR
--UPDATE FDR SET  CLOSDATE = CASE WHEN BALANCE=0 THEN GETDATE()  ELSE NULL END WHERE COMPANY = @M_COMPANY AND TDNO = @M_TDNO AND FDRSNO = @M_FDRSNO
--IF @@ERROR <> 0 GOTO EERROR
FETCH NEXT FROM TONAC INTO @M_FDRSNO,@M_AMT
END
CLOSE TONAC
DEALLOCATE TONAC
UPDATE TDMASTER SET ONACBAL= ONACBAL + @M_TXNAMT  WHERE TDNO = @M_TDNO  AND COMPANY = @M_COMPANY
IF @@ERROR <> 0 GOTO EERROR
SELECT @SQL = 'DROP TABLE ' +  @M_TABLE
EXEC( @SQL)
IF @@ERROR <> 0 GOTO EERROR
COMMIT TRANSACTION
RETURN (0)
EERROR:
ROLLBACK TRANSACTION
RAISERROR('ON ACCOUNT UPDATION FAILED',16,-1)
SELECT @SQL = 'DROP TABLE ' +  @M_TABLE
EXEC( @SQL)
RETURN (1)



GO
/****** Object:  StoredProcedure [dbo].[onetimeupdateforoutletnumering]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[onetimeupdateforoutletnumering] 
/* with encryption */
AS

--CREATE PROCEDURE onetimeupdateforoutletnumering AS
declare @outlet numeric 
declare @sno numeric
declare @fy1 numeric
declare @txndt datetime
declare @sdt datetime
declare @edt datetime
--set @txndt= getdate()
--exec getfy @txndt,1,@fy1 out
declare cfy cursor for select fy,date from finyear where company=1 order by fy
open cfy
fetch next from cfy into @fy1,@txndt
while @@fetch_status=0
begin

declare c1 cursor for select bno,code from outlet
open c1
fetch next from c1 into @sno,@outlet 
while @@fetch_status=0
begin
if not exists(select * from serviceasno where outlet=@outlet and fy=@fy1)
begin

set @sdt = dbo.getbofy(@txndt)
set @edt = dbo.geteofy(@txndt)
print convert(varchar(20),@sdt) +  ' ' +  convert(varchar(20),@edt)

select @sno=max(sno) from kithist1 where outlet=@outlet and 
datediff(d,'1/1/2000',txndt) between
datediff(d,'1/1/2000',@sdt) and
datediff(d,'1/1/2000',@edt)

insert into serviceasno (outlet,fy,sno) values (@outlet,@fy1,isnull(@sno,0))

end

fetch next from c1 into @sno,@outlet 
end
close c1
deallocate c1

fetch next from cfy into @fy1,@txndt
end
close cfy
deallocate cfy




GO
/****** Object:  StoredProcedure [dbo].[Online24Posting]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author:	 PowerBrain Software (India)
-- Create date: 14 June 2012
-- Description:	For 24 Online Posting Request
-- =============================================
CREATE PROCEDURE [dbo].[Online24Posting]
@rno numeric,@SeqNo as integer,@amt numeric(18,2),@txndt datetime
/* with encryption */
AS
BEGIN
declare @gno int
declare @outlet int
declare @sql varchar(6000)
declare @rv varchar(100)
declare @t1 varchar(100)

EXEC RANDFILE @RV OUT
select @t1 = 'INET' + ltrim(@RV)
SET @SQL = 'SELECT * INTO ' + @T1 + '  FROM  DUMMYph'
EXEC (@SQL)

select @outlet = inetoutlet from setting 

select @gno-gno from room where rno=@rno

    -- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
insert into ol24 (gno,amount,seqno,txndt) values 
(@gno,@amt,@seqno,@txndt)

if @amt <> 0 
begin
	set @sql = 'insert into ' + @t1 + ' (detail,amount) values (''WiFi Usage Charges'', ' + convert(varchar(20),@amt) + ')'
	exec(@sql)
	set @sql =  'declare @ocno int exec pbpp1u  ''' + @t1 + ''' ,' + str(@outlet) + ',''' + convert(varchar(20),getdate()) + ''',2,0, ' + str(@gno) + ','''','''','''',0, @ocno out'
	exec(@sql)
end

END




GO
/****** Object:  StoredProcedure [dbo].[OTACommEntry]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Rm.Ramanathan
-- Create date: 14 July 2019
-- Description:	OTA commision Module (created for Golden Grand)
-- =============================================
CREATE PROCEDURE [dbo].[OTACommEntry] 
@commrate money,@gstrate money,@tcsrate  money,@tdsrate money,@commamt  money,@gstamt  money,@tcsamt  money,@tdsamt money,@voucherno varchar(100), @user varchar(100) ,@ctrlno int out
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	set @ctrlno=99
insert into otacommission 
(voucherno,entrydate,commission,gst,tcs,tdsamt,commrate,gstrate,tcsrate,tdsrate,status,usernm)	values 
(@voucherno,getdate(),@commamt,@gstamt,@tcsamt,@tdsamt,@commrate,@gstrate,@tcsrate,@tdsrate,'',@user) 
select @ctrlno=@@IDENTITY 
return @ctrlno
END

GO
/****** Object:  StoredProcedure [dbo].[pbpa1106]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.  Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[pbpa1106] @CNO INT,@NAMT MONEY,@MOP INT,@CRNUM CHAR(20),@CRNAME VARCHAR(50),@CRTYPE CHAR(20) ,@GLACTNUM INT,@gno numeric,@narration varchar(200),@newtxndt datetime = '1/1/2000'
/* with encryption */
AS
--DECLARE @GNO INT
DECLARE @COMPANY INT
DECLARE @TXNDT DATETIME
DECLARE @GRNO INT
DECLARE @FY INT
DECLARE @RET_VAL INT
DECLARE @AMOUNT MONEY
DECLARE @RNO INT
DECLARE @SPLIT CHAR(1)
DECLARE @ACTNUM INT
DECLARE @PARTIC varCHAR(500)
DECLARE @ADVRNO INT
declare @oldmop int
declare @oldglactnum numeric
declare @eno numeric
declare @errmsg varchar(2000)
set @errmsg = 'Error in Advance Editing'
BEGIN TRANSACTION
SELECT @oldmop=mop,@oldglactnum=glactnum,@ADVRNO=ANO,@SPLIT=SPLIT,@GRNO=GRNO,@COMPANY=COMPANY,@TXNDT=TXNDT,@AMOUNT=AMOUNT FROM ADVANCE WHERE CNO=@CNO
if @oldmop=4 and @oldglactnum=56 begin
set @errmsg = 'This advance receipt is auto generated out of the Advance amount received at the time of Reservations' + nchar(13) + 'So editing not allowed'
goto e_error
end

if datediff(d,@newtxndt,'1/1/2000')<>0 
begin
	if exists (select * from guest where gno=@gno and co_date is not null)
	   if exists (select * from guest where gno=@gno and datediff(d,'1/1/2000',@newtxndt)  
		not between datediff(d,'1/1/2000',ci_date) and datediff(d,'1/1/2000',co_date) )
		begin
			set @errmsg = CONVERT(VARCHAR(20),@NEWTXNDT) + 'The Advance date does not fall within the stay period of the Guest' + char(13) + 'Modifications Aborted'
			goto e_error
		end

	if exists (select * from guest where gno=@gno and co_date is not null)
	   if exists (select * from guest where gno=@gno and datediff(d,@newtxndt,ci_date) > 0)
		begin 
			set @errmsg =  'The advance date falls befor check in date' + char(13) + 'Editing Aboted'
			goto e_error
		end
end

EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
EXEC DELGRNO @COMPANY,@FY,@GRNO,@TXNDT,@RET_VAL OUT
IF @@ERROR <> 0 GOTO E_ERROR
--DELETE FROM ADVANCE WHERE CNO= @CNO

if datediff(d,@newtxndt,'1/1/2000')<>0 
    begin
		set @txndt= @newtxndt
		EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
		EXEC  GETGRP @TXNDT,@COMPANY,@GRNO OUT
		IF @GRNO=0 GOTO E_ERROR
	end
	--set @errmsg = convert(varchar(20),@txndt) goto e_error

UPDATE advance SET txndt=@txndt,grno=@grno,mop = @MOP ,crcardnum=@CRNUM,crcardname=@CRNAME,crcardbank=@CRTYPE,amount=@NAMT,glactnum = @GLACTNUM,gno=@gno WHERE CNO=@CNO

if exists(select * from setting where g_finaccount=0) goto final

select @eno=isnull(eno,0),@rno=rno from guest where gno=@gno
--SELECT @RNO=RNO FROM ROOM WHERE GNO=@GNO
if @@error <> 0 goto e_error
select @actnum= case 
  when @mop=1 then 1
  when @mop=2 then 22
  when @mop=3 then 46
  when @mop=4 then @glactnum
                                     end

declare @details varchar(500)
set @details=''
if ltrim(rtrim(@narration)) <>' ' select @details = ' Txn. Details : ' + ltrim(rtrim(@narration))
set @details = isnull(@details,'')
set @partic = 'Adv.R.No.' + ltrim(str(@advrno)) + ' Room No.' + ltrim(str(@rno)) + ' GR No.:' + ltrim(str(@eno)) + ' Sys.No.:' + ltrim(str(@gno)) + @details
set @partic = case
  when @mop=1 then @partic
  when @mop=2 then @partic +' ' +  rtrim(ltrim(@crnum)) + ' ' + rtrim(ltrim(@crname)) + ' ' + rtrim(ltrim(@crTYPE))
  when @mop=3 then @partic + ' ' + rtrim(ltrim(@crnum)) + ' ' + rtrim(ltrim(@crname)) + ' ' + rtrim(ltrim(@crTYPE))
  when @mop=4 then @partic + ' ' + rtrim(ltrim(@crname))
  end
if @company=1 if @split = 'Y'  set @partic = @partic + ' Split Bill'  else  set @partic = @partic + ' Main Bill'

insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
 values (@company, @fy,  @actnum ,@Namt , 'To ' + @partic  , 'D', @txndt , @grno ,'Y', @advrno,'ADV')
if @@error <> 0 goto e_error
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
 values (@company, @fy,  50 ,@Namt ,'By ' + @partic , 'C', @txndt , @grno ,'Y', @advrno,'ADV')
if @@error <> 0 goto e_error
declare @guestname varchar(50)
select @eno=eno,@guestname=name from guest_view1 where gno =@gno
declare @usernm varchar(50)
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm ,'Advance Receipt Edited - Guest Name ' + rtrim(@guestname) + ' Entry Number ' + ltrim(str(@eno)) +  ' Old  Amount - ' + convert(varchar(20),@amount)+ ' New  Amount - ' + convert(varchar(20),@namt)+ ' Advance Receipt No. ' + ltrim(str(@advrno))+ ' dt ' + convert(varchar(20),@txndt,6)  ,10,@gno)
IF @@ERROR <> 0 GOTO E_ERROR
final:
COMMIT TRANSACTION
RETURN
E_ERROR:
ROLLBACK TRANSACTION
RAISERROR(@errmsg,16,-1)
RETURN




GO
/****** Object:  StoredProcedure [dbo].[pbpa1508]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.   Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[pbpa1508]  @CNO INT,@usernm varchar(50)='SYS',@userno numeric=0,@delres int=0   
/* with encryption */
AS
DECLARE @GNO INT
DECLARE @COMPANY INT
DECLARE @TXNDT DATETIME
DECLARE @GRNO INT
DECLARE @FY INT
DECLARE @RET_VAL INT
DECLARE @AMOUNT MONEY
declare @glactnum numeric
declare @guestname varchar(50)
declare @ano int
declare @eno int
declare @split char(1)
declare @t1 varchar(50)
declare @t2 varchar(50)
declare @foodbill money
declare @spldisc money
declare @crname varchar(50)
declare @crnum varchar(20)
declare @crtype varchar(50)
declare @glactno numeric
declare @mop int
declare @bcompany numeric
declare @mainbilldate datetime
declare @sql varchar(5000)
declare @rv varchar(10)
--
declare @rent money
declare @discount money
declare @item1 money
declare @item2 money
declare @item3 money
declare @item4 money
declare @item5 money
declare @item6 money
declare @item7 money
declare @item8 money
declare @item9 money
declare @item10 money
declare @outlet1 int
declare @outlet2 int
declare @outlet3 int
declare @outlet4 int
declare @outlet5 int
declare @outlet6 int
declare @outlet7 int
declare @outlet8 int
declare @outlet9 int
declare @outlet10 int
declare @sc money
declare @lt money
declare @total money
declare @taxable money
declare @section char(1)
declare @brno numeric
BEGIN TRANSACTION
if not exists(select * from advance where cno=@cno) begin Print 'The Advance Receipt is not existing now' goto e_error end
select @mop=mop,@glactnum=glactnum,@split=split,@ano=ano,@GNO=GNO,@GRNO=GRNO,@COMPANY=COMPANY,@TXNDT=TXNDT,@AMOUNT=AMOUNT FROM ADVANCE WHERE CNO=@CNO
--print str(@delres) + ' delres' goto e_error
/*
if @mop=4 and @glactnum=56 and @delres=0 begin
   print 'This Advance Receipt is auto generated out of the Advance Amount Received at the time of Reservations' + nchar(13) + 'So deletion not allowed'
   goto e_error
end
*/
set @bcompany=@company
if exists(select * from guest where gno=@gno and co_date is not null)
  begin
   if not exists(select * from servicepermission where userno=@userno and serviceno=203 and outlettype=10 and permission='Y')
   begin
   print 'This guest is already checked-out ...' + nchar(13) +  'You are not permitted to delete the advance Receipts after the guest checked out'
   goto e_error
   end
 end
EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
IF @@ERROR <> 0 GOTO E_ERROR
EXEC DELGRNO @COMPANY,@FY,@GRNO,@TXNDT,@RET_VAL OUT
IF @@ERROR <> 0 GOTO E_ERROR
DELETE FROM ADVANCE WHERE CNO= @CNO
IF @@ERROR <> 0 GOTO E_ERROR

if @mop=4 and @glactnum=56 and @delres=0 
begin
	declare @reserve_ref numeric
	declare @ctrlno numeric
	select @reserve_ref=reserve_ref from guest where gno=@gno
	if @reserve_ref<>0
	begin
		select @ctrlno=ctrlno from sptbrs1 where myctrlno=@reserve_ref
		update sptbrs set advanceadjd=advanceadjd-isnull(@amount,0) where ctrlno=@ctrlno
		if @@error <> 0 goto e_error
		update guest set reserve_adv=0,reserve_adv_cno=0 where gno=@gno
	end
end
declare @aftercheckout int
set @aftercheckout = 0
if exists(select * from guest where gno=@gno and co_date is not null)
begin

	set @aftercheckout=1
/*
	exec randfile @rv out
	select @t1 = 'adv1' + ltrim(@RV)
	set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'') drop table ' + @T1
	exec(@sql)
	if @@error<>0 goto e_error
	set @t2 = 'adv2' + ltrim(@RV)
	set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T2 +''' and xtype = ''U'') drop table ' + @T2
	exec(@sql)
	if @@error<>0 goto e_error
	set @sql = 'select * into ' + @T2 + ' from dummypymt'
	exec(@sql)
	if @@error<>0 goto e_error
*/
	select @brno=brno from guest where gno=@gno

	select @grno=grno,@cno=cno,@mainbilldate=txndt  from billhist1 where company=@company and brno=@brno and split=@split

/*
	exec delgrno @bcompany,@fy,@grno,@mainbilldate,@ret_val out
	if @split='Y'
	   set @section = 'B'
	else
	   set @section = 'A'
	SET @SQL = 'SELECT * INTO ' + @T1 + ' FROM BILLHIST WHERE CNO=' + STR(@CNO)
	EXEC (@SQL)
	SET @SQL = 'UPDATE ' + @T1 + ' SET SECTION=''' + @SPLIT + ''' , BILLORDER = ' + STR(@BCOMPANY)
	EXEC(@SQL)
*/
	update billhist1 set advance=advance-@amount where cno=@cno
/*
	select @foodbill=foodbill,@spldisc=spldiscount,@crname=crname,@crnum=crnum,@crtype=crtype,@glactno=glactno,@mop=mop,@split=split,@bcompany=company from billhist1 where cno=@cno
	set @sql = 'insert into ' + @t2 + '  (company,crname,crnum,crtype,glactno,mop,section,spldiscount,foodbill) VALUES (' + 
	str(@bcompany)+  ',''' + @crname + ''',''' + @crnum + ''',''' + @crtype + ''',' + STR(@glactno) + ',' + STR(@mop) + ', '''+ @split + ''',' + convert(varchar(20),@spldisc)+ ',' + convert(varchar(20),@foodbill)+  ' )'
	exec (@sql)
	if @@error <> 0  goto e_error
	exec pbpc1o  @t1,@t2,@gno,@mainbilldate,@CNO,0,1,0
	if @@error <> 0  goto e_error
*/
	exec pbvc @cno
end

select @eno=eno,@guestname=name from guest_view1 where gno =@gno
--select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

if @aftercheckout=1 
   set @sql = ' After Check out'
else
  set @sql = ''
insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm,'Advance Receipt Deleted - Guest Name ' + rtrim(@guestname) + ' Entry Number ' + ltrim(str(@eno)) +  ' Amount - ' + convert(varchar(20),@amount)+' Advance Receipt No. ' + ltrim(str(@ano))+ ' dt ' + convert(varchar(20),@txndt,6) +'-'+ @sql ,10,@gno)
if @@error <> 0  goto e_error
set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'') drop table ' + @T1
exec(@sql)
if @@error<>0 goto e_error
set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T2 +''' and xtype = ''U'') drop table ' + @T2
exec(@sql)
if @@error<>0 goto e_error
COMMIT TRANSACTION
RETURN
E_ERROR:
ROLLBACK TRANSACTION
set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'') drop table ' + @T1
exec(@sql)
if @@error<>0 goto e_error
set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T2 +''' and xtype = ''U'') drop table ' + @T2
exec(@sql)
if @@error<>0 goto e_error
RAISERROR('PROBLEM IN DELETING ADVANCE RECEIPT ENTRY',16,-1)
RETURN



GO
/****** Object:  StoredProcedure [dbo].[pbpa1u]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:01 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:48 AM ******/
CREATE PROCEDURE [dbo].[pbpa1u] @gno int,@amt money,@company int,@mop int,
@crname varchar(50),@crnum varchar(20),@crbank varchar(20),@glactnum int , @split char(1),@ctrlno int out,@narration varchar(100)
/* with encryption */
as
declare @ano int
declare @m_year int
declare @m_bofy  datetime
declare @txndt datetime
declare @advrno int
declare @fy int
declare @grno int
declare @sql varchar(500)
declare @rno int
declare @actnum int
declare @partic varchar(200)
declare @usernm varchar(50)
declare @eno numeric
declare @emsg as varchar(1000)
begin transaction
if @amt=0 
begin
set @emsg =  'Amount can not be Zero'
goto e_error
end
if not exists(select * from room where gno = @gno)
begin
set @emsg = 'The Guest is now vaccated'
 goto e_error
end
if exists (select * from room where gno=@gno and hostid<>'') 
  begin 
         set @emsg = 'The Room is under check out process by some else...No transaction allowed at this stage'
         goto e_error
  end
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
if isnull(@usernm,'')='' begin print 'You are not logged in' goto e_error end
select @txndt = convert(varchar(20),getdate(),109)
select @rno=rno from room where gno = @gno

set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@txndt)

--print str(@company) goto e_error
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
EXEC  GETGRP @TXNDT,@COMPANY,@GRNO OUT
IF @GRNO=0 GOTO E_ERROR
update company set advrno = case when advrdt <> @m_bofy then 1 else advrno + 1 end ,advrdt = case  when advrdt <> @m_bofy then @m_bofy else advrdt end where company = @company
if @@error <> 0 goto e_error
select @advrno=advrno from company where company=@company
select @eno=isnull(eno,0) from guest where gno=@gno
insert into advance (gno,txndt,company,ano,mop,crcardnum,crcardname,crcardbank,grno,amount,glactnum,split,usernm,narration) values (@gno,getdate(),@company,@advrno,@mop,@crnum,@crname,@crbank,@grno,@amt,@glactnum,@split,@usernm,@narration)
if @@error <> 0 goto e_error
select @ctrlno=@@identity
if exists(select * from setting where g_finaccount=0) goto final

select @actnum= case 
  when @mop=1 then 1
  when @mop=2 then 22
  when @mop=3 then 46
  when @mop=4 then @glactnum
end

declare @details varchar(500)
set @details=''
if ltrim(rtrim(@narration)) <>' ' select @details = ltrim(rtrim(@narration))
set @details = isnull(@details,'')
set @partic =  @details + ' Adv.R.No.' + ltrim(str(@advrno)) + ' Room No.' + ltrim(str(@rno)) + ' GR No.:' + ltrim(str(@eno)) + ' Sys.No.: ' + ltrim(str(@gno))
set @partic = case
  when @mop=1 then @partic
  when @mop=2 then @partic +' ' +  rtrim(ltrim(@crnum)) + ' ' + rtrim(ltrim(@crname)) + ' ' + rtrim(ltrim(@crbank))
  when @mop=3 then @partic + ' ' + rtrim(ltrim(@crnum)) + ' ' + rtrim(ltrim(@crname)) + ' ' + rtrim(ltrim(@crbank))
  when @mop=4 then @partic + ' ' + rtrim(ltrim(@crname))
  end
if @company=1 if @split = 'Y'  set @partic = @partic + ' Split Bill'  else  set @partic = @partic + ' Main Bill'

insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
 values (@company, @fy,  @actnum ,@amt , 'To ' + substring(@partic,1,96)  , 'D', @txndt , @grno ,'Y', @advrno,'ADV')
if @@error <> 0 goto e_error
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
 values (@company, @fy,  50 ,@amt ,'By ' + rtrim(substring(@partic,1,96))  , 'C', @txndt , @grno ,'Y', @advrno,'ADV')
if @@error <> 0 goto e_error
final:
commit transaction
return @ctrlno
e_error:
rollback transaction
raiserror(@emsg ,16,-1)
return 0


GO
/****** Object:  StoredProcedure [dbo].[pbpc1i]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
CREATE procedure [dbo].[pbpc1i]  @m_rno int,@opt int,@name varchar(50),@add1 varchar(50),@add2 varchar(50),
@add3 varchar(50),@city varchar(50),@state varchar(50),@pin char(10),@phone varchar(50),
@fax varchar(50),@email varchar(50),@rent money ,@discount money,@sc money,
@company int,@m_cno int,@ci_date datetime,@male int,@female int,@child int,
@foreign int,@address char(5),@eno numeric,
@foodplan int ,@temptable1 varchar(50),@temptable2 varchar(50),@temptable3 varchar(50),@expecteddep datetime,
@grpname varchar(30)='' ,@addad char(1)=' ',@erasetemp char(1) = ' ',@reserved numeric=0,@resadv money=0,@ltcal char(1)='Y',
@fp_desc varchar(100)='',@arrivedfrom varchar(50)=' ',@destination varchar(50)=''
/* with necryption */
as
declare @msgno numeric
declare @m_gno numeric
declare @sql varchar(5000)
declare @oc_name varchar(50)
declare @oc_father varchar(50)
declare @oc_sex char(1)
declare @oc_age int
declare @oc_job varchar(50)
declare @pp_no varchar(50)
declare @pp_issuedate datetime
declare @pp_issueplace varchar(50)
declare @nationality varchar(50)
declare @outlet int
declare @chtype int

declare @doa datetime
declare @port varchar(50)
declare @employed char(1)
declare @stayduration int
declare @regnno varchar(50)
declare @regndate datetime
declare @regnoffice varchar(50)
declare @purpose varchar(50)
declare @ctrlno1 numeric
declare @fptype tinyint
declare @lttype tinyint
declare @wingno tinyint
declare @rent1 money
declare @rent2 money
declare @defnationality varchar(50)
declare @g_autoguestad int
declare @g_autoguestset int
declare @bnoiseno int
declare @bnogen int
declare @bno numeric
declare @m_year int
declare @m_bofy datetime
declare @stopservice tinyint


begin transaction

if isnull(@company,0) <> 0 
select @stopservice=stopservice from gcompany where comno=@company 

set @stopservice=isnull(@stopservice,0)

select top 1 @bnogen=g_bno_generation,
@defnationality=G_DefaultNationality,@g_autoguestad=g_autoguestad,@g_autoguestset=g_autoguestset,@bnoiseno=bnoiseno from setting
if @bnogen=1 
	if @bnoiseno=1 
		if @eno=0
			begin
				print 'The entry number can not be 0 as it is taken as the bill number'  
                                                   goto e_error
			end

if not exists(select * from hmsfolder where version_no = (select version_no from comuser where computer=host_name()) ) 
  begin 
  print object_name(@@PROCID)  + 'You are using a wrong version of the Softwre Installed...Please update the version and try'
  goto e_error
end

if ltrim(@arrivedfrom) = '' goto e_error

if exists(select * from room where rno=@m_rno and gno<>0)
begin
	print 'selected room is aleary checked in'
	goto e_error
end

if exists(select * from sptbrooms_v  where rno=@m_rno and status1<>'Y'
and (
(datediff(s,'1/1/90',@expecteddep)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)  or
datediff(s,'1/1/90',@ci_date)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)) or 
(datediff(s,'1/1/90',@ci_date) < datediff(s,'1/1/90',departuredate) and datediff(s,'1/1/90',@expecteddep) >= datediff(s,'1/1/90',departuredate))
))
begin
print  'This room (' + ltrim(str(@m_rno)) + ') is blocked for the period during which the expected departure date of this guest falls ' + char(13) + 'Check - in aborted - This is a SQL server messsage ' + convert(varchar(20),@expecteddep) + '  ' +  convert(varchar(20),@ci_date)
goto e_error
end
select @wingno=roomwing from room where rno=@m_rno
set @rent1= case when @male+@female = 1 then @rent  else 0 end
set @rent2=case when @male+@female > 1 then @rent else 0 end
if @foreign=1
	set @nationality='Foreigner'
else
	set @nationality=@defnationality

if @opt=0 or @opt=2
begin
	if @g_autoguestad=1
	begin
		insert into guestad([name],add1,add2,city,state,pin,phone,fax,email,address,company,rent,discount,sc,nationality)
		values
		(@name,@add1,@add2,@city,@state,@pin,@phone,@fax,@email,@address,@company,@rent,@discount,@sc,@nationality)
		if @@error<>0
		begin
			print 'updation in guestad failed'
			goto e_error
		end
		select @m_cno=max(cno) from guestad
	end
end

if @opt=1
begin
	update guestad set lastvisit=@ci_date,rent=@rent where cno=@m_cno
	if @@error<>0
	begin
		print 'updation in guestad failed'
		goto e_error
	end
end

if @m_cno<>0
	if @g_autoguestset=1
		if not exists(select * from guestset where cno=@m_cno and roomtype=@wingno and fptype=@foodplan)
			insert into guestset (cno,roomtype,lttype,rent1,rent2,discount,sc,fptype,renttype) values (@m_cno,@wingno,0,@rent1,@rent2,@discount,@sc,@foodplan,1)
		else
			begin
			update guestset set lastvisit=getdate()
			if exists(select * from guestset where cno=@m_cno and roomtype=@wingno and fptype=@foodplan and rent1=0)
			update guestset set rent1=@rent1 where cno=@m_cno and roomtype=@wingno and fptype=@foodplan
			
			if exists(select * from guestset where cno=@m_cno and roomtype=@wingno and rent2=0 and fptype=@foodplan)
			update guestset set rent2=@rent2 where cno=@m_cno and roomtype=@wingno and fptype=@foodplan
			end

--PRINT 'PERIYA KARUPPU'
-----------
declare @usernm varchar(50)
select @usernm=usernm from comuser where computer=host_name() and sessionid=''

insert guest (cno,ci_date,rno,eno,foodplan,rent,discount,sc,male,female,child,ci_time,grpname,expecteddep,userchkin,reserve_ref,
reserve_adv,lt,company,fp_details,arrivedfrom,destination,address,name,add1,add2,add3,city,state,phone,email,nationality,STOPSERVICE)
values (@m_cno,@ci_date,@m_rno,@eno,@foodplan,@rent,@discount,
@sc,@male,@female,@child,substring(convert(varchar(20),@ci_date),13,7),@grpname,@expecteddep,@usernm,@reserved,@resadv,@ltcal,
isnull(@company,0),@fp_desc,@arrivedfrom,@destination,@address,@name,@add1,@add2,@add3,@city,@state,@phone,@email,@nationality,@stopservice)
if @@error <> 0
begin
	print 'updation in guest failed'
	goto e_error
end
if @eno=0  --new on 07 March 2017 - for Sukhmani Guwahati
	if exists(select * from setting where enoisgno=1)
		begin
			set @eno=@m_gno
			update guest set eno=gno where gno=@m_gno
		end



select @m_gno=ident_current('guest')
update room set gno=@m_gno,grpname=@grpname where rno = @m_rno
if @@error <> 0
begin
	print 'updation in room table failed'
	goto e_error
end

insert groom (gno,ci_date,rno,rent) values (@m_gno,@ci_date,@m_rno,@rent)
if @@error <> 0
begin
set @msgno = 50007
goto e_error
end

------------
if @foreign=0
begin
	select @sql = 'declare  temp1 cursor for select [name],father,sex,age,job from ' +  @temptable1 
	execute( @sql)
	open temp1
	fetch next from temp1 into @oc_name,@oc_father,@oc_sex,@oc_age,@oc_job
	while @@fetch_status=0
		begin
		insert goccupants (gno,[name],father,sex,age,job) values (@m_gno,@oc_name,@oc_father,@oc_sex,@oc_age,@oc_job)
		if @@error <> 0
			begin
			print 'goccupants updation failed'
			goto e_error
			end
		fetch next from temp1 into @oc_name,@oc_father,@oc_sex,@oc_age,@oc_job
		end
	close temp1
	deallocate temp1
end

---------------
if @foreign=1
	begin
	select @sql = 'declare  temp1 cursor for select [name],pp_no,pp_issuedate,pp_issueplace,nationality, doa,port,employed,stayduration,regnno,regndate,regnoffice,purpose   from ' +  @temptable2 
	execute( @sql)
	open temp1
	fetch next from temp1 into @oc_name,@pp_no,@pp_issuedate,@pp_issueplace,@nationality,@doa, @port, @employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose
	while @@fetch_status=0
	begin
		if ltrim(@regnno)=''  set @regndate=null
		if upper(@nationality)<>'INDIAN'
		begin
			insert gpassport (gno,[name],pp_no,pp_issuedate,pp_issueplace,nationality,doa,port,employed,stayduration,regnno,regndate,regnoffice,purpose,edate) values (@m_gno,@oc_name,@pp_no,@pp_issuedate,@pp_issueplace,@nationality,@doa,@port,@employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose,@ci_date)
			if @@error <> 0
				begin
				--set @msgno=50009
				print 'pp updation failed'
				goto e_error
				end
		end
		fetch next from temp1 into @oc_name,@pp_no,@pp_issuedate,@pp_issueplace,@nationality,@doa, @port, @employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose
	end
	close temp1
	deallocate temp1
end

select @sql = 'declare  temp1 cursor for select outlet,chtype from ' +  @temptable3
execute( @sql)
open temp1
fetch next from temp1 into @outlet,@chtype
while @@fetch_status=0
begin
insert gsplit (gno,outlet,chtype) values (@m_gno,@outlet,@chtype)
if @@error <> 0
begin
print 'split updation failed'
goto e_error
end
fetch next from temp1 into @outlet,@chtype
end
close temp1
deallocate temp1
-----------
if @bnogen=1
	if @bnoiseno=1 
		begin
			update guest set bno=eno where gno=@m_gno
		end
	else
		begin
			--if datepart(mm,@ci_date) >=  1 and datepart(mm,@ci_date) <= 3
			--	set @m_year = datepart(yy,@ci_date)-1
			--else
			--	set @m_year = datepart(yy,@ci_date)
			--select @m_bofy = '1 apr ' + str(@m_year)
			select @m_bofy = dbo.getfy(@ci_date)
			update company set bno=case when datediff(d,bdt,@m_bofy)<>0 then 1 else bno+1 end , bdt= case  when datediff(d,bdt,@m_bofy)<>0 then @m_bofy else bdt end where company = 1
			if @@error<>0
				begin 
				print 'bno generation failed'
				goto e_error
				end
			select @bno=bno from company where company= 1
			update guest set bno=@bno where gno=@m_gno
			if @@error <> 0
				begin
				print 'billhist1 updation failed'
				goto e_error
				end
		end
----------------------
declare @ctrlno numeric
if @reserved <> 0 
	begin
	update sptbrs1 set arrivedrooms=arrivedrooms+1 where myctrlno=@reserved
	if @@error<>0 begin print 'Error in updating the arrived rooms ' + ltrim(str(@reserved)) goto e_error end
	if exists(select * from sptbrooms where rno=@m_rno and myctrlno=@reserved)
	begin
		update sptbrooms set status1='Y' where rno=@m_rno and myctrlno=@reserved
		if @@error <>0 goto e_error
	end
if @resadv<>0
	begin
		select @ctrlno=ctrlno from sptbrs1 where myctrlno=@reserved
		update sptbrs set advanceadjd=advanceadjd+@resadv where ctrlno=@ctrlno
		if @@error<>0 begin print 'Error in updateting the advance adjusted' goto e_error end
		declare @narration varchar(100)
		set @narration = 'Against Reservation No ' + ltrim(str(@ctrlno))
		exec pbpa1u @m_gno,@resadv,1,4,'','','',56,'N',@ctrlno1 out , @narration
		if @@error<>0 begin print 'Error in advance updating'  goto e_error end
		update guest set reserve_adv_cno=@ctrlno1 where gno=@m_gno
		if @@error<>0 begin print 'Error in advance ctrlno updation' goto e_error end
	end
end
commit transaction
goto final
e_error:
rollback transaction
raiserror  ('checkin failed' , 16 ,1)
goto final
final:
if @erasetemp=' '
begin
select @sql = 'drop table ' + @temptable1
exec(@sql)
select @sql = 'drop table ' + @temptable2
exec(@sql)
select @sql = 'drop table ' + @temptable3
exec(@sql)
end



GO
/****** Object:  StoredProcedure [dbo].[pbpc1iq]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbpc1iq]  @M_RNO INT,@OPT INT,@NAME VARCHAR(50),@ADD1 VARCHAR(50),@ADD2 VARCHAR(50),
@ADD3 VARCHAR(50),@CITY VARCHAR(50),@STATE VARCHAR(50),@PIN CHAR(10),@PHONE VARCHAR(50),
@FAX VARCHAR(50),@EMAIL VARCHAR(50),@RENT MONEY ,@DISCOUNT MONEY,@SC MONEY,
@COMPANY INT,@M_CNO INT,@CI_DATE DATETIME,@MALE INT,@FEMALE INT,@CHILD INT,
@FOREIGN varchar(50),@ADDRESS CHAR(5),@ENO INT,
@foodplan int ,@TEMPTABLE1 VARCHAR(50),@TEMPTABLE2 VARCHAR(50),@TEMPTABLE3 VARCHAR(50),@expecteddep datetime,
@grpname varchar(30)='' ,@addad char(1)=' ',@erasetemp char(1) = ' ',@reserved numeric=0,@resadv money=0,@ltcal char(1)='Y',
@fp_desc varchar(100)='',@arrivedfrom varchar(50)=' ',@destination varchar(50)='',@autobill char(1)='N',@autoamt money=0, @autocode numeric=0,@autoqty numeric=0,@memo varchar(200)='',
@voucherno varchar(50)='',@idtype varchar(50)='',@idno varchar(50)='',@carno varchar(50)='',@allinclusive char(1)='N',@renttype numeric =1,
@lttype int, @NettRent money,@NREXST int=0,@stopextra tinyint=0,@mktmgr numeric=1,@stcal tinyint = 0,
@dobm tinyint=0,@dobd tinyint=0,@domm tinyint=0,@domd tinyint=0,@extrabedcharges money=0,
@gstin nvarchar(50)='',@extrabed money=0,@food money=0,@commctrlno numeric=0
/* with encryption */
AS
declare @msg varchar(200)
DECLARE @MSGNO INT
DECLARE @M_GNO INT
DECLARE @SQL VARCHAR(5000)
DECLARE @OC_NAME VARCHAR(50)
DECLARE @OC_FATHER VARCHAR(50)
DECLARE @OC_SEX CHAR(1)
DECLARE @OC_AGE INT
DECLARE @OC_JOB VARCHAR(50)
DECLARE @PP_NO VARCHAR(50)
DECLARE @PP_ISSUEDATE DATETIME
DECLARE @PP_ISSUEPLACE VARCHAR(50)
DECLARE @NATIONALITY VARCHAR(50)
DECLARE @OUTLET INT
DECLARE @CHTYPE INT
declare @doa datetime
declare @port varchar(50)
declare @employed char(1)
declare @stayduration int
declare @regnno varchar(50)
declare @regndate datetime
declare @regnoffice varchar(50)
declare @purpose varchar(50)
declare @vno varchar(100)
declare @vip varchar(100)
declare @vid datetime
declare @vvd datetime
declare @pped datetime
declare @lpsi varchar(100)
declare @dob datetime
declare @place2p varchar(200)
declare @country2p varchar(100)
declare @phoneno varchar(100)
declare @email_pp varchar(100)
declare @sex char(1)
declare @parentage varchar(100)
declare @gno numeric
declare @wingno int
declare @rent1 money
declare @rent2 money
declare @fptype tinyint
declare @reserve_ref numeric
declare @defnationality varchar(50)
declare @g_autoguestad int
declare @g_autoguestset int
declare @bnogen int
declare @bno int
declare @m_year int
declare @m_bofy datetime
declare @bnoiseno int
declare @nationalitybybirth varchar(50)
declare @surname varchar(50)
declare @arrivedfromport varchar(50)
declare @addressinindia varchar(250)
declare @nativeaddress varchar(250)
declare @vtype varchar(50)
declare @visalifetime tinyint
declare @imagectrlno numeric
declare @stopservice tinyint
BEGIN TRANSACTION


if isnull(@company,0) <> 0 
select @stopservice=stopservice from gcompany where comno=@company 

set @stopservice=isnull(@stopservice,0)




select top 1 @bnoiseno=bnoiseno,@bnogen=G_BNO_GENERATION,@defnationality=g_defaultnationality,@g_autoguestad=g_autoguestad,@g_autoguestset=g_autoguestset  from setting
select @m_gno=gno,@gno=gno,@grpname=grpname,@wingno=roomwing from room where rno=@m_rno
set @rent1= case when @male+@female = 1 then @rent  else 0 end
set @rent2=case when @male+@female > 1 then @rent else 0 end
set @m_gno=@gno
if not exists(select * from hmsfolder where version_no = (select top 1 version_no from comuser where [host_id]=host_id() and sessionid='') ) 
  begin 
  print 'You are using a wrong version of the Softwre Installed...Please update the version (4) and try'
  goto e_error
end
if ltrim(@arrivedfrom) = '' begin print 'Arrived From Information is required '  goto e_error end
if exists(select * from room where rno=@m_rno and gno=0) begin print 'Selected Room is not Checked in' goto e_error end

select @reserve_ref=isnull(reserve_ref,0) from guest where gno=@gno
set @reserved = @reserve_ref
if exists(select * from sptbrooms_v  where rno=@m_rno and datediff(d,arrivaldate,getdate()) <= 0 and status1<>'Y'
and (
(datediff(s,'1/1/90',@expecteddep)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)  or
datediff(s,'1/1/90',@ci_date)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)) or 
(datediff(s,'1/1/90',@ci_date) < datediff(s,'1/1/90',departuredate) and datediff(s,'1/1/90',@expecteddep) >= datediff(s,'1/1/90',departuredate))
) and myctrlno<> @reserve_ref)
begin
declare @ctrl numeric
select @ctrl=ctrlno_o from sptbrooms_v  where rno=@m_rno and datediff(d,arrivaldate,getdate()) <= 0 and status1<>'Y'
and (
(datediff(s,'1/1/90',@expecteddep)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)  or
datediff(s,'1/1/90',@ci_date)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)) or 
(datediff(s,'1/1/90',@ci_date) < datediff(s,'1/1/90',departuredate) and datediff(s,'1/1/90',@expecteddep) >= datediff(s,'1/1/90',departuredate))
)
print  'This room (' + ltrim(str(@m_rno)) + ') is blocked for the period during which the expected departure date of this guest falls' + char(13) + 'Check - in aborted - This is a SQL server messsage ' + convert(varchar(20),@expecteddep) + '  ' +  convert(varchar(20),@ci_date)  + '  Reservation ref. No.' + ltrim(str(@ctrl))
goto e_error
end
if @bnogen=1 
	if @bnoiseno=1
		if @eno=0 begin print 'Entry number can not be 0 as it is used as Bill Number' goto e_error end


set @nationality=@foreign

if @opt=0 or @opt=2
begin
	if @g_autoguestad=1
	begin
		if not exists(select * from guestad where name=@name and add1=@add1 and add2=@add2 and add3=@add3  and city=@city)
		begin
			insert into guestad([name],add1,add2,city,state,pin,phone,fax,email,address,company,rent,discount,sc,nationality)
			values
			(@name,@add1,@add2,@city,@state,@pin,@phone,@fax,@email,@address,@company,@rent,@discount,@sc,@nationality)
			if @@error<>0
			begin
				print 'updation in guestad failed'
				goto e_error
			end
			select @m_cno=max(cno) from guestad
		end
	end
end

if @opt=1
begin
	update guestad set lastvisit=@ci_date,rent=@rent where cno=@m_cno
	if @@error<>0
	begin
		print 'updation in guestad failed'
		goto e_error
	end
end

declare @usernm varchar(50)
--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
--DELETE FROM GUESTSET WHERE CNO=@M_CNO AND FPTYPE=@FOODPLAN AND ROOMTYPE=@WINGNO
if @m_cno<>0
	if @g_autoguestset=1
		begin
			if not exists(select * from guestset where cno=@m_cno and roomtype=@wingno and fptype=@foodplan and renttype=@renttype)
				begin
				insert into guestset (cno,roomtype,lttype,rent1,rent2,discount,sc,fptype,renttype) values (@m_cno,@wingno,0,@rent1,@rent2,@discount,@sc,@foodplan,@renttype)
				if @@error<>0 
					begin
					print 'Error in updating Guest Setting' goto e_error
					end
				end
			else
				begin
				update guestset set lastvisit=getdate() where cno=@m_cno and roomtype=@wingno and FPTYPE=@FOODPLAN AND RENTTYPE=@RENTTYPE
				if exists(select * from guestset where cno=@m_cno and roomtype=@wingno and FPTYPE=@FOODPLAN AND RENTTYPE=@RENTTYPE AND rent1=0)
				update guestset set rent1=@rent1 where cno=@m_cno and roomtype=@wingno AND FPTYPE=@FOODPLAN AND RENTTYPE=@RENTTYPE  
				if exists(select * from guestset where cno=@m_cno and roomtype=@wingno and FPTYPE=@FOODPLAN AND RENTTYPE=@RENTTYPE AND rent2=0)
				update guestset set rent2=@rent2 where cno=@m_cno and roomtype=@wingno and FPTYPE=@FOODPLAN AND RENTTYPE=@RENTTYPE
				end
		end

declare @tempco numeric
select @tempco=isnull(company,0) from guest where gno=@gno
if @tempco<>0   if @company=0 set @company=@tempco
update guest set
otacommctrlno=@commctrlno ,
cno=@m_cno, ci_date=@ci_date,rno=@m_rno,eno=@eno,foodplan=@foodplan,rent=@rent,discount=@discount,sc=@sc,male=@male,female=@female,
child=@child,ci_time=convert(varchar(5),@ci_date,8),grpname=@grpname,expecteddep=@expecteddep,userchkin=@usernm,
lt=@ltcal,company=isnull(@company,0),fp_details=@fp_desc,arrivedfrom=@arrivedfrom,
destination=@destination,qcheck=' ',autobill=@autobill,autoqty=@autoqty,autoamt=@autoamt, autoitem=@autocode,
address=@address,name=@name,add1=@add1,add2=@add2,add3=@add3,city=@city,state=@state,pin=@pin,phone=@phone,fax=@fax,email=@email,voucherno=@voucherno,
nationality=@nationality,
idtype = @idtype,idno=@idno,carno=@carno,
allinclusive=@allinclusive,
renttype1=@renttype,
lttype=@lttype,
nettrent=@nettrent,
nettrentexcludingst=@nrexst,
stopextra=@stopextra,
st=@stcal,
dobm=@dobm,dobd=@dobd,domm=@domm,domd=@domd,ExtraBedCharges=case when isnull(extrabedcharges,0)=0 then @extrabedcharges else extrabedcharges end,
gstin=@gstin,extrabed=@extrabed,food=@food,stopservice=@stopservice 
where gno=@gno
if @@error<>0 begin print 'Updation in Guest Register failed' goto e_error end

if @eno=0  --new on 07 March 2017 - for Sukhmani Guwahati
	if exists(select * from setting where enoisgno=1)
		begin
			set @eno=@m_gno
			update guest set eno=gno where gno=@m_gno
		end



update room set qcheck=' ',ci='Y',ol24ci='Y' where rno=@m_rno
if @@error<>0 begin print 'Updation in Room Table failed' goto e_error end
insert groom (gno,ci_date,rno,rent) values (@m_gno,@ci_date,@m_rno,@rent)
if @@error<>0 begin print 'Updation in Room Table failed' goto e_error end

if @memo<>''
begin
declare @memo1 varchar(100)
declare @memo2 varchar(100)
set @memo = substring(ltrim(rtrim(@memo)) + space(200),1,200)
set @memo1 = ltrim(rtrim(substring(@memo,1,100)))
set @memo2 = ltrim(rtrim(substring(@memo,101,100)))
delete from gm_master where gno=@m_gno
insert into gm_master (gno,txndt,usernm) values (@m_gno,getdate(),'RES')
if @@error<>0 begin print 'Error in updating Memorandum' goto e_error end
declare @gm_ctrlno numeric
set @gm_ctrlno = ident_current('gm_master')
insert into guest_memo (ctrlno_ref,details) values (@gm_ctrlno,@memo1)
if @memo2<>'' insert into guest_memo (ctrlno_ref,details) values (@gm_ctrlno,@memo2)
end

select @sql = 'declare  temp1 cursor for select [name],father,sex,age,job from ' +  @temptable1 
execute( @sql)
open temp1
fetch next from temp1 into @oc_name,@oc_father,@oc_sex,@oc_age,@oc_job
while @@fetch_status=0
begin
insert goccupants (gno,[name],father,sex,age,job) values (@m_gno,@oc_name,@oc_father,@oc_sex,@oc_age,@oc_job)
if @@error <> 0
begin
print 'goccupants updation failed'
goto e_error
end
fetch next from temp1 into @oc_name,@oc_father,@oc_sex,@oc_age,@oc_job
end
close temp1
deallocate temp1
if @foreign<> 'Indian'
begin
select @sql = 'declare  temp1 cursor for select [name],pp_no,pp_issuedate,pp_issueplace,nationality, doa,port,employed,stayduration,regnno,regndate,regnoffice,purpose,vno,vip,vid,vvd,pped,lpsi,dob,place2p,country2p,phoneno,email,sex,parentage,nationalitybybirth,surname,arrivedfromport,addressinindia,nativeaddress,vtype,visalifetime,imagectrlno   from ' +  @temptable2  + '  where rno=' + str(@m_rno)
execute( @sql)
open temp1

--FETCH NEXT FROM TEMP1 INTO @OC_NAME,@PP_NO,@PP_ISSUEDATE,@PP_ISSUEPLACE,@NATIONALITY,@doa, @port, @employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose,@vno,@vip,@vid,@vvd,@pped,@lpsi,@dob,@place2p,@country2p,@phoneno,@email_pp,@sex,@parentage
fetch next from TEMP1 into @oc_name,@pp_no,@pp_issuedate,@pp_issueplace,@nationality,@doa, @port, @employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose,@vno,@vip,@vid,@vvd,@pped,@lpsi,@dob,@place2p,@country2p,@phoneno,@email_pp,@sex,@parentage,@nationalitybybirth,@surname,@arrivedfromport,@addressinindia,@nativeaddress,@vtype,@visalifetime,@imagectrlno
WHILE @@FETCH_STATUS=0
BEGIN
if ltrim(@regnno)=''  set @regndate=null
if upper(@nationality)<>'INDIAN' 
	begin
	insert into GPASSPORT (gno,[name],pp_no,pp_issuedate,pp_issueplace,nationality,doa,port,employed,stayduration,regnno,regndate,regnoffice,purpose,edate,vno,vip,vid,vvd,pped,lpsi,dob,place2p,country2p,phoneno,email,sex,parentage,nationalitybybirth,surname,arrivedfromport,addressinindia,nativeaddress,vtype,visalifetime,imagectrlno)
	 VALUES 
	(@M_GNO,@OC_NAME,@PP_NO,@PP_ISSUEDATE,@PP_ISSUEPLACE,@NATIONALITY,@doa,@port,@employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose,@ci_date,@vno,@vip,@vid,@vvd,@pped,@lpsi,@dob,@place2p,@country2p,@phoneno,@email_pp,@sex,@parentage,@nationalitybybirth,@surname,@arrivedfromport,@addressinindia,@nativeaddress,@vtype,@visalifetime,isnull(@imagectrlno,0))
	if @@error<>0
		begin
		--SET @MSGNO=50009
		print 'pp updation failed'
		goto e_error
		end
	end
--FETCH NEXT FROM TEMP1 INTO @OC_NAME,@PP_NO,@PP_ISSUEDATE,@PP_ISSUEPLACE,@NATIONALITY,@doa, @port, @employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose,@vno,@vip,@vid,@vvd,@pped,@lpsi,@dob,@place2p,@country2p,@phoneno,@email_pp,@sex,@parentage
fetch next from TEMP1 into @oc_name,@pp_no,@pp_issuedate,@pp_issueplace,@nationality,@doa, @port, @employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose,@vno,@vip,@vid,@vvd,@pped,@lpsi,@dob,@place2p,@country2p,@phoneno,@email_pp,@sex,@parentage,@nationalitybybirth,@surname,@arrivedfromport,@addressinindia,@nativeaddress,@vtype,@visalifetime,@imagectrlno
END
close temp1
deallocate temp1
end

select @sql = 'declare  temp1 cursor for select outlet,chtype from ' +  @temptable3
execute( @sql)
open temp1
fetch next from temp1 into @outlet,@chtype
while @@fetch_status=0
	begin
	insert gsplit (gno,outlet,chtype) values (@m_gno,@outlet,@chtype)
	IF @@ERROR <> 0
		begin
		print 'split updation failed'

		goto e_error
		end
	fetch next from temp1 into @outlet,@chtype
	end
close temp1
deallocate temp1
-------------------
if @bnogen=1 
if @bnoiseno=1
	begin
		update guest set bno=eno where gno=@m_gno	
	end
else
	begin
		--if datepart(mm,@ci_date) >=  1 and datepart(mm,@ci_date) <= 3
		--	set @m_year = datepart(yy,@ci_date)-1
		--else
		--	set @m_year = datepart(yy,@ci_date)
		--select @m_bofy = '1 apr ' + str(@m_year)
		--update company set bno=case when bdt <> @m_bofy then 1 else bno+1 end , bdt= case  when bdt <> @m_bofy then @m_bofy else bdt end where company = 1
		--if @@error <> 0 
		--begin 
		--	print 'bno generation failed'
		--	goto e_error
		--end
		--select @bno=bno from company where company= 1
		exec getbno @ci_date,@bno out
		update guest set bno=@bno where gno=@m_gno
		if @@error <> 0
		begin
			print 'billhist1 updation failed'
			goto e_error
		end
	end

---------------

declare @ctrlno numeric
declare @ctrlno1 numeric
declare @channelexbed money
declare @channelexbedok tinyint
if @reserved=0
update guest set mktmgr=@mktmgr where gno=@gno

if @reserved <> 0 
begin
select @ctrlno=ctrlno,@channelexbed=channelexbed,@channelexbedok=channelexbedok from sptbrs1 where myctrlno=@reserved
update sptbrs set otacommctrlno=@commctrlno where ctrlno=@ctrlno 
--update sptbrs1 set arrivedrooms=arrivedrooms+1 where myctrlno=@reserved
if @@error<>0 begin print 'Error in updating the arrived rooms ' + ltrim(str(@reserved)) goto e_error end
if @channelexbedok=0 
	begin
	update guest set channelexbed=@channelexbed where gno=@gno
	update sptbrs1 set channelexbedok=1 where myctrlno=@reserved
	end
if @@error<>0 begin print 'Error in updating the Guest Book  ' + ltrim(str(@reserved)) goto e_error end

if @resadv<>0
	begin
	--select @ctrlno=ctrlno from sptbrs1 where myctrlno=@reserved
	update sptbrs set advanceadjd=advanceadjd+@resadv where ctrlno=@ctrlno
	if @@error<>0 begin print 'Error in updateting the advance adjusted' goto e_error end
	exec pbpa1u @m_gno,@resadv,1,4,'','','',56,'N',@ctrlno1 out
	if @@error<>0 begin print 'Error in advance updating'  goto e_error end
	select @mktmgr=mktmgr from sptbrs  where ctrlno=@ctrlno
	update guest set reserve_adv_cno=@ctrlno1,mktmgr=@mktmgr where gno=@gno
	if @@error<>0 begin print 'Error in advance ctrlno updation' goto e_error end
	end
end

if @reserved=0
begin
	declare @dt datetime
	set @dt = @ci_Date
	While DateDiff(d,@DT, @expecteddep) >= 0
	begin
		if not exists (select * from staahinv where status='' and datediff(d,txndt,@dt) = 0) insert into staahinv (txndt) values (@DT)
		if not exists (select * from crsinv where status='' and datediff(d,txndt,@dt) = 0) insert into crsinv (txndt,status,message) values (@DT,'','')
		set @dt = dateadd(d,1,@dt)
	end
end


COMMIT TRANSACTION
GOTO FINAL
E_ERROR:
ROLLBACK TRANSACTION
set @msg = @msg + 'Check-In Failed'
RAISERROR  (@msg , 16 ,1)
GOTO FINAL
FINAL:
if @erasetemp=' '
begin
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE1
EXEC(@SQL)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE2
EXEC(@SQL)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE3
EXEC(@SQL)
end


GO
/****** Object:  StoredProcedure [dbo].[pbpc1iUSELESS]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
CREATE procedure [dbo].[pbpc1iUSELESS]  @m_rno int,@opt int,@name varchar(50),@add1 varchar(50),@add2 varchar(50),
@add3 varchar(50),@city varchar(50),@state varchar(50),@pin char(10),@phone varchar(50),
@fax varchar(50),@email varchar(50),@rent money ,@discount money,@sc money,
@company int,@m_cno int,@ci_date datetime,@male int,@female int,@child int,
@foreign int,@address char(5),@eno numeric,
@foodplan int ,@temptable1 varchar(50),@temptable2 varchar(50),@temptable3 varchar(50),@expecteddep datetime,
@grpname varchar(30)='' ,@addad char(1)=' ',@erasetemp char(1) = ' ',@reserved numeric=0,@resadv money=0,@ltcal char(1)='Y',
@fp_desc varchar(100)='',@arrivedfrom varchar(50)=' ',@destination varchar(50)=''
/* with encryption */
as
declare @msgno numeric
declare @m_gno numeric
declare @sql varchar(5000)
declare @oc_name varchar(50)
declare @oc_father varchar(50)
declare @oc_sex char(1)
declare @oc_age int
declare @oc_job varchar(50)
declare @pp_no varchar(50)
declare @pp_issuedate datetime
declare @pp_issueplace varchar(50)
declare @nationality varchar(50)
declare @outlet int
declare @chtype int

declare @doa datetime
declare @port varchar(50)
declare @employed char(1)
declare @stayduration int
declare @regnno varchar(50)
declare @regndate datetime
declare @regnoffice varchar(50)
declare @purpose varchar(50)
declare @ctrlno1 numeric
declare @fptype tinyint
declare @lttype tinyint
declare @wingno tinyint
declare @rent1 money
declare @rent2 money
declare @defnationality varchar(50)
declare @g_autoguestad int
declare @g_autoguestset int
declare @bnoiseno int
declare @bnogen int
declare @bno numeric
declare @m_year int
declare @m_bofy datetime
declare @stopservice tinyint


begin transaction

if isnull(@company,0) <> 0 
select @stopservice=stopservice from gcompany where comno=@company 

set @stopservice=isnull(@stopservice,0)

select top 1 @bnogen=g_bno_generation,
@defnationality=G_DefaultNationality,@g_autoguestad=g_autoguestad,@g_autoguestset=g_autoguestset,@bnoiseno=bnoiseno from setting
if @bnogen=1 
	if @bnoiseno=1 
		if @eno=0
			begin
				print 'The entry number can not be 0 as it is taken as the bill number'  
                                                   goto e_error
			end

if not exists(select * from hmsfolder where version_no = (select version_no from comuser where [host_id]=host_id()) ) 
  begin 
  print object_name(@@PROCID)  + 'You are using a wrong version of the Softwre Installed...Please update the version (3) and try'
  goto e_error
end

if ltrim(@arrivedfrom) = '' goto e_error

if exists(select * from room where rno=@m_rno and gno<>0)
begin
	print 'selected room is aleary checked in'
	goto e_error
end

if exists(select * from sptbrooms_v  where rno=@m_rno and status1<>'Y'
and (
(datediff(s,'1/1/90',@expecteddep)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)  or
datediff(s,'1/1/90',@ci_date)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)) or 
(datediff(s,'1/1/90',@ci_date) < datediff(s,'1/1/90',departuredate) and datediff(s,'1/1/90',@expecteddep) >= datediff(s,'1/1/90',departuredate))
))
begin
print  'This room (' + ltrim(str(@m_rno)) + ') is blocked for the period during which the expected departure date of this guest falls ' + char(13) + 'Check - in aborted - This is a SQL server messsage ' + convert(varchar(20),@expecteddep) + '  ' +  convert(varchar(20),@ci_date)
goto e_error
end
select @wingno=roomwing from room where rno=@m_rno
set @rent1= case when @male+@female = 1 then @rent  else 0 end
set @rent2=case when @male+@female > 1 then @rent else 0 end
if @foreign=1
	set @nationality='Foreigner'
else
	set @nationality=@defnationality

if @opt=0 or @opt=2
begin
	if @g_autoguestad=1
	begin
	if not exists(select * from guestad where name=@name and add1=@add1 and add2=@add2 and add3=@add3  and city=@city)
        begin	 
	 		insert into guestad([name],add1,add2,city,state,pin,phone,fax,email,address,company,rent,discount,sc,nationality)
			values
			(@name,@add1,@add2,@city,@state,@pin,@phone,@fax,@email,@address,@company,@rent,@discount,@sc,@nationality)
			if @@error<>0
			begin
				print 'updation in guestad failed(1)'
				goto e_error
			end
			select @m_cno=max(cno) from guestad
		end
     end 
end

if @opt=1
begin
	update guestad set lastvisit=@ci_date,rent=@rent where cno=@m_cno
	if @@error<>0
	begin
		print 'updation in guestad failed(2)'
		goto e_error
	end
end

if @m_cno<>0
	if @g_autoguestset=1
		if not exists(select * from guestset where cno=@m_cno and roomtype=@wingno and fptype=@foodplan)
			insert into guestset (cno,roomtype,lttype,rent1,rent2,discount,sc,fptype,renttype) values (@m_cno,@wingno,0,@rent1,@rent2,@discount,@sc,@foodplan,1)
		else
			begin
			update guestset set lastvisit=getdate()
			if exists(select * from guestset where cno=@m_cno and roomtype=@wingno and fptype=@foodplan and rent1=0)
			update guestset set rent1=@rent1 where cno=@m_cno and roomtype=@wingno and fptype=@foodplan
			
			if exists(select * from guestset where cno=@m_cno and roomtype=@wingno and rent2=0 and fptype=@foodplan)
			update guestset set rent2=@rent2 where cno=@m_cno and roomtype=@wingno and fptype=@foodplan
			end

--PRINT 'PERIYA KARUPPU'
-----------
declare @usernm varchar(50)
--select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

insert guest (cno,ci_date,rno,eno,foodplan,rent,discount,sc,male,female,child,ci_time,grpname,expecteddep,userchkin,reserve_ref,
reserve_adv,lt,company,fp_details,arrivedfrom,destination,address,name,add1,add2,add3,city,state,phone,email,nationality,STOPSERVICE)
values (@m_cno,@ci_date,@m_rno,@eno,@foodplan,@rent,@discount,
@sc,@male,@female,@child,substring(convert(varchar(20),@ci_date),13,7),@grpname,@expecteddep,@usernm,@reserved,@resadv,@ltcal,
isnull(@company,0),@fp_desc,@arrivedfrom,@destination,@address,@name,@add1,@add2,@add3,@city,@state,@phone,@email,@nationality,@stopservice)
if @@error <> 0
begin
	print 'updation in guest failed'
	goto e_error
end
if @eno=0  --new on 07 March 2017 - for Sukhmani Guwahati
	if exists(select * from setting where enoisgno=1)
		begin
			set @eno=@m_gno
			update guest set eno=gno where gno=@m_gno
		end



select @m_gno=ident_current('guest')
update room set gno=@m_gno,grpname=@grpname,cismssent=0 where rno = @m_rno
if @@error <> 0
begin
	print 'updation in room table failed'
	goto e_error
end

insert groom (gno,ci_date,rno,rent) values (@m_gno,@ci_date,@m_rno,@rent)
if @@error <> 0
begin
set @msgno = 50007
goto e_error
end

------------
if @foreign=0
begin
	select @sql = 'declare  temp1 cursor for select [name],father,sex,age,job from ' +  @temptable1 
	execute( @sql)
	open temp1
	fetch next from temp1 into @oc_name,@oc_father,@oc_sex,@oc_age,@oc_job
	while @@fetch_status=0
		begin
		insert goccupants (gno,[name],father,sex,age,job) values (@m_gno,@oc_name,@oc_father,@oc_sex,@oc_age,@oc_job)
		if @@error <> 0
			begin
			print 'goccupants updation failed'
			goto e_error
			end
		fetch next from temp1 into @oc_name,@oc_father,@oc_sex,@oc_age,@oc_job
		end
	close temp1
	deallocate temp1
end

---------------
if @foreign=1
	begin
	select @sql = 'declare  temp1 cursor for select [name],pp_no,pp_issuedate,pp_issueplace,nationality, doa,port,employed,stayduration,regnno,regndate,regnoffice,purpose   from ' +  @temptable2 
	execute( @sql)
	open temp1
	fetch next from temp1 into @oc_name,@pp_no,@pp_issuedate,@pp_issueplace,@nationality,@doa, @port, @employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose
	while @@fetch_status=0
	begin
		if ltrim(@regnno)=''  set @regndate=null
		if upper(@nationality)<>'INDIAN'
		begin
			insert gpassport (gno,[name],pp_no,pp_issuedate,pp_issueplace,nationality,doa,port,employed,stayduration,regnno,regndate,regnoffice,purpose,edate) values (@m_gno,@oc_name,@pp_no,@pp_issuedate,@pp_issueplace,@nationality,@doa,@port,@employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose,@ci_date)
			if @@error <> 0
				begin
				--set @msgno=50009
				print 'pp updation failed'
				goto e_error
				end
		end
		fetch next from temp1 into @oc_name,@pp_no,@pp_issuedate,@pp_issueplace,@nationality,@doa, @port, @employed,@stayduration,@regnno,@regndate,@regnoffice,@purpose
	end
	close temp1
	deallocate temp1
end

select @sql = 'declare  temp1 cursor for select outlet,chtype from ' +  @temptable3
execute( @sql)
open temp1
fetch next from temp1 into @outlet,@chtype
while @@fetch_status=0
begin
insert gsplit (gno,outlet,chtype) values (@m_gno,@outlet,@chtype)
if @@error <> 0
begin
print 'split updation failed'
goto e_error
end
fetch next from temp1 into @outlet,@chtype
end
close temp1
deallocate temp1
-----------
if @bnogen=1
	if @bnoiseno=1 
		begin
			update guest set bno=eno where gno=@m_gno
		end
	else
		begin
			--if datepart(mm,@ci_date) >=  1 and datepart(mm,@ci_date) <= 3
			--	set @m_year = datepart(yy,@ci_date)-1
			--else
			--	set @m_year = datepart(yy,@ci_date)
			--select @m_bofy = '1 apr ' + str(@m_year)
			select @m_bofy = dbo.getfy(@ci_date)
			update company set bno=case when datediff(d,bdt,@m_bofy)<>0 then 1 else bno+1 end , bdt= case  when datediff(d,bdt,@m_bofy)<>0 then @m_bofy else bdt end where company = 1
			if @@error<>0
				begin 
				print 'bno generation failed'
				goto e_error
				end
			select @bno=bno from company where company= 1
			update guest set bno=@bno where gno=@m_gno
			if @@error <> 0
				begin
				print 'billhist1 updation failed'
				goto e_error
				end
		end
----------------------
declare @ctrlno numeric
if @reserved <> 0 
	begin
	update sptbrs1 set arrivedrooms=arrivedrooms+1 where myctrlno=@reserved
	if @@error<>0 begin print 'Error in updating the arrived rooms ' + ltrim(str(@reserved)) goto e_error end
	if exists(select * from sptbrooms where rno=@m_rno and myctrlno=@reserved)
	begin
		update sptbrooms set status1='Y' where rno=@m_rno and myctrlno=@reserved
		if @@error <>0 goto e_error
	end
if @resadv<>0
	begin
		select @ctrlno=ctrlno from sptbrs1 where myctrlno=@reserved
		update sptbrs set advanceadjd=advanceadjd+@resadv where ctrlno=@ctrlno
		if @@error<>0 begin print 'Error in updateting the advance adjusted' goto e_error end
		declare @narration varchar(100)
		set @narration = 'Against Reservation No ' + ltrim(str(@ctrlno))
		exec pbpa1u @m_gno,@resadv,1,4,'','','',56,'N',@ctrlno1 out , @narration
		if @@error<>0 begin print 'Error in advance updating'  goto e_error end
		update guest set reserve_adv_cno=@ctrlno1 where gno=@m_gno
		if @@error<>0 begin print 'Error in advance ctrlno updation' goto e_error end
	end
end
commit transaction
goto final
e_error:
rollback transaction
raiserror  ('checkin failed' , 16 ,1)
goto final
final:
if @erasetemp=' '
begin
select @sql = 'drop table ' + @temptable1
exec(@sql)
select @sql = 'drop table ' + @temptable2
exec(@sql)
select @sql = 'drop table ' + @temptable3
exec(@sql)
end




GO
/****** Object:  StoredProcedure [dbo].[pbpc1o]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.   Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.   Script Date: 1/20/03 11:51:47 AM ******/
CREATE PROCEDURE [dbo].[pbpc1o]  @T1 VARCHAR(100),@T2 VARCHAR(100) ,@GNO numeric ,@M_TXNDT DATETIME ,@updbillhist int = 0 ,@grpco int = 0 ,@nochange int=0,@newgrno int=0,@con_id char(20)=null ,@partic_con varchar(100)=null,@grpstart int=0,@grpend int=0,@grpgno numeric=0 
/* with encryption */
as
declare @details varchar(500)
declare @roomdues money
DECLARE @ADVANCE MONEY
DECLARE @MOP INT
DECLARE @CRNAME VARCHAR(50)
DECLARE @CRNUM VARCHAR(20)
DECLARE @CRTYPE VARCHAR(50)
DECLARE @GLACTNO INT
DECLARE @M_GRNO INT
DECLARE @M_YEAR INT
DECLARE @M_BOFY DATETIME
DECLARE @BNO1  INT
DECLARE @BNO INT
DECLARE @SQL VARCHAR(5000)
DECLARE @SQL1 VARCHAR(5000)
DECLARE @SQL2 VARCHAR(5000)
DECLARE @COMPANY INT
DECLARE @CHANNEL INT
DECLARE @BP1 INT
DECLARE @TXNDT DATETIME
DECLARE @AMT MONEY
DECLARE @RENT MONEY
DECLARE @DISCOUNT MONEY
DECLARE @ITEM1 MONEY
DECLARE @ITEM2 MONEY
DECLARE @ITEM3 MONEY
DECLARE @ITEM4 MONEY
DECLARE @ITEM5 MONEY
DECLARE @ITEM6 MONEY
DECLARE @ITEM7 MONEY
DECLARE @ITEM8 MONEY
DECLARE @ITEM9 MONEY
DECLARE @ITEM10 MONEY
DECLARE @LT MONEY
DECLARE @SC MONEY
DECLARE @TOTAL MONEY
DECLARE @SDT DATETIME
DECLARE @EDT DATETIME
DECLARE @LRATE MONEY
DECLARE @SAMT MONEY
DECLARE @EAMT MONEY
DECLARE @FAMT MONEY
DECLARE @LTAMT MONEY
DECLARE @SCAMT MONEY
DECLARE @TNO INT
DECLARE @GTOTAL MONEY
DECLARE @CTOTAL MONEY
DECLARE @BILLORDER INT
DECLARE @SECTION CHAR(1)
DECLARE @HOSTID char(18)
DECLARE @OUTLET1 INT
DECLARE @OUTLET2 INT
DECLARE @OUTLET3 INT
DECLARE @OUTLET4 INT
DECLARE @OUTLET5 INT
DECLARE @OUTLET6 INT
DECLARE @OUTLET7 INT
DECLARE @OUTLET8 INT
DECLARE @OUTLET9 INT
DECLARE @OUTLET10 INT
DECLARE @OUTLET INT
DECLARE @TYPEOUTLET INT
DECLARE @SELFDEAL CHAR(1)
DECLARE @A INT
DECLARE @B MONEY
DECLARE @SPLIT CHAR(1)
DECLARE @OL1 INT
DECLARE @OL2 INT
DECLARE @OL3 INT
DECLARE @OL4 INT
DECLARE @OL5 INT
DECLARE @OL6 INT
DECLARE @OL7 INT
DECLARE @OL8 INT
DECLARE @OL9 INT
DECLARE @OL10 INT
DECLARE @AMT1 MONEY
DECLARE @AMT2 MONEY
DECLARE @AMT3 MONEY
DECLARE @AMT4 MONEY
DECLARE @AMT5 MONEY
DECLARE @AMT6 MONEY
DECLARE @AMT7 MONEY
DECLARE @AMT8 MONEY
DECLARE @AMT9 MONEY
DECLARE @AMT10 MONEY
DECLARE @HNO_MYBOOK INT
DECLARE @HNO_HTLBOOK INT
DECLARE @CRENT MONEY
DECLARE @CDISCOUNT MONEY
DECLARE @M_FY INT
DECLARE @ACTNUM INT
DECLARE @OLCO INT
DECLARE @MAXCO INT
DECLARE @PROCESS INT
DECLARE @CNO numeric
DECLARE @OLNM1 CHAR(10)
DECLARE @OLNM2 CHAR(10)
DECLARE @OLNM3 CHAR(10)
DECLARE @OLNM4 CHAR(10)
DECLARE @OLNM5 CHAR(10)
DECLARE @OLNM6 CHAR(10)
DECLARE @OLNM7 CHAR(10)
DECLARE @OLNM8 CHAR(10)
DECLARE @OLNM9 CHAR(10)
DECLARE @OLNM10 CHAR(10)
DECLARE @SPLDISCOUNT MONEY
declare @foodbill money
declare @linkno int
declare @taxable money
DECLARE @G_BNO_GENERATION INT
declare @cur_rno int
declare @detailrno numeric
declare @tfy int
declare @grpname varchar(20)
declare @grpcounter int
declare @usernm varchar(50)
declare @brno numeric
declare @eofy datetime
declare @spldiscreason varchar(200)
declare @spldiscauth varchar(50)
declare @st  money
declare @cess money
declare @cess2 money
declare @otherchgs money
declare @err varchar(5000)
declare @extrabed money
declare @exbedgst money
declare @food money
declare @foodgst money
declare @gstin varchar(50)  

--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

Set @hostid = host_id()
select @detailrno=rno,@gstin=gstin  from guest where gno=@gno
set nocount on
set @taxable=0
--print 'vanthutten'
--goto e_error

if not exists(select * from hmsfolder where version_no = (select version_no from comuser where [host_id]=host_id() and sessionid='') ) 
	begin 
	set @err='You are using a wrong version of the Softwre Installed...Please update the version (5) and try'
	raiserror(@err,16,-1)
	return
	end
if @updbillhist=0
	begin
		IF NOT EXISTS(SELECT * FROM ROOM WHERE GNO = @GNO)
		begin
		set @err =  'The room is already checked out by some other terminal during your process' + str(@gno)
		raiserror(@err,16,-1)
		return
		end
		if not exists (select * from room where hostid=(select computer from comuser where [host_id]=host_id()) and gno=@gno )
		begin
		set @err =  'The Check-Process Lock is released by some one else...Re-Do the Check-Out again'
		raiserror(@err,16,-1)
		return
		end
	end
if exists (select * from chalan where ogno=@gno and status<>'C')
	begin
	set @err =  'Travel Chalan(s) outstanding against this customer..clear them and then checkout'
	raiserror(@err,16,-1)
	return
	end

declare @code numeric
select @code=rate5 from stdisd3 where sno=21
if @code=17  and datepart(hh,getdate()) > 10  
begin
	set @err =  'Error in Check-Out L'
	raiserror(@err,16,-1)
	return
end

begin transaction
if @updbillhist=0
begin
if not exists(select * from brno) insert into brno (brno) values (0)
	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

	UPDATE ROOM SET GNO=0,CHECKOUT=' ',  HOSTID = ' ' ,STATUS='A' ,grpname = ' ' ,wu=' ',[time]=' ',cismssent=0 WHERE GNO=@GNO
	if @@error<>0   begin print 'Updation of Room failure'  goto e_error end
    update crcardhold set status='C' where gno=@gno and status=' ' and holdtype=1
	if @@error<>0   begin print 'Updation of CrCardHold failure'  goto e_error end
	update guest set co_date=@m_txndt,co_time=substring(convert(varchar(20),@m_txndt),13,7),userchkout=@usernm  WHERE GNO=@GNO
	if @@error<>0   begin print 'Updation of Guest failure'  goto e_error end
	update groom set co_date=@m_txndt where gno=@gno and co_date is null
	if @@error<>0   begin print 'Updation of Groom failure'  goto e_error end
	if @grpgno=0 
		begin
			update brno set brno=brno+1
			select @brno=brno from brno
		end
	else
		select @brno=brno from guest  where gno=@grpgno
end

SELECT @MAXCO=MAX(COMPANY) FROM COMPANY
SET @COMPANY = 1
SET @SECTION = 'N'
WHILE @COMPANY <= @MAXCO
BEGIN
	SET @PROCESS = 0
	SELECT @SQL = 'DECLARE T1C CURSOR FOR SELECT  TXNDT,DISCOUNT,RENT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,'
	SELECT @SQL = @SQL + 'isnull(OUTLET1,0),isnull(OUTLET2,0),isnull(OUTLET3,0),isnull(OUTLET4,0),isnull(OUTLET5,0),isnull(OUTLET6,0),isnull(OUTLET7,0),isnull(OUTLET8,0),isnull(OUTLET9,0),isnull(OUTLET10,0),SC,LT,TOTAL,isnull(taxable,0),isnull(rno,0),st,cess,cess2,isnull(otherchgs,0),LRATE,isnull(extrabed,0),isnull(exbedgst,0),isnull(food,0),isnull(foodgst,0)'
	SELECT @SQL = @SQL + ' FROM ' + @T1 + '  WHERE BILLORDER=' + ltrim(STR(@COMPANY)) + ' AND SECTION = ''' + @SECTION + ''''
	SELECT @SQL = @SQL + ' ORDER BY TXNDT'
	exec(@sql)
	IF @@ERROR<>0 GOTO E_ERROR
	OPEN T1C
	FETCH NEXT FROM T1C INTO @TXNDT,@DISCOUNT,@RENT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,
	@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10,@SC , @LT,@TOTAL ,@taxable,@cur_rno,@st,@cess,@cess2,@otherchgs,@lrate,@extrabed,@exbedgst,@food,@foodgst
	SET  @SPLIT=@SECTION
	set @ltamt = 0
	set @scamt = 0
	SET @CRENT = 0
	SET @CDISCOUNT = 0
	SET @CTOTAL = 0
	SET @AMT1 = 0
	SET @AMT2 = 0
	SET @AMT3 = 0
	SET @AMT4 = 0
	SET @AMT5 = 0
	SET @AMT6 = 0
	SET @AMT7 = 0
	SET @AMT8 = 0
	SET @AMT9 = 0
	SET @AMT10 = 0
	SET @OL1 = 0
	SET @OL2 = 0
	SET @OL3 = 0
	SET @OL4 = 0
	SET @OL5 = 0
	SET @OL6 = 0
	SET @OL7 = 0
	SET @OL8 = 0
	SET @OL9 = 0
	SET @OL10 = 0
	SET @OLNM1 = ' '
	SET @OLNM2 = ' '
	SET @OLNM3 = ' '
	SET @OLNM4 = ' '
	SET @OLNM5 = ' '
	SET @OLNM6 = ' '
	SET @OLNM7 = ' '
	SET @OLNM8 = ' '
	SET @OLNM9 = ' '
	SET @OLNM10 = ' '
	WHILE @@FETCH_STATUS=0 
             BEGIN
	set @lrate= isnull(@lrate,0)
	set @cess2=isnull(@cess2,0)
	SET @PROCESS = 1
	  IF @TXNDT < '1 APRIL 9998'
	   BEGIN
		 if @updbillhist=0
		     begin
			IF @OLNM1=' '
			if @outlet1 >300
			    select @olnm1=shname from toutlet where outlet=@outlet1-300
			else if @outlet1 >200
			   select @olnm1 = shname from phoutlet where outlet=@outlet1-200
			else
			   select @olnm1=shname from restco where channel=@outlet1-100
			IF @OLNM2 = ' '
			if @outlet2 >300
			    select @olnm2=shname from toutlet where outlet=@outlet2-300

			else if @outlet2 >200
			   select @olnm2 = shname from phoutlet where outlet=@outlet2-200
			else
			   select @olnm2=shname from restco where channel=@outlet2-100
			IF @OLNM3 = ' '
			if @outlet3 >300
			    select @olnm3=shname from toutlet where outlet=@outlet3-300
			else if @outlet3 >200
			   select @olnm3 = shname from phoutlet where outlet=@outlet3-200
			else
			   select @olnm3=shname from restco where channel=@outlet3-100
			IF @OLNM4 = ' '
			if @outlet4 >300
			    select @olnm4=shname from toutlet where outlet=@outlet4-300
			else if @outlet4 >200
			   select @olnm4 = shname from phoutlet where outlet=@outlet4-200
			else
			   select @olnm4=shname from restco where channel=@outlet4-100
			IF @OLNM5 = ' '
			if @outlet5 >300
			    select @olnm5=shname from toutlet where outlet=@outlet5-300
			else if @outlet5 >200
			   select @olnm5 = shname from phoutlet where outlet=@outlet5-200
			else
			   select @olnm5=shname from restco where channel=@outlet5-100
			IF @OLNM6 = ' '
			if @outlet6 >300
			    select @olnm6=shname from toutlet where outlet=@outlet6-300
			else if @outlet6 >200
			   select @olnm6 = shname from phoutlet where outlet=@outlet6-200
			else
			   select @olnm6=shname from restco where channel=@outlet6-100
			IF @OLNM7 = ' '
			if @outlet7 >300
			    select @olnm7=shname from toutlet where outlet=@outlet7-300
			else if @outlet7 >200
			   select @olnm7 = shname from phoutlet where outlet=@outlet7-200
			else
			   select @olnm7=shname from restco where channel=@outlet7-100
			IF @OLNM8 = ' '
			if @outlet8 >300
			    select @olnm8=shname from toutlet where outlet=@outlet8-300
			else if @outlet8 >200
			   select @olnm8 = shname from phoutlet where outlet=@outlet8-200
			else
			   select @olnm8=shname from restco where channel=@outlet8-100
			IF @OLNM9 = ' '
			if @outlet9 >300
			    select @olnm9=shname from toutlet where outlet=@outlet9-300
			else if @outlet9 >200
			   select @olnm9 = shname from phoutlet where outlet=@outlet9-200
			else
			   select @olnm9=shname from restco where channel=@outlet9-100
			IF @OLNM10 = ' '
			if @outlet10 >300
			    select @olnm10=shname from toutlet where outlet=@outlet10-300
			else if @outlet10 >200
			   select @olnm10 = shname from phoutlet where outlet=@outlet10-200
			else
			   select @olnm10=shname from restco where channel=@outlet10-100
			--set @cur_rno = 0
			--select top 1 @cur_rno=rno from groom where gno=@gno  and  datediff(d,'1/1/1990',@txndt) between datediff(d,'1/1/1990',ci_date) and datediff(d,'1/1/1990',co_date)
			   INSERT INTO BILLHIST(GNO,TXNDT,DISCOUNT,RENT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,
			   OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10,SC,LT,TOTAL,COMPANY,HOSTID,taxable,rno,st,cess,cess2,otherchgs,lrate,extrabed,exbedgst,food,foodgst )
			--                                        OLNM1,OLNM2,OLNM3,OLNM4,OLNM5,OLNM6,OLNM7,OLNM8,OLNM9,OLNM10)
			   VALUES(@GNO,@TXNDT,@DISCOUNT,@RENT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,
			   @OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10,@SC , @LT,@TOTAL ,
			   @company, @hostid,@taxable,@cur_rno,@st,@cess,@cess2,@otherchgs,@lrate,@extrabed,@exbedgst,@food,@foodgst)
			--                                        @OLNM1,@OLNM2,@OLNM3,@OLNM4,@OLNM5,@OLNM6,@OLNM7,@OLNM8,@OLNM9,@OLNM10)
			   IF @@ERROR <> 0
			    BEGIN
			    print 'Error occured during billhist insertion...Check out Failed'
			    CLOSE T1C
			    DEALLOCATE T1C
			    GOTO E_ERROR
			    END
		    end
		else
		if @nochange=0
			begin
				--set @cur_rno = 0
				--select  top 1 @cur_rno=rno from groom where gno=@gno  and  @txndt between ci_date and co_date
				update billhist set discount = @discount,rent = @rent, item1 = @item1, item2 = @item2, item3 = @item3, item4 = @item4, item5 = @item5, item6 = @item6, item7 = @item7, item8 = @item8, item9 = @item9, item10 = @item10,
				sc=@sc,lt=@lt,total=@total,taxable=@taxable,rno=@cur_rno,orent=@rent,st=@st,cess=@cess,cess2=@cess2,otherchgs=@otherchgs,lrate=@lrate,
				extrabed=@extrabed,exbedgst=@exbedgst,food=@food,foodgst=@foodgst  
				where company=@company and gno=@gno and datediff(dd,'1/1/80',txndt) = datediff(dd,'1/1/80',@txndt) and cno = @updbillhist
				--select * from billhist where cno=@updbillhist
				IF @@ERROR <> 0
				BEGIN
				print 'Error occured during billhist updation...Check out Failed'
				CLOSE T1C
				DEALLOCATE T1C
				GOTO E_ERROR
				END
			  end
	   END
	   FETCH NEXT FROM T1C INTO @TXNDT,@DISCOUNT,@RENT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,
	   @OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10,@SC , @LT,@TOTAL ,@taxable,@cur_rno,@st,@cess,@cess2,@otherchgs,@lrate,@extrabed,@exbedgst,@food,@foodgst
            END
	-------- END OF EACH COMPANY IND.DATA
	CLOSE T1C
	DEALLOCATE T1C
--if @company=2 and @split='N'  begin print str
--(@process) + ' ' + @split + ' ' + str(@company) + ' ' + @hostid +' ' +  str(@gno)   goto e_error end 
-- new test
	if exists(select * from roomdues where gno=@gno and company=@company and split=@split)
		if not exists(select * from billhist where hostid=@hostid)
                                begin
		INSERT INTO BILLHIST(GNO,TXNDT,DISCOUNT,RENT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,
                                SC,LT,TOTAL,COMPANY,HOSTID,taxable,rno,st,cess,cess2,otherchgs,lrate,extrabed,exbedgst,food,foodgst )
		VALUES(@GNO,@m_txndt,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0 ,
		@company, @hostid, 0, 0,0,0,0,0,0,0,0,0,0)
		if @@error<>0 goto e_error
		set @process=1
		end

-- new test ends   
-- new for only advance in split bills
--if @split='Y' begin  print str(@gno) + ' ' + str(@company) + @split +' '+ str(@process) goto e_error end
if exists (select * from advance where gno=@gno and company=@company and split=@split) 
	if not exists(select * from billhist where hostid=@hostid)
		begin
		INSERT INTO BILLHIST(GNO,TXNDT,DISCOUNT,RENT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,
		                SC,LT,TOTAL,COMPANY,HOSTID,taxable,rno,st,cess,cess2,otherchgs,lrate,extrabed,exbedgst,food,foodgst )
		VALUES(@GNO,@m_txndt,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0 ,
		@company, @hostid, 0, 0,0,0,0,0,0,0,0,0,0)
		if @@error<>0 goto e_error
		set @process=1
		end
-- new for advance only ends

declare @diff numeric
declare @bdt11 datetime
set @m_grno=0
set @advance=0
	IF @PROCESS=1 
	BEGIN
		EXEC @M_FY= GETFY  @M_TXNDT,@COMPANY,@M_FY OUT
		IF @@ERROR <> 0 GOTO E_ERROR

		--IF DATEPART(mm,@M_TXNDT) >=  1 AND DATEPART(mm,@M_TXNDT) <= 3  SET @M_YEAR = DATEPART(yy,@M_TXNDT)-1 ELSE SET @M_YEAR = DATEPART(yy,@M_TXNDT)
		--SELECT @M_BOFY = '1 APR ' + STR(@M_YEAR)
		--set @eofy = '31 mar ' + str(@m_year+1)

		set @m_bofy = dbo.getbofy(@m_txndt)
		set @eofy = dbo.geteofy(@m_txndt)
		set @m_year = datepart(yy,@m_bofy)
		
		SET @SECTION=@SPLIT
		SET @G_BNO_GENERATION=0

	if @grpstart=0 or  (not exists (select * from billhist1 where brno=@brno and company=@company and split=@split))
		if @updbillhist=0
			begin
				IF @company=1 
					BEGIN
					IF @SPLIT='N' 
						BEGIN
							SELECT @G_BNO_GENERATION=G_BNO_GENERATION FROM SETTING
							IF @G_BNO_GENERATION=0
								begin
									if exists (select * from guest  where gno=@gno and bno<>0)
									   select @bno=bno from guest where gno=@gno
									else
										EXEC getbno @m_txndt,@BNO OUT
								end
								else
								begin
										if exists(select * from guest where gno=@gno and bno<>0)
										select @bno=bno from guest where gno=@gno
										else
										EXEC getbno @m_txndt,@BNO OUT
								end                      
						end           
					ELSE IF @SPLIT='Y'
						BEGIN
								update finyear set bno1=bno1+1 where fy=@m_fy
								if @@error<>0 goto e_error
								select @bno=bno1 from finyear where fy=@m_fy
						END
				end
				ELSE   --if company > 1
					BEGIN
							update finyear set bno2=bno2+1 where fy=@m_fy
							if @@error<>0 goto e_error
							select @bno=bno2 from finyear where fy=@m_fy
					END
			end   -- updthist=0 ends
         else
		              select @bno=bno from billhist1 where cno=@updbillhist
else   -- if grpstarts = 1
select @bno=bno from guest where gno=@gno

		SET @SQL1 = 'DECLARE TEMPC CURSOR FOR SELECT MOP,CRNAME,CRNUM,CRTYPE,GLACTNO,SPLDISCOUNT,foodbill,spldiscreason,spldiscauth FROM ' + @T2 + '  WHERE COMPANY= ' + LTRIM(STR(@COMPANY)) + '  AND SECTION= ''' + @SPLIT + ''''
		EXEC(@SQL1)
		OPEN TEMPC
		SET @MOP=0
		SET @CRNAME=''
		SET @CRNUM=''
		SET @CRTYPE=''
		SET @GLACTNO=0
		set @spldiscount=0
		set @foodbill=0
		IF @SPLIT <> 'C' and @split<> 'F'
			begin
			FETCH NEXT FROM TEMPC INTO @MOP,@CRNAME,@CRNUM,@CRTYPE,@GLACTNO,@SPLDISCOUNT,@foodbill,@spldiscreason,@spldiscauth
			end
		set @sql1 = 'update ' + @t2 + ' set spldiscount=0,foodbill=0 where company = ' + str(@company) + ' and section=''' + @split + ''''
		exec(@sql1)
		CLOSE TEMPC
		DEALLOCATE TEMPC
                                if @grpstart=0 or  (not exists (select * from billhist1 where brno=@brno and company=@company and split=@split))
                                begin
			if @updbillhist=0
				begin
				inSERT INTO BILLHIST1 (brno,COMPANY,SPLIT,BNO,TXNDT,ADVANCE,CRNAME,CRNUM,CRTYPE,GLACTNO,MOP,GRNO,SPLDISCOUNT,foodbill,
				OLNM1,OLNM2,OLNM3,OLNM4,OLNM5,OLNM6,OLNM7,OLNM8,OLNM9,OLNM10,spldiscreason,spldiscauth,gstin)
				VALUES (@brno,@COMPANY,@SPLIT,@BNO,@M_TXNDT,@ADVANCE,@CRNAME,@CRNUM,@CRTYPE,@GLACTNO,@MOP,@M_GRNO,isnull(@SPLDISCOUNT,0),isnull(@foodbill,0),
				@OLNM1,@OLNM2,@OLNM3,@OLNM4,@OLNM5,@OLNM6,@OLNM7,@OLNM8,@OLNM9,@OLNM10,@spldiscreason,@spldiscauth,@gstin)
				if @@error<>0 goto e_error
	                                                set @cno=@@identity
				UPDATE BILLHIST SET CNO=@CNO,HOSTID=0 WHERE HOSTID=@hostid
				if @@error<>0 goto e_error
				update guest set brno=@brno where gno=@gno
				if @@error<>0 goto e_error
				if @company=1 and @split='N' begin  update guest set bno=@bno where gno=@gno if @@error<>0 goto e_error end
				end
	                      	        ELSE
				BEGIN
				UPDATE BILLHIST SET CNO=@UPDBILLHIST,HOSTID=0  WHERE HOSTID=@hostid
				if @@error<>0 goto e_error
				update billhist1 set grno=@m_grno,spldiscount=@spldiscount,foodbill=@foodbill WHERE CNO=@UPDBILLHIST
				set @cno=@updbillhist
				if @@error<>0 goto e_error
				end
		end
		else
		begin
			select @cno=cno,@brno=brno from billhist1 where brno=(select brno from guest where gno=@grpgno) and company=@company and split=@split
			update guest set brno=@brno where gno=@gno
			if @@error<>0 goto e_error
			if @company=1 and @split='N' begin  update guest set bno=@bno where gno=@gno if @@error<>0 goto e_error end
			update billhist set cno=@cno,hostid=0 where hostid=@hostid 
			if @@error<>0 goto e_error
		end
                if @grpend=0 begin exec pbvc @cno if @@error<>0 begin print 'Error in vouching' goto e_error end   end                  
	end
       if @grpgno<>0
		if @grpend=0 
		  	if @process<> 1   /* this is for left out group bills*/
				if exists(select * from billhist1 where brno=@brno and company=@company and split=@split)
				begin
					select @cno=cno from billhist1 where brno=@brno and company=@company and split=@split
					exec pbvc @cno if @@error<>0 begin print 'Error in vouching' goto e_error end                 
				end          

if @company=1 and @section='N'
begin
	if exists (select * from guest where gno=@gno and datediff(d,co_date,expecteddep)<>0)
	begin
		declare @dt datetime
		declare @co_date datetime
		declare @expecteddep datetime
		select @co_date=co_date,@expecteddep=expecteddep from guest where gno=@gno
		set @dt = @co_date
		While DateDiff(d,@DT, @expecteddep) >= 0
		begin
			if not exists (select * from staahinv where status='' and datediff(d,txndt,@dt) = 0) insert into staahinv (txndt) values (@DT)
			if not exists (select * from crsinv where status='' and datediff(d,txndt,@dt) = 0) insert into crsinv (txndt,status,message) values (@DT,'','')
			set @dt = dateadd(d,1,@dt)
		end
	end

end


	IF @COMPANY=1 AND @SECTION='N'
	SET @SECTION='Y'
	ELSE IF @COMPANY=1 AND @SECTION='Y'
	SET @SECTION='C'
	ELSE IF @COMPANY=1 AND @SECTION='C'
	SET @SECTION='F'
	ELSE IF @COMPANY > 1 AND @SECTION='N'
	SET @SECTION = 'C'
	ELSE IF @COMPANY > 1 AND @SECTION = 'C'
	SET @SECTION = 'F'
	ELSE
	begin
	SET @COMPANY=@COMPANY+1
	set  @section = 'N'
	end
end
set @sql = 'drop table ' + @t1
exec(@sql)
if @grpco=0
begin
set @sql = 'drop table ' + @t2
exec(@sql)
end
COMMIT TRANSACTION
RETURN
E_ERROR:
ROLLBACK TRANSACTION
RAISERROR('CHECKOUT FAILED',16,-1)
RETURN



GO
/****** Object:  StoredProcedure [dbo].[pbpc2ig]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
CREATE PROCEDURE [dbo].[pbpc2ig] @OPT INT,@NAME VARCHAR(50),@ADD1 VARCHAR(50),@ADD2 VARCHAR(50),
@ADD3 VARCHAR(50),@CITY VARCHAR(50),@STATE VARCHAR(50),@PIN CHAR(10),@PHONE VARCHAR(50),
@FAX VARCHAR(50),@EMAIL VARCHAR(50),@DISCOUNT MONEY,@SC MONEY,
@COMPANY INT,@M_CNO INT,@CI_DATE DATETIME,
@FOREIGN INT,@ADDRESS CHAR(5),
@foodplan int ,@temptable1 VARCHAR(50),@TEMPTABLE2 VARCHAR(50),@TEMPTABLE3 VARCHAR(50),@TEMPTABLE4 VARCHAR(50),
@grpname varchar(30)='' ,@expecteddep datetime,@reservation numeric = 0,@advance money=0,@ltcal char(1)='Y',
@fp_desc varchar(100)='',@arrivedfrom varchar(50)='' , @destination varchar(50)=' ' 
/* with necryption */
as
declare @sql varchar(5000)
declare @rno int
declare @rent money
declare @male int
declare @female int
declare @child int
declare @eno int
declare @addad char(1)
declare @t1 varchar(50)
declare @t2 varchar(50)
declare @t3 varchar(50)
declare @name1 varchar(50)
declare @father varchar(50)
declare @sex char(1)
declare @age numeric
declare @job varchar(50)
declare @nation varchar(50)
declare @ppno varchar(50)
declare @ppdate varchar(50)
declare @ppissue varchar(50)
declare @doa datetime
declare @port varchar(50)
declare @employed char(1)
declare @stayduration int
declare @purpose varchar(50)
declare @regnno varchar(50)
declare @regndate varchar(50)
declare @regnoffice varchar(50)
declare @thisadvance money
declare @perroomadvance money
declare @ci_date2 datetime
declare @firstroomadv  money
declare @firstroom int
set @firstroom=0
set nocount on
set @t1 = ''
set @t2 = ''
if datepart(hh,@ci_date) = 12 and datepart(mi,@ci_date)=0 and datepart(ss,@ci_date)=0 
  begin
  print 'Time of check-in is found incorrect or exact [12:00:00]...This will affect the checkout calculations..pls correct the date'
  goto e_error
  end
begin transaction
declare @rnos numeric
set @sql = 'declare ttcur cursor for select count(*)  from '  + @temptable4
exec (@sql)
open ttcur
fetch next from ttcur into @rnos
close ttcur
deallocate ttcur
if @reservation <> 0   set @addad = 'N'  else if @opt = 0 or @opt = 2     set @addad = 'Y'  else     set @addad = 'N'
if @reservation <> 0
   if @advance <> 0
      begin
      set @perroomadvance = floor(@advance/@rnos)
      set @firstroomadv = @perroomadvance + (@advance - (@perroomadvance*@rnos))
      end
--print convert(varchar(20),@advance) + ' room nos' + str(@rnos) + ' per room ' + convert(varchar(20),@perroomadvance)
--goto e_error
 set @t1 = @temptable1 + '1'
set @t2 = @temptable2 + '1'
--print 'ramanathan'
if exists(select [name],xtype from sysobjects where name = @t1 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t1
   exec(@sql)
   if @@error<> 0 goto E_ERROR
  end
if exists(select [name],xtype from sysobjects where name = @t2 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t2
   exec(@sql)
   if @@error<> 0 goto E_ERROR
  end
set @sql = ' select * into ' + @t1 + ' from dummygd '
exec(@sql)
if @@error<> 0 goto E_ERROR
set @sql = ' select * into ' + @t2 + ' from dummypp '
exec(@sql)
if @@error<> 0 goto E_ERROR
--set @addad = 'Y'
set @sql = 'declare grc cursor for select rno,rent,male,female,child,eno  from ' + @temptable4
exec(@sql)
open grc
fetch next from grc into @rno,@rent,@male,@female,@child,@eno
if @@error<> 0 goto E_ERROR
while @@fetch_status=0
begin
------ individual details starts
if @foreign=1 
begin
set @sql = 'truncate table ' + @t2
exec(@sql)
if @@error<> 0 goto E_ERROR
set @sql = 'declare tcur cursor for select [name],nationality,pp_no,pp_issuedate,pp_issueplace,doa,port,employed,stayduration,purpose,regnno,regndate,regnoffice  from ' + @temptable2 + ' where rno =' + str(@rno)
exec(@sql)
open tcur
fetch next from tcur into @name1,@nation,@ppno,@ppdate,@ppissue,@doa,@port,@employed,@stayduration,@purpose,@regnno,@regndate,@regnoffice
if @@error<> 0 goto E_ERROR
while @@fetch_status=0
begin

set @sql = 'insert into '+  @t2 + ' (name,nationality,pp_no,pp_issuedate,pp_issueplace,doa,port,employed,stayduration,purpose,regnno,regndate,regnoffice) values (''' + @name1 + ''','''+ @nation + ''',''' + @ppno + ''',''' + convert(varchar(20),@ppdate) + ''',''' + @ppissue + ''',''' + convert(varchar(20),@doa) + ''',''' + @port  + ''','''  + @employed + ''',' + str(@stayduration) + ',''' + @purpose + ''',''' + @regnno + ''',''' + convert(varchar(20),@regndate) + ''',''' + @regnoffice + ''')'
exec(@sql)
if @@error<> 0 goto E_ERROR
fetch next from tcur into @name1,@nation,@ppno,@ppdate,@ppissue,@doa,@port,@employed,@stayduration,@purpose,@regnno,@regndate,@regnoffice
end
close tcur
deallocate tcur
end
else
begin
set @sql = 'truncate table ' + @t1
exec(@sql)
if @@error<> 0 goto E_ERROR
set @sql = 'declare tcur cursor for select [name],father,sex,age,job from ' + @temptable1 + ' where rno = ' + str(@rno)
exec (@sql)
if @@error<> 0 goto E_ERROR
open tcur
fetch next from tcur into @name1,@father,@sex,@age,@job
if @@error<> 0 goto E_ERROR
while @@fetch_status=0
begin
set @sql = 'insert into ' + @t1 + '([name],father,sex,age,job) values ('''+ @name1 + ''',''' + @father + ''',''' + @sex + ''',' + str(@age) + +','''+ @job + ''')'
exec(@sql)
if @@error<> 0 goto E_ERROR
fetch next from tcur into @name1,@father,@sex,@age,@job
end
close tcur
deallocate tcur
end
-------- individual details ends
declare @myctrlno numeric
select top 1 @myctrlno=myctrlno from sptbrs1 where ctrlno=@reservation and noofrooms-arrivedrooms > 0
if @advance<>0
   if @firstroom = 0 
      set @thisadvance = @firstroomadv
   else 
      set @thisadvance = @perroomadvance
/*
   if @perroomadvance < @advance
      set @thisadvance=@perroomadvance
  else
      begin
      set @thisadvance = @advance
       print convert(varchar(20),@thisadvance) goto e_error
     end
  
  set @advance=@advance-@thisadvance
*/
set @firstroom = 1 
set @ci_date2=@ci_date
exec pbpc1i  @RNO ,@OPT, @NAME ,@ADD1 ,@ADD2 ,@ADD3 ,@CITY,@STATE ,@PIN ,@PHONE ,@FAX ,@EMAIL,@RENT ,@DISCOUNT,
@SC ,@COMPANY ,@M_CNO,@CI_DATE2,@MALE,@FEMALE,@CHILD ,@FOREIGN,@ADDRESS ,@ENO ,
@foodplan  ,@T1 ,@T2 ,@TEMPTABLE3 ,@expecteddep,@grpname ,@addad,'Y',@myctrlno,@thisadvance,@ltcal,@fp_desc,@arrivedfrom,@destination
if @@error<> 0 goto E_ERROR
set @addad  = 'N '
fetch next from grc into @rno,@rent,@male,@female,@child , @eno
end
COMMIT TRANSACTION
goto final
final:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE1
EXEC(@SQL)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE2
EXEC(@SQL)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE3
EXEC(@SQL)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE4
EXEC(@SQL)
if exists(select [name],xtype from sysobjects where name = @t1 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t1
   exec(@sql)
  end
if exists(select [name],xtype from sysobjects where name = @t2 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t2
   exec(@sql)
  end
return 0
E_ERROR:
rollback transaction
if exists(select [name],xtype from sysobjects where name = @t1 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t1
   exec(@sql)
  end
if exists(select [name],xtype from sysobjects where name = @t2 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t2
   exec(@sql)
  end
raiserror('Group Checkin Failed',16,-1)
goto final



GO
/****** Object:  StoredProcedure [dbo].[pbpc2igUSELESS]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
CREATE PROCEDURE [dbo].[pbpc2igUSELESS] @OPT INT,@NAME VARCHAR(50),@ADD1 VARCHAR(50),@ADD2 VARCHAR(50),
@ADD3 VARCHAR(50),@CITY VARCHAR(50),@STATE VARCHAR(50),@PIN CHAR(10),@PHONE VARCHAR(50),
@FAX VARCHAR(50),@EMAIL VARCHAR(50),@DISCOUNT MONEY,@SC MONEY,
@COMPANY INT,@M_CNO INT,@CI_DATE DATETIME,
@FOREIGN INT,@ADDRESS CHAR(5),
@foodplan int ,@temptable1 VARCHAR(50),@TEMPTABLE2 VARCHAR(50),@TEMPTABLE3 VARCHAR(50),@TEMPTABLE4 VARCHAR(50),
@grpname varchar(30)='' ,@expecteddep datetime,@reservation numeric = 0,@advance money=0,@ltcal char(1)='Y',
@fp_desc varchar(100)='',@arrivedfrom varchar(50)='' , @destination varchar(50)=' ' 
/* with encryption */
as
declare @sql varchar(5000)
declare @rno int
declare @rent money
declare @male int
declare @female int
declare @child int
declare @eno int
declare @addad char(1)
declare @t1 varchar(50)
declare @t2 varchar(50)
declare @t3 varchar(50)
declare @name1 varchar(50)
declare @father varchar(50)
declare @sex char(1)
declare @age numeric
declare @job varchar(50)
declare @nation varchar(50)
declare @ppno varchar(50)
declare @ppdate varchar(50)
declare @ppissue varchar(50)
declare @doa datetime
declare @port varchar(50)
declare @employed char(1)
declare @stayduration int
declare @purpose varchar(50)
declare @regnno varchar(50)
declare @regndate varchar(50)
declare @regnoffice varchar(50)
declare @thisadvance money
declare @perroomadvance money
declare @ci_date2 datetime
declare @firstroomadv  money
declare @firstroom int
set @firstroom=0
set nocount on
set @t1 = ''
set @t2 = ''
if datepart(hh,@ci_date) = 12 and datepart(mi,@ci_date)=0 and datepart(ss,@ci_date)=0 
  begin
  print 'Time of check-in is found incorrect or exact [12:00:00]...This will affect the checkout calculations..pls correct the date'
  goto e_error
  end
begin transaction


declare @rnos numeric
set @sql = 'declare ttcur cursor for select count(*)  from '  + @temptable4
exec (@sql)
open ttcur
fetch next from ttcur into @rnos
close ttcur
deallocate ttcur
if @reservation <> 0   set @addad = 'N'  else if @opt = 0 or @opt = 2     set @addad = 'Y'  else     set @addad = 'N'
if @reservation <> 0
   if @advance <> 0
      begin
      set @perroomadvance = floor(@advance/@rnos)
      set @firstroomadv = @perroomadvance + (@advance - (@perroomadvance*@rnos))
      end
--print convert(varchar(20),@advance) + ' room nos' + str(@rnos) + ' per room ' + convert(varchar(20),@perroomadvance)
--goto e_error
 set @t1 = @temptable1 + '1'
set @t2 = @temptable2 + '1'
--print 'ramanathan'
if exists(select [name],xtype from sysobjects where name = @t1 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t1
   exec(@sql)
   if @@error<> 0 goto E_ERROR
  end
if exists(select [name],xtype from sysobjects where name = @t2 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t2
   exec(@sql)
   if @@error<> 0 goto E_ERROR
  end
set @sql = ' select * into ' + @t1 + ' from dummygd '
exec(@sql)
if @@error<> 0 goto E_ERROR
set @sql = ' select * into ' + @t2 + ' from dummypp '
exec(@sql)
if @@error<> 0 goto E_ERROR
--set @addad = 'Y'
set @sql = 'declare grc cursor for select rno,rent,male,female,child,eno  from ' + @temptable4
exec(@sql)
open grc
fetch next from grc into @rno,@rent,@male,@female,@child,@eno
if @@error<> 0 goto E_ERROR
while @@fetch_status=0
begin
------ individual details starts
if @foreign=1 
begin
set @sql = 'truncate table ' + @t2
exec(@sql)
if @@error<> 0 goto E_ERROR
set @sql = 'declare tcur cursor for select [name],nationality,pp_no,pp_issuedate,pp_issueplace,doa,port,employed,stayduration,purpose,regnno,regndate,regnoffice  from ' + @temptable2 + ' where rno =' + str(@rno)
exec(@sql)
open tcur
fetch next from tcur into @name1,@nation,@ppno,@ppdate,@ppissue,@doa,@port,@employed,@stayduration,@purpose,@regnno,@regndate,@regnoffice
if @@error<> 0 goto E_ERROR
while @@fetch_status=0
begin

set @sql = 'insert into '+  @t2 + ' (name,nationality,pp_no,pp_issuedate,pp_issueplace,doa,port,employed,stayduration,purpose,regnno,regndate,regnoffice) values (''' + @name1 + ''','''+ @nation + ''',''' + @ppno + ''',''' + convert(varchar(20),@ppdate) + ''',''' + @ppissue + ''',''' + convert(varchar(20),@doa) + ''',''' + @port  + ''','''  + @employed + ''',' + str(@stayduration) + ',''' + @purpose + ''',''' + @regnno + ''',''' + convert(varchar(20),@regndate) + ''',''' + @regnoffice + ''')'
exec(@sql)
if @@error<> 0 goto E_ERROR
fetch next from tcur into @name1,@nation,@ppno,@ppdate,@ppissue,@doa,@port,@employed,@stayduration,@purpose,@regnno,@regndate,@regnoffice
end
close tcur
deallocate tcur
end
else
begin
set @sql = 'truncate table ' + @t1
exec(@sql)
if @@error<> 0 goto E_ERROR
set @sql = 'declare tcur cursor for select [name],father,sex,age,job from ' + @temptable1 + ' where rno = ' + str(@rno)
exec (@sql)
if @@error<> 0 goto E_ERROR
open tcur
fetch next from tcur into @name1,@father,@sex,@age,@job
if @@error<> 0 goto E_ERROR
while @@fetch_status=0
begin
set @sql = 'insert into ' + @t1 + '([name],father,sex,age,job) values ('''+ @name1 + ''',''' + @father + ''',''' + @sex + ''',' + str(@age) + +','''+ @job + ''')'
exec(@sql)
if @@error<> 0 goto E_ERROR
fetch next from tcur into @name1,@father,@sex,@age,@job
end
close tcur
deallocate tcur
end
-------- individual details ends
declare @myctrlno numeric
select top 1 @myctrlno=myctrlno from sptbrs1 where ctrlno=@reservation and noofrooms-arrivedrooms > 0
if @advance<>0
   if @firstroom = 0 
      set @thisadvance = @firstroomadv
   else 
      set @thisadvance = @perroomadvance
/*
   if @perroomadvance < @advance
      set @thisadvance=@perroomadvance
  else
      begin
      set @thisadvance = @advance
       print convert(varchar(20),@thisadvance) goto e_error
     end
  
  set @advance=@advance-@thisadvance
*/
set @firstroom = 1 
set @ci_date2=@ci_date
exec pbpc1i  @RNO ,@OPT, @NAME ,@ADD1 ,@ADD2 ,@ADD3 ,@CITY,@STATE ,@PIN ,@PHONE ,@FAX ,@EMAIL,@RENT ,@DISCOUNT,
@SC ,@COMPANY ,@M_CNO,@CI_DATE2,@MALE,@FEMALE,@CHILD ,@FOREIGN,@ADDRESS ,@ENO ,
@foodplan  ,@T1 ,@T2 ,@TEMPTABLE3 ,@expecteddep,@grpname ,@addad,'Y',@myctrlno,@thisadvance,@ltcal,@fp_desc,@arrivedfrom,@destination
if @@error<> 0 goto E_ERROR
set @addad  = 'N '
fetch next from grc into @rno,@rent,@male,@female,@child , @eno
end
COMMIT TRANSACTION
goto final
final:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE1
EXEC(@SQL)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE2
EXEC(@SQL)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE3
EXEC(@SQL)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE4
EXEC(@SQL)
if exists(select [name],xtype from sysobjects where name = @t1 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t1
   exec(@sql)
  end
if exists(select [name],xtype from sysobjects where name = @t2 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t2
   exec(@sql)
  end
return 0
E_ERROR:
rollback transaction
if exists(select [name],xtype from sysobjects where name = @t1 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t1
   exec(@sql)
  end
if exists(select [name],xtype from sysobjects where name = @t2 and xtype = 'U')
   begin
   set @sql = 'drop table ' + @t2
   exec(@sql)
  end
raiserror('Group Checkin Failed',16,-1)
goto final




GO
/****** Object:  StoredProcedure [dbo].[pbpc3cm]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:48 AM ******/
CREATE PROCEDURE [dbo].[pbpc3cm]  @cno numeric,@mop int, @crname varchar(50), @crnum varchar(19), @crtype char(10), @glactno  int    
/* with encryption */
AS
declare @t1 varchar(50)
declare @t2 varchar(50)
declare @rv varchar(50)
declare @sql varchar(5000)
declare @sql1 varchar(5000)
declare @split char(1)
DECLARE @BCOMPANY INT
DECLARE @GNO INT
DECLARE @SECTION CHAR(1)
DECLARE @MAINBILLDATE DATETIME
DECLARE @GRNO INT
DECLARE @RET_VAL INT
DECLARE @FY INT
declare @spldiscount money
declare @eno numeric
declare @total money
declare @oldmop int
declare @opayment varchar(50)
declare @payment varchar(50)
declare @usernm varchar(50)
declare @m_coname varchar(50)
declare @foodbill money
set nocount on 
begin transaction
SELECT @foodbill=foodbill,@spldiscount=spldiscount,@GRNO=GRNO,@SPLIT=SPLIT,@BCOMPANY=COMPANY,@MAINBILLDATE=TXNDT  FROM BILLHIST1 WHERE CNO=@CNO
select @oldmop=mop,@opayment=ltrim(rtrim(payment))+' '+ltrim(rtrim(crname)),@total=total from billview where cno = @cno
EXEC @FY= GETFY  @mainbilldate,@bCOMPANY,@FY OUT
EXEC DELGRNO @bCOMPANY,@FY,@GRNO,@mainbilldate,@RET_VAL OUT
IF @@ERROR <> 0 GOTO E_ERROR
UPDATE BILLHIST1 SET MOP=@MOP,CRNAME=@CRNAME,CRNUM=@CRNUM,CRTYPE=@CRTYPE,@GRNO=GRNO,GLACTNO=@GLACTNO WHERE CNO=@CNO
if @oldmop=4
begin
     update roomdues set amount=0 where cno=@cno
     if @@error <> 0 goto e_error
     exec pproomdue @cno
     if @@error <> 0 goto e_error
     delete from roomdues where cno=@cno
end
exec pbvc @cno
if @@error <> 0  goto e_error
select @gno=max(gno) from billhist where cno=@cno
select @eno=eno from guest where gno=@gno
select @m_coname=name from company where company=@bcompany
--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @payment=ltrim(rtrim(payment))+' '+ltrim(rtrim(crname)),@total=total from billview where cno = @cno
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@m_coname))+ ' Entry No ' + ltrim(str(@eno)) + ' Dt. ' + convert(varchar(20),@mainbilldate,3) + ' Amt ' + ltrim(convert(varchar(20), @total)) +  ' MOP changed from '+ltrim(rtrim(@opayment)) + ' to ' + ltrim(rtrim(@payment))  ,3,@gno)
if @@error <> 0  goto e_error
commit transaction
return 
e_error:
rollback transaction
raiserror('MODE CHANGE OF BILL  failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbpc4dco]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo   Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo   Script Date: 1/20/03 11:51:49 AM ******/
CREATE PROCEDURE [dbo].[pbpc4dco] @gno numeric,@usernm varchar(50)='SYS'  
/* with encryption */
AS
DECLARE @SQL VARCHAR(5000)
DECLARE @RNO INT
DECLARE @COMPANY INT
DECLARE @GRNO INT
DECLARE @TXNDT DATETIME
DECLARE @FY INT
DECLARE @RET_VAL INT
DECLARE @GRPNAME VARCHAR(50)
declare @cno numeric
declare @eno numeric
declare @guestname varchar(50)
declare @bno numeric
declare @gno1 numeric
declare @cno1 numeric
declare @brno numeric
set nocount on
begin transaction
select @brno=brno,@txndt=co_date  from guest where gno=@gno
if @txndt=null goto e_error
declare c1 cursor for select gno from guest where brno=@brno
open c1
fetch next from c1 into @gno
while @@fetch_status=0 
begin
	update guest set co_date=null,grpcounter=0 where gno=@gno
	if @@error<>0 goto e_error
	update groom set co_date=NULL where gno=@gno and co_date=@txndt
	if @@error<>0 goto e_error
	select  @eno=eno,@cno=cno,@rno=rno,@grpname=grpname FROM GUEST WHERE GNO = @GNO
	if exists(select * from room where rno=@rno and gno<>0)
	begin
                       SELECT @GNO=GNO FROM ROOM WHERE RNO=@RNO
		print 'Room No ' + ltrim(str(@rno)) +   '  ' + LTRIM(STR(@GNO)) + '....... is not vacant now....Deletion of this bill not possible'
		goto e_error
	end
	update room set gno=@gno,status=' ',grpname=replace(@grpname,'''','''''') where rno=@rno
	if @@error<>0 goto e_error
           update crcardhold set status=' ' where gno=@gno and status='C' and holdtype=1
	if @@error<>0 goto e_error
            delete from addroom where gno=@gno
	if @@error<>0 goto e_error
              
	select @guestname=name from guest where gno = @gno
	fetch next from c1 into @gno
end
close c1
deallocate c1
declare tempc cursor for select company,grno,cno from billhist1 where  brno=@brno
open tempc
fetch next from tempc into @company,@grno,@cno
while @@fetch_status=0
begin
             delete from billhist where cno=@cno
             select @fy=0
	exec getfy @txndt,@company,@fy out
	if @@error<>0 goto e_error                              
	set @ret_val=0
	exec delgrno @company,@fy,@grno,@txndt,@ret_val out
             if @@error<>0 goto e_error
	if @ret_val=1 goto e_error
	fetch next from tempc into @company,@grno,@cno
end
close tempc
deallocate tempc
set @bno=0
select @bno=bno from billhist1 where brno=@brno and company=1 and split NOT IN ('Y','F','C')
if @bno is null set @bno=0
if exists (select * from roomdues where cno in (select cno from billhist1 where brno=@brno))
   begin
   declare c1 cursor for select cno from roomdues where cno in (select cno from billhist1 where brno=@brno)
   open c1
   fetch next from c1 into @cno1
   while @@fetch_status=0
   begin
   update roomdues set amount=0 where cno=@cno1
   if @@error<>0 goto e_error
   exec pproomdue @cno1 
   if @@error<>0 goto e_error
   fetch next from c1 into @cno1
   end
   close c1
   deallocate c1 
   delete from roomdues where cno in (select cno from billhist1 where brno=@brno)
   if @@error<>0 goto e_error
   end
 
delete from billhist1 where brno=@brno
if @@error<>0 goto e_error
--select @usernm=isnull(usernm,'') from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
declare @narration varchar(200)
set @narration= 'Final Bill Deleted Bill No.'  + ltrim(str(@bno)) + ' GRNO ' +ltrim(str(@eno))  + ' Guest Name-' + @guestname
insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm,@narration  ,1,@gno)
if @@error<>0 
begin
print 'Error in updating accesslog'
goto e_error
end
COMMIT TRANSACTION
FINAL:
RETURN
E_ERROR:
ROLLBACK TRANSACTION
RAISERROR('DELETION OF BILLS FAILED..(Can be due reversal of SUB-GL accounts also..Please Check',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[PBPChargeCard]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[PBPChargeCard] @rno numeric 
/* with encryption */
AS
declare @gno numeric
declare @t1 varchar(50)
declare @rv varchar(50)
declare @sql varchar(5000)
declare @outlet numeric
declare @amt money
select @gno=gno from room where rno=@rno
select @outlet=cardChargeOutlet,@amt=CardChargeAmt from setting
if isnull(@amt,0)=0  return
begin transaction

if @gno=0 
begin
	print 'The room is vacant now'
	goto e_error
end

exec randfile @rv out
select @t1 = 'tl' + ltrim(@RV)
set @sql = 'select * into ' + @t1 + ' from dummyph'
exec (@sql)
if @@error<>0 goto e_error

set @sql = 'insert into ' + @t1 + ' (detail,amount) values (''Charges for Extra Key Card'',' + str(@amt) + ')'
exec(@sql)
if @@error <> 0 goto e_error
set @sql =  'update comuser set control=''Y'' where [host_id]=host_id() declare @ocno int exec pbpp1u  ''' + @t1 + ''' ,' + str(@outlet) + ',''' + convert(varchar(20),getdate()) + ''',2,0, ' + str(@gno) + ','''','''','''',0, @ocno out'	
exec(@sql)
if @@error<>0 goto e_error
commit transaction

return
e_error:
rollback transaction
raiserror('Error in card charging',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbpe1u]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[pbpe1u]     @S_EXTN varchar(6), @E_EXTN varchar(6),@PHONE_NUM varchar(30),@AMOUNT money,@STATION char(2) ,
@FROM_TIME varchar(8),@TO_TIME varchar(10),@DURATION numeric ,@IS_ISD char(1),@COUNTRY varchar(30),@CITY varchar(30),@PULSE int,
@gno numeric,@direction char(1)='O'
/* with encryption */
 AS
--print 'periya'
declare @ident numeric
begin transaction
insert into epbx ([date],s_extn,e_extn,phone_num,amount,station,from_time,to_time,duration,is_isd,country,city,pulse,gno,direction)
values(getdate(),@s_extn,@s_extn,@phone_num,@amount,@station,@from_time,@to_time,@duration,@is_isd,@country,@city,@pulse,@gno,@direction)
if @@error<>0 goto e_error
set @ident = @@identity
if @direction='O'
	begin
	declare @number varchar(50)
	declare @amt money
	declare @t1 varchar(50)
	declare @sql varchar(500)
	declare @rv varchar(50)
	declare @extn char(4)
	declare @outlet int
	declare @ocno int
	declare @strate money
	declare @stamt money
	declare @sttableno int 
	declare @cess1 money
	declare @cess2 money

	EXEC RANDFILE @RV OUT
	select @t1 = 'tl' + ltrim(@RV)
	SET @SQL = 'SELECT * INTO ' + @T1 + '  FROM  DUMMYph'
	EXEC (@SQL)
	if @@error<>0 goto e_error
	select @extn=@e_extn,@number=@phone_num,@amt=@amount
	if exists(select * from room where extn = @e_extn and gno=0)
	  update epbx set status='U' where ctrlno=@ident
	  if @@error <> 0 goto e_error
	--print 'preiya'
	--goto e_error
	if @gno<>0 and @amt <> 0
		begin
			if not exists(select * from EXTNNUMBERS where cast(extn as numeric)=cast(@phone_num as numeric))
				begin
				set @sql = 'insert into ' + @t1 + ' (detail,amount) values (''' + ltrim(str(@duration))+'Secs - '+ltrim(@number) + ''' , ' + convert(varchar(20),@amt) + ')'
				exec(@sql)
				if @@error <> 0 goto e_error
				if substring(@number,1,1) = '0' 
					select @outlet=outlet from sptbph where calltype=2
				else
					select @outlet=outlet from sptbph where calltype=1

				select @sttableno=sttableno from phoutlet where outlet=@outlet

				declare @txndt varchar(20)
				set @txndt = convert(varchar(20),getdate())
				select @strate=rate from sttabler where  sttableno=@sttableno and (@amt >= samt and @amt <= eamt ) and datediff(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt) 

				exec GetServiceTaxR @amt,@txndt,@stamt out,@cess1 out,@outlet,@sttableno,@cess2 out

				set @sql =  'declare @ocno int exec pbpp1u  ''' + @t1 + ''' ,' + str(@outlet) + ',''' + convert(varchar(20),getdate()) + ''',2,0, ' + str(@gno) + ','''','''','''',0, @ocno out,0,0,' + convert(varchar(20),@strate) + ','+ convert(varchar(20),@stamt)
				exec(@sql)
				if @@error<>0 goto e_error
				end
			else 
				begin
					set @sql = 'drop table ' + @t1
					exec(@SQL)
					if @@error <> 0 goto e_error
				end
		end
	ELSE
		BEGIN
			set @sql = 'drop table ' + @t1
			exec(@SQL)
			if @@error <> 0 goto e_error
		END
end
commit transaction
return
e_error:
rollback transaction
raiserror('error in epbx updation',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbpga]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[pbpga] @dt datetime,@dt2 datetime ,@cnt numeric output,@dt3 datetime out  
/* with encryption */
AS
select * into #dummy from dummydt
set @dt2 = dateadd(s,-1,@dt2)
truncate table #dummy
declare @d datetime
declare @tr int
/*
set @d=@dt
while datediff(d,@d,@dt2) > -1
begin
insert into #dummy (dt) values (@d)
set @d = dateadd(d,1,@d)
end
*/

set @d=@dt
while datediff(d,@d,@dt2) > -1
begin
set @tr=isnull((select count(*) from room),0)-isnull((select count(*) from room where status NOT in (' ','A') and datediff(d,'1/1/90',isnull(statusupto,getdate())) > datediff(d,'1/1/90',@d)),0)
insert into #dummy (dt,tr) values (@d,@tr)
set @d = dateadd(d,1,@d)
end

select expecteddep into #dumgus from guest where gno in (select gno from room where gno <> 0)
select @tr=count(*) from  room
select * into #sp1 from (
/* Daywise calculation
select arrivaldate,departuredate,noofrooms,arrivedrooms,cancelledrooms  from sptbrs join sptbrs1 on sptbrs.ctrlno=sptbrs1.ctrlno
where noofrooms > (arrivedrooms+cancelledrooms) and status<>'C' and 
((datediff(d,'1/1/90',ARRIVALDATE) between
datediff(d,'1/1/90',@dt) and datediff(d,'1/1/90',@dt2))
or
(datediff(d,'1/1/90',departuredate) between
datediff(d,'1/1/90',@dt) and datediff(d,'1/1/90',@dt2))
or
(datediff(d,arrivaldate,@dt) > 0 and datediff(d,@dt2,departuredate) > 0) )
) a where datediff(d,getdate(),arrivaldate) >= 0
select cnt ,dt into #dummy3   from 
(select dt,(
select tr-isnull(count(*),0)-
isnull((select sum(isnull(noofrooms,0)-isnull(cancelledrooms,0)-isnull(arrivedrooms,0)) from #sp1 where datediff(d,'1/1/90',dt) between
datediff(d,'1/1/90',arrivaldate) and (datediff(d,'1/1/90',departuredate)-1)),0)
cnt 
from #dumgus where datediff(d,'1/1/90',expecteddep) >
datediff(d,'1/1/90',dt) ) cnt 
from #dummy where datediff(d,'1/1/90',dt) between datediff(d,'1/1/90',@dt)
and datediff(d,'1/1/90',@dt2)) a
*/
/* secondwise calculation*/
select arrivaldate,departuredate,noofrooms,arrivedrooms,cancelledrooms  from sptbrs join sptbrs1 on sptbrs.ctrlno=sptbrs1.ctrlno
where noofrooms > (arrivedrooms+cancelledrooms) and status<>'C' and 
((datediff(s,'1/1/90',ARRIVALDATE) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,'1/1/90',departuredate) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,arrivaldate,@dt) > 0 and datediff(s,@dt2,departuredate) > 0) )
) a where datediff(s,getdate(),arrivaldate) >= 0
select cnt ,dt into #dummy3   from 
(select dt,(
select tr-isnull(count(*),0)-
isnull((select sum(isnull(noofrooms,0)-isnull(cancelledrooms,0)-isnull(arrivedrooms,0)) from #sp1 where datediff(s,'1/1/90',dt) between
datediff(s,'1/1/90',arrivaldate) and (datediff(s,'1/1/90',departuredate)-1)),0)
cnt 
from #dumgus where datediff(s,'1/1/90',expecteddep) >
datediff(s,'1/1/90',dt) ) cnt 
from #dummy where datediff(s,'1/1/90',dt) between datediff(s,'1/1/90',@dt)
and datediff(s,'1/1/90',@dt2)) a



select  @cnt= cnt ,@dt3= dt from #dummy3 where cnt=(select min(cnt) from #dummy3)
 
return


GO
/****** Object:  StoredProcedure [dbo].[pbpga1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbpga1] @dt datetime,@dt2 datetime  , @f1 varchar(50) = ''
/* with encryption */
AS
set @dt2 = dateadd(d,-1,@dt2)
select * into #dummy from dummydt
truncate table #dummy
declare @d datetime
declare @tr int
set @d=@dt
while datediff(d,@d,@dt2) > -1
begin
set @tr=isnull((select count(*) from room),0)-isnull((select count(*) from room where status NOT in (' ','A') and datediff(d,'1/1/90',isnull(statusupto,getdate())) > datediff(d,'1/1/90',@d)),0)
insert into #dummy (dt,tr) values (@d,@tr)
set @d = dateadd(d,1,@d)
end

--select * into tempdum from #dummy

select expecteddep,roomtype=WINGS.DESCRIPTION into #dumgus from room join guest on guest.gno=room.gno  JOIN WINGS ON WINGNO=ROOMWING where room.gno<>0   --guest.gno in (select gno from room where gno <> 0)
--select @tr=count(*) from  room
select * into #sp1 from (
select arrivaldate,departuredate,noofrooms,arrivedrooms,cancelledrooms  from sptbrs join sptbrs1 on sptbrs.ctrlno=sptbrs1.ctrlno
where 
noofrooms > (arrivedrooms+cancelledrooms) and  status <> 'C' and 
((datediff(d,'1/1/90',ARRIVALDATE) between
datediff(d,'1/1/90',@dt) and datediff(d,'1/1/90',@dt2))
or
(datediff(d,'1/1/90',departuredate) between
datediff(d,'1/1/90',@dt) and datediff(d,'1/1/90',@dt2))
or
(datediff(d,arrivaldate,@dt) > 0 and datediff(d,@dt2,departuredate) > 0) )
) a where datediff(d,getdate(),arrivaldate) >= 0

-- here is the final collection
select @tr=count(*) from room

select convert(varchar(20),dt,6) dt ,@tr- isnull(tr,0) tr,isnull(cr,0) cr,isnull(rr,0) rr ,tr-(isnull(cr,0)+isnull(rr,0)) ar  into #dummy2 from (
select dt, tr,
(select count(*)  from #dumgus where datediff(d,'1/1/90',expecteddep) >
datediff(d,'1/1/90',dt)  ) cr,
(select sum(isnull(noofrooms,0)-isnull(cancelledrooms,0)-isnull(arrivedrooms,0)) from #sp1 where datediff(d,'1/1/90',dt) between
datediff(d,'1/1/90',arrivaldate) and (datediff(d,'1/1/90',departuredate)-1))
 rr
from #dummy where datediff(d,'1/1/90',dt) between datediff(d,'1/1/90',@dt)
and datediff(d,'1/1/90',@dt2)) a
--drop table t123456
--select * into t123456 from #dummy2
if @f1<>'' 
begin
declare @sql varchar(2000)
if exists (select name,xtype from sysobjects where name = @f1  and xtype = 'U') exec ('drop table ' +  @f1)
set @sql = 'select * into ' +  @f1 + ' from #dummy2'
exec(@sql)
end
select * from #dummy2
return

GO
/****** Object:  StoredProcedure [dbo].[pbpga1_hourly]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbpga1_hourly] @dt datetime  
/* with encryption */
AS
declare @dt2 datetime
set @dt = str(day(@dt)) + ' ' + datename(mm,@dt) + ' ' + str(year(@dt)) + ' 00:00:00'
set @dt2 = str(day(@dt)) + ' ' + datename(mm,@dt) + ' ' + str(year(@dt)) + ' 23:00:00'
select * into #dummy from dummydt
truncate table #dummy
declare @d datetime
declare @x int
set @d=@dt
set @x = 0
while @x <= 23
begin
insert into #dummy (dt) values (@d)
set @d = dateadd(hh,1,@d)
set @x=@x+1
end

select expecteddep,roomtype=WINGS.DESCRIPTION into #dumgus from room join guest on guest.gno=room.gno  JOIN WINGS ON WINGNO=ROOMWING where room.gno<>0   --guest.gno in (select gno from room where gno <> 0)
declare @tr int
select @tr=count(*) from  room

select * into #sp1 from (
select arrivaldate,departuredate,noofrooms,arrivedrooms,cancelledrooms  from sptbrs join sptbrs1 on sptbrs.ctrlno=sptbrs1.ctrlno
where 
noofrooms > (arrivedrooms+cancelledrooms) and  status <> 'C' and 
((datediff(hh,'1/1/90',ARRIVALDATE) between
datediff(hh,'1/1/90',@dt) and datediff(hh,'1/1/90',@dt2))
or
(datediff(hh,'1/1/90',departuredate) between
datediff(hh,'1/1/90',@dt) and datediff(hh,'1/1/90',@dt2))
or
(datediff(hh,arrivaldate,@dt) > 0 and datediff(hh,@dt2,departuredate) > 0) )
) a where datediff(hh,getdate(),arrivaldate) >= 0

select convert(varchar(20),dt,8) dt ,isnull(cr,0) cr,isnull(rr,0) rr ,tr-(isnull(cr,0)+isnull(rr,0)) ar  into #dummy2 from (
select dt, @tr tr,
(select count(*)  from #dumgus where datediff(hh,'1/1/90',expecteddep) >
datediff(hh,'1/1/90',dt)  ) cr,
(select sum(isnull(noofrooms,0)-isnull(cancelledrooms,0)-isnull(arrivedrooms,0)) from #sp1 where datediff(hh,'1/1/90',dt) between
datediff(hh,'1/1/90',arrivaldate) and (datediff(hh,'1/1/90',departuredate)-1))
 rr
from #dummy where datediff(hh,'1/1/90',dt) between datediff(hh,'1/1/90',@dt)
and datediff(hh,'1/1/90',@dt2)) a


select * from #dummy2
return




GO
/****** Object:  StoredProcedure [dbo].[pbpga1new]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbpga1new] @dt datetime,@dt2 datetime  
/* with encryption */
AS
set @dt2 = dateadd(n,-1,@dt2)
select * into #dummy from dummydt
truncate table #dummy
declare @d datetime
set @d=@dt
while datediff(n,@d,@dt2) > -1
begin
insert into #dummy (dt) values (@d)
set @d = dateadd(n,1,@d)
end
select expecteddep,roomtype=WINGS.DESCRIPTION into #dumgus from room join guest on guest.gno=room.gno  JOIN WINGS ON WINGNO=ROOMWING where room.gno<>0   --guest.gno in (select gno from room where gno <> 0)
declare @tr int
select @tr=count(*) from  room
select * into #sp1 from (
select arrivaldate,departuredate,noofrooms,arrivedrooms,cancelledrooms  from sptbrs join sptbrs1 on sptbrs.ctrlno=sptbrs1.ctrlno
where 
noofrooms > (arrivedrooms+cancelledrooms) and  status <> 'C' and 
((datediff(n,'1/1/90',ARRIVALDATE) between
datediff(n,'1/1/90',@dt) and datediff(n,'1/1/90',@dt2))
or
(datediff(n,'1/1/90',departuredate) between
datediff(n,'1/1/90',@dt) and datediff(n,'1/1/90',@dt2))
or
(datediff(n,arrivaldate,@dt) > 0 and datediff(n,@dt2,departuredate) > 0) )
) a where datediff(n,getdate(),arrivaldate) >= 0
select convert(varchar(20),dt,6) dt ,isnull(cr,0) cr,isnull(rr,0) rr ,tr-(isnull(cr,0)+isnull(rr,0)) ar  into #dummy2 from (
select dt, @tr tr,
(select count(*)  from #dumgus where datediff(n,'1/1/90',expecteddep) >
datediff(n,'1/1/90',dt)  ) cr,
(select sum(isnull(noofrooms,0)-isnull(cancelledrooms,0)-isnull(arrivedrooms,0)) from #sp1 where datediff(n,'1/1/90',dt) between
datediff(n,'1/1/90',arrivaldate) and (datediff(n,'1/1/90',departuredate)-1))
 rr
from #dummy where datediff(n,'1/1/90',dt) between datediff(n,'1/1/90',@dt)
and datediff(n,'1/1/90',@dt2)) a
select * from #dummy2
return




GO
/****** Object:  StoredProcedure [dbo].[pbpga2]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO






CREATE PROCEDURE [dbo].[pbpga2] @dt datetime,@dt2 datetime ,@t123 varchar(50)='temp12345678'
/* with encryption */
AS
-- test for category wise room status 24-feb-2006 Rm.Ramanathan
--set @dt2 = dateadd(s,-1,@dt2)
declare @sql varchar(2000)
set @sql = 'if exists (select name,xtype from sysobjects where name = ''' + @t123 +  ''' and xtype = ''U'') drop table ' + @t123 
exec (@sql )

select * into #dummy from dummydt2
truncate table #dummy
declare @d datetime
declare @roomtype varchar(50)
declare @cnt2 numeric
declare @br numeric --blocked rooms

declare @mstr varchar(20)
declare @g_12noonstr varchar(20)
select @g_12noonstr=g_12noonstr from SETTING 

set @MSTR =  replace(str(cast(substring(@g_12noonstr, 1, 2) as int)-1,2),space(1),'0')  +  ':' + '59'
---------------------------
set @dt = convert(varchar(20),@dt,106) + ' ' +  @mstr
set @dt2 = convert(varchar(20),@dt2,106) + ' ' +  @g_12noonstr

set @d=@dt
while datediff(d,@d,@dt2) > 0
	begin
	declare c1 cursor for select wings.description [roomtype], count(*) cnt  from room join wings on room.roomwing=wings.wingno  group by wings.description
	open c1
	fetch next  from c1 into @roomtype,@cnt2
	while @@fetch_status=0
		begin
		set @br = isnull((select count(*) from room where roomtype=@roomtype and gno=0 and status NOT in (' ','A') and datediff(d,'1/1/90',isnull(statusupto,getdate())) > datediff(d,'1/1/90',@d)),0)
		-- The above line was introdued on 18-Oct-2012 to make the available rooms - (minus) Blocked rooms
		insert into #dummy (dt,roomtype,tr) values (@d,@roomtype,@cnt2-@br )
		fetch next from c1 into @roomtype,@cnt2
		end
		close c1
		deallocate c1
		set @d = dateadd(d,1,@d)
	end


select expecteddep,roomtype into #dumgus from guest join room on guest.gno=room.gno where guest.gno in (select gno from room where gno <> 0)
declare @tr int
select @tr=count(*) from  room

---------------------------
declare @ad datetime
declare @dd datetime
declare @nr1 int 
declare @rt varchar(50) 

declare c1 cursor for 
select arrivaldate,departuredate,nor=noofrooms - (arrivedrooms + cancelledrooms),
roomtype  from sptbrs join sptbrs1 on sptbrs.ctrlno=sptbrs1.ctrlno
where noofrooms > (arrivedrooms+cancelledrooms) and status <> 'C' and 
datediff(d,getdate(),arrivaldate) >= 0  and
( (datediff(s,'1/1/90',ARRIVALDATE) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,'1/1/90',departuredate) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,arrivaldate,@dt) > 0 and datediff(s,@dt2,departuredate) > 0)) 
declare @nr2 int
create table #dum (dt datetime,roomtype varchar(50)  collate SQL_Latin1_General_CP1_CI_AS)
open c1
fetch next from c1 into @ad,@dd,@nr1  ,@rt
while @@FETCH_STATUS=0
begin
	while DATEDIFF(d,@ad,@dd) > 0
	begin
	   set @nr2 = @nr1
	    while @nr2 > 0
	      begin
			insert into #dum (dt,ROOMTYPE) values (@ad,@rt)
			set @nr2 = @nr2 -1 
		  end
		set @ad = DATEADD(d,1,@ad)
	end
	fetch next from c1 into @ad,@dd,@nr1  ,@rt
end
CLOSE C1
DEALLOCATE C1

select count(*) cnt, min(dt) dt,roomtype into #sp1 from #dum group by DATEDIFF(d,'1/1/2000',dt),roomtype 
select convert(varchar(20),dt,6) [Date] ,roomtype [Room Type],   tr [Total Rooms],er [Continuing Rooms],rr [Reserved Rooms]  
,tr-(er+rr) [Available Rooms]  into #dummy2 from (
select dt ,roomtype,tr,
isnull((select count(*) from #dumgus where datediff(s,'1/1/2000',expecteddep) > datediff(s,'1/1/2000',dt)  and  roomtype= #dummy.roomtype),0) er,
isnull((select isnull(cnt,0) from #sp1 where datediff(d,dt,#dummy.dt)=0  
and roomtype= #dummy.roomtype ),0) rr 
from #dummy where datediff(s,'1/1/2000',dt) between datediff(s,'1/1/2000',@dt)
and (datediff(s,'1/1/2000',@dt2)-1)  ) a 




Select outert.*
,wings.staahcode,notreadyrooms=(select count(*) from room where status<>'' and room.roomtype=outert.[room type])
from #dummy2 outert 
inner join 

( 
 Select t.[Room Type] [Room Type], min(xx.[Available Rooms]) [Available Rooms], 
 min(Date) as Date from 
 #dummy2 t

  inner join 
    (   Select [Room Type], min([Available Rooms]) as [Available Rooms]
        from #dummy2 
        group by [Room Type]
     ) xx 
    on t.[Room Type] = xx.[Room Type] and t.[Available Rooms] = xx.[Available Rooms]
 group by t.[Room Type], xx.[Available Rooms]
) outerx
on outerx.[Room Type] = outert.[Room Type] 
and outerx.[Available Rooms] = outert.[Available Rooms]
and outerx.Date = outert.Date 

join wings on wings.description=outert.[Room Type]

order by outert.[Room Type]

if @t123 <> 'temp12345678' exec ('select  
[Room Type] [Room Type], min([Available Rooms]) [Available Rooms],Notreadyrooms=(select count(*) from room where room.roomtype=t.roomtype)  
into ' + @t123 + '  from #dummy2 t group by [Room Type] ')
--select * from #sp1 
return
e_error:
raiserror('error',16,-1)



GO
/****** Object:  StoredProcedure [dbo].[pbpga3]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbpga3] @dt datetime,@dt2 datetime ,@category varchar(50) ,@ar int out 
/* with encryption */
AS
-- test for category wise room status 24-feb-2006 Rm.Ramanathan
set nocount on
set @dt2 = dateadd(d,-1,@dt2)
select * into #dummy from dummydt2
truncate table #dummy
declare @d datetime
declare @roomtype varchar(50)
declare @cnt2 numeric
set @d=@dt
while datediff(d,@d,@dt2) > -1
	begin
	declare c1 cursor for select wings.description [roomtype], count(*) cnt  from room join wings on room.roomwing=wings.wingno group by wings.description
	open c1
	fetch next  from c1 into @roomtype,@cnt2
	while @@fetch_status=0
		begin
		insert into #dummy (dt,roomtype,tr) values (@d,@roomtype,@cnt2)
		fetch next from c1 into @roomtype,@cnt2
		end
		close c1
		deallocate c1
		set @d = dateadd(d,1,@d)
	end
      
select convert(varchar(20),dt,6) [Date] ,roomtype [Room Type],   tr [Total Rooms],er [Continuing Rooms],rr [Reserved Rooms]   ,tr-(er+rr) [Available Rooms]  into #dummy2 from (
select dt ,roomtype,tr,
isnull((select count(*) from room_guest where datediff(d,'1/1/90',expecteddep) > datediff(d,'1/1/90',dt)  and  roomtype= #dummy.roomtype),0) er,
isnull((select sum(isnull(noofrooms,0)-isnull(cancelledrooms,0)-isnull(arrivedrooms,0)) from sptbrs_v1 where datediff(d,'1/1/90',dt) between
datediff(d,'1/1/90',arrivaldate) and (datediff(d,'1/1/90',departuredate)-1)  and roomtype= #dummy.roomtype ),0) rr 
from #dummy where datediff(d,'1/1/90',dt) between datediff(d,'1/1/90',@dt)
and datediff(d,'1/1/90',@dt2)   ) a
select @ar=[available rooms]  from #dummy2 where [room type]=@category
set nocount off
return




GO
/****** Object:  StoredProcedure [dbo].[pbpga5]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[pbpga5] @dt datetime,@dt2 datetime,  @opt int=0 
/* with encryption */
AS
-- test for category wise room status 24-feb-2006 Rm.Ramanathan

set @dt2 = dateadd(s,-1,@dt2)
select * into #dummy from dummydt2
truncate table #dummy
declare @d datetime
declare @roomtype varchar(50)
declare @cnt2 numeric
declare @nr numeric
set @d=@dt
while datediff(s,@d,@dt2) > -1
	begin
	declare c1 cursor for select roomtype, count(*) cnt  from room  group by roomtype
	open c1
	fetch next  from c1 into @roomtype,@cnt2
	while @@fetch_status=0
		begin
		select @nr=count(*) from room where status not in ('','A') and datediff(s,'1/1/90',isnull(statusupto,getdate())) > datediff(s,'1/1/90',@d) and roomtype=@roomtype
		insert into #dummy (dt,roomtype,tr) values (@d,@roomtype,@cnt2-@nr)
		fetch next from c1 into @roomtype,@cnt2
		end
		close c1
		deallocate c1
		set @d = dateadd(d,1,@d)
	end
select expecteddep,roomtype into #dumgus from guest join room on guest.gno=room.gno  where guest.gno in (select gno from room where gno <> 0)
declare @tr int
select @tr=count(*) from  room

select * into #sp11 from (
select arrivaldate,departuredate,noofrooms,arrivedrooms,cancelledrooms,roomtype  from sptbrs join sptbrs1 on sptbrs.ctrlno=sptbrs1.ctrlno
where noofrooms > (arrivedrooms+cancelledrooms) and status <> 'C' and 
( (datediff(s,'1/1/90',ARRIVALDATE) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,'1/1/90',departuredate) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,arrivaldate,@dt) > 0 and datediff(s,@dt2,departuredate) > 0) )

) a where datediff(s,getdate(),arrivaldate) >= 0


declare @ad datetime
declare @dd datetime
declare @nr1 int 
declare @rt varchar(50) 

declare c1 cursor for 
select arrivaldate,departuredate,nor=noofrooms - (arrivedrooms + cancelledrooms),
roomtype  from sptbrs join sptbrs1 on sptbrs.ctrlno=sptbrs1.ctrlno
where noofrooms > (arrivedrooms+cancelledrooms) and status <> 'C' and 
( (datediff(s,'1/1/90',ARRIVALDATE) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,'1/1/90',departuredate) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,arrivaldate,@dt) > 0 and datediff(s,@dt2,departuredate) > 0)) 
declare @nr2 int
create table #dum (dt datetime,roomtype varchar(50))
open c1
fetch next from c1 into @ad,@dd,@nr1  ,@rt
while @@FETCH_STATUS=0
begin
	while DATEDIFF(d,@ad,@dd) > 0
	begin
set @nr2 = @nr1
	    while @nr2 > 0
	      begin
			insert into #dum (dt,ROOMTYPE) values (@ad,@rt)
			set @nr2 = @nr2 -1 
		  end
		set @ad = DATEADD(d,1,@ad)
	end
	fetch next from c1 into @ad,@dd,@nr1  ,@rt
end
CLOSE C1
DEALLOCATE C1

select count(*) nr, min(dt) dt,roomtype into #sp1 from #dum group by DATEDIFF(d,'1/1/2000',dt),roomtype


if @opt=0  
begin   
select convert(varchar(20),dt,6) [Date] ,roomtype [Room Type],   tr [Total Rooms],er [Continuing Rooms],rr+rr1 [Reserved Rooms]   ,tr-(er+rr+rr1) [Available Rooms]  into #dummy2 from (
select dt ,roomtype,tr,
isnull((select count(*) from #dumgus where datediff(s,'1/1/90',expecteddep) > datediff(s,'1/1/90',dt)  and  roomtype= #dummy.roomtype),0) er,
isnull((select max(nr) from 
#sp1 where datediff(d,'1/1/90',dt) between datediff(d,'1/1/90',@dt) and (datediff(d,'1/1/90',@dt2)-1)  
and roomtype= #dummy.roomtype ),0) rr ,
isnull((select sum(noofrooms) from blockrooms where datediff(s,'1/1/90',dt) between
datediff(s,'1/1/90',fromdate) and (datediff(s,'1/1/90',todate)-1)  and roomtype= #dummy.roomtype ),0) rr1
from #dummy where datediff(s,'1/1/90',dt) between datediff(s,'1/1/90',@dt)
and datediff(s,'1/1/90',@dt2)   ) a
end
else
begin
select convert(varchar(20),dt,6) [Date] ,roomtype [Room Type],   tr [Total Rooms],er [Continuing Rooms],rr [Reserved Rooms]   ,tr-(er+rr) [Available Rooms]  into #dummy3 from (
select dt ,roomtype,tr,
isnull((select count(*) from #dumgus where datediff(s,'1/1/90',expecteddep) > datediff(s,'1/1/90',dt)  and  roomtype= #dummy.roomtype),0) er,
isnull((select sum(isnull(nr,0)) from #sp1 where datediff(s,'1/1/90',dt) between
datediff(s,'1/1/90',@dt) and (datediff(s,'1/1/90',@dt2)-1)  and roomtype= #dummy.roomtype ),0) rr 
from #dummy where datediff(s,'1/1/90',dt) between datediff(s,'1/1/90',@dt)
and datediff(s,'1/1/90',@dt2)   ) a
end
if @opt=0
select                [Room Type]  ,[Available Rooms]=min([available rooms])         from        #dummy2   where [Room Type] in (select description from wings where status<>'C') group by [Room Type]
else
select                [Room Type]  ,[Available Rooms]=min([available rooms])         from        #dummy3   group by [Room Type]
return
e_error:
raiserror('error',16,-1)





GO
/****** Object:  StoredProcedure [dbo].[pbpga6]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbpga6] @dt datetime,@dt2 datetime,  @opt int=0 , @roomtyperqd varchar(50) , @ar int out 
/* with encryption */
AS
-- test for category wise room status 24-feb-2006 Rm.Ramanathan
set @dt2 = dateadd(d,-1,@dt2)
select * into #dummy from dummydt2
truncate table #dummy
declare @d datetime
declare @roomtype varchar(50)
declare @cnt2 numeric
set @d=@dt
while datediff(d,@d,@dt2) > -1
	begin
	declare c1 cursor for select wings.description [roomtype], count(*) cnt  from room join wings on room.roomwing=wings.wingno group by wings.description
	open c1
	fetch next  from c1 into @roomtype,@cnt2
	while @@fetch_status=0
		begin
		insert into #dummy (dt,roomtype,tr) values (@d,@roomtype,@cnt2)
		fetch next from c1 into @roomtype,@cnt2
		end
		close c1
		deallocate c1
		set @d = dateadd(d,1,@d)
	end
select expecteddep,roomtype into #dumgus from guest join room on guest.gno=room.gno  where guest.gno in (select gno from room where gno <> 0)
declare @tr int
select @tr=count(*) from  room
select * into #sp1 from (
select arrivaldate,departuredate,noofrooms,arrivedrooms,cancelledrooms,roomtype  from sptbrs join sptbrs1 on sptbrs.ctrlno=sptbrs1.ctrlno
where noofrooms > (arrivedrooms+cancelledrooms) and status <> 'C' and 
 (datediff(d,'1/1/90',ARRIVALDATE) between
datediff(d,'1/1/90',@dt) and datediff(d,'1/1/90',@dt2))
or
(datediff(d,'1/1/90',departuredate) between
datediff(d,'1/1/90',@dt) and datediff(d,'1/1/90',@dt2))
or
(datediff(d,arrivaldate,@dt) > 0 and datediff(d,@dt2,departuredate) > 0) 
) a where datediff(d,getdate(),arrivaldate) >= 0
if @opt=0  
begin   
select convert(varchar(20),dt,6) [Date] ,roomtype [Room Type],   tr [Total Rooms],er [Continuing Rooms],rr+rr1 [Reserved Rooms]   ,tr-(er+rr+rr1) [Available Rooms]  into #dummy2 from (
select dt ,roomtype,tr,
isnull((select count(*) from #dumgus where datediff(d,'1/1/90',expecteddep) > datediff(d,'1/1/90',dt)  and  roomtype= #dummy.roomtype),0) er,
isnull((select sum(isnull(noofrooms,0)-isnull(cancelledrooms,0)-isnull(arrivedrooms,0)) from #sp1 where datediff(d,'1/1/90',dt) between
datediff(d,'1/1/90',arrivaldate) and (datediff(d,'1/1/90',departuredate)-1)  and roomtype= #dummy.roomtype ),0) rr ,
isnull((select sum(noofrooms) from blockrooms where datediff(d,'1/1/90',dt) between
datediff(d,'1/1/90',fromdate) and (datediff(d,'1/1/90',todate)-1)  and roomtype= #dummy.roomtype ),0) rr1
from #dummy where datediff(d,'1/1/90',dt) between datediff(d,'1/1/90',@dt)
and datediff(d,'1/1/90',@dt2)   ) a
end
else
begin
select convert(varchar(20),dt,6) [Date] ,roomtype [Room Type],   tr [Total Rooms],er [Continuing Rooms],rr [Reserved Rooms]   ,tr-(er+rr) [Available Rooms]  into #dummy3 from (
select dt ,roomtype,tr,
isnull((select count(*) from #dumgus where datediff(d,'1/1/90',expecteddep) > datediff(d,'1/1/90',dt)  and  roomtype= #dummy.roomtype),0) er,
isnull((select sum(isnull(noofrooms,0)-isnull(cancelledrooms,0)-isnull(arrivedrooms,0)) from #sp1 where datediff(d,'1/1/90',dt) between
datediff(d,'1/1/90',arrivaldate) and (datediff(d,'1/1/90',departuredate)-1)  and roomtype= #dummy.roomtype ),0) rr 
from #dummy where datediff(d,'1/1/90',dt) between datediff(d,'1/1/90',@dt)
and datediff(d,'1/1/90',@dt2)   ) a
end
if @opt=0
	select @ar=min([Available Rooms]) from #dummy2 where [Room Type]=@roomtyperqd  group by [Room Type]
else
	select @ar=min([Available Rooms]) from #dummy3 where [Room Type]=@roomtyperqd group by [Room Type]
return
e_error:
raiserror('error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[pbpga7]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[pbpga7]
@dt datetime,@dt2 datetime
AS
BEGIN
	SET NOCOUNT ON;
select distinct(roomtype) roomtype ,
(select cnt=count(*)  from room where roomtype=a.roomtype
and RNO not in 
(
select rno from sptbrooms_v where 
noofrooms > (arrivedrooms+cancelledrooms) and status <> 'C' and
( (datediff(s,'1/1/90',ARRIVALDATE) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,'1/1/90',departuredate) between
datediff(s,'1/1/90',@dt) and datediff(s,'1/1/90',@dt2))
or
(datediff(s,arrivaldate,@dt) > 0 and datediff(s,@dt2,departuredate) > 0)) 
))
from room a

END





GO
/****** Object:  StoredProcedure [dbo].[pbpk0503]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:47 AM ******/
/* Change Mode */ 
CREATE PROCEDURE [dbo].[pbpk0503] @M_BNO INT,@N_MOP INT ,@N_GNO INT, @N_BRNAME VARCHAR(50),
@N_CRNO CHAR(19) ,@N_CRTYPE VARCHAR(20),@N_RNO INT,@COMPELSPLIT CHAR(1)='X',@USERNO INT=0 , @checkuser int = 0, @rundelcout int = 0  ,@checkpw int=121,@checkpw1 int=127
/* with encryption */
AS
DECLARE @TEMPTABLE VARCHAR(250)
DECLARE @M_NEWROFF MONEY
DECLARE @M_NEWDISC  MONEY
DECLARE @M_OUTLET INT
DECLARE @M_TXNDT DATETIME 
DECLARE @M_MOP INT
DECLARE  @M_DRATE MONEY
DECLARE @M_GNO_COMPCODE_CGPNO INT
DECLARE @M_BRNAME_CARDNAME VARCHAR(50)
DECLARE @M_CRNUM CHAR(19)
DECLARE @M_CRTYPE VARCHAR(20)
DECLARE @M_RNO INT
DECLARE @M_WNO INT
DECLARE @M_ROAMT MONEY
DECLARE @M_KOT INT
DECLARE @M_CODE  INT
DECLARE @M_QTY     INT
DECLARE @M_RATE  MONEY
DECLARE @M_AMT    MONEY
DECLARE @M_TAX     MONEY
DECLARE @M_DISC   MONEY
DECLARE @M_STRATE MONEY
DECLARE @SQL VARCHAR(500)
DECLARE @M_ACTNUM INT
DECLARE @M_ST_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @M_RO_ACTNUM INT
DECLARE @M_SC_ACTNUM INT
DECLARE @G_AUTOKOT INT
DECLARE @M_KOTDATE DATETIME 
DECLARE @M_BILLDATE INT
DECLARE @M_SNO INT
DECLARE @M_CHANNEL INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_YEAR INT
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_SALE MONEY
DECLARE @M_STAMT MONEY
DECLARE @M_DAMT MONEY
DECLARE @M_GRNO INT
DECLARE @M_DACTNO INT 
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_PARTIC VARCHAR(50)
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT
DECLARE @M_GNO INT
DECLARE @M_CO1GRNO INT
DECLARE @M_CO1FY INT
DECLARE @M_SCAMT MONEY
DECLARE @FF_VOU CHAR(1)
declare @transact int
declare @finalbillamt1 money
declare @finalbillamt2 money
declare @billgno int
declare @billoutlet int
declare @oldsplit char(1)
DECLARE @HTL_RECEIVABLE INT
DECLARE @SELF_RECEIVABLE INT
declare @oldpayment varchar(20)
declare @newpayment varchar(20)
declare @orno int
declare @lux char(1)
declare @ctrlno numeric
declare @osname varchar(20)
declare @nsname varchar(20)
declare @userorg varchar(20)
declare @servicetax money
declare @cess money
declare @couponamt money
declare @gstin nvarchar(15) 
declare @err varchar(1000)
set @err= 'Error : '
set @transact=0
set @finalbillamt1 = 0
set @finalbillamt2 = 0
set @billoutlet = 0
SET @OLDSPLIT=' '
set @billgno=0
if not exists(select * from hmsfolder where version_no = (select version_no from comuser where [host_id]=host_id()) ) 
  begin 
  print 'You are using a wrong version of the Softwre Installed...Please update the version (6) and try'
  raiserror('Error' ,16,-1)
  return
end

SELECT @M_DRATE=DRATE,@M_TXNDT=TXNDT,@M_OUTLET= OUTLET,@M_GNO_COMPCODE_CGPNO  =GNO,@lux=lux,
@servicetax=servicetax,@cess=cess,
@M_RNO=RNO,@M_MOP=PMODE,@M_SNO =SNO,@M_SPLIT=SPLIT,@M_SELFDEAL=SELFDEAL,@M_ROAMT=ROAMT,@M_SCAMT=SCAMT,@userorg=usernm
,@couponamt=isnull(ppcouponamt,0),@finalbillamt1=finalamt,@gstin=gstin 
FROM KITHIST1 WHERE BNO = @M_BNO
if @m_mop = 2
	begin
	declare @irnsuppress int
	select @irnsuppress=irnsuppress from billhist1 where 
	cno=(select cno from billhist1 where brno=(select brno from guest where gno=@M_GNO_COMPCODE_CGPNO ) and company=1 and split='N')
	if @irnsuppress=1 
		begin
		print 'this is e-invoice generated bill.modification not allowed'
		raiserror('Error',16,-1)
		return
		end
	end


set @orno=@m_rno
select @oldpayment=name from restpmode where pmode=@m_mop
set @billoutlet = @m_outlet
SET @OLDSPLIT=@m_SPLIT
if exists (select * from servicepermission where userno=(select userno from comuser where [host_id]=host_id()) and serviceno=@checkpw and permission='N' and outlet=@m_outlet) 
begin
	print 'You are not authorised to use this option'
	raiserror('Error' ,16,-1)
return
end

if @m_mop=2 
  begin
  set @transact = 1
  set @billgno = @m_gno_compcode_cgpno
   if exists (select * from room where gno=@billgno and hostid <> '' )  begin print 'The room is under check-out process' goto e_error end
  end


if @transact=1 and @rundelcout=0
	begin
	if exists(select * from guest where gno=@m_gno_compcode_cgpno and co_date is not null)
	begin
		if exists (select * from servicepermission where userno=(select userno from comuser where [host_id]=host_id()) and serviceno=@checkpw1 and permission='N' and outlet=@m_outlet) 
			begin
				print 'You are not authorised to use this option after checkout'
				raiserror('Error' ,16,-1)
			return
			end
		end
	end
BEGIN TRANSACTION
if @m_mop=2 set @osname = '-' + case when @m_split='N' then 'Main' when @m_split='Y' then 'Split' else 'Complementary' end 
else
set @osname=''
---------------------- Permission Check
if @checkuser=0 begin
--print @checkuser
	if @userno =0 
		begin
		print 'Unauthorised access'
		goto e_error
		end
	
	
/*	if not exists(select * from servicepermission where userno=@userno and serviceno=121 and outlet=@m_outlet and outlettype=1 and permission='Y')
		begin
		print 'You are not permitted to change mode of payment for  Bill of this Outlet' 
		goto e_error
		end
*/
end

--------------end of permission check
SELECT @M_SALE=SUM(QTY*RATE),@M_STAMT=SUM(TAX),@M_DAMT=SUM(DISCOUNT) FROM KITHIST WHERE BNO=@M_BNO
SELECT @M_CHANNEL=CHANNEL FROM OUTLET WHERE CODE=@M_OUTLET
SELECT @M_COMPANY=COMPANY FROM RESTCO WHERE CHANNEL=@M_CHANNEL
--print 'periya'
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
SELECT @M_OLNAME=[OLNAME],@M_ACTNUM=ACTNUM,@M_ST_ACTNUM=ST_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_RO_ACTNUM = RO_ACTNUM,@M_SC_ACTNUM=SC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK ,@HTL_RECEIVABLE=HTL_RECEIVABLE,
@SELF_RECEIVABLE=SELF_RECEIVABLE ,@FF_VOU=FREEFOODVOUCHING
FROM CO_REST_OL  WHERE OCODE = @M_OUTLET
IF @@ERROR <> 0  GOTO E_ERROR
--,@FF_vou=freefoodvouching
SELECT @M_DACTNO = 
                              CASE 
                                          WHEN @M_MOP = 1 THEN 1           --- CASH SALES
                                          WHEN @M_MOP = 2 THEN               -- ROOM SERVICE
                                                                                                   CASE WHEN @M_COMPANY = 1 THEN @SELF_RECEIVABLE
                                                                                                    ELSE  
                                                                                                                  CASE
                                                                                                                  WHEN @M_SELFDEAL='Y' THEN @SELF_RECEIVABLE   ELSE @M_HNO_MYBOOK
                                                                                                                  END
                                                                                                     END
                                          WHEN @M_MOP = 3 THEN 23  -- BILL RECEIVABLE
                                          WHEN @M_MOP = 4 THEN @M_GNO_COMPCODE_CGPNO   -- GL A/C
                                          WHEN @M_MOP = 5 THEN 22   -- CREDIT CARD 
                                          WHEN @M_MOP = 6 THEN 0   -- COMPLEMENTARTY No vouching
                               END
--set @finalbillamt1 = (@M_SALE + @M_STAMT+@M_SCAMT-@M_DAMT+@M_ROAMT + @servicetax+@cess - @couponamt)
if @m_mop=2 
  begin
  set @transact = 1
  set @billgno = @m_gno_compcode_cgpno
   if exists (select * from room where gno=@billgno and hostid <> '' )  begin print 'The room is under check-out process' goto e_error end
  end

exec serviceadeleting @m_bno
if @@error<>0 goto e_error
subfinal:

IF @COMPELSPLIT<>'X'  SET @M_SPLIT=@COMPELSPLIT
	SELECT @M_MOP = @N_MOP
	SELECT @M_GNO_COMPCODE_CGPNO = @N_GNO
	SELECT @M_BRNAME_CARDNAME = @N_BRNAME
	SELECT @M_CRNUM = @N_CRNO
	SELECT @M_CRTYPE = @N_CRTYPE
	SELECT @M_RNO = @N_RNO
	IF @COMPELSPLIT='X'
	BEGIN
	if @m_mop = 2 
		begin
		set @m_split='N'
		if @M_BRNAME_CARDNAME='YES'  
			begin 
				set @m_split='C' 
			end
		else
			begin 
			if @m_company = 1
				begin
				if exists(select * from gsplit where gno=@M_GNO_COMPCODE_CGPNO and outlet = @m_outlet and chtype =1)  set @m_split = 'Y'
				end 
			end
		end
	END

if @m_mop = 2

begin 
  if exists (select * from room where gno=@n_gno and hostid <> '' ) begin print 'The room is under checkout process'   goto E_ERROR end
 -- if exists (select * from guest_view1 where gno=@n_gno and datediff(d,'1/1/80',ci_date)  >  datediff(d,'1/1/80',@m_txndt) ) begin print 'The checkin date is latest than this bill date'  goto E_ERROR end
  select @n_rno=rno from guest where gno = @n_gno
end
if not exists (select * from co_rest_ol where ocode=@m_outlet) 
begin
print 'You are not permitted to delete/modify the bills of this outlet from this system-' + host_name()
goto e_error
end

SELECT @M_OLNAME=[OLNAME],@M_ACTNUM=ACTNUM,@M_ST_ACTNUM=ST_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_RO_ACTNUM = RO_ACTNUM,@M_SC_ACTNUM=SC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK ,@HTL_RECEIVABLE=HTL_RECEIVABLE,
@SELF_RECEIVABLE=SELF_RECEIVABLE
FROM CO_REST_OL  WHERE OCODE = @M_OUTLET
IF @M_SPLIT='C' AND @FF_VOU='N'  SET @M_SPLIT='F'

if @m_mop=2  -- this change made on 20-08-2009 8:00 pm
	begin
	set @nsname = '-' + case when @m_split='N' then 'Main' when @m_split='Y' then 'Split' else 'Complementary' end
    select @gstin=case when @m_split<>'Y' then gstin else gstinsplit end from guest where gno=@n_gno 
	end
else
set @nsname=''
----t 'periya'  goto e_error
declare @vatonsc tinyint
select @vatonsc=g_scvat from setting 
UPDATE KITHIST1 SET
PMODE=@M_MOP ,
GNO = @M_GNO_COMPCODE_CGPNO,
RNO = @M_RNO,
--CRNAME=@M_BRNAME_CARDNAME,
CRNUM=@M_CRNUM,
CRTYPE=@M_CRTYPE,
SPLIT=@M_SPLIT,
vatonsc=@vatonsc,
gstin=@gstin,
crname=@n_brname
WHERE BNO=@M_BNO



set @finalbillamt2 = @finalbillamt1 --( @M_SALE + @M_STAMT+@M_SCAMT-@M_DAMT+@M_ROAMT+ @servicetax+@cess-@couponamt)
exec serviceavouching @m_bno
if @@error<>0 goto e_error
final:
if @transact=1 and @rundelcout=0
begin
	IF @billgno=@M_GNO_COMPCODE_CGPNO
		begin
		set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalbillamt1) + ',0' +
		+ ',' + str(@m_company) + ',' + str(100+@billoutlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @oldsplit + ''',0,' + convert(varchar(20),@finalbillamt2) + ',''' + @m_split + ''','+
		str(@userno)+','''+@lux+''',' + str(@checkuser)
		exec (@sql)
		if @@error <> 0  goto e_error
		end
	else
		begin
		set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalbillamt1) + ',0,' + str(@m_company) + ',' + str(100+@billoutlet)
		+ ',''' + convert(varchar(20),@m_txndt) + ''',''' + @oldsplit + ''',0,' + convert(varchar(20),@finalbillamt2) + ',''X'','+str(@userno)
		+','''+@lux+''','+str(@checkuser)
		exec (@sql)
		if @@error <> 0  goto e_error
		end
end

COMMIT TRANSACTION
if @checkuser=0 begin
select @newpayment=name from restpmode where pmode=@m_mop
declare @usernm varchar(50)
--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@m_olname))+ ' Bill No ' + ltrim(str(@m_sno)) + ' Dated ' + convert(varchar(20),@m_txndt,3) + ' Amt ' + ltrim(convert(varchar(20), @finalbillamt1)) +  ' MOP from '+ltrim(rtrim(@oldpayment)) +  @osname + case when @orno<> 0 then ' Room No : ' + ltrim(str(@orno)) else ' ' end + ' to ' + ltrim(rtrim(@newpayment)) + @nsname + case when @m_rno<> 0 then ' Room No : ' + ltrim(str(@m_rno)) else ' ' end +  ' changed' ,2,@billgno)
end

GOTO FINAL1
E_ERROR:
ROLLBACK TRANSACTION
set @err = @err + 'MODE CHANGING  OF CHANNEL SERVICE VOUCHER failed in CHMDKIT '
raiserror (@err,16,-1)
GOTO FINAL1
FINAL1:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
RETURN

GO
/****** Object:  StoredProcedure [dbo].[pbpk1106]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo    Script Date: 1/20/03 11:51:47 AM ******/
/* Editing */
CREATE PROCEDURE [dbo].[pbpk1106] @TEMPTABLE VARCHAR(250),@M_BNO INT,@M_NEWROFF MONEY ,@M_NEWDISC  MONEY ,@M_NEWSCAMT MONEY,@M_NEWSCRATE MONEY,@userno int=0,@accesslog int=0 ,
@servicetax money=0,@cess money=0 ,@ratetype int = 5,@ndiscount money=0,@packingchgs money = 0,@ndiscgst money=0,
@partpayment money=0,@crname1 varchar(50) = '',@crnum1 varchar(20) = ''  ,@crbank1 varchar(20) = '' ,@gno2 numeric=0 ,@mop1 int=0,
@gstin nvarchar(50)
/* with encryption */
AS
DECLARE @TESTDATE DATETIME
DECLARE @M_OUTLET INT
DECLARE @M_TXNDT DATETIME 
DECLARE @M_MOP INT
DECLARE  @M_DRATE MONEY
DECLARE @M_GNO_COMPCODE_CGPNO INT
DECLARE @M_BRNAME_CARDNAME VARCHAR(50)
DECLARE @M_CRNUM CHAR(19)
DECLARE @M_CRTYPE VARCHAR(20)
DECLARE @M_RNO INT
DECLARE @M_WNO INT
DECLARE @M_ROAMT MONEY
DECLARE @M_SCAMT MONEY
DECLARE @M_KOT INT
DECLARE @M_CODE  INT
DECLARE @M_QTY     INT
DECLARE @M_RATE  MONEY
DECLARE @M_AMT    MONEY
DECLARE @M_TAX     MONEY
DECLARE @M_DISC   MONEY
DECLARE @M_STRATE MONEY
DECLARE @M_SCRATE MONEY
DECLARE @SQL VARCHAR(5000)
DECLARE @M_ACTNUM INT
DECLARE @M_ST_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @M_RO_ACTNUM INT
DECLARE @M_SC_ACTNUM INT
DECLARE @G_AUTOKOT INT
DECLARE @M_KOTDATE DATETIME 
DECLARE @M_BILLDATE INT
DECLARE @M_SNO INT
DECLARE @M_CHANNEL INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_YEAR INT
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_SALE MONEY
DECLARE @M_STAMT MONEY
DECLARE @M_DAMT MONEY
DECLARE @M_GRNO INT
DECLARE @M_DACTNO INT 
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_PARTIC VARCHAR(50)
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT
DECLARE @M_GNO INT
DECLARE @M_CO1GRNO INT
DECLARE @M_CO1FY INT
DECLARE @RET_VAL INT
declare @transact int
declare @finalbillamt1 money
declare @finalbillamt2 money
declare @billgno int
declare @billoutlet int
DECLARE @HTL_RECEIVABLE INT
DECLARE @SELF_RECEIVABLE INT
declare @oldamt money
declare @newamt money
declare @usernm varchar(30)
declare @lux char(1)
declare @ctrlno numeric
declare @servicetaxold money
declare @cessold money
declare @couponamt money

declare @st1 money
declare @cess1 money
declare @sc1 money

declare @cess2 money
declare @cess21 money

declare @mst1 money
declare @mcess1 money
declare @msc1 money
declare @oldgstin nvarchar(50) 
if not exists(select * from hmsfolder where version_no = (select version_no from comuser where [host_id]=host_id()) ) 
  begin 
  print 'You are using a wrong version of the Softwre Installed...Please update the version (7) and try'
  goto e_error
end
set @transact=0
set @finalbillamt1 = 0
set @finalbillamt2 = 0
set @billoutlet = 0
set @billgno=0
SELECT @M_DRATE=DRATE,@M_TXNDT=TXNDT,@M_OUTLET= OUTLET,@M_GNO_COMPCODE_CGPNO  =GNO,
@M_RNO=RNO,@M_MOP=PMODE,@M_SNO =SNO,@M_SPLIT=SPLIT,@M_SELFDEAL=SELFDEAL,@M_ROAMT=ROAMT,@M_SCAMT=SCAMT,
@M_BRNAME_CARDNAME=CRNAME,@M_CRNUM=CRNUM,@M_CRTYPE=CRTYPE,@lux=lux,@servicetaxold=servicetax,@cessold=cess
,@couponamt=isnull(ppcouponamt,0),@oldamt=finalamt,@oldgstin=gstin
FROM KITHIST1 WHERE BNO = @M_BNO
if @ratetype=5 select @ratetype=ratetype from kithist1 where bno=@m_bno
if not exists (select * from co_rest_ol where ocode=@m_outlet) 
begin
print 'You are not permitted to delete/modify the bills of this outlet from this system-' + host_name()
goto e_error
end
set @billoutlet = @m_outlet
if @userno =0 
begin
print 'Unauthorised access'
goto e_error
end
if not exists(select * from servicepermission where userno=@userno and serviceno=120 and outlet=@m_outlet and outlettype=1 and permission='Y')
begin
print 'You are not permitted to edit Bill of this Outlet'
goto e_error
end

SELECT @M_SALE=SUM(QTY*RATE),@M_STAMT=SUM(TAX),@M_DAMT=SUM(DISCOUNT) FROM KITHIST WHERE BNO=@M_BNO
declare @stset tinyint
SELECT @M_CHANNEL=CHANNEL,@stset=stset FROM OUTLET WHERE CODE=@M_OUTLET
SELECT @M_COMPANY=COMPANY FROM RESTCO WHERE CHANNEL=@M_CHANNEL

EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
SELECT @M_OLNAME=[OLNAME],@M_ACTNUM=ACTNUM,@M_ST_ACTNUM=ST_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_RO_ACTNUM = RO_ACTNUM,@M_SC_ACTNUM=SC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK ,@HTL_RECEIVABLE=HTL_RECEIVABLE,
@SELF_RECEIVABLE=SELF_RECEIVABLE
FROM CO_REST_OL  WHERE OCODE = @M_OUTLET

--set @oldamt = (@M_SALE + @M_STAMT+@M_SCAMT-@M_DAMT+@M_ROAMT + @servicetaxold+@cessold - @couponamt)

if @m_mop=2 
	begin
		set @transact = 1
		set @finalbillamt1 = @oldamt --  (@M_SALE + @M_STAMT+@M_SCAMT-@M_DAMT+@M_ROAMT+@servicetaxold+@cessold-@couponamt)
		set @billgno = @m_gno_compcode_cgpno
		if exists (select * from room where gno=@billgno and hostid <> '' )
		begin
			print 'Room is under checkout process...No modifications allowed'
			goto e_error
		end
		if exists (select * from guest where gno=@billgno and datediff(d,'1/1/80',case when datepart(hh,ci_date) < 7 then ci_date-1 else ci_date end) > datediff(d,'1/1/80', @m_txndt)) begin print 'Error in the CI Date - The Check-In is greater than the Bill Date - Not Allowed' + convert(varchar(20),@m_txndt)  goto e_error end
	end

begin transaction

declare @vatonsc tinyint
select @vatonsc=g_scvat from setting 
update kithist1 set vatonsc=@vatonsc,stset=@stset where bno=@m_bno

exec serviceadeleting @m_bno
if @@error <> 0 goto e_error

declare tckh cursor for select ctrlno from kithist where bno=@m_bno
open tckh
fetch next from tckh into @ctrlno
while @@fetch_status=0 begin
delete from kithist where ctrlno=@ctrlno
fetch next from tckh into @ctrlno
end
close tckh
deallocate tckh
SUBFINAL:
------ UPDATING STARTS
SELECT @M_SALE   = 0
SELECT @M_STAMT  = 0
SELECT @M_DAMT     = 0
SELECT @M_ROAMT = @M_NEWROFF
SELECT @M_SCAMT = @M_NEWSCAMT
SELECT @M_SCRATE = @M_NEWSCRATE

set @st1 = 0
set @cess1 = 0
set @sc1 = 0
set @cess2 = 0


UPDATE KITHIST1 SET ROAMT=@M_ROAMT,DRATE=@M_NEWDISC,SCAMT=@M_SCAMT,SCRATE=@M_SCRATE,servicetax=@servicetax,cess=@cess,ndiscount=@ndiscount,packingchgs=@packingchgs ,gstin=@gstin,ndiscgst=@ndiscgst 
WHERE BNO=@M_BNO


SET @SQL = 'DECLARE UPDTKIT CURSOR FOR SELECT CODE,QTY,RATE, STRATE , AMOUNT,TAX  ,DISCOUNT,KOT,st,cess,sc,cess2  FROM '+  @TEMPTABLE 
EXEC (@SQL)
OPEN UPDTKIT
FETCH NEXT FROM UPDTKIT INTO @M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_AMT,@M_TAX, @M_DISC,@M_KOT,@mst1,@mcess1,@msc1,@cess21
WHILE  @@FETCH_STATUS=0
	BEGIN
		SELECT @M_SALE = @M_SALE + @M_AMT
--	if @m_amt <> (@m_rate*@m_qty) begin print convert(varchar(20),@m_amt) + ' ' + convert(varchar(20),(@m_rate*@m_qty)) + ' ' + str(@m_code) + 'bill' + str(@m_bno) + ' qty ' + str(@m_qty) + ' rate ' + convert(varchar(20),@m_rate) goto e_error end
		SELECT @M_STAMT = @M_STAMT + @M_TAX
		SELECT  @M_DAMT = @M_DAMT +  @M_DISC
		set @st1 = @st1+@mst1
		set @cess1 = @cess1 + @mcess1
		set @sc1 = @sc1 + @msc1
		set @cess2 = @cess2 + @cess21
		INSERT KITHIST (BNO,KOT,CODE,QTY,RATE,STRATE,TAX,DISCOUNT,st,cess,sc,cess2) VALUES
		(@M_BNO,@M_KOT,@M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_TAX,@M_DISC,@mst1,@mcess1,@msc1,@cess21)
		IF @@ERROR<>0 GOTO E_ERROR 
		FETCH NEXT FROM UPDTKIT INTO @M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_AMT,@M_TAX, @M_DISC,@M_KOT,@mst1,@mcess1,@msc1,@cess21
	END
DEALLOCATE UPDTKIT

if @m_sale+@m_stamt+@m_scamt-@m_damt+@m_roamt +@st1+@cess1+@sc1+@cess2+@packingchgs = 0 begin print ' The Edited bill can not be zero value...you may opt for deltion of this bill' goto e_error end
update kithist1 set scrate=@m_scrate,servicetax=0,cess=0,scamt=0,ratetype=@ratetype, 
finalamt=@M_SALE+@M_STAMT+@M_SCAMT-@M_DAMT+@servicetax+cess+@M_ROAMT-@couponamt+@st1+@cess1+@sc1+@cess2+@packingchgs-@ndiscount,
ndiscount=@ndiscount, 
partpayment=@partpayment,
pmode1=@mop1,
gno1=@gno2,
crname1=@crname1,
crnum1=@crnum1,
crbank1=@crbank1,
ndiscgst=@ndiscgst
where bno=@m_bno


set @newamt = (@M_SALE + @M_STAMT+@M_SCAMT-@M_DAMT+@servicetax+@cess+@M_ROAMT-@couponamt-@ndiscount + @st1+@sc1+@cess1+ @cess2 + @packingchgs - @ndiscgst)
--print 
--convert(varchar(20),@m_sale) + ' ' + 
--convert(varchar(20),@m_stamt) +  ' ' + 
--convert(varchar(20),@m_scamt) +  ' ' + 
--convert(varchar(20),@m_damt) +  ' ' + 
--convert(varchar(20),@servicetax) +  ' ' + 
--convert(varchar(20),@cess) +  ' ' + 
--convert(varchar(20),@m_roamt) +  ' ' + 
--convert(varchar(20),@couponamt) +  ' ' + 
--convert(varchar(20),@st1) +  ' ' + 
--convert(varchar(20),@cess) +  ' ' + 
--convert(varchar(20),@sc1)  


if @m_mop=2
set @finalbillamt2 = @newamt

exec serviceavouching @m_bno
if @@error <> 0 goto e_error
if @transact=1
   begin
   set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalbillamt1) + ',' + convert(varchar(20),@finalbillamt2) + ',' + str(@m_company) + ',' + str(100+@billoutlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,0,''X'','+str(@userno)+','''+@lux+''''
--print @sql goto e_error
   exec (@sql)
   if @@error <> 0  goto e_error
   end
goto final
FINAL:

--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

if @accesslog=0
begin
---print str(@m_sno) + ' '+ convert(varchar(20),@m_txndt,6) + ' ' + convert(varchar(20),@oldamt)
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, 
ltrim(rtrim(@M_olname))+ ' Bill No ' + ltrim(str(@m_sno)) + ' Dated ' + convert(varchar(20),@m_txndt,6) + ' Edited - Old amt = '+ convert(varchar(20), @oldamt) + ' ,  New Amount = '+ convert(varchar(20),@newamt) + ' OLDGSTIN ' + @oldgstin + ' New GSTIN ' + @gstin,2,@billgno)
end
COMMIT TRANSACTION
GOTO FINAL1
E_ERROR:
ROLLBACK TRANSACTION
raiserror ('EDITING OF CHANNEL SERVICE VOUCHER failed in EDITKIT ',16,-1)
GOTO FINAL1
FINAL1:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
RETURN

GO
/****** Object:  StoredProcedure [dbo].[pbpk1508]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:47 AM ******/
/* Deleting */
CREATE PROCEDURE [dbo].[pbpk1508] @M_BNO INT,@usernm varchar(50) ='SYS',@userno numeric=0,@executedelcout int=0 ,@sessionid varchar(100)='' 
/* with encryption */
AS
DECLARE @TEMPTABLE VARCHAR(255)
DECLARE @M_OUTLET INT
DECLARE @M_TXNDT DATETIME 
DECLARE @M_MOP INT
DECLARE  @M_DRATE MONEY
DECLARE @M_GNO_COMPCODE_CGPNO INT
DECLARE @M_BRNAME_CARDNAME VARCHAR(50)
DECLARE @M_CRNUM CHAR(19)
DECLARE @M_CRTYPE VARCHAR(20)
DECLARE @M_RNO INT
DECLARE @M_WNO INT
DECLARE @M_ROAMT MONEY
DECLARE @M_SCAMT MONEY
DECLARE @M_KOT INT
DECLARE @M_CODE  INT
DECLARE @M_QTY     INT
DECLARE @M_RATE  MONEY
DECLARE @M_AMT    MONEY
DECLARE @M_TAX     MONEY
DECLARE @M_DISC   MONEY
DECLARE @M_STRATE MONEY
DECLARE @SQL VARCHAR(5000)
DECLARE @M_ACTNUM INT
DECLARE @M_ST_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @M_RO_ACTNUM INT
DECLARE @M_SC_ACTNUM INT
DECLARE @G_AUTOKOT INT
DECLARE @M_KOTDATE DATETIME 
DECLARE @M_BILLDATE INT
DECLARE @M_SNO INT
DECLARE @M_CHANNEL INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_YEAR INT
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_SALE MONEY
DECLARE @M_STAMT MONEY
DECLARE @M_DAMT MONEY
DECLARE @M_GRNO INT
DECLARE @M_DACTNO INT 
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_PARTIC VARCHAR(50)
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT
DECLARE @M_GNO INT
DECLARE @M_CO1GRNO INT
DECLARE @M_CO1FY INT
declare @transact int
declare @finalbillamt1 money
declare @finalbillamt2 money
declare @billgno int
declare @billoutlet int
DECLARE @HTL_RECEIVABLE INT
DECLARE @SELF_RECEIVABLE INT
declare @payment varchar(50) 
declare @lux char(1)
declare @ctrlno numeric
declare @servicetax money
declare @cess money
declare @couponamt money
BEGIN TRANSACTION
if not exists(select * from hmsfolder where version_no = (select version_no from comuser where [host_id]=host_id() and isnull(sessionid,'')=@sessionid) ) 
  begin 
  print 'You are using a wrong version of the Softwre Installed...Please update the version (8) and try'
  goto e_error
end
set @transact=0
set @finalbillamt1 = 0
set @finalbillamt2 = 0
set @billoutlet = 0
set @billgno=0
SELECT @M_DRATE=DRATE,@M_TXNDT=TXNDT,@M_OUTLET= OUTLET,@M_GNO_COMPCODE_CGPNO  =GNO,
@M_RNO=RNO,@M_MOP=PMODE,@M_SNO =SNO,@M_SPLIT=SPLIT,@M_SELFDEAL=SELFDEAL,@M_ROAMT=ROAMT,@M_SCAMT=SCAMT,@lux=lux,
@servicetax=servicetax,@cess=cess,@couponamt=ppcouponamt,@finalbillamt1=finalamt
FROM KITHIST1 WHERE BNO = @M_BNO

if not exists (select * from co_rest_ol where ocode=@m_outlet) 
begin
print str(@m_bno) + ' You are not permitted to delete/modify the bills of this outlet ' + str(isnull(@m_outlet,0)) + ' from this system-' + host_name()
goto e_error
end

set @billoutlet = @m_outlet
SELECT @M_SALE=SUM(QTY*RATE),@M_STAMT=SUM(TAX),@M_DAMT=SUM(DISCOUNT) FROM KITHIST WHERE BNO=@M_BNO
SELECT @M_CHANNEL=CHANNEL FROM OUTLET WHERE CODE=@M_OUTLET
SELECT @M_COMPANY=COMPANY FROM RESTCO WHERE CHANNEL=@M_CHANNEL
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
SELECT @M_OLNAME=[OLNAME],@M_ACTNUM=ACTNUM,@M_ST_ACTNUM=ST_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_RO_ACTNUM = RO_ACTNUM,@M_SC_ACTNUM=SC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK ,@HTL_RECEIVABLE=HTL_RECEIVABLE,
@SELF_RECEIVABLE=SELF_RECEIVABLE
FROM CO_REST_OL1  WHERE OCODE = @M_OUTLET
if @userno=0 begin print 'Unauthorised Acces..Deletion denied' goto e_error end


if not exists(select * from servicepermission where userno=@userno and serviceno=119 and outlet=@m_outlet and outlettype=1 and permission='Y')
begin
print 'You are not permitted to delete Bill of this Outlet'
goto e_error
end
if @m_mop=2 
	begin
		set @transact = 1
		--set @finalbillamt1 = (@M_SALE + @M_STAMT+@M_SCAMT-@M_DAMT+@M_ROAMT+@servicetax+@cess-@couponamt)
		set @billgno = @m_gno_compcode_cgpno
		if exists (select * from room where gno=@billgno and hostid <> '' ) begin print 'The room is under checkout process'   goto E_ERROR end
	end
exec serviceadeleting @m_bno
if @@error<>0 goto e_error
declare tckh cursor for select ctrlno from kithist where bno=@m_bno
open tckh
fetch next from tckh into @ctrlno
while @@fetch_status=0 begin
delete from kithist where ctrlno=@ctrlno
if @@error<>0 goto e_error
fetch next from tckh into @ctrlno
end
close tckh
deallocate tckh
DELETE FROM KITHIST1 WHERE BNO=@M_BNO
     IF @@ERROR <> 0 GOTO E_ERROR
delete from prepaidhist where bno=@m_bno
FINAL:

if @transact=1 
	if @executedelcout=0
		begin
		set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalbillamt1) + ',' + convert(varchar(20),@finalbillamt2) + ',' + str(@m_company) + ',' + str(100+@billoutlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,0,''X'',' + str(@userno) + ',''' + @lux + ''''
		exec (@sql)
		if @@error <> 0  goto e_error
		end

if @executedelcout=0
	begin
	set  @payment = case when @m_mop= 1 then 'cash' when @m_mop=2 then 'Room Service ' + ltrim(str(@m_rno)) when @m_mop=3 then 'Bill Receivable' when @m_mop=4 then 'GL Account' when @m_mop=5 then 'Credit Card' when @m_mop=6 then 'Complementary' else 'Unkown' end
if @m_split='F' or @m_split ='C' set @payment=@payment + ' Complementary'
	--select @usernm=usernm from comuser where [host_id]=host_id() and isnull(sessionid,'')=@sessionid
	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
	insert into acclog (txndt,usernm,narration,txntype,gno,sessionid) values (getdate(),@usernm, ltrim(rtrim(@M_olname))+ ' Bill No ' + ltrim(str(@m_sno)) + ' Dated ' + convert(varchar(20),@m_txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@finalbillamt1)) +  ' Mode of payment '+ltrim(rtrim(@payment)) + ' Deleted ',3,@billgno,@sessionid)
	if @@error<>0 goto e_error
	end
COMMIT TRANSACTION
RETURN 0
E_ERROR:
ROLLBACK TRANSACTION
raiserror ('DELETION OF CHANNEL SERVICE VOUCHER failed in DELKIT ',16,-1)
RETURN 1

GO
/****** Object:  StoredProcedure [dbo].[pbpk1Bill]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Ramanathan
-- Create date: 14 November 2014
-- Description:	Billing for Rooms FoodBill
-- =============================================
CREATE PROCEDURE [dbo].[pbpk1Bill]
	-- Add the parameters for the stored procedure here
    @reqamt money,@txndt datetime,@gno numeric,@famt money out
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	declare @bno numeric
declare @scrate money
declare @drate money
declare @outlet numeric
declare @usernm varchar(50)
declare @year int
declare @bofy datetime
declare @qty int
declare @rate money
declare @total money
declare @tax money
declare @disc money
declare @roamt money
declare @scset int
declare @stset int
--declare @split char(1)
declare @sno int
declare @amt money

declare @saleamt money
declare @sctotal money
declare @disctotal money

declare @strate money
declare @scamt money
declare @total1 money
declare @wno int
declare @ratetype int
declare @selfdeal char(1)
declare @g_staxincl char(1)
declare @sql varchar(8000)
declare @sc_table tinyint
declare @sc_room tinyint
declare @sc_door tinyint
declare @commissionrate numeric -- given to agents
declare @guestdiscount numeric -- given to agents
declare @duedt datetime --- coupon duedt
declare @agentcode numeric
declare @couponcode numeric
declare @scadd money
declare @g_scvat tinyint 
declare @servicetax money
declare @cess money
declare @servicetaxeligible char(1)
declare @stonsc int
declare @couponamt decimal(18,2)
declare @servicetax1 money
declare @cess1 money
declare @stadd money
declare @vatonst tinyint
declare @code numeric
declare @rno numeric
declare @channel int
declare @bp int
declare @ctrlno numeric
declare @stpercent numeric(5,2)
declare @stable money
declare @brno numeric
declare @cess2 money
begin transaction

select @rno=rno,@brno=brno from guest where gno=@gno
select @scset=scset,@stset=stset,@selfdeal=selfdeal,@scrate=scrate,@outlet=code,
@sc_table=sc_table,@sc_room=sc_room,@sc_door=sc_door,@servicetaxeligible=servicetax,@vatonst=vatonst,
@g_staxincl=g_staxincl,@channel=channel from outlet where onepercent=1

exec getservicetaxr  100 ,@txndt ,@servicetax1 out ,@cess1 out,@outlet,1000,@cess2 out 
--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

select top 1 @code=code,@strate=tax  from kitmast_v where outlet = @outlet and norate='Y' 
SET @STRATE = ISNULL(@STRATE,0)
set @stpercent = @servicetax1 + @cess1 + @strate  -- this the @strate
if @strate is null begin raiserror('There is no flexi rate item in the selected outet' ,16,-1) return end
 
select @g_scvat=g_scvat from (select top 1 * from setting) a
set @servicetax1= 0
set @cess1 = 0 
set @total = 0
set @scrate = 0
set @saleamt = 0
set @sctotal = 0
set @disctotal = 0
set @servicetax = 0
set @cess = 0
exec  GetServiceASno @outlet,@txndt,@sno out
if @@error <> 0 goto e_error    
	set @drate=0 
    set @qty = 1


/**/
	set @rate = 100

		select @amt = @qty*@rate
		select @disc= 0
		select @saleamt = @saleamt + @amt
		select @disctotal = @disctotal + @disc

		set @scamt=0
		if @g_scvat=1 
			set @scadd = @scamt
		else
			set @scadd = 0

		if @servicetaxeligible = 'Y'
			begin
				select @stonsc=g_stonsc from setting
				if @stonsc=1 
						select @stable = @amt + @scamt - @disc
				else
						select @stable = @amt  - @disc 
				exec getservicetaxr @stable ,@txndt ,@servicetax1 out ,@cess1 out,@outlet 
				if @@error <> 0 goto e_error    
				set @servicetax = @servicetax + @servicetax1
				set @cess = @cess + @cess1
			end

		if @vatonst= 1 
			set @stadd = @servicetax1+@cess1
		else
			set @stadd = 0

		if @stset=0
			select @tax=round((@amt + @scadd + @stadd)*@strate/100,2)
		else
			select @tax=round(((@amt + @scadd + @stadd) - @disc)*@strate/100,2)
				
		/* Service Charges Calculation according to the Mode of the bill modified on 27/07/2009***/
	

		select @total =( @total + @amt + @tax + @scamt) - @disc

 		set @total = @total + @servicetax1 + @cess1

		set @rate = (100/@total)* @reqamt

		--print 'Total ' + convert(varchar(20),@total) + ' Rate ' + convert(varchar(20),@rate)  + ' ReqAmt ' + convert(varchar(20),@reqamt) 
		 
/**/
		set @rate = round(floor(@rate/10)*10,0)
		--print convert(varchar(20),@stpercent) + ' Total ' + convert(varchar(20),@total) + ' Rate ' + convert(varchar(20),@rate)  + ' ReqAmt ' + convert(varchar(20),@reqamt)

		select @amt = @qty*@rate
		select @disc= 0
		select @saleamt = @saleamt + @amt
		select @disctotal = @disctotal + @disc

		set @scamt=0
		if @g_scvat=1 
			set @scadd = @scamt
		else
			set @scadd = 0

		if @servicetaxeligible = 'Y'
			begin
				select @stonsc=g_stonsc from setting
				if @stonsc=1 
						select @stable = @amt + @scamt - @disc
				else
						select @stable = @amt  - @disc 
				exec getservicetaxr @stable ,@txndt ,@servicetax1 out ,@cess1 out,@outlet ,@cess2 out
				if @@error <> 0 goto e_error    
				set @servicetax = @servicetax1
				set @cess = @cess1
			end

		if @vatonst= 1 
			set @stadd = @servicetax1+@cess1
		else
			set @stadd = 0

		if @stset=0
			select @tax=round((@amt + @scadd + @stadd)*@strate/100,2)
		else
			select @tax=round(((@amt + @scadd + @stadd) - @disc)*@strate/100,2)
				
		/* Service Charges Calculation according to the Mode of the bill modified on 27/07/2009***/
	

		select @total =(@amt + @tax + @scamt) - @disc


 		set @total = @total + @servicetax1 + @cess1

		--print 'Total ' + convert(varchar(20),@total) + ' Rate ' + convert(varchar(20),@rate)  + ' ReqAmt ' + convert(varchar(20),@reqamt)  		goto e_Error

		if exists(select * from outlet where code=@outlet and roundoff='Y')
			begin
			select @roamt = @total  - floor(@total)
			if @roamt <  .5 set @roamt = @roamt*-1 else set @roamt= round(@total,0) - @total
			end
		else
			select @roamt=0

		set @roamt=isnull(@roamt,0)
		set @ratetype = 1
		select top 1 @wno=code from waiter
		set @couponamt = 0 
		declare @pax int
		declare @type int
		declare @smsddr char(1)
		set @pax = 1
		set @type = 0
		set @smsddr = ' '
		set @couponamt = 0
		INSERT KITHIST1 (DRATE,TXNDT,OUTLET,SNO,ROAMT,RATETYPE,SCAMT,SCRATE,STSET,SCSET,wno,txntime,selfdeal,USERNM,finalamt,tno,ptype,pax,servicetax,cess,smsddr,ppcouponamt,ndiscount,vatonst,GNO,RNO,PMODE,split)  VALUES
		(@DRATE,@TXNDT,@OUTLET,@SNO,@ROAMT,@RATETYPE,0,@SCRATE,@STSET,@SCSET,
		@wno,convert(varchar(5),getdate(),8),@selfdeal,@USERNM,@total+@roamt-@couponamt,@code,@type,@pax, 0,0,@smsddr,@couponamt,0,@vatonst,@gno,@rno,2,'N')
		if @@error <> 0 goto e_error    
		set @bno=@@IDENTITY 
	
	    INSERT KITHIST (BNO,KOT,CODE,QTY,RATE,STRATE,TAX,DISCOUNT,preference,st,cess,sc) VALUES
        (@BNO,1,@CODE,1,@RATE,@STRATE,@TAX,@DISC,'',@servicetax1,@cess1,0)
		if @@error <> 0 goto e_error    

		exec serviceavouching @bno
		if @@error <> 0 goto e_error    

		select @famt=finalamt from kithist1 where bno=@bno
		select @bp=billposition1  from restco where channel=@channel
		--set @sql = 'select @ctrlno=ctrlno from where gno=@gno and datediff(d,txndt,@txndt)=0 and company=1 and split in (''N'','''')'
		--exec sp_executesql N'select @ctrlno=ctrlno from billhist where gno=@gno and datediff(d,txndt,@txndt)=0 and company=1 and split in (''N'','''')' , N'@txndt datetime,@ctrlno numeric output,@gno numeric' , @gno = @gno,@txndt=@txndt,@ctrlno=@ctrlno

		select @ctrlno=ctrlno from billhist where cno=(select cno from billhist1 where brno=@brno and company=1 and split in ('N','',' '))
		and datediff(d,txndt,@txndt)=0 --and company=1 and split in ('N','',' ')

		if @ctrlno = 0 
			begin
			print 'The ctrlno can not be zero'
			goto e_error
			end
		set @sql = 'update billhist set outlet' + ltrim(rtrim(str(@bp))) + '=' + ltrim(str(100+@channel)) + ',item'+ ltrim(rtrim(str(@bp))) + '= item'+ ltrim(rtrim(str(@bp))) + '+' + convert(varchar(20),@famt) + ',total=total+' + convert(varchar(20),@famt)  + ' where ctrlno=' + str(@ctrlno)
---print @sql goto e_error
		--update billhist set outlet1 = 100+@outlet,item1=@famt,total=total+@famt where ctrlno=@ctrlno
        exec(@sql) 
if @@error <> 0 goto e_error    
commit transaction
return
e_error:
rollback transaction
raiserror('Error in kot posting',16,-1)
return
END

GO
/****** Object:  StoredProcedure [dbo].[pbpkb1u]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:01 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:48 AM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 10/14/02 1:17:58 PM ******/
CREATE PROCEDURE [dbo].[pbpkb1u]  @TEMPTABLE VARCHAR(255), @M_OUTLET INT, @M_TXNDT DATETIME=' ' , 
@M_MOP INT=1 , @M_DRATE MONEY=0, @M_GNO_COMPCODE_CGPNO  INT=0,
@M_BRNAME_CARDNAME VARCHAR(50)=' ',@M_CRNUM CHAR(19)=' ',@M_CRTYPE VARCHAR(20)=' ',
@M_RNO INT=0,@M_WNO INT=0,@M_ROAMT MONEY=0 ,@M_RATETYPE INT,
@M_SCAMT MONEY,@M_SCRATE MONEY,@M_STSET INT,@M_SCSET INT, @obno int  out,@USERNM VARCHAR(20)='SYS',@pax int=1,
@servicetax decimal(14,2),@cess decimal(14,2),@couponamt decimal(18,2)=0,
@couponlist varchar(50) = '',@ndiscount money=0,@packingchgs money=0,
@gstin nvarchar(50)='',@gstinsplit nvarchar(50)='',@ndiscgst money=0

/* with encryption */
AS
declare @error varchar(2000)
set @error = 'Service A Billing Failed'
declare @m_kot int
DECLARE @M_CODE  INT
DECLARE @M_QTY INT
DECLARE @M_RATE MONEY
DECLARE @M_AMT MONEY
DECLARE @M_TAX MONEY
DECLARE @M_DISC MONEY
DECLARE @M_STRATE MONEY
DECLARE @SQL VARCHAR(500)
DECLARE @M_ACTNUM INT
DECLARE @M_ST_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @M_RO_ACTNUM INT
DECLARE @M_SC_ACTNUM INT
DECLARE @G_AUTOKOT char(1)
DECLARE @M_BNO INT
DECLARE @M_KOTDATE DATETIME 
DECLARE @M_BILLDATE INT
DECLARE @M_SNO INT
DECLARE @M_CHANNEL INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_YEAR INT
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_SALE MONEY
DECLARE @M_STAMT MONEY
DECLARE @M_DAMT MONEY
DECLARE @M_GRNO INT
DECLARE @M_DACTNO INT 
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_PARTIC VARCHAR(50)
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT
DECLARE @M_CO1GRNO INT
DECLARE @M_CO1FY INT
DECLARE @LUX CHAR(1)
DECLARE @FF_VOU CHAR(1)
DECLARE @HTL_RECEIVABLE INT
DECLARE @SELF_RECEIVABLE INT
declare @m_roff1 money
declare @ci_date datetime
declare @g_kotfinaltime int
declare @pf varchar(200)

declare @st1 money
declare @cess1 money
declare @sc1 money

declare @mst1 money
declare @mcess1 money
declare @msc1 money
declare @cess2 money 
declare @mcess21 money 
declare @vatonst int

select @g_kotfinaltime=g_kotfinaltime from setting
set nocount on
begin transaction
set @m_roff1=@m_roamt
if @m_mop=2
if exists(select * from room where gno=@m_gno_compcode_cgpno and ltrim(checkout)<>'' and rtrim(ltrim(hostid)) <> '')
begin
set @error = 'The selected Room is under Checkout process by some other....Billing rejected'
goto e_error
end
--set @error = @ndiscgst
---print @ndiscgst goto e_error
if @m_mop=2
  if @m_gno_compcode_cgpno=0
    begin
      set @error =  'Error in data entry..check'
      goto e_error
    end
IF @M_MOP = 2 
begin
IF NOT EXISTS (SELECT * FROM ROOM WHERE GNO = @M_GNO_COMPCODE_CGPNO) 
BEGIN
    RAISERROR('ROOM IS VACCATED DURING YOUR BILLING PROCESS',16,-1)
    RETURN
END    
if not exists (select * from room where gno=@m_gno_compcode_cgpno and rno=@m_rno)
   begin
   raiserror('Inconsistant Information on room number and guest',16,-1)
   return
   end
end
SELECT @FF_VOU='Y'
SELECT @M_SPLIT = 'N'
select @g_autokot=autokot,@m_txndt=curdate from outlet where code=@m_outlet

/***** The following 4 line included on 08/08/09*****/

declare @txndt datetime
declare @outlet numeric
set @outlet=@m_outlet
set @txndt = @m_txndt

while datediff(day,@txndt,getdate()) > 0 
begin
if datediff(day,@txndt,getdate())=1 
	begin
		if  datediff(Minute, convert(varchar(20),getdate(),101),getdate())  > @g_kotfinaltime
			begin
				if exists(select * from currentkot where outlet=@outlet)
					begin
					set @error =  'KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
					goto e_error
					end   
				else
					begin
					update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
					end
			end
		else 
		begin
		goto ok
		end

	end
else
	begin
			if exists(select * from currentkot where outlet=@outlet)
				begin
				set @error =  'KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
				goto e_error
				end   
			else
				begin
				update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
				end
	end
	   select @txndt=curdate from outlet where code=@outlet
end --- new
ok:

set @m_txndt=@txndt

set @m_txndt = convert(varchar(20),@m_txndt,106) + ' '+ convert(varchar(8),getdate(),8)
SELECT @M_TXNDT1 = CONVERT(VARCHAR(20),@M_TXNDT)
SELECT @M_SALE  = 0
SELECT @M_STAMT = 0
SELECT @M_DAMT = 0

set @m_bofy = dbo.getbofy(@m_txndt)
set @m_year = datepart(yy,@m_bofy)

SELECT @M_CHANNEL=CHANNEL FROM OUTLET WHERE CODE = @M_OUTLET
SELECT @M_COMPANY=COMPANY FROM RESTCO WHERE CHANNEL=@M_CHANNEL
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
set @m_split = 'N'
if @m_mop = 2
	begin
	select @ci_date=ci_date from guest where gno=@M_GNO_COMPCODE_CGPNO 
	if datediff(d,@ci_date,@m_txndt) < 0
		begin
		Set @error =  'Guest is checked in after the bill date of this KOT...Please make the bill after 3 o clock   '  +convert(varchar(20),@ci_date ) + '  '+  convert(varchar(20),@m_txndt)
		goto e_error
		end  
	if @M_BRNAME_CARDNAME='YES'
		set @m_split='C'
	else
		begin
		if exists(select * from gsplit where gno=@M_GNO_COMPCODE_CGPNO and outlet = @m_outlet and chtype=1)
		set @m_split = 'Y'
		end 
	end
-- Modififed on 07 MAy 2018   to give seperate gstin for split bills
if @m_split='Y' set @gstin= @gstinsplit
---


IF @G_AUTOKOT='Y'
    BEGIN
    UPDATE OUTLET SET KOTNO=CASE WHEN KOTDATE=curdate THEN KOTNO+1 ELSE 1 END,KOTDATE= CASE WHEN KOTDATE <  curDATE THEN curdate ELSE KOTDATE  END  WHERE CODE=@M_OUTLET
    IF @@ERROR<>0 GOTO E_ERROR
    SELECT @M_KOT=KOTNO FROM OUTLET WHERE CODE = @M_OUTLET
    set @sql = 'update ' + @temptable + ' set kot=' + Str(@m_kot)
    exec(@sql)
    END

exec  GetServiceASno @m_outlet,@m_txndt,@m_sno out

-- GET BILL NUMBER  - Removed on 07 May 2012

SELECT @M_OLNAME=[OLNAME],@M_ACTNUM=ACTNUM,@M_ST_ACTNUM=ST_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_RO_ACTNUM = RO_ACTNUM,@M_SC_ACTNUM=SC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_SELFDEAL=SELFDEAL,@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK ,
@LUX=LUX,@FF_VOU=FREEFOODVOUCHING,@HTL_RECEIVABLE=HTL_RECEIVABLE,@SELF_RECEIVABLE=SELF_RECEIVABLE,
@m_scset=scset,@m_stset=stset,@vatonst=vatonst
FROM CO_REST_OL  WHERE OCODE = @M_OUTLET
--set @error=  str(@m_rno) goto e_error

IF @M_SPLIT='C' AND @FF_VOU='N'  SELECT @M_SPLIT='F'
INSERT KITHIST1 (DRATE,TXNDT,OUTLET,GNO,RNO,PMODE,SNO,SPLIT,SELFDEAL,ROAMT,RATETYPE,SCAMT,SCRATE,STSET,SCSET,CRNAME,CRNUM,CRTYPE,LUX,wno,txntime,USERNM,pax,servicetax,cess,ppcouponamt,ndiscount,vatonst,packingchgs,gstin,ndiscgst)  VALUES
(@M_DRATE,@M_TXNDT,@M_OUTLET,@M_GNO_COMPCODE_CGPNO,@M_RNO,@M_MOP,@M_SNO,@M_SPLIT,@M_SELFDEAL,@M_ROAMT,@M_RATETYPE,@M_SCAMT,@M_SCRATE,@M_STSET,@M_SCSET,
@M_BRNAME_CARDNAME,@M_CRNUM,@M_CRTYPE,@LUX,@m_wno,convert(varchar(5),getdate(),8),@USERNM,@pax,@servicetax,@cess,@couponamt,@ndiscount,@vatonst,@packingchgs,@gstin,@ndiscgst)
IF @@ERROR<>0 GOTO E_ERROR
select @obno = @@identity
SELECT @M_BNO=MAX(BNO) FROM KITHIST1 WHERE OUTLET= @M_OUTLET 
if @obno<>@m_bno begin print 'Number Conflict'  goto e_error end

set @st1 = 0
set @sc1 = 0
set @cess1 = 0
set @cess2 = 0
declare  @name varchar(200)
SET @SQL = 'DECLARE UPDTKIT CURSOR FOR SELECT CODE,QTY,RATE, STRATE , AMOUNT,TAX  ,DISCOUNT,KOT,preference,st,cess,sc,cess2 FROM '+  @TEMPTABLE 
EXEC (@SQL)
OPEN UPDTKIT
FETCH NEXT FROM UPDTKIT INTO @M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_AMT,@M_TAX, @M_DISC,@M_KOT,@pf,@mst1,@mcess1,@msc1,@mcess21
WHILE  @@FETCH_STATUS=0
 BEGIN
   select @name=name from kitprime where icode=(select icode from kitmast where code=@m_code)
   SELECT @M_SALE = @M_SALE + @M_AMT
   SELECT @M_STAMT = @M_STAMT + @M_TAX
   SELECT  @M_DAMT = @M_DAMT +  @M_DISC
   select @st1 = @st1 + @mst1
   select @sc1 = @sc1 + @msc1
   select @cess1 = @cess1 + @mcess1
   select @cess2 = @cess2 + @mcess21
    INSERT KITHIST (BNO,KOT,CODE,QTY,RATE,STRATE,TAX,DISCOUNT,preference,st,cess,sc,cess2,name) VALUES
    (@M_BNO,@M_KOT,@M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_TAX,@M_DISC,@pf,@mst1,@mcess1,@msc1,@mcess21,@name)
    IF @@ERROR<>0 GOTO E_ERROR 
    FETCH NEXT FROM UPDTKIT INTO @M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_AMT,@M_TAX, @M_DISC,@M_KOT,@pf,@mst1,@mcess1,@msc1,@mcess21
END
close updtkit
DEALLOCATE UPDTKIT


if @couponamt<>0
	begin
	declare @couponno varchar(50)
	declare @camt decimal(18,2)
	declare @ecamt decimal(18,2)
	set @ecamt = 0
	set @sql = 'declare co1 cursor for select couponno,amt from ' + @couponlist 
	exec(@sql)
	open co1
	fetch next from co1 into @couponno,@camt
	while @@fetch_status=0
		begin
			if @camt <>0 and @couponno <> ''
				begin 
					insert into prepaidhist (bno,couponno,amt) values (@obno,@couponno,@camt)
					set @ecamt=@ecamt+@camt
				end
			fetch next from co1 into @couponno,@camt
		end
		close co1
		deallocate co1
		if @ecamt <> @couponamt 
			begin 
			print 'There is difference between the coupon amt ount and the list' goto e_error
			end
		set @sql = 'drop table ' + @couponlist
		--exec(@sql)
	end

declare @vatonsc tinyint
select @vatonsc=g_scvat from setting 
update kithist1 set finalamt=@M_SALE+@M_STAMT+@M_SCAMT-@M_DAMT+@M_ROAMT +@servicetax+@cess-@couponamt+@st1+@cess1+@sc1-@ndiscount+@cess2+@packingchgs-@ndiscgst ,vatonsc=@vatonsc  where bno=@obno
exec serviceavouching @obno
if @gstin <> '' and @m_mop=2  
begin
declare @kname varchar(100)
declare @kadd1  varchar(100)
declare @kadd2 varchar(100)
declare @kcity varchar(50)
declare @kstate varchar(100)
declare @kpin char(6)
declare @kphone varchar(15)
declare @kemail varchar(200)
if exists(select * from guest where gno=@M_GNO_COMPCODE_CGPNO and gstin=@gstin)
select @kname=name,@kadd1=add1,@kadd2=add2,@kcity=city,@kstate=state,@kpin=pin,@kphone=phone,@kemail=email from guest where gno=@M_GNO_COMPCODE_CGPNO 
update kithist1 set name=@kname,add1=@kadd1,add2=@kadd2,city=@kcity,state=@kstate,pin=@kpin,phone=@kphone,email=@kemail where bno=@m_bno
end


FINAL:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
if @couponlist <> ''
begin
set @sql = 'drop table ' + @couponlist
exec(@sql)
end 
COMMIT TRANSACTION
RETURN 0
E_ERROR:
ROLLBACK TRANSACTION
--raiserror ('updation failed in updtkit ',16,-1)
raiserror (@error,16,-1)
RETURN 1

GO
/****** Object:  StoredProcedure [dbo].[pbpkb1u_co]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:01 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:48 AM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 10/14/02 1:17:58 PM ******/
CREATE PROCEDURE [dbo].[pbpkb1u_co] @TEMPTABLE VARCHAR(255), @M_OUTLET INT, @M_TXNDT DATETIME=' ' , 
@M_MOP INT=1 , @M_DRATE MONEY=0, @M_GNO_COMPCODE_CGPNO  INT=0,
@M_BRNAME_CARDNAME VARCHAR(50)=' ',@M_CRNUM CHAR(19)=' ',@M_CRTYPE VARCHAR(20)=' ',
@M_RNO INT=0,@M_WNO INT=0,@M_ROAMT MONEY=0 ,@M_RATETYPE INT,
@M_SCAMT MONEY,@M_SCRATE MONEY,@M_STSET INT,@M_SCSET INT, @obno int  out,
@USERNM VARCHAR(20)='SYS',@tobe int=0,@autoposting char(1)='N',@pax int=0,
@servicetax decimal(14,2)=0,@cess decimal(14,2)=0,@ndiscount money=0,@packingchgs money=0,
@gstin nvarchar(50)='',@gstinsplit nvarchar(50)='',@ndiscgst money=0
/* with encryption */
AS
declare @userno numeric
declare @m_kot int
DECLARE @M_CODE  INT
DECLARE @M_QTY     INT
DECLARE @M_RATE  MONEY
DECLARE @M_AMT    MONEY
DECLARE @M_TAX     MONEY
DECLARE @M_DISC   MONEY
DECLARE @M_STRATE MONEY
DECLARE @SQL VARCHAR(500)
DECLARE @M_ACTNUM INT
DECLARE @M_ST_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @M_RO_ACTNUM INT
DECLARE @M_SC_ACTNUM INT
DECLARE @G_AUTOKOT char(1)
DECLARE @M_BNO INT
DECLARE @M_KOTDATE DATETIME 
DECLARE @M_BILLDATE INT
DECLARE @M_SNO INT
DECLARE @M_CHANNEL INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_YEAR INT
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_SALE MONEY
DECLARE @M_STAMT MONEY
DECLARE @M_DAMT MONEY
DECLARE @M_GRNO INT
DECLARE @M_DACTNO INT 
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_PARTIC VARCHAR(50)
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT
DECLARE @M_CO1GRNO INT
DECLARE @M_CO1FY INT
DECLARE @LUX CHAR(1)
DECLARE @FF_VOU CHAR(1)
DECLARE @HTL_RECEIVABLE INT
DECLARE @SELF_RECEIVABLE INT
declare @m_roff1 money
declare @ci_date datetime
declare @gno numeric
declare @eofy datetime
declare @err varchar(2000)

declare @st1 money
declare @cess1 money
declare @sc1 money

declare @mst1 money
declare @mcess1 money
declare @msc1 money

declare @cess2 money
declare @mcess21 money

declare @vatonst tinyint
set @err = ''
set nocount on
if not exists(select * from hmsfolder where version_no = (select 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 (9) and try'
	raiserror(@err,16,1)
	return
end
set @gno = @M_GNO_COMPCODE_CGPNO

if @tobe=0 and @m_mop=2
if not exists(select * from guest where gno=@gno and datediff(s,'1/1/90',@m_txndt) between datediff(s,'1/1/90',ci_Date-1) and datediff(s,'1/1/90',isnull(co_date,getdate())))
	begin
	set @err =  'The KOT Bill Date falls out of the stay period of the selected guest'
	raiserror(@err,16,1)
	return
end
set @m_roff1=@m_roamt
SELECT @FF_VOU='Y'
SELECT @M_SPLIT = 'N'
select @g_autokot=autokot from outlet where code=@m_outlet
SELECT @M_TXNDT1 = CONVERT(VARCHAR(20),@M_TXNDT)
SELECT @M_SALE  = 0
SELECT @M_STAMT = 0
SELECT @M_DAMT = 0
set @m_bofy = dbo.getbofy(@m_txndt)
set @m_year = datepart(yy,@m_bofy)

set @eofy = dbo.geteofy(@m_bofy) --'31 mar ' + str(@m_year+1)

SELECT @M_CHANNEL=CHANNEL,@vatonst=vatonst FROM OUTLET WHERE CODE = @M_OUTLET
SELECT @M_COMPANY=COMPANY FROM RESTCO WHERE CHANNEL=@M_CHANNEL
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
set @m_split = 'N'
BEGIN TRANSACTION

IF @G_AUTOKOT='Y'
    BEGIN
    SELECT @M_KOT=max(kot) FROM kithist WHERE bno in (select bno from kithist1 where datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@m_txndt))
     set @m_kot = @m_kot+1
    set @sql = 'update ' + @temptable + ' set kot=' + Str(@m_kot)
    exec(@sql)
    END

exec  GetServiceASno @m_outlet,@m_txndt,@m_sno out

--set @err = @m_txndt goto e_error

SELECT @M_OLNAME=[OLNAME],@M_ACTNUM=ACTNUM,@M_ST_ACTNUM=ST_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_RO_ACTNUM = RO_ACTNUM,@M_SC_ACTNUM=SC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_SELFDEAL=SELFDEAL,@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK ,
@LUX=LUX,@FF_VOU=FREEFOODVOUCHING,@HTL_RECEIVABLE=HTL_RECEIVABLE,@SELF_RECEIVABLE=SELF_RECEIVABLE,
@m_scset=scset,@m_stset=stset
FROM CO_REST_OL1  WHERE OCODE = @M_OUTLET
IF @M_SPLIT='C' AND @FF_VOU='N'  SELECT @M_SPLIT='F'

-- Modififed on 07 MAy 2018   to give seperate gstin for split bills
if @m_split='Y' set @gstin= @gstinsplit
---



INSERT KITHIST1 (DRATE,TXNDT,OUTLET,GNO,RNO,PMODE,SNO,SPLIT,SELFDEAL,ROAMT,RATETYPE,SCAMT,SCRATE,STSET,SCSET,CRNAME,CRNUM,CRTYPE,LUX,wno,txntime,USERNM,autoposting,pax,servicetax,cess,ndiscount,vatonst,packingchgs,gstin,ndiscgst)  VALUES
(@M_DRATE,@M_TXNDT,@M_OUTLET,@M_GNO_COMPCODE_CGPNO,@M_RNO,@M_MOP,@M_SNO,@M_SPLIT,@M_SELFDEAL,@M_ROAMT,@M_RATETYPE,@M_SCAMT,@M_SCRATE,@M_STSET,@M_SCSET,
@M_BRNAME_CARDNAME,@M_CRNUM,@M_CRTYPE,@LUX,@m_wno,convert(varchar(5),@m_txndt,8),@USERNM,@autoposting,@pax,@servicetax,@cess,@ndiscount,@vatonst,@packingchgs,@gstin ,@ndiscgst)
IF @@ERROR<>0 GOTO E_ERROR
select @obno = @@identity
SELECT @M_BNO=MAX(BNO) FROM KITHIST1 WHERE OUTLET= @M_OUTLET 
if @obno<>@m_bno begin print 'Number Conflict'  goto e_error end

set @st1 = 0
set @sc1 = 0
set @cess1 = 0
set @cess2 = 0
set @mcess21 = 0

SET @SQL = 'DECLARE UPDTKIT CURSOR FOR SELECT CODE,QTY,RATE, STRATE , AMOUNT,TAX  ,DISCOUNT,KOT,st,cess,sc,cess2  FROM '+  @TEMPTABLE 
EXEC (@SQL)
OPEN UPDTKIT
FETCH NEXT FROM UPDTKIT INTO @M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_AMT,@M_TAX, @M_DISC,@M_KOT,@mst1,@mcess1,@msc1,@mcess21
WHILE  @@FETCH_STATUS=0
	BEGIN
	SELECT @M_SALE = @M_SALE + @M_AMT
	SELECT @M_STAMT = @M_STAMT + @M_TAX
	SELECT  @M_DAMT = @M_DAMT +  @M_DISC
	select @st1 = @st1 + @mst1
	select @sc1 = @sc1 + @msc1
	select @cess1 = @cess1 + @mcess1
	set @cess2 = @cess2+ @mcess21
	INSERT KITHIST (BNO,KOT,CODE,QTY,RATE,STRATE,TAX,DISCOUNT,st,cess,sc,cess2) VALUES
	(@M_BNO,@M_KOT,@M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_TAX,@M_DISC,@mst1,@mcess1,@msc1,@mcess21)
	IF @@ERROR<>0 GOTO E_ERROR 
	FETCH NEXT FROM UPDTKIT INTO @M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_AMT,@M_TAX, @M_DISC,@M_KOT,@mst1,@mcess1,@msc1,@mcess21
	END

DEALLOCATE UPDTKIT
declare @vatonsc tinyint
select @vatonsc=g_scvat from setting 
update kithist1 set finalamt=@M_SALE+@M_STAMT+@M_SCAMT-@M_DAMT+@M_ROAMT +@servicetax+@cess+@st1+@cess1+@sc1+@cess2-@ndiscount+@ndiscgst,vatonsc=@vatonsc  where bno=@obno
--print 'periya1' goto e_error
begin try
exec serviceavouching @obno
end try
begin catch
set @err=error_message()
end catch

if @gstin <> '' and @m_mop=2  
begin
declare @kname varchar(100)
declare @kadd1  varchar(100)
declare @kadd2 varchar(100)
declare @kcity varchar(50)
declare @kstate varchar(100)
declare @kpin char(6)
declare @kphone varchar(15)
declare @kemail varchar(200)
if exists(select * from guest where gno=@M_GNO_COMPCODE_CGPNO and gstin=@gstin)
select @kname=name,@kadd1=add1,@kadd2=add2,@kcity=city,@kstate=state,@kpin=pin,@kphone=phone,@kemail=email from guest where gno=@M_GNO_COMPCODE_CGPNO 
update kithist1 set name=@kname,add1=@kadd1,add2=@kadd2,city=@kcity,state=@kstate,pin=@kpin,phone=@kphone,email=@kemail where bno=@m_bno
end
--GOTO E_ERROR
FINAL:
declare @finalbillamt1 money
declare @finalbillamt2 money
declare @billamt money
set @finalbillamt1 = 0
set @finalbillamt2 = 0
set @billamt = @M_SALE+@M_STAMT+@M_SCAMT-@M_DAMT+@M_ROAMT  +@servicetax+@cess +@st1+@cess1+@sc1+@cess2-@ndiscount
if @tobe=0 and @m_mop=2
begin
   select @userno=userno from comuser where [host_id]=host_id() and sessionid=''
   set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@finalbillamt1) + ',' + convert(varchar(20),@finalbillamt2) + ',' + str(@m_company) + ',' + str(100+@m_outlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,' +convert(varchar(20),@billamt)+    ',''N'','+str(@userno)+','''+@lux+''''
   exec (@sql)
   if @@error <> 0  goto e_error
end
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
COMMIT TRANSACTION
RETURN 0
E_ERROR:
ROLLBACK TRANSACTION
set @err = 'updation failed in updtkit ' + @err 
raiserror (@err,16,-1)
RETURN 1

GO
/****** Object:  StoredProcedure [dbo].[pbpkb2uko]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[pbpkb2uko] 
@TEMPTABLE VARCHAR(255),@outlet numeric, @kotype int,@kotcode varchar(50),@wno int,@ratetype int
/* with encryption */
AS
declare @kot int
declare @txndt datetime
declare @sql varchar(1000)
declare @g_autokot char(1)
declare @code int
declare @qty int
declare @rate money
declare @strate money
declare @tax money
declare @amt money
declare @oldrt int
declare @kotype1 int
declare @kotcode1 int
declare @g_kotfinaltime int
declare @pf1 varchar(300)
declare @kotconstant numeric
select @g_kotfinaltime=g_kotfinaltime from setting 
if not exists(select * from kotconstant) insert into kotconstant values(0)
update kotconstant set ctrlno=ctrlno+1
select @kotconstant=ctrlno from kotconstant 
set nocount on
--@TEMPTABLE VARCHAR(255), @OUTLET INT,@kotype int,@kotcode int,@wno int,@ratetype int
begin transaction
if not exists(select * from hmsfolder where version_no = (select version_no from comuser where [host_id]=host_id()) ) 
  begin 
  declare @hmsfolder varchar(50)
  declare @comuser varchar(50)
  select top 1 @hmsfolder=version_no from hmsfolder
  select top 1 @comuser=version_no from comuser
  print 'You are using a wrong version ' + @comuser + '  of the Softwre Installed...Please update the version (10) and try - ' + @hmsfolder
  goto e_error
end
if @kotype=2
if exists(select * from room where gno=@kotcode and ltrim(checkout)<>'' and rtrim(ltrim(hostid)) <> '')
begin
print 'The selected Room is under Checkout process by some other....Billing rejected'
goto e_error
end
IF @kotype = 2
IF NOT EXISTS (SELECT * FROM ROOM WHERE GNO = @kotcode) 
BEGIN
    RAISERROR('ROOM IS VACCATED DURING YOUR BILLING PROCESS',16,-1)
    RETURN
END    
select @txndt=curdate,@g_autokot=autokot from outlet where code=@outlet

while datediff(day,@txndt,getdate()) > 0 
begin
if datediff(day,@txndt,getdate())=1 
	begin
		if  datediff(Minute, convert(varchar(20),getdate(),101),getdate())  > @g_kotfinaltime
			begin
				if exists(select * from currentkot where outlet=@outlet)
					begin
					print 'KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
					goto e_error
					end   
				else
					begin
					update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
					end
			end
		else 
		begin
		goto ok
		end

	end
else
	begin
			if exists(select * from currentkot where outlet=@outlet)
				begin
				print 'KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
				goto e_error
				end   
			else
				begin
				update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
				end
	end
	   select @txndt=curdate from outlet where code=@outlet
end --- new
ok:


--SELECT @G_AUTOKOT=0  FROM SETTING
-- GET KOT NUMBER
IF @G_AUTOKOT='Y'
    BEGIN
    UPDATE OUTLET SET KOTNO=CASE WHEN KOTDATE=curdate THEN KOTNO+1 ELSE 1 END,KOTDATE= CASE WHEN KOTDATE <  curDATE THEN curdate ELSE KOTDATE  END  WHERE CODE=@OUTLET
    IF @@ERROR<>0 GOTO E_ERROR
    SELECT @KOT=KOTNO FROM OUTLET WHERE CODE = @OUTLET
    set @sql = 'update ' + @temptable + ' set kot = ' + Str(@kot)
    exec(@sql)
    END
if exists(select * from currentkot where outlet=@outlet and pend_type=@kotype and pend_code =@kotcode)
  begin
   select top 1 @oldrt=ratetype from currentkot  where outlet=@outlet and pend_type=@kotype and pend_code =@kotcode
   if @oldrt<>@ratetype
      begin
      print 'The rate type entered earlier differ from the current rate type....ratetype should be same for any single bill'
      goto e_error
      end
   end

declare @sqno numeric
if not exists(select * from sqno) insert into sqno values (0)
update sqno set sqno=sqno+1
select @sqno=sqno from SQNO

SET @SQL = 'DECLARE UPDTKIT CURSOR FOR SELECT CODE,QTY,RATE, STRATE , AMOUNT,TAX  ,KOT,kotype,kotcode,preference  FROM '+  @TEMPTABLE 
EXEC (@SQL)
OPEN UPDTKIT
FETCH NEXT FROM UPDTKIT INTO @code,@qty,@rate,@strate,@amt,@tax,@kot,@kotype1,@kotcode1,@pf1
WHILE  @@FETCH_STATUS=0
 BEGIN
    INSERT currentkot (outlet,kot,code,qty,rate,strate,tax,pend_type,pend_code,waiter,ratetype,preference,sqno,kotconstant) VALUES
    (@outlet,@kot,@code,@qty,@rate,@strate,@tax,@kotype,@kotcode,@wno,@ratetype,@pf1,@sqno,@kotconstant)
    IF @@ERROR<>0 GOTO E_ERROR 
    FETCH NEXT FROM UPDTKIT INTO @code,@qty,@rate,@strate,@amt,@tax,@kot,@kotype1,@kotcode1,@pf1
END
DEALLOCATE UPDTKIT
FINAL:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
COMMIT TRANSACTION
RETURN 0
E_ERROR:
ROLLBACK TRANSACTION
raiserror ('updation failed in updtkit ',16,-1)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
RETURN 1




GO
/****** Object:  StoredProcedure [dbo].[pbpkb2uko_multi]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO



/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE  PROCEDURE [dbo].[pbpkb2uko_multi]
@TEMPTABLE VARCHAR(255), @kotype int,@kotcode varchar(50),@wno int,@ratetype int,@sqno numeric out
/* with encryption */
AS
declare @outlet numeric
declare @kot int
declare @txndt datetime
declare @sql varchar(5000)
declare @g_autokot char(1)
declare @code int
declare @qty int
declare @rate money
declare @strate money
declare @tax money
declare @amt money
declare @oldrt int
declare @kotype1 int
declare @kotcode1 varchar(50)
declare @g_kotfinaltime int
declare @preference varchar(500)
declare @kotconstant numeric
declare @scrate money

if not exists(select * from kotconstant) insert into kotconstant values(0)
update kotconstant set ctrlno = ctrlno+1
select @kotconstant=ctrlno from kotconstant 

select @g_kotfinaltime=g_kotfinaltime from setting
set nocount on
begin transaction
--if not exists(select * from hmsfolder where version_no = (select top 1 version_no from comuser where [host_id]=host_id()) ) 
--  begin 
--  print 'You are using a wrong version of the Softwre Installed...Please update the version (11) and try'
--  goto e_error
--end
if @kotype=2
if exists(select * from room where gno=@kotcode and ltrim(checkout)<>'' and rtrim(ltrim(hostid)) <> '')
begin
print 'The selected Room is under Checkout process by some other....Billing rejected'
goto e_error
end
IF @kotype = 2
IF NOT EXISTS (SELECT * FROM ROOM WHERE GNO = @kotcode) 
begin
    print  'ROOM IS VACCATED DURING YOUR BILLING PROCESS'
    goto e_error
end
if not exists(select * from sqno) insert into sqno values (0)
update sqno set sqno=isnull(sqno,0)+1
select @sqno=sqno from SQNO

select * into #dummykit from dummykit
set @sql = 'insert  into #dummykit select * from ' + @temptable
exec(@sql)
declare c11 cursor for select distinct outlet from kitmast_v join #dummykit a on a.code=kitmast_v.code
open c11
fetch next from c11 into @outlet
while @@fetch_status=0 begin
--
select @txndt=curdate,@g_autokot=autokot from outlet where code=@outlet
while datediff(day,@txndt,getdate()) > 0 
begin
if datediff(day,@txndt,getdate())=1 
	begin
		if  datediff(Minute, convert(varchar(20),getdate(),101),getdate())  > @g_kotfinaltime
			begin
				if exists(select * from currentkot where outlet=@outlet)
					begin
					print 'KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
					goto e_error
					end   
				else
					begin
					update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
					end
			end
		else 
			begin
			goto ok
			end

	end
else
	begin
			if exists(select * from currentkot where outlet=@outlet)
				begin
				print 'KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
				goto e_error
				end   
			else
				begin
				update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
				end
	end
	   select @txndt=curdate from outlet where code=@outlet

end --- new
ok:

select * into #dummykit1 from dummykit
set @sql = 'insert into #dummykit1 select * from ' + @temptable + ' where code in (select code from kitmast_v where outlet=' + str(@outlet) + ')'
exec(@sql)
IF @G_AUTOKOT='Y'
	BEGIN
		UPDATE OUTLET SET KOTNO=CASE WHEN datediff(d,KOTDATE,curdate)=0 THEN KOTNO+1 ELSE 1 END,KOTDATE= CASE WHEN KOTDATE <  curDATE THEN curdate ELSE KOTDATE  END  WHERE CODE=@OUTLET
		IF @@ERROR<>0 GOTO E_ERROR
		SELECT @KOT=KOTNO FROM OUTLET WHERE CODE = @OUTLET
		--set @sql = 'update ' + @temptable + ' set kot = ' + Str(@kot)
		--exec(@sql)
		update #dummykit1 set kot=@kot
	END
if exists(select * from currentkot where outlet=@outlet and pend_type=@kotype and pend_code =@kotcode)
	begin
		select top 1 @oldrt=ratetype from currentkot  where outlet=@outlet and pend_type=@kotype and pend_code =@kotcode
		if @oldrt<>@ratetype
			begin
			print 'The rate type entered earlier differ from the current rate type....ratetype should be same for any single bill'
			goto e_error
			end
	end
--SET @SQL = 'DECLARE UPDTKIT CURSOR FOR SELECT CODE,QTY,RATE, STRATE , AMOUNT,TAX  ,KOT,kotype,kotcode  FROM '+  @TEMPTABLE 
--EXEC (@SQL)
declare updtkit cursor for select code,qty,rate,strate,amount,tax,kot,kotype,kotcode,preference,sc from #dummykit1
OPEN UPDTKIT
FETCH NEXT FROM UPDTKIT INTO @code,@qty,@rate,@strate,@amt,@tax,@kot,@kotype1,@kotcode1,@preference,@scrate
WHILE  @@FETCH_STATUS=0
	BEGIN
	    set @tax = @rate*@qty/100
		INSERT currentkot (outlet,kot,code,qty,rate,strate,tax,pend_type,pend_code,waiter,ratetype,preference,sqno,kotconstant,scrate) VALUES
		(@outlet,@kot,@code,@qty,@rate,@strate,@tax,@kotype,@kotcode,@wno,@ratetype,@preference,@sqno,@kotconstant,@scrate)
		IF @@ERROR<>0 GOTO E_ERROR 
		FETCH NEXT FROM UPDTKIT INTO @code,@qty,@rate,@strate,@amt,@tax,@kot,@kotype1,@kotcode1,@preference,@scrate
	END
close updtkit
deallocate updtkit
drop table #dummykit1
fetch next from c11 into @outlet
end
close c11
deallocate c11
FINAL:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
COMMIT TRANSACTION
RETURN 0
E_ERROR:
ROLLBACK TRANSACTION
raiserror ('Updation Failed in UPDTKIT',16,-1)
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
RETURN 1

GO
/****** Object:  StoredProcedure [dbo].[pbplt]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





/* PowerBrain Software (India) New Delhi*/
CREATE  PROCEDURE [dbo].[pbplt] @gno numeric 
/* with encryption */
AS
--print 'periya'
declare @cod datetime
declare @cid datetime
declare @sql varchar(5000)
declare @cno numeric
declare @rno numeric
declare @orno numeric
declare @trno int
declare @erno int
declare @diff int
declare @cnt numeric
declare @cnt1 int
declare @rent money
declare @lt money
declare @taxable money
declare @amt money
declare @x numeric
declare @ctrlno numeric
declare @totalbefore money
declare @totalbeforeind money
declare @discount money
declare @head int
declare @rentrate2 money
declare @bno numeric
declare @foodbill money
declare @foodbill1 money
declare @brno numeric
declare @stcal tinyint

declare @st numeric(18,2)
declare @stontariff char(3)
declare @strate money
declare @stdiscount money
declare @stlessamt numeric(18,2)
declare @cessrate money
declare @cess numeric(18,2)
declare @allinclusive char(1)
declare @renttype int
declare @txndt datetime
declare @lrate money
declare @cess2 money
declare @cessonwhat2 int
declare @cessrate2 money
begin transaction
set @trno=0
set @erno=0
select @brno=brno,@stcal=st,@renttype=renttype1 from guest where gno=@gno
select @cno=cno,@bno=bno from billhist1 where brno = @brno and company=1 and split='N'
select @rno=rno,@cod=co_date,@cid=ci_date,@head=male+female from guest where gno=@gno
set @orno=@rno
select * into #dummy from billhist where gno=@gno
select @totalbefore=sum(rent+lt-discount) from billhist where cno=@cno
select @totalbeforeind=sum(isnull(rent,0)+isnull(lt,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0) +isnull(cess2,0) ) from #dummy

select @rentrate2=case when @head > 1 then rentrate22 else rentrate21 end  from room where rno=@rno
if @rentrate2=0 begin print 'Bill.No. ' + ltrim(str(@bno)) + 'New Room Rent is Zero' goto e_error end
/* This was altered on 29-07-2007 ....To be tracked with different hotels */
exec spltp2 @rentrate2,@cod,@lt out,1,@rno,@rentrate2,@head,0,1
select @trno=round(cast(rent+lt-discount as int )/cast(@rentrate2+@lt as int) ,0)  from #dummy where rent+lt-discount = (select max(rent+lt-discount) from #dummy)
if @trno <=0 
begin
select @rent=max(rent) from #dummy
print 'Sys.No. ' + convert(varchar(20),@gno) + ' Bill No.' + convert(varchar(20),@bno) + ' The Rent2 - ' + convert(varchar(20),@rentrate2) + '  is higher than the charged rent - '  + convert(varchar(20),@rent)
goto e_error

end
select @erno=count(*) from addroom where gno=@gno and rno<>@rno
set @erno=isnull(@erno,0)
set @erno=@erno+1
set @diff=@trno-@erno
--print str(@trno) + ' ' + str(@erno) goto e_error
---select @trno=round(cast(rent+lt-discount as int )/cast(@rentrate2 as int) ,0)  from #dummy where rent+lt-discount = (select max(rent+lt-discount) from #dummy)
update billhist set orent=rent where gno=@gno
update #dummy set rent=@rentrate2*(@trno) where rent<>0
update guest  set rent=@rentrate2*(@trno) where gno=@gno
update #dummy set lt=0
update #dummy set discount=0
if exists(select count(*) from guest where gno=@gno and omale=0)  update guest set omale=male where gno=@gno
if @head<@diff+1
   update guest set male=male + ((@diff+1) - @head) where gno=@gno
select @head=male+female from guest where gno=@gno
declare @totroom int
set @totroom=@trno

declare tc1 cursor for select rent,lt,taxable,ctrlno,discount,txndt,st,cess,cess2 from #dummy
open tc1
fetch next from tc1 into @rent,@lt,@taxable,@ctrlno,@discount,@txndt,@st,@cess,@cess2
while @@fetch_status=0 begin
set @amt=@rent
exec spltp2 @amt,@cod,@lt out,@totroom,@rno,@rent,@head,@discount,1 ,0,@lrate out
update #dummy set lt=isnull(@lt,0) where current of tc1

--print convert(varchar(20),@lt) + ' ' + convert(varchar(20),@rent) goto e_error

---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
declare @amttemp money
if @stcal=0   -- added on 05 nov 2013
	if @rent<>0
	begin
		select top 1  @stontariff=ontariff from sttable where (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		if upper(@stontariff)='YES'
			begin
				declare @tariff money
				--set @tariff=    (select case when @head=1 then rent1 else rent2 end  from room where rno=@crno)
				exec findrent @renttype,@rno,@head,@txndt,0,@tariff out
				set @amttemp=case when @rent/@totroom > @tariff then @rent/@totroom else @tariff end
			end
		else
			set @amttemp=(@rent-@discount)/@totroom
			set @strate = 0

set @st=0


		if exists(select * from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)))
		select @strate=rate,@stdiscount=discount,@stlessamt=lamt,@cessrate=cess , 
		@cessonwhat2=cessonwhat,@cessrate2=isnull(cess2,0)
		from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		
		set @strate = isnull(@strate,0)
		set @stdiscount = isnull(@stdiscount,0)
		set @stlessamt=isnull(@stlessamt,0)
		set @cessrate = isnull(@cessrate,0)

		set @amttemp = @amttemp - @stlessamt
		set @st=0
		set @cess=0
		set @cess2 = 0
		--if @amttemp>0 
		--	if @strate<>0
		--		set @st = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@strate/100)) as decimal(12,2))
		--	else
		--		set @st=0
		
		--if @st > 0 
		--	begin
		--	if @cessrate > 0 
		--		set @cess = @st * @cessrate / 100
		--	if @cessrate2 > 0
		--		set @cess2= @st * @cessrate2/100
		--	end
/*		set @sql= 'update ' + @temptable + ' set st = ' + convert(varchar(20),@st)  +'  where current of cur_temp'
		exec(@sql)
		
		set @sql= 'update ' + @temptable + ' set cess = ' + convert(varchar(20),@cess)  +'  where current of cur_temp'
		exec(@sql)

*/
        exec GetST @txndt,@rno ,@amttemp,0,@st out,@cess out,1,@cess2 out


		set @st = @st * @totroom
		set @cess = @cess * @totroom
		set @cess2 = @cess2 * @totroom 

		update #dummy set st=isnull(@st,0),cess=isnull(@cess,0),cess2=isnull(@cess2,0)  where current of tc1


	end

---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


update billhist set lrate=@lrate, rent=isnull(@rent,0),lt=isnull(@lt,0),discount=isnull(@discount,0),st=@st,cess=@cess,cess2=@cess2,total=isnull(@rent,0)+isnull(@lt,0)+sc+ ISNULL(@st,0)+ISNULL(@CESS,0)+@cess2+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-isnull(@discount,0) where ctrlno=@ctrlno
if @@error <>0 goto e_error
fetch next from tc1 into @rent,@lt,@taxable ,@ctrlno,@discount,@txndt,@st,@cess,@cess2
end
close tc1
deallocate tc1


select @lt=sum(lt) from #dummy
declare @roomtype char(50)
select @roomtype = roomtype from room where rno=@rno
if @diff > 0 begin
	if exists(select RNo from room where roomtype=@roomtype and rno not in (select rno from addroom where gno=@gno union select rno from billhist where (datediff(d,'1/1/90',txndt) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod)) and rent <> 0 and rno is not null))  begin
		select RNo  into #dummy1 from room where roomtype=@roomtype and rno not in (select rno from addroom where gno in (select gno from guest where (datediff(d,'1/1/90',ci_date) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod))  and (datediff(d,'1/1/90',co_date) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod)))                union select rno from billhist where (datediff(d,'1/1/90',txndt) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod)) and rent <> 0 and rno is not null)
		declare tc cursor for select rno from #dummy1
		select @cnt=count(*) from #dummy1
		open tc 
		fetch next from tc into @rno  
		while @@fetch_status=0 begin
			insert into addroom (gno,rno) values (@gno,@rno)
			set @diff=@diff-1
			if @diff= 0  break           
			fetch next from tc into @rno  
		end
		close tc
		deallocate tc
		drop table #dummy1
	end
end  
if @diff > 0 begin
	select @cnt1=cast(count(*) as int) from room where roomtype=@roomtype and rno not in (select rno from addroom where gno=@gno union select rno from room where rno=@orno)
	declare tc2 cursor for select rno from room where roomtype=@roomtype and  rno not in  (select rno from addroom where gno=@gno ) and rno<>@orno
	open tc2
	fetch next from tc2 into @rno
	while @@fetch_status=0 begin
		if @diff=0 break
		insert into addroom (rno,gno) values (@rno,@gno)
		set @diff=@diff-1
		fetch next from tc2 into @rno
	end
	close tc2
	deallocate tc2
end
declare @grno numeric
declare @diffb money
declare @fy int
exec getfy @cod,1,@fy out
if @fy=0 goto e_error
if @@error <> 0 goto e_error
select @grno=grno from billhist1 where cno=@cno
set @rent=0
set @lt=0
select @rent=sum(isnull(rent,0)),@lt=sum(isnull(lt,0)),@st=sum(isnull(st,0)),@cess=sum(isnull(cess,0)),@cess2=sum(isnull(cess2,0))   from #dummy
if @rent+@lt+@st+@cess+@cess2<@totalbeforeind 
	begin
    set @foodbill1 = @totalbeforeind-(@rent+@lt+@st+@cess+@cess2)
    --print convert(varchar(20),ISNULL(@foodbill1,0)) + ' ' + convert(varchar(20),@totalbeforeind,0) + ' ' + convert(varchar(20),(@rent+@lt+@st+@cess)) + ' ' + convert(varchar(20),@rent)  + ' ' + convert(varchar(20),@lt)  + ' ' + convert(varchar(20),@st)  + ' ' + convert(varchar(20),@cess) goto e_error
 	update billhist1 set foodbill=foodbill + @foodbill1 where cno=@cno
	if @@error <> 0 begin print 'Food Bill amount updation in Bill not effected' goto e_error end
	end

if @diffb > 0  
begin
	update billhist1 set spldiscount=spldiscount+@diffb where cno=@cno
end
else
	if @rent+@lt+@st+@cess+@cess2 > @totalbeforeind begin print ltrim(str(@bno) )+ ' '+ convert(varchar(20),@cod,103)  + ' - There may be variation in Rent during the stay. Total  of Rent and LT before is ' + convert(varchar(20),@totalbefore) + '  where as New Rent ' + convert(varchar(20),@rent) + ' +  LT '+convert(varchar(20),@lt)  + ' is ' + convert(varchar(20),@rent+@lt) + ' - Process Aborted'  goto e_error end
exec PbVC11 @cno
commit transaction
return
e_error:
rollback transaction
raiserror('error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbplt2015]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

/* PowerBrain Software (India) New Delhi*/
CREATE  PROCEDURE [dbo].[pbplt2015] @gno numeric 
/* with encryption */
AS

declare @cod datetime
declare @cid datetime
declare @sql varchar(5000)
declare @cno numeric
declare @rno numeric
declare @orno numeric
declare @trno int
declare @erno int
declare @diff int
declare @cnt numeric
declare @cnt1 int
declare @rent money
declare @lt money
declare @taxable money
declare @amt money
declare @x numeric
declare @ctrlno numeric
declare @totalbefore money
declare @totalbeforeind money
declare @discount money
declare @head int
declare @rentrate2 money
declare @bno numeric
declare @foodbill money
declare @foodbill1 money
declare @brno numeric
declare @stcal tinyint

declare @st numeric(18,2)
declare @stontariff char(3)
declare @strate money
declare @stdiscount money
declare @stlessamt numeric(18,2)
declare @cessrate money
declare @cess numeric(18,2)
declare @allinclusive char(1)
declare @renttype int
declare @txndt datetime
declare @outlet int
declare @item varchar(20)
declare @lrate money
declare @totold money
declare @totnew money
declare @diffnew money
declare @famt money
declare @tospldiscount money
declare @cess2 money

select @outlet=code,@item='item' + ltrim(rtrim(str(channel))) from outlet where onepercent=1

begin transaction

set @trno=0
set @erno=0
select @brno=brno,@stcal=st,@renttype=renttype1 from guest where gno=@gno
select @cno=cno,@bno=bno from billhist1 where brno = @brno and company=1 and split='N'
select @rno=rno,@cod=co_date,@cid=ci_date,@head=male+female from guest where gno=@gno
set @orno=@rno
--select * into #dummy from billhist where gno=@gno
select *,rentlt=rent,ltlt=lt,discountlt=discount,stlt=st,cesslt=cess,cess2lt=cess2,itemx=0 into #dummy from billhist where gno=@gno

update #dummy set rentlt=rent,ltlt=lt,discountlt=discount,stlt=st,cesslt=cess
select @totalbefore=sum(rent+lt-discount) from billhist where cno=@cno
select @totalbeforeind=sum(isnull(rent,0)+isnull(lt,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0)+isnull(cess2,0) ) from #dummy
select @rentrate2=case when @head > 1 then rentrate22 else rentrate21 end  from room where rno=@rno
if @rentrate2=0 begin print 'Bill.No. ' + ltrim(str(@bno)) + 'New Room Rent is Zero' goto e_error end

exec spltp2 @rentrate2,@cod,@lt out,1,@rno,@rentrate2,@head,0,1,0,@lrate out 

--select @trno=round(cast(rent+lt-discount as int )/cast(@rentrate2+@lt as int) ,0)  from #dummy where rent+lt-discount = (select max(rent+lt-discount) from #dummy)
delete from addroom where gno=@gno
--select @erno=count(*) from addroom where gno=@gno and rno<>@rno
--set @erno=isnull(@erno,0)
--set @erno=@erno+1
--set @diff=@trno-@erno
--print str(@trno) + ' ' + str(@erno) goto e_error
---select @trno=round(cast(rent+lt-discount as int )/cast(@rentrate2 as int) ,0)  from #dummy where rent+lt-discount = (select max(rent+lt-discount) from #dummy)
update billhist set orent=rent where gno=@gno
update #dummy set rent=@rentrate2 where rent<>0
update #dummy set lt=0
update #dummy set discount=0
if exists(select count(*) from guest where gno=@gno and omale=0)  update guest set omale=male where gno=@gno
--if @head<@diff+1
--   update guest set male=male + ((@diff+1) - @head) where gno=@gno
select @head=male+female from guest where gno=@gno
declare @totroom int

set @totroom=1 --@trno
set @trno=1
declare tc1 cursor for select rent,lt,taxable,ctrlno,discount,txndt,st,cess,cess2 from #dummy
open tc1
fetch next from tc1 into @rent,@lt,@taxable,@ctrlno,@discount,@txndt,@st,@cess,@cess2
while @@fetch_status=0 begin
set @amt=@rent
exec spltp2 @amt,@cod,@lt out,@totroom,@rno,@rent,@head,@discount,1 
update #dummy set lt=isnull(@lt,0) where current of tc1

--print convert(varchar(20),@lt) + ' ' + convert(varchar(20),@rent) goto e_error

---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
declare @amttemp money
if @stcal=0   -- added on 05 nov 2013
	if @rent<>0
	begin
		select top 1  @stontariff=ontariff from sttable where (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		if upper(@stontariff)='YES'
			begin
				declare @tariff money
				--set @tariff=    (select case when @head=1 then rent1 else rent2 end  from room where rno=@crno)
				exec findrent @renttype,@rno,@head,@txndt,0,@tariff out
				set @amttemp=case when @rent/@totroom > @tariff then @rent/@totroom else @tariff end
			end
		else
			set @amttemp=(@rent-@discount)/@totroom
			set @strate = 0
set @st=0
		if exists(select * from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)))
		select @strate=rate,@stdiscount=discount,@stlessamt=lamt,@cessrate=cess  from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		
		set @strate = isnull(@strate,0)
		set @stdiscount = isnull(@stdiscount,0)
		set @stlessamt=isnull(@stlessamt,0)
		set @cessrate = isnull(@cessrate,0)

		set @amttemp = @amttemp - @stlessamt
		set @st=0
		set @cess=0
		set @cess2 = 0

		--if @amttemp>0 
		--	if @strate<>0
		--		set @st = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@strate/100)) as decimal(12,2))
		--	else
		--		set @st=0
		
		--if @st > 0 
		--	if @cessrate > 0 
		--		set @cess = @st * @cessrate / 100

        exec GetST @txndt,@rno ,@amttemp,0,@st out,@cess out,1,@cess2 out


/*		set @sql= 'update ' + @temptable + ' set st = ' + convert(varchar(20),@st)  +'  where current of cur_temp'
		exec(@sql)
		
		set @sql= 'update ' + @temptable + ' set cess = ' + convert(varchar(20),@cess)  +'  where current of cur_temp'
		exec(@sql)

*/
		set @cess2 = isnull(@cess2,0)

		set @st = @st * @totroom
		set @cess = @cess * @totroom
		set @cess2 = @cess2 * @totroom

		update #dummy set st=isnull(@st,0),cess=isnull(@cess,0),cess2=isnull(@cess2,0)  where current of tc1


	end

---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

update billhist set lrate=@lrate,rent=isnull(@rent,0),lt=isnull(@lt,0),discount=isnull(@discount,0),st=@st,cess=@cess,cess2=@cess2,total=isnull(@rent,0)+isnull(@lt,0)+sc+ ISNULL(@st,0)+ISNULL(@CESS,0)+ISNULL(@CESS2,0)+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-isnull(@discount,0) where ctrlno=@ctrlno

if @@error <>0 goto e_error
fetch next from tc1 into @rent,@lt,@taxable ,@ctrlno,@discount,@txndt,@st,@cess,@cess2
end
close tc1
deallocate tc1

select @lt=sum(lt) from #dummy
declare @roomtype char(50)
select @roomtype = roomtype from room where rno=@rno

declare @grno numeric
declare @diffb money
declare @fy int
exec getfy @cod,1,@fy out
if @fy=0 goto e_error
if @@error <> 0 goto e_error
select @grno=grno from billhist1 where cno=@cno
set @rent=0
set @lt=0
select @rent=sum(isnull(rent,0)),@lt=sum(isnull(lt,0)),@st=sum(isnull(st,0)),@cess=sum(isnull(cess,0)),@cess2=sum(isnull(cess2,0)),@discount=sum(isnull(discount,0))  from #dummy
if @rent+@lt+@st+@cess+@cess2<@totalbeforeind 
	begin
    set @foodbill1 = @totalbeforeind-(@rent+@lt+@st+@cess+@cess2-@discount)
    --print convert(varchar(20),ISNULL(@foodbill1,0)) + ' ' + convert(varchar(20),@totalbeforeind,0) + ' ' + convert(varchar(20),(@rent+@lt+@st+@cess)) + ' ' + convert(varchar(20),@rent)  + ' ' + convert(varchar(20),@lt)  + ' ' + convert(varchar(20),@st)  + ' ' + convert(varchar(20),@cess) goto e_error
	declare @olddata money
	declare @newdata money
	declare @amt1 money

	declare @amt2 money

	declare d1 cursor for select txndt,(rent+lt+st+cess+cess2-discount) newrent,(rentlt+ltlt+stlt+cesslt+cess2lt-discountlt) oldrent from #dummy
	open d1
	fetch next from d1 into @txndt,@newdata,@olddata
    while @@fetch_status=0
		begin
		if @olddata > @newdata 
			begin
				set @amt1 = @olddata-@newdata
				if @amt1 > 25
					begin

					while @amt1 > 25
						begin
							set @amt2 = case 
							when @amt1 > 25 and @amt1 <= 200  then @amt1
							when @amt1 > 200 and @amt1 <= 300  then 200
							when @amt1 > 300 and @amt1 <= 400  then 300
							when @amt1 > 400 and @amt1 <= 500  then 400
							when @amt1 > 500  then 500
							end 

							EXEC dbo.pbpk1bill @amt2,@txndt,@gno,@famt out
							if @@error <> 0 goto e_error    
							set @diffb = isnull(@diffb,0)+(@amt2-@famt)
							set @foodbill1=@foodbill1-@famt
							--print 'amt ' + convert(varchar(20),@amt1) + ' ' + convert(varchar(20),@amt2)
							set @amt1 =  @amt1 - @amt2 
							--print 'amt ' + convert(varchar(20),@amt1) + ' ' + convert(varchar(20),@amt2) goto e_error

						end
/*
					EXEC dbo.pbpk1bill @amt1,@txndt,@gno,@famt out
					if @@error <> 0 goto e_error    
					set @diffb = isnull(@diffb,0)+(@amt1-@famt)
					set @foodbill1=@foodbill1-@famt
*/

--					print convert(varchar(20),@txndt) + 'Food Bill ' +  convert(varchar(20),@foodbill1) + ' Sent Amount ' + convert(varchar(20),@amt1) + ' Vouchers amt ' + convert(varchar(20),@famt)
--goto e_error
--print convert(varchar(20),@foodbill1)					if @foodbill1 < 0 
--					print convert(varchar(20),@foodbill1)  +' ' + convert(varchar(20),@amt1) + ' ' + convert(varchar(20),@txndt) + ' ' + str(@gno) + ' ' + convert(varchar(20),@olddata) --+ ' ' + convert(varchar(20),@newdata) goto e_error 
					end
			end
		fetch next from d1 into @txndt,@newdata,@olddata
		end
	close d1
	deallocate d1

 	update billhist1 set foodbill=foodbill + @foodbill1 where cno=@cno
	if @@error <> 0 begin print 'Food Bill amount updation in Bill not effected' goto e_error end
	end

--if @diffb > 0  
--	begin
--		update billhist1 set spldiscount=spldiscount+@diffb where cno=@cno
--	end
--else
--	if @rent+@lt+@st+@cess > @totalbeforeind begin print ltrim(str(@bno) )+ ' '+ convert(varchar(20),@cod,103)  + ' - There may be variation in Rent during the stay. Total  of Rent and LT before is ' + convert(varchar(20),@totalbefore) + '  where as New Rent ' + convert(varchar(20),@rent) + ' +  LT '+convert(varchar(20),@lt)  + ' is ' + convert(varchar(20),@rent+@lt) + ' - Process Aborted'  goto e_error end
exec PbVC11 @cno
commit transaction
return
e_error:
rollback transaction
raiserror('error',16,-1)
return



GO
/****** Object:  StoredProcedure [dbo].[pbpltNEW]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




/* PowerBrain Software (India) New Delhi*/
CREATE  PROCEDURE [dbo].[pbpltNEW] @gno numeric 
/* with encryption */
AS
--print 'periya'
declare @cod datetime
declare @cid datetime
declare @sql varchar(5000)
declare @cno numeric
declare @rno numeric
declare @orno numeric
declare @trno int
declare @erno int
declare @diff int
declare @cnt numeric
declare @cnt1 int
declare @rent money
declare @lt money
declare @taxable money
declare @amt money
declare @x numeric
declare @ctrlno numeric
declare @totalbefore money
declare @totalbeforeind money
declare @discount money
declare @head int
declare @rentrate2 money
declare @bno numeric
declare @foodbill money
declare @foodbill1 money
declare @brno numeric
declare @stcal tinyint

declare @st numeric(18,2)
declare @stontariff char(3)
declare @strate money
declare @stdiscount money
declare @stlessamt numeric(18,2)
declare @cessrate money
declare @cess numeric(18,2)
declare @cessrate2 money
declare @cess2 numeric(18,2)
declare @allinclusive char(1)
declare @renttype int
declare @txndt datetime
declare @outlet int
declare @item varchar(20)
declare @lrate money

select @outlet=code,@item='item' + ltrim(rtrim(str(channel))) from outlet where onepercent=1

begin transaction
set @trno=0
set @erno=0
select @brno=brno,@stcal=st,@renttype=renttype1 from guest where gno=@gno
select @cno=cno,@bno=bno from billhist1 where brno = @brno and company=1 and split='N'
select @rno=rno,@cod=co_date,@cid=ci_date,@head=male+female from guest where gno=@gno
set @orno=@rno
--select * into #dummy from billhist where gno=@gno
select *,rentlt=rent,ltlt=lt,discountlt=discount,stlt=st,cesslt=cess,itemx=0,cess2lt=cess2 into #dummy from billhist where gno=@gno

-- 04 November 2014
--alter table #dummy add rentlt money not null default 0
--alter table #dummy add ltlt money not null default 0 
--alter table #dummy add discountlt money not null default 0 
--alter table #dummy add stlt money not null default 0 
--alter table #dummy add cesslt money not null default 0 
--alter table #dummy add itemx money not null default 0 
update #dummy set rentlt=rent,ltlt=lt,discountlt=discount,stlt=st,cesslt=cess
--exec sp_executesql 'update #dummy set itemx=' + @item 
--
select @totalbefore=sum(rent+lt-discount) from billhist where cno=@cno
select @totalbeforeind=sum(isnull(rent,0)+isnull(lt,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0)+isnull(cess2,0) ) from #dummy
--print convert(varchar(20),@totalbeforeind) goto e_error
select @rentrate2=case when @head > 1 then rentrate22 else rentrate21 end  from room where rno=@rno
if @rentrate2=0 begin print 'Bill.No. ' + ltrim(str(@bno)) + 'New Room Rent is Zero' goto e_error end
/* This was altered on 29-07-2007 ....To be tracked with different hotels */
exec spltp2 @rentrate2,@cod,@lt out,1,@rno,@rentrate2,@head,0,1,0,@lrate out 

select @trno=round(cast(rent+lt-discount as int )/cast(@rentrate2+@lt as int) ,0)  from #dummy where rent+lt-discount = (select max(rent+lt-discount) from #dummy)
select @erno=count(*) from addroom where gno=@gno and rno<>@rno
set @erno=isnull(@erno,0)
set @erno=@erno+1
set @diff=@trno-@erno
--print str(@trno) + ' ' + str(@erno) goto e_error
---select @trno=round(cast(rent+lt-discount as int )/cast(@rentrate2 as int) ,0)  from #dummy where rent+lt-discount = (select max(rent+lt-discount) from #dummy)
update billhist set orent=rent where gno=@gno
update #dummy set rent=@rentrate2*(@trno) where rent<>0
update #dummy set lt=0
update #dummy set discount=0
if exists(select count(*) from guest where gno=@gno and omale=0)  update guest set omale=male where gno=@gno
if @head<@diff+1
update guest set male=male + ((@diff+1) - @head) where gno=@gno
select @head=male+female from guest where gno=@gno
declare @totroom int
set @totroom=@trno

declare tc1 cursor for select rent,lt,taxable,ctrlno,discount,txndt,st,cess,cess2 from #dummy
open tc1
fetch next from tc1 into @rent,@lt,@taxable,@ctrlno,@discount,@txndt,@st,@cess,@cess2
while @@fetch_status=0 begin
set @amt=@rent
exec spltp2 @amt,@cod,@lt out,@totroom,@rno,@rent,@head,@discount,1 
update #dummy set lt=isnull(@lt,0) where current of tc1

--print convert(varchar(20),@lt) + ' ' + convert(varchar(20),@rent) goto e_error

---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
declare @amttemp money
if @stcal=0   -- added on 05 nov 2013
	if @rent<>0
	begin
		select top 1  @stontariff=ontariff from sttable where (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		if upper(@stontariff)='YES'
			begin
				declare @tariff money
				--set @tariff=    (select case when @head=1 then rent1 else rent2 end  from room where rno=@crno)
				exec findrent @renttype,@rno,@head,@txndt,0,@tariff out
				set @amttemp=case when @rent/@totroom > @tariff then @rent/@totroom else @tariff end
			end
		else
			set @amttemp=(@rent-@discount)/@totroom
			set @strate = 0

set @st=0

		if exists(select * from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)))
		select @strate=rate,@stdiscount=discount,@stlessamt=lamt,@cessrate=cess from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		
		set @strate = isnull(@strate,0)
		set @stdiscount = isnull(@stdiscount,0)
		set @stlessamt=isnull(@stlessamt,0)
		set @cessrate = isnull(@cessrate,0)

		set @amttemp = @amttemp - @stlessamt
		set @st=0
		set @cess=0
		set @cess2=0

		--if @amttemp>0 
		--	if @strate<>0
		--		set @st = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@strate/100)) as decimal(12,2))
		--	else
		--		set @st=0
		
		--if @st > 0
		--	if @cessrate > 0 
		--		set @cess = @st * @cessrate / 100

        exec GetST @txndt,@rno ,@amttemp,0,@st out,@cess out,1,@cess2 out


/*		set @sql= 'update ' + @temptable + ' set st = ' + convert(varchar(20),@st)  +'  where current of cur_temp'
		exec(@sql)
		
		set @sql= 'update ' + @temptable + ' set cess = ' + convert(varchar(20),@cess)  +'  where current of cur_temp'
		exec(@sql)

*/

		set @st = @st * @totroom
		set @cess = @cess * @totroom
		set @cess2 = @cess2 * @totroom

		update #dummy set st=isnull(@st,0),cess=isnull(@cess,0),cess2=isnull(@cess2,0)  where current of tc1


	end

---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@



update billhist set lrate=@lrate,rent=isnull(@rent,0),lt=isnull(@lt,0),discount=isnull(@discount,0),st=@st,cess=@cess,cess2=@cess2,total=isnull(@rent,0)+isnull(@lt,0)+sc+ ISNULL(@st,0)+ISNULL(@CESS,0)+ISNULL(@CESS2,0)+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-isnull(@discount,0) where ctrlno=@ctrlno
if @@error <>0 goto e_error
fetch next from tc1 into @rent,@lt,@taxable ,@ctrlno,@discount,@txndt,@st,@cess,@cess2
end
close tc1
deallocate tc1


select @lt=sum(lt) from #dummy
declare @roomtype char(50)
select @roomtype = roomtype from room where rno=@rno
if @diff > 0 begin
	if exists(select RNo from room where roomtype=@roomtype and rno not in (select rno from addroom where gno=@gno union select rno from billhist where (datediff(d,'1/1/90',txndt) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod)) and rent <> 0 and rno is not null))  begin
		select RNo  into #dummy1 from room where roomtype=@roomtype and rno not in (select rno from addroom where gno in (select gno from guest where (datediff(d,'1/1/90',ci_date) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod))  and (datediff(d,'1/1/90',co_date) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod)))                union select rno from billhist where (datediff(d,'1/1/90',txndt) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod)) and rent <> 0 and rno is not null)
		declare tc cursor for select rno from #dummy1
		select @cnt=count(*) from #dummy1
		open tc 
		fetch next from tc into @rno  
		while @@fetch_status=0 begin
			insert into addroom (gno,rno) values (@gno,@rno)
			set @diff=@diff-1
			if @diff= 0  break           
			fetch next from tc into @rno  
		end
		close tc
		deallocate tc
		drop table #dummy1
	end
end  
if @diff > 0 begin
	select @cnt1=cast(count(*) as int) from room where roomtype=@roomtype and rno not in (select rno from addroom where gno=@gno union select rno from room where rno=@orno)
	declare tc2 cursor for select rno from room where roomtype=@roomtype and  rno not in  (select rno from addroom where gno=@gno ) and rno<>@orno
	open tc2
	fetch next from tc2 into @rno
	while @@fetch_status=0 begin
		if @diff=0 break
		insert into addroom (rno,gno) values (@rno,@gno)
		set @diff=@diff-1
		fetch next from tc2 into @rno
	end
	close tc2
	deallocate tc2
end
declare @grno numeric
declare @diffb money
declare @fy int
exec getfy @cod,1,@fy out
if @fy=0 goto e_error
if @@error <> 0 goto e_error
select @grno=grno from billhist1 where cno=@cno
set @rent=0
set @lt=0
select @rent=sum(isnull(rent,0)),@lt=sum(isnull(lt,0)),@st=sum(isnull(st,0)),@cess=sum(isnull(cess,0)),@cess2=sum(isnull(cess2,0)),@discount=sum(isnull(discount,0))  from #dummy
if @rent+@lt+@st+@cess+@cess2<@totalbeforeind 
	begin
    set @foodbill1 = @totalbeforeind-(@rent+@lt+@st+@cess+@cess2-@discount)
    ---print convert(varchar(20),ISNULL(@foodbill1,0)) + ' ' + convert(varchar(20),@totalbeforeind,0) + ' ' + convert(varchar(20),(@rent+@lt+@st+@cess)) + ' ' + convert(varchar(20),@rent)  + ' ' + convert(varchar(20),@lt)  + ' ' + convert(varchar(20),@st)  + ' ' + convert(varchar(20),@cess) goto e_error
	declare @olddata money
	declare @newdata money
	declare @famt money
	declare @amt1 money
	declare d1 cursor for select txndt,(rent+lt+st+cess-discount) newrent,(rentlt+ltlt+stlt+cesslt-discountlt) oldrent from #dummy
	open d1
	fetch next from d1 into @txndt,@newdata,@olddata
    while @@fetch_status=0
		begin
		if @olddata > @newdata 
			begin
				set @amt1 = @olddata-@newdata
				if @amt1 > 25
					begin
					EXEC dbo.pbpk1bill @amt1,@txndt,@gno,@famt out
					if @@error <> 0 goto e_error    
					--print convert(varchar(20),@amt1) + ' ' + convert(varchar(20),@famt) + convert(varchar(20),@foodbill1)
print convert(varchar(20),@txndt) + convert(varchar(20),@foodbill1) + ' ' + convert(varchar(20),@famt)
					set @foodbill1=@foodbill1-@famt
print convert(varchar(20),@foodbill1)					if @foodbill1 < 0 
					print convert(varchar(20),@foodbill1)  +' ' + convert(varchar(20),@amt1) + ' ' + convert(varchar(20),@txndt) + ' ' + str(@gno) + ' ' + convert(varchar(20),@olddata) --+ ' ' + convert(varchar(20),@newdata) goto e_error 
					end
			end
		fetch next from d1 into @txndt,@newdata,@olddata
		end
	close d1
	deallocate d1

 	update billhist1 set foodbill=foodbill + @foodbill1 where cno=@cno
	if @@error <> 0 begin print 'Food Bill amount updation in Bill not effected' goto e_error end
	end

if @diffb > 0  
begin
	update billhist1 set spldiscount=spldiscount+@diffb where cno=@cno
end
else
	if @rent+@lt+@st+@cess+@cess2 > @totalbeforeind begin print ltrim(str(@bno) )+ ' '+ convert(varchar(20),@cod,103)  + ' - There may be variation in Rent during the stay. Total  of Rent and LT before is ' + convert(varchar(20),@totalbefore) + '  where as New Rent ' + convert(varchar(20),@rent) + ' +  LT '+convert(varchar(20),@lt)  + ' is ' + convert(varchar(20),@rent+@lt) + ' - Process Aborted'  goto e_error end
exec PbVC11 @cno
commit transaction
return
e_error:
rollback transaction
raiserror('error',16,-1)
return



GO
/****** Object:  StoredProcedure [dbo].[pbpma1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
CREATE PROCEDURE [dbo].[pbpma1] @Gno numeric 
/* with encryption */
AS
--print 'periya'
declare @cod datetime
declare @cid datetime
declare @sql varchar(5000)
declare @Cno numeric
declare @rno numeric
declare @orno numeric
declare @trno int
declare @erno int
declare @diff int
declare @cnt numeric
declare @cnt1 int
declare @rent money
declare @lt money
declare @taxable money
declare @amt money
declare @x numeric
declare @ctrlno numeric
declare @totalbefore money
declare @discount money
declare @head int
declare @brno numeric
begin transaction
set @trno=0
set @erno=0
select @brno=brno,@rno=rno,@cod=co_date,@cid=ci_date,@head=male+female from guest where gno=@gno
select @cno=cno  from billhist1 where brno = @brno and company=1 and split in ('N','  ')
set @orno=@rno
select * into #dummy from billhist where gno=@gno
select @totalbefore=sum(rent+lt) from #dummy
select @trno=round(cast(rent+lt+taxable-discount as int )/499,0) + case when (cast(rent+lt+taxable-discount as int )%499) > 0 then 1 else 0 end  from #dummy where rent+lt+taxable-discount = (select max(rent+lt+taxable-discount) from #dummy)
select @erno=count(*) from addroom where gno=@gno
set @erno=@erno+1
set @diff=@trno-@erno
if @diff=0 begin Print  'No Difference Arrvied' goto e_error end
update billhist set orent=rent where cno=@cno
update #dummy set rent=rent+lt
update #dummy set lt=0
if exists(select count(*) from guest where gno=@gno and omale=0)  update guest set omale=male where gno=@gno
if @head<@diff+1
   update guest set male=male + ((@diff+1) - @head) where gno=@gno
declare tc1 cursor for select rent,lt,taxable,ctrlno,discount from #dummy
open tc1
fetch next from tc1 into @rent,@lt,@taxable,@ctrlno,@discount
while @@fetch_status=0 begin
set @amt=@rent+@lt+@taxable-@discount
exec spltp @amt,@cod,@lt out,@trno
update #dummy set lt=@lt where current of tc1
update billhist set rent=@rent,lt=@lt where ctrlno=@ctrlno
fetch next from tc1 into @rent,@lt,@taxable ,@ctrlno,@discount
end
close tc1
deallocate tc1
select @lt=sum(lt) from #dummy
if @lt<>0 begin print 'In-Appropirate Calculation' goto e_error end
if @diff > 0 begin
   if exists(select RNo from room where rno not in (select rno from addroom where gno=@gno union select rno from billhist where (datediff(d,'1/1/90',txndt) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod)) and rent <> 0 and rno is not null))  begin
     select RNo  into #dummy1 from room where rno not in (select rno from addroom where gno in (select gno from guest where (datediff(d,'1/1/90',ci_date) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod))  and (datediff(d,'1/1/90',co_date) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod)))                union select rno from billhist where (datediff(d,'1/1/90',txndt) between datediff(d,'1/1/90',@cid) and datediff(d,'1/1/90',@cod)) and rent <> 0 and rno is not null)
     declare tc cursor for select rno from #dummy1
     select @cnt=count(*) from #dummy1
     open tc 
     fetch next from tc into @rno  
     while @@fetch_status=0 begin
        insert into addroom (gno,rno) values (@gno,@rno)
           set @diff=@diff-1
           if @diff= 0  break           
           fetch next from tc into @rno  
      end 
      close tc
      deallocate tc
      drop table #dummy1
   end
end  
--print 'P.......' + stR(@diff)
--goto e_error
if @diff > 0 begin
   select @cnt1=cast(count(*) as int) from room where rno not in (select rno from addroom where gno=@gno union select rno from room where rno=@orno)
   set @cnt1=convert(int,rand()*1000)%@cnt1
   set @x = convert(int,rand()*1000)%@cnt1 --0 earlier it was zero
   declare tc2 cursor for select rno from room where rno not in  (select rno from addroom where gno=@gno ) and rno<>@orno
   open tc2
   fetch next from tc2 into @rno
   while @@fetch_status=0 begin
           if @diff=0 break
--           if @x>=@cnt1 
              insert into addroom (rno,gno) values (@rno,@gno)
           set @diff=@diff-1
           set @x=@x+1
           fetch next from tc2 into @rno
   end
   close tc2
   deallocate tc2
end
/*
declare @grno numeric
select @grno=grno from billhist1 where cno=@cno
set @rent=0
set @lt=0
select @rent=sum(rent),@lt=sum(lt) from #dummy
if @rent+@lt<>@totalbefore begin Print 'Difference in change' goto e_error end
update vouchers set amt=@rent where company=1 and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@cod) and grno = @grno and actnum = 15 and amttyp='C'
update vouchers set amt=@lt  where company=1 and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@cod) and grno = @grno and actnum = 47 and amttyp='C'
*/
--print 'periya' + str(@cno)
exec pbvc @cno,@cno
commit transaction
return
e_error:
rollback transaction
raiserror('error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbpma2]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
CREATE PROCEDURE [dbo].[pbpma2] @gno numeric 
/* with encryption */
AS
--print 'periya'
declare @cod datetime
declare @cid datetime
declare @sql varchar(5000)
declare @cno numeric
declare @rno numeric
declare @trno int
declare @erno int
declare @diff int
declare @cnt numeric
declare @cnt1 int
declare @rent money
declare @lt money
declare @taxable money
declare @amt money
declare @x numeric
declare @ctrlno numeric
declare @totalbefore money
declare @discount money
declare @brno numeric
begin transaction
select @brno=brno,@rno=rno,@cod=co_date,@cid=ci_date from guest where gno=@gno
select @cno=cno from billhist1 where cno = @cno
select * into #dummy from billhist where cno=@cno
if exists(select count(*) from guest where omale<>0 and gno=@gno)
  update guest set male=omale where gno=@gno
update #dummy set lt=lt+(rent-orent)
update #dummy set rent=orent
declare tc1 cursor for select rent,lt,taxable,ctrlno,discount from #dummy
open tc1
fetch next from tc1 into @rent,@lt,@taxable,@ctrlno,@discount
while @@fetch_status=0 begin
update billhist set rent=@rent,lt=@lt where ctrlno=@ctrlno
fetch next from tc1 into @rent,@lt,@taxable ,@ctrlno,@discount
end
close tc1
deallocate tc1
delete from addroom where gno=@gno
declare @grno numeric
select @grno=grno from billhist1 where cno=@cno
set @rent=0
set @lt=0
select @rent=sum(rent),@lt=sum(lt) from #dummy
update vouchers set amt=@rent where company=1 and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@cod) and grno = @grno and actnum = 15 and amttyp='C'
update vouchers set amt=@lt  where company=1 and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@cod) and grno = @grno and actnum = 47 and amttyp='C'
commit transaction
return
e_error:
rollback transaction
raiserror('error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbpp0503]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:47 AM ******/
CREATE PROCEDURE [dbo].[pbpp0503] 
@M_CNO INT,@N_MOP INT, @N_GNO_COMPCODE_CGPNO INT ,@NCRNAME VARCHAR(50),@NCRNUM VARCHAR(20),@NCRTYPE VARCHAR(20), @COMPELSPLIT CHAR(1)='X',@userno int=0,@checkuser int = 0, @rundelcout int = 0,@checkpw int=133,@checkpw1 int=139
/* with encryption */
AS
DECLARE @M_SALE MONEY
DECLARE @M_YEAR INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_GRNO INT
DECLARE @M_CO1GRNO INT
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_PARTIC VARCHAR(200)
DECLARE @M_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @SQL VARCHAR(500)
DECLARE @M_DETAIL VARCHAR(200)
DECLARE @M_DACTNO INT
DECLARE @M_AMT MONEY
--DECLARE @M_BNO INT
DECLARE @M_SNO INT
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT 
DECLARE @M_RNO INT
DECLARE @M_CO1FY INT
DECLARE @M_OUTLET INT
DECLARE @M_DAMT MONEY
DECLARE @M_DRATE MONEY
DECLARE @M_GNO_COMPCODE_CGPNO INT
DECLARE @M_CD_NUM VARCHAR(20)
DECLARE @M_CD_TYPE VARCHAR(20)
DECLARE @M_TXNDT DATETIME
DECLARE @M_MOP INT
DECLARE @CRNUM VARCHAR(20)
DECLARE @CRNAME VARCHAR(50)
DECLARE @CRTYPE VARCHAR(20)
DECLARE @FF_VOU CHAR(1)
DECLARE @OLDSPLIT CHAR(1)
declare @transact int
declare @finalbillamt1 money
declare @finalbillamt2 money
declare @billgno int
declare @billoutlet int
declare @oldpayment varchar(20)
declare @newpayment varchar(20)
declare @orno int
declare @lux char(1)
declare  @osname varchar(20)
declare  @nsname varchar(20)
declare @scamt money
declare @stamt money
declare @gstin nvarchar(15) 
set @transact=0
set @finalbillamt1 = 0
set @finalbillamt2 = 0
set @billoutlet = 0
SET @OLDSPLIT=' '
set @billgno=0
BEGIN TRANSACTION
SELECT @M_DRATE=DRATE,@M_TXNDT=TXNDT,@M_OUTLET= OUTLET,@M_GNO_COMPCODE_CGPNO  =GNO,
@M_MOP=PMODE,@M_SNO =SNO,@M_SPLIT=SPLIT,@M_SELFDEAL=SELFDEAL,@M_DAMT=DAMT,
@scamt=scamt,@stamt=stamt,
@CRNAME=CRNAME,@CRNUM=CRNUM,@CRTYPE=CRTYPE,@M_RNO=RNO,@lux=lux,@gstin=gstin 
FROM PHONE WHERE CNO = @M_CNO
if exists (select * from guest_view1 where gno=@N_GNO_COMPCODE_CGPNO and datediff(d,'1/1/80',case when datepart(hh,ci_date) < 7 then ci_date-1 else ci_date end) > datediff(d,'1/1/80', @m_txndt)) begin print 'Inconsistant Txn.Date with Check-In Date'  goto e_error end

if not exists(select * from servicepermission where userno=@userno and serviceno=@checkpw and outlet=@m_outlet and outlettype=2 and permission='Y')
begin
print 'You ' + str(@userno) + ' are not permitted to Change Mode of Payment for the Bills of this Outlet - ' + str(@m_outlet) + ' ' + STR(@checkpw)
goto e_error
end

set @orno = @m_rno
select @oldpayment=name from restpmode where pmode=@m_mop
set @billoutlet = @m_outlet
SET @OLDSPLIT=@m_SPLIT
if @oldsplit='C' set @oldsplit='F'
set @osname=case when @oldsplit='N' then 'Main' when @oldsplit='Y' then 'Split' else 'Complementary' end
SELECT @M_SALE=SUM(AMOUNT) FROM PHHIST WHERE CNO=@M_CNO
SELECT @M_COMPANY=COMPANY FROM PHOUTLET WHERE OUTLET=@M_OUTLET
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
SELECT @M_OLNAME=[NAME],@M_ACTNUM=GL_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END,
@M_SELFDEAL=SELFDEAL,@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK ,
@FF_VOU=FREEFOODVOUCHING
FROM PHOUTLET  WHERE OUTLET = @M_OUTLET
SELECT @M_COMPANY=COMPANY FROM PHOUTLET WHERE OUTLET=@M_OUTLET
--PRINT @M_TXNDT1
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
set @finalbillamt1 = (@M_SALE -@M_DAMT + @scamt+@stamt)
set @billgno = @m_gno_compcode_cgpno
if @m_mop=2 
	begin
	set @transact = 1
	if exists (select * from room where gno=@billgno and hostid <> '' )  begin print 'This room is under checkout operations...modifications not allowed' goto e_error end
	end

if exists(select * from guest where gno=@m_gno_compcode_cgpno and co_date is not null)
if not exists(select * from servicepermission where userno=@userno and serviceno=@checkpw1 and outlet=@m_outlet and outlettype=2 and permission='Y')
begin
print 'You are not permitted to Change Mode of Payment for the Bills of this Outlet after checkout'
goto e_error
end


exec servicebdeleting @m_cno
if @@error <> 0 goto e_error
SEMIFINAL:
IF @COMPELSPLIT<>'X'  SET @M_SPLIT = @COMPELSPLIT
SELECT @M_MOP = @N_MOP
SELECT @CRNAME = @NCRNAME
SELECT @CRNUM = @NCRNUM
SELECT @CRTYPE = @NCRTYPE
SELECT @M_GNO_COMPCODE_CGPNO = @N_GNO_COMPCODE_CGPNO
select @m_rno=0
IF @COMPELSPLIT='X'
begin
set @m_split = 'N'
if @m_mop = 2 
begin
 if @CRNAME='YES'
    set @m_split='C'
 else
                    if @m_company = 1
                           begin
  if exists(select * from gsplit where gno=@M_GNO_COMPCODE_CGPNO and outlet = @m_outlet and chtype =2)
  set @m_split = 'Y'
                           end 
end
end
if @m_mop = 2
begin 
  if exists (select * from room where gno=@M_GNO_COMPCODE_CGPNO and hostid <> '' )  
                 begin print 'This guest is under checkout process' goto E_ERROR end
 select @m_rno=rno from guest where gno=@m_gno_compcode_cgpno
end
SELECT @M_OLNAME=[NAME],@M_ACTNUM=GL_ACTNUM,@FF_VOU=FREEFOODVOUCHING,
@M_DC_ACTNUM=DC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_SELFDEAL=SELFDEAL,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK,@M_CO1GRNO=CO1GRNO
FROM PHOUTLET  WHERE OUTLET = @M_OUTLET
IF @M_SPLIT='C' AND @FF_VOU='N'  SELECT @M_SPLIT='F'
set @nsname=case when @m_split='N' then 'Main' when @m_split='Y' then 'Split' else 'Complementary' end

if @m_mop=2 select @gstin=case when @m_split<>'Y' then gstin else gstinsplit end from guest where gno=@m_gno_compcode_cgpno

UPDATE PHONE SET PMODE=@M_MOP,
CRNAME=@CRNAME,
CRNUM=@CRNUM,
CRTYPE=@CRTYPE,
GNO=@M_GNO_COMPCODE_CGPNO,
SPLIT=@M_SPLIT,
rno=@m_rno,
gstin=@gstin
WHERE CNO = @M_CNO
IF @@ERROR<>0 GOTO E_ERROR
if @m_mop=2
set @finalbillamt2 = (@M_SALE -@M_DAMT + @scamt+@stamt)

exec servicebvouching @m_cno
if @@error <> 0 goto e_error
final:
if @transact=1 and @rundelcout=0
	begin
	IF @billgno=@M_GNO_COMPCODE_CGPNO
		begin
		set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalbillamt1) + ',0' +
		+ ',' + str(@m_company) + ',' + str(200+@billoutlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @oldsplit + ''',0,' + convert(varchar(20),@finalbillamt2) + ',''' + @m_split + ''','+

		str(@userno)+','''+@lux+''',' + str(@checkuser)
		exec (@sql)
		if @@error <> 0  goto e_error
		end
	else
		begin
		set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalbillamt1) + ',0,' + str(@m_company) + ',' + str(200+@billoutlet)
		+ ',''' + convert(varchar(20),@m_txndt) + ''',''' + @oldsplit + ''',0,' + convert(varchar(20),@finalbillamt2) + ',''X'','+str(@userno)
		+','''+@lux+''','+str(@checkuser)
		exec (@sql)
		if @@error <> 0  goto e_error
		end
	end
if @checkuser=0 
	begin
	select @newpayment=name from restpmode where pmode=@m_mop
	declare @usernm varchar(50)
	select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
	insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@m_olname))+ ' Bill No ' + ltrim(str(@m_sno)) + ' Dated ' + convert(varchar(20),@m_txndt,3) + ' Amt ' + ltrim(convert(varchar(20), @finalbillamt1)) +  ' MOP from '+ltrim(rtrim(@oldpayment)) +'-'+ @osname + ' '+  ltrim(str(@orno))+ ' to ' + ltrim(rtrim(@newpayment)) + '- '+ @nsname + ' ' +   ltrim(str(@m_rno))+ ' changed' ,7,@billgno)
	end
COMMIT TRANSACTION
RETURN 0
E_ERROR:
WHILE @@TRANCOUNT > 0 
BEGIN
ROLLBACK TRANSACTION
END
raiserror ('CHANGING MODE OF PAYMENT ABSTRACT SERVICE VOUCHER failed in EDITPH111 ',16,-1)
RETURN 1




GO
/****** Object:  StoredProcedure [dbo].[pbpp1106]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo    Script Date: 1/20/03 11:51:47 AM ******/
CREATE PROCEDURE [dbo].[pbpp1106] @TEMPTABLE VARCHAR(50),@M_CNO INT,@n_drate money, @n_damt money,@userno int=0,@nscrate money=0,@nscamt money=0,@nstrate money=0,@nstamt money=0,@gstin varchar(50) =''
/* with encryption */
AS
DECLARE @M_SALE MONEY
DECLARE @M_YEAR INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_GRNO INT
DECLARE @M_CO1GRNO INT
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_PARTIC VARCHAR(200)
DECLARE @M_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @SQL VARCHAR(500)
DECLARE @M_DETAIL VARCHAR(200)
DECLARE @M_DACTNO INT
DECLARE @M_AMT MONEY
DECLARE @M_BNO INT
DECLARE @M_SNO INT
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT 
DECLARE @M_RNO INT
DECLARE @M_CO1FY INT
DECLARE @M_OUTLET INT
DECLARE @M_DAMT MONEY
DECLARE @M_DRATE MONEY
DECLARE @M_GNO_COMPCODE_CGPNO INT
DECLARE @M_CD_NUM VARCHAR(20)
DECLARE @M_CD_TYPE VARCHAR(20)
DECLARE @M_TXNDT DATETIME
DECLARE @M_MOP INT
DECLARE @CRNUM VARCHAR(20)
DECLARE @CRNAME VARCHAR(50)
DECLARE @CRTYPE VARCHAR(20)
declare @transact int
declare @finalbillamt1 money
declare @finalbillamt2 money
declare @billgno int
declare @billoutlet int
declare @usernm  varchar(30)
declare @oldamt money
declare @newamt money
declare @lux char(1)
declare @oscrate money
declare @oscamt money
declare @ostrate money
declare @ostamt money
set @transact=0
set @finalbillamt1 = 0
set @finalbillamt2 = 0
set @billoutlet = 0
set @billgno=0
set @m_bno = @m_cno
SELECT @M_DRATE=DRATE,@M_TXNDT=TXNDT,@M_OUTLET= OUTLET,@M_GNO_COMPCODE_CGPNO  =GNO,
@M_MOP=PMODE,@M_SNO =SNO,@M_SPLIT=SPLIT,@M_SELFDEAL=SELFDEAL,@M_DAMT=DAMT,
@CRNAME=CRNAME,@CRNUM=CRNUM,@CRTYPE=CRTYPE,@M_RNO=RNO,@lux=lux,@oscrate=scrate,@oscamt=scamt
,@ostrate=strate,@ostamt=stamt
FROM PHONE WHERE CNO = @M_CNO

if not exists(select * from servicepermission where userno=@userno and serviceno=132 and outlet=@m_outlet and outlettype=2 and permission='Y')
begin
print 'You are not permitted to Edit the Bills of this Outlet'
goto e_error
end


set @billoutlet = @m_outlet
SELECT @M_SALE=SUM(AMOUNT) FROM PHHIST WHERE CNO=@M_CNO



SELECT @M_COMPANY=COMPANY FROM PHOUTLET WHERE OUTLET=@M_OUTLET
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
SELECT @M_OLNAME=[NAME],@M_ACTNUM=GL_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END,
@M_SELFDEAL=SELFDEAL,@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK 
FROM PHOUTLET  WHERE OUTLET = @M_OUTLET
SELECT @M_COMPANY=COMPANY FROM PHOUTLET WHERE OUTLET=@M_OUTLET
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
SELECT @M_DACTNO = 
      CASE 
                  WHEN @M_MOP = 1 THEN 1           --- CASH SALES
                  WHEN @M_MOP = 2 THEN               -- ROOM SERVICE
	                        CASE WHEN @M_COMPANY = 1 THEN 24 
							ELSE  
									  CASE
									  WHEN @M_SELFDEAL='Y' THEN  24   ELSE @M_HNO_MYBOOK
									  END
							END
                  WHEN @M_MOP = 3 THEN 23  -- BILL RECEIVABLE
                  WHEN @M_MOP = 4 THEN @M_GNO_COMPCODE_CGPNO   -- GL A/C
                  WHEN @M_MOP = 5 THEN 22   -- CREDIT CARD 
                  WHEN @M_MOP = 6 THEN 0   -- COMPLEMENTARTY No vouching 
       END

BEGIN TRANSACTION
 set @finalbillamt1 = (@M_SALE -@M_DAMT + @oscamt+@ostamt)
 set @oldamt = (@M_SALE -@M_DAMT + @oscamt+@ostamt)

if @m_mop=2 
	begin
	set @transact = 1

	set @billgno = @m_gno_compcode_cgpno
	if exists (select * from room where gno=@billgno and hostid <> '' ) begin print 'This room is under checkout operations..modifications not allowed'   goto e_error end
	if exists (select * from guest_view1 where gno=@billgno and datediff(d,'1/1/80',case when datepart(hh,ci_date) < 7 then ci_date-1 else ci_date end) > datediff(d,'1/1/80', @m_txndt)) begin print 'Inconsistant Txn.Date with Check-In Date'  goto e_error end
	end

if exists(select * from setting where g_finaccount=0) goto semifinal
if @m_mop=6 GOTO SEMIFINAL
if @m_mop=2 and @m_split='F' goto semifinal


exec servicebdeleting @m_cno
if @@error <> 0 goto e_error

SEMIFINAL:
DELETE FROM PHHIST WHERE CNO=@M_CNO
     IF @@ERROR <> 0 GOTO E_ERROR
SELECT @M_SALE = 0
SELECT @M_DRATE = @N_DRATE
SELECT @M_DAMT = @N_DAMT
SELECT @M_OLNAME=[NAME],@M_ACTNUM=GL_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_SELFDEAL=SELFDEAL,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK,@M_CO1GRNO=CO1GRNO
FROM PHOUTLET  WHERE OUTLET = @M_OUTLET
UPDATE PHONE SET DRATE = @M_DRATE,DAMT=@M_DAMT,scrate=@nscrate,scamt=@nscamt,strate=@nstrate,stamt=@nstamt,gstin=@gstin
WHERE CNO = @M_CNO
IF @@ERROR<>0 GOTO E_ERROR
SET @SQL = 'DECLARE UPDTKIT CURSOR FOR SELECT DETAIL , AMOUNT FROM '+  @TEMPTABLE 
EXEC (@SQL)
OPEN UPDTKIT
FETCH NEXT FROM UPDTKIT INTO @M_DETAIL,@M_AMT
WHILE  @@FETCH_STATUS=0
 BEGIN
   SELECT @M_SALE = @M_SALE + @M_AMT
    INSERT PHHIST (CNO,PARTIC,AMOUNT) VALUES
    (@M_CNO,@M_DETAIL , @M_AMT)
    IF @@ERROR<>0 GOTO E_ERROR 
    FETCH NEXT FROM UPDTKIT INTO @M_DETAIL,@M_AMT
END
DEALLOCATE UPDTKIT
--print convert(varchar(20),@nstamt) goto e_error
set @finalbillamt2 =( @M_SALE -@M_DAMT + @nscamt+@nstamt)
set @newamt = (@M_SALE -@M_DAMT + @NSCAMT+@nstamt)
update phone set finalamt=@newamt where CNO=@m_cno
exec servicebvouching @m_cno
if @@error <> 0 goto e_error
--print convert(varchar(20),@newamt) +  convert(varchar(20),@oldamt)
---goto e_error
if @transact=1
   begin
   set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalbillamt1) + ',' + convert(varchar(20),@finalbillamt2) + ',' + str(@m_company) + ',' + str(200+@billoutlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,0,''X'','+str(@userno)+','''+@lux+''''
   exec (@sql)
   if @@error <> 0  goto e_error
   end
--select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@M_olname))+ ' Bill No ' + ltrim(str(@m_sno)) + ' Dated ' + convert(varchar(20),@m_txndt,6) + ' Edited - Old amt = '+ convert(varchar(20), @oldamt) + ' ,  New Amount = '+ convert(varchar(20),@newamt),5,@billgno)
FINAL:
COMMIT TRANSACTION
GOTO FINAL1
RETURN 0
E_ERROR:
WHILE @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION 
END 
raiserror ('EDITING OF ABSTRACT SERVICE VOUCHER failed in EDITPH1112 ',16,-1)
GOTO FINAL1
RETURN 1
FINAL1:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)




GO
/****** Object:  StoredProcedure [dbo].[pbpp1508]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:47 AM ******/
CREATE PROCEDURE [dbo].[pbpp1508] @M_CNO INT,@usernm varchar(50)='SYS',@userno numeric=0,@executedelcout int=0
/* with encryption */
AS
DECLARE @M_SALE MONEY
DECLARE @M_YEAR INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_GRNO INT
DECLARE @M_CO1GRNO INT
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_PARTIC VARCHAR(200)
DECLARE @M_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @SQL VARCHAR(5000)
DECLARE @M_DETAIL VARCHAR(200)
DECLARE @M_DACTNO INT
DECLARE @M_AMT MONEY
DECLARE @M_BNO INT
DECLARE @M_SNO INT
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT 
DECLARE @M_RNO INT
DECLARE @M_CO1FY INT
DECLARE @M_OUTLET INT
DECLARE @M_DAMT MONEY
DECLARE @M_DRATE MONEY
DECLARE @M_GNO_COMPCODE_CGPNO INT
DECLARE @M_CD_NUM VARCHAR(20)
DECLARE @M_CD_TYPE VARCHAR(20)
DECLARE @M_TXNDT DATETIME
DECLARE @M_MOP INT
declare @transact int
declare @finalbillamt1 money
declare @finalbillamt2 money
declare @billgno int
declare @billoutlet int
declare @payment varchar(50)
declare @lux char(1)
declare @scamt money
declare @stamt money
set @transact=0
set @finalbillamt1 = 0
set @finalbillamt2 = 0
set @billoutlet = 0
SELECT @M_DRATE=DRATE,@M_TXNDT=TXNDT,@M_OUTLET= OUTLET,@M_GNO_COMPCODE_CGPNO  =GNO,
@M_MOP=PMODE,@M_SNO =SNO,@M_SPLIT=SPLIT,@M_SELFDEAL=SELFDEAL,@M_DAMT=DAMT,@m_rno=rno,@lux=lux,@scamt=scamt,@stamt=stamt
FROM PHONE WHERE CNO = @M_CNO
set @billoutlet = @m_outlet
SELECT @M_SALE=SUM(AMOUNT) FROM PHHIST WHERE CNO=@M_CNO
SELECT @M_COMPANY=COMPANY FROM PHOUTLET WHERE OUTLET=@M_OUTLET
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
SELECT @M_OLNAME=[NAME],@M_ACTNUM=GL_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END,
@M_SELFDEAL=SELFDEAL,@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK 
FROM PHOUTLET  WHERE OUTLET = @M_OUTLET
SELECT @M_COMPANY=COMPANY FROM PHOUTLET WHERE OUTLET=@M_OUTLET
--PRINT @M_TXNDT1
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT

if @userno=0 
begin
print 'Unauthrised Acces..Deletion denied'
goto e_error
end
if not exists(select * from servicepermission where userno=@userno and serviceno=131 and outlet=@m_outlet and outlettype=2 and permission='Y')
begin
print 'You are not permitted to delete Bill of this Outlet'
goto e_error
end
if @m_mop=2 
  begin
 set @transact = 1
 set @finalbillamt1 = (@M_SALE -@M_DAMT + @scamt+@stamt)
 set @billgno = @m_gno_compcode_cgpno
  if exists (select * from room where gno=@billgno and hostid <> '' ) begin print 'This room is under checkout operation...modifications not allowed'  goto e_error end
  end
begin transaction
exec servicebdeleting @m_cno
if @@error <> 0 goto e_error
FINAL:
if @transact=1
if @executedelcout=0
   begin
   set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalbillamt1) + ',' + convert(varchar(20),@finalbillamt2) + ',' + str(@m_company) + ',' + str(200+@billoutlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,0,''X'','  + str(@userno)+','''+@lux+''''
   exec (@sql)
   if @@error <> 0  goto e_error
   end
if @executedelcout=0
	begin
	set  @payment = case when @m_mop= 1 then 'cash' when @m_mop=2 then 'Room Service ' + ltrim(str(@m_rno)) when @m_mop=3 then 'Bill Receivable' when @m_mop=4 then 'GL Account' when @m_mop=5 then 'Credit Card' when @m_mop=6 then 'Complementary' else 'Unkown' end
	--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
	insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@M_olname))+ ' Bill No ' + ltrim(str(@m_sno)) + ' Dated ' + convert(varchar(20),@m_txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@M_SALE-@M_DAMT)) +  ' Mode of payment '+ltrim(rtrim(@payment)) + ' Deleted ',6,@billgno)
	if @@error<>0 goto e_error
	end

DELETE FROM PHONE WHERE CNO=@M_CNO
     IF @@ERROR <> 0 GOTO E_ERROR
DELETE FROM PHHIST WHERE CNO=@M_CNO
     IF @@ERROR <> 0 GOTO E_ERROR
COMMIT TRANSACTION
RETURN 0
E_ERROR:
WHILE @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION 
END 
raiserror ('DELETION OF ABSTRACT SERVICE VOUCHER failed in DELPH ',16,-1)
RETURN 1




GO
/****** Object:  StoredProcedure [dbo].[pbpp1u]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:01 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:48 AM ******/
CREATE PROCEDURE [dbo].[pbpp1u]  @TEMPTABLE VARCHAR(50) ,@M_OUTLET INT, @M_TXNDT DATETIME,
@M_MOP INT, @M_DRATE MONEY,@M_GNO_COMPCODE_CGPNO  INT ,@CRNAME CHAR(50), @CRNUM CHAR(20),
@CRTYPE CHAR(20),@M_DAMT  MONEY,@OCNO INT OUT,@scrate money=0,@scamt money=0,@strate money=0,@stamt money =0,@exbedSPLIT int=0,
@gstin nvarchar(50)='',@gstinsplit nvarchar(15)=''
/* with encryption */
AS
DECLARE @M_SALE MONEY
DECLARE @M_YEAR INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_GRNO INT
DECLARE @M_CO1GRNO INT
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_PARTIC VARCHAR(200)
DECLARE @M_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @SQL VARCHAR(500)
DECLARE @M_DETAIL VARCHAR(200)
DECLARE @M_DACTNO INT
DECLARE @M_AMT MONEY
DECLARE @M_BNO INT
DECLARE @M_SNO INT
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT 
DECLARE @M_RNO INT
DECLARE @LUX CHAR(1)
DECLARE @M_CO1FY INT
DECLARE @FF_VOU CHAR(1)
declare @usernm varchar(50)
declare @errmsg varchar(1000)
set @errmsg='updation failed in updtkit 11'
--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

set @usernm = isnull(@usernm,'SYS')
SELECT @M_RNO = 0 
if @m_mop=2
if exists(select * from room where gno=@m_gno_compcode_cgpno and ltrim(checkout)<>'' and rtrim(ltrim(hostid)) <> '')
begin
set @errmsg = 'The selected Room is under Checkout process by some other....Billing rejected'
goto e_error
raiserror('Error',16,-1)
return
end
if @m_mop=2 
   if @m_gno_compcode_cgpno = 0
      begin
      print 'Error in Data Feeding ....Please check'
      goto e_error
   end
IF @M_MOP = 2 
IF NOT EXISTS (SELECT * FROM ROOM WHERE GNO = @M_GNO_COMPCODE_CGPNO) 
BEGIN
   --RAISERROR('ROOM IS VACATED DURING YOUR BILLING PROCESS',16,-1)
    --RETURN
     print 'Room is vaccated during your billing process'
    goto e_error
END    
IF @M_MOP=2
SELECT @M_RNO=RNO FROM ROOM  WHERE GNO=@M_GNO_COMPCODE_CGPNO
--IF @M_TXNDT < '1 APR 1950'
--SELECT @M_TXNDT = GETDATE()
SELECT @M_TXNDT1 = CONVERT(VARCHAR(20),@M_TXNDT)
SELECT @M_SALE  = 0
set @m_bofy = dbo.getbofy(@m_txndt)
set @m_year = datepart(yy,@m_bofy)
SELECT @M_COMPANY=COMPANY FROM PHOUTLET WHERE OUTLET=@M_OUTLET
--PRINT @M_TXNDT1
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
BEGIN TRANSACTION
UPDATE PHOUTLET SET SNO=CASE WHEN BILLDATE <> @M_BOFY THEN 1 ELSE SNO+1 END , BILLDATE= CASE  WHEN BILLDATE <> @M_BOFY THEN @M_BOFY ELSE BILLDATE END WHERE OUTLET = @M_OUTLET
IF @@ERROR<>0 GOTO E_ERROR
SELECT @M_OLNAME=[NAME],@M_SNO=SNO,@M_ACTNUM=GL_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_SELFDEAL=SELFDEAL,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK,@M_CO1GRNO=CO1GRNO,
@LUX=LUX,@FF_VOU=FREEFOODVOUCHING
FROM PHOUTLET  WHERE OUTLET = @M_OUTLET
set @m_split = 'N'
if @exbedSPLIT=1
	set @m_split='Y'
else
	if @m_mop = 2 
		begin
			if @CRNAME='YES'
				set @m_split='C'
			else
			--if @m_company = 1
				begin
				if exists(select * from gsplit where gno=@M_GNO_COMPCODE_CGPNO and outlet = @m_outlet and chtype =2)
				set @m_split = 'Y'
				end 
		end
--goto e_error
--if @m_mop=2 and @m_split='C'  and @ff_vou='N'  set @m_split='F'
IF @M_SPLIT='C' AND @FF_VOU='N'  SELECT @M_SPLIT='F'

if @m_split='Y' if @gstinsplit <> '' set @gstin=@gstinsplit 

INSERT PHONE (DRATE,TXNDT,OUTLET,GNO,PMODE,SNO,
SPLIT,SELFDEAL , DAMT,RNO,CRNAME,CRNUM,CRTYPE,LUX,
TXNTIME,usernm,scrate,scamt,strate,stamt,gstin) 

VALUES

(@M_DRATE,@M_TXNDT,@M_OUTLET,@M_GNO_COMPCODE_CGPNO,@M_MOP,@M_SNO,
@M_SPLIT,@M_SELFDEAL,@M_DAMT,@M_RNO,@CRNAME,@CRNUM,@CRTYPE,@LUX,
convert(varchar(5),getdate(),8),@usernm ,@scrate,@scamt,@strate,@stamt,@gstin)
IF @@ERROR<>0 GOTO E_ERROR

SELECT @OCNO=@@IDENTITY
SELECT @M_BNO=MAX(CNO) FROM PHONE WHERE OUTLET= @M_OUTLET 
SET @SQL = 'DECLARE UPDTKIT CURSOR FOR SELECT DETAIL , AMOUNT FROM  '+  @TEMPTABLE 
EXEC (@SQL)
OPEN UPDTKIT
FETCH NEXT FROM UPDTKIT INTO @M_DETAIL,@M_AMT
WHILE  @@FETCH_STATUS=0
 BEGIN

   SELECT @M_SALE = @M_SALE + @M_AMT
    INSERT PHHIST (CNO,PARTIC,AMOUNT) VALUES
    (@M_BNO,@M_DETAIL , @M_AMT)

    IF @@ERROR<>0 begin set @errmsg=@m_detail GOTO E_ERROR end
    FETCH NEXT FROM UPDTKIT INTO @M_DETAIL,@M_AMT
END
DEALLOCATE UPDTKIT

select @m_sale=sum(amount) from phhist where cno=@m_bno
declare @famt money
select @famt = @m_sale+scamt+stamt-damt from phone where cno=@m_bno 
---set @msg = convert(varchar(20),@famt) goto e_error

update phone set finalamt = @famt where cno=@m_bno  


exec servicebvouching @m_bno

--GOTO E_ERROR
FINAL:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
COMMIT TRANSACTION
RETURN 0
E_ERROR:
ROLLBACK TRANSACTION
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
raiserror (@errmsg,16,-1)
RETURN 1




GO
/****** Object:  StoredProcedure [dbo].[pbpp1u_co]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:01 PM ******/
/****** Object:  Stored Procedure dbo.    Script Date: 1/20/03 11:51:48 AM ******/
CREATE PROCEDURE [dbo].[pbpp1u_co]  @TEMPTABLE VARCHAR(50) ,@M_OUTLET INT, @M_TXNDT DATETIME,
@M_MOP INT, @M_DRATE MONEY,@M_GNO_COMPCODE_CGPNO  numeric ,@CRNAME CHAR(50), @CRNUM CHAR(20),
@CRTYPE CHAR(20),@M_DAMT  MONEY,@OCNO INT OUT,@scrate money=0,@scamt money = 0,@strate money=0,@stamt money = 0,
@gstin varchar(50)='',@gstinsplit nvarchar(15)=''
/* with encryption */
AS
declare @userno numeric
DECLARE @M_SALE MONEY
DECLARE @M_YEAR INT
DECLARE @M_COMPANY INT
DECLARE @M_FY INT
DECLARE @M_TXNDT1 VARCHAR(20)
DECLARE @M_BOFY DATETIME
DECLARE @M_SPLIT CHAR(1)
DECLARE @M_GRNO INT
DECLARE @M_CO1GRNO INT
DECLARE @M_OLNAME VARCHAR(50)
DECLARE @M_PARTIC VARCHAR(200)
DECLARE @M_ACTNUM INT
DECLARE @M_DC_ACTNUM INT
DECLARE @SQL VARCHAR(500)
DECLARE @M_DETAIL VARCHAR(200)
DECLARE @M_DACTNO INT
DECLARE @M_AMT MONEY
DECLARE @M_BNO INT
DECLARE @M_SNO INT
DECLARE @M_SELFDEAL CHAR(1)
DECLARE @M_HNO_MYBOOK INT
DECLARE @M_HNO_HTLBOOK INT 
DECLARE @M_RNO INT
DECLARE @LUX CHAR(1)
DECLARE @M_CO1FY INT
DECLARE @FF_VOU CHAR(1)
declare @gno numeric
declare @billgno numeric
declare @msg varchar(5000)
declare @usernm varchar(50) 
--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
set @usernm = isnull(@usernm,'SYS')

BEGIN TRANSACTION
set @billgno=@M_GNO_COMPCODE_CGPNO
set @msg= 'Error :'
set @gno=@billgno
SELECT @M_RNO = 0 
if exists (select * from guest where gno=@billgno and co_date = null)
begin
set @msg =  'This option is for only checkedout entries' goto e_error
end
if not exists(select * from hmsfolder where version_no = (select version_no from comuser where [host_id]=host_id()) ) 
  begin 
  set @msg = 'You are using a wrong version of the Softwre Installed...Please update the version (12) and try'
  goto e_error
end
set @gno = @M_GNO_COMPCODE_CGPNO
if not exists(select * from guest where gno=@gno and datediff(d,'1/1/90',@m_txndt) between datediff(d,'1/1/90',ci_Date-1) and datediff(d,'1/1/90',co_date))
begin
set @msg = 'The KOT Bill Date falls out of the stay period of the selected guest' goto e_error
end
SELECT @M_SPLIT = 'N'
IF @M_TXNDT < '1 APR 1950'
SELECT @M_TXNDT = GETDATE()
SELECT @M_TXNDT1 = CONVERT(VARCHAR(20),@M_TXNDT)
SELECT @M_SALE  = 0
IF DATEPART(mm,@M_TXNDT) >=  1 AND DATEPART(mm,@M_TXNDT) <= 3
     SET @M_YEAR = DATEPART(yy,@M_TXNDT)-1
ELSE
     SET @M_YEAR = DATEPART(yy,@M_TXNDT)
SELECT @M_BOFY = '1 APR ' + STR(@M_YEAR)
SELECT @M_COMPANY=COMPANY FROM PHOUTLET WHERE OUTLET=@M_OUTLET
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
UPDATE PHOUTLET SET SNO=CASE WHEN BILLDATE <> @M_BOFY THEN 1 ELSE SNO+1 END , BILLDATE= CASE  WHEN BILLDATE <> @M_BOFY THEN @M_BOFY ELSE BILLDATE END WHERE OUTLET = @M_OUTLET
IF @@ERROR<>0 GOTO E_ERROR
SELECT @M_OLNAME=[NAME],@M_SNO=SNO,@M_ACTNUM=GL_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_SELFDEAL=SELFDEAL,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK,@M_CO1GRNO=CO1GRNO,
@LUX=LUX,@FF_VOU=FREEFOODVOUCHING
FROM PHOUTLET  WHERE OUTLET = @M_OUTLET
set @m_split = 'N'
if @m_mop = 2 
begin
 if @CRNAME='YES'
  set @m_split='C'
 else
                           begin
  if exists(select * from gsplit where gno=@M_GNO_COMPCODE_CGPNO and outlet = @m_outlet and chtype =2)
  set @m_split = 'Y'
                           end 
end
IF @M_SPLIT='C' AND @FF_VOU='N'  SELECT @M_SPLIT='F'

if @m_mop=2  and @m_split='Y' select @gstin=gstinsplit from guest where gno=@M_GNO_COMPCODE_CGPNO


INSERT PHONE (DRATE,TXNDT,OUTLET,GNO,PMODE,SNO,SPLIT,SELFDEAL , DAMT,RNO,CRNAME,CRNUM,CRTYPE,LUX,TXNTIME,usernm,scrate,scamt,strate,stamt,gstin) VALUES
(@M_DRATE,@M_TXNDT,@M_OUTLET,@M_GNO_COMPCODE_CGPNO,@M_MOP,@M_SNO,
@M_SPLIT,@M_SELFDEAL,@M_DAMT,@M_RNO,@CRNAME,@CRNUM,@CRTYPE,@LUX,convert(varchar(5),getdate(),8),@usernm ,@scrate,@scamt ,@strate,@stamt,@gstin )
IF @@ERROR<>0 GOTO E_ERROR
SELECT @OCNO=@@IDENTITY
SELECT @M_BNO=MAX(CNO) FROM PHONE WHERE OUTLET= @M_OUTLET 
SET @SQL = 'DECLARE UPDTKIT CURSOR FOR SELECT DETAIL , AMOUNT FROM  '+  @TEMPTABLE 
EXEC (@SQL)
OPEN UPDTKIT
FETCH NEXT FROM UPDTKIT INTO @M_DETAIL,@M_AMT
WHILE  @@FETCH_STATUS=0
 BEGIN
   SELECT @M_SALE = @M_SALE + @M_AMT
    INSERT PHHIST (CNO,PARTIC,AMOUNT) VALUES
    (@M_BNO,@M_DETAIL , @M_AMT)
    IF @@ERROR<>0 GOTO E_ERROR 
    FETCH NEXT FROM UPDTKIT INTO @M_DETAIL,@M_AMT
END
DEALLOCATE UPDTKIT
SELECT @M_PARTIC = CASE
                                                   WHEN @M_MOP = 1  THEN  'TO CASH SALES ' + LTRIM(@M_OLNAME)
                                                   WHEN @M_MOP = 2  THEN  'TO ROOM SERVICE  SALES ' + LTRIM(@M_OLNAME)
                                                    WHEN @M_MOP = 3  THEN  'TO ' + RTRIM(@CRNAME) + '-B.NO ' + LTRIM(STR(@M_SNO)) + ' ' + @M_OLNAME
                                                    WHEN @M_MOP = 4  THEN  'TO ' + 'B.NO ' + LTRIM(STR(@M_SNO)) + ' ' + @M_OLNAME
                                                    WHEN @M_MOP = 5  THEN  'TO ' + RTRIM(@CRNAME) + ',' + RTRIM(@CRNUM) + ',' + RTRIM(@CRTYPE) + '-B.NO ' + LTRIM(STR(@M_SNO)) + ' ' + @M_OLNAME
                                                    WHEN @M_MOP = 6  THEN   'TO ' + RTRIM(@CRNAME) + '-B.NO ' + LTRIM(STR(@M_SNO)) + ' ' + @M_OLNAME
                                                    END
SELECT @M_DACTNO = 
                              CASE 
                                          WHEN @M_MOP = 1 THEN 1           --- CASH SALES
                                          WHEN @M_MOP = 2 THEN               -- ROOM SERVICE
                                                                                                   CASE WHEN @M_COMPANY = 1 THEN 24 
                                                                                                    ELSE  
                                                                                                                  CASE
                                                                                                                  WHEN @M_SELFDEAL='Y' THEN  24   ELSE @M_HNO_MYBOOK
                                                                                                                  END
                                                                                                     END
                                          WHEN @M_MOP = 3 THEN 23  -- BILL RECEIVABLE
                                          WHEN @M_MOP = 4 THEN @M_GNO_COMPCODE_CGPNO   -- GL A/C
                                          WHEN @M_MOP = 5 THEN 22   -- CREDIT CARD 
                                          WHEN @M_MOP = 6 THEN 0   -- COMPLEMENTARTY No vouching 
                               END
/* This portion is updated for non-finacial packages on 03 May 2016 Bala/Subbu Issue */ 
declare @famt money
declare @sale money 
select @sale=sum(amount) from phhist where cno=@m_bno
select @famt = @sale+scamt+stamt+damt from phone where cno=@m_bno 
update phone set finalamt = @famt where cno=@m_bno   --v 13.0.7 20 April 2012 @ kottaiyur
/***/

if exists(select * from setting where g_finaccount=0) goto final
if @m_mop=6 GOTO FINAL
if @m_mop=2 and @m_split='F' goto final
exec servicebvouching @m_bno
FINAL:
declare @finalbillamt1 money
declare @finalbillamt2 money
set @finalbillamt1=0
select @finalbillamt2=finalamt from phone where cno=@m_bno 
--set @finalbillamt2 = @M_SALE-@M_DAMT
--set @msg= 'Final Amt ' + convert(varchar(20),@finalbillamt2) + ' ' + str(@m_bno) goto e_error
   select @userno=userno from comuser where [host_id]=host_id() and sessionid=''

      set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalbillamt1) + ',0' 
      + ',' + str(@m_company) + ',' + str(200+@m_outlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + 'N' + ''',0,' + convert(varchar(20),@finalbillamt2) + ',''' + @m_split + ''','+
      str(@userno)+','''+@lux+''',1'  
      exec (@sql)
      if @@error <> 0  goto e_error
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
COMMIT TRANSACTION
RETURN 0
E_ERROR:
ROLLBACK TRANSACTION
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
set @msg = @msg + 'Updation Failed in UpDtKIT'
raiserror (@msg,16,-1)
RETURN 1




GO
/****** Object:  StoredProcedure [dbo].[pbpsb]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* Program By Rm.Ramanathan  PowerBrain Software (India) 06 Oct 2004 */
--CREATE PROCEDURE pbpsb @bills  varchar(50),@days int=1,@r1 varchar(50)
CREATE PROCEDURE [dbo].[pbpsb] @gno numeric,@days int=1,@r1 varchar(50)
/* with encryption */
AS
--declare @gno numeric
declare @company numeric
declare @cno numeric
declare @t1 varchar(50) 
declare @t2 varchar(50)
declare @sql varchar(8000)
declare @rv varchar(20)
declare @foodbill money
declare @spldisc money
declare @crname varchar(50)
declare @crnum varchar(50)
declare @crtype varchar(50)
declare @glactno numeric
declare @mop int
declare @split char(1)
declare @bcompany int
declare @grno numeric
declare @fy numeric
declare @mainbilldate datetime
declare @ret_value numeric
declare @bno numeric
declare @cid datetime
declare @cod datetime
declare @cod1 datetime
declare @cno1 numeric
declare @co1 int
declare @name varchar(100)
declare @mainbno numeric
declare @brno numeric
--Print 'Periya'
begin transaction
	select @brno=brno from guest where gno=@gno
	if exists (select * from guest where gno=@gno and datediff(d,ci_date,co_date) > 1)
	 begin
		select @brno=brno from guest where gno=@gno
		if exists(select * from billhist1 where brno=@brno and company=1 and split='N')   
		begin
			select @mainbno=bno,@cno=cno,@grno=grno,@mainbilldate=txndt from billhist1 where brno=@brno and company=1 and split='N'
			select @name=[name],@cid=ci_date,@cod=co_date from guest_view_new where gno=@gno
			set @cod1 = dateadd(d,@days,@cid)
			set @cod= str(day(@cod1)) + ' '+ datename(m,@cod1) + ' ' + str(year(@cod1)) + ' ' + convert(varchar(20),@cod,8)
			if not exists(select * from billhist1 where brno=@brno and split not in ('C','F') and mop <> 0) 
			begin
				declare tc1 cursor for select bno from kithist1 where gno=@gno and datediff(s,'1/1/90',txndt) > datediff(s,'1/1/90',dateadd(d,@days,@cid)) and split not in ('C','F')
				open tc1
				fetch next from tc1 into @bno
				while @@fetch_status=0 
				begin
					exec pbpk0503 @bno,1,0,' ',' ',' ',0,'X',0,1
					if @@error <> 0 goto e_error
					fetch next from tc1 into @bno
				end
				close tc1
				deallocate tc1
				declare tc1 cursor for select cno from phone where gno=@gno and datediff(s,'1/1/90',txndt) > datediff(s,'1/1/90',dateadd(d,@days,@cid)) and split not in ('C','F')
				open tc1
				fetch next from tc1 into @bno
				while @@fetch_status=0 
				begin
					exec pbpp0503 @bno,1,0,'','','','X',0,1
					if @@error <> 0 goto e_error
					fetch next from tc1 into @bno
				end
				close tc1
				deallocate tc1
				declare tc1 cursor for select chctrlno from chalan where gno=@gno and mop=2 and datediff(s,'1/1/90',finaldate) > datediff(s,'1/1/90',dateadd(d,@days,@cid)) and split not in ('C','F')
				open tc1
				fetch next from tc1 into @bno
				while @@fetch_status=0 
				begin
					exec pbpt0503 @bno,0,1,'' ,'', '' ,0,1
					fetch next from tc1 into @bno
				end 
				close tc1
				deallocate tc1
				select @cno=cno,@grno=grno,@mainbilldate=txndt from billhist1 where brno=@brno and company=1 and split='N'
				update guest set co_date=@cod where gno=@gno
				if @@error <> 0 goto e_error
				delete from billhist where gno=@gno and datediff(d,'1/1/90',txndt) > datediff(d,'1/1/90',@cod)
				declare c1 cursor for select cno,company,grno from billhist1 where brno=@brno
				open c1
				fetch next from c1 into @cno,@company,@grno
				while @@fetch_status=0 
				begin
					exec @fy=getfy @mainbilldate,@company,@fy
					if @@error <> 0 goto e_error
					exec delgrno @company,@fy,@grno,@mainbilldate,@ret_value out
					if @ret_value<>0 goto e_error
					exec getgrp @cod,@company,@grno out
					if @@error<>0 goto e_error
					update billhist1 set grno=@grno where cno=@cno
					update billhist1 set txndt=@cod where brno=@brno
					exec pbvc @cno,@cno
					fetch next from c1 into @cno,@company,@grno
				end
				close c1
				deallocate c1
			end
			else 
			begin
				set @sql = 'insert into ' + @r1 + ' values (''' + str(@mainbno) +'-Date' + convert(varchar(20),@mainbilldate,3) + '-'+   @name + ' Mode of Payment is not cash in some bills' + ''')'
				exec(@sql) 
			end
		end -- end of new if (if exists other mode of payments)
	end
	else begin
	select @mainbno=bno,@cno=cno,@grno=grno,@mainbilldate=txndt from billhist1 where brno=@brno and company=1 and split='N'
	select @name=[name],@cid=ci_date,@cod=co_date from guest_view_new where gno=@gno
	set @sql = 'insert into ' + @r1 + ' values (''' + str(@mainbno) +'-Date' + convert(varchar(20),@mainbilldate,3) + '-'+   @name + ' stay day is only one ' + ''')'
	exec(@sql)
	end
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[pbpscommEdit]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[pbpscommEdit] @octrlno numeric,@txndt datetime, @gno numeric, @details varchar(100) , @amt money, @mop tinyint, @actno numeric , @ctrlno numeric  out
/* with encryption */
AS
declare @usernm varchar(50)
declare @grno numeric
declare @partic varchar(200)
declare @rno numeric
declare @company int
declare @fy numeric
declare @vno numeric

declare @ogrno numeric
declare @otxndt datetime
declare @ofy int
declare @oamt money
declare @odetails varchar(100)
declare @omop int
begin transaction

set @company=1

select @ogrno=grno,@otxndt=txndt,@oamt=amt,@odetails=details,@omop=mop,@vno=CommnVNO   from commission where ctrlno=@octrlno

exec getfy @otxndt,1,@ofy out
if @@error<> 0 goto e_error
exec delgrno 1,@ofy,@ogrno,@otxndt,@fy out
if @@error<> 0 goto e_error

exec getfy @txndt,1,@fy out
if @@error<> 0 goto e_error

--this is not necessory for editing
--update finyear set commnvno=commnvno+1 where fy=@fy
--select @vno=commnvno from finyear where fy=@fy

select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @rno=rno from guest where gno=@gno

exec getgrp @txndt,1,@grno out
if @grno=0 goto e_error
--print 'periya' goto e_error
delete from commission where ctrlno=@octrlno

insert into commission (commnvno,gno,details,amt,txndt,paidby,grno,mop,glactno) values (@vno,@gno,@details,@amt,@txndt,@usernm,@grno,@mop,@actno) 
if @@error<>0 goto e_error
set @ctrlno = ident_current('commission')

if exists(select * from setting where g_finaccount=0) goto final
set @partic='Commn. Payment Voucher No..' + ltrim(str(@vno)) + '-Sys.No' + ltrim(str(@gno)) + '-R.No' + ltrim(str(@rno)) + '-' + @details
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
 values (@company, @fy,  74 ,@amt , 'To ' + @partic  , 'D', @txndt , @grno ,'Y', @vno)                     -- 74 commission paid account
if @@error <> 0 goto e_error

insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
values (@company, @fy,  @actno  ,@amt , 'By ' + @partic  , 'C', @txndt , @grno ,'Y', @vno)
if @@error <> 0 goto e_error

--select @usernm=usernm from comuser where [host_id]=host_id() and isnull(sessionid,'')=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
	insert into acclog (txndt,usernm,narration,txntype,gno,sessionid) values (getdate(),@usernm, ' Commission Voucher Dated ' + convert(varchar(20),@otxndt,6) + ' Amount ' + ltrim(convert(varchar(20),@oamt)) +  ' Mode of payment '+ case when @omop=0 then 'Cash Payment' else 'GL Account' end + ' Modified to  Dated ' + convert(varchar(20),@txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@amt)) +  ' Mode of payment '+ case when @mop=0 then 'Cash Payment' else 'GL Account' end + ' Modified ',300,@gno,'')
	if @@error<>0 goto e_error

final:
commit transaction
return @ctrlno

e_error:
rollback transaction
raiserror('Error in Commission Payment Vouchers',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbpscommpay]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbpscommpay] @txndt datetime, @gno numeric, @details varchar(100) , @amt money, @mop tinyint, @actno numeric , @ctrlno numeric  out
/* with encryption */
 AS
declare @usernm varchar(50)
declare @grno numeric
declare @partic varchar(200)
declare @rno numeric
declare @company int
declare @fy numeric
declare @vno numeric

begin transaction

set @company=1
exec getfy @txndt,1,@fy out
if @@error<> 0 goto e_error

update finyear set commnvno=commnvno+1 where fy=@fy
select @vno=commnvno from finyear where fy=@fy

--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @rno=rno from guest where gno=@gno

exec getgrp @txndt,1,@grno out
if @grno=0 goto e_error
--print 'periya' goto e_error
insert into commission (commnvno,gno,details,amt,txndt,paidby,grno,mop,glactno) values (@vno,@gno,@details,@amt,@txndt,@usernm,@grno,@mop,@actno) 
if @@error<>0 goto e_error
set @ctrlno = ident_current('commission')

if exists(select * from setting where g_finaccount=0) goto final
set @partic='Commn. Payment Voucher No..' + ltrim(str(@vno)) + '-Sys.No' + ltrim(str(@gno)) + '-R.No' + ltrim(str(@rno)) + '-' + @details
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
 values (@company, @fy,  74 ,@amt , 'To ' + @partic  , 'D', @txndt , @grno ,'Y', @vno)                     -- 74 commission paid account
if @@error <> 0 goto e_error

insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
 values (@company, @fy,  @actno  ,@amt , 'By ' + @partic  , 'C', @txndt , @grno ,'Y', @vno)
if @@error <> 0 goto e_error

final:
commit transaction
return @ctrlno

e_error:
rollback transaction
raiserror('Error in Commission Payment Vouchers',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbpsdelcomm]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbpsdelcomm] @ctrlno numeric  
/* with encryption */
AS

declare @grno numeric
declare @fy numeric
declare @txndt datetime
declare @ret_val numeric
declare @gno numeric
declare @amt money
declare @vno numeric
declare @usernm varchar(50)
begin transaction
select @txndt=txndt,@grno=grno,@gno=gno,@amt=amt,@vno=commnvno  from commission where ctrlno=@ctrlno
exec @fy=getfy @txndt,1,@fy out
if @@error<>0 goto e_error

exec delgrno 1,@fy,@grno,@txndt,@ret_val out
if @@error<>0 goto e_error

delete from commission where ctrlno=@ctrlno
if @@error<>0 goto e_error

--select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm,  'Commission Voucher No.' + ltrim(str(@vno)) + '  for Rs.' + convert(varchar(20),@amt) + ' deleted'  ,300,@gno)
if @@error<>0 goto e_error

commit transaction
return
e_error:
rollback transaction
raiserror('error in deletion of commission voucher,',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbpt0503]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* Program By Rm.Ramanathan  PowerBrain Software (India) 06 Oct 2004 */
CREATE PROCEDURE [dbo].[pbpt0503] 
@ctrlno int,@userno int, @nmop int,@cc_name varchar(30),@cc_num varchar(20),@cc_bank varchar(30),@nglactno numeric,@checkuser int=0 , @rundelcout int=0 ,@splitc int=0 
/* with encryption */
AS
declare @txndt datetime
declare @grno_co1 int
declare @retval int
declare @finalamt1 money
declare @finalamt2 money 
declare @m_txndt datetime
declare @transact int
declare @m_split char(1)
declare @sql varchar(8000)
declare @lux char(1)
declare @ngno numeric
declare @company int
declare @fy int
declare @outlet int
declare @purchase int
declare @sales int
declare @advrecd int
declare @advpaid int
declare @advance money
declare @advance_t money
declare @staxrate money
declare @selfdeal char(1)
declare @hno_mybook int
declare @hno_htlbook int
declare @htl_purchase int
declare @htl_sales int
declare @htl_receivable int
declare @chlno int
declare @tttype int
declare @grno numeric
declare @grno_t numeric
declare @myreceivable numeric
declare @dactno numeric
declare @amt money
declare @olname varchar(50)
declare @partic varchar(100)
declare @nota int
declare @tacode int
declare @ta numeric
declare @gno numeric
declare @finalamt money
declare @finalamt_t money
declare @split char(1)
declare @mop int
declare @basicamt money
declare @discount money
declare @tax money
declare @basciamt money
declare @basicamt_t money
declare @glactno numeric
declare @bata money
declare @bata_t money
declare @otherchgs money
declare @oldmod varchar(20)
declare @newmod varchar(20)
declare @stype int 
declare @status char(1)
declare @otherchgsac numeric
declare @bataac numeric
set @gno=0
set @transact=0
set @ngno = 0

select @nota=notravelagent,@ta=glactno,@olname=olname,@myreceivable=my_receivable,@company=company,@chlno=chlno,
@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,@htl_purchase=htl_purchase,@htl_sales=htl_sales,
@htl_receivable=htl_receivable,@purchase=purchase1,@sales=sales1,@advrecd=advancerecd,@advpaid=advancepaid,@staxrate=staxrate,
@advance=advance,@advance_t=advance_t,
@tttype=tttype ,@tacode=travelagent,
@otherchgsac=otherchgsac,@bataac=bataac
from tt_chalan_ol where chctrlno=@ctrlno

select @status=status,@mop=mop,@lux=lux, @m_split=split,@gno=gno,@company=company,@txndt=finaldate,@grno=grno,@grno_t=grno_t,@grno_co1=grno_co1,@stype=stype , @olname=olname,@split=split from tt_chalan_ol where chctrlno=@ctrlno
if @stype>2 begin print 'No mode change feature is available for this chalan' goto e_error end
if @status<>'C' begin print 'This  chalan is still not closed' goto e_error end
begin transaction
set @oldmod = case when @mop = 1 then 'Cash' when @mop=2 then 'Room A/c' when @mop=3 then 'Bills Revbl' when @mop=4 then 'GL A/c' when @mop=5 then 'Cr Card' else '**UnKnown**' end
set @newmod = case when @nmop = 1 then 'Cash' when @nmop=2 then 'Room A/c' when @nmop=3 then 'Bills Revbl' when @nmop=4 then 'GL A/c' when @nmop=5 then 'Cr Card' else '**UnKnown**' end
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
if @nmop = 2  set @ngno=@nglactno
if @nmop=2 set @split= case when @splitc=0 then 'N' else 'C' end
if @ngno<>0
  if exists (select * from room where gno=@ngno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
if @gno<>0 if not exists (select * from room where gno=@gno) begin
  if @checkuser=0
  if not exists(select * from servicepermission where userno=@userno and serviceno=185 and outlettype=4 and permission='Y') begin
    print 'You are not permitted to Revive Chalans after check-out'
    goto e_error
  end
  set @transact=1
end
select @m_txndt=finaldate,@outlet=outlet,@chlno=chlno,@amt=amt  from chalan where chctrlno=@ctrlno
if @@error <> 0 goto e_error
exec delgrno @company,@fy,@grno,@txndt,@retval out
if @@error <> 0 goto e_error
begin
declare @fy1 numeric
exec getfy @txndt,1,@fy1 out
if @fy1=0 goto e_error
exec delgrno 1,@fy1,@grno_co1,@txndt,@retval out
if @@error <> 0 goto e_error
end
/*
exec delgrno @company,@fy,@grno_t,@txndt,@retval out
if @@error <> 0 goto e_error
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
end
*/
if @transact=1 and @rundelcout=0
   begin
   update chalan set ogno=0 where chctrlno=@ctrlno
   set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@amt) + ',0,' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,0,''X'',' + str(@userno) + ',''' + @lux + ''',' + str(@checkuser)
   exec (@sql)
   if @@error <> 0  goto e_error
   end
set @mop = @nmop
update chalan set cc_name=@cc_name,cc_num=@cc_num,cc_bank=@cc_bank,mop=@mop,gno=@ngno,glactnum=@nglactno,split=@split where chctrlno=@ctrlno

if @stype=1  exec sptt1vpc @ctrlno
if @stype=2  exec sptt2vpc @ctrlno

final:

declare @m_olname varchar(30)
declare @usernm varchar(30)
--select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @m_olname=[name] from toutlet where outlet=@outlet
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@M_olname))+ case when @stype=1 then ' TT' when @stype=2 then 'Ticketing' end + ' Chalan  No ' + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@AMT)) +  ' MOP Changed from '+ @oldmod + ' to ' + @newmod ,11,@gno)
if @@error <> 0 goto e_error
commit transaction
return
e_error:
raiserror('deletion of chalan failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[pbpt3_0503]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/* Program By Rm.Ramanathan  PowerBrain Software (India) 06 Oct 2004 */
CREATE PROCEDURE [dbo].[pbpt3_0503] 
@ctrlno int,@userno int, @nmop int,@cc_name varchar(30),@cc_num varchar(20),@cc_bank varchar(30),@nglactno numeric,@checkuser int=0 , @rundelcout int=0 ,@splitc int=0 
/* with encryption */
AS
declare @txndt datetime
declare @grno_co1 int
declare @retval int
declare @finalamt1 money
declare @finalamt2 money 
declare @m_txndt datetime
declare @transact int
declare @m_split char(1)
declare @sql varchar(8000)
declare @lux char(1)
declare @ngno numeric
declare @company int
declare @fy int
declare @outlet int
declare @purchase int
declare @sales int
declare @advrecd int
declare @advpaid int
declare @advance money
declare @advance_t money
declare @staxrate money
declare @selfdeal char(1)
declare @hno_mybook int
declare @hno_htlbook int
declare @htl_purchase int
declare @htl_sales int
declare @htl_receivable int
declare @chlno int
declare @tttype int
declare @grno numeric
declare @grno_t numeric
declare @myreceivable numeric
declare @dactno numeric
declare @amt money
declare @olname varchar(50)
declare @partic varchar(100)
declare @nota int
declare @tacode int
declare @ta numeric
declare @gno numeric
declare @finalamt money
declare @finalamt_t money
declare @split char(1)
declare @mop int
declare @basicamt money
declare @discount money
declare @tax money
declare @basciamt money
declare @basicamt_t money
declare @glactno numeric
declare @bata money
declare @bata_t money
declare @otherchgs money
declare @oldmod varchar(20)
declare @newmod varchar(20)
declare @stype int 
declare @status char(1)
declare @otherchgsac numeric
declare @bataac numeric
set @gno=0
set @transact=0
set @ngno = 0

select @outlet=outlet,
@status=[status],
@mop=mop,
@txndt=finaldate,
@chlno=chlno
from ta_chalan where ctrlno=@ctrlno

select @company=company,@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,@htl_receivable=htl_receivable,@olname=name,@myreceivable=my_receivable from toutlet where outlet=@outlet

--select @nota=notravelagent,@ta=glactno,@olname=olname,@myreceivable=my_receivable,@company=company,@chlno=chlno,
--@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,@htl_purchase=htl_purchase,@htl_sales=htl_sales,
--@htl_receivable=htl_receivable,@purchase=purchase1,@sales=sales1,@advrecd=advancerecd,@advpaid=advancepaid,@staxrate=staxrate,
--@advance=advance,@advance_t=advance_t,
--@tttype=tttype ,@tacode=travelagent,
--@otherchgsac=otherchgsac,@bataac=bataac
--from tt_chalan_ol where chctrlno=@ctrlno

if @status<>'C' begin print 'This  chalan is still not closed' goto e_error end
begin transaction
set @oldmod = case when @mop = 1 then 'Cash' when @mop=2 then 'Room A/c' when @mop=3 then 'Bills Revbl' when @mop=4 then 'GL A/c' when @mop=5 then 'Cr Card' else '**UnKnown**' end
set @newmod = case when @nmop = 1 then 'Cash' when @nmop=2 then 'Room A/c' when @nmop=3 then 'Bills Revbl' when @nmop=4 then 'GL A/c' when @nmop=5 then 'Cr Card' else '**UnKnown**' end
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
if @nmop = 2  set @ngno=@nglactno
if @nmop=2 set @split= case when @splitc=0 then 'N' else 'C' end
if @ngno<>0
	if exists (select * from room where gno=@ngno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
if @gno<>0 if not exists (select * from room where gno=@gno) 
	begin
	if @checkuser=0
		if not exists(select * from servicepermission where userno=@userno and serviceno=185 and outlettype=4 and permission='Y') 
			begin
			print 'You are not permitted to Revive Chalans after check-out'
			goto e_error
			end
	set @transact=1
	end
select @m_txndt=finaldate,@outlet=outlet,@chlno=chlno,@amt=finalamt,@grno=grno1,@grno_co1=grno3  from ta_chalan where ctrlno=@ctrlno
if @@error <> 0 goto e_error
exec delgrno @company,@fy,@grno,@txndt,@retval out
if @@error <> 0 goto e_error

if @grno_co1<>0
begin
declare @fy1 numeric
exec getfy @txndt,1,@fy1 out
if @fy1=0 goto e_error
exec delgrno 1,@fy1,@grno_co1,@txndt,@retval out
if @@error <> 0 goto e_error
end

if @transact=1 and @rundelcout=0
   begin
   --update ta_chalan set ogno=0 where ctrlno=@ctrlno
   set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@amt) + ',0,' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,0,''X'',' + str(@userno) + ',''' + @lux + ''',' + str(@checkuser)
   exec (@sql)
   if @@error <> 0  goto e_error
   end
set @mop = @nmop
update ta_chalan set crname=@cc_name,crnum=@cc_num,crbank=@cc_bank,mop=@mop,gno=@ngno,glac=@nglactno,split=@split where ctrlno=@ctrlno

exec sptt3vpc @ctrlno
if @@error<>0 goto e_error

final:

declare @m_olname varchar(30)
declare @usernm varchar(30)
--select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @m_olname=[name] from toutlet where outlet=@outlet
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@M_olname))+ ' Hotel Chalan  No ' + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@AMT)) +  ' MOP Changed from '+ @oldmod + ' to ' + @newmod ,11,@gno)
if @@error <> 0 goto e_error
commit transaction
return
e_error:
raiserror('Mode Change of Hotel Chalan failed',16,-1)
return





GO
/****** Object:  StoredProcedure [dbo].[pbpv1u]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[pbpv1u]  @TEMPTABLE VARCHAR(255) ,@channelcode char(1)=' ' ,@m_grno numeric = 0,
@OGRNO NUMERIC OUT,@mvchno numeric=0,@txntype char(5)='TRFR',@username varchar(50)='NULL'  ,@sessionid varchar(200)=''
/* with encryption */
AS
set xact_abort on
DECLARE @M_COMPANY INT
DECLARE @M_TXNDT DATETIME
DECLARE @M_ACTNUM INT
DECLARE @M_PARTIC VARCHAR(500)
DECLARE @M_AMTTYP CHAR(1)
DECLARE @M_AMT FLOAT
DECLARE @SQL VARCHAR(255)
DECLARE @M_TXNTYP CHAR(1)
DECLARE @M_FDRSNO INT
DECLARE @M_FY INT
--DECLARE @M_GRNO INT
DECLARE @M_CR FLOAT
DECLARE @M_DR FLOAT
DECLARE @M_CCR FLOAT
DECLARE @M_CDR FLOAT
declare @retval int
declare @cash int
declare @m_partic1 varchar(50)
declare @payee varchar(100)
declare @txnno2 numeric
declare @txnno2_c numeric
DECLARE @SELLER NUMERIC
DECLARE @BILLNO VARCHAR(50)
DECLARE @BILLDATE VARCHAR(20)
DECLARE @TOTAMT NUMERIC(18,2)
DECLARE @PERCENTAGE NUMERIC(18,2)
DECLARE @ACTNO VARCHAR(50)
DECLARE @SELLERNAME VARCHAR(50)
DECLARE @GSTIN VARCHAR(15)
DECLARE @CASHPURCHASE TINYINT 
declare @chlno_rtxnno numeric
--DECLARE @ERR INT
--SELECT @ERR=0
set nocount on
SELECT @M_DR = 0
SELECT @M_CR = 0
set @cash = 0

begin transaction [V1]
--declare @usernm varchar(50)
--Select @usernm=usernm from comuser where [host_id]=host_id()
SELECT @SQL = 'DECLARE CURSOR1 CURSOR FOR SELECT SUM(AMT)  FROM '+ @TEMPTABLE +'  WHERE AMTTYP=''C'''
EXEC (@SQL)
OPEN CURSOR1
FETCH NEXT FROM CURSOR1 INTO @M_CCR
IF  @@FETCH_STATUS=0
   IF @M_CCR IS NOT NULL
       SET @M_CR = @M_CCR
DEALLOCATE  CURSOR1
SELECT @SQL = 'DECLARE CURSOR1 CURSOR FOR SELECT SUM(AMT)  FROM '+ @TEMPTABLE +'  WHERE AMTTYP=''D'''
EXEC (@SQL)
OPEN CURSOR1
FETCH NEXT FROM CURSOR1 INTO @M_CDR
IF  @@FETCH_STATUS=0 
    IF @M_CDR IS NOT NULL
        SET @M_DR = @M_CDR
DEALLOCATE  CURSOR1
SELECT @SQL = 'DECLARE CURSOR1 CURSOR FOR SELECT TOP 1 COMPANY,TXNDT,TXNTYP,actnum  FROM '+ @TEMPTABLE 
EXEC (@SQL)
OPEN CURSOR1
FETCH NEXT FROM CURSOR1 INTO @M_COMPANY,@M_TXNDT,@M_TXNTYP ,@m_actnum
IF @@FETCH_STATUS=0
  DEALLOCATE  CURSOR1
ELSE
RAISERROR( 'No vouchers entered to post' ,11,1)
EXEC  GETFY @M_TXNDT,@M_COMPANY,@M_FY OUT
IF @M_FY=0 GOTO E_ERROR
if @@error <> 0 goto e_error
if @m_grno=0
begin
--	BEGIN TRANSACTION [v3]
    while 1=1
    begin
	    EXEC  GETGRP @M_TXNDT,@M_COMPANY,@M_GRNO OUT
	    IF @M_GRNO=0 GOTO E_ERROR
	    if @@error <> 0 
	    begin
        --rollback transaction [v3]
	    goto e_error
	    end
        if not exists(select * from vouchers where datediff(d,@m_txndt,txndt)=0 and company=@m_company and grno=@m_grno) 
        break
	    set @m_grno=@m_grno
	    --COMMIT TRANSACTION [v3]
    end
end

declare @x numeric
set @x=0
while exists (select * from vouchers where company=@m_company and fy=@m_fy and datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@m_txndt) and grno=@m_grno)
begin
set @x=@x+1
if @x > 10 break
EXEC  GETGRP @M_TXNDT,@M_COMPANY,@M_GRNO OUT
IF @M_GRNO=0 GOTO E_ERROR
if @@error <> 0 goto e_error
set @m_grno=@m_grno
end
select @m_partic=cgp_desc from glprimld where company= @m_company and fy=@m_fy and cgp_actno = @m_actnum
if @m_dr <> @m_cr set @cash = 1
/* this is for cash vouchers to club
IF @M_DR<>@M_CR
BEGIN
  IF @M_DR > @M_CR
     BEGIN
     SELECT @SQL = 'INSERT ' + @TEMPTABLE +' (ACTNUM,AMTTYP,AMT,PARTIC) VALUES (1,''C'','+'CONVERT(MONEY,''' + STR(@M_DR-@M_CR,14,2) + '''),''' + @m_partic +''' )'
     EXEC(@SQL)
     END
  ELSE
     BEGIN
     SELECT @SQL = 'INSERT ' + @TEMPTABLE +' (ACTNUM,AMTTYP,AMT,PARTIC) VALUES (1,''D'','+'CONVERT(MONEY,''' + STR(@M_CR-@M_DR,14,2) + '''),''' + @m_partic + ''')'
     EXEC(@SQL)
     END
END
*/
set @mvchno =  isnull(@mvchno,0)

SET @SQL = 'DECLARE UPDTVCH CURSOR FOR SELECT ACTNUM,PARTIC,AMTTYP,AMT,FDRSNO,payee,txnno2,SELLER,BILLNO,BILLDATE_closedate,TOTAMT_balance,PERCENTAGE,ACTNO,SELLERNAME,GSTIN,CASHPURCHASE,chlno_rtxnno FROM '+  @TEMPTABLE 
EXEC (@SQL)
OPEN UPDTVCH
FETCH NEXT FROM UPDTVCH INTO @M_ACTNUM,@M_PARTIC,@M_AMTTYP,@M_AMT,@M_FDRSNO,@payee,@txnno2,@SELLER,@BILLNO,@BILLDATE,@TOTAMT,@PERCENTAGE,@ACTNO,@SELLERNAME,@GSTIN,@CASHPURCHASE,@chlno_rtxnno
WHILE  @@FETCH_STATUS=0 BEGIN

INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,channelcode,payee,mvchno,txnno2,txntype,[user],sessionid,SELLER,BILLNO,BILLDATE_closedate,TAXTYPE,totamt_balance,percentage,actno,SELLERNAME,GSTIN,CASHPURCHASE,chlno_rtxnno)
VALUES (@M_COMPANY,@M_FY,@M_ACTNUM,@M_AMT, CASE WHEN @M_AMTTYP='D' THEN 'TO ' ELSE 'BY ' END + @M_PARTIC  ,@M_AMTTYP,@M_TXNDT,@M_GRNO,@M_FDRSNO,@channelcode,@payee,@mvchno,isnull(@txnno2,0),@txntype,@username,@sessionid,@SELLER,@BILLNO,@BILLDATE,0,@totamt,@PERCENTAGE,@actno,@SELLERNAME,@GSTIN,@CASHPURCHASE,@chlno_rtxnno )
IF @@ERROR<>0 GOTO E_ERROR
if @cash=1
  begin
     if @m_amttyp = 'D' 
        set  @m_amttyp = 'C'
    else
       set @m_amttyp = 'D'
     select @m_partic1=cgp_desc from glprimld where company= @m_company and fy=@m_fy and cgp_actno = @m_actnum
-- the next line was a test to create the exact txnno for cash accounts . How ever it  is not used since the cash transactions are also reposted when they are edited   
  if isnull(@txnno2,0) <> 0 set @txnno2_c = @txnno2+1 else set @txnno2_c = 0
      INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,mvchno,txnno2,txntype,[user],sessionid)
      VALUES (@M_COMPANY,@M_FY,1,@M_AMT, CASE WHEN @M_AMTTYP='D' THEN 'TO ' ELSE 'BY ' END + rtrim(@m_partic1)+'-'+@M_PARTIC  ,@M_AMTTYP,@M_TXNDT,@M_GRNO,@M_FDRSNO,@mvchno,isnull(@txnno2,0),@txntype,@username,@sessionid)
      IF @@ERROR<>0 GOTO E_ERROR
end
   
FETCH NEXT FROM UPDTVCH INTO @M_ACTNUM,@M_PARTIC,@M_AMTTYP,@M_AMT,@M_FDRSNO,@payee,@txnno2,@SELLER,@BILLNO,@BILLDATE,@TOTAMT,@PERCENTAGE,@ACTNO,@SELLERNAME,@GSTIN,@CASHPURCHASE,@chlno_rtxnno
END
DEALLOCATE UPDTVCH
set @retval = 0
SET @OGRNO = @M_GRNO
final:
SELECT @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
commit  transaction [V1]
RETURN @retval
E_ERROR:
ROLLBACK TRANSACTION [V1]
raiserror('Problem',16,-1)
set @retval =1
goto final




GO
/****** Object:  StoredProcedure [dbo].[pbsgetsac]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbsgetsac]   @Supp  numeric,@fy numeric, @Actno numeric=0,@Return numeric out, @already numeric out
/* with encryption */
AS
declare @oacno numeric
declare @facno numeric
declare @ofy int
declare @fy1 numeric
declare @company numeric
begin transaction
select @fy1=min(fy) from finyear where company=(select company from finyear where fy=@fy)
delete from suppglac where fy not in (select fy from finyear)
delete from suppglac where supplier not in (select sid from suppliers) 
select @company=company from finyear where fy=@fy1

if not exists(select * from SuppGLAC where supplier=@supp and fy in (select fy from finyear where company=@company) )
begin
	set @return = 0
	set @already=0
	goto Final
end

set @return=0
set @already=1
select top 1 @return=isnull(glactno,0) from suppglac where supplier=@supp and fy  in (select fy from finyear where company=@company) 
if @@error <> 0 goto e_error

final:
commit transaction
return @return
E_ERROR:
rollback transaction
raiserror('Error in updation',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[pbsgetsac_NEW]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[pbsgetsac_NEW]   @Supp  numeric,@fy numeric, @Actno numeric=0,@Return numeric out, @already numeric out
/* with encryption */
AS
declare @oacno numeric
declare @facno numeric
declare @ofy int
declare @fy1 numeric
declare @company numeric
begin transaction
select @company=company from finyear where fy=@fy
delete from suppglac where fy not in (select fy from finyear where company=@company)
delete from suppglac where supplier not in (select sid from suppliers) 
if not exists(select * from SuppGLAC where supplier=@supp)
begin
	set @return = 0
	set @already=0
	goto Final
end
set @return=0
set @already=1
declare @cnt numeric
select @cnt=count(*) from SUPPGLAC where supplier=@supp
select top 1 @return=isnull(glactno,0) from suppglac where supplier=@supp 
if @@error <> 0 goto e_error
	if @cnt > 1 
	begin
	delete from suppglac where supplier=@supp
	insert into suppglac (supplier,fy,glactno ) values (@supp,@fy,@return)
	end 
else
declare @today datetime
set @today = getdate() 
exec getfy @today,@company,@fy
update SUPPGLAC set fy=@fy
final:
commit transaction
return @return
E_ERROR:
rollback transaction
raiserror('Error in updation',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[pbspeb]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/*  PowerBrain Software (India) 02-02-2006 */
CREATE PROCEDURE [dbo].[pbspeb] @t1 varchar(50) ,@cno numeric, @gno numeric ,@drate money,@spldiscount money,@foodbill money  
/* with encryption */
AS
declare @sql varchar(5000)
declare @brno numeric
declare @company int
declare @fy int
declare @ret_val numeric
declare @txndt datetime
declare @rent money
declare @lt money
declare @discount money
declare @ctrlno numeric
declare @usernm varchar(50)
declare @total1 money
declare @total2 money
declare @advance money
declare @roomdues money 
declare @name varchar(50)
declare @grno numeric
declare @bno numeric
declare @fb money
declare @sd money
declare @rent1 money
declare @sc money
declare @st money
declare @cess money
declare @cess2 money 
declare @otherchgs money
declare @lrate money
declare @extrabed money
declare @food money
declare @exbedgst money
declare @foodgst money

select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
begin transaction
select @bno=bno,@fb=foodbill,@sd =spldiscount,@txndt=txndt,@grno=grno from billhist1 where cno=@cno
select @total1=sum(total) from billhist where cno=@cno
select @advance=sum(amount) from advance where gno in (select gno from billhist where cno=@cno) and company=1 and split='N'
select @roomdues=sum(amount) from roomdues where gno in (select gno from billhist where cno=@cno)
select @total1 = isnull(@total1,0)+isnull(@roomdues,0)+isnull(@fb,0)-isnull(@sd,0)-isnull(@advance,0)
select @name=name from guest where gno=@gno
exec getfy @txndt,1,@fy out
if @@error<>0 goto e_error            
set @ret_val=0
exec delgrno 1,@fy,@grno,@txndt,@ret_val out
if @@error<>0 goto e_error
if @ret_val=1 goto e_error
update billhist1 set spldiscount=@spldiscount,foodbill=@foodbill where cno=@cno
set @rent1=0
set @sql='declare c1 cursor for select rent,lt,discount,ctrlno,sc,st,cess,cess2,otherchgs,lrate,extrabed,food,exbedgst,foodgst from ' + @t1
exec(@sql)
open c1
fetch next from c1 into @rent,@lt,@discount,@ctrlno,@sc,@st,@cess,@cess2,@otherchgs,@lrate,@extrabed,@food,@exbedgst,@foodgst
while @@fetch_status=0
begin
update billhist set rent=@rent,lt=@lt,discount=@discount,sc=@sc,st=isnull(@st,0),cess=isnull(@cess,0),cess2=isnull(@cess2,0),otherchgs=isnull(@otherchgs,0),lrate=isnull(@lrate,0),extrabed=isnull(@extrabed,0),food=isnull(@food,0) where ctrlno=@ctrlno
-- important ..introduced on 09 August 2017
update billhist set foodgst=case when food<>0 then round(food*lrate/100,2) else 0 end,exbedgst=case when extrabed<>0 then round(extrabed*lrate/100,2) else 0 end where ctrlno=@ctrlno
---- new line above
update billhist set total=rent+lt+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10+sc+st+cess+cess2+otherchgs-discount+extrabed+exbedgst+food+foodgst where ctrlno=@ctrlno
if @rent1 < @rent set @rent1=@rent
if @@error<>0 goto e_error
fetch next from c1 into @rent,@lt,@discount,@ctrlno,@sc,@st,@cess,@cess2,@otherchgs,@lrate,@extrabed,@food,@exbedgst,@foodgst
end
close c1
deallocate c1
update guest set discount=@drate,rent=@rent1  where gno=@gno
exec pbvc @cno,@cno
if @@error<>0 goto e_error
select @total2=sum(total) from billhist where cno=@cno
select @total2 = isnull(@total2,0)+isnull(@roomdues,0)+isnull(@foodbill,0)-isnull(@spldiscount,0)-isnull(@advance,0)
set @sql = 'Final Bill Edited ' + ltrim(str(@bno))+ ' Dated ' + convert(varchar(20),@txndt)  + '-Old Amount ' +  convert(varchar(20),isnull(@total1,0)) + ' New Amount ' + convert(varchar(20),isnull(@total2,0)) + ' Guest Name ' + @name
 insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, @sql ,0,@gno)
if @@error <> 0  goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[PbVC]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) - 01-02-2006 */
CREATE PROCEDURE [dbo].[PbVC] @cno numeric,@updbillhist int=0 
/* with encryption */
AS
declare @mop int
declare @crnum varchar(50)
declare @crname varchar(50)
declare @crtype varchar(50)
declare @spldiscount money
declare @foodbill money
declare @bno numeric
declare @glactno numeric
declare @grno numeric
declare @brno numeric     --pk
declare @txndt datetime
declare @rent money
declare @rentNT money
declare @lt money
declare @discount money
declare @sc money
declare @item1 money
declare @item2 money
declare @item3 money
declare @item4 money
declare @item5 money
declare @item6 money
declare @item7 money
declare @item8 money
declare @item9 money
declare @item10 money
declare @advance money
declare @details varchar(200)
declare @company integer
declare @split char(1)
declare @roomdues money
declare @fy int
declare @actnum numeric
declare @linkno numeric
declare @ctotal money
declare @usernm varchar(50)
declare @partic_con varchar(150)
declare @con_id varchar(50)
declare @a int
declare @ol1 int
declare @ol2 int
declare @ol3 int
declare @ol4 int
declare @ol5 int
declare @ol6 int
declare @ol7 int
declare @ol8 int
declare @ol9 int
declare @ol10 int
declare @typeoutlet int
declare @amt money
declare @grpname varchar(100)
declare @grpcounter int
declare @gno numeric
declare @name varchar(100)
declare @desc varchar(150)
declare @grprooms int
declare @grpnow int
declare @voucherno varchar(50)
declare @eno numeric
declare @rno numeric
declare @st money
declare @cess money
declare @cess2 money
declare @cessac2 numeric
declare @otherchgs money
declare @lrate money
declare @rentac numeric
declare @chgrno numeric
declare @chfy numeric
declare @chcompany numeric
declare @channel varchar(100)
declare @ischannel int
declare @channelamt money
declare @staahid varchar(100)
declare @chdisc money
declare @upfront int
declare @chdiscamt money
declare @chglactno numeric
declare @commissionac numeric
declare @chnettrate tinyint
declare @err varchar(4000)
declare @extrabed money
declare @exbedgst money
declare @food money
declare @foodgst money
declare @commcompany int 

--print 'periya'
--if @split='Y'  print 'periya karuppu ' +  str(@company) +' '+ @split + ' '+  convert(varchar(20),((@ctotal   +@foodbill+@roomdues)-(@advance+@spldiscount)))
set @err = 'Error in Checkout Vouching'
begin transaction 

--select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
select @mop=mop,@crnum=crnum,@crname=crname,@crtype=crtype,@spldiscount=isnull(spldiscount,0),@foodbill=isnull(foodbill,0),
@bno=bno,@glactno=glactno,@grno=grno, @brno=brno,@txndt=txndt,@company=company,@split=split  from billhist1 where cno=@cno
if @split='C' goto label1
select @gno=min(gno) from billhist where cno=@cno

set @commcompany=@company

exec @fy=getfy @txndt,@company,@fy out
if @@error<>0 goto e_error
/* Introdused on 25 March 2009 for Controling the Credits to Bill TO Company Mode */

if @grno=0
begin
	exec getgrp @txndt,@company,@grno out
	if @@error <>0 goto e_error
	set @grno = @grno 
	update billhist1 set grno=@grno where cno=@cno
end

--- the following declaration made on 12 jan 2014' to avoid any change in teh variable value with in this procedure...this is use in the last snippet
set @chgrno=@grno
set @chfy = @fy
set @chcompany=@company

set @chdiscamt = 0
set @staahid = ''

exec  delgrno @company,@fy,@grno,@txndt,@advance out   -- no relation for advance ..just used for currency variable
if @@error <>0 goto e_error
set @advance=0
select  @advance=sum(isnull(amount,0)) from advance where company=@company and split=@split  and gno in (select gno from guest where brno=@brno )
--set @err =  convert(varchar(20),@advance) goto e_error
set @roomdues=0
select @roomdues= sum(isnull(amount,0)) from roomdues where company=@company and  split=@split  and gno in (select gno from guest where brno=@brno)
set @advance=isnull(@advance,0)
set @roomdues=isnull(@roomdues,0)
update billhist1 set advance=@advance where cno=@cno
if exists (select * from billhist where cno=@cno) 
	select @ctotal=sum(isnull(total,0)),
	@rent=sum(case when isnull(lt,0) <> 0 then isnull(rent,0) else 0 end),
	@rentNT=sum(case when isnull(lt,0) = 0 then isnull(rent,0) else 0 end),
	@extrabed=sum(isnull(extrabed,0)),@exbedgst=sum(isnull(exbedgst,0)),@food=sum(isnull(food,0)),@foodgst=sum(isnull(foodgst,0)),
	@otherchgs=sum(isnull(otherchgs,0)), @lt=sum(isnull(lt,0)),@discount=sum(isnull(discount,0)),@sc=sum(isnull(sc,0)),
	@item1=sum(isnull(item1,0)),@item2=sum(isnull(item2,0)),@item3=sum(isnull(item3,0)),@item4=sum(isnull(item4,0)),
	@item5=sum(isnull(item5,0)),@item6=sum(isnull(item6,0)),@item7=sum(isnull(item7,0)),@item8=sum(isnull(item8,0)),
	@item9=sum(isnull(item9,0)),@item10=sum(isnull(item10,0)),
	@ol1=max(isnull(outlet1,0)),@ol2=max(isnull(outlet2,0)),@ol3=max(isnull(outlet3,0)),
	@ol4=max(isnull(outlet4,0)),@ol5=max(isnull(outlet5,0)),@ol6=max(isnull(outlet6,0)),
	@ol7=max(isnull(outlet7,0)),@ol8=max(isnull(outlet8,0)),@ol9=max(isnull(outlet9,0)),
	@ol10=max(isnull(outlet10,0)) ,@st=sum(isnull(st,0)),@cess=sum(isnull(cess,0)) ,@cess2=sum(isnull(cess2,0)) 
	from billhist where  cno=@cno
else
	begin
	select @ctotal=0,@rent=0,@otherchgs=0,@lt=0,@discount=0,@sc=0,@item1=0,@item2=0,@item3=0,@item4=0,@item5=0,@item6=0,@item7=0,
	@item8=0,@item9=0,@item10=0,@ol1=0,@ol2=0,@ol3=0,@ol4=0,@ol5=0,@ol6=0,@ol7=0,@ol8=0,@ol9=0,@ol10=0,@st=0,@cess=0,@extrabed=0,@exbedgst=0,@food=0,@foodgst=0
    end
set @ctotal=isnull(@ctotal,0)
/*  This was created on 18/09/2006 to test the split bill with advance and no bill entries
if @split='Y'
begin
print  'tot' + convert(varchar(20),@ctotal)  + ' adv ' + convert(varchar(20),@advance) + ' fb'  +  convert(varchar(20),@foodbill) + ' '+ convert(varchar(20),@roomdues) 
goto e_error
end
*/
select @RNO=RNO,@eno=eno,@grpname=grpname,@grpcounter=grpcounter,@name=name,@voucherno=voucherno from guest where gno=@gno
select @grprooms=count(*) from guest where grpname=@grpname
select @grpnow =count(*) from guest where brno=@brno
--select @name=name from guestad where cno=(select cno from guest where gno=@gno)
set @name = substring(@name,1,20)
if isnull(@grpname,'') = '' 
	set @desc=rtrim(ltrim(@voucherno)) + case when ltrim(rtrim(@voucherno))<>'' then '-' else '' end +rtrim(ltrim(@name))
else
	set @desc='Group-' + ltrim(rtrim(@grpname)) +'-' + ltrim(rtrim(str(@grpcounter))) + '-'+ ltrim(rtrim(str(isnull(@grpnow,0))))+'/'+ ltrim(rtrim(str(isnull(@grprooms,0)))) +'-'+rtrim(ltrim(@voucherno)) + case when ltrim(rtrim(@voucherno))<>'' then '-' else '' end +rtrim(ltrim(@name))

if @staahid<>'' set @desc = @desc + ' Staah ID - ' + @staahid

  --   print @desc goto e_error

if @split='Y'
	select @details= 'Split Bill No ' + ltrim(str(@bno)) +  ' ' + @desc + ' Sys BR.No.' + ltrim(str(@brno)) + ' GR No:' + ltrim(str(@eno)) + ' R.No' + ltrim(str(@rno))
else
	select @details= + 'Bill No ' + ltrim(str(@bno))+ ' ' + @desc +  '-Sys No.' + ltrim(str(@brno)) + ' GR No:' + ltrim(str(@eno)) + ' R.No' + ltrim(str(@rno))
if @mop=4 set @details = @details + '-Put in Sys.No '  + ltrim(str(@glactno))  --' Room Account
--print 'periya' + @split
--goto e_error
declare @nofa int 
set @nofa=0
if exists(select * from setting where g_finaccount=0) 
	begin 
		set @nofa = 1
	end  -- Stops for PowerHms-Lite

--if exists(select * from setting where g_finaccount=0) 
--	begin 
--		commit transaction 
--		return 
--	end  -- Stops for PowerHms-Lite

declare @finalamt money
set @chdiscamt = 0
set @staahid = ''
if @split <> 'F'
begin
	if @company=1
	begin
	select @channel=channel,@ischannel=ischannel,@channelamt=channelamt,@staahid=staahid from guest where gno=@gno
	if @ischannel=1 
		begin
		declare @dtl varchar(2000)
		select @chdisc=discount,@upfront=upfront,@chglactno=glactno,@commissionac=commissionac,@chnettrate=nettrate
		from staahchannel where staahname=@channel
		if @upfront=1 and @chnettrate=0
			begin
			set @chdiscamt = @channelamt*@chdisc/100
			set @dtl = 'Commission Paid to ' + @channel  + ' Res.ID - ' + @staahid + ' Bill No:' + ltrim(rtrim(str(@bno))) + ' Sys.No' + ltrim(rtrim(str(@gno)))
			if @nofa=0
				begin
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (@chcompany,@chfy,@commissionac,@chdiscamt,'TO ' + @dtl   ,'D',@txndt,@grno,0,'Y','COUT')
				if @@error <> 0 goto e_error
				end
			end
		end
	set @finalamt = ((@ctotal+@foodbill+@roomdues)-(@advance+@spldiscount+@chdiscamt))
	if @split <> 'C' and @split <> 'F'
	begin
	/*Inserted on  29-Mar-2009 to check the debits on debit balance and act acroding to the setting */
	if @finalamt  > 0
		begin
			if @mop=3
				begin
					declare @allowcredit int
					select @allowcredit=allowcredit from setting
					if @allowcredit=0
						begin
							if exists(select * from glprimld where fy=@fy and flag=3 and cgp_actno=@glactno and allowcredit='N' and (balance-@finalamt) < 0)
								begin 
									set @err = 'No Credit Facility is allowed to ' + (select cgp_Desc from glprimld where cgp_actno=@glactno and fy=@fy)
									goto e_error
								end
						end
				end
		end
	declare @pcon varchar(100)
	--this following portion is repeated for company<>1 also at the end ...please take care
	if @mop=4  --modified on 27-03-2009 further modified on 05 Nov 2012 ..removed flag @updbillhist and multiple delete check
		begin
			if exists(select * from roomdues where cno=@cno)
				begin
					update roomdues set amount=0 where cno=@cno
					if @@error <> 0 goto e_error
					exec pproomdue @cno
					if @@error <> 0 goto e_error
					delete from roomdues where cno=@cno
				end
				insert into roomdues (gno,cno,amount,usernm,company,split,txndt) values(@glactno,@cno,@finalamt ,@usernm,@company,@split,@txndt)
				if @@error <> 0 goto e_error
				exec pproomdue @cno 
				if @@error<>0 goto e_error 
		end
end

if exists(select * from setting where g_finaccount=0) 
	begin 
		commit transaction 
		return 
	end  -- Stops for PowerHms-Lite



if cursor_status('global','c123') > 0
begin
close c123
deallocate c123
end

declare @ltamt1 money

declare c123 cursor for select sum(rent) amt,sum(lt) lt,lrate from billhist where cno=@cno group by lrate
open c123
fetch next from c123 into @rent,@ltamt1,@lrate
while @@fetch_status=0
	begin
	if @rent <> 0
	begin
	-----------------------------------------------------------------------------------------------------------
	if exists (select * from rentaccount where lrate=@lrate) 
	    begin
			select @rentac=actnum from rentaccount where lrate=@lrate
			if not exists(select cgp_actno=@rentac from glprimld where fy=@fy)
			select @rentac = 15
		end
	else                                                                                                  --  
		if @ltamt1<>0                                                                        --  
			select @rentac=15                                                     --
		else                                                                                       --   
			select @rentac=80                                                     --
	-----------------------------------------------------------------------------------------------------------
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
			values (@company,@fy,@rentac,@rent, 'BY ' + @details ,'C',@txndt,@grno,0,'Y','COUT')
			if @@error <>0 goto e_error
	end
	fetch next from c123 into @rent,@ltamt1,@lrate
	end
	close c123
	deallocate c123

if @extrabed<>0
begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,97,@extrabed, 'BY ' + @details, 'C',@txndt,@grno,0,'Y','COUT')
	if @@error<>0 goto e_error
end
if @exbedgst<>0
begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,98,@exbedgst, 'BY ' + @details, 'C',@txndt,@grno,0,'Y','COUT')
	if @@error<>0 goto e_error
end
if @food<>0
begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,99,@FOOD, 'BY ' + @details, 'C',@txndt,@grno,0,'Y','COUT')
	if @@error<>0 goto e_error
end
if @foodGST<>0
begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,100,@FOODGST, 'BY ' + @details, 'C',@txndt,@grno,0,'Y','COUT')
	if @@error<>0 goto e_error
end


if @discount <> 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,13,@discount, 'TO ' + @details, 'D',@txndt,@grno,0,'Y','COUT')
	if @@error<>0 goto e_error
	end
if @otherchgs > 0 
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,79,@otherchgs, 'By ' + @details, 'C',@txndt,@grno,0,'Y','COUT')
	if @@error<>0 goto e_error
	end
else
	if @otherchgs < 0 
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,79,@otherchgs, 'To ' + @details, 'D',@txndt,@grno,0,'Y','COUT')
		if @@error<>0 goto e_error
		end
	end   --if the company <> 1
	else
	begin
	if @mop=4  --modified on 27-03-2009 further modified on 05 Nov 2012 ..removed flag @updbillhist and multiple delete check
		begin
			if exists(select * from roomdues where cno=@cno)
				begin
					update roomdues set amount=0 where cno=@cno
					if @@error <> 0 goto e_error
					exec pproomdue @cno
					if @@error <> 0 goto e_error
					delete from roomdues where cno=@cno
				end
				set @finalamt = ((@ctotal+@foodbill+@roomdues)-(@advance+@spldiscount+@chdiscamt))
				insert into roomdues (gno,cno,amount,usernm,company,split,txndt) values(@glactno,@cno,@finalamt ,@usernm,@company,@split,@txndt)
				if @@error <> 0 goto e_error
				exec pproomdue @cno 
				if @@error<>0 goto e_error 
		end
	end

if exists(select * from setting where g_finaccount=0) 
	begin 
		commit transaction 
		return 
	end  -- Stops for PowerHms-Lite


	if @spldiscount <> 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,63,@spldiscount,'TO ' + @details   ,'D',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @foodbill <> 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,58,@foodbill, 'TO ' + @details + ' Food Bills-external'  ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @lt<>0
	begin
	declare @ltnum numeric
	--select @ltnum=glactno from ltmaster where @txndt between sdt and edt 
	select @ltnum=glactno from ltmaster where convert(varchar(20),@txndt,106) between sdt and edt 

	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,@ltnum,@lt, 'BY '+@details   ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @sc<>0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,49,@sc, 'BY ' + @details   ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @st<>0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,75,@st, 'BY ' + @details   ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @cess<>0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,76,@cess, 'BY ' + @details   ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end

	if @cess2<>0 
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,92,@cess2, 'BY ' + @details   ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end

	if @advance<>0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@FY,50,@advance, 'TO ' +@details   ,'D',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end





end --- this is for split<>'C' 

if @roomdues<>0
begin
	if @roomdues > 0
	begin
		insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values(@company,@fy,67,@roomdues,'By ' + @details ,'C',@txndt,@grno,0,'Y','COUT')
		if @@error <>0 goto e_error
	end
	else
	begin
		insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values(@company,@fy,67,abs(@roomdues),'To ' + @details ,'D',@txndt,@grno,0,'Y','COUT')
		if @@error <>0 goto e_error
	end
end
---lk
if @company=1 and @mop=3 
	if exists(select * from linker where company=1 and actnum=@glactno)
		select @linkno=linkid from linker where company=1 and actnum=@glactno
	else

	begin
		select @linkno=max(linkid) from linker
		set  @linkno = isnull(@linkno,0)+1
		insert into linker (company,linkid,actnum) values (1,@linkno,@glactno)
		if @@error<>0 goto e_error
	end
if @company<>1 and @mop=3
	if not exists(select * from linker where company=@company and actnum=@glactno)
		if @linkno is not null
			insert into linker (company,linkid,actnum) values (@company,@linkno,@glactno)
----lk end 
       
set @actnum = case when @mop=0 then 1 when @mop = 1 then 22     when @mop=2  then 46    when @mop=3 then @glactno     when @mop=4 then 67     else 0 end 
if @actnum=0 goto e_error
/*
if @mop=4 and @updbillhist=0
	if not exists(select * from room where gno=@glactno)
	begin
		print 'The requested room has already checked out....Settlement Aborted' goto e_error
	end
if @mop=4 and @updbillhist<>0
	if exists(select * from billhist1 where brno=@glactno and company=@company and split=@split and mop=4)
	begin
		print 'The Room Account Debited for this bill is also checked out through other Room Account' +nchar(13)+ 'Nested Deletion is not possible' + nchar(13) + 'First Change the Mode of Payment of the Parent Bill' + nchar(13) + 'and then try modifying this bill'
		goto e_error
	end
*/

-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------

	---- OTA Commission Vouching since 28 Dec 2019
	declare @commctrlno numeric
	select @commctrlno=otacommctrlno from guest where gno=@gno 
	declare @commission numeric(18,2)
	declare @commgst numeric(18,2)
	declare @commtcs numeric(18,2)
	declare @commtds numeric(18,2)
	declare @status char(1)
	declare @retval numeric
	declare @commno numeric
	declare @commdetails varchar(200)
	declare @gname varchar(100)
	declare @commname varchar(200)
	declare @commCNO as numeric
	declare @commGLACtno numeric
	declare @totcommamt money
	set @totcommamt=0
	if @split='N' and @commcompany=1
	if @commctrlno<>0 
	begin
		select @commcno=cno,@status=status,@commission=commission ,@commgst=GST,@commtcs=tcs,@commtds=tdsamt from otacommission where ctrlno=@commctrlno
		select @commglactno=commglactno,@commname=name from gcompany where comno=(select company from guest where gno=@gno)
		if isnull(@commglactno,0) <> 0
		begin
				if isnull(@commcno,0)=0
				begin
				update finyear set commcno=isnull(commcno,0)+1 where fy=@fy
				select @commcno=commcno from finyear where fy=@fy
				end

		update OTACommission set grno=@grno,txndt=@txndt,status='C',cno=@commcno  where ctrlno=@commctrlno
		select @gname=name  from guest where gno=@gno
		set @commdetails='OTA commssion to OTA :' + ltrim(rtrim('ram')) + ' GNO:' + ltrim(str(@gno)) +' Guest : ' + @gname 
			if @commission<>0
			begin
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (1,@fy,101,@commission, 'To ' + @commdetails, 'D',@txndt,@grno,0,'Y','COMM')
				if @@error<>0 goto e_error
			end
			if @commgst<>0
			begin
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (1,@fy,102,@commgst, 'To ' + @commdetails, 'D',@txndt,@grno,0,'Y','COMM')
				if @@error<>0 goto e_error
			end
			if @commtcs<>0
			begin
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (1,@fy,103,@commtcs, 'BY ' + @commdetails, 'C',@txndt,@grno,0,'Y','COMM')
				if @@error<>0 goto e_error
			end
			if @commtds<>0
			begin
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype,seller,billdate_closedate,Billno,taxtype,actno,totamt_balance)
				values (1,@fy,111,@commtds, 'BY ' + @commdetails, 'C',@txndt,@grno,0,'Y','COMM',@commctrlno,'31 mar 2020','','','194M',@commtds )
				if @@error<>0 goto e_error
			end

			if @commission+@commgst <> 0 
			begin
			set	@totcommamt = @commission+@commgst-(@COMMTCS+@COMMTDS)
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (1,@fy,@commglactno,@commission+@commgst-(@COMMTCS+@COMMTDS), 'To ' + @commdetails, 'C',@txndt,@grno,0,'Y','COMM')
				if @@error<>0 goto e_error
			end
			end
	end
	---- OTA commission Vouching ends

-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------
print convert(varchar(20),(@ctotal+@foodbill+@roomdues)-(@advance+@spldiscount+@chdiscamt+@totcommamt))
print convert(varchar(20),@advance+@spldiscount+@chdiscamt)-(@ctotal+@foodbill+@roomdues)
--goto e_error

if @split <> 'C' and @split <> 'F'
begin
	
	if ((@ctotal+@foodbill+@roomdues)-(@advance+@spldiscount+@chdiscamt))  > 0
	begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,@actnum,(@ctotal+@foodbill+@roomdues-(@advance+@spldiscount+@chdiscamt)) , 'TO '+@details   ,'D',@txndt,@grno,0,'Y','COUT')
		if @@error <> 0 goto e_error
	end
	IF ((@advance+@spldiscount+@chdiscamt)-(@ctotal+@foodbill+@roomdues)) > 0
	begin

		if @actnum=22  set @actnum=68
		--following code inserted on 26 april 2020   corona loakdown period
		--if exists(select * from glprimld where company=@company and fy=@fy and cgp_actno=@actnum and asset='D') 
		--begin
		--set @err='Trying to post a credit (REFUND) entry into a SUBGL DEBIT account...not allowed' goto e_error
		--end
	
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values(@company,@fy,@actnum,(@advance+@spldiscount+@chdiscamt)-(@ctotal+@foodbill+@roomdues) , 'BY ' +@details + ' refund'   ,'C',@txndt,@grno,0,'Y','COUT')
		if @@error <> 0 goto e_error
	end
end
else if  @split = 'C'
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,26,(@ctotal+@foodbill+@roomdues) , 'BY ' + @details  ,'D',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
declare @outlet int
declare @olco int
declare @selfdeal char(1)
declare @hno_mybook int 
declare @hno_htlbook int
declare @tfy int
if exists (select * from billhist where cno=@cno) 
if @split<>'F'
begin
	set @a=1
	while @a<=10
	begin
		-- print 'periyakaruppa 1111'
		if @a=1 begin set @amt=@item1 set @typeoutlet=@ol1 end
		if @a=2 begin set @amt=@item2 set @typeoutlet=@ol2 end
		if @a=3 begin set @amt=@item3 set @typeoutlet=@ol3 end
		if @a=4 begin set @amt=@item4 set @typeoutlet=@ol4 end
		if @a=5 begin set @amt=@item5 set @typeoutlet=@ol5 end
		if @a=6 begin set @amt=@item6 set @typeoutlet=@ol6 end
		if @a=7 begin set @amt=@item7 set @typeoutlet=@ol7 end
		if @a=8 begin set @amt=@item8 set @typeoutlet=@ol8 end
		if @a=9 begin set @amt=@item9 set @typeoutlet=@ol9 end
		if @a=10 begin set @amt=@item10 set @typeoutlet=@ol10 end
		if @amt > 0
			if @typeoutlet > 300
			begin 
				set @outlet = @typeoutlet-300
				select @olco= company,@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook from toutlet where outlet = @outlet
				if @olco=@company and @company<>1
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (@company,@fy,@hno_mybook,@amt, 'BY '+@details  ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
				if (@selfdeal <> 'Y' and @olco > 1) or (@selfdeal='Y' and @olco=1) 
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (1,@fy,24,@amt, 'BY ' + @details    ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
			end
			else
			if @typeoutlet > 200
			begin 
				set @outlet = @typeoutlet - 200
				select @olco = company,@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook from co_phone_ol where outlet = @outlet
				if @olco=@company
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (@company,@fy,@hno_mybook,@amt, 'BY ' + @details  ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
				if @selfdeal <> 'Y' and @olco > 1
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (@olco,@fy,24,@amt, 'BY ' + @details   ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
			end
			else
			begin
				set @outlet = @typeoutlet - 100
				select @olco=cocode,@hno_htlbook=htl_receivable from co_rest_ol1 where rcode=@outlet    --- do not change rcode   <-Never Change(2014)
				--print 'periya karuppu 111' + str(@outlet)  + str(@a)  + '  '+ str(@ol1) + str(@olco)+ '  '+str(@company)
				--goto e_error  
				IF @olco=@company
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (@company,@fy ,       24        ,   @amt, 'BY ' +@details  ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
				else
				begin
					exec @tfy=getfy @txndt,1,@tfy out
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (1,@tfy, @hno_htlbook   ,@amt, 'BY ' + @details  ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
			end 
 	set @a  = @a + 1
	end
end

label1:
--print convert(Varchar(20),((@ctotal+@foodbill+@roomdues)-(@advance+@spldiscount)))
--goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror(@err,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[PbVC11]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) - 01-02-2006 */
-- This program is used by Cash Bill 2   -  31-May-2011 (Bala's Marriage)
CREATE PROCEDURE [dbo].[PbVC11] @cno numeric,@updbillhist int=0 
/* with encryption */
AS
declare @mop int
declare @crnum varchar(50)
declare @crname varchar(50)
declare @crtype varchar(50)
declare @spldiscount money
declare @foodbill money
declare @bno numeric
declare @glactno numeric
declare @grno numeric
declare @brno numeric     --pk
declare @txndt datetime
declare @rent money
declare @rentNT money
declare @lt money
declare @discount money
declare @sc money
declare @item1 money
declare @item2 money
declare @item3 money
declare @item4 money
declare @item5 money
declare @item6 money
declare @item7 money
declare @item8 money
declare @item9 money
declare @item10 money
declare @advance money
declare @details varchar(200)
declare @company integer
declare @split char(1)
declare @roomdues money
declare @fy int
declare @actnum numeric
declare @linkno numeric
declare @ctotal money
declare @usernm varchar(50)
declare @partic_con varchar(150)
declare @con_id varchar(50)
declare @a int
declare @ol1 int
declare @ol2 int
declare @ol3 int
declare @ol4 int
declare @ol5 int
declare @ol6 int
declare @ol7 int
declare @ol8 int
declare @ol9 int
declare @ol10 int
declare @typeoutlet int
declare @amt money
declare @grpname varchar(100)
declare @grpcounter int
declare @gno numeric
declare @name varchar(100)
declare @desc varchar(150)
declare @grprooms int
declare @grpnow int
declare @voucherno varchar(50)
declare @eno numeric
declare @rno numeric
declare @st money
declare @cess money
declare @lrate money
declare @rentac numeric
--if @split='Y'  print 'periya karuppu ' +  str(@company) +' '+ @split + ' '+  convert(varchar(20),((@ctotal   +@foodbill+@roomdues)-(@advance+@spldiscount)))
begin transaction 
--print 'Inform to PowerBrain about this immediately'  goto e_error

--select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
select @mop=mop,@crnum=crnum,@crname=crname,@crtype=crtype,@spldiscount=isnull(spldiscount,0),@foodbill=isnull(foodbill,0),
@bno=bno,@glactno=glactno,@grno=grno, @brno=brno,@txndt=txndt,@company=company,@split=split  from billhist1 where cno=@cno
set @actnum = case when @mop=0 then 1 when @mop = 1 then 22     when @mop=2  then 46    when @mop=3 then @glactno     when @mop=4 then 67     else 0 end 
if @actnum=0 goto e_error
select @gno=min(gno) from billhist where cno=@cno
exec @fy=getfy @txndt,@company,@fy out
if @@error<>0 goto e_error
/* Introdused on 25 March 2009 for Controling the Credits to Bill TO Company Mode */

if @grno=0
begin
	exec getgrp @txndt,@company,@grno out
	if @@error <>0 goto e_error
	set @grno = @grno 
	update billhist1 set grno=@grno where cno=@cno
end
--exec  delgrno @company,@fy,@grno,@txndt,@advance out   -- no relation for advance ..just used for currency variable
exec  delgrno1 @company,@fy,@grno,@txndt,@actnum

if @@error <>0 goto e_error
set @advance=0
select  @advance=sum(isnull(amount,0)) from advance where company=@company and split=@split  and gno in (select gno from billhist where cno=@cno)
set @roomdues=0
select @roomdues= sum(isnull(amount,0)) from roomdues where company=@company and  split=@split  and gno in (select gno from billhist where cno=@cno)
set @advance=isnull(@advance,0)
set @roomdues=isnull(@roomdues,0)
update billhist1 set advance=@advance where cno=@cno
if exists (select * from billhist where cno=@cno) 
	select @ctotal=sum(isnull(total,0)),
	@rent=sum(case when isnull(lt,0) <> 0 then isnull(rent,0) else 0 end),
	@rentNT=sum(case when isnull(lt,0) = 0 then isnull(rent,0) else 0 end),
	@lt=sum(isnull(lt,0)),@discount=sum(isnull(discount,0)),@sc=sum(isnull(sc,0)),
	@item1=sum(isnull(item1,0)),@item2=sum(isnull(item2,0)),@item3=sum(isnull(item3,0)),@item4=sum(isnull(item4,0)),
	@item5=sum(isnull(item5,0)),@item6=sum(isnull(item6,0)),@item7=sum(isnull(item7,0)),@item8=sum(isnull(item8,0)),
	@item9=sum(isnull(item9,0)),@item10=sum(isnull(item10,0)),
	@ol1=max(isnull(outlet1,0)),@ol2=max(isnull(outlet2,0)),@ol3=max(isnull(outlet3,0)),
	@ol4=max(isnull(outlet4,0)),@ol5=max(isnull(outlet5,0)),@ol6=max(isnull(outlet6,0)),
	@ol7=max(isnull(outlet7,0)),@ol8=max(isnull(outlet8,0)),@ol9=max(isnull(outlet9,0)),
	@ol10=max(isnull(outlet10,0)) ,@st=sum(st),@cess=sum(cess) 
	from billhist where  cno=@cno
else
	begin
	select @ctotal=0,@rent=0,@lt=0,@discount=0,@sc=0,@item1=0,@item2=0,@item3=0,@item4=0,@item5=0,@item6=0,@item7=0,
	@item8=0,@item9=0,@item10=0,@ol1=0,@ol2=0,@ol3=0,@ol4=0,@ol5=0,@ol6=0,@ol7=0,@ol8=0,@ol9=0,@ol10=0,@st=0,@cess=0
              end
set @ctotal=isnull(@ctotal,0)
/*  This was created on 18/09/2006 to test the split bill with advance and no bill entries
if @split='Y'
begin
print  'tot' + convert(varchar(20),@ctotal)  + ' adv ' + convert(varchar(20),@advance) + ' fb'  +  convert(varchar(20),@foodbill) + ' '+ convert(varchar(20),@roomdues) 
goto e_error
end
*/
select @rno=rno,@eno=eno,@grpname=grpname,@grpcounter=grpcounter,@name=name,@voucherno=voucherno from guest where gno=@gno
select @grprooms=count(*) from guest where grpname=@grpname
select @grpnow =count(*) from guest where brno=@brno
--select @name=name from guestad where cno=(select cno from guest where gno=@gno)
set @name = substring(@name,1,20)
if isnull(@grpname,'') = '' 
	set @desc=rtrim(ltrim(@voucherno)) + case when ltrim(rtrim(@voucherno))<>'' then '-' else '' end +rtrim(ltrim(@name)) 
else
	set @desc='Group-' + ltrim(rtrim(@grpname)) +'-' + ltrim(rtrim(str(@grpcounter))) + '-'+ ltrim(rtrim(str(isnull(@grpnow,0))))+'/'+ ltrim(rtrim(str(isnull(@grprooms,0)))) +'-'+rtrim(ltrim(@voucherno)) + case when ltrim(rtrim(@voucherno))<>'' then '-' else '' end +rtrim(ltrim(@name))

  --   print @desc goto e_error


if @split='Y'
	select @details= 'Split Bill No ' + ltrim(str(@bno)) +  ' ' + @desc + ' Sys BR.No.' + ltrim(str(@brno)) + ' GR No:' + ltrim(str(@eno)) + ' R.No.' + ltrim(str(@rno))
else
	select @details= + 'Bill No ' + ltrim(str(@bno))+ ' ' + @desc +  '-Sys No.' + ltrim(str(@brno)) + ' GR No:' + ltrim(str(@eno))+ ' R.No.' + ltrim(str(@rno))
if @mop=4 set @details = @details + '-Put in Sys.No '  + ltrim(str(@glactno))  --' Room Account
--print 'periya' + @split
--goto e_error

if @split <> 'C' and @split <> 'F'
begin
	/*Inserted on  29-Mar-2009 to check the debits on debit balance and act acroding to the setting */
		if ((@ctotal+@foodbill+@roomdues)-(@advance+@spldiscount))  > 0
		begin
	if @mop=3 and @company=1
	begin
		declare @allowcredit int
		select @allowcredit=allowcredit from setting
		if @allowcredit=0
		begin
			if exists(select * from glprimld where company=1 and fy=@fy and flag=3 and cgp_actno=@glactno and allowcredit='N' and (balance-((@ctotal+@foodbill+@roomdues)-(@advance+@spldiscount))) < 0)
			begin 
				print 'No Debits are allowed to this Account'
				goto e_error
			end
		end
	end
end


	declare @pcon varchar(100)
	if @mop=4  --modified on 27-03-2009
	begin
		if @updbillhist<>0
		begin
			if exists(select * from roomdues where cno=@cno)
			begin
				update roomdues set amount=0 where cno=@cno
				if @@error <> 0 goto e_error
				exec pproomdue @cno
				if @@error <> 0 goto e_error
				delete from roomdues where cno=@cno
			end
		end
		insert into roomdues (gno,cno,amount,usernm,company,split,txndt) values(@glactno,@cno,((@CTOTAL+@foodbill+@roomdues)-(@ADVANCE+@spldiscount)) ,@usernm,@company,@split,@txndt)
		if @@error <> 0 goto e_error
		if @updbillhist<>0 begin exec pproomdue @cno if @@error<>0 goto e_error end
	end
end
if exists(select * from setting where g_finaccount=0) begin commit transaction return end  -- Stops for PowerHms-Lite

if @split <> 'F'
begin
	if @company=1
	begin
/*
		if @rent<>0
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,15,@rent, 'BY ' + @details ,'C',@txndt,@grno,0,'Y','COUT')
		if @@error <>0 goto e_error
		end
		if @rentNT<>0
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,80,@rentNT, 'BY ' + @details ,'C',@txndt,@grno,0,'Y','COUT')
		if @@error <>0 goto e_error
		end
*/
if cursor_status('global','c123') > 0
begin
close c123
deallocate c123
end
declare @ltamt1 money

declare c123 cursor for select sum(rent) amt,sum(lt) lt,lrate from billhist where cno=@cno group by lrate
open c123
fetch next from c123 into @rent,@ltamt1,@lrate
while @@fetch_status=0
begin
if @rent <> 0
begin
-----------------------------------------------------------------------------------------------------------
if exists (select * from rentaccount where lrate=@lrate)                          --
	select @rentac=actnum from rentaccount where lrate=@lrate    --
else                                                                                                  --  
	if @ltamt1<>0                                                                        --  
		select @rentac=15                                                     --
	else                                                                                       --   
		select @rentac=80                                                     --
-----------------------------------------------------------------------------------------------------------
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,@rentac,@rent, 'BY ' + @details ,'C',@txndt,@grno,0,'Y','COUT')
		if @@error <>0 goto e_error


end
fetch next from c123 into @rent,@ltamt1,@lrate
end
close c123
deallocate c123






		if @discount <> 0
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,13,@discount, 'TO ' + @details, 'D',@txndt,@grno,0,'Y','COUT')
		if @@error<>0 goto e_error
		end
	end
	if @spldiscount <> 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,63,@spldiscount,'TO ' + @details   ,'D',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @foodbill <> 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,58,@foodbill, 'TO ' + @details + ' Food Bills-external'  ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @lt<>0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,47,@lt, 'BY '+@details   ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @sc<>0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,49,@sc, 'BY ' + @details   ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @st<>0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,75,@st, 'BY ' + @details   ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @cess<>0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,76,@cess, 'BY ' + @details   ,'C',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
	if @advance<>0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@FY,50,@advance, 'TO ' +@details   ,'D',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
end --- this is for split<>'C' 

if @roomdues<>0
begin
	if @roomdues > 0
	begin
		insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values(@company,@fy,67,@roomdues,'By ' + @details ,'C',@txndt,@grno,0,'Y','COUT')
		if @@error <>0 goto e_error
	end
	else
	begin
		insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values(@company,@fy,67,abs(@roomdues),'To ' + @details ,'D',@txndt,@grno,0,'Y','COUT')
		if @@error <>0 goto e_error
	end
end
---lk
if @company=1 and @mop=3 
	if exists(select * from linker where company=1 and actnum=@glactno)
		select @linkno=linkid from linker where company=1 and actnum=@glactno
	else
	begin
		select @linkno=max(linkid) from linker
		set  @linkno = isnull(@linkno,0)+1
		insert into linker (company,linkid,actnum) values (1,@linkno,@glactno)
		if @@error<>0 goto e_error
	end
if @company<>1 and @mop=3
	if not exists(select * from linker where company=@company and actnum=@glactno)
		if @linkno is not null
			insert into linker (company,linkid,actnum) values (@company,@linkno,@glactno)
----lk end 
       
set @actnum = case when @mop=0 then 1 when @mop = 1 then 22     when @mop=2  then 46    when @mop=3 then @glactno     when @mop=4 then 67     else 0 end 
if @actnum=0 goto e_error
if @mop=4 and @updbillhist=0
	if not exists(select * from room where gno=@glactno)
	begin
		print 'The requested room has already checked out....Settlement Aborted' goto e_error
	end
if @mop=4 and @updbillhist<>0
	if exists(select * from billhist1 where brno=@glactno and company=@company and split=@split and mop=4)
	begin
		print 'The Room Account Debited for this bill is also checked out through other Room Account' +nchar(13)+ 'Nested Deletion is not possible' + nchar(13) + 'First Change the Mode of Payment of the Parent Bill' + nchar(13) + 'and then try modifying this bill'
		goto e_error
	end
if @split <> 'C' and @split <> 'F'
begin
set @split=@split
/*	
	if ((@ctotal+@foodbill+@roomdues)-(@advance+@spldiscount))  > 0
	begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode)
		values (@company,@fy,@actnum,(@ctotal+@foodbill+@roomdues-(@advance+@spldiscount)) , 'TO '+@details   ,'D',@txndt,@grno,0,'Y')
		if @@error <> 0 goto e_error
	end
	IF ((@advance+@spldiscount)-(@ctotal+@foodbill+@roomdues)) > 0
	begin
		if @actnum=22  set @actnum=68
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode)
		values(@company,@fy,@actnum,(@advance+@spldiscount)-(@ctotal+@foodbill+@roomdues) , 'BY ' +@details + ' refund'   ,'C',@txndt,@grno,0,'Y')
		if @@error <> 0 goto e_error
	end
*/
end

else if  @split = 'C'
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,26,(@ctotal+@foodbill+@roomdues) , 'BY ' + @details  ,'D',@txndt,@grno,0,'Y','COUT')
	if @@error <> 0 goto e_error
	end
declare @outlet int
declare @olco int
declare @selfdeal char(1)
declare @hno_mybook int 
declare @hno_htlbook int
declare @tfy int
if exists (select * from billhist where cno=@cno) 
if @split<>'F'
begin
	set @a=1
	while @a<=10
	begin
		-- print 'periyakaruppa 1111'
		if @a=1 begin set @amt=@item1 set @typeoutlet=@ol1 end
		if @a=2 begin set @amt=@item2 set @typeoutlet=@ol2 end
		if @a=3 begin set @amt=@item3 set @typeoutlet=@ol3 end
		if @a=4 begin set @amt=@item4 set @typeoutlet=@ol4 end
		if @a=5 begin set @amt=@item5 set @typeoutlet=@ol5 end
		if @a=6 begin set @amt=@item6 set @typeoutlet=@ol6 end
		if @a=7 begin set @amt=@item7 set @typeoutlet=@ol7 end
		if @a=8 begin set @amt=@item8 set @typeoutlet=@ol8 end
		if @a=9 begin set @amt=@item9 set @typeoutlet=@ol9 end
		if @a=10 begin set @amt=@item10 set @typeoutlet=@ol10 end
		if @amt > 0
			if @typeoutlet > 300
			begin 
				set @outlet = @typeoutlet-300
				select @olco= company,@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook from toutlet where outlet = @outlet
				if @olco=@company
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (@company,@fy,@hno_mybook,@amt, 'BY '+@details  ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
				if @selfdeal <> 'Y' and @olco > 1
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (1,@fy,24,@amt, 'BY ' + @details    ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
			end
			else
			if @typeoutlet > 200
			begin 
				set @outlet = @typeoutlet - 200
				select @olco = company,@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook from co_phone_ol where outlet = @outlet
				if @olco=@company
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (@company,@fy,@hno_mybook,@amt, 'BY ' + @details  ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
				if @selfdeal <> 'Y' and @olco > 1
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (@olco,@fy,24,@amt, 'BY ' + @details   ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
			end
			else
			begin
				set @outlet = @typeoutlet - 100
				select @olco=cocode,@hno_htlbook=htl_receivable from co_rest_ol1 where rcode=@outlet    --- do not change rcode 
				--print 'periya karuppu 111' + str(@outlet)  + str(@a)  + '  '+ str(@ol1) + str(@olco)+ '  '+str(@company)
				--- goto e_error  
				IF @olco=@company
				begin
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (@company,@fy ,       24        ,   @amt, 'BY ' +@details  ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
				else
				begin
					exec @tfy=getfy @txndt,1,@tfy out
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
					values (1,@tfy, @hno_htlbook   ,@amt, 'BY ' + @details  ,'C',@txndt,@grno,0,'Y','COUT')
					if @@error <> 0 goto e_error
				end
			end 
 	set @a  = @a + 1
	end
end
--print convert(Varchar(20),((@ctotal+@foodbill+@roomdues)-(@advance+@spldiscount)))
--goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return



GO
/****** Object:  StoredProcedure [dbo].[PermitKOt]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[PermitKOt] @outlet int,@cnt int out ,@msg varchar(500) out,@dt datetime out
/* with encryption */
AS
BEGIN
	declare @txndt datetime;
	declare @g_kotfinaltime int;
	select @g_kotfinaltime=g_kotfinaltime from setting
	SET NOCOUNT ON;
	set @txndt= getdate()
	select @txndt=curdate from outlet where code=@outlet
select @cnt=0
select @msg = 'Permitted'
set @dt = @txndt
while datediff(day,@txndt,getdate()) > 0 
begin
if datediff(day,@txndt,getdate())=1 
	begin
---print convert(varchar(20),@txndt)   +  ' ' + str(datediff(day,@txndt,getdate())) + convert(varchar(20),getdate()) 

		if  datediff(Minute, convert(varchar(20),getdate(),101),getdate())  > @g_kotfinaltime
			begin
				if exists(select * from currentkot where outlet=@outlet)
					begin
					set @msg = 'KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
					select @cnt=1
					end   
				else
					begin
					update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
					end
			end
		else 
		begin
		goto ok
		end
	end
else
	begin
			if exists(select * from currentkot where outlet=@outlet)
				begin
				set @msg = 'KOT entering not allowed for ' + convert(varchar(20),@txndt,1) +' after ' +  ltrim(str(floor(@g_kotfinaltime/60)))+':'+ltrim(str(@g_kotfinaltime%60)) + 'am of ' + convert(varchar(20),getdate(),1) + ' ...since there are some pending orders not billed '
				select @cnt=1
				end   
			else
				begin
				update outlet set curdate= convert(varchar(20),getdate(),101) where code=@outlet
				end
	end
    select @txndt=curdate from outlet where code=@outlet
	break
end --- new
ok:
END

GO
/****** Object:  StoredProcedure [dbo].[pproomdue]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.   Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[pproomdue]   @CNOo numeric   
/* with encryption */
AS
DECLARE @GNO INT
DECLARE @COMPANY INT
DECLARE @TXNDT DATETIME
DECLARE @GRNO INT
DECLARE @FY INT
DECLARE @RET_VAL INT
DECLARE @AMOUNT MONEY
declare @guestname varchar(50)
declare @ano int
declare @eno int
declare @split char(1)
declare @t1 varchar(50)
declare @t2 varchar(50)
declare @foodbill money
declare @spldisc money
declare @crname varchar(50)
declare @crnum varchar(20)
declare @crtype varchar(50)
declare @glactno numeric
declare @mop int
declare @bcompany numeric
declare @mainbilldate datetime
declare @sql varchar(5000)
declare @rv varchar(10)
--
declare @rent money
declare @discount money
declare @item1 money
declare @item2 money
declare @item3 money
declare @item4 money
declare @item5 money
declare @item6 money
declare @item7 money
declare @item8 money
declare @item9 money
declare @item10 money
declare @outlet1 int
declare @outlet2 int
declare @outlet3 int
declare @outlet4 int
declare @outlet5 int
declare @outlet6 int
declare @outlet7 int
declare @outlet8 int
declare @outlet9 int
declare @outlet10 int
declare @sc money
declare @lt money
declare @total money
declare @taxable money
declare @section char(1)
declare @usernm varchar(50)
declare @cno numeric 
declare @brno numeric
if not exists(select * from billhist1 where cno=@cnoo) return
if not exists(select * from roomdues where cno=@cnoo) return
select @gno=gno from roomdues where cno=@cnoo
select @split=split,@company=company from billhist1 where cno=@cnoo
select @brno=brno from guest where gno=@gno
if not exists(select * from billhist1 where brno=@brno and company=@company and split=@split) return
select @cno=cno from billhist1 where brno=@brno and company=@company and split=@split
BEGIN TRANSACTION
--print 'periya karupa'
select @mainbilldate=txndt,@grno=grno,@company =company from billhist1 where cno=@cno
select @gno=min(gno) from billhist where cno=@cno
set @txndt=@mainbilldate
exec @fy=getfy @txndt,@company,@fy
if @@error<>0 goto e_error
exec delgrno @company,@fy,@grno,@mainbilldate,@ret_val out
if @@error<>0 goto e_error
exec pbvc @cno,@cno
if @@error <> 0  goto e_error
select @eno=eno,@guestname=name from guest_view1 where gno =@gno
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
set @sql='Due to deletion/Modification of its Room Due Bill' 
insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm,'Bill Modified - Guest Name ' + rtrim(@guestname) + ' Entry Number ' + ltrim(str(@eno)) +  ' dt ' + convert(varchar(20),@txndt,6) +'-'+ @sql ,10,@gno)
if @@error <> 0  goto e_error
set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'') drop table ' + @T1
exec(@sql)
if @@error<>0 goto e_error
set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T2 +''' and xtype = ''U'') drop table ' + @T2
exec(@sql)
if @@error<>0 goto e_error
COMMIT TRANSACTION
RETURN
E_ERROR:
ROLLBACK TRANSACTION
set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'') drop table ' + @T1
exec(@sql)
if @@error<>0 goto e_error
set @sql  = 'if exists (select name,xtype from sysobjects where name = ''' + @T2 +''' and xtype = ''U'') drop table ' + @T2
exec(@sql)
if @@error<>0 goto e_error
RAISERROR('PROBLEM IN DELETING ADVANCE RECEIPT ENTRY',16,-1)
RETURN




GO
/****** Object:  StoredProcedure [dbo].[PrePaidStockPosting]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 14 July 2013
-- Description:	Prepaid coupon stock entry
-- =============================================
CREATE PROCEDURE [dbo].[PrePaidStockPosting]
@cno numeric,
@series varchar(10),
@edt datetime,
@fromno numeric,
@tono numeric,
@denomination decimal(18,2),
@discount decimal(18,2),
@finalamount decimal(18,2),
@usernm varchar(20),
@mop int,
@crname varchar(50),
@crnum varchar(50),
@crbank varchar(50),
@glactno numeric
/* with encryption */
AS
BEGIN
print 'ok'
--SET NOCOUNT ON;
--begin transaction
--declare @ctrlno numeric
--insert into prepaidcoupons 
--(cno,series,sdt,edt,fromno,tono,denomination,discount,finalamount,usernm,mop,crname,crnum,crbank,glactno) 
--values 
--(@cno,@series,getdate(),@edt,@fromno,@tono,@denomination,@discount,@finalamt,@usernm,@mop,@crname,@crnum,@crbank,@glactno)
--if @@error<>0 goto e_error
--set @ctrlno= ident_current('prepaidcoupons')

--declare @coupontype int
--declare @company numeric
--declare @fy numeric
--declare @txndt datetime
--declare @name varchar(100)
--select @name=name,@company=company,@coupontype=coupontype from prepaidcouponmaster where ctrlno=@cno
--if @coupontype=1
--	begin
--	declare @grno numeric
--	set @txndt = getdate()
--	exec @fy=getfy @txndt,@company,@fy out
--	if @@error<>0 goto e_error
--	exec getgrp @txndt,@company,@grno out
--	if @@error <>0 goto e_error

--	set @ACTNO = @cno
--	set @AMT = @finalamt
--	set @AMTTYP = 'C'
--	set @DESC = 'By Coupon No' + @series + ' ' + ltrim(str(@fromno)) + '-' + ltrim(str(@tono))
--	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
--	(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','TRFR',@usernm)
--	if @@error <> 0 goto e_error

--	set @actno = case 
--		when @mop=0 then 1
--		when @mop=1 then 22
--		when @mop=2 then 46
--		when @mop=3 then @glactno 
--		end
--    set @amttyp = 'D'
--	set @DESC = 'To ' + ltrim(rtrim(@name)) + '-Coupon No' + @series + ' ' + ltrim(str(@fromno)) + '-' + ltrim(str(@tono))
--	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
--	(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','TRFR',@usernm)
--	if @@error <> 0 goto e_error
--update prepiadcoupons set grno=@grno where ctrlno=@ctrlno
--	end
--commit transaction
--return
--E_Error:
--rollback transaction
--raiserror('Error in Pre Paid Coupon Stock Posting',16,-1)
--return	
END



GO
/****** Object:  StoredProcedure [dbo].[PrePaidStockPostingProc]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 14 July 2013
-- Description:	Prepaid coupon stock entry
-- =============================================
CREATE PROCEDURE [dbo].[PrePaidStockPostingProc]
@cno numeric,
@series varchar(10),
@edt datetime,
@fromno numeric,
@tono numeric,
@denomination decimal(18,2),
@discount decimal(18,2),
@finalamount decimal(18,2),
@usernm varchar(20),
@mop int,
@crname varchar(50),
@crnum varchar(50),
@crbank varchar(50),
@glactno numeric
/* with encryption */
AS
BEGIN
SET NOCOUNT ON;
begin transaction
declare @ctrlno numeric
insert into prepaidcoupons 
(cno,series,sdt,edt,fromno,tono,denomination,discount,finalamount,usernm,mop,crname,crnum,crbank,glactno) 
values 
(@cno,@series,getdate(),@edt,@fromno,@tono,@denomination,@discount,@finalamount,@usernm,@mop,@crname,@crnum,@crbank,@glactno)
if @@error<>0 goto e_error
set @ctrlno= ident_current('prepaidcoupons')

declare @coupontype int
declare @company numeric
declare @fy numeric
declare @txndt datetime
declare @name varchar(100)
declare @actno numeric
declare @desc varchar(200)
declare @amt decimal(18,2)
declare @amttyp char(1)
declare @glac numeric
select @glac=glactno,@name=name,@company=company,@coupontype=coupontype from prepaidcouponmaster where ctrlno=@cno
if @coupontype=0
	begin
	declare @grno numeric
	set @txndt = getdate()
	exec @fy=getfy @txndt,@company,@fy out
	if @@error<>0 goto e_error
	exec getgrp @txndt,@company,@grno out
	if @@error <>0 goto e_error

	set @ACTNO = @glac
	set @AMT = @finalamount
	set @AMTTYP = 'C'
	set @DESC = 'By Coupon No' + @series + ' ' + ltrim(str(@fromno)) + '-' + ltrim(str(@tono))
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
	(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','TRFR',@usernm)
	if @@error <> 0 goto e_error

	set @actno = case 
		when @mop=0 then 1
		when @mop=1 then 22
		when @mop=2 then 46
		when @mop=3 then @glactno 
		end
    set @amttyp = 'D'
	set @DESC = 'To ' + ltrim(rtrim(@name)) + '-Coupon No' + @series + ' ' + ltrim(str(@fromno)) + '-' + ltrim(str(@tono))
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
	(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','TRFR',@usernm)
	if @@error <> 0 goto e_error
update prepaidcoupons set grno=@grno where ctrlno=@ctrlno
	end
commit transaction
return
E_Error:
rollback transaction
raiserror('Error in Pre Paid Coupon Stock Posting',16,-1)
return	
END



GO
/****** Object:  StoredProcedure [dbo].[qcheckres]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[qcheckres] @rno int,@name varchar(50) ,@grpname varchar(50),@ctrlno numeric ,@resadv money,@myctrlno numeric ,@chargeextrabed tinyint = 1
/* with encryption */
AS
declare @usernm varchar(50)
declare @userno numeric
--declare @myctrlno numeric
declare @gno numeric
declare @ctrlno1 numeric
declare @narration varchar(100)
declare @cno numeric
declare @tempadv money
declare @company numeric
-- added on 26 Oct 2008
declare @cch_crnum varchar(20)
declare @cch_crname varchar(50)
declare @cch_crbank varchar(50)
declare @cch_refno varchar(50)
declare @cch_amount money
declare @cch_duedt datetime
declare @cch_ctrlno numeric
declare @ci_date datetime
declare @expecteddep datetime
declare @arrivaldate datetime
declare @memo varchar(200)
declare @mktmgr numeric
declare @channel varchar(100)
declare @ischannel int
declare @chglactno numeric
declare @channelamt money
declare @staahid varchar(100)
declare @extrabedcharges money
declare @gstin nvarchar(50)
declare @stopservice tinyint 

set @ci_date = getdate()
set @arrivaldate = getdate()
select @mktmgr=mktmgr,@expecteddep = departuredate,@memo=isnull(memorandum,'')  from sptbrs_v1 where myctrlno=@myctrlno
--print convert(varchar(20),@ctrlno) + ' ' + convert(varchar(20),@myctrlno)
--raiserror('erpr',16,-1)
--return
begin transaction
select @cno=cno,@channel=channel,@ischannel=ischannel,@channelamt=channelamt,@staahid=staahid,
@extrabedcharges=ExtraBedCharges,@gstin=gstin from sptbrs where ctrlno=@ctrlno

if @chargeextrabed = 0
begin
set @extrabedcharges = 0
end

select @userno=userno, @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
if not exists(select * from servicepermission where userno=@userno and permission='Y' and serviceno=1)
  begin print @usernm +'!  You are not permitted to user this feature' goto e_error end
if exists(select * from sptbrs where ctrlno=@ctrlno and advancerecd-isnull(advanceadjd,0)-isnull(refundamount,0) < @resadv)
begin
select @tempadv= advancerecd-isnull(advanceadjd,0)-isnull(refundamount,0)
 from sptbrs where ctrlno=@ctrlno 
print 'Advance to adjust is greater than the available amount in advance' + convert(varchar(20),@resadv) + ' ' +convert(varchar(20),@tempadv )
goto e_error
end

if exists(select * from sptbrooms_v  where rno=@rno and datediff(d,getdate(),arrivaldate) >= 0 and status1 <> 'Y'
and (
(datediff(s,'1/1/90',@expecteddep)-1  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)-1  or
datediff(s,'1/1/90',@ci_date)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)-1) or 
(datediff(s,'1/1/90',@ci_date) < datediff(s,'1/1/90',departuredate) and datediff(s,'1/1/90',@expecteddep) >= datediff(s,'1/1/90',departuredate))
) and myctrlno<> @myctrlno)
begin
declare @ctrl numeric
select @ctrl=ctrlno_o from sptbrooms_v  where rno=@rno  and datediff(d,getdate(),arrivaldate) >= 0 and status1 <> 'Y'
and (
(datediff(s,'1/1/90',@expecteddep)-1  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)-1  or
datediff(s,'1/1/90',@ci_date)  between  datediff(s,'1/1/90',arrivaldate)  and datediff(s,'1/1/90',departuredate)-1) or 
(datediff(s,'1/1/90',@ci_date) < datediff(s,'1/1/90',departuredate) and datediff(s,'1/1/90',@expecteddep) >= datediff(s,'1/1/90',departuredate))
)
print  'Server Message: This room (' + ltrim(str(@rno)) + ') is blocked for the period during which the expected departure date of this guest falls' + char(13) + 'Check - in aborted - This is a SQL server messsage ' + convert(varchar(20),@expecteddep) + '  ' +  convert(varchar(20),@ci_date)  + '  Reservation ref. No.' + ltrim(str(@ctrl))
goto e_error
end

declare @nor numeric
select @tempadv= advancerecd-isnull(advanceadjd,0)-isnull(refundamount,0)
 from sptbrs where ctrlno=@ctrlno 

select @nor=sum(noofrooms-arrivedrooms-cancelledrooms) from sptbrs1 where ctrlno=@ctrlno

--print str(@nor) + ' ' + convert(varchar(20),@tempadv) + ' ' + convert(varchar(20),@resadv)

---goto e_error

if @nor=1 and @tempadv > 0 and @tempadv <> @resadv
begin
print 'The advance amount is not fully utilized' goto e_error
end
declare @fptype int 
select @fptype=fptype from sptbrs1 where myctrlno=@myctrlno
select @company=isnull(company,0) from sptbrs where ctrlno=@ctrlno
if @company<> 0 select @stopservice=isnull(stopservice,0) from gcompany where comno=@company 
set @stopservice=isnull(@stopservice,0)

if not exists(select * from room where rno=@Rno and gno=0) goto e_error
insert into guest (
qname,cno,rno,foodplan,userchkin,qcheck,grpname,company,name,expecteddep,mktmgr,
channel,ischannel,channelamt,staahid,ExtraBedCharges,gstin,stopservice)  
values (
@name,@cno,@rno,@fptype,@usernm,'Y',@grpname,@company,@name,@expecteddep,@mktmgr,
@channel,@ischannel,@channelamt,@staahid,isnull(@extrabedcharges,0),@gstin,@stopservice)
set @gno=@@identity
update room set GNO = @gno,Qcheck='Y',grpname=@grpname where rno=@rno 
--select top 1  @myctrlno=myctrlno from sptbrs1 where ctrlno=@ctrlno and noofrooms-arrivedrooms > 0
--print str(@myctrlno) + ' Periya' goto e_error
update sptbrs1 set arrivedrooms=arrivedrooms+1 where myctrlno = @myctrlno
if @@error <>0 goto e_error
if exists(select * from sptbrooms where rno=@rno and myctrlno=@myctrlno)
begin
	update sptbrooms set status1='Y' where rno=@rno and myctrlno=@myctrlno
	if @@error <>0 goto e_error
end
if @chargeextrabed=1 
update sptbrs set extrabedalloted=isnull(extrabedalloted,0)+1 where ctrlno=@ctrlno
 
--print convert(varchar(20),@ctrlno) + ' ' + convert(varchar(20),@myctrlno)
--goto e_error
update guest set reserve_ref=@myctrlno where gno=@gno
if @@error<>0 begin print 'Error in Reservation Ref ctrlno updation' goto e_error end
if @resadv<>0
begin
	set @narration = 'Ref.No' + ltrim(str(@ctrlno)) + '/' + ltrim(str(@myctrlno)) + ' Adv.ag. Res. No. ' + ltrim(str(@ctrlno))
	update sptbrs set advanceadjd=advanceadjd+@resadv where ctrlno=@ctrlno
	if @@error<>0 begin print 'Error in updateting the advance adjusted' goto e_error end
	exec pbpa1u @gno,@resadv,1,4,'','','',56,'N',@ctrlno1 out,@narration
	if @@error<>0 begin print 'Error in advance updating'  goto e_error end
	update guest set reserve_adv_cno=@ctrlno1,reserve_adv=@resadv where gno=@gno
	if @@error<>0 begin print 'Error in advance ctrlno updation' goto e_error end
end

select @cch_amount=sum(amount) from crcardhold where holdtype=2 and gno=@ctrlno and status=' '
if isnull(@cch_amount,0) > 0 
begin
declare c1 cursor for select ctrlno,crnum,crname,crbank,refno,duedt,amount from crcardhold where gno=@ctrlno and holdtype=2 and status=' '
open c1
fetch next  from c1 into @cch_ctrlno,@cch_crnum,@cch_crname,@cch_crbank,@cch_refno,@cch_duedt,@cch_amount
while @@fetch_status=0
	begin
		insert into crcardhold (holdtype,gno,crnum,crname,crbank,amount,duedt,refno,txndt,usernm)
		values 
		(1,@gno,@cch_crnum,@cch_crname,@cch_crbank,@cch_amount,@cch_duedt,@cch_refno,getdate(),@usernm)
		if @@error<>0 goto e_error
		update crcardhold set status='M' where ctrlno=@cch_ctrlno
		fetch next  from c1 into @cch_ctrlno,@cch_crnum,@cch_crname,@cch_crbank,@cch_refno,@cch_duedt,@cch_amount
	end
close c1
deallocate c1
end

if @memo<>''
begin
declare @memo1 varchar(100)
declare @memo2 varchar(100)
set @memo = substring(ltrim(rtrim(@memo)) + space(200),1,200)
set @memo1 = ltrim(rtrim(substring(@memo,1,100)))
set @memo2 = ltrim(rtrim(substring(@memo,101,100)))
delete from gm_master where gno=@gno
insert into gm_master (gno,txndt,usernm) values (@gno,getdate(),'RES')
if @@error<>0 begin print 'Error in updating Memorandum' goto e_error end
declare @gm_ctrlno numeric
set @gm_ctrlno = ident_current('gm_master')
insert into guest_memo (ctrlno_ref,details) values (@gm_ctrlno,@memo1)
if @memo2<>'' insert into guest_memo (ctrlno_ref,details) values (@gm_ctrlno,@memo2)
end


commit transaction
return
e_error:
rollback transaction
set @narration = 'Error in Quick Checkin : ' --'The Room ' + str(@rno) + ' is not vacant now .. Check-In Failed'
raiserror(@narration,16,-1)
 return




GO
/****** Object:  StoredProcedure [dbo].[QRNO]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <01 01 2015>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[QRNO] @dt datetime,@expecteddep datetime
/* with encryption */
AS
BEGIN
	While DateDiff(d,@DT, @expecteddep) >= 0
	begin
		if not exists (select * from staahinv where status='' and datediff(d,txndt,@dt) = 0) insert into staahinv (txndt) values (@DT)
		if not exists (select * from crsinv where status='' and datediff(d,txndt,@dt) = 0) insert into crsinv (txndt,status,message) values (@DT,'','')
		set @dt = dateadd(d,1,@dt)
	end
END

GO
/****** Object:  StoredProcedure [dbo].[RANDFILE]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.RANDFILE    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.randfile    Script Date: 1/20/03 11:51:46 AM ******/
CREATE PROCEDURE [dbo].[RANDFILE] @ret_val  varchar(10) out  
/* with encryption */
AS
declare @a varchar(10)
declare @b varchar(10)
set @a = ltrim(str(convert(int,rand()*100000)%10000))
set @ret_val = 'XXX' + char(str(65 + (convert(int,rand()*1000)%26))) + char(str(65 + (convert(int,rand()*1000)%26))) + char(str(65 + (convert(int,rand()*1000)%26)))+ @a + char(str(65 + (convert(int,rand()*1000)%26)))




GO
/****** Object:  StoredProcedure [dbo].[RemoveColumnWithDefaultConstraints]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[RemoveColumnWithDefaultConstraints] 
    -- Add the parameters for the stored procedure here
    @tableName nvarchar(4000), 
    @columnName nvarchar(4000)
/* with encryption */
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    DECLARE @ConstraintName nvarchar(200)
    SELECT @ConstraintName = Name 
    FROM SYS.DEFAULT_CONSTRAINTS 
    WHERE PARENT_OBJECT_ID = OBJECT_ID(@tableName) 
        AND PARENT_COLUMN_ID = (SELECT column_id FROM sys.columns WHERE NAME = (@columnName) 
        AND object_id = OBJECT_ID(@tableName))
    IF @ConstraintName IS NOT NULL
        EXEC('ALTER TABLE ' + @tableName + ' DROP CONSTRAINT ' + @ConstraintName)

    IF EXISTS(SELECT * FROM sys.columns WHERE Name = @columnName  
        AND Object_ID = Object_ID(@tableName))
        EXEC('ALTER TABLE ' + @tableName + ' DROP COLUMN ' + @columnName)       
END

GO
/****** Object:  StoredProcedure [dbo].[removeduplicatesfromlinker]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[removeduplicatesfromlinker]
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @fy numeric
	declare @actnum numeric
	declare @ctrlno numeric
	declare c1 cursor for select fy,actnum,ctrlno from linker
	open c1
	fetch next from c1 into @fy,@actnum,@ctrlno
	while @@fetch_status=0
	begin

	delete from linker where fy=@fy and actnum=@actnum and ctrlno <> @ctrlno

	fetch next from c1 into @fy,@actnum,@ctrlno
	end
	close c1
	deallocate c1
END

GO
/****** Object:  StoredProcedure [dbo].[RemoveSplit]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[RemoveSplit] @company numeric, @dt datetime
AS
BEGIN
	SET NOCOUNT ON
	declare @brno numeric(20,0) 

    	delete from billhist where cno in (select cno from billhist1 where company=@company and DATEDIFF(D,TXNDT,@DT) >= 0 and split='Y')
		delete from billhist1 where company=@company and DATEDIFF(D,TXNDT,@DT) >= 0 and split='Y'

END
GO
/****** Object:  StoredProcedure [dbo].[REWRITE]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.REWRITE    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.rewrite    Script Date: 1/20/03 11:51:48 AM ******/
CREATE PROCEDURE [dbo].[REWRITE] @company int,@fy int 
/* with encryption */
AS
declare @date datetime
declare @sql varchar(5000)
declare @x int
declare @amt money
declare @actnum int
set nocount on
select @date=date from finyear where company = @company  and fy = @fy
begin transaction
set @sql = 'declare tempc cursor for select  cgp_actno from glprimld where company = ' + str(@company) + ' and fy = '  + str(@fy)
exec(@sql)
open tempc
fetch next from tempc into @actnum
while @@fetch_status = 0
begin
      set @x = 1 
      while @x <= 12
            begin
             select @amt = 0
              select  @amt = sum(amt) from vouchers where company = @company and fy = @fy  and actnum = @actnum and  amttyp = 'D'  and  datepart(mm,txndt) = @x  group by company,fy,actnum,amttyp,datepart(mm,txndt)
 set @sql = 'update glprimld set debit' + lTrim(Str(@x)) + ' = ' + convert(varchar(20),@amt) + ' where company = ' + Str(@company) + ' and fy = ' + Str(@fy) + ' and cgp_actno = ' + Str(@actnum)
 exec(@sql)
             select @amt = 0
             select  @amt = sum(amt) from vouchers where company = @company and fy = @fy  and actnum = @actnum and  amttyp = 'C'  and  datepart(mm,txndt) = @x  group by company,fy,actnum,amttyp,datepart(mm,txndt)
 set @sql = 'update glprimld set credit' + lTrim(Str(@x)) + ' = ' + convert(varchar(20),@amt) + ' where company = ' + Str(@company) + ' and fy = ' + Str(@fy) + ' and cgp_actno = ' + Str(@actnum)
 exec(@sql)
             set @x = @x + 1
           end
 Exec fillbal  @date,@company,@actnum,1
 update comuser set chktime=getdate() where [host_id]=host_id()
 fetch next from tempc into @actnum
end
close tempc
deallocate tempc
commit transaction




GO
/****** Object:  StoredProcedure [dbo].[REWRITE_new]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.REWRITE    Script Date: 15/02/2004 6:23:00 PM ******/
/****** Object:  Stored Procedure dbo.rewrite    Script Date: 1/20/03 11:51:48 AM ******/
/******  rewrite_new was created on 08-03-2009 1.30 am *****/
CREATE PROCEDURE [dbo].[REWRITE_new] @actnum numeric, @fy int,@fillbal int=0 
/* with encryption */
AS
declare @date datetime
declare @sql varchar(5000)
declare @x int
declare @amt money
declare @company numeric
declare @opgbal money
--declare @actnum int
set nocount on
select @date=date,@company=company  from finyear where  fy = @fy
--      set @x = 1 
--      while @x <= 12
--            begin
--             select @amt = 0
--              select  @amt = sum(amt) from vouchers where fy = @fy  and actnum = @actnum and  amttyp = 'D'  and  datepart(mm,txndt) = @x  group by fy,actnum,amttyp,datepart(mm,txndt)
-- set @sql = 'update glprimld set debit' + lTrim(Str(@x)) + ' = ' + convert(varchar(20),@amt) + ' where fy = ' + Str(@fy) + ' and cgp_actno = ' + Str(@actnum)
-- exec(@sql)
--             select @amt = 0
--             select  @amt = sum(amt) from vouchers where company = @company and fy = @fy  and actnum = @actnum and  amttyp = 'C'  and  datepart(mm,txndt) = @x  group by company,fy,actnum,amttyp,datepart(mm,txndt)
-- set @sql = 'update glprimld set credit' + lTrim(Str(@x)) + ' = ' + convert(varchar(20),@amt) + ' where  fy = ' + Str(@fy) + ' and cgp_actno = ' + Str(@actnum)
-- exec(@sql)
--             set @x = @x + 1
--           end

--if @fillbal=1 
--	BEGIN
	Exec fillbal  @date,@company,@actnum,@fillbal
--	END
--ELSE
--	BEGIN
--	UPDATE GLPRIMLD SET BALANCE=(OPGBAL+(SELECT SUM(CASE WHEN AMTTYP='C' THEN AMT ELSE -AMT END) FROM VOUCHERS WHERE ACTNUM=@ACTNUM AND FY=@FY) ) where fy =@fy and cgp_actno =  @actnum
--	END
update comuser set chktime=getdate() where [host_id]=host_id()
return
e_error:
raiserror('error in rewriting',16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[SALEADJ]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/*3  may 2004*/
CREATE PROCEDURE [dbo].[SALEADJ] @t1 varchar(50) , @t2 varchar(50), @code numeric ,@percent numeric
/* with encryption */
 AS
declare  @COUNT numeric
declare @sql varchar(5000)
declare @qty numeric
declare @qty1 numeric
declare @code1 numeric
declare @tax1 money
declare @rate money
declare @rate1 money
declare @tochg numeric
declare @skip tinyint
declare @first tinyint
declare @tax money
declare @name varchar(100)
declare @skip1 tinyint
declare @bno numeric
declare @skip2 tinyint
begin transaction
set @sql = 'declare tc cursor for select qty from ' + @t1 + ' where code = ' + str(@code)
--print @sql
exec(@sql)
open tc
set @count=0
fetch next from tc into @qty
while @@fetch_status=0
begin
set @count = @count + @qty
fetch next from tc into @qty
end
close tc
deallocate tc
--print @count
--goto e_error
set @tochg = @count * @percent/100
set @skip = case when @percent < 25 then 3
                            when @percent >=25 and @percent < 50 then 2
                            when @percent >=  50 and @percent <=80 then 1
                            else 0 end
set @sql =  'declare tc1 cursor for select qty,rate,tax,bno  from ' + @t1 + ' where code='  + str(@code)
exec(@sql)
if @@error<>0 goto e_error
open tc1
fetch next from tc1 into @qty,@rate,@tax,@bno
set @skip1= 0
set @skip2=@skip*@qty
--print @t1  + ' ' + str(@code) goto e_error
while @@fetch_status=0
begin
--print 'periya'
--goto e_error
if @skip1=0
begin
set @first = 0
set @sql = 'declare tc2 cursor for select code,qty,rate from ' + @t2
exec(@sql)
if @@error<>0 goto e_error
open tc2
fetch next from tc2 into @code1,@qty1,@rate1
while @@fetch_status=0
begin
select @name=[name],@tax1=tax  from kitmast_v where code=@code1
if @first=0 
   begin
   set @sql = 'update ' + @t1 + ' set qty=' + str(@qty1*@qty) +',rate=' + convert(varchar(20),@rate1) +  ',name=''' + @name +   ''',code=' + str(@code1) + ',tax=' + convert(varchar(20),(@qty*@qty1*@rate1*@tax1)/100)+ ',strate='+ convert(varchar(20),@tax1) + ',grp=1000,amt=' + convert(varchar(20),@qty*@qty1*@rate1) + ' where current of tc1'
   exec(@sql)
   if @@error<>0 begin print 'Update failed' goto e_error end
   set @first=1
   end
else
   begin 
   set @sql = 'insert into ' + @t1 + ' (code,qty,rate,tax,name,amt,bno,discount,strate,kot,ctrlno,grp,grpname,iname,preference) values (' + str(@code1) + ',' + str(@qty1*@qty) + ',' +  convert(varchar(20),@rate1)  + ','  + convert(varchar(20),(@qty*@qty1*@rate1*@tax1)/100)   + ',''' + @name + ''',' + convert(varchar(20),@qty*@qty1*@rate1) + ',' + str(@bno) + ',0,'+convert(varchar(20),@tax1) + ',0,0,1000,'' '','' '','' '')'
   exec (@sql)
   if @@error<>0 begin print 'insert failed' goto e_error end
   end
   fetch next from tc2 into @code1,@qty1,@rate1
end
close tc2
deallocate tc2
end
set @skip1=@skip1+1
if @skip1 > @skip2 set @skip1=0
fetch next from tc1 into @qty,@rate,@tax,@bno
end
close tc1
deallocate tc1
commit transaction
return
e_error:
rollback transaction
raiserror('SaleAdj failed',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[saleadj1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/* 3 may 2004 ----- Ramanathan PowerBrain*/
CREATE PROCEDURE [dbo].[saleadj1] @t1 varchar(50),@userno numeric  
/* with encryption */
AS
declare @sql varchar(5000)
declare @code numeric
declare @rate money
declare @qty numeric
declare @strate money
declare @amt money
declare @tax money
declare @discount money
declare @kot numeric
declare @drate money
declare @ro money
declare @t2 varchar(50)
declare @rv varchar(50)
declare @bno numeric
declare @scamt money
declare @scrate money
declare @a1 money
declare @tx1 money
declare @d1 money
declare @a2 money
declare @a3 money
begin transaction
exec randfile @rv out
set @t2 = 'ks' + @rv
set @sql= 'declare tc cursor for select distinct bno from ' + @t1  + ' where grp=1000'
exec (@sql)
open tc
fetch next from tc into @bno
while @@fetch_status=0
 begin
 set @sql = 'declare tc1 cursor for select code,rate,qty,strate,amt,tax,discount,kot from ' + @t1 + ' where bno = ' + str(@bno)
 exec(@sql)
 open tc1
 fetch next from tc1 into @code,@rate,@qty,@strate,@amt,@tax,@discount,@kot 
 set @sql = 'if exists(select * from sysobjects where name=''' + @t2  +  ''' and xtype=''U'') drop table ' + @t2
 exec(@sql)
 set @sql = 'select * into ' + @t2 + ' from dummykit'
 exec (@sql)
 while @@fetch_status=0
  begin
  select @ro=roamt,@scrate=scrate,@drate=drate,@scamt=scamt from kithist1 where bno=@bno
                        set @discount = round(@qty*@rate*@drate/100,1)
                        set @tax = round(@qty*@rate*@strate/100,1)
  set @sql = 'insert into ' + @t2 + ' values (' + str(@code) + ','  + str(@qty) + ',' + convert(varchar(20),@rate) + ',' + convert(varchar(20),@strate) + ',' + convert(varchar(20),(@qty*@rate)) + ',' +convert(varchar(20),@tax) + ',' +  convert(varchar(20),@discount) + ',' + str(@kot) + ',0,0,'''')'
              exec(@sql)
  if @@error <> 0 goto e_error
  fetch next from tc1 into @code,@rate,@qty,@strate,@amt,@tax,@discount,@kot  
          --print 'periya' 
  end
----For finding out the Round Off Discount Amount (11-11-2005)
select * into #dumkit from dummykit
set @sql = 'insert into #dumkit select  *  from ' + @t2
exec(@sql)
select @a3=sum(amount),@d1=sum(discount),@tx1=sum(tax)  from #dumkit
drop table #dumkit
set @a1=(@a3+@tx1-@d1+@scamt) *100
set @a2=floor((@a3+@tx1-@d1+@scamt))*100
if @a1-@a2 > 0 and @a1-@a2 < 50    set @ro = ((@a1/100)-(@a2/100)) *-1 
if @a1-@a2 >= 50  set @ro=ceiling(@a1/100)-(@a1/100)
----
exec pbpk1106 @t2,@bno,@ro,@drate,@scamt,@scrate,@userno,1
if @@error<>0 goto e_error
close tc1
deallocate tc1
fetch next from tc into @bno
end 
close tc
deallocate tc
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[saleadj11]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) New Delhi*/
/* 3 may 2004 ----- Ramanathan PowerBrain*/
CREATE  PROCEDURE [dbo].[saleadj11] @t1 varchar(50),@userno numeric ,@sdt datetime, @edt datetime,@company int,@rcode int ,@outlet int 
/* with encryption */
AS
declare @sql varchar(5000)
declare @code numeric
declare @rate money
declare @qty numeric
declare @strate money
declare @amt money
declare @tax money
declare @discount money
declare @kot numeric
declare @drate money
declare @ro money
declare @t2 varchar(50)
declare @rv varchar(50)
declare @bno numeric
declare @scamt money
declare @scrate money
declare @ctrlno numeric
declare @dt datetime
declare @sale money
declare @roamt money
declare @total money
declare @orate money
declare @g_staxincl char
declare @roamtold money
declare @roamtnew money
declare @finalamtold decimal(18,2)
declare @finalamtnew decimal(18,2)
declare @finalamtdiff decimal(18,2)
declare @txndt datetime
declare @mamt as decimal(18,2)
declare @servicetax decimal(14,2)
declare @cess decimal (14,2) 
declare @tamt decimal(14,2)
declare @vatonst int
declare @st1 decimal(18,2)
declare @cess1 decimal(18,2)
declare @sc1 decimal(18,2)
declare @stset tinyint
declare @scset tinyint
declare @stincl decimal(18,2)
declare @scincl decimal(18,2)
declare @vatonsc tinyint
declare @cess2 decimal(18,2)
declare @cess21 decimal(18,2)

set @dt=@sdt
while datediff(d,'1/1/90',@dt) <= datediff(d,'1/1/90',@edt)
begin
begin transaction

if cursor_status('global','tc1') > 0
begin
close tc1
deallocate tc1
end

set @sql = 'declare tc1 cursor for select bno from ' + @t1 + ' where datediff(d,''1/1/90'',txndt) = datediff(d,''1/1/90'',''' + convert(varchar(20),@dt) + ''') group by bno'
exec(@sql)
open tc1
fetch next from tc1 into @bno
while @@fetch_status=0 
	begin
    select @outlet=outlet,@txndt=txndt,@finalamtold=finalamt from kithist1 where bno=@bno
    select @g_staxincl=g_staxincl from outlet where code=@outlet
	update kithist1 set scamt=0,servicetax=0,cess=0 where bno=@bno
	exec serviceadeleting @bno
	if @@error<>0 goto e_error

	if cursor_status('global','tc2') > 0
		begin
		close tc2
		deallocate tc2
		end

	set @sql = 'declare tc2 cursor for select code,rate,qty,strate,amt,tax,discount,kot,ctrlno,bno,orate from ' + @t1 + ' where bno=' + str(@bno)
	exec (@sql)
	open tc2

	fetch next from tc2 into @code,@rate,@qty,@strate,@amt,@tax,@discount,@kot ,@ctrlno,@bno,@orate
	while @@fetch_status=0
		begin

			select @ro=roamt,@scrate=scrate,@drate=drate,@scamt=scamt,@vatonst=vatonst,@vatonsc=vatonsc,@stset=stset,@scset=scset from kithist1 where bno=@bno
			if @qty=0
			begin
				delete from kithist where ctrlno=@ctrlno
 			end
			else
				begin

				--set @sql = 'update kithist set code=' + convert(varchar(20),@code) + ',qty=' + convert(varchar(20),@qty) + ',rate=' + convert(varchar(20),@rate) +' ,tax='+convert(varchar(20),@tax) +',discount= ' + convert(varchar(20),@discount) + ' where ctrlno=' + convert(varchar(20),@ctrlno) 

				--print @sql goto e_error
				update kithist set code=@code,qty=@qty,rate=@rate,tax=@tax,discount=@discount where ctrlno=@ctrlno
				if @@error <> 0 goto e_error

					select @amt=@qty*@rate
					if @drate<> 0 set @discount = round(@qty*@rate*@drate/100,1) else set @discount=0
					set @sc1=0
					if @scrate<>0
						begin
							if @scset=0
							set @sc1 = @amt * @scrate/100
							else
							set @sc1 = (@amt - @discount) * @scrate/100
						end
						set @sc1 = round(@sc1,2)
		
					-- Service Tax Calculation
					set @st1=0 
					set @cess1=0
					set @cess21=0
					if exists(select servicetax from outlet where code=@outlet and servicetax='Y')
					begin
						if exists(select g_stonsc from setting where g_stonsc=1)
							set @tamt = @amt + @sc1 - @discount
						else
							set @tamt = @amt - @discount
						exec getServiceTaxr @tamt ,@txndt,@st1 out,@cess1 out,@outlet,1000,@cess21 OUT
					end	

					if @vatonst=0 
					   set @stincl =0
					else
					   set @stincl=@st1+@cess1 

					if @vatonsc=0
					   set @scincl=0
					else
					   Set @scincl=@sc1

					if @stset=0 
						set @tamt =  @amt + @scincl + @stincl 
					else
						set @tamt =  @amt + @scincl + @stincl - @discount

						set @tax = round(@tamt * @strate /100,2)
						set @tax = case when @tax >= 0 then @tax else 0 end

					update kithist set code=@code,qty=@qty,rate=@rate,tax=@tax,discount=@discount,st=@st1,sc=@sc1,cess=@cess1,cess2=@cess21 where ctrlno=@ctrlno
					if @@error <> 0 goto e_error
				end
			fetch next from tc2 into @code,@rate,@qty,@strate,@amt,@tax,@discount,@kot,@ctrlno,@bno,@orate
		end
	close tc2
	deallocate tc2

	exec serviceavouching @bno
	if @@error <> 0 goto e_error

    select @finalamtnew=finalamt from kithist1 where bno=@bno
	set @mamt=@finalamtold-@finalamtnew
	if not exists(select * from massdel where datediff(d,txndt,@txndt)=0) insert into massdel (txndt,amt) values (@txndt,0)  update massdel set amt=amt+@mamt where datediff(d,txndt,@txndt)=0

	fetch next from tc1 into @bno
	end
close tc1
deallocate tc1
commit transaction
set @dt = dateadd(d,1,@dt)     
end
delete from kithist where qty=0
return
e_error:
rollback transaction
raiserror('Error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[saleadj111]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[saleadj111]
@bno numeric 
/* with encryption */
AS
BEGIN
SET NOCOUNT ON;
declare @sql varchar(5000)
declare @code numeric
declare @rate money
declare @qty numeric
declare @strate money
declare @amt money
declare @tax money
declare @discount money
declare @kot numeric
declare @drate money
declare @ro money
declare @t2 varchar(50)
declare @rv varchar(50)

declare @finalamtnew money 
declare @txndt datetime
declare @scamt money
declare @scrate money
declare @ctrlno numeric
declare @dt datetime
declare @sale money
declare @roamt money
declare @total money
declare @orate money
declare @g_staxincl char
declare @roamtold money
declare @roamtnew money
declare @finalamtold decimal(18,2)
declare @finalamtdiff decimal(18,2)
declare @mamt as decimal(18,2)
declare @servicetax decimal(14,2)
declare @cess decimal (14,2) 
declare @tamt decimal(14,2)
declare @vatonst int
declare @st1 decimal(18,2)
declare @cess1 decimal(18,2)
declare @sc1 decimal(18,2)
declare @stset tinyint
declare @scset tinyint
declare @stincl decimal(18,2)
declare @scincl decimal(18,2)
declare @vatonsc tinyint
declare @outlet int 
declare @cess2 decimal(18,2) 
select @finalamtold=finalamt,@txndt=txndt,@outlet=outlet from kithist1 where bno=@BNO

begin transaction 
select @outlet=outlet,@txndt=txndt,@finalamtold=finalamt from kithist1 where bno=@bno
select @g_staxincl=g_staxincl from outlet where code=@outlet

exec serviceadeleting @BNO
update kithist1 set scrate=0,scamt=0 where bno=@bno
update kithist set sc=0 where bno=@bno

	if cursor_status('global','tc2') > 0
		begin
		close tc2
		deallocate tc2
		end

	set @sql = 'declare tc2 cursor for select code,rate,qty,strate,tax,discount,kot,ctrlno,bno from kithist where bno=' + str(@bno)
	exec (@sql)
	open tc2
	fetch next from tc2 into @code,@rate,@qty,@strate,@tax,@discount,@kot ,@ctrlno,@bno
	while @@fetch_status=0
		begin
					select @amt=@qty*@rate
					set @sc1=0
					-- Service Tax Calculation
					set @st1=0 
					set @cess1=0
					set @cess2 = 0
					if exists(select servicetax from outlet where code=@outlet and servicetax='Y')
					begin
						if exists(select g_stonsc from setting where g_stonsc=1)
							set @tamt = @amt + @sc1 - @discount
						else
							set @tamt = @amt - @discount
						exec getServiceTaxr @tamt ,@txndt,@st1 out,@cess1 out,@outlet,0,@cess2
					end	

					if @vatonst=0 
					   set @stincl =0
					else
					   set @stincl=@st1+@cess1 

					if @vatonsc=0
					   set @scincl=0
					else
					   Set @scincl=@sc1

					if @stset=0 
						set @tamt =  @amt + @scincl + @stincl 
					else
						set @tamt =  @amt + @scincl + @stincl - @discount

						set @tax = round(@tamt * @strate /100,2)
						set  @tax = case when @tax >= 0 then @tax else 0 end

					update kithist set tax=@tax,st=@st1,sc=@sc1,cess=@cess1,cess2=@cess2 where ctrlno=@ctrlno
					if @@error <> 0 goto e_error
			fetch next from tc2 into @code,@rate,@qty,@strate,@tax,@discount,@kot,@ctrlno,@bno
		end
	close tc2
	deallocate tc2
	exec serviceavouching @bno
	if @@error <> 0 goto e_error
select @finalamtnew=finalamt from kithist1 where bno=@BNO
set @amt = @finalamtold - @finalamtnew 
if not exists(select * from massdel where datediff(d,txndt,@txndt)=0) insert into massdel (txndt,amt) values (@txndt,0)  update massdel set amt= amt + (@finalamtold-@finalamtnew) where datediff(d,txndt,@txndt)=0
commit transaction 
return
e_error: 
set @sql = 'Error in updating Bill' + ltrim(str(@bno))
rollback transaction raiserror(@sql,16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[saleadj2]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) New Delhi*/
/* 3 May 2004 */
CREATE  PROCEDURE [dbo].[saleadj2] @T1 varchar(50),@percent money 
/* with encryption */
AS
declare @txndt datetime
declare @sql varchar(8000)
declare @sql1 varchar(8000)
declare @amt money
declare @oamt money
declare @bno numeric
declare @cnt1 numeric
--print 'periya'
begin transaction
set @sql = 'update ' + @t1 + ' set txndt = convert(varchar(20),txndt,106)'
exec(@sql)
set @sql = 'declare tc cursor for select distinct txndt from ' + @t1 + ' order by txndt'
exec(@sql)
open tc
fetch next from tc into @txndt
while @@fetch_status=0
begin
 set @sql = 'declare tc1 cursor for select sum(qty*rate) from ' + @t1 +  ' where datediff(d,''1/1/90'',txndt) = datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt)  + ''')'
 exec (@sql)
 open tc1
 fetch next from tc1 into @oamt
 close tc1
 deallocate tc1 
        while 1=1
 begin       
  set @sql = 'update ' + @t1 + ' set qty=qty-1 where qty > 1 and strate > 0 and datediff(d,''1/1/90'',txndt) = datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt)  + ''')'
  exec (@sql)
  if @@rowcount=0 goto break1
  set @sql = 'declare tc1 cursor for select sum(qty*rate) from '+ @t1 +  ' where datediff(d,''1/1/90'',txndt) = datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt)  + ''')'
  exec(@sql)
  open tc1
  fetch next from tc1 into @amt
  close tc1
  deallocate tc1
         if @amt <= (@oamt-(@oamt*@percent/100)) goto break2
 end                
 break1:
            set @sql = 'declare tc1 cursor for select sum(qty*rate) from '+ @t1 +  ' where datediff(d,''1/1/90'',txndt) = datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt)  + ''')'
 exec(@sql)
 open tc1
 fetch next from tc1 into @amt
 close tc1
 deallocate tc1
 if @amt <= (@oamt-(@oamt*@percent/100)) goto break2
-- Second STage starts
 set @sql = 'declare tc2 cursor for select distinct bno from ' + @t1 + ' where datediff(d,''1/1/90'',txndt) = datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt)  + ''')'
            exec(@sql)
            if @@error<>0 goto e_error
 open tc2
 fetch next from tc2 into @bno
 while @@fetch_status=0
 begin
--delete from '+ @t1 + ' where bno = ' + str(@bno) + ' and strate > 0 '
  set @sql =
'declare @bno1 numeric 
declare @cnt int 
select @cnt=count(*) from ' + @t1 + ' where bno= ' + str(@bno) + '
if @cnt > 1
begin
select @cnt=count(*) from ' + @t1 + ' where bno = ' + Str(@bno) + ' and strate = 0 
if @cnt > 0
begin
	declare @xx int
	declare @ctrlno numeric
	declare tt2 cursor for select ctrlno from ' +@t1 + ' where bno=' + str(@bno) + '
	open tt2
	fetch next from tt2 into @ctrlno
	set @xx = 0
	while @@fetch_status=0
	begin 
	if @xx <> 0
	update '+ @t1 + '  set qty=0 where ctrlno=@ctrlno 
	set @xx = @xx + 1
	fetch next from tt2 into @ctrlno
	end
        close tt2 deallocate tt2
end
'   --- 1 line change  (Again modified on 17 March 2013)

SET @SQL1 ='
else
begin
set @cnt=1
declare tc3 cursor for select bno from ' + @t1 + '  where bno = ' + str(@bno)+ '
open tc3 
fetch next from tc3 into @bno1 
while @@fetch_status=0 
begin 
if @cnt=0 update ' + @t1 +  '  set qty=0 where current of tc3
if @@error<>0 begin print ''error in tc3'' rollback raiserror(''error'',16,-1) end
set @cnt=0 
fetch next from tc3 into @bno1
end 
close tc3  
deallocate tc3 end  end'
               exec(@sql+@sql1)
if @@error<>0 goto e_error
         set @sql = 'declare tc1 cursor for select sum(qty*rate) from '+ @t1 +  ' where datediff(d,''1/1/90'',txndt) = datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt)  + ''')'
  exec(@sql)
if @@error<>0 goto e_error
  open tc1
  fetch next from tc1 into @amt
  close tc1
  deallocate tc1
        if @amt <= (@oamt-(@oamt*@percent/100))begin close tc2  deallocate tc2 goto break2 end
  fetch next from tc2 into @bno
  end     
        close tc2
        deallocate tc2
--goto break2
--third stage starts
 set @sql = 'declare tc2 cursor for select distinct bno from ' + @t1 + ' where datediff(d,''1/1/90'',txndt) = datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt)  + ''')'
        exec(@sql)
if @@error<>0 goto e_error
 open tc2
 fetch next from tc2 into @bno
 while @@fetch_status=0
 begin
  set @sql =
'declare @bno1 numeric 
declare @cnt int 
select @cnt=count(*) from ' + @t1 + ' where bno= ' + str(@bno) + '
if @cnt > 1 '
SET @SQL1 ='
begin
set @cnt=1
declare tc3 cursor for select bno from ' + @t1 + ' where bno = ' + str(@bno)+ '
open tc3 
fetch next from tc3 into @bno1 
while @@fetch_status=0 
begin 
if @cnt=0 update  ' + @t1 + '  set qty=0 where current of tc3
if @@error<>0 begin print ''error in tc3'' rollback raiserror(''error'',16,-1) end
set @cnt=0 
fetch next from tc3 into @bno1
end 
close tc3  
deallocate tc3 end '
               exec(@sql+@sql1)
if @@error<>0 goto e_error
         set @sql = 'declare tc1 cursor for select sum(qty*rate) from '+ @t1 +  ' where datediff(d,''1/1/90'',txndt) = datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt)  + ''')'
  exec(@sql)
if @@error<>0 goto e_error
  open tc1
  fetch next from tc1 into @amt
  close tc1
  deallocate tc1
         if @amt <= (@oamt-(@oamt*@percent/100)) close tc2 deallocate tc2 goto break2
  fetch next from tc2 into @bno
  end
        close tc2
        deallocate tc2
break2:
fetch next from tc into @txndt
end
set @sql = 'update ' + @t1 + ' set tax = (qty*rate*strate)/100 where strate > 0'
exec (@sql)
set @sql = 'update ' + @t1 + ' set grp = 1000,amt=qty*rate'
exec (@sql)
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return





GO
/****** Object:  StoredProcedure [dbo].[seradj]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[seradj] @temp varchar(50),@percent money=50
/* with encryption */
 AS
declare @sql varchar(8000)
declare @cno numeric
declare @brno numeric
declare @sc money
declare @ctrlno numeric
declare @newsc  money
declare @foodbill money
begin transaction
set @sql='declare c1 cursor for select gno from ' + @temp
exec(@sql)
open c1
fetch next from c1 into @cno
while @@fetch_status=0
	begin
	declare c2 cursor for select  sc , ctrlno from billhist where cno=@cno
	open c2
	fetch next from c2 into @sc,@ctrlno
	set @foodbill=0
	while @@fetch_status=0
		begin
		if isnull(@sc,0) <> 0
			begin
			set @newsc = round(@sc*@percent/100,2)
			set @foodbill = @foodbill + (@sc-@newsc)
			update billhist set sc=@newsc where ctrlno=@ctrlno
			update billhist set total=rent+lt+sc+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-discount  where ctrlno=@ctrlno
			if @@error <> 0 goto e_error
			end
		fetch next from c2 into @sc,@ctrlno
		end
		update billhist1 set foodbill=foodbill+@foodbill where cno=@cno
		exec pbvc @cno
		if @@error <> 0 goto e_error
		close c2
		deallocate c2
		fetch next from c1 into @cno
	end
close c1
deallocate c1
commit transaction
return
e_error:
rollback transaction
raiserror('Error in SC Adjustment',16,-1)
return



GO
/****** Object:  StoredProcedure [dbo].[serviceadeleting]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE   PROCEDURE [dbo].[serviceadeleting] @bno numeric
/* with encryption */
as
if exists(select * from setting where g_finaccount=0) return

declare @mop int
declare @split char(1)
declare @partic varchar(200)
declare @outlet numeric
declare @company numeric
declare @fy numeric
declare @co1fy numeric
declare @ol_name varchar(50)
declare @sno numeric
declare @brname_cardname varchar(50)
declare @crnum varchar(50)
declare @crtype varchar(50)
declare @self_receivable numeric
declare @hno_mybook numeric
declare @selfdeal  char(1)
declare @gno_compcode_cgpno numeric
declare @sale decimal(15,2)
declare @stamt decimal(15,2)
declare @scamt decimal(15,2)
declare @roamt decimal(15,2)
declare @dcamt decimal(15,2)
declare @txndt datetime
declare @ff_vou char(1)
declare @olname varchar(50)
declare @lux char(1)

declare @co1grno numeric
declare @actnum numeric
declare @grnocash numeric
declare @grnors numeric
declare @grno numeric
declare @st_actnum numeric
declare @sc_actnum numeric
declare @dc_actnum numeric
declare @ro_actnum numeric
declare @hno_htlbook numeric
declare @htl_receivable numeric
declare @scset int
declare @stset int
declare @usernm varchar(50)
declare @dactno numeric

declare  @actno numeric
declare @amt decimal(15,2)
declare @amttyp char(1)
declare @desc varchar(200)
declare @servicetax decimal(14,2)
declare @cess decimal(14,2)
declare @couponamt decimal(14,2)

declare @st1 decimal(14,2)
declare @cess1 decimal(14,2)
declare @sc1 decimal(14,2)

declare @ndiscount decimal(14,2)
declare @servicetaxacno numeric
declare @cessacno numeric
declare @cess2 money
declare @packingchgs money
declare @partpayment money
declare @mop1 tinyint
declare @gno1 numeric
declare @dactno1 numeric
declare @sttableno numeric
declare @ndiscgst money
declare @err varchar(1000)
set @err = 'Error'


select @split=split,@txndt=txndt,@mop=pmode,@outlet=outlet,@usernm=usernm,@sno=sno,@scamt=scamt,@roamt=roamt,
@gno_compcode_cgpno=gno,@servicetax=servicetax,@cess=cess,
@couponamt=isnull(ppcouponamt,0),@ndiscount = ndiscount,@packingchgs=packingchgs,
@partpayment=partpayment,@mop1=pmode1,@gno1=gno1,@ndiscgst=ndiscgst
from kithist1 where bno=@bno

if @mop = 2
	begin
	declare @irnsuppress int
	select @irnsuppress=irnsuppress from billhist1 where 
	cno=(select cno from billhist1 where brno=(select brno from guest where gno=@GNO_COMPCODE_CGPNO ) and company=1 and split='N')
	if @irnsuppress=1 
		begin
		print 'this is e-invoice generated bill.modification not allowed'
		raiserror('Error',16,-1)
		return
		end
	end



select @dactno1 = case
	when @mop1 = 1 then 1  				--cash sales
	when @mop1=2 then 				--room service
		case 
		when @company=1 then @self_receivable
		else
			case when @selfdeal='Y' then @self_receivable 
			else @hno_mybook 
			end
		end
	when @mop1=3 then 23				--bills receivable
	when @mop1=4 then @gno1			 	-- GL A/C
	when @mop1=5 then 22 				-- credir card
	when @mop1=6 then 0 				-- complementary - no vouching
end

--print 'periyakaruppu' goto e_error

if @mop<>2 begin update kithist1 set split='N' where bno=@bno set @split='N' end

select @stamt=sum(tax),@dcamt=sum(discount),@sale=sum(qty*rate),@st1=sum(st),@cess1=sum(cess) ,@sc1=sum(sc),@cess2=sum(cess2) from kithist where bno=@bno

select @company=cocode,@olname=[olname],@actnum=actnum,@st_actnum=st_actnum,@dc_actnum=dc_actnum,@ro_actnum = ro_actnum,@sc_actnum=sc_actnum,
@selfdeal=selfdeal,@co1grno=co1grno,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook ,@lux=lux,@ff_vou=freefoodvouching,@htl_receivable=htl_receivable,
@self_receivable=self_receivable,@scset=scset,@stset=stset from co_rest_ol1  where ocode = @outlet

select @servicetaxacno=servicetaxacno,@cessacno=cessacno,@sttableno=sttableno  from outlet where code=@outlet
--select @servicetaxacno=taxacno from sttabler where  sttableno= @sttableno and @txndt between sdt and edt
select @servicetaxacno=taxacno from sttabler where  sttableno= @sttableno and DATEDIFF(d,'1/1/2000',@txndt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)

if @split='C' and @ff_vou='N'  select @split='F'


--print @split + ' periyakaruppu' goto e_error


if @mop=2 and (@split='F' OR @SPLIT='C')
return  -- if freefood is not to be vouchers

exec @fy=getfy @txndt,@company,@fy out
--if @@error<>0 goto e_error

-- This portion gets the GRNO for the particular USER
if not exists(select * from serviceagrno where usernm=@usernm and outlet=@outlet and datediff(d,@txndt,txndt)=0) return

--if @mop=1 
--	select @grno=grnocash from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
--else
--	select @grno=grnors from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0

if @mop=1 
	select @grno=grnocash from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=2
	select @grno=grnors from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=3 
	select @grno=grnobr from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=4 
	select @grno=grnogl from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=5 
	select @grno=grnocc from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else 
	select @grno=0

--- This was created on 10 Feb 2016
if @mop > 2 and @grno=0
	select @grno=grnors from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
---

-- GRNO Generation ends
-- GRNO Generation ends

if @mop=6 goto subfinal
if @mop=2 and @split='F' goto subfinal

select @dactno = case
	when @mop = 1 then 1  				--cash sales
	when @mop=2 then 				--room service
		case 
		when @company=1 then @self_receivable
		else
			case when @selfdeal='Y' then @self_receivable 
			else @hno_mybook 
			end
		end
	when @mop=3 then 23				--bills receivable
	when @mop=4 then @gno_compcode_cgpno 	-- GL A/C
	when @mop=5 then 22 				-- credir card
	when @mop=6 then 0 				-- complementary - no vouching
end


set @ACTNO = @actnum
set @AMT = @sale
set @AMTTYP = 'C'

if not exists (select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
return

if exists (select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and amt < @amt)
return

begin transaction
	if exists(select * from VOUCHERS 
	where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
        begin
		update vouchers set amt=amt-@amt
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
	
	    if @@error <> 0 goto e_error
		end
	set @ACTNO = @st_actnum
	set @AMT = @stamt
	set @AMTTYP = 'C'

	if exists(select * from VOUCHERS 
	where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
        begin
		update vouchers set amt=amt-@amt
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
		if @@error <> 0 goto e_error
		end
	set @ACTNO = @dc_actnum
	set @AMT = @dcamt + @ndiscount
	set @AMTTYP = 'D'

	if exists(select * from VOUCHERS 
	where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
		begin
		update vouchers set amt=amt-@amt
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
		if @@error <> 0 goto e_error
		end

if @servicetax+@cess + @st1 + @cess1 - @ndiscgst <>0 
	begin
		set @ACTNO = isnull(@servicetaxacno,77)
		set @AMT = @servicetax + @st1 - @ndiscgst
		set @AMTTYP = 'C'
		if exists(select * from VOUCHERS 
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
		begin
		update vouchers set amt=amt-@amt
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
		if @@error <> 0 goto e_error
		end
		set @ACTNO = isnull(@cessacno ,78)
		set @AMT = @cess + @cess1
		set @AMTTYP = 'C'
		if exists(select * from VOUCHERS 
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
		begin
		update vouchers set amt=amt-@amt
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
		if @@error <> 0 goto e_error
		end
	end

if @cess2 <>0 
	begin
		set @ACTNO = 93
		set @AMT = @cess2
		set @AMTTYP = 'C'
		if exists(select * from VOUCHERS 
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
			begin
			update vouchers set amt=amt-@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
			end
	end

if @packingchgs <>0 
	begin
		set @ACTNO = 95
		set @AMT = @packingchgs
		set @AMTTYP = 'C'
		if exists(select * from VOUCHERS 
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
			begin
			update vouchers set amt=amt-@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
			end
	end
	--print convert(varchar(20),@partpayment) goto e_error
if @partpayment <>0 
	begin
		set @ACTNO = @dactno1
		set @AMT = @partpayment
		set @AMTTYP = 'D'
		if exists(select * from VOUCHERS 
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt and totamt_balance=@amt)
			begin
			update vouchers set amt=amt-@amt,totamt_balance=totamt_balance-@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
			end
			else
			begin
			update vouchers set amt=amt-@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
			end

	end

if @roamt < 0 
	begin
		set @ACTNO = @ro_actnum
		set @AMT = abs(@roamt)
		set @AMTTYP = 'D'
		if exists(select * from VOUCHERS 
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
		begin
		update vouchers set amt=amt-@amt
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
		if @@error <> 0 begin print convert(varchar(20),@amt) goto e_error end
		end
	end
else
	begin
		set @ACTNO = @ro_actnum
		set @AMT = abs(@roamt)
		set @AMTTYP = 'C'
		if exists(select * from VOUCHERS 
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
		begin
		update vouchers set amt=amt-@amt
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
		if @@error <> 0 goto e_error
		end
	end

set @ACTNO = @sc_actnum
set @AMT = @scamt  + @sc1
set @AMTTYP = 'C'
if exists(select * from VOUCHERS 
where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and AMT >= @amt)
begin
update vouchers set amt=amt-@amt
where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
if @@error <> 0 goto e_error
end
---print convert(varchar(20),@partpayment)  goto e_error

if @mop >  2  
	begin
		set @ACTNO = @dactno
		set @AMT = @sale+@stamt+@scamt-@dcamt+@roamt+@servicetax+@cess+@st1+@cess1+@sc1+@cess2+@packingchgs-@couponamt-@ndiscount-@partpayment - @ndiscgst 
		set @AMTTYP = 'D'
		--print @actno
		--goto e_error
		delete from vouchers 
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and refno=@bno
		if @@error <> 0 goto e_error
	end
else  --if cash or rs
	begin
		set @ACTNO = @dactno
		--set @AMT = @sale+@stamt+@scamt-@dcamt+@roamt+@servicetax+@cess-@couponamt
		set @AMT = @sale+@stamt+@scamt-@dcamt+@roamt+@servicetax+@cess+@st1+@cess1+@sc1+@cess2+@packingchgs-@couponamt-@ndiscount-@partpayment - @ndiscgst
		set @AMTTYP = 'D'

		if exists(select* from vouchers where amt >= @amt and company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin
				update vouchers set amt=amt-@amt
				where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
				if @@error <> 0 goto e_error
--select @dcamt=amt from vouchers 
--				where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
--print convert(varchar(20),@amt) + ' ' + convert(varchar(20),@dcamt)
--goto e_error


		end
				
		if @mop=2 and  @selfdeal='N' and @company >1
			begin
				exec @fy=getfy @txndt,1,@fy out
		--			set @err='Error :'  +convert(varchar(20),@txndt) + ' ' + str(@fy)
		--goto E_error

				set @ACTNO = @htl_receivable
				set @AMT = @sale+@stamt+@scamt-@dcamt+@roamt+@servicetax+@cess+@st1+@cess1+@sc1+@cess2+@packingchgs-@couponamt-@ndiscount-@partpayment - @ndiscgst
				set @AMTTYP = 'D'
				set @company=1
				set @grno = @co1grno
				if exists(select* from vouchers where amt >= @amt and company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=@outlet)
				begin
					update vouchers set amt=amt-@amt
					where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=@outlet
					if @@error <> 0 goto e_error
				end
				else
				if exists(select* from vouchers where amt >= @amt and company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=0)
				begin
					update vouchers set amt=amt-@amt
					where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=0
					if @@error <> 0 goto e_error
				end


				set @ACTNO = @hno_htlbook
				set @AMT = @sale+@stamt+@scamt-@dcamt+@roamt+@servicetax+@cess+@st1+@cess1+@sc1+@cess2+@packingchgs-@couponamt-@ndiscount-@partpayment - @ndiscgst
				set @AMTTYP = 'C'
				if exists(select* from vouchers where amt >= @amt and company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=@outlet)
				begin
					update vouchers set amt=amt-@amt
					where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=@outlet
					if @@error <> 0 goto e_error
				end
				else
				if exists(select* from vouchers where amt >= @amt and company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=0)
				begin
					update vouchers set amt=amt-@amt
					where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=0
					if @@error <> 0 goto e_error
				end



			end
	end

if @couponamt<>0
	begin
    declare @camt decimal(18,2)
    declare @couponno varchar(50)
    declare co1 cursor for select couponno,amt from prepaidhist where bno=@bno
	open co1
	fetch next from co1 into @couponno,@camt
	while @@fetch_status=0
	begin
		select @actno=glactno from prepaidcouponmaster where ctrlno= substring(@couponno,1,5)
		set @AMT = @camt
		set @AMTTYP = 'D'
		set @DESC = 'To Sales'	
		if exists(select* from vouchers where amt >= @amt and company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin
			update vouchers set amt=amt-@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
		fetch next from co1 into @couponno,@camt
		end
	close co1
	deallocate co1
end

commit transaction
subfinal:
return
e_error:
rollback transaction
raiserror(@err,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[ServiceAVouching]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO



/*
 PowerBrain Software (India)  23-Feb-2011  Created for User Wise Vouching Specially for Ajanta
*/

CREATE  PROCEDURE [dbo].[ServiceAVouching]  @bno numeric,@sessionid varchar(200)=''
/* with encryption */
AS 
declare @mop int
declare @split char(1)
declare @partic varchar(200)
declare @outlet numeric
declare @company numeric
declare @fy numeric
declare @co1fy numeric
declare @ol_name varchar(50)
declare @sno numeric
declare @brname_cardname varchar(50)
declare @crnum varchar(50)
declare @crtype varchar(50)
declare @self_receivable numeric
declare @hno_mybook numeric
declare @selfdeal  char(1)
declare @gno_compcode_cgpno numeric
declare @sale decimal(15,2)
declare @stamt decimal(15,2)
declare @scamt decimal(15,2)
declare @roamt decimal(15,2)
declare @dcamt decimal(15,2)
declare @txndt datetime
declare @ff_vou char(1)
declare @olname varchar(50)
declare @lux char(1)
declare @co1grno numeric
declare @actnum numeric
declare @grnocash numeric
declare @grnors numeric
declare @grnocc numeric
declare @grnobr numeric
declare @grnogl numeric
declare @grno numeric
declare @st_actnum numeric
declare @sc_actnum numeric
declare @dc_actnum numeric
declare @ro_actnum numeric
declare @hno_htlbook numeric
declare @htl_receivable numeric
declare @scset int
declare @stset int
declare @usernm varchar(50)
declare @dactno numeric

declare  @actno numeric
declare @amt decimal(15,2)
declare @amttyp char(1)
declare @desc varchar(200)
declare @servicetax decimal(14,2)
declare @cess decimal(14,2)
declare @cess2 decimal(14,2)
declare @err varchar(5000)
declare @couponamt decimal(18,2)

declare @st1 money        --servicetax from kithist
declare @cess1 money   -- cess from kithist
declare @sc1 money      -- service charges from kithist
declare @scrate1 money -- service charges rate from kithist
declare @cess21 money   -- cess from kithist

declare @ndiscount money -- General Discount  (Overall lumpsum discount)
declare @servicetaxacno numeric
declare @cessacno numeric
declare @packingchgs money
declare @partpayment money
declare @mop1 tinyint
declare @dactno1 numeric
declare @gno1 numeric
declare @sttableno numeric
declare @ndiscgst money
select @split=split,@txndt=txndt,@mop=pmode,@outlet=outlet,@usernm=usernm,@sno=sno,@scamt=scamt,@roamt=roamt,
@gno_compcode_cgpno=gno,@brname_cardname=crname,@crnum=crnum,@crtype=crtype ,@servicetax=servicetax,@cess=cess,
@couponamt=isnull(ppcouponamt,0),@ndiscount=ndiscount,@packingchgs=packingchgs,@partpayment=isnull(partpayment,0),
@mop1=pmode1,@gno1=gno1,@ndiscgst=ndiscgst
from kithist1 where bno=@bno

if @mop<>2
begin 
update kithist1 set split='N' where bno=@bno set @split='N' 
----print str(@mop) + str(@bno) + @split goto e_error
end

if not exists (select * from kithist where bno=@bno) begin print str(@bno) + ' Data not found' return end


select @stamt=sum(tax),@dcamt=sum(discount),@sale=sum(isnull(qty,0)*isnull(rate,0)),@st1=sum(isnull(st,0)), @cess1=sum(isnull(cess,0)), @sc1= sum(isnull(sc,0)),@cess2=sum(cess2)    from kithist where bno=@bno

select @company=cocode,@olname=[olname],@actnum=actnum,@st_actnum=st_actnum,@dc_actnum=dc_actnum,@ro_actnum = ro_actnum,@sc_actnum=sc_actnum,
@selfdeal=selfdeal,@co1grno=co1grno,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook ,@lux=lux,@ff_vou=freefoodvouching,@htl_receivable=htl_receivable,
@self_receivable=self_receivable,@scset=scset,@stset=stset from co_rest_ol1  where ocode = @outlet

-- these lines are included/modified for welcoming GST  June2017
select @sttableno=sttableno,@cessacno=cessacno from outlet where code=@outlet
if exists(select * from sttabler where sttableno=@sttableno and DATEDIFF(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt))
select @servicetaxacno=taxacno from sttabler where  sttableno= @sttableno and DATEDIFF(d,'1/1/2000',@txndt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
if @servicetaxacno=0 select @servicetaxacno=servicetaxacno  from outlet where code=@outlet
--  end of GST  2017

if @split='C' and @ff_vou='N'  select @split='F'
/* Updation of finalamt and removing tax and discount made on 26 July 2011 Tuesday (Lachu is not still ok)  for Managers Report */

--print str(@bno) 
-- + ' sale' + convert(varchar(20),isnull(@sale,0))
-- + '  st ' + convert(varchar(20),@stamt)
-- + ' dcamt ' + convert(varchar(20),@dcamt)
-- + ' roamt' + convert(varchar(20),@roamt) 
--+
--' scamt ' + convert(varchar(20),@scamt) + '  service tax ' + convert(varchar(20),@servicetax) + ' cess ' + convert(varchar(20),@cess) 
--+ convert(varchar(20),@txndt) + @usernm

declare @finalamt money 
update kithist1 set finalamt=@sale+@stamt+@scamt-@dcamt+@roamt +@servicetax+@cess-@couponamt+@st1+@cess1+@sc1+@cess2+@packingchgs-@ndiscount-@ndiscgst where bno=@bno
select @finalamt=finalamt from kithist1 where bno=@bno
--------new 8/12/2016
if @partpayment > 0
if @partpayment > @finalamt
	begin
	print 'Part Payment Amount' + convert(varchar(20),@partpayment) +' must be lesser than or equal to the total amount of the bill ' +  convert(varchar(20),@finalamt)
	goto e_error
	end
if @partpayment > 0
	begin
	if @mop1=@mop 
		begin 
		print 'Mode of Payment for Part Payment and Final Payment Can not be same'
		goto e_error
		end
	if @mop1=2 
		begin
		print 'Part Payment Mode can not be Room Account'
		goto e_error
		end
	if @partpayment<>0  and @mop=2 
		begin
		print 'The Mode of Payment can not be Room Account if the amount is paid in two parts'
		goto e_error
		end
	end---------------new end

if @mop=2 and (@split='F' or @split='C')
	begin
	--print @split + 'i aM HERE' goto e_error
	--update kithist1 set roamt=0,scamt=0,finalamt=@sale  where bno=@bno
	--update kithist set tax=0,discount=0 where bno=@bno
	return  -- if freefood is not to be vouchers
	end

if @mop=6 
	begin
	--update kithist1 set roamt=0,scamt=0,finalamt=@sale  where bno=@bno
	--update kithist set tax=0,discount=0 where bno=@bno
	return
	end

if exists(select * from setting where g_finaccount=0) return



begin transaction
exec @fy=getfy @txndt,@company,@fy out
if @@error<>0 goto e_error

-- This portion gets the GRNO for the particular USER
if not exists(select * from serviceagrno where usernm=@usernm and outlet=@outlet and datediff(d,@txndt,txndt)=0)
begin
	set @grnocash=0
	set @grnors=0
	set @grnogl=0
	set @grnocc=0
	set @grnobr=0
	begin transaction [v4]
		exec getgrpVOU @txndt,@company,@grnocash out,1
		if @@error <>0 goto e_error
		exec getgrpVOU @txndt,@company,@grnors out,1
		if @@error <>0 goto e_error
		exec getgrpVOU @txndt,@company,@grnogl out,1
		if @@error <>0 goto e_error
		exec getgrpVOU @txndt,@company,@grnocc out,1
		if @@error <>0 goto e_error
		exec getgrpVOU @txndt,@company,@grnobr out,1
		if @@error <>0 goto e_error
		insert into serviceagrno (txndt,outlet,usernm,grnocash,grnors,grnocc,grnobr,grnogl) values (@txndt,@outlet,@usernm,@grnocash,@grnors,@grnocc,@grnobr,@grnogl)
	commit transaction [v4]
end

if @mop=1 
	select @grno=grnocash from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=2
	select @grno=grnors from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=3 
	select @grno=grnobr from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=4 
	select @grno=grnogl from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=5 
	select @grno=grnocc from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else 
	select @grno=0
-- GRNO Generation ends

--- This was created on 10 Feb 2016
if @mop > 2 and @grno=0
	select @grno=grnors from serviceagrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
---



select @partic = case  
	when @mop=1 then 'To Cash Sales ' + ltrim(@olname)
	when @mop=2 then 'To Room Service Sales ' +  ltrim(@olname)
	when @mop=3 then 'To '  + rtrim(@brname_cardname)  + 'B.No' + ltrim(str(@sno)) + ' ' +  ltrim(@olname)
	when @mop=4 then 'To B.No ' + ltrim(str(@sno)) + ' '  +  ltrim(@olname)
	when @mop=5 then 'To ' + rtrim(@brname_cardname) + ',' + rtrim(@crnum) + ','+ rtrim(@crtype) + ' B.No.' + ltrim(str(@sno)) + ' ' +   ltrim(@olname)
	when @mop=6 then 'To '  + rtrim(@brname_cardname)  + 'B.No' + ltrim(str(@sno)) + ' ' +  ltrim(@olname)
end

select @dactno = case
	when @mop = 1 then 1  				--cash sales
	when @mop=2 then 				--room service
		case 
		when @company=1 then @self_receivable
		else
			case when @selfdeal='Y' then @self_receivable 
			else @hno_mybook 
			end
		end
	when @mop=3 then 23				--bills receivable
	when @mop=4 then @gno_compcode_cgpno 	-- GL A/C
	when @mop=5 then 22 				-- credir card
	when @mop=6 then 0 				-- complementary - no vouching
end

select @dactno1 = case
	when @mop1 = 1 then 1  				--cash sales
	when @mop1=2 then 				--room service
		case 
		when @company=1 then @self_receivable
		else
			case when @selfdeal='Y' then @self_receivable 
			else @hno_mybook 
			end
		end
	when @mop1=3 then 23				--bills receivable
	when @mop1=4 then @gno1			 	-- GL A/C
	when @mop1=5 then 22 				-- credir card
	when @mop1=6 then 0 				-- complementary - no vouching
end

declare @pmodedesc varchar(50)
select @pmodedesc = case 
when @mop=1 then 'Cash'
when @mop=2 then 'RS'
when @mop=3 then 'BR'
when @mop=4 then 'GL'
when @mop=5 then 'CC'
else ''
end

if @sale<>0 
	begin
	set @ACTNO = @actnum
	set @AMT = isnull(@sale,0)
	set @AMTTYP = 'C'
	set @DESC = 'By Sales-' + @pmodedesc 	
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt,sessionid=@sessionid
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end
------------------------------------------------------------------------------------------------------

if @stamt<>0
	begin
	set @ACTNO = @st_actnum
	set @AMT = @stamt
	set @AMTTYP = 'C'
	set @DESC = 'By Sales-' + @pmodedesc 
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt,sessionid=@sessionid
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end
------------------------------------------------------------------------------------------------------

if @dcamt+@ndiscount<>0
	begin
	set @ACTNO = @dc_actnum
	set @AMT = @dcamt + @ndiscount
	set @AMTTYP = 'D'
	set @DESC = 'To Sales-' + @pmodedesc 	
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt,sessionid=@sessionid
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end
------------------------------------------------------------------------------------------------------

if @roamt <> 0 
	if @roamt < 0 
		begin

			set @ACTNO = @ro_actnum
			set @AMT = abs(@roamt)
			set @AMTTYP = 'D'
			set @DESC = 'To Sales-' + @pmodedesc
			if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
				begin 
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
					(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 

					if @@error <> 0 goto e_error
				end
			else
				begin
					update vouchers set amt=amt+@amt,sessionid=@sessionid
					where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
					if @@error <> 0 goto e_error
				end
		end
	else
		begin
			set @ACTNO = @ro_actnum
			set @AMT = abs(@roamt)
			set @AMTTYP = 'C'
			set @DESC = 'By Sales-' + @pmodedesc	
			if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
				begin 
					insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
					(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
					if @@error <> 0 goto e_error
				end
			else
				begin
					update vouchers set amt=amt+@amt,sessionid=@sessionid
					where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
					if @@error <> 0 goto e_error
				end
		end
------------------------------------------------------------------------------------------------------
if @scamt+@sc1 <> 0
	begin
	set @ACTNO = @sc_actnum
	set @AMT = @scamt + @sc1
	set @AMTTYP = 'C'
	set @DESC = 'By Sales-' + @pmodedesc
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt,sessionid=@sessionid
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end

------------------------------------------------------------------------------------------------------
if @servicetax + @st1 - @ndiscgst<> 0
	begin
	set @ACTNO = isnull(@servicetaxacno,77)
	set @AMT = @servicetax + @st1 - @ndiscgst
	set @AMTTYP = 'C'
	set @DESC = 'By Sales-' + @pmodedesc
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 

			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt,sessionid=@sessionid
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end


------------------------------------------------------------------------------------------------------
if @cess + @cess1 <> 0
	begin
	set @ACTNO = isnull(@cessacno,78)
	set @AMT = @cess + @cess1
	set @AMTTYP = 'C'
	set @DESC = 'By Sales-' + @pmodedesc
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt,sessionid=@sessionid
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end

------------------------------------------------------------------------------------------------------

if @cess2 <> 0
	begin
	set @ACTNO = 93
	set @AMT = @cess2
	set @AMTTYP = 'C'
	set @DESC = 'By Sales-' + @pmodedesc
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt,sessionid=@sessionid
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end

------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------

if @packingchgs <> 0
	begin
	set @ACTNO = 95
	set @AMT = @packingchgs
	set @AMTTYP = 'C'
	set @DESC = 'By Sales-' + @pmodedesc
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt,sessionid=@sessionid
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end

------------------------------------------------------------------------------------------------------
if @couponamt <> 0
begin
    declare @camt decimal(18,2)
    declare @couponno varchar(50)
    declare co1 cursor for select couponno,amt from prepaidhist where bno=@bno
	open co1
	fetch next from co1 into @couponno,@camt
	while @@fetch_status=0
	begin
		select @actno=glactno from prepaidcouponmaster where ctrlno= substring(@couponno,1,5)
		set @AMT = @camt
		set @AMTTYP = 'D'
		set @DESC = 'To Sales-' + @pmodedesc
		if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
			begin 
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
				(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
				if @@error <> 0 goto e_error
			end
		else
			begin
				update vouchers set amt=amt+@amt,sessionid=@sessionid
				where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
				if @@error <> 0 goto e_error
			end
		fetch next from co1 into @couponno,@camt
		end
	close co1
	deallocate co1
end

if @partpayment<>0
	begin
		set @amt=@partpayment
		set @ACTNO = @dactno1
		set @AMTTYP = 'D'
		set @DESC = @partic
		if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
			begin 
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
				(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
				if @@error <> 0 goto e_error
			end
		else
			begin
				update vouchers set amt=amt+@amt,sessionid=@sessionid
				where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
				if @@error <> 0 goto e_error
			end
	 end




------------------------------------------------------------------------------------------------------
set @AMT = @sale+@stamt+@scamt-@dcamt+@roamt+@servicetax+@cess+@st1+@cess1+@sc1+@cess2+@packingchgs-@couponamt - @ndiscount - @partpayment - @ndiscgst

if @mop > 2 
	begin
		set @ACTNO = @dactno
		set @AMTTYP = 'D'
		set @DESC = @partic
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid,refno) values
		(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid,@bno) 
		if @@error <> 0 goto e_error
	 end
else
	begin
		set @ACTNO = @dactno
		set @AMTTYP = 'D'
		set @DESC = @partic

		if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
			begin 
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid) values
				(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid) 
				if @@error <> 0 goto e_error
			end
		else
			begin
				update vouchers set amt=amt+@amt,sessionid=@sessionid
				where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
				if @@error <> 0 goto e_error
			end

--select @dcamt=amt from vouchers 
--where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm

--print convert(varchar(20),@amt) + ' ' + convert(varchar(20),@dcamt)
--goto e_error

	
		if @mop=2 and @selfdeal='N' and @company<>1
			begin 
				exec @co1fy= getfy @txndt,1,@co1fy out
				set @company=1
				set @fy=@co1fy
				set @grno=@co1grno
				set @ACTNO = @htl_receivable
				set @AMTTYP = 'D'
				set @DESC = @partic
				if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=@outlet)
					begin 
						insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid,outlet) values
						(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid,@outlet) 
						if @@error <> 0 goto e_error
					end
				else
					begin
						update vouchers set amt=amt+@amt,sessionid=@sessionid
						where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=@outlet
						if @@error <> 0 goto e_error
					end
				set @ACTNO = @hno_htlbook
				set @AMTTYP = 'C'
				set @DESC = @partic
				if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=@outlet)
					begin 
						insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],sessionid,outlet) values
						(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-A',@usernm,@sessionid,@outlet) 
						if @@error <> 0 goto e_error
					end
				else
					begin
						update vouchers set amt=amt+@amt,sessionid=@sessionid
						where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and outlet=@outlet
						if @@error <> 0 goto e_error
					end
			end
	end

final:
commit transaction
return
e_error:
rollback transaction
raiserror(@err,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[servicebdeleting]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[servicebdeleting] @cno numeric 
/* with encryption */
as
if exists(select * from setting where g_finaccount=0) return
declare @mop int
declare @split char(1)
declare @partic varchar(200)
declare @outlet numeric
declare @company numeric
declare @fy numeric
declare @co1fy numeric
declare @ol_name varchar(50)
declare @sno numeric
declare @crname varchar(50)
declare @crnum varchar(50)
declare @crtype varchar(50)
declare @self_receivable numeric
declare @hno_mybook numeric
declare @selfdeal  char(1)
declare @gno_compcode_cgpno numeric
declare @sale decimal(15,2)
declare @stamt decimal(15,2)
declare @scamt decimal(15,2)
declare @roamt decimal(15,2)
declare @dcamt decimal(15,2)
declare @txndt datetime
declare @ff_vou char(1)
declare @olname varchar(50)
declare @lux char(1)

declare @co1grno numeric
declare @actnum numeric
declare @grnocash numeric
declare @grnors numeric
declare @grno numeric
declare @st_actnum numeric
declare @sc_actnum numeric
declare @dc_actnum numeric
declare @ro_actnum numeric
declare @hno_htlbook numeric
declare @htl_receivable numeric
declare @scset int
declare @stset int
declare @usernm varchar(50)
declare @dactno numeric

declare  @actno numeric
declare @amt decimal(15,2)
declare @amttyp char(1)
declare @desc varchar(200)
declare @sttableno numeric

select @txndt=txndt,@outlet=outlet,@gno_compcode_cgpno=gno,@mop=pmode,@sno=sno,@split=split,@selfdeal=selfdeal,@dcamt=damt,
@crname=crname,@crnum=crnum,@crtype=crtype,@lux=lux,@usernm=usernm,@scamt=scamt,@stamt=stamt from phone where cno=@cno

select @sale=sum(amount) from phhist where cno=@cno

select @company=company,@olname=[name],@sno=sno,@actnum=gl_actnum,@dc_actnum=dc_actnum, @selfdeal=selfdeal,
@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,@co1grno=co1grno,@lux=lux,@ff_vou=freefoodvouching,@st_actnum=st_actnum,@sttableno=sttableno from phoutlet where outlet=@outlet

exec @fy=getfy @txndt,@company,@fy out
if @@error<>0 goto e_error
declare @st money
set @st = @st_actnum

-- 25-9-2025 - these lines are important to get the GST Actnum for this outlet 
if exists(select * from sttabler where sttableno=@sttableno and 
DATEDIFF(d,'1/1/2000',@txndt)between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
 and rate <>0  and taxacno<>0)
select @st_actnum=taxacno   from sttabler where sttableno=@sttableno and 
DATEDIFF(d,'1/1/2000',@txndt)between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
and rate <>0  

---print convert(varchar(20),@st) + ' ' + convert(varchar(20),@st_actnum) +  ' ' + str(@sttableno)
---goto e_error

select @dactno = case
	when @mop=1 then 1				--cash sales
	when @mop=2 then				-- Room Account
		case	when @company=1 then 24
			else
				case	when @selfdeal='Y' then 24
				else	@hno_mybook
				end
			end
	when @mop=3 then 23 				--Bill Receivable
	when @mop=4 then @gno_compcode_cgpno 	-- GL A/C
	when @mop=5 then 22				--Credit Card
	when @mop=6 then 0				-- Complementary
end
if @split='C' set @split='F'
print @split
--goto e_error
if @mop=6 return
if @mop=2 and @split='F' return
-- This portion gets the GRNO for the particular USER
--if not exists(select * from servicebgrno where usernm=@usernm and outlet=@outlet and datediff(d,@txndt,txndt)=0) goto e_error

if @mop=1 
	select @grno=grnocash from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=2
	select @grno=grnors from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=3
	select @grno=grnobr from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=4
	select @grno=grnogl from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=5
	select @grno=grnocc from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0

-- GRNO Generation ends

set @ACTNO = @actnum
set @AMT = @sale 
set @AMTTYP = 'C'
---print convert(varchar(20),@amt) + str(@grno) goto e_error
--if not exists (select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
--begin
--print 'Error1' goto e_error
--return
--end
if exists (select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and amt < @amt)
begin
print 'Error2' goto e_error
return
end

begin transaction
update vouchers set amt=amt-@amt
where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
if @@error <> 0 goto e_error

if @dcamt >  0
	begin
	set @ACTNO = @dc_actnum
	set @AMT = @dcamt
	set @AMTTYP = 'D'
	update vouchers set amt=amt-@amt
	where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
	if @@error <> 0 goto e_error
	end
if @scamt >  0
	begin
	set @ACTNO = 49
	set @AMT = @scamt
	set @AMTTYP = 'C'
	update vouchers set amt=amt-@amt
	where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
	if @@error <> 0 goto e_error
	end

if @stamt >  0
	begin
	set @ACTNO = @st_actnum
	set @AMT = @stamt
	set @AMTTYP = 'C'
	update vouchers set amt=amt-@amt
	where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
	if @@error <> 0 goto e_error
	end

if @mop > 2
	begin
		set @ACTNO = @dactno
		set @AMT = @sale-@dcamt+@scamt+@stamt
		set @AMTTYP = 'D'
		delete from vouchers 
		where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm and refno=@cno
		if @@error <> 0 goto e_error
	end
else
	begin
	set @ACTNO = @dactno
	set @AMT = @sale-@dcamt+@scamt+@stamt
	set @AMTTYP = 'D'
	update vouchers set amt=amt-@amt
	where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
	if @@error <> 0 goto e_error
	if @mop=2 and @selfdeal='N' and  @company<>1
		begin
			exec @fy=getfy @txndt,1,@fy out
			set @company=1
			
			set @ACTNO = 24
			set @AMT = @sale-@dcamt+@scamt+@stamt
			set @AMTTYP = 'D'
							set @grno=@co1grno

			update vouchers set amt=amt-@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
			
			set @ACTNO = @hno_htlbook
			set @AMT = @sale-@dcamt+@scamt+@stamt
			set @AMTTYP = 'C'
			update vouchers set amt=amt-@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end
subfinal:
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[servicebvouching]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[servicebvouching] @cno numeric 
/* with encryption */
AS
if exists(select * from setting where g_finaccount=0) return
declare @msg varchar(5000)
declare @mop int
declare @split char(1)
declare @partic varchar(200)
declare @outlet numeric
declare @company numeric
declare @fy numeric
declare @co1fy numeric
declare @ol_name varchar(50)
declare @sno numeric
declare @crname varchar(50)
declare @crnum varchar(50)
declare @crtype varchar(50)
declare @self_receivable numeric
declare @hno_mybook numeric
declare @selfdeal  char(1)
declare @gno_compcode_cgpno numeric
declare @sale decimal(15,2)
declare @stamt decimal(15,2)
declare @scamt decimal(15,2)
declare @roamt decimal(15,2)
declare @dcamt decimal(15,2)
declare @txndt datetime
declare @ff_vou char(1)
declare @olname varchar(50)
declare @lux char(1)



declare @co1grno numeric
declare @actnum numeric
declare @grnocash numeric
declare @grnors numeric
declare @grnogl numeric
declare @grnocc numeric
declare @grnobr numeric

declare @grno numeric
declare @st_actnum numeric
declare @sc_actnum numeric
declare @dc_actnum numeric
declare @ro_actnum numeric
declare @hno_htlbook numeric
declare @htl_receivable numeric
declare @scset int
declare @stset int
declare @usernm varchar(50)
declare @dactno numeric

declare @actno numeric
declare @amt decimal(15,2)
declare @amttyp char(1)
declare @desc varchar(200)
declare @sttableno int 

select @txndt=txndt,@outlet=outlet,@gno_compcode_cgpno=gno,@mop=pmode,@sno=sno,@split=split,@selfdeal=selfdeal,@dcamt=damt,
@crname=crname,@crnum=crnum,@crtype=crtype,@lux=lux,@usernm=usernm,@scamt=scamt,@stamt=stamt from phone where cno=@cno

select @sale=sum(amount) from phhist where cno=@cno

declare @famt money
select @famt = @sale+scamt+stamt-damt from phone where cno=@cno 
---set @msg = convert(varchar(20),@famt) goto e_error

update phone set finalamt = @famt where cno=@cno   --v 13.0.7 20 April 2012 @ kottaiyur

select @company=company,@olname=[name],@sno=sno,@actnum=gl_actnum,@dc_actnum=dc_actnum, @selfdeal=selfdeal,
@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,@co1grno=co1grno,@lux=lux,@ff_vou=freefoodvouching,@st_actnum=st_actnum,@sttableno=sttableno  from phoutlet where outlet=@outlet

--if exists(select * from sttabler where sttableno=@sttableno and convert(varchar(20),@txndt,106) between sdt and edt and rate <>0  and taxacno<>0)
--select @st_actnum=taxacno   from sttabler where sttableno=@sttableno and convert(varchar(20),@txndt,106) between sdt and edt and rate <>0  

if exists(select * from sttabler where sttableno=@sttableno and DATEDIFF(d,'1/1/2000',@txndt)between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt) and rate <>0  and taxacno<>0)
select @st_actnum=taxacno   from sttabler where sttableno=@sttableno and DATEDIFF(d,'1/1/2000',@txndt)between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt) and rate <>0  


exec @fy=getfy @txndt,@company,@fy out
if @@error<>0 return

select @partic = case
	when @mop=1 then 'To cash Sales-' + ltrim(@olname)
	when @mop=2 then 'To room A/C Sales-' + ltrim(@olname)
	when @mop=3 then 'To ' + rtrim(@crname) +  '-BNo.' + ltrim(str(@sno)) + ' ' + ltrim(@olname)
	when @mop=4 then 'To ' + ltrim(str(@sno)) + '-' + ltrim(@olname)
	when @mop=5 then 'To ' + rtrim(@crname) + '-'+ rtrim(@crnum) + '-'+ rtrim(@crtype) + '-BNo.' + ltrim(str(@sno)) + '-' + ltrim(@olname)
	when @mop=6 then 'To ' + rtrim(@crname) +  '-BNo.' + ltrim(str(@sno)) + ' ' + ltrim(@olname)
end
select @dactno = case
	when @mop=1 then 1				--cash sales
	when @mop=2 then				-- Room Account
		case	when @company=1 then 24
			else
				case	when @selfdeal='Y' then 24
				else	@hno_mybook
				end
			end
	when @mop=3 then 23 				--Bill Receivable
	when @mop=4 then @gno_compcode_cgpno 	-- GL A/C
	when @mop=5 then 22				--Credit Card
	when @mop=6 then 0				-- Complementary
end
if @split='C' set @split='F'
if @mop=6 return
print @split
--goto e_error
if @mop=2 and @split='F' return
begin transaction
-- This portion gets the GRNO for the particular USER
if not exists(select * from servicebgrno where usernm=@usernm and outlet=@outlet and datediff(d,@txndt,txndt)=0)
begin
	exec getgrpVOU @txndt,@company,@grnocash out,2
	if @@error <>0 goto e_error
	exec getgrpVOU @txndt,@company,@grnors out,2
	if @@error <>0 goto e_error
	exec getgrpVOU @txndt,@company,@grnogl out,2
	if @@error <>0 goto e_error
	exec getgrpVOU @txndt,@company,@grnocc out,2
	if @@error <>0 goto e_error
	exec getgrpVOU @txndt,@company,@grnobr out,2
	if @@error <>0 goto e_error
	insert into servicebgrno (txndt,outlet,usernm,grnocash,grnors,grnogl,grnocc,grnobr) values (@txndt,@outlet,@usernm,@grnocash,@grnors,@grnogl,@grnocc,@grnobr)
end

if @mop=1 
	select @grno=grnocash from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=2
	select @grno=grnors from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=3
	select @grno=grnobr from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=4
	select @grno=grnogl from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0
else if @mop=5
	select @grno=grnocc from servicebgrno where outlet=@outlet and usernm=@usernm and datediff(d,txndt,@txndt)=0

-- GRNO Generation ends
	set @usernm=substring(@usernm,1,10)

if @sale<>0 
	begin
	set @ACTNO = @actnum
	set @AMT = @sale
	set @AMTTYP = 'C'
	set @DESC = 'By Sales'	
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-B',@usernm) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end

------------------------------------------------------------------------------------------------------
if @dcamt<>0
	begin
	set @ACTNO = @dc_actnum
	set @AMT = @dcamt
	set @AMTTYP = 'D'
	set @DESC = 'To Sales'
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-B',@usernm) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
if @scamt<>0
	begin
	set @ACTNO = 49
	set @AMT = @scamt
	set @AMTTYP = 'C'
	set @DESC = 'By Sales'
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-B',@usernm) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end
------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------
if @stamt<>0
	begin
	set @ACTNO = @st_Actnum
	set @AMT = @stamt
	set @AMTTYP = 'C'
	set @DESC = 'By Sales'
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-B',@usernm) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end
	end

------------------------------------------------------------------------------------------------------

if @mop > 2 
	begin
	set @ACTNO = @dactno
	set @AMT = @sale + @scamt - @dcamt + @stamt
	set @AMTTYP = 'D'
	set @DESC = @partic
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user],refno) values
	(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-B',@usernm,@cno) 
	if @@error <> 0 goto e_error
	end
else
	begin
	set @ACTNO = @dactno
	set @AMT = @sale  + @scamt - @dcamt + @stamt
	set @AMTTYP = 'D'
	set @DESC = @partic
	if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
		begin 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
			(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-B',@usernm) 
			if @@error <> 0 goto e_error
		end
	else
		begin
			update vouchers set amt=amt+@amt
			where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
			if @@error <> 0 goto e_error
		end


	if @mop=2 and @selfdeal='N' and @company<>1
		begin
		SET @COMPANY=1
		exec @fy = getfy @txndt,1,@fy out
		
		set @ACTNO = 24
		set @AMT = @sale + @scamt - @dcamt
		set @AMTTYP = 'D'
		set @DESC = @partic
						set @grno=@co1grno

		if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
			begin 
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
				(@COMPANY,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-B',@usernm) 
				if @@error <> 0 goto e_error
			end
		else
			begin
				update vouchers set amt=amt+@amt
				where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
				if @@error <> 0 goto e_error
			end
		
		set @ACTNO = @hno_htlbook
		set @AMT = @sale + @scamt - @dcamt
		set @AMTTYP = 'C'
		set @DESC = @partic
		if not exists(select * from vouchers where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm)
			begin 
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,txntype,[user]) values
				(@company,@fy,@actno,@amt,@desc,@amttyp,@txndt,@grno,'Y','SER-B',@usernm) 
				if @@error <> 0 goto e_error
			end
		else
			begin
				update vouchers set amt=amt+@amt
				where company=@company and fy=@fy and datediff(d,txndt,@txndt)=0 and actnum=@actno and grno=@grno and amttyp=@amttyp and [user]=@usernm
				if @@error <> 0 goto e_error

			end
		end
	end
------------------------------------------------------------------------------------------------------

final:
commit transaction
return
e_error:
rollback transaction
set @msg = 'Error : ' + @msg 
raiserror(@msg,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[ServicePermissionUpdate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<PowerBrain Software>
-- Create date: <15 July 2012>
-- Description:	<Updating Servicepermission with users left out>
-- =============================================
CREATE PROCEDURE [dbo].[ServicePermissionUpdate]
/* with encryption */
AS
BEGIN
SET NOCOUNT ON;

declare @serviceno numeric
declare @userno numeric
declare @outlet numeric
declare @outlettype numeric
set @outlettype = 5

declare c1 cursor for select serviceno from servicenumbers where outlettype=@outlettype
open c1
fetch next from c1 into @serviceno
while @@fetch_status=0
begin
declare c2 cursor for select userno from basic
open c2
fetch next from c2 into @userno
while @@fetch_status=0
begin
declare c3 cursor for select company from company
open c3
fetch next from c3 into @outlet
while @@fetch_status=0
begin
if not exists(select * from servicepermission where userno=@userno and serviceno=@serviceno and outlet=@outlet)
insert into servicepermission (serviceno,userno,permission,outlet,outlettype)
values (@serviceno,@userno,'N',@outlet,@outlettype)
fetch next from c3 into @outlet
end
close c3
deallocate c3
fetch next from c2 into @userno
end
close c2
deallocate c2
fetch next from c1 into @serviceno
end
close c1
deallocate c1

set @outlettype=1
declare c1 cursor for select serviceno from servicenumbers where outlettype=@outlettype
open c1
fetch next from c1 into @serviceno
while @@fetch_status=0
begin
declare c2 cursor for select userno from basic
open c2
fetch next from c2 into @userno
while @@fetch_status=0
begin
declare c3 cursor for select code from outlet 
open c3
fetch next from c3 into @outlet
while @@fetch_status=0
begin
if not exists(select * from servicepermission where userno=@userno and serviceno=@serviceno and outlet=@outlet)
insert into servicepermission (serviceno,userno,permission,outlet,outlettype)
values (@serviceno,@userno,'N',@outlet,@outlettype)
fetch next from c3 into @outlet
end
close c3
deallocate c3
fetch next from c2 into @userno
end
close c2
deallocate c2
fetch next from c1 into @serviceno
end
close c1
deallocate c1


set @outlettype=2
declare c1 cursor for select serviceno from servicenumbers where outlettype=@outlettype
open c1
fetch next from c1 into @serviceno
while @@fetch_status=0
begin
declare c2 cursor for select userno from basic
open c2
fetch next from c2 into @userno
while @@fetch_status=0
begin
declare c3 cursor for select outlet from phoutlet 
open c3
fetch next from c3 into @outlet
while @@fetch_status=0
begin
if not exists(select * from servicepermission where userno=@userno and serviceno=@serviceno and outlet=@outlet)
insert into servicepermission (serviceno,userno,permission,outlet,outlettype)
values (@serviceno,@userno,'N',@outlet,@outlettype)
fetch next from c3 into @outlet
end
close c3
deallocate c3
fetch next from c2 into @userno
end
close c2
deallocate c2
fetch next from c1 into @serviceno
end
close c1
deallocate c1




set @outlettype=10
declare c1 cursor for select serviceno from servicenumbers where outlettype=@outlettype
open c1
fetch next from c1 into @serviceno
while @@fetch_status=0
begin
declare c2 cursor for select userno from basic
open c2
fetch next from c2 into @userno
while @@fetch_status=0
	begin
	if not exists(select * from servicepermission where userno=@userno and serviceno=@serviceno)
	insert into servicepermission (serviceno,userno,permission,outlet,outlettype)
	values (@serviceno,@userno,'N',0,@outlettype)
	fetch next from c2 into @userno
	end
close c2
deallocate c2
fetch next from c1 into @serviceno
end
close c1
deallocate c1

set @outlettype=4
declare c1 cursor for select serviceno from servicenumbers where outlettype=@outlettype
open c1
fetch next from c1 into @serviceno
while @@fetch_status=0
begin
declare c2 cursor for select userno from basic
open c2
fetch next from c2 into @userno
while @@fetch_status=0
	begin
	if not exists(select * from servicepermission where userno=@userno and serviceno=@serviceno)
	insert into servicepermission (serviceno,userno,permission,outlet,outlettype)
	values (@serviceno,@userno,'N',0,@outlettype)
	fetch next from c2 into @userno
	end
close c2
deallocate c2
fetch next from c1 into @serviceno
end
close c1
deallocate c1

set @outlettype=6
declare c1 cursor for select serviceno from servicenumbers where outlettype=@outlettype
open c1
fetch next from c1 into @serviceno
while @@fetch_status=0
begin
declare c2 cursor for select userno from basic
open c2
fetch next from c2 into @userno
while @@fetch_status=0
	begin
	if not exists(select * from servicepermission where userno=@userno and serviceno=@serviceno)
	insert into servicepermission (serviceno,userno,permission,outlet,outlettype)
	values (@serviceno,@userno,'N',0,@outlettype)
	fetch next from c2 into @userno
	end
close c2
deallocate c2
fetch next from c1 into @serviceno
end
close c1
deallocate c1

set @outlettype=20
declare c1 cursor for select serviceno from servicenumbers where outlettype=@outlettype
open c1
fetch next from c1 into @serviceno
while @@fetch_status=0
begin
declare c2 cursor for select userno from basic
open c2
fetch next from c2 into @userno
while @@fetch_status=0
	begin
	if not exists(select * from servicepermission where userno=@userno and serviceno=@serviceno)
	insert into servicepermission (serviceno,userno,permission,outlet,outlettype)
	values (@serviceno,@userno,'N',0,@outlettype)
	fetch next from c2 into @userno
	end
close c2
deallocate c2
fetch next from c1 into @serviceno
end
close c1
deallocate c1

set @outlettype=0
declare c1 cursor for select serviceno from servicenumbers where outlettype=@outlettype
open c1
fetch next from c1 into @serviceno
while @@fetch_status=0
begin
declare c2 cursor for select userno from basic
open c2
fetch next from c2 into @userno
while @@fetch_status=0
	begin
	if not exists(select * from servicepermission where userno=@userno and serviceno=@serviceno)
	insert into servicepermission (serviceno,userno,permission,outlet,outlettype)
	values (@serviceno,@userno,'N',0,@outlettype)
	fetch next from c2 into @userno
	end
close c2
deallocate c2
fetch next from c1 into @serviceno
end
close c1
deallocate c1

set @outlettype=3
declare c1 cursor for select serviceno from servicenumbers where outlettype=@outlettype
open c1
fetch next from c1 into @serviceno
while @@fetch_status=0
begin
declare c2 cursor for select userno from basic
open c2
fetch next from c2 into @userno
while @@fetch_status=0
	begin
	if not exists(select * from servicepermission where userno=@userno and serviceno=@serviceno)
	insert into servicepermission (serviceno,userno,permission,outlet,outlettype)
	values (@serviceno,@userno,'N',0,@outlettype)
	fetch next from c2 into @userno
	end
close c2
deallocate c2
fetch next from c1 into @serviceno
end
close c1
deallocate c1




END




GO
/****** Object:  StoredProcedure [dbo].[setcat]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[setcat]  @formname varchar(50),@controlno int , @controlvalue int , @printername varchar(200)
/* with encryption */
AS
declare @usernm varchar(50)
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
if not exists (select * from settingcatelog where computername=@usernm and formname=@formname and controlno=@controlno)
insert into settingcatelog (computername,formname,controlno,controlvalue) values (@usernm,@formname,@controlno,@controlvalue)
update settingcatelog set controlvalue=@controlvalue, printername=@printername where computername=@usernm and formname=@formname and controlno=@controlno 
return




GO
/****** Object:  StoredProcedure [dbo].[SETOPGBAL]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SETOPGBAL]
	-- Add the parameters for the stored procedure here
@FY INT
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

	DECLARE @M_COMPANY INT
DECLARE @M_FYDATE DATETIME
DECLARE @P_FYDATE DATETIME
DECLARE @M_FY INT
DECLARE @P_FY INT
DECLARE @NEXTYEARAC NUMERIC
DECLARE @BALANCE MONEY
SET @M_FY=@FY
SELECT @M_COMPANY=COMPANY FROM FINYEAR WHERE FY=@FY
UPDATE GLPRIMLD SET OPGBAL=0 WHERE FY=@M_FY
SELECT @M_FYDATE=[DATE] FROM FINYEAR WHERE COMPANY=@M_COMPANY AND FY=@M_FY
SELECT @P_FYDATE= DATEADD(YEAR,-1,@M_FYDATE)
SELECT @P_FY=FY FROM FINYEAR WHERE COMPANY=@M_COMPANY AND datediff(d,'1/1/90',[DATE] )= datediff(d,'1/1/90',@P_FYDATE)

if cursor_status('global','c1') > 0
begin
close c1
deallocate c1
end

DECLARE C1 CURSOR FOR SELECT NEXTYEARAC,BALANCE FROM  GLPRIMLD WHERE FY=@P_FY AND GRP<> 7
OPEN C1
FETCH NEXT FROM C1 INTO @NEXTYEARAC,@BALANCE
WHILE @@FETCH_STATUS=0
BEGIN
UPDATE GLPRIMLD SET OPGBAL=@BALANCE WHERE FY=@M_FY AND CGP_ACTNO=@NEXTYEARAC
FETCH NEXT FROM C1 INTO @NEXTYEARAC,@BALANCE
END
CLOSE C1
DEALLOCATE C1
UPDATE GLPRIMLD SET OPGBAL=OPGBAL+(SELECT SUM(BALANCE) FROM GLPRIMLD WHERE FY=@P_FY AND GRP=7),
BALANCE=OPGBAL+(SELECT SUM(BALANCE) FROM GLPRIMLD WHERE FY=@P_FY AND GRP=7)
WHERE FY=@M_FY AND CGP_ACTNO=52

    -- Insert statements for procedure here
END
GO
/****** Object:  StoredProcedure [dbo].[setserialno]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[setserialno] 
/* with encryption */
AS
update serialno set cnt=cnt+1




GO
/****** Object:  StoredProcedure [dbo].[shiftf12]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[shiftf12] @dt datetime=null,@showall int=0
/* with encryption */
AS


declare @company numeric
declare @sql varchar(8000)
declare @sql2 varchar(8000)
declare @sql3 varchar(8000)
declare @sql4 varchar(8000)
declare @sql5 varchar(8000)
declare @sql6 varchar(8000)
declare @sql7 varchar(8000)
declare @sql8 varchar(8000)
if exists (select name,xtype from sysobjects where name = 'temps12' and xtype = 'U') drop table temps12

set @dt = isnull(@dt,getdate())
exec  fillfyforlinker @dt

set @sql='set nocount on select linkid,dummy=0,company,fy, aname=(select cgp_desc from glprimld where cgp_actno=actnum and fy=linker.fy),
balance1=(select balance from glprimld where cgp_actno=actnum and fy=linker.fy),hyper1=''gltxn.aspx?fy=''+ltrim(rtrim(str(fy)))+''&actnum=''+ ltrim(str(actnum))'

set @sql2 = 'update #dummy1 set dummy=0'
set @sql3 = @sql2
set @sql4 = @sql2
set @sql5 = 'balance1'
set @sql6 = 'hyper1,balance1,fy,company'
set @sql7= @sql2
set @sql8 = 'abs(balance1)'
declare c1 cursor for select company from company where company<>1 and link<>'N' order by company
open c1
fetch next from c1 into @company
while @@fetch_status=0
begin
set @sql = @sql + ',actnum' + ltrim(rtrim(str(@company))) + '=0' 
set @sql = @sql + ',fy' + ltrim(rtrim(str(@company))) + '=0'
set @sql = @sql + ',company' + ltrim(rtrim(str(@company))) + '=0'

--set @sql = @sql +  ',hyper' + ltrim(str(@company)) + '=''gltxn.aspx?fy='' + ltrim(str(fy' +ltrim(str(@company))+')) +''&actnum=''+ ltrim(str(actnum' + ltrim(str(@company)) + ')) ,balance' + ltrim(rtrim(str(@company))) + '=0'
--set @sql = @sql + ',balance' + ltrim(rtrim(str(@company))) + '=0'
set @sql = @sql +',balance' + ltrim(rtrim(str(@company))) + '=0,hyper' + ltrim(str(@company)) + '=''                                                                                                                          '''
set @sql2 = @sql2 + ',fy' + ltrim(rtrim(@company)) + '=isnull((select top 1 fy from linker where company=' + str(@company) + ' and linkid=#dummy1.linkid),0),company' + ltrim(str(@company)) + '=' + ltrim(str(@company))
set @sql3 = @sql3 + ',actnum' + ltrim(rtrim(@company)) + '=isnull((select top 1 actnum from linker where company=' + str(@company) + 'and linkid=#dummy1.linkid),0)'
set @sql4 = @sql4 + ',balance' + ltrim(rtrim(str(@company)))+ '= isnull((select balance from glprimld where fy=#dummy1.fy' + ltrim(rtrim(str(@company))) + ' and cgp_actno=#dummy1.actnum' + ltrim(str(@company)) + '),0)'
set @sql7 = @sql7 + ',hyper' + ltrim(str(@company)) + '=''gltxn.aspx?fy='' + ltrim(str(fy' +ltrim(str(@company))+')) +''&actnum=''+ ltrim(str(actnum' + ltrim(str(@company)) + '))'
set @sql5 = @sql5 + '+balance' + ltrim(rtrim(str(@company)))
set @sql8 = @sql8 + '+abs(balance' + ltrim(rtrim(str(@company))) + ')'
set @sql6 = @sql6 + ',hyper' + ltrim(str(@company))+  ',balance' +  ltrim(rtrim(str(@company))) +  ',fy' +  ltrim(rtrim(str(@company))) +  ',company' +  ltrim(rtrim(str(@company)))
fetch next from c1 into @company
end
close c1
deallocate c1

set @sql = @sql + 'into #dummy1 from linker where company=1      '
if @showall=0
set @sql = @sql +' ' +  @sql2 + ' ' + @sql3 + ' ' + @sql4 + ' ' +@sql7 +  ' set nocount off  select aname,debitbal=case when ' + @sql5 + ' < 0 then ' + @sql5 + ' else 0 end, creditbal=case when ' + @sql5 + ' > 0 then ' + @sql5 + ' else 0 end,' + @sql6 + '  into temps12 from #dummy1 where ' + @sql5 + ' <> 0 order by aname'
else
set @sql = @sql +' ' +  @sql2 + ' ' + @sql3 + ' ' + @sql4 + ' ' +@sql7 +  ' set nocount off  select aname,debitbal=case when ' + @sql5 + ' < 0 then ' + @sql5 + ' else 0 end, creditbal=case when ' + @sql5 + ' > 0 then ' + @sql5 + ' else 0 end,' + @sql6 + '  into temps12 from #dummy1 where ' + @sql8 + ' <> 0 order by aname'
exec (@sql )
--print @sql



GO
/****** Object:  StoredProcedure [dbo].[smsinfo]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[smsinfo] @txndt datetime,@rooms numeric out, @cash money out ,@totrent money out, @banquet money out 
/* with encryption */
AS
declare @dt datetime
declare @bofy datetime
declare @year numeric
declare @rec money
declare @pay money
declare @dt1 datetime
declare @dt2 datetime
declare @ciexcuse numeric
declare @rent money
declare @getdate datetime
select @ciexcuse=g_cit_excuse from setting
set @getdate=getdate()
set @dt = dateadd(d,1,@txndt)
set @dt1 = dateadd(n,720-@ciexcuse,@dt)
set @dt2 = dateadd(d,-1,@txndt)

if DATEDIFF(d,@dt,@getdate) <>0 begin print @txndt declare @err varchar(100) set @err='ERROR '+convert(varchar(20),@dt) raiserror(@err,16,-1) return end

select @rooms=count(*),@rent=sum(rent) from 
(select rent=dbo.findrentforoneday(guest.gno,@txndt,@getdate) from guest where co_date is null and datediff(s,@txndt,ci_date) <= datediff(s,@txndt,@dt1)) a
 
set @totrent=@rent
select @rent=sum((isnull(rent,0)-isnull(discount,0)+isnull(sc,0)+isnull(st,0)+isnull(lt,0)+isnull(cess,0)+isnull(cess2,0))),@year=count(*) from billhist where rent<>0 and datediff(d,txndt,@txndt)=0 
set @totrent=isnull(@totrent,0) + isnull(@rent,0)
set @rooms=@rooms+@year

select @rent=sum(isnull(rent,0)-isnull(discount,0)+isnull(sc,0)+isnull(st,0)+isnull(lt,0)+isnull(cess,0)+isnull(cess2,0)),@year=count(*) from billhist where rent<>0 and datediff(d,txndt,@dt2)=0 
and exists(select * from guest where gno=billhist.gno and datediff(d,ci_date,@txndt)=0)
set @totrent=isnull(@totrent,0) + isnull(@rent,0)
set @rooms=@rooms+@year
 

select @cash=sum(amt+tax-discount+roamt+scamt+servicetax+cess+st1+cess1+sc1)  from (
select outlet,pmode,split,roamt,scamt,servicetax,cess ,
(select sum(qty*rate) from kithist where bno=kithist1.bno) amt ,
(select sum(discount) from kithist where bno=kithist1.bno) discount,
(select sum(tax) from kithist where bno=kithist1.bno) tax ,
(select sum(st) from kithist where bno=kithist1.bno) st1 ,
(select sum(cess) from kithist where bno=kithist1.bno) cess1 ,
(select sum(sc) from kithist where bno=kithist1.bno) sc1

from kithist1 
where outlet in (select code from outlet where channel in (select channel from restco where info='Y')) and 
datediff(d,txndt,@txndt)=0 and pmode < 6
and  split not in  ('F','C'))  a 

set @cash=isnull(@cash,0)
set @rooms = isnull(@rooms,0)
select @banquet=sum(billamount) from banquet where status='B' and datediff(d,billdate,@txndt) = 0 
set @banquet=isnull(@banquet,0)




GO
/****** Object:  StoredProcedure [dbo].[sp_cb]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sp_cb    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE procedure [dbo].[sp_cb]  @temptable varchar(50), @gno int,@mark int=0
/* with encryption */
as
declare @sql varchar(5000)
declare @company int
declare @channel int
declare @bp1 int
declare @txndt datetime
declare @amt money
declare @rent money
declare @discount money
declare @item1 money
declare @item2 money
declare @item3 money
declare @item4 money
declare @item5 money
declare @item6 money
declare @item7 money
declare @item8 money
declare @item9 money
declare @item10 money
declare @lt money
declare @sc numeric(18,2)
declare @total money
declare @sdt datetime
declare @edt datetime
declare @lrate money
declare @samt money
declare @eamt money
declare @famt money
declare @ltamt numeric(18,2)
declare @amt2 money
declare @tno int
declare @gtotal money
declare @ctotal money
declare @billorder int
declare @section char(1)
declare @temptabletemp varchar(50)
declare @hostid char(18)
declare @outlet1 int
declare @outlet2 int
declare @outlet3 int
declare @outlet4 int
declare @outlet5 int
declare @outlet6 int
declare @outlet7 int
declare @outlet8 int
declare @outlet9 int
declare @outlet10 int
declare @yesitis int
declare @totrooms int
declare @temp varchar(20)
declare @ltcal char(1)
declare @ltontariff char(1)
declare @head int
declare @temptot money
declare @temptot1 money
declare @temptot2 money
declare @g_disc_ltrules tinyint
--declare @taxsystem int
--declare @stamt decimal(12,2)
declare @st numeric(18,2)
declare @stontariff char(3)
declare @strate money
declare @stdiscount money
declare @stlessamt numeric(18,2)
declare @cessrate money
declare @cess numeric(18,2)
declare @cess2 numeric(18,2)
declare @cessrate2 numeric(18,2)
declare @cessonwhat2 tinyint
declare @allinclusive char(1)
declare @renttype int
declare @nettrent numeric(18,2)
declare @lttype int
declare @NREXST tinyint
declare @stcal tinyint
declare @difamt money
declare @extrabed money
declare @exbedgst money
declare @food money
declare @foodgst money

declare @tariff money
declare @errmsg varchar(8000)
declare @oextrabed decimal 
declare @ofood decimal
declare @amt1 money

set @errmsg = 'Bill Creation Error'
--set @taxsystem=0
--select @taxsystem=g_taxsystem from setting
set nocount on
begin transaction

set @hostid = @temptable
set @temp=@temptable
if @mark=0
	if not  exists (select * from room where gno=@gno and hostid = (select computer from comuser where [host_id]=host_id())  and checkout = 'Y')
		if not  exists (select * from room where gno = @gno and checkout<> 'Y') 
			begin
			print 'Either the selected room is already checked out from some other computer or is in the Check-Out Process'
			goto e_error
			end

if @mark=0 update room set checkout='Y',  hostid = (select computer from comuser where [host_id]=host_id())  where gno = @gno

select @NREXST=nettrentexcludingst,@ltcal=lt,@stcal=st,@renttype=renttype1,@nettrent=nettrent,@lttype=lttype  from guest where gno = @gno

set @billorder = 1
set @yesitis = 1

declare cur_restco cursor for select company,channel,billposition1 from restco order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
set @cess=0
set @st = 0
	if @company=1
		begin
			declare cur_kitroom cursor for 
			select txndt,amt from (
			select txndt,amt=finalamt,
			kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
			from kithist1 
			join co_rest_ol1 kit on kithist1.outlet=kit.ocode
			where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and lux='Y'  and split='N'
		end
	else
		begin
			declare cur_kitroom cursor for 
			select txndt,amt from (
			select txndt,amt=finalamt ,
			kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
			from kithist1 
			join co_rest_ol1 kit on kithist1.outlet=kit.ocode
			where gno = @gno and pmode=2)  a where company=@company   and channel=@channel and lux='Y' and selfdeal='N' and split='N'
		end
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
	begin
		set @yesitis = 1
		--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,0,0)'
		set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
		exec(@sql)
		if @bp1<>0
		begin
			set @sql = 'update  ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
			+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			exec(@sql)
			if @@error<>0
			begin
				close cur_kitroom
				deallocate cur_kitroom
				close cur_restco
				deallocate cur_restco
				goto e_error
			end
		end
		fetch next from cur_kitroom into @txndt,@amt
	end
	close cur_kitroom
	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
-- why here the diff in company 1 and other is not specified is due  to the billpostion1 field
--1

declare cur_restco cursor for select company,outlet,billposition1 from phoutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and split = 'N' and lux = 'Y' group by txndt
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)+scamt+stamt-damt
from phone where gno=@gno and pmode=2 and outlet=@channel and split='N' and lux='Y'
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
exec(@sql) 
if @bp1<>0
begin
set @sql = 'update  ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  + ' where datediff(d,''1/1/80'',txndt)='+str(datediff(d,'1/1/80',@txndt))
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
-------- first  try for travel desk 
--2
declare cur_restco cursor for select company,outlet,billposition1 from toutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and split = 'N' and lux = 'Y' group by finaldate
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1


set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
exec(@sql)

if @bp1<>0
begin
set @sql = 'update  ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 300 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  
+ ' where datediff(d,''1/1/90'',txndt)=datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt) + ''')'
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
-----traveldesk ends
--print 'peria'
--if @ltcal='Y'
--begin
set @totrooms = 1 
select @totrooms=count(*) from addroom where gno=@gno
if @totrooms < 1 set @totrooms=1
declare @crno numeric
set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,rno,head,st,cess,cess2,extrabed,food  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@crno,@head,@st,@cess,@cess2,@extrabed,@food
set @ctotal = 0
while @@fetch_status=0
begin
set @oextrabed = @extrabed
set @ofood = @food
set @exbedgst=0
set @foodgst=0
	if @ltcal='Y'   -- calculate lt
			begin
			set @sql = 'update ' + @temptable  + ' set taxable=' +convert (varchar(14),@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10) + ' where current of cur_temp'
			exec(@sql)
			set @ltamt = 0
			declare @ltamttemp money
			declare @amttemp money
			declare @amttemp1 money
			declare @g_ltonsc tinyint
			set @ltamttemp=0
			set @amttemp=@rent
IF datediff(d,@txndt,'31 jan 9999')<>0
			IF NOT EXISTS(SELECT * FROM ltontariff where datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
				BEGIN
					PRINT 'Your LT On Tariff Table is set properly..Checkout Rejected'  + CONVERT(VARCHAR(20),@TXNDT)
					goto e_error
				end
				declare @ltrateontariff char(1)
			select @ltontariff=ltontariff,@ltrateontariff=ltrateontariff from ltontariff where datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)
			select  top 1  @g_disc_ltrules=g_disc_ltrules ,@g_ltonsc=g_ltonsc from setting
			--set @amttemp1=@rent
			if @rent<>0
			---if @ltontariff='Y'   
			exec findrent @renttype,@crno,@head,@txndt,0,@amttemp out

			set @tariff = @amttemp 
			
			if @ltrateontariff='Y'  --NEW ON 22 SEP 2017
				set @amttemp1= @rent --case when @rent > @tariff then @rent else @tariff end
			else
				if @ltontariff='Y'
				set @amttemp1= case when @rent > @amttemp then @rent else @amttemp end
				else
				set @amttemp1 = @rent

			set @amttemp = @amttemp1

			if @ltontariff='Y'
				set @amt = @amttemp+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10
			else
				if @g_disc_ltrules=0
					set @amt = @amttemp+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10-@discount
				else
					set @amt = @amttemp+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10

			set @amt= isnull(@amt,0)
	set @amt1=@amt
			if @g_ltonsc=1 set @amt=@amt+@sc
			if  @ltontariff<>'Y' set @amt = @amt/@totrooms
			select @tno=tno from ltmaster where convert(datetime,@txndt,106)  between sdt and edt
			declare @taxed money
			declare @nontaxable money
			set @taxed=0
			set @nontaxable=0
			--set @taxsystem=0
			--if @taxsystem=0
			--if @taxsystem=1 
			--	begin
			--		set @sql = 'declare cur_lttable cursor for select lrate,samt,eamt,famt from lttable where tno = ' +  ltrim(str(@tno))
			--		exec(@sql)
			--		open cur_lttable
			--		fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
			--		set @amttemp = @amt
			--		while @@fetch_status=0 and @amt > 0
			--			begin
			--				  if @eamt > @amt  set @amt2 = @amt   else   set @amt2 = @eamt
			--				  set @ltamt = round(@ltamt + ((@amt2 + @famt) * @lrate / 100),2)
			--			   if @lrate > 0 set @taxed=@taxed + @amt2  else set @nontaxable=@nontaxable+@amt2

			--				  set @amt = @amt - @amt2
			--				  if @eamt > @amttemp set @amt2=@amttemp else set @amt2 = @eamt

			--			if @ltontariff='Y'
			--				  set @ltamttemp =  round(@ltamttemp+((@amt2+@famt)*@lrate/100),2)
			--			else
			--				  set @ltamttemp =  @ltamttemp+round(((@amt2+@famt)*@lrate/100),2)
			--				  set @amttemp=@amttemp-@amt2
			--				  fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
			--			end
			--			close cur_lttable
			--			deallocate cur_lttable

			--	end
			--else

					if isnull(@amt,0) <> 0
					begin
					if @ltrateontariff='Y'
						set @sql = 'declare ct1 cursor for select lrate from lttable where tno=' + str(@tno) + ' and ' + 
						convert(varchar(20),case when (@tariff * 100) > (floor(case when @rent > @tariff then @rent else @tariff end) * 100) then floor(case when @rent > @tariff then @rent else @tariff end)+1 else case when @rent > @tariff then @rent else @tariff end end)
						+ ' between samt and eamt'
					else					
						set @sql = 'declare ct1 cursor for select lrate from lttable where tno=' + str(@tno) + ' and ' + 
						convert(varchar(20),case when (@amt * 100) > (floor(@amt) * 100) then floor(@amt)+1 else @amt end)
						+ ' between samt and eamt'
						exec(@sql)

						open ct1
						fetch next from ct1 into @lrate
						if @@fetch_status=0
						begin

									set @ltamttemp =  round(@amt*@lrate/100,2)
						end
						close ct1
						deallocate ct1
					end
				set @ltamt = round(@ltamttemp,2,1)

					set @sql = 'declare cur_lttable cursor for select lrate,samt,eamt,famt from lttable where tno = ' +  ltrim(str(@tno)) + ' and ' + str(@amt1) + 'between samt and eamt'
					exec(@sql)
					open cur_lttable
					fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
					while @@fetch_status=0 
					begin
					set @lrate=@lrate  
					fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
					end
					close cur_lttaBLE
					DEALLOCATE CUR_LTTABLE

				if @lrate<>0
					begin
						if @lttype=0 
						begin
							if @extrabed <> 0 set @exbedgst = @extrabed * @lrate/100
							if @food <> 0     set @foodgst = @food * @lrate/100
						end
						if @lttype=1 
						begin
							if @extrabed <> 0 
							begin
								set @exbedGST = (@lrate/(@LRATE+100)) * @extrabed
								set @exTRABED = @oextrabed - @exBEDGST
							end
							if @food <> 0     
								begin
								set @foodGST = (@lrate/(100+@LRATE)) * @FOOD
								set @food = @ofood - @foodgst
								end
						end
					end
--set @exbedgst = round(@exbedgst,2) 
--set @foodgst = round(@foodgst,2) 
					if @ltamt < 0 set @ltamt=0
					set @sql= 'update ' + @temptable + ' set lt = ' + convert(varchar(20),@ltamt)  + ',lrate=' + convert(varchar(20),@lrate) + ',exbedgst=' + convert(varchar(20),@exbedgst) + ',foodgst=' + convert(varchar(20),@foodgst) + ',extrabed=' + convert(varchar(20),@extrabed) + ',food=' + convert(varchar(20),@food) + '  where current of cur_temp'
					exec(@sql)
		end --- end of @ltcal='Y'
	else
		begin ----beginning of @ltcal<>'Y'
		set @sql = 'update ' + @temptable + ' set lt = 0 ,lrate=0,exbedgst=0,foodgst=0 where current of cur_temp'
		exec(@sql)
		end ---- end of @ltcal <>'Y'

	if @ltamt<>0
		begin
			set @temptot =  @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@ltamt-@discount
			set @temptot1 = @temptot*100
			set @temptot2 = floor(@temptot1/10)*10
			if @temptot1-@temptot2 = 1 set @ltamt=@ltamt-.01
			set @sql= 'update ' + @temptable + ' set lt = ' + convert(varchar(20),@ltamt)  +'  where current of cur_temp'
			exec(@sql)
		end

---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---- 27 apr 2011 (calculation of service tax)
--if @ltcal='Y'  -- added on 28-Jun-2013

declare @cessonwhat tinyint


if @stcal=0   -- added on 05 nov 2013
	if @rent<>0
	begin
		select @allinclusive=allinclusive from guest where gno=@gno
		select top 1  @stontariff=ontariff from sttable where (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		
		if upper(@stontariff)='YES'
			begin
				--set @tariff=    (select case when @head=1 then rent1 else rent2 end  from room where rno=@crno)
				exec findrent @renttype,@crno,@head,@txndt,0,@tariff out
				set @amttemp=case when @rent > @tariff then @rent else @tariff end
				--print convert(varchar(20),@amttemp)
				--goto e_error
			end
		else
			set @amttemp=@rent-@discount


			set @strate = 0
			set @cessrate = 0
			set @cessrate2 = 0 



		if exists(select * from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)))
       	select @cessonwhat=cessonwhat,@cessrate=cess,  @strate=rate,@stdiscount=discount,@stlessamt=lamt,
		@cessonwhat2=cessonwhat,@cessrate2=cess2
		from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		
		set @strate = isnull(@strate,0)
		set @stdiscount = isnull(@stdiscount,0)
		set @stlessamt=isnull(@stlessamt,0)
		set @cessrate = isnull(@cessrate,0)

		set @amttemp = @amttemp - @stlessamt
		set @st=0
		set @cess=0
		set @cess2=0
		if @amttemp>0 
			begin
			if @strate<>0
				set @st = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@strate/100)) as decimal(12,2))
			else
				set @st=0

				if @cessonwhat=0
							begin
								if @cessrate > 0 
									set @cess = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@cessrate/100)) as decimal(12,2))
							end
							else
							begin
								if @st > 0 
									if @cessrate > 0 
										set @cess = @st * @cessrate / 100
							end
				if @cessonwhat2=0
							begin
								if @cessrate2 > 0 
									set @cess2 = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@cessrate2/100)) as decimal(12,2))
							end
							else
							begin
								if @st > 0 
									if @cessrate2 > 0 
										set @cess2 = @st * @cessrate2 / 100
							end
			end
set @st = round(@st,2,1)
set @cess = round(@cess,2,1)
set @cess2 = round(@cess2,2,1)
		set @sql= 'update ' + @temptable + ' set st = ' + convert(varchar(20),@st)  +'  where current of cur_temp'
		exec(@sql)
		
		set @sql= 'update ' + @temptable + ' set cess = ' + convert(varchar(20),@cess)  +'  where current of cur_temp'
		exec(@sql)

		set @sql= 'update ' + @temptable + ' set cess2 = ' + convert(varchar(20),@cess2)  +'  where current of cur_temp'
		exec(@sql)

	end
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

	if @ltcal='Y'   -- calculate lt (no tax exemption)
	if @lttype=1
		BEGIN
	   if @nrexst=0
			begin
			-- next snippet added on 09 August 2012 After skype with bala
			if exists(select * from grent where gno=@gno)
			if exists(select * from grent where gno=@gno and datediff(n,'1/1/2000',@txndt + ' 12:00:00') between datediff(n,'1/1/2000',ci_date) and  datediff(n,'1/1/2000',co_date)-1)
				select @nettrent=nettrent from grent where gno=@gno and datediff(n,'1/1/2000',@txndt + ' 12:00:00') between datediff(n,'1/1/2000',ci_date) and  datediff(n,'1/1/2000',co_date)-1 
			else
				select @nettrent=nettrent from grent where gno=@gno and datediff(d,'1/1/2000',@txndt) >= datediff(d,'1/1/2000',ci_date)
                set @difamt=abs(@nettrent - (@rent + (@ltamt-@discount+@st+@sc+@cess)))
				if @difamt <> 0 and @difamt < 10 
					begin
 					set @rent = @nettrent - (@ltamt-@discount+@st+@sc+@cess+@CESS2)
					end
			end
        else
			begin
				set @difamt = abs(@nettrent - (@rent + (@ltamt-@discount+@sc)))
				if @difamt <> 0 and @difamt < 10 
					set @rent = @nettrent - (@ltamt-@discount+@sc)
			end
		END
	set @sql = 'update ' + @temptable + ' set rent=' + convert(varchar(20),@rent) + ' where current of cur_temp'
	exec(@sql)
	set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st+cess+cess2+extrabed+exbedgst+food+foodgst where current of cur_temp'
	exec (@sql)
	--set @ctotal = @ctotal + @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@ltamt-@discount+@st+@cess+@cess2+@extrabed+@exbedgst+@food+@foodgst
	set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)+isnull(@sc,0)-isnull(@discount,0)+isnull(@st,0) + isnull(@cess,0) + isnull(@cess2,0) + isnull(@extrabed,0) + isnull(@exbedgst,0) + isnull(@food,0) + isnull(@foodgst,0)

	fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@crno,@head,@st,@cess,@cess2,@extrabed,@food
end
close cur_temp
deallocate cur_temp
--end  -- end of @ltcal='Y'
-------------------------------------------------------------------
-------------------------------------------------------------------
-------------------------------------------------------------------
--3

-- here starts the no lt calculation 
declare cur_restco cursor for select company,channel,billposition1 from restco order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--if @company=1
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'N' and lux <> 'Y' group by txndt
--else
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'N' and lux <> 'Y' and selfdeal = 'N' group by txndt
if @company=1
	begin
	declare cur_kitroom cursor for 
	select txndt,amt from (
	select txndt,amt=finalamt,
	kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
	from kithist1 
	join co_rest_ol1 kit on kithist1.outlet=kit.ocode
	where gno = @gno and pmode=2)  a where company=1 and  channel=@channel and  lux<>'Y'  and split='N'
	end
else
---@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  06/07/2016
	begin
		declare cur_kitroom cursor for 
		select txndt,amt from (
		select txndt,amt=finalamt,
		kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
		from kithist1 
		join co_rest_ol1 kit on kithist1.outlet=kit.ocode
		where gno = @gno and pmode=2)   a where company=@company and  channel=@channel and  lux<>'Y'  and  selfdeal='N' and split='N'
	end

	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
	begin
	set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
	exec(@sql)
	if @bp1<>0
	begin
		set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)
		+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
		exec(@sql)
		if @@error<>0
			begin
			close cur_kitroom
			deallocate cur_kitroom
			close cur_restco
			deallocate cur_restco
			goto e_error
			end
	end
	fetch next from cur_kitroom into @txndt,@amt
	end
	close cur_kitroom
	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
	end
	close cur_restco
	deallocate cur_restco

declare cur_restco cursor for select company,outlet,billposition1 from phoutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
	begin
	---declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and split = 'N' and lux <> 'Y' group by txndt
	declare cur_kitroom cursor for 
	select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)+scamt+stamt-damt
	from phone where gno=@gno  and pmode=2 and outlet=@channel and split='N' and lux<>'Y'
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
		begin
		set @yesitis = 1
		--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
		set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
		exec(@sql)
		if @bp1 <> 0
			begin
			set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)
			+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			exec(@sql)

			if @@error<>0
				begin
				close cur_kitroom
				deallocate cur_kitroom
				close cur_restco
				deallocate cur_restco
				goto e_error
				end
			end
		fetch next from cur_kitroom into @txndt,@amt
		end
	close cur_kitroom
	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
	end
close cur_restco
deallocate cur_restco


-------- first  try for travel desk 
declare cur_restco cursor for select company,outlet,billposition1 from toutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
	begin
	declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and split = 'N' and lux <> 'Y' group by finaldate
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
		begin
		set @yesitis = 1
		--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
		set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
		exec(@sql)
		if @bp1<>0
			begin
			set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 300 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)
			+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			exec(@sql)
			if @@error<>0
				begin
				close cur_kitroom
				deallocate cur_kitroom
				close cur_restco
				deallocate cur_restco
				goto e_error
				end
			end
		fetch next from cur_kitroom into @txndt,@amt
		end
	close cur_kitroom
	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
	end
close cur_restco
deallocate cur_restco

-----traveldesk ends
set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,st,cess,cess2,extrabed,exbedgst,food,foodgst  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess,@extrabed,@exbedgst,@food,@foodgst
set @ctotal = 0
while @@fetch_status=0
	begin
		set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0)+isnull(cess2,0)  + isnull(extrabed,0) + isnull(exbedgst,0) + isnull(food,0) + isnull(foodgst,0) where current of cur_temp'
		exec (@sql)
		set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)+isnull(@sc,0)-isnull(@discount,0)+isnull(@st,0) + isnull(@cess,0) + isnull(@cess2,0) + isnull(@extrabed,0) + isnull(@exbedgst,0) + isnull(@food,0) + isnull(@foodgst,0)
		fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	end
close cur_temp

if @yesitis=1
	begin
		--set @sql = 'insert into ' + @temptable  + ' ( total,txndt)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31 jan 9999'' )'
		set @sql = 'insert into ' + @temptable  + ' ( total,txndt, rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (' + convert(varchar(20),isnull(@total,0)) + ',''31 jan 9999'',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0, 0,0,0, 0,0,0,0)'
		exec (@sql)
		set @sql = 'update ' + @temptable + ' set billorder=1,section=''A'''
		exec (@sql)
	end
deallocate cur_temp


--here ends no lt calculation
--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--------------------------------------------------------------------

set @temptabletemp = @temptable
set @temptable = @temptable + 't'
set @sql = 'if exists (select name,xtype from sysobjects where name = ''' + @temptable +''' and xtype = ''u'') drop table ' + @temptable
exec (@sql)
set @sql = 'select * into ' + @temptable + '  from  dummybill'
exec (@sql)
--if @@error<>0 begin raiserror('New Error',16,-1) end
--print @temptable + '  test' + @sql goto e_error

---------------------------------------------------here starts the sepaerate bill calculation
set @ltamt=0
set @yesitis = 0
declare cur_restco cursor for select company,channel,billposition1 from restco order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
	begin
	if @company=1
		begin
		declare cur_kitroom cursor for 
		select txndt,amt from (
		select txndt,amt=finalamt,
		kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
		from kithist1 
		join co_rest_ol1 kit on kithist1.outlet=kit.ocode
		where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and split='Y'
		end
	else
		begin
		declare cur_kitroom cursor for 
		select txndt,amt from (
		select txndt,amt=finalamt,
		kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
		from kithist1 
		join co_rest_ol1 kit on kithist1.outlet=kit.ocode
		where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and selfdeal='N' and split='Y'
		end
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
		begin
		set @yesitis = 1
		--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
		set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
		exec(@sql)
		if @@error <> 0 begin print 'Error' goto e_error end
		if @bp1 <> 0
			begin
			set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
			+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			exec(@sql)
			if @@error<>0
				begin
				close cur_kitroom
				deallocate cur_kitroom
				close cur_restco
				deallocate cur_restco
				goto e_error
				end
			end
		fetch next from cur_kitroom into @txndt,@amt
		end
	close cur_kitroom
	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
	end
close cur_restco
deallocate cur_restco

declare cur_restco cursor for select company,outlet,billposition1 from phoutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
	begin
	declare cur_kitroom cursor for 
	select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)+scamt+stamt-damt
	from phone where gno=@gno and pmode=2  and outlet=@channel and split='Y'
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
		begin
		set @yesitis = 1
		--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
		set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
		exec(@sql)
		if @@error <> 0 begin print 'Error' goto e_error end
		if @bp1 <> 0
			begin
			set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
			+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			exec(@sql)
			if @@error<>0
				begin
				close cur_kitroom
				deallocate cur_kitroom
				close cur_restco
				deallocate cur_restco
				goto e_error
				end
			end
		fetch next from cur_kitroom into @txndt,@amt
		end
	close cur_kitroom
	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
	end

close cur_restco
deallocate cur_restco
-------- first  try for travel desk 


declare cur_restco cursor for select company,outlet,billposition1 from toutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
	begin
	declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and split = 'Y'   group by finaldate
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
		begin
		set @yesitis = 1
		--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
		set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
		exec(@sql)
		if @@error <> 0 begin print 'Error' goto e_error end

		if @bp1<>0
			begin
			set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 300 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
			+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			exec(@sql)
			if @@error<>0
				begin
				close cur_kitroom
				deallocate cur_kitroom
				close cur_restco
				deallocate cur_restco

				goto e_error
				end
			end
		fetch next from cur_kitroom into @txndt,@amt
		end
	close cur_kitroom
	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
	end
close cur_restco
deallocate cur_restco

-----traveldesk ends
--print 'peria'
set @sql = 'declare cur_temp cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),st,cess,cess2,extrabed,exbedgst,food,foodgst  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
set @ctotal = 0
while @@fetch_status=0
	begin
	set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0)+isnull(cess2,0)+isnull(extrabed,0)+isnull(exbedgst,0)+isnull(food,0)+isnull(foodgst,0) where current of cur_temp'
	exec (@sql)
	set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)+isnull(@cess2,0) + isnull(@extrabed,0) + isnull(@exbedgst,0) + isnull(@food,0) + isnull(@foodgst,0)  
	fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	end
	close cur_temp
	deallocate cur_temp
--******************************************************
--******************************************************
--******************************************************
--******************************************************
--******************************************************
if @yesitis=1 
	begin
	set @sql = 'insert into ' + @temptable + ' (total,txndt) values( ' +  convert(varchar(14),isnull(@ctotal,0) ) + ','' 31 jan 9999'' )'
	exec (@sql)
	if @@error<>0 begin print 'Section B Error' goto e_error end
	set @sql = 'update ' + @temptable + ' set billorder=1,section=''B'''
	exec (@sql)
	end
--******************************************************
--******************************************************
--******************************************************
--******************************************************
--******************************************************
--******************************************************
--print 'periyakaruppa 3445'

--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--section B bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
--set @sql = 'drop table tempram select * into tempram from ' + @temptable
--exec(@sql) 
set @sql = 'declare t_cur cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),billorder,section,isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0),isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0),st,cess,cess2,extrabed,exbedgst,food,foodgst   from ' + @temptable
exec (@sql)
open t_cur
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
while @@fetch_status=0
	begin
	set @st = 0
	set @cess = 0
	set @cess2 = 0
	set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst)  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
	set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
	set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
	set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' +  convert(varchar(20),@st)  + ',' +  convert(varchar(20),@cess) + ',' +  convert(varchar(20),@cess2) + ',' 
	set @sql = @sql +  convert(varchar(20),isnull(@extrabed,0)) + ',' +  convert(varchar(20),isnull(@exbedgst,0)) + ',' +  convert(varchar(20),isnull(@food,0)) + ',' +  convert(varchar(20),isnull(@foodgst,0)) + ')'
	exec(@sql)
	if @@error <> 0
		begin
		close t_cur
		deallocate t_cur
		goto e_error
		end
	fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	end
close t_cur
deallocate t_cur


--here ends seperate bills calculations
-- no mistake upto here
--print 'ramanathan' + @temptable
--------------------------------------------------------------------
set @sql ='truncate table ' + @temptable ------------------------------------------------------- other company bills starts here
exec(@sql)
--if @company > 1 print @temptabletemp goto e_error


declare cur_co cursor for select company from company where company > 1 order by company
open cur_co
fetch next from cur_co into @company
while @@fetch_status=0
	begin
	set @yesitis = 0
	declare cur_restco cursor for select company,channel,billposition2 from restco where company = @company  and billposition2<>0 order by billposition2
	open cur_restco
	fetch next from cur_restco into @company,@channel,@bp1 
	while @@fetch_status=0
	begin
	--      declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel  and selfdeal='Y' and split='N'  group by txndt
	declare cur_kitroom cursor for 
	select txndt,amt from (
	select txndt,amt=finalamt,
	kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
	from kithist1 
	join co_rest_ol1 kit on kithist1.outlet=kit.ocode
	where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and split='N' and selfdeal='Y'
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
	begin
	set @yesitis = 1
	--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
	exec(@sql)
		if @bp1<>0 
		begin
				set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
	+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
				exec(@sql)
					if @@error<>0
					begin 
						close cur_kitroom 
					deallocate cur_kitroom
					close cur_restco 
					deallocate cur_restco
					goto e_error
					end
		end
		fetch next from cur_kitroom into @txndt,@amt
	end
	close cur_kitroom 
	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
	end
	close cur_restco
	deallocate cur_restco

	declare cur_restco cursor for select company,outlet,billposition2 from phoutlet where company=@company and billposition2<>0 order by billposition2
	open cur_restco
	fetch next from cur_restco into @company,@channel,@bp1 
	while @@fetch_status=0
		begin
		declare cur_kitroom cursor for 
		select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)+scamt+stamt-damt
		from phone where gno=@gno  and pmode=2 and outlet=@channel and split='N' and selfdeal='Y'
		open cur_kitroom
		fetch next from cur_kitroom into @txndt,@amt  
		while @@fetch_status=0
			begin
			set @yesitis = 1
			--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
			set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
			exec(@sql)
			if @bp1<>0
				begin
				set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
				+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			    exec(@sql)
				if @@error<>0
				begin
					close cur_kitroom
					deallocate cur_kitroom
					close cur_restco
					deallocate cur_restco
					goto e_error
				end
				end
			fetch next from cur_kitroom into @txndt,@amt
			end
		close cur_kitroom 
		deallocate cur_kitroom
		fetch next from cur_restco into @company,@channel,@bp1
		end
	close cur_restco
	deallocate cur_restco

	/* here starts travel desk */
	declare cur_restco cursor for select company,outlet,billposition2 from toutlet where company=@company and billposition2<>0 order by billposition2
	open cur_restco
	fetch next from cur_restco into @company,@channel,@bp1
	while @@fetch_status=0
		begin
		declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and selfdeal= 'Y'  and split='N'  group by finaldate
		open cur_kitroom
		fetch next from cur_kitroom into @txndt,@amt
		while @@fetch_status=0
			begin
			set @yesitis = 1
			--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
			set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
			exec(@sql)
			if @bp1<>0
				begin
				set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 300 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  
				+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
				exec(@sql)
				if @@error<>0
					begin
					close cur_kitroom
					deallocate cur_kitroom
					close cur_restco
					deallocate cur_restco
					goto e_error
					end
				end
			fetch next from cur_kitroom into @txndt,@amt
			end
		close cur_kitroom
		deallocate cur_kitroom
		fetch next from cur_restco into @company,@channel,@bp1
		end
	close cur_restco
	deallocate cur_restco
	/*here ends travel desk*/

	--set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,st,cess,cess2  from ' + @temptable
	set @sql = 'declare cur_temp cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),st,cess,cess2,extrabed,exbedgst,food,foodgst  from ' + @temptable

	exec (@sql)
	open cur_temp
	fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	set @ctotal = 0
	while @@fetch_status=0
	begin
	set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st+cess+cess2+extrabed+exbedgst+food+foodgst  where current of cur_temp'
	exec (@sql)
	--set @ctotal = @ctotal + @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@lt+@sc-@discount+@st+@cess+@cess2
	set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)+isnull(@sc,0)-isnull(@discount,0)+isnull(@st,0) + isnull(@cess,0) + isnull(@cess2,0) + isnull(@extrabed,0) + isnull(@exbedgst,0) + isnull(@food,0) + isnull(@foodgst,0)
	fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	end
	close cur_temp
	deallocate cur_temp

	if @yesitis=1
		begin
		set @sql = 'insert into ' +  @temptable  + ' ( total,txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst)  values( ' + convert(varchar(14),isnull(@ctotal,0) ) + ', '' 31jan 9999'',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'' '' ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
		exec (@sql)
		if @@error <> 0 goto e_error
		---- updating original tempfile
		set @sql = 'update ' + @temptable + ' set billorder=' + str(@company) + ',section=''' + 'A' + ''''
		exec (@sql)
		if @@error<>0 
		begin
		print 'error'
		goto e_error
		end
		end

	set @sql = 'declare t_cur cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst  from ' + @temptable
	exec (@sql)
	open t_cur
	fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	if @@error <> 0 goto e_error
	while @@fetch_status=0
		begin
		set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst)  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
		set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
		set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
		set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' + str(@st) + ',' + str(@cess) + ',' + str(@cess2) 
		set @sql = @sql + ',' +  convert(varchar(20),@extrabed) + ',' +  convert(varchar(20),@exbedgst) + ',' +  convert(varchar(20),@food) + ',' +  convert(varchar(20),@foodgst) + ')'

		exec(@sql)
		if @@error <> 0
			begin
			close t_cur
			deallocate t_cur
			goto e_error
			end

		fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
		end
	close t_cur
	deallocate t_cur
	set @sql ='truncate table ' + @temptable 
	exec(@sql)
	fetch next from cur_co into @company
	end
close cur_co
deallocate cur_co
----------------------------------------------------------other company bills end here
----------------------complementary bills starts here (first vouching items next no voucher items
---- here starts the complementary bills processing

set @ltamt=0
set @yesitis = 0
declare cur_co cursor for select company from company order by company
open cur_co
fetch next from cur_co into @company
while @@FETCH_STATUS=0
begin
	set @sql ='truncate table ' + @temptable 
	exec(@sql)
	set @yesitis=0
declare cur_restco cursor for select company,channel,billposition1 from restco WHERE COMPANY=@COMPANY order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
if @company=1
begin
	declare cur_kitroom cursor for 
	select txndt,amt from (
	select txndt,amt=finalamt,
	kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
	from kithist1 
	join co_rest_ol1 kit on kithist1.outlet=kit.ocode
	where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and split='C'
end
else
begin
	declare cur_kitroom cursor for 
	select txndt,amt from (
	select txndt,amt=finalamt,
	kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
	from kithist1 
	join co_rest_ol1 kit on kithist1.outlet=kit.ocode
	where gno = @gno and pmode=2)  a where company=@company and channel=@channel and selfdeal='N' and split ='C'
end
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
	begin
	set @yesitis = 1
	--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
	exec(@sql)
	if @bp1 <> 0
		begin
		set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))
		exec(@sql)
		if @@error<>0
			begin
			close cur_kitroom
			deallocate cur_kitroom
			close cur_restco
			deallocate cur_restco
			goto e_error
			end
		end
	fetch next from cur_kitroom into @txndt,@amt
	end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco

declare cur_restco cursor for select company,outlet,billposition1 from phoutlet where COMPANY=@COMPANY AND billposition1 <> 0 and selfdeal = case when company=1 then 'Y' else 'N' end order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
	begin
	declare cur_kitroom cursor for 
	select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)+scamt+stamt-damt
	from phone where gno=@gno  and pmode=2 and outlet=@channel and split='C'
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
		begin
		set @yesitis = 1
		--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
		set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
		exec(@sql)
		if @bp1 <> 0
			begin
			set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
			+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			exec(@sql)
			if @@error<>0
				begin
				close cur_kitroom
				deallocate cur_kitroom
				close cur_restco
				deallocate cur_restco
				goto e_error
				end
			end
		fetch next from cur_kitroom into @txndt,@amt
		end
	close cur_kitroom

	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
	end

close cur_restco
deallocate cur_restco

	/* here starts travel desk complimentary  24 feb 2018 */
	declare cur_restco cursor for select company,outlet,billposition1 from toutlet where company=@company and billposition1<>0 order by billposition1
	open cur_restco
	fetch next from cur_restco into @company,@channel,@bp1
	while @@fetch_status=0
		begin
		if @company=1 
		declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and selfdeal= 'Y'  and split IN ('C','F') and company=@company  group by finaldate
		else
		declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and selfdeal= 'N'  and split IN ('C','F') and company=@company  group by finaldate

		open cur_kitroom
		fetch next from cur_kitroom into @txndt,@amt
		while @@fetch_status=0
			begin
			set @yesitis = 1
			--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
			set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
			exec(@sql)
			if @bp1<>0
				begin
				set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 300 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  
				+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
				exec(@sql)
				if @@error<>0
					begin
					close cur_kitroom
					deallocate cur_kitroom
					close cur_restco
					deallocate cur_restco
					goto e_error
					end
				end
			fetch next from cur_kitroom into @txndt,@amt
			end
		close cur_kitroom
		deallocate cur_kitroom
		fetch next from cur_restco into @company,@channel,@bp1
		end
	close cur_restco
	deallocate cur_restco


/*here ends travel desk*/



--print 'periya2'


set @sql = 'declare cur_temp cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),st,cess,cess2,extrabed,exbedgst,food,foodgst   from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
set @ctotal = 0
while @@fetch_status=0
	begin
	set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st+cess+cess2 where current of cur_temp'
	set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0)+isnull(cess2,0) where current of cur_temp'
	set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0)+isnull(cess2,0)+isnull(extrabed,0)+isnull(exbedgst,0)+isnull(food,0)+isnull(foodgst,0) where current of cur_temp'
	exec (@sql)
	set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)+isnull(@cess2,0)
	fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	end
close cur_temp
deallocate cur_temp
if @yesitis=1 
	begin
		set @sql = 'insert into ' +  @temptable  + '  ( total,txndt)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31 jan 9999'' )'
		exec (@sql)
		set @sql = 'update ' + @temptable + ' set billorder=1,section=''C'''
		exec (@sql)
	end

--	declare @cnt int
--	declare @tt varchar(500)
--exec sp_executesql N'select @cnt=count(*)  from @tt' ,N'@tt , @cnt int output',@tt=@temptable,@cnt=@cnt output   
--print @cnt goto e_error

set @sql = 'declare t_cur cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),billorder,section,isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0),isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0) ,isnull(st,0),isnull(cess,0),isnull(cess2,0),extrabed,exbedgst,food,foodgst from ' + @temptable 
exec (@sql)
open t_cur
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
while @@fetch_status=0
begin
	set @st=0
	set @cess=0
	set @cess2=0
	set @sql = 'if not exists(select * from ' + @temptabletemp + ' where datediff(d,txndt,''' + convert(varchar(20),@txndt) + ''')=0 and billorder = 1 and section=''C'')   insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst)  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
	set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
	set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
	set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' + str(@st) + ',' + str(@cess) + ',' + str(@cess2) 
	set @sql = @sql + ','+ str(isnull(@extrabed,0)) +','+ str(isnull(@exbedgst,0)) +','+ str(isnull(@food,0)) +','+ str(isnull(@foodgst,0)) + ')'
	--print @sql goto e_error
	exec(@sql)
	if @@error <> 0
		begin
		close t_cur
		deallocate t_cur
		goto e_error
		end
	fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
end
close t_cur
deallocate t_cur
fetch next from cur_co into @company
end
close cur_co
deallocate cur_co

---- here ends complementary bills processing for company 1  - to be vouched item
--set @sql = 'drop table tempramtemp select * into tempramtemp from ' + @temptabletemp
--exec(@sql) 

DECLARE @COMPANY1 NUMERIC
set @sql ='truncate table ' + @temptable ------------------------------------------------------- other company  complementary bills starts here
exec(@sql)
set @yesitis=0
declare cur_co cursor for select company from company where company > 1 order by company
Open cur_co
fetch next from cur_co into @company1
while @@fetch_status=0
	begin
	set @yesitis = 0
	declare cur_restco cursor for select channel,billposition2 from restco where company=@company1  and billposition2<>0  order by billposition2
	open cur_restco
	fetch next from cur_restco into @channel,@bp1 
	while @@fetch_status=0
		begin
		declare cur_kitroom cursor for 
		select txndt,amt from (
		select txndt,amt=finalamt,
		kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
		from kithist1 
		join co_rest_ol1 kit on kithist1.outlet=kit.ocode
		where gno = @gno and pmode=2)  a where company=@company1 and channel=@channel and selfdeal='Y' and split='C'
		open cur_kitroom
		fetch next from cur_kitroom into @txndt,@amt
		while @@fetch_status=0
			begin
			set @yesitis = 1
			set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
			exec(@sql)
			if @bp1<>0 
				begin
				set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  + 'where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))
				exec(@sql)
				if @@error<>0
					begin 
					close cur_kitroom 
					deallocate cur_kitroom
					close cur_restco 
					deallocate cur_restco
					goto e_error
					end
				end
			fetch next from cur_kitroom into @txndt,@amt
			end
		close cur_kitroom 
		deallocate cur_kitroom
		fetch next from cur_restco into @channel,@bp1
		end
	close cur_restco
	deallocate cur_restco
	
--print 'peiryaan' goto e_error

	declare cur_restco cursor for select outlet,billposition2 from phoutlet where company=@company1 and billposition2<>0 order by billposition2
	open cur_restco
	fetch next from cur_restco into @channel,@bp1
	while @@fetch_status=0
		begin
		declare cur_kitroom cursor for 
		select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)+scamt+stamt-damt
		from phone where gno=@gno  and pmode=2 and outlet=@channel and split='C' and selfdeal='Y'
		open cur_kitroom
		fetch next from cur_kitroom into @txndt,@amt
		while @@fetch_status=0
			begin
			set @yesitis = 1
			--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
			set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
			exec(@sql)
			if @bp1<>0
				begin
				set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  + 'where datediff(d,''1/1/80'',txndt) = ' + str(datediff(d,'1/1/80',@txndt))
				exec(@sql)
				if @@error<>0
					begin
					close cur_kitroom
					deallocate cur_kitroom
					close cur_restco
					deallocate cur_restco
					goto e_error
					end
				end
			fetch next from cur_kitroom into @txndt,@amt
			end
		close cur_kitroom
		deallocate cur_kitroom
		fetch next from cur_restco into @channel,@bp1
		end
	close cur_restco
	deallocate cur_restco

	set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total ,st,cess,cess2,extrabed,exbedgst,food,foodgst from ' + @temptable
	exec (@sql)
	open cur_temp
	fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	set @ctotal = 0
	while @@fetch_status=0
		begin
		set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0) where current of cur_temp'
		exec (@sql)
		set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)+isnull(@cess2,0) + isnull(@extrabed,0) + isnull(@exbedgst,0) + isnull(@food,0) + isnull(@foodgst,0)  
		fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
		end
	close cur_temp
	deallocate cur_temp


	if @yesitis=1
		begin
		set @sql = 'insert into ' +  @temptable  + ' ( total,txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,extrabed,exbedgst,food,foodgst)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31jan 9999'',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'' '' ,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0)'
		exec (@sql)
		if @@error <> 0 goto e_error
		---- updating original tempfile
		set @sql = 'update ' + @temptable + ' set billorder=' + str(@company1) + ',section=''' + 'C' + ''''
		exec (@sql)
		end
	set @sql = 'declare t_cur cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),billorder,section,isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0),isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0),isnull(st,0),isnull(cess,0),isnull(cess2,0),extrabed,exbedgst,food,foodgst  from ' + @temptable 
	exec (@sql)
	open t_cur
	fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	while @@fetch_status=0
		begin
		set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst)  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
		set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
		set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
		set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' + str(@st)  + ',' + str(@cess)+ ',' + str(@cess2) 
		set @sql = @sql + ','+ str(isnull(@extrabed,0)) +','+ str(isnull(@exbedgst,0)) +','+ str(isnull(@food,0)) +','+ str(isnull(@foodgst,0)) + ')'
		exec(@sql)
		if @@error <> 0
			begin
			close t_cur
			deallocate t_cur
			goto e_error
			end
		fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
		end
	close t_cur
	deallocate t_cur
	set @sql ='truncate table ' + @temptable 
	exec(@sql)
	fetch next from cur_co into @company1
end

close cur_co
deallocate cur_co
----------------------------------------------------------other company complementary  bills end here (with vouching)
--print 'periya2' goto e_error

---- 'F' STARTS  for other companies non selfdeal
------
-------
------------
-----------
set @ltamt=0
declare cur_co cursor for select company from company where company > 1
open cur_co
fetch next  from cur_co into @company
while @@fetch_status=0
begin
set @yesitis = 0
set @sql ='truncate table ' + @temptable 
exec(@sql)

declare cur_restco cursor for select channel,billposition1 from restco WHERE COMPANY=@company and billposition1<> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @channel,@bp1
while @@fetch_status=0
	begin
	declare cur_kitroom cursor for 
	select txndt,amt from (
	select txndt,amt=finalamt,
	kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
	from kithist1 
	join co_rest_ol1 kit on kithist1.outlet=kit.ocode
	where gno = @gno and pmode=2 and split='F')  a where company=@company and channel=@channel and selfdeal='N' and split='F'
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
		begin
		set @yesitis = 1
		---set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
		set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
		exec(@sql)
		if @bp1 <> 0
			begin
			set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
			+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			exec(@sql)
			if @@error<>0
				begin
				close cur_kitroom
				deallocate cur_kitroom
				close cur_restco
				deallocate cur_restco
				goto e_error
				end
			end
		fetch next from cur_kitroom into @txndt,@amt
		end
	close cur_kitroom
	deallocate cur_kitroom
	fetch next from cur_restco into @channel,@bp1
	end
close cur_restco
deallocate cur_restco

--print 'periya3' goto e_error

declare cur_restco cursor for select outlet,billposition1 from phoutlet where COMPANY=@company AND billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @channel,@bp1
while @@fetch_status=0
begin
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)+scamt+stamt-damt
from phone where gno=@gno  and pmode=2 and outlet=@channel and selfdeal='N' and split='F'
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
	begin
	set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt, rent,discount,item1,item2,item3, item4,item5,item6,item7,item8, item9,item10,lt,sc,total, outlet1,outlet2,outlet3,outlet4,outlet5, outlet6,outlet7,outlet8,outlet9,outlet10, st,cess,cess2,extrabed,exbedgst, food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
	exec(@sql)
	if @bp1 <> 0
		begin
		set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
		+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
		exec(@sql)
		if @@error<>0
			begin
			close cur_kitroom
			deallocate cur_kitroom
			close cur_restco
			deallocate cur_restco
			goto e_error
			end
		end
	fetch next from cur_kitroom into @txndt,@amt
	end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @channel,@bp1
end
close cur_restco
deallocate cur_restco
--print 'peria ' + @temptable goto e_error

set @sql = 'declare cur_temp cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),st,cess,cess2,extrabed,exbedgst,food,foodgst  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
set @ctotal = 0
	while @@fetch_status=0
	begin
	set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0)+isnull(cess2,0)+extrabed+exbedgst+food+foodgst where current of cur_temp'
	exec (@sql)
	set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)+isnull(@cess2,0)
	--fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2
	fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	end
close cur_temp
deallocate cur_temp

if @yesitis=1
	begin
	--set @sql = 'insert into ' +  @temptable  + ' ( total,txndt)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31 jan 9999'' )'
	set @sql = 'insert into ' + @temptable  + '(txndt, rent,discount,item1,item2,item3, item4,item5,item6,item7,item8, item9,item10,lt,sc,total, outlet1,outlet2,outlet3,outlet4,outlet5, outlet6,outlet7,outlet8,outlet9,outlet10, st,cess,cess2,extrabed,exbedgst, food,foodgst) values (''31 Jan 9999'',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,' + convert(varchar(14),@ctotal ) +' ,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
	exec (@sql)
	set @sql = 'update ' + @temptable + ' set billorder=1,section=''F'''
	exec (@sql)
	end

-- Note : 27 Feb 2018
-- Very Important - Here the company should be 1 as other compnies are billing on Company 1 only
set @sql = 'declare t_cur cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),billorder,section,isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0),isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0) ,st,cess,cess2,extrabed,exbedgst,food,foodgst  from ' + @temptable 
exec (@sql)
open t_cur
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
while @@fetch_status=0
	begin
		set @st=0
		set @cess=0
		set @cess2=0
	set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst )  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
	set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
	set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
	set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) +  ',' + str(@st) + ',' + str(@cess) + ',' + str(@cess2) 
	set @sql = @sql + ',' +  convert(varchar(20),@extrabed) + ',' +  convert(varchar(20),@exbedgst) + ',' +  convert(varchar(20),@food) + ',' +  convert(varchar(20),@foodgst) + ')'

	exec(@sql)
	if @@error <> 0
		begin
		close t_cur
		deallocate t_cur
		goto e_error
		end
	--fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2
	fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	end
close t_cur
deallocate t_cur
fetch next  from cur_co into @company
end
close cur_co
deallocate cur_co


--print 'periyaan 1111' goto e_error
---- here ends complementary bills processing for company 1  - no  voucher item
------
------
--------
---- 'F' STARTS  for other companies selfdeal

set @sql ='truncate table ' + @temptable ------------------------------------------------------- other company  complementary bills starts here
exec(@sql)
declare cur_co cursor for select company from company where company > 1 order by company
open cur_co
fetch next from cur_co into @company
while @@fetch_status=0
	begin
	set @yesitis = 0
	declare cur_restco cursor for select company,channel,billposition2 from restco where company = @company  and billposition2<>0 order by billposition2
	open cur_restco
	fetch next from cur_restco into @company,@channel,@bp1 
	while @@fetch_status=0
		begin
		--   declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel  and selfdeal='Y' and split='F'  group by txndt
		declare cur_kitroom cursor for 
		select txndt,amt from (
		select txndt,amt=finalamt,
		kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
		from kithist1 
		join co_rest_ol1 kit on kithist1.outlet=kit.ocode
		where gno = @gno and pmode=2)  a where company=@company and channel=@channel and selfdeal='Y' and split='F'
		open cur_kitroom
		fetch next from cur_kitroom into @txndt,@amt
		while @@fetch_status=0
			begin
			set @yesitis = 1
			--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
			set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
			exec(@sql)
			if @bp1<>0 
				begin
				set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  
				+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
				exec(@sql)
				if @@error<>0
					begin 
					close cur_kitroom 
					deallocate cur_kitroom
					close cur_restco 
					deallocate cur_restco
					goto e_error
					end
				end
			fetch next from cur_kitroom into @txndt,@amt
			end
		close cur_kitroom 
		deallocate cur_kitroom
		fetch next from cur_restco into @company,@channel,@bp1
		end
	close cur_restco
	deallocate cur_restco
	declare cur_restco cursor for select company,outlet,billposition2 from phoutlet where company=@company and billposition2<>0 order by billposition2
	open cur_restco
	fetch next from cur_restco into @company,@channel,@bp1
	while @@fetch_status=0
		begin
		--declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and selfdeal= 'Y'  and split='F'  group by txndt
		declare cur_kitroom cursor for 
		select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)+scamt+stamt-damt
		from phone where gno=@gno and pmode=2  and outlet=@channel and split='F' and selfdeal='Y'
		open cur_kitroom
		fetch next from cur_kitroom into @txndt,@amt
		while @@fetch_status=0
			begin
			set @yesitis = 1
			--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0)'
			set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0, 0,0,0,0)'
			exec(@sql)
			if @bp1<>0
				begin
				set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  
				+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
				exec(@sql)
				if @@error<>0
					begin
					close cur_kitroom
					deallocate cur_kitroom
					close cur_restco
					deallocate cur_restco
					goto e_error
					end
				end
			fetch next from cur_kitroom into @txndt,@amt
			end
		close cur_kitroom
		deallocate cur_kitroom
		fetch next from cur_restco into @company,@channel,@bp1
		end
	close cur_restco
	deallocate cur_restco
---print 'periyaan555' goto e_error
	set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,st,cess,cess2,extrabed,exbedgst,food,foodgst  from ' + @temptable
	--print @sql
	exec (@sql)
	open cur_temp
	fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	set @ctotal = 0
	while @@fetch_status=0
		begin
		set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0)+isnull(cess2,0) + isnull(extrabed,0) + isnull(exbedgst,0) + isnull(food,0) + isnull(foodgst,0)   where current of cur_temp'
		exec (@sql)
		set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)+isnull(@cess2,0) + isnull(@extrabed,0) + isnull(@exbedgst,0) + isnull(@food,0) + isnull(@foodgst,0)  
		fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
		end
	close cur_temp
	deallocate cur_temp
--print 'periya2222' goto e_error
	if @yesitis=1
		begin
		set @sql = 'insert into ' +  @temptable  + ' ( total,txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31 jan 9999'',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, '' '' , 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0,0,0)'
		exec (@sql)
		if @@error <> 0 goto e_error
		---- updating original tempfile
		set @sql = 'update ' + @temptable + ' set billorder=' + str(@company) + ',section=''' + 'F' + ''''
		exec (@sql)
		end
--set @sql = 'drop table tempram select * into tempram from ' + @temptable
--exec(@sql) 
--print 'periya 433' goto e_error
	set @sql = 'declare t_cur cursor for select txndt,
	isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),
	billorder,section,
	isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0), isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0),st,cess,cess2,extrabed,exbedgst,food,foodgst  from ' + @temptable
	exec (@sql)
	open t_cur
	fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
	if @@error <> 0 goto e_error
	while @@fetch_status=0
		begin
--		print 'periya karuppa 111'  goto e_error
		set @st=0
		set @cess=0
		set @cess2=0
		set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst )  values( ''' + convert(varchar(16),@txndt,106)+''',' + convert(varchar(14),@rent)
		set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
		set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
		set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' + str(@st) + ',' + str(@cess)+ ',' + str(@cess2) 
		set @sql = @sql + ',' +  convert(varchar(20),@extrabed) + ',' +  convert(varchar(20),@exbedgst) + ',' +  convert(varchar(20),@food) + ',' +  convert(varchar(20),@foodgst) + ')'
		exec(@sql)
--		print 'Periya 232' goto e_error
		if @@error <> 0
			begin
			close t_cur
			deallocate t_cur
			goto e_error
			end
		fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
		end
	close t_cur
	deallocate t_cur
	set @sql ='truncate table ' + @temptable 
	exec(@sql)
	fetch next from cur_co into @company
	end
close cur_co
deallocate cur_co
----------------------------------------------------------other company complementary  bills end here   no voucher items
--set @sql = 'drop table tempramtemp select * into tempramtemp from ' + @temptabletemp
--exec(@sql) 
--print 'periya 444' goto e_error
top1:
set @sql = 'drop table ' + @temptable
exec(@sql)
--- new try nov 24 2005
set @sql = 'declare sp_cbcursor cursor  for select company,split from roomdues where gno=' + str(@gno) 
exec (@sql)
if @@error <> 0 goto e_error
open sp_cbcursor
if @@error <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
if @@error <> 0 goto e_error
while @@fetch_status=0 
	begin
	set @sql = 'if not exists(select * from ' + @temptabletemp + ' where billorder = ' + str(@billorder) + ' and section = ''' + case when @section = 'Y' then 'B' else 'A'  end  +''')'
	set @sql = @sql + '  insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst)  values( ''' + convert(varchar(16),'31 jan 9999',106)+''',0' 
	set @sql = @sql + ' ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,' + str(@billorder) + ',''' + case when @section='Y' then 'B' else 'A' end  + ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'

	exec(@sql)
	if @@error <> 0 goto e_error
	fetch next from sp_cbcursor into @billorder,@section
	if @@error <> 0 goto e_error
	end

close sp_cbcursor
if @@error <> 0 goto e_error
deallocate sp_cbcursor
if @@error <> 0 goto e_error
---- for advance unaccounted 25 nov 2005

set @sql = 'declare sp_cbcursor cursor  for select company,split from advance where gno=' + str(@gno) 
exec (@sql)
if @@error <> 0 goto e_error
open sp_cbcursor
if @@error <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
if @@error <> 0 goto e_error
while @@fetch_status=0 
	begin
	set @sql = 'if not exists(select * from ' + @temptabletemp + ' where billorder = ' + str(@billorder) + ' and section = ''' + case when @section = 'Y' then 'B' else 'A'  end  +''')'
	set @sql = @sql + '  insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess,cess2,extrabed,exbedgst,food,foodgst)  values( ''' + convert(varchar(16),'31 jan 9999',106)+''',0' 
	set @sql = @sql + ' ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,' + str(@billorder) + ',''' + case when @section='Y' then 'B' else 'A' end  + ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
	exec(@sql)
	if @@error <> 0 goto e_error
	fetch next from sp_cbcursor into @billorder,@section
	if @@error <> 0 goto e_error
	end

close sp_cbcursor
if @@error <> 0 goto e_error
deallocate sp_cbcursor
if @@error <> 0 goto e_error
-- this is included for classic  (to calculate the service charges on all the services along wiht the rent 23-nov-2007 11:46 am

if @lttype=2  -- new insertion on 25-11-2011 for Almondz
	begin
	set @ctotal=0
	declare @otherchgs money
	set @sql = 'declare cnr cursor for select  txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,st,cess,cess2 ,extrabed,exbedgst,food,foodgst  from ' + @temptabletemp --+ ' where rent <>0'
	exec(@sql)
	open cnr
	fetch next from cnr  into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@st,@cess ,@cess2,@extrabed,@exbedgst,@food,@foodgst
	while @@fetch_status=0
		begin
		if @rent<>0
			begin
				set @sql = 'update ' + @temptabletemp + ' set otherchgs=isnull(' + convert(varchar(20),@nettrent) + '- (rent+lt+st+cess+cess2),0)   where txndt=''' + convert(varchar(20),@txndt) + ''' and billorder=' +  str(@billorder) + ' and section=''' + @section + ''''
				exec(@sql)
			end
			set @sql = 'update ' + @temptabletemp + ' set total =total+isnull(otherchgs,0)  where txndt=''' + convert(varchar(20),@txndt) + ''' and billorder=' +  str(@billorder) + ' and section=''' + @section + ''''
			exec(@sql)
			set @sql = 'update ' + @temptabletemp + ' set otherchgs=isnull(otherchgs,0)  where txndt=''' + convert(varchar(20),@txndt) + ''' and billorder=' +  str(@billorder) + ' and section=''' + @section + ''''
			exec(@sql)
			set @sql='declare cnr1 cursor for select  otherchgs from ' + @temptabletemp +  '  where txndt=''' + convert(varchar(20),@txndt) + ''' and   billorder=' +  str(@billorder) + ' and section=''' + @section + ''''
			exec(@sql)
			open cnr1
			fetch next from cnr1 into @otherchgs	
			close cnr1
			deallocate cnr1
			--set @ctotal = @ctotal + isnull(@rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@lt+@sc-@discount+@st+@cess+@cess2+@otherchgs,0)+isnull(@extrabed,0)+isnull(@exbedgst,0)+isnull(@food,0)+isnull(@foodgst,0) 
			set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)+isnull(@sc,0)-isnull(@discount,0)+isnull(@st,0) + isnull(@cess,0) + isnull(@cess2,0) + isnull(@extrabed,0) + isnull(@exbedgst,0) + isnull(@food,0) + isnull(@foodgst,0)
			fetch next from cnr into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@st,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
		end
	close cnr
	deallocate cnr
	set @sql = 'update ' +  @temptabletemp  + ' set  total=' + convert(varchar(20),@ctotal) + '  where  datediff(d,''31 jan 9999'',txndt) = 0 and  billorder=' +  str(@billorder) + ' and section=''' + @section + ''''
	exec(@sql)
	end  -- end of new insertion

declare @scforall tinyint
declare @scrate money
declare @scratestr varchar(50)
set @scrate=0
declare @g_sc_afterdisc int
declare @g_sc_ltrules int
select @scforall=scforall,@g_sc_afterdisc=g_sc_afterdisc,@G_sc_Ltrules=g_sc_ltrules from setting
select @scrate=sc from guest where gno=@gno
set @scratestr = convert(varchar(20),@scrate)

if @scforall=1   -- these codes will work only when the scforall is set to 1 in the setting table   (date of insertion 07 december 2007) for hotel classic
	if @scrate<>0
		begin
			if @g_sc_ltrules=0
				if @g_sc_afterdisc=0
					set @sql = 'update ' + @temptabletemp + ' set sc=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10) * ' +  @scratestr   + '/100 '
				else
					set @sql = 'update ' + @temptabletemp + ' set sc=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-discount) * ' +  @scratestr  + '/100 '
			else
				begin
					set @amttemp=(select case when @head=1 then rent1 else rent2 end  from room where rno=@crno)
					if @g_sc_afterdisc=0
						set @sql = 'update ' + @temptabletemp + ' set sc=(' + str(@amttemp) + '+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10) * ' + @scratestr   + '/100 '
					else
						set @sql = 'update ' + @temptabletemp + ' set sc=(' + str(@amttemp) + '+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-discount) * ' +  @scratestr  + '/100 '
				end
			exec(@sql)
			set @sql = 'update ' + @temptabletemp + ' set total=rent+isnull(otherchgs,0)+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+isnull(sc,0)-discount+st+cess+cess2+isnull(extrabed,0)+isnull(exbedgst,0)+isnull(food,0)+isnull(foodgst,0) where sc <>  0 '
			exec(@sql)

			set @sql = 'update ' + @temptabletemp + '   set total=(select sum(total) from ' + @temptabletemp + ' b  where datediff(d,b.txndt,''31 jan  9999'') <> 0 and b.billorder=' + @temptabletemp + '.billorder and b.section=' + @temptabletemp + '.section ) where datediff(d,txndt,''31 jan 9999'') = 0'
			exec (@sql)
		end

set @sql = 'update ' + @temptabletemp + '   set total=(select sum(total) from ' + @temptabletemp + ' b  where datediff(d,b.txndt,''31 jan  9999'') <> 0 and b.billorder=' + @temptabletemp + '.billorder and b.section=' + @temptabletemp + '.section ) where datediff(d,txndt,''31 jan 9999'') = 0'
exec (@sql)

set @sql = 'update ' + @temptabletemp + ' set st=isnull(st,0),cess=isnull(cess,0),lrate=isnull(lrate,0) '
exec(@sql)
-- end of service charging
-- new try ends
commit transaction
return
e_error:
rollback transaction
raiserror(@errmsg,16,-1)
return


GO
/****** Object:  StoredProcedure [dbo].[sp_cb_fast]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
CREATE PROCEDURE [dbo].[sp_cb_fast] @t1 varchar(50),@gno numeric 
/* with encryption */
AS
declare @sql  varchar(5000) 
declare @amt money
declare @txndt datetime
declare @rent money
declare @discount money
declare @sc money
set @sql = 'declare tc cursor for select txndt,rent,discount,sc from ' + @t1 
exec(@sql)
open tc
fetch next from tc into @txndt,@rent,@discount,@sc
while @@fetch_status=0 begin
        select @AMT=sum(finalamt) from kithist1  where outlet in (select ocode from co_rest_ol1 where lux='Y' and cocode=1)
        and pmode=2 and gno=@gno and split = 'N' and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item1=item1+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
        select @AMT=sum(finalamt) from kithist1  where outlet in (select ocode from co_rest_ol1 where lux='Y' and cocode<>1 and selfdeal='N')
        and pmode=2 and gno=@gno and split = 'N' and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item1=item1+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
        select @AMT=sum(amt) from ph_room where outlet in (
        select outlet from phoutlet where lux='Y') and gno=@gno and company=1 and split = 'N' and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item1=item1+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
       select @AMT=sum(amt) from tt_chalan_ol where outlet in (
        select outlet from toutlet where lux='Y' and company=1 ) and gno=@gno and  split = 'N' and datediff(d,'1/1/90',finaldate) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item1=item1+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
       select @AMT=sum(amt) from tt_chalan_ol where outlet in (
        select outlet from toutlet where lux='Y' and company<>1 and selfdeal='N' ) and gno=@gno and  split=  'N' and datediff(d,'1/1/90',finaldate) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item1=item1+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
        fetch next from tc into @txndt,@rent,@discount,@sc
end
close tc
deallocate tc
--raiserror('i m here',16,-1)
-- LT calculation starts
declare @totrooms numeric
declare @item1 money
declare @item2 money
declare @item3 money
declare @item4 money
declare @item5 money
declare @item6 money
declare @item7 money
declare @item8 money
declare @item9 money
declare @item10 money
declare @amt1 money
declare @amt2 money
declare @lt money
declare @ctotal money
declare @total money 
declare @ltamt money
declare @drate money
declare @tno int
declare @lrate money
declare @eamt money
declare @samt money
declare @famt money
set @sql = 'update ' + @t1 + ' set taxable=0,lt=0'
exec(@sql)
      
SET @TOTROOMS = 1 
SELECT @TOTROOMS=COUNT(*) FROM ADDROOM WHERE GNO=@GNO
IF @TOTROOMS < 1 SET @TOTROOMS=1
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL  FROM ' + @T1
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
set @sql = 'update ' + @t1  + ' set taxable=' +convert (varchar(14),@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10) + ' where current of cur_temp'
exec(@sql)
SET @AMT = @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10-@DISCOUNT
SET @AMT = @AMT/@TOTROOMS
SET @LTAMT = 0
declare @ltamttemp money
declare @amttemp money
declare @amttemp1 money
set @ltamttemp=0
set @amttemp=@rent
set @amttemp1=@rent
SELECT @TNO=TNO FROM LTMASTER WHERE CONVERT(DATETIME,@TXNDT,106)  BETWEEN SDT AND EDT
SET @SQL = 'DECLARE CUR_LTTABLE CURSOR FOR SELECT LRATE,SAMT,EAMT,FAMT FROM LTTABLE WHERE TNO = ' +  LTRIM(STR(@TNO))
EXEC(@SQL)
OPEN CUR_LTTABLE
FETCH NEXT FROM CUR_LTTABLE INTO @LRATE,@SAMT,@EAMT,@FAMT
WHILE @@FETCH_STATUS=0 AND @AMT > 0
BEGIN
      If @EAMT > @AMT  SET @AMT2 = @AMT   Else   SET @AMT2 = @EAMT
      SET @LTAMT = round(@LTAMT + ((@AMT2 + @FAMT) * @LRATE / 100),2)
      SET @AMT = @AMT - @AMT2
      if @eamt > @amttemp set @amt2=@amttemp else set @amt2 = @eamt
      set @ltamttemp = round(@ltamttemp+((@amt2+@famt)*@lrate/100),2)
      set @amttemp=@amttemp-@amt2
      FETCH NEXT FROM CUR_LTTABLE INTO @LRATE,@SAMT,@EAMT,@FAMT
END
CLOSE CUR_LTTABLE
DEALLOCATE CUR_LTTABLE
set @amt2 = (@ltamttemp + @amttemp1)*100
set @amttemp = floor(        ((@ltamttemp+@amttemp1)*100) /10  )*10
if @amt2 > @amttemp set @ltamt = @ltamt  -( ( @amt2-@amttemp)/100)
if @ltamt < 0 set @ltamt=0
SET @SQL = 'UPDATE ' + @T1 + ' SET LT =' + CONVERT(VARCHAR(14), @LTAMT) + '  WHERE CURRENT OF CUR_TEMP'
EXEC(@SQL)
SET @SQL = 'UPDATE ' + @T1 + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LTamt-@DISCOUNT
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
--- LT calculation ends
set @sql = 'declare tc cursor for select txndt,rent,discount,sc from ' + @t1 
exec(@sql)
open tc
fetch next from tc into @txndt,@rent,@discount,@sc
while @@fetch_status=0 begin
       -- select @AMT=sum(amt) from kit_room where gno=@gno and ((company=1 and split not in ('N','C','F'))  or (company <> 1 and split not in ('C','F')) )   and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)
        select @AMT=sum(finalamt) from kithist1  where outlet in (select ocode from co_rest_ol1 where (lux='N' and (cocode=1 or cocode<>1 and selfdeal='N') ) )
        and pmode=2 and gno=@gno and split  not in ('N', 'C','F') and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item2=item2+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
        select @AMT=sum(finalamt) from kithist1  where outlet in (select ocode from co_rest_ol1 where Cocode<>1)
        and pmode=2 and gno=@gno and split  not in ( 'C','F') and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) and selfdeal = 'Y'
        set @sql = 'update ' + @t1 + ' set item2=item2+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
        select @AMT=sum(amt) from ph_room where gno=@gno and ((company=1 and split not in ('N','C','F'))  or (company <> 1 and split not in ('C','F')) )  and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item2=item2+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
        fetch next from tc into @txndt,@rent,@discount,@sc
       select @AMT=sum(amt) from tt_chalan_ol where outlet in (
        select outlet from toutlet where lux='Y' and company=1 ) and gno=@gno and  split = 'Y' and datediff(d,'1/1/90',finaldate) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item1=item1+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
       select @AMT=sum(amt) from tt_chalan_ol where outlet in (
        select outlet from toutlet where lux='N' and company=1  ) and gno=@gno and  split not in ('C','F') and datediff(d,'1/1/90',finaldate) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item1=item1+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
       select @AMT=sum(amt) from tt_chalan_ol where outlet in (
        select outlet from toutlet where company<>1 and selfdeal='Y' ) and gno=@gno and  split not in ('C','F') and datediff(d,'1/1/90',finaldate) = datediff(d,'1/1/90',@txndt)
        set @sql = 'update ' + @t1 + ' set item1=item1+' + convert(varchar(20),@amt) + ' where current of tc'
        exec(@sql)
end
close tc
deallocate tc
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL  FROM ' + @T1
--PRINT @SQL
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'UPDATE ' + @T1 + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LT+@SC-@DISCOUNT
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
return




GO
/****** Object:  StoredProcedure [dbo].[sp_cb_test]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sp_cb    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[sp_cb_test]  @TEMPTABLE VARCHAR(50), @GNO INT,@mark int=0
/* with encryption */
AS
DECLARE @SQL VARCHAR(5000)
DECLARE @COMPANY INT
DECLARE @CHANNEL INT
DECLARE @BP1 INT
DECLARE @TXNDT DATETIME
DECLARE @AMT MONEY
DECLARE @RENT MONEY
DECLARE @DISCOUNT MONEY
DECLARE @ITEM1 MONEY
DECLARE @ITEM2 MONEY
DECLARE @ITEM3 MONEY
DECLARE @ITEM4 MONEY
DECLARE @ITEM5 MONEY
DECLARE @ITEM6 MONEY
DECLARE @ITEM7 MONEY
DECLARE @ITEM8 MONEY
DECLARE @ITEM9 MONEY
DECLARE @ITEM10 MONEY
DECLARE @LT MONEY
DECLARE @SC MONEY
DECLARE @TOTAL MONEY
DECLARE @SDT DATETIME
DECLARE @EDT DATETIME
DECLARE @LRATE MONEY
DECLARE @SAMT MONEY
DECLARE @EAMT MONEY
DECLARE @FAMT MONEY
DECLARE @LTAMT MONEY
DECLARE @AMT2 MONEY
DECLARE @TNO INT
DECLARE @GTOTAL MONEY
DECLARE @CTOTAL MONEY
DECLARE @BILLORDER INT
DECLARE @SECTION CHAR(1)
DECLARE @TEMPTABLETEMP VARCHAR(50)
DECLARE @HOSTID char(18)
DECLARE @OUTLET1 INT
DECLARE @OUTLET2 INT
DECLARE @OUTLET3 INT
DECLARE @OUTLET4 INT
DECLARE @OUTLET5 INT
DECLARE @OUTLET6 INT
DECLARE @OUTLET7 INT
DECLARE @OUTLET8 INT
DECLARE @OUTLET9 INT
DECLARE @OUTLET10 INT
DECLARE @YESITIS INT
DECLARE @TOTROOMS INT
declare @temp varchar(20)
declare @ltcal char(1)
declare @ltontariff char(1)
declare @head int
declare @temptot money
declare @temptot1 money
declare @temptot2 money
set nocount on
BEGIN TRANSACTION
set @hostid = @temptable
set @temp=@temptable
if @mark=0
IF NOT  EXISTS (SELECT * FROM ROOM WHERE GNO=@GNO AND HOSTID = HOST_name() AND CHECKOUT = 'Y')
    IF NOT  EXISTS (SELECT * FROM ROOM WHERE GNO = @GNO AND CHECKOUT<> 'Y') 
       begin
       Print 'Either the selected room is already checked out from some other computer or is in the checkout process'
        GOTO E_ERROR
       end
if @mark=0
UPDATE ROOM SET CHECKOUT='Y',  HOSTID = HOST_name() WHERE GNO = @GNO
select @ltcal=lt from guest where gno = @gno
SET @BILLORDER = 1
SET @YESITIS = 1
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,CHANNEL,BILLPOSITION1 FROM RESTCO ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
IF @COMPANY=1
begin
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'N' AND LUX = 'Y' GROUP BY TXNDT
select txndt,amt into #sp_cb1 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company  and channel=@channel and lux='Y'  and split='N'
declare cur_kitroom cursor for select txndt,amt from #sp_cb1
end
ELSE
begin
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'N' AND LUX = 'Y'  AND SELFDEAL='N'  GROUP BY TXNDT
select txndt,amt into #sp_cb2 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company   and channel=@channel and lux='Y' and selfdeal='N' and split='N'
declare cur_kitroom cursor for select txndt,amt from #sp_cb2
end
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1<>0
BEGIN
SET @SQL = 'UPDATE  ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 100 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
if @company=1
drop table #sp_cb1
else
drop table #sp_cb2
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
-- why here the diff in company 1 and other is not specified is due  to the billpostion1 field
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION1 FROM PHOUTLET where billposition1 <> 0 ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--DECLARE CUR_KITROOM CURSOR FOR select TXNDT,'AMT' = SUM(AMT) from PH_ROOM WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SPLIT = 'N' AND LUX = 'Y' GROUP BY TXNDT
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno) into #sp_cb3
from phone where gno=@gno and outlet=@channel and split='N' and lux='Y'
declare cur_kitroom cursor for select txndt,amt from #sp_cb3
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1<>0
BEGIN
SET @SQL = 'UPDATE  ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 200 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)  + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)='+STR(DATEDIFF(d,'1/1/80',@TXNDT))
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
drop table #sp_cb3
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCo
-------- first  try for travel desk 
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION1 FROM TOUTLET where billposition1 <> 0 ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
DECLARE CUR_KITROOM CURSOR FOR select finaldate,'AMT' = SUM(AMT) from tt_chalan_ol  WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SPLIT = 'N' AND LUX = 'Y' GROUP BY finaldate
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1<>0
BEGIN
SET @SQL = 'UPDATE  ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 300 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)  
+ ' where datediff(d,''1/1/90'',txndt)=datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt) + ''')'
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
-----traveldesk ends
--PRINT 'PERIA'
--if @ltcal='Y'
--begin
SET @TOTROOMS = 1 
SELECT @TOTROOMS=COUNT(*) FROM ADDROOM WHERE GNO=@GNO
IF @TOTROOMS < 1 SET @TOTROOMS=1
declare @crno numeric
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,rno,head  FROM ' + @TEMPTABLE
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@crno,@head
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
if @ltcal='Y'   -- calculate LT
begin
set @sql = 'update ' + @temptable  + ' set taxable=' +convert (varchar(14),@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10) + ' where current of cur_temp'
exec(@sql)
SET @LTAMT = 0
declare @ltamttemp money
declare @amttemp money
declare @amttemp1 money
set @ltamttemp=0
set @amttemp=@rent
select @ltontariff=ltontariff from ltontariff where datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)
--set @amttemp1=@rent
if @rent<>0
if @ltontariff='Y'   set @amttemp=(select case when @head=1 then rent1 else rent2 end  from room where rno=@crno)
--print @ltontariff + str(@head) + '  ' + convert(varchar(20),@amttemp)
--goto e_error
set @amttemp1=case when @rent > @amttemp then @rent else @amttemp end
set @amttemp = @amttemp1
if @ltontariff='Y'
	SET @AMT = @amttemp+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10
else
	SET @AMT = @amttemp+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10-@DISCOUNT
if  @ltontariff<>'Y' SET @AMT = @AMT/@TOTROOMS
SELECT @TNO=TNO FROM LTMASTER WHERE CONVERT(DATETIME,@TXNDT,106)  BETWEEN SDT AND EDT
declare @taxed money
declare @nontaxable money
set @taxed=0
set @nontaxable=0
SET @SQL = 'DECLARE CUR_LTTABLE CURSOR FOR SELECT LRATE,SAMT,EAMT,FAMT FROM LTTABLE WHERE TNO = ' +  LTRIM(STR(@TNO))
EXEC(@SQL)
OPEN CUR_LTTABLE
FETCH NEXT FROM CUR_LTTABLE INTO @LRATE,@SAMT,@EAMT,@FAMT
WHILE @@FETCH_STATUS=0 AND @AMT > 0
BEGIN
      If @EAMT > @AMT  SET @AMT2 = @AMT   Else   SET @AMT2 = @EAMT
      SET @LTAMT = round(@LTAMT + ((@AMT2 + @FAMT) * @LRATE / 100),2)
   if @lrate > 0 set @taxed=@taxed + @amt2  else set @nontaxable=@nontaxable+@amt2
      SET @AMT = @AMT - @AMT2
      if @eamt > @amttemp set @amt2=@amttemp else set @amt2 = @eamt
if @ltontariff='Y'
      set @ltamttemp =  round(@ltamttemp+((@amt2+@famt)*@lrate/100),2)
else
      set @ltamttemp =  @ltamttemp+round(((@amt2+@famt)*@lrate/100),2)
      set @amttemp=@amttemp-@amt2
      FETCH NEXT FROM CUR_LTTABLE INTO @LRATE,@SAMT,@EAMT,@FAMT
END
CLOSE CUR_LTTABLE
DEALLOCATE CUR_LTTABLE
/*
set @amt2 = (@ltamttemp + @amttemp1)*100
set @amttemp = floor(        ((@ltamttemp+@amttemp1)*100) /10  )*10
if @amt2 > @amttemp set @ltamt = @ltamt  -( ( @amt2-@amttemp)/100)
*/
set @ltamt = @ltamttemp
if @ltamt < 0 set @ltamt=0
--SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET LT =' + CONVERT(VARCHAR(14), @LTAMT) + ',taxed=' + convert(varchar(20),@taxed) + ',nontaxable=' + convert(varchar(20),@nontaxable) +    '  WHERE CURRENT OF CUR_TEMP'
--SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET LT =' + CONVERT(VARCHAR(14), @LTAMT) + ',taxed=' + convert(varchar(20),@taxed) + ',nontaxable=' + convert(varchar(20),@nontaxable) +    '  WHERE CURRENT OF CUR_TEMP'
set @sql= 'update ' + @temptable + ' set lt = ' + convert(varchar(20),@ltamt)  +'  where current of cur_temp'
EXEC(@SQL)
end --- end of @ltcal='Y'
else
begin ----beginning of @ltcal<>'Y'
SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET LT = 0  WHERE CURRENT OF CUR_TEMP'
EXEC(@SQL)
end ---- end of @ltcal <>'Y'
if @ltamt<>0
begin
set @temptot =  @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LTamt-@DISCOUNT
set @temptot1 = @temptot*100
set @temptot2 = floor(@temptot1/10)*10
if @temptot1-@temptot2 = 1 set @ltamt=@ltamt-.01
set @sql= 'update ' + @temptable + ' set lt = ' + convert(varchar(20),@ltamt)  +'  where current of cur_temp'
EXEC(@SQL)
end
SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LTamt-@DISCOUNT
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@crno,@head
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
--end  -- end of @ltcal='Y'
-------------------------------------------------------------------
-------------------------------------------------------------------
-------------------------------------------------------------------
-- HERE STARTS THE NO LT CALCULATION 
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,CHANNEL,BILLPOSITION1 FROM RESTCO ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--if @company=1
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'N' AND LUX <> 'Y' GROUP BY TXNDT
--else
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'N' AND LUX <> 'Y' AND SELFDEAL = 'N' GROUP BY TXNDT
IF @COMPANY=1
begin
select txndt,amt into #sp_cb5 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company and  channel=@channel and  lux<>'Y'  and split='N'
declare cur_kitroom cursor for select txndt,amt from #sp_cb5
end
ELSE
begin
select txndt,amt into #sp_cb6 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company and  channel=@channel and  lux<>'Y'  and  selfdeal='N' and split='N'
declare cur_kitroom cursor for select txndt,amt from #sp_cb6
end
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1<>0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 100 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
if @company=1
drop table #sp_cb5
else
drop table #sp_cb6
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION1 FROM PHOUTLET WHERE BILLPOSITION1 <> 0 ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
---DECLARE CUR_KITROOM CURSOR FOR select TXNDT,'AMT' = SUM(AMT) from PH_ROOM WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SPLIT = 'N' AND LUX <> 'Y' GROUP BY TXNDT
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno) into #dummy
from phone where gno=@gno and outlet=@channel and split='N' and lux<>'Y'
declare cur_kitroom cursor for select txndt,amt from #dummy
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1 <> 0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 200 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
drop table #dummy
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
-------- first  try for travel desk 
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION1 FROM TOUTLET where billposition1 <> 0 ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
DECLARE CUR_KITROOM CURSOR FOR select finaldate,'AMT' = SUM(AMT) from tt_chalan_ol  WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SPLIT = 'N' AND LUX <> 'Y' GROUP BY finaldate
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1<>0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 300 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
-----traveldesk ends
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL  FROM ' + @TEMPTABLE
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + isnull(@RENT,0)+isnull(@ITEM1,0)+isnull(@ITEM2,0)+isnull(@ITEM3,0)+isnull(@ITEM4,0)+isnull(@ITEM5,0)+isnull(@ITEM6,0)+isnull(@ITEM7,0)+isnull(@ITEM8,0)+isnull(@ITEM9,0)+isnull(@ITEM10,0)+isnull(@LT,0)+isnull(@SC,0)-isnull(@DISCOUNT,0)
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
IF @YESITIS=1
 BEGIN
     SET @SQL = 'INSERT INTO ' +  @TEMPTABLE  + ' ( TOTAL,TXNDT)  VALUES( ' + CONVERT(VARCHAR(14),@CTOTAL ) + ', '' 31 JAN 9999'' )'
     EXEC (@SQL)
     SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET BILLORDER=1,SECTION=''A'''
      EXEC (@SQL)
 END
--HERE ENDS NO LT CALCULATION
--------------------------------------------------------------------
SET @TEMPTABLETEMP = @TEMPTABLE
SET @TEMPTABLE = @TEMPTABLE + 'T'
SET @SQL = 'if exists (select name,xtype from sysobjects where name = ''' + @TEMPTABLE +''' and xtype = ''U'') drop table ' + @TEMPTABLE 
EXEC (@SQL)
SET @SQL = 'SELECT * INTO ' + @TEMPTABLE + '  FROM  DUMMYBILL'
EXEC (@SQL)
---------------------------------------------------HERE STARTS THE SEPAERATE BILL CALCULATION
SET @LTAMT=0
SET @YESITIS = 0
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,CHANNEL,BILLPOSITION1 FROM RESTCO ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--IF @COMPANY=1
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'Y'  GROUP BY TXNDT
--ELSE
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'Y'  AND SELFDEAL = 'N'  GROUP BY TXNDT
IF @COMPANY=1
begin
select txndt,amt into #dummy1 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company  and channel=@channel and split='Y'
declare cur_kitroom cursor for select txndt,amt from #dummy1
end
ELSE
begin
select txndt,amt into #dummy2 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company  and channel=@channel and selfdeal='N' and split='Y'
declare cur_kitroom cursor for select txndt,amt from #dummy2
end
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1 <> 0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 100 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
if @company=1 drop table #dummy1 else drop table #dummy2
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION1 FROM PHOUTLET WHERE BILLPOSITION1 <> 0 ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--DECLARE CUR_KITROOM CURSOR FOR select TXNDT,'AMT' = SUM(AMT) from PH_ROOM WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SPLIT= 'Y' GROUP BY TXNDT
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno) into #dummy3
from phone where gno=@gno and outlet=@channel and split='Y'
declare cur_kitroom cursor for select txndt,amt from #dummy3
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1 <> 0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 200 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
drop table #dummy3
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
-------- first  try for travel desk 
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION1 FROM TOUTLET where billposition1 <> 0 ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
DECLARE CUR_KITROOM CURSOR FOR select finaldate,'AMT' = SUM(AMT) from tt_chalan_ol  WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SPLIT = 'Y'   GROUP BY finaldate
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1<>0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 300 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
-----traveldesk ends
--PRINT 'PERIA'
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,isnull(RENT,0),isnull(DISCOUNT,0),isnull(ITEM1,0),isnull(ITEM2,0),isnull(ITEM3,0),isnull(ITEM4,0),isnull(ITEM5,0),isnull(ITEM6,0),isnull(ITEM7,0),isnull(ITEM8,0),isnull(ITEM9,0),isnull(ITEM10,0),isnull(LT,0),isnull(SC,0),isnull(TOTAL,0)  FROM ' + @TEMPTABLE
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LT-@DISCOUNT
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
       IF @YESITIS=1 
 BEGIN
  SET @SQL = 'INSERT INTO ' +  @TEMPTABLE  + ' ( TOTAL,TXNDT)  VALUES( ' + CONVERT(VARCHAR(14),@CTOTAL ) + ', '' 31 JAN 9999'' )'
  EXEC (@SQL)
  SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET BILLORDER=1,SECTION=''B'''
  EXEC (@SQL)
 END
--PRINT 'PERIYAKARUPPA 3445'
SET @SQL = 'DECLARE T_CUR CURSOR FOR SELECT TXNDT,isnull(RENT,0),isnull(DISCOUNT,0),isnull(ITEM1,0),isnull(ITEM2,0),isnull(ITEM3,0),isnull(ITEM4,0),isnull(ITEM5,0),isnull(ITEM6,0),isnull(ITEM7,0),isnull(ITEM8,0),isnull(ITEM9,0),isnull(ITEM10,0),isnull(LT,0),isnull(SC,0),isnull(TOTAL,0),billorder,section,isnull(OUTLET1,0),isnull(OUTLET2,0),isnull(OUTLET3,0),isnull(OUTLET4,0),isnull(OUTLET5,0),isnull(OUTLET6,0),isnull(OUTLET7,0),isnull(OUTLET8,0),isnull(OUTLET9,0),isnull(OUTLET10,0)  FROM ' + @TEMPTABLE 
EXEC (@SQL)
OPEN T_CUR
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'INSERT INTO ' +  @TEMPTABLETEMP + ' ( TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ''' + CONVERT(VARCHAR(16),@TXNDT)+''',' + CONVERT(VARCHAR(14),@RENT)
SET @SQL = @SQL + ' ,' + CONVERT(VARCHAR(15),@DISCOUNT) + ',' + CONVERT(VARCHAR(15),@ITEM1)+ ',' + CONVERT(VARCHAR(15),@ITEM2)+ ',' + CONVERT(VARCHAR(15),@ITEM3)+ ',' + CONVERT(VARCHAR(15),@ITEM4)+ ',' + CONVERT(VARCHAR(15),@ITEM5)
SET @SQL = @SQL +  ',' + CONVERT(VARCHAR(15),@ITEM6)+ ',' + CONVERT(VARCHAR(15),@ITEM7)+ ',' + CONVERT(VARCHAR(15),@ITEM8)+ ',' + CONVERT(VARCHAR(15),@ITEM9)+ ','+CONVERT(VARCHAR(15),@ITEM10)+ ',' + CONVERT(VARCHAR(15),@LT)+ ',' + CONVERT(VARCHAR(15),@SC)+ ',' + CONVERT(VARCHAR(15),@TOTAL) + ',' + STR(@BILLORDER) + ',''' + @SECTION + ''','
SET @SQL = @SQL + STR(@OUTLET1) + ','+ STR(@OUTLET2)+','+ STR(@OUTLET3)+','+ STR(@OUTLET4)+','+ STR(@OUTLET5)+','+ STR(@OUTLET6)+','+ STR(@OUTLET7)+','+ STR(@OUTLET8)+','+ STR(@OUTLET9)+','+ STR(@OUTLET10) + ')'
EXEC(@SQL)
IF @@ERROR <> 0
BEGIN
CLOSE T_CUR
DEALLOCATE T_CUR
GOTO E_ERROR
END
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
END
CLOSE T_CUR
DEALLOCATE T_CUR
--HERE ENDS SEPERATE BILLS CALCULATIONS
-- no mistake upto here
--print 'ramanathan' + @temptable
--------------------------------------------------------------------
SET @SQL ='TRUNCATE TABLE ' + @TEMPTABLE ------------------------------------------------------- OTHER COMPANY BILLS STARTS HERE
EXEC(@SQL)
DECLARE CUR_CO CURSOR FOR SELECT COMPANY FROM COMPANY WHERE COMPANY > 1 ORDER BY COMPANY
OPEN CUR_CO
FETCH NEXT FROM CUR_CO INTO @COMPANY
WHILE @@FETCH_STATUS=0
BEGIN
   SET @YESITIS = 0
    DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,CHANNEL,BILLPOSITION2 FROM RESTCO WHERE COMPANY = @COMPANY  AND BILLPOSITION2<>0 ORDER BY BILLPOSITION2
    OPEN CUR_RESTCO
    FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1 
   WHILE @@FETCH_STATUS=0
      BEGIN
--      DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL  AND SELFDEAL='Y' and split='N'  GROUP BY TXNDT
select txndt,amt into #dummy4 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company  and channel=@channel and split='N' and selfdeal='Y'
declare cur_kitroom cursor for select txndt,amt from #dummy4
     OPEN CUR_KITROOM
      FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
       WHILE @@FETCH_STATUS=0
            BEGIN
                 SET @YESITIS = 1
                SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)' 
                --PRINT @SQL
                EXEC(@SQL)
                       IF @BP1<>0 
                       BEGIN
                              SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 100 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
                             EXEC(@SQL)
                                 IF @@ERROR<>0
                                 BEGIN 
                                     CLOSE CUR_KITROOM 
                                    DEALLOCATE CUR_KITROOM
                                  CLOSE CUR_RESTCO 
                                  DEALLOCATE CUR_RESTCO
                                  GOTO E_ERROR
                                 END
                       END
                     FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
            END
drop table #dummy4
       CLOSE CUR_KITROOM 
      DEALLOCATE CUR_KITROOM
      FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
   END
   CLOSE CUR_RESTCO
   DEALLOCATE CUR_RESTCO
   DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION2 FROM PHOUTLET WHERE COMPANY=@COMPANY AND BILLPOSITION2<>0 ORDER BY BILLPOSITION2
   OPEN CUR_RESTCO
   FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1 
   WHILE @@FETCH_STATUS=0
   BEGIN
--     DECLARE CUR_KITROOM CURSOR FOR select TXNDT,'AMT' = SUM(AMT) from PH_ROOM WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SELFDEAL= 'Y'  and split='N'  GROUP BY TXNDT
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno) into #dummy6
from phone where gno=@gno and outlet=@channel and split='N' and selfdeal='Y'
declare cur_kitroom cursor for select txndt,amt from #dummy6
     OPEN CUR_KITROOM
    FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT  
   WHILE @@FETCH_STATUS=0
      BEGIN
         SET @YESITIS = 1
         SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
          EXEC(@SQL)
          IF @BP1<>0
          BEGIN
                SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 200 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
               EXEC(@SQL)
              IF @@ERROR<>0
                   BEGIN
                       CLOSE CUR_KITROOM
                       DEALLOCATE CUR_KITROOM
                        CLOSE CUR_RESTCO
                      DEALLOCATE CUR_RESTCO
                     GOTO E_ERROR
                   END
          END
         FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
      END
drop table #dummy6
     CLOSE CUR_KITROOM 
      DEALLOCATE CUR_KITROOM
     FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
   END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
/* here starts travel desk */
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION2 FROM TOUTLET WHERE COMPANY=@COMPANY AND BILLPOSITION2<>0 ORDER BY BILLPOSITION2
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
DECLARE CUR_KITROOM CURSOR FOR select finaldate,'AMT' = SUM(AMT) from tt_chalan_ol  WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SELFDEAL= 'Y'  and split='N'  GROUP BY finaldate
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1<>0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 300 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)  
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
/*here ends travel desk*/
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL  FROM ' + @TEMPTABLE
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LT+@SC-@DISCOUNT
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
      IF @YESITIS=1
 BEGIN
 SET @SQL = 'INSERT INTO ' +  @TEMPTABLE  + ' ( TOTAL,TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,billorder,section,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ' + CONVERT(VARCHAR(14),@CTOTAL ) + ', '' 31JAN 9999'',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'' '' ,0,0,0,0,0,0,0,0,0,0)'
 --print @sql
 EXEC (@SQL)
 if @@error <> 0 goto e_error
 ---- UPDATING ORIGINAL TEMPFILE
 SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET BILLORDER=' + STR(@COMPANY) + ',section=''' + 'A' + ''''
 EXEC (@SQL)
 END
SET @SQL = 'DECLARE T_CUR CURSOR FOR SELECT TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10  FROM ' + @TEMPTABLE
EXEC (@SQL)
OPEN T_CUR
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
IF @@ERROR <> 0 GOTO E_ERROR
WHILE @@FETCH_STATUS=0
BEGIN
--PRINT 'PERIYA KARUPPA 111'
SET @SQL = 'INSERT INTO ' +  @TEMPTABLETEMP + ' ( TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ''' + CONVERT(VARCHAR(16),@TXNDT)+''',' + CONVERT(VARCHAR(14),@RENT)
SET @SQL = @SQL + ' ,' + CONVERT(VARCHAR(15),@DISCOUNT) + ',' + CONVERT(VARCHAR(15),@ITEM1)+ ',' + CONVERT(VARCHAR(15),@ITEM2)+ ',' + CONVERT(VARCHAR(15),@ITEM3)+ ',' + CONVERT(VARCHAR(15),@ITEM4)+ ',' + CONVERT(VARCHAR(15),@ITEM5)
SET @SQL = @SQL +  ',' + CONVERT(VARCHAR(15),@ITEM6)+ ',' + CONVERT(VARCHAR(15),@ITEM7)+ ',' + CONVERT(VARCHAR(15),@ITEM8)+ ',' + CONVERT(VARCHAR(15),@ITEM9)+ ','+CONVERT(VARCHAR(15),@ITEM10)+ ',' + CONVERT(VARCHAR(15),@LT)+ ',' + CONVERT(VARCHAR(15),@SC)+ ',' + CONVERT(VARCHAR(15),@TOTAL) + ',' + STR(@BILLORDER) + ',''' + @SECTION + ''','
SET @SQL = @SQL + STR(@OUTLET1) + ','+ STR(@OUTLET2)+','+ STR(@OUTLET3)+','+ STR(@OUTLET4)+','+ STR(@OUTLET5)+','+ STR(@OUTLET6)+','+ STR(@OUTLET7)+','+ STR(@OUTLET8)+','+ STR(@OUTLET9)+','+ STR(@OUTLET10) + ')'
EXEC(@SQL)
IF @@ERROR <> 0
BEGIN
CLOSE T_CUR
DEALLOCATE T_CUR
GOTO E_ERROR
END
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
END
CLOSE T_CUR
DEALLOCATE T_CUR
SET @SQL ='TRUNCATE TABLE ' + @TEMPTABLE 
EXEC(@SQL)
FETCH NEXT FROM CUR_CO INTO @COMPANY
END
CLOSE CUR_CO
DEALLOCATE CUR_CO
----------------------------------------------------------OTHER COMPANY BILLS END HERE
----------------------COMPLEMENTARY BILLS STARTS HERE (FIRST VOUCHING ITEMS NEXT NO VOUCHER ITEMS
---- here starts the Complementary bills processing
SET @LTAMT=0
SET @YESITIS = 0
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,CHANNEL,BILLPOSITION1 FROM RESTCO ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--IF @COMPANY=1
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'C'  GROUP BY TXNDT
--ELSE
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'C'  AND SELFDEAL = 'N'  GROUP BY TXNDT
if @company=1
begin
select txndt,amt into #dummy8 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company  and channel=@channel and split='C'
declare cur_kitroom cursor for select txndt,amt from #dummy8
end
ELSE

begin

--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'N' AND LUX = 'Y'  AND SELFDEAL='N'  GROUP BY TXNDT
select txndt,amt into #dummy9 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company and channel=@channel and selfdeal='N' and split='C'
declare cur_kitroom cursor for select txndt,amt from #dummy9
end
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1 <> 0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 100 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)  + ' WHERE datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
if @company=1 drop table #dummy8 else drop table #dummy9
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION1 FROM PHOUTLET WHERE BILLPOSITION1 <> 0 ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--DECLARE CUR_KITROOM CURSOR FOR select TXNDT,'AMT' = SUM(AMT) from PH_ROOM WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SPLIT= 'C' GROUP BY TXNDT
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno) into #dummy81
from phone where gno=@gno and outlet=@channel and split='C'
declare cur_kitroom cursor for select txndt,amt from #dummy81
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1 <> 0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 200 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
drop table #dummy81
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
--PRINT 'PERIA'
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,isnull(RENT,0),isnull(DISCOUNT,0),isnull(ITEM1,0),isnull(ITEM2,0),isnull(ITEM3,0),isnull(ITEM4,0),isnull(ITEM5,0),isnull(ITEM6,0),isnull(ITEM7,0),isnull(ITEM8,0),isnull(ITEM9,0),isnull(ITEM10,0),isnull(LT,0),isnull(SC,0),isnull(TOTAL,0)  FROM ' + @TEMPTABLE
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LT+@SC-@DISCOUNT
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
       IF @YESITIS=1 
 BEGIN
  SET @SQL = 'INSERT INTO ' +  @TEMPTABLE  + ' ( TOTAL,TXNDT)  VALUES( ' + CONVERT(VARCHAR(14),@CTOTAL ) + ', '' 31 JAN 9999'' )'
  EXEC (@SQL)
  SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET BILLORDER=1,SECTION=''C'''
  EXEC (@SQL)
 END
--PRINT 'PERIYAKARUPPA 3445'
SET @SQL = 'DECLARE T_CUR CURSOR FOR SELECT TXNDT,isnull(RENT,0),isnull(DISCOUNT,0),isnull(ITEM1,0),isnull(ITEM2,0),isnull(ITEM3,0),isnull(ITEM4,0),isnull(ITEM5,0),isnull(ITEM6,0),isnull(ITEM7,0),isnull(ITEM8,0),isnull(ITEM9,0),isnull(ITEM10,0),isnull(LT,0),isnull(SC,0),isnull(TOTAL,0),billorder,section,isnull(OUTLET1,0),isnull(OUTLET2,0),isnull(OUTLET3,0),isnull(OUTLET4,0),isnull(OUTLET5,0),isnull(OUTLET6,0),isnull(OUTLET7,0),isnull(OUTLET8,0),isnull(OUTLET9,0),isnull(OUTLET10,0)  FROM ' + @TEMPTABLE 
EXEC (@SQL)
OPEN T_CUR
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'INSERT INTO ' +  @TEMPTABLETEMP + ' ( TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ''' + CONVERT(VARCHAR(16),@TXNDT)+''',' + CONVERT(VARCHAR(14),@RENT)
SET @SQL = @SQL + ' ,' + CONVERT(VARCHAR(15),@DISCOUNT) + ',' + CONVERT(VARCHAR(15),@ITEM1)+ ',' + CONVERT(VARCHAR(15),@ITEM2)+ ',' + CONVERT(VARCHAR(15),@ITEM3)+ ',' + CONVERT(VARCHAR(15),@ITEM4)+ ',' + CONVERT(VARCHAR(15),@ITEM5)
SET @SQL = @SQL +  ',' + CONVERT(VARCHAR(15),@ITEM6)+ ',' + CONVERT(VARCHAR(15),@ITEM7)+ ',' + CONVERT(VARCHAR(15),@ITEM8)+ ',' + CONVERT(VARCHAR(15),@ITEM9)+ ','+CONVERT(VARCHAR(15),@ITEM10)+ ',' + CONVERT(VARCHAR(15),@LT)+ ',' + CONVERT(VARCHAR(15),@SC)+ ',' + CONVERT(VARCHAR(15),@TOTAL) + ',' + STR(@BILLORDER) + ',''' + @SECTION + ''','
SET @SQL = @SQL + STR(@OUTLET1) + ','+ STR(@OUTLET2)+','+ STR(@OUTLET3)+','+ STR(@OUTLET4)+','+ STR(@OUTLET5)+','+ STR(@OUTLET6)+','+ STR(@OUTLET7)+','+ STR(@OUTLET8)+','+ STR(@OUTLET9)+','+ STR(@OUTLET10) + ')'
EXEC(@SQL)
IF @@ERROR <> 0
BEGIN
CLOSE T_CUR
DEALLOCATE T_CUR
GOTO E_ERROR
END
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
END
CLOSE T_CUR
DEALLOCATE T_CUR
---- here ends Complementary bills processing FOR COMPANY 1  - TO BE VOUCHED ITEM
SET @SQL ='TRUNCATE TABLE ' + @TEMPTABLE ------------------------------------------------------- OTHER COMPANY  COMPLEMENTARY BILLS STARTS HERE
EXEC(@SQL)
DECLARE CUR_CO CURSOR FOR SELECT COMPANY FROM COMPANY WHERE COMPANY > 1 ORDER BY COMPANY
OPEN CUR_CO
FETCH NEXT FROM CUR_CO INTO @COMPANY
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 0
    DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,CHANNEL,BILLPOSITION2 FROM RESTCO WHERE COMPANY = @COMPANY  AND BILLPOSITION2<>0 ORDER BY BILLPOSITION2
    OPEN CUR_RESTCO
    FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1 
   WHILE @@FETCH_STATUS=0
   BEGIN
--   DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL  AND SELFDEAL='Y' and split='C'  GROUP BY TXNDT
select txndt,amt into #dummy10 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company and channel=@channel and selfdeal='Y' and split='C'
declare cur_kitroom cursor for select txndt,amt from #dummy10
   OPEN CUR_KITROOM
   FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
   WHILE @@FETCH_STATUS=0
   BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
   EXEC(@SQL)
   IF @BP1<>0 
   BEGIN
   SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 100 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)  + 'where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))
   EXEC(@SQL)
   IF @@ERROR<>0
   BEGIN 
   CLOSE CUR_KITROOM 
   DEALLOCATE CUR_KITROOM
   CLOSE CUR_RESTCO 
   DEALLOCATE CUR_RESTCO
   GOTO E_ERROR
   END
   END
   FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
   END
drop table #dummy10
   CLOSE CUR_KITROOM 
   DEALLOCATE CUR_KITROOM
   FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
   END
   CLOSE CUR_RESTCO
   DEALLOCATE CUR_RESTCO
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION2 FROM PHOUTLET WHERE COMPANY=@COMPANY AND BILLPOSITION2<>0 ORDER BY BILLPOSITION2
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--DECLARE CUR_KITROOM CURSOR FOR select TXNDT,'AMT' = SUM(AMT) from PH_ROOM WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SELFDEAL= 'Y'  and split='C'  GROUP BY TXNDT
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno) into #dummy11
from phone where gno=@gno and outlet=@channel and split='C' and selfdeal='Y'
declare cur_kitroom cursor for  select txndt,amt from #duumy11
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1<>0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 200 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)  + 'where datediff(d,''1/1/80'',txndt) = ' + str(datediff(d,'1/1/80',@txndt))
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
drop table #dummy11
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL  FROM ' + @TEMPTABLE
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LT+@SC-@DISCOUNT
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
      IF @YESITIS=1
 BEGIN
 SET @SQL = 'INSERT INTO ' +  @TEMPTABLE  + ' ( TOTAL,TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,billorder,section,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ' + CONVERT(VARCHAR(14),@CTOTAL ) + ', '' 31JAN 9999'',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'' '' ,0,0,0,0,0,0,0,0,0,0)'
 EXEC (@SQL)
 if @@error <> 0 goto e_error
 ---- UPDATING ORIGINAL TEMPFILE
 SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET BILLORDER=' + STR(@COMPANY) + ',section=''' + 'C' + ''''
 EXEC (@SQL)
 END
SET @SQL = 'DECLARE T_CUR CURSOR FOR SELECT TXNDT,isnull(RENT,0),isnull(DISCOUNT,0),isnull(ITEM1,0),isnull(ITEM2,0),isnull(ITEM3,0),isnull(ITEM4,0),isnull(ITEM5,0),isnull(ITEM6,0),isnull(ITEM7,0),isnull(ITEM8,0),isnull(ITEM9,0),isnull(ITEM10,0),isnull(LT,0),isnull(SC,0),isnull(TOTAL,0),billorder,section,isnull(OUTLET1,0),isnull(OUTLET2,0),isnull(OUTLET3,0),isnull(OUTLET4,0),isnull(OUTLET5,0),isnull(OUTLET6,0),isnull(OUTLET7,0),isnull(OUTLET8,0),isnull(OUTLET9,0),isnull(OUTLET10,0)  FROM ' + @TEMPTABLE 
EXEC (@SQL)
OPEN T_CUR
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'INSERT INTO ' +  @TEMPTABLETEMP + ' ( TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ''' + CONVERT(VARCHAR(16),@TXNDT)+''',' + CONVERT(VARCHAR(14),@RENT)
SET @SQL = @SQL + ' ,' + CONVERT(VARCHAR(15),@DISCOUNT) + ',' + CONVERT(VARCHAR(15),@ITEM1)+ ',' + CONVERT(VARCHAR(15),@ITEM2)+ ',' + CONVERT(VARCHAR(15),@ITEM3)+ ',' + CONVERT(VARCHAR(15),@ITEM4)+ ',' + CONVERT(VARCHAR(15),@ITEM5)
SET @SQL = @SQL +  ',' + CONVERT(VARCHAR(15),@ITEM6)+ ',' + CONVERT(VARCHAR(15),@ITEM7)+ ',' + CONVERT(VARCHAR(15),@ITEM8)+ ',' + CONVERT(VARCHAR(15),@ITEM9)+ ','+CONVERT(VARCHAR(15),@ITEM10)+ ',' + CONVERT(VARCHAR(15),@LT)+ ',' + CONVERT(VARCHAR(15),@SC)+ ',' + CONVERT(VARCHAR(15),@TOTAL) + ',' + STR(@BILLORDER) + ',''' + @SECTION + ''','
SET @SQL = @SQL + STR(@OUTLET1) + ','+ STR(@OUTLET2)+','+ STR(@OUTLET3)+','+ STR(@OUTLET4)+','+ STR(@OUTLET5)+','+ STR(@OUTLET6)+','+ STR(@OUTLET7)+','+ STR(@OUTLET8)+','+ STR(@OUTLET9)+','+ STR(@OUTLET10) + ')'
EXEC(@SQL)
IF @@ERROR <> 0
BEGIN
CLOSE T_CUR
DEALLOCATE T_CUR
GOTO E_ERROR
END
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
END
CLOSE T_CUR
DEALLOCATE T_CUR
SET @SQL ='TRUNCATE TABLE ' + @TEMPTABLE 
EXEC(@SQL)
FETCH NEXT FROM CUR_CO INTO @COMPANY
END
CLOSE CUR_CO
DEALLOCATE CUR_CO
----------------------------------------------------------OTHER COMPANY COMPLEMENTARY  BILLS END HERE (with vouching)
SET @LTAMT=0
SET @YESITIS = 0
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,CHANNEL,BILLPOSITION1 FROM RESTCO ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--IF @COMPANY=1
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'F'  GROUP BY TXNDT
--ELSE
--DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL AND SPLIT = 'F'  AND SELFDEAL = 'N'  GROUP BY TXNDT
if @company=1
begin
select txndt,amt into #duumy12 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company  and channel=@channel and split='F'
declare cur_kitroom cursor for select txndt,amt from #dummy12
end
ELSE
begin
select txndt,amt into #dummy13 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company and channel=@channel and selfdeal='N' and split='F'
declare cur_kitroom cursor for  select txndt,amt from #dummy13
end
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1 <> 0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 100 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
if @company=1 drop table #dummy12 else drop table #dummy13
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION1 FROM PHOUTLET WHERE BILLPOSITION1 <> 0 ORDER BY BILLPOSITION1
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--DECLARE CUR_KITROOM CURSOR FOR select TXNDT,'AMT' = SUM(AMT) from PH_ROOM WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SPLIT= 'F' GROUP BY TXNDT
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno) into #dummy14
from phone where gno=@gno and outlet=@channel and split='F'
declare cur_kitroom cursor for select txndt,amt from #dummy14
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1 <> 0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 200 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
drop table #dummy14
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
--PRINT 'PERIA'
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,isnull(RENT,0),isnull(DISCOUNT,0),isnull(ITEM1,0),isnull(ITEM2,0),isnull(ITEM3,0),isnull(ITEM4,0),isnull(ITEM5,0),isnull(ITEM6,0),isnull(ITEM7,0),isnull(ITEM8,0),isnull(ITEM9,0),isnull(ITEM10,0),isnull(LT,0),isnull(SC,0),isnull(TOTAL,0)  FROM ' + @TEMPTABLE
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LT+@SC-@DISCOUNT
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
       IF @YESITIS=1 
 BEGIN
  SET @SQL = 'INSERT INTO ' +  @TEMPTABLE  + ' ( TOTAL,TXNDT)  VALUES( ' + CONVERT(VARCHAR(14),@CTOTAL ) + ', '' 31 JAN 9999'' )'
  EXEC (@SQL)
  SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET BILLORDER=1,SECTION=''F'''
  EXEC (@SQL)
 END
--PRINT 'PERIYAKARUPPA 3445'
SET @SQL = 'DECLARE T_CUR CURSOR FOR SELECT TXNDT,isnull(RENT,0),isnull(DISCOUNT,0),isnull(ITEM1,0),isnull(ITEM2,0),isnull(ITEM3,0),isnull(ITEM4,0),isnull(ITEM5,0),isnull(ITEM6,0),isnull(ITEM7,0),isnull(ITEM8,0),isnull(ITEM9,0),isnull(ITEM10,0),isnull(LT,0),isnull(SC,0),isnull(TOTAL,0),billorder,section,isnull(OUTLET1,0),isnull(OUTLET2,0),isnull(OUTLET3,0),isnull(OUTLET4,0),isnull(OUTLET5,0),isnull(OUTLET6,0),isnull(OUTLET7,0),isnull(OUTLET8,0),isnull(OUTLET9,0),isnull(OUTLET10,0)  FROM ' + @TEMPTABLE 
EXEC (@SQL)
OPEN T_CUR
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'INSERT INTO ' +  @TEMPTABLETEMP + ' ( TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ''' + CONVERT(VARCHAR(16),@TXNDT)+''',' + CONVERT(VARCHAR(14),@RENT)
SET @SQL = @SQL + ' ,' + CONVERT(VARCHAR(15),@DISCOUNT) + ',' + CONVERT(VARCHAR(15),@ITEM1)+ ',' + CONVERT(VARCHAR(15),@ITEM2)+ ',' + CONVERT(VARCHAR(15),@ITEM3)+ ',' + CONVERT(VARCHAR(15),@ITEM4)+ ',' + CONVERT(VARCHAR(15),@ITEM5)
SET @SQL = @SQL +  ',' + CONVERT(VARCHAR(15),@ITEM6)+ ',' + CONVERT(VARCHAR(15),@ITEM7)+ ',' + CONVERT(VARCHAR(15),@ITEM8)+ ',' + CONVERT(VARCHAR(15),@ITEM9)+ ','+CONVERT(VARCHAR(15),@ITEM10)+ ',' + CONVERT(VARCHAR(15),@LT)+ ',' + CONVERT(VARCHAR(15),@SC)+ ',' + CONVERT(VARCHAR(15),@TOTAL) + ',' + STR(@BILLORDER) + ',''' + @SECTION + ''','
SET @SQL = @SQL + STR(@OUTLET1) + ','+ STR(@OUTLET2)+','+ STR(@OUTLET3)+','+ STR(@OUTLET4)+','+ STR(@OUTLET5)+','+ STR(@OUTLET6)+','+ STR(@OUTLET7)+','+ STR(@OUTLET8)+','+ STR(@OUTLET9)+','+ STR(@OUTLET10) + ')'
EXEC(@SQL)
IF @@ERROR <> 0
BEGIN
CLOSE T_CUR
DEALLOCATE T_CUR
GOTO E_ERROR
END
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
END
CLOSE T_CUR
DEALLOCATE T_CUR
---- here ends Complementary bills processing FOR COMPANY 1  - NO  VOUCHER ITEM
SET @SQL ='TRUNCATE TABLE ' + @TEMPTABLE ------------------------------------------------------- OTHER COMPANY  COMPLEMENTARY BILLS STARTS HERE
EXEC(@SQL)
DECLARE CUR_CO CURSOR FOR SELECT COMPANY FROM COMPANY WHERE COMPANY > 1 ORDER BY COMPANY
OPEN CUR_CO
FETCH NEXT FROM CUR_CO INTO @COMPANY
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 0
    DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,CHANNEL,BILLPOSITION2 FROM RESTCO WHERE COMPANY = @COMPANY  AND BILLPOSITION2<>0 ORDER BY BILLPOSITION2
    OPEN CUR_RESTCO
    FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1 
   WHILE @@FETCH_STATUS=0
   BEGIN
--   DECLARE CUR_KITROOM CURSOR  FOR select TXNDT,'AMT' = SUM(AMT) from KIT_ROOM WHERE GNO = @GNO AND COMPANY = @COMPANY   AND CHANNEL = @CHANNEL  AND SELFDEAL='Y' and split='F'  GROUP BY TXNDT
select txndt,amt into #dummy15 from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno)  a where company=@company and channel=@channel and selfdeal='Y' and split='F'
declare cur_kitroom cursor for select txndt,amt from #dummy15
   OPEN CUR_KITROOM
   FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
   WHILE @@FETCH_STATUS=0
   BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
   EXEC(@SQL)
   IF @BP1<>0 
   BEGIN
   SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 100 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)  
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
   EXEC(@SQL)
   IF @@ERROR<>0
   BEGIN 
   CLOSE CUR_KITROOM 
   DEALLOCATE CUR_KITROOM
   CLOSE CUR_RESTCO 
   DEALLOCATE CUR_RESTCO
   GOTO E_ERROR
   END
   END
   FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
   END
drop table #dummy15
   CLOSE CUR_KITROOM 
   DEALLOCATE CUR_KITROOM
   FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
   END
   CLOSE CUR_RESTCO
   DEALLOCATE CUR_RESTCO
DECLARE CUR_RESTCO CURSOR FOR SELECT COMPANY,OUTLET,BILLPOSITION2 FROM PHOUTLET WHERE COMPANY=@COMPANY AND BILLPOSITION2<>0 ORDER BY BILLPOSITION2
OPEN CUR_RESTCO
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
WHILE @@FETCH_STATUS=0
BEGIN
--DECLARE CUR_KITROOM CURSOR FOR select TXNDT,'AMT' = SUM(AMT) from PH_ROOM WHERE GNO = @GNO AND  OUTLET = @CHANNEL AND SELFDEAL= 'Y'  and split='F'  GROUP BY TXNDT
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno) into #dummy16
from phone where gno=@gno and outlet=@channel and split='F' and selfdeal='Y'
declare cur_kitroom cursor for select txndt,amt from #dummy16
OPEN CUR_KITROOM
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
WHILE @@FETCH_STATUS=0
BEGIN
SET @YESITIS = 1
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLE + ' WHERE DATEDIFF(d,''1/1/80'',TXNDT)=' + STR(DATEDIFF(d,'1/1/80',@TXNDT))  + ')   INSERT INTO ' + @TEMPTABLE  + '(TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @BP1<>0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE  + ' SET OUTLET' + LTRIM(STR(@BP1)) + ' = 200 + ' + STR(@CHANNEL) + ',ITEM' + LTRIM(STR(@BP1)) + ' = ITEM' + LTRIM(STR(@BP1)) + ' + '  + CONVERT(VARCHAR(14),@AMT)  
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
EXEC(@SQL)
IF @@ERROR<>0
BEGIN
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
GOTO E_ERROR
END
END
FETCH NEXT FROM CUR_KITROOM INTO @TXNDT,@AMT
END
drop table #dummy16
CLOSE CUR_KITROOM
DEALLOCATE CUR_KITROOM
FETCH NEXT FROM CUR_RESTCO INTO @COMPANY,@CHANNEL,@BP1
END
CLOSE CUR_RESTCO
DEALLOCATE CUR_RESTCO
SET @SQL = 'DECLARE CUR_TEMP CURSOR FOR SELECT TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL  FROM ' + @TEMPTABLE
--PRINT @SQL
EXEC (@SQL)
OPEN CUR_TEMP
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
SET @CTOTAL = 0
WHILE @@FETCH_STATUS=0
BEGIN
SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET TOTAL=RENT+ITEM1+ITEM2 +ITEM3+ITEM4 +ITEM5+ITEM6+ITEM7+ITEM8+ITEM9+ITEM10+LT+SC-DISCOUNT WHERE CURRENT OF CUR_TEMP'
EXEC (@SQL)
SET @CTOTAL = @CTOTAL + @RENT+@ITEM1+@ITEM2+@ITEM3+@ITEM4+@ITEM5+@ITEM6+@ITEM7+@ITEM8+@ITEM9+@ITEM10+@LT+@SC-@DISCOUNT
FETCH NEXT FROM CUR_TEMP INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL
END
CLOSE CUR_TEMP
DEALLOCATE CUR_TEMP
      IF @YESITIS=1
 BEGIN
 SET @SQL = 'INSERT INTO ' +  @TEMPTABLE  + ' ( TOTAL,TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,billorder,section,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ' + CONVERT(VARCHAR(14),@CTOTAL ) + ', '' 31JAN 9999'',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'' '' ,0,0,0,0,0,0,0,0,0,0)'
 EXEC (@SQL)
 if @@error <> 0 goto e_error
 ---- UPDATING ORIGINAL TEMPFILE
 SET @SQL = 'UPDATE ' + @TEMPTABLE + ' SET BILLORDER=' + STR(@COMPANY) + ',section=''' + 'F' + ''''
 EXEC (@SQL)
 END
SET @SQL = 'DECLARE T_CUR CURSOR FOR SELECT TXNDT,
isnull(RENT,0),isnull(DISCOUNT,0),isnull(ITEM1,0),isnull(ITEM2,0),isnull(ITEM3,0),isnull(ITEM4,0),isnull(ITEM5,0),isnull(ITEM6,0),isnull(ITEM7,0),isnull(ITEM8,0),isnull(ITEM9,0),isnull(ITEM10,0),isnull(LT,0),isnull(SC,0),isnull(TOTAL,0),
BILLORDER,SECTION,
isnull(OUTLET1,0),isnull(OUTLET2,0),isnull(OUTLET3,0), isnull(OUTLET4,0),isnull(OUTLET5,0),isnull(OUTLET6,0),isnull(OUTLET7,0),isnull(OUTLET8,0),isnull(OUTLET9,0),isnull(OUTLET10,0)  FROM ' + @TEMPTABLE
EXEC (@SQL)
OPEN T_CUR
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
IF @@ERROR <> 0 GOTO E_ERROR
WHILE @@FETCH_STATUS=0
BEGIN
--PRINT 'PERIYA KARUPPA 111'
SET @SQL = 'INSERT INTO ' +  @TEMPTABLETEMP + ' ( TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ''' + CONVERT(VARCHAR(16),@TXNDT,106)+''',' + CONVERT(VARCHAR(14),@RENT)
SET @SQL = @SQL + ' ,' + CONVERT(VARCHAR(15),@DISCOUNT) + ',' + CONVERT(VARCHAR(15),@ITEM1)+ ',' + CONVERT(VARCHAR(15),@ITEM2)+ ',' + CONVERT(VARCHAR(15),@ITEM3)+ ',' + CONVERT(VARCHAR(15),@ITEM4)+ ',' + CONVERT(VARCHAR(15),@ITEM5)
SET @SQL = @SQL +  ',' + CONVERT(VARCHAR(15),@ITEM6)+ ',' + CONVERT(VARCHAR(15),@ITEM7)+ ',' + CONVERT(VARCHAR(15),@ITEM8)+ ',' + CONVERT(VARCHAR(15),@ITEM9)+ ','+CONVERT(VARCHAR(15),@ITEM10)+ ',' + CONVERT(VARCHAR(15),@LT)+ ',' + CONVERT(VARCHAR(15),@SC)+ ',' + CONVERT(VARCHAR(15),@TOTAL) + ',' + STR(@BILLORDER) + ',''' + @SECTION + ''','
SET @SQL = @SQL + STR(@OUTLET1) + ','+ STR(@OUTLET2)+','+ STR(@OUTLET3)+','+ STR(@OUTLET4)+','+ STR(@OUTLET5)+','+ STR(@OUTLET6)+','+ STR(@OUTLET7)+','+ STR(@OUTLET8)+','+ STR(@OUTLET9)+','+ STR(@OUTLET10) + ')'
EXEC(@SQL)
IF @@ERROR <> 0
BEGIN
CLOSE T_CUR
DEALLOCATE T_CUR
GOTO E_ERROR
END
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,@BILLORDER,@SECTION,@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10
END
CLOSE T_CUR
DEALLOCATE T_CUR
SET @SQL ='TRUNCATE TABLE ' + @TEMPTABLE 
EXEC(@SQL)
FETCH NEXT FROM CUR_CO INTO @COMPANY
END
CLOSE CUR_CO
DEALLOCATE CUR_CO
----------------------------------------------------------OTHER COMPANY COMPLEMENTARY  BILLS END HERE   NO VOUCHER ITEMS
top1:
SET @SQL = 'DROP TABLE ' + @TEMPTABLE
EXEC(@SQL)
--- new try nov 24 2005
set @sql = 'declare sp_cbcursor cursor  for select company,split from roomdues where gno=' + str(@gno) 
exec (@sql)
IF @@ERROR <> 0 goto e_error
open sp_cbcursor
IF @@ERROR <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
IF @@ERROR <> 0 goto e_error
while @@fetch_status=0 
begin
set @sql = 'if not exists(select * from ' + @temptabletemp + ' where billorder = ' + str(@billorder) + ' and section = ''' + case when @section = 'Y' then 'B' else 'A'  end  +''')'
SET @SQL = @sql + '  INSERT INTO ' +  @TEMPTABLETEMP + ' ( TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ''' + CONVERT(VARCHAR(16),'31 jan 9999',106)+''',0' 
SET @SQL = @SQL + ' ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,' + STR(@BILLORDER) + ',''' + case when @SECTION='Y' then 'B' else 'A' end  + ''',0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @@ERROR <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
IF @@ERROR <> 0 goto e_error
end
close sp_cbcursor
IF @@ERROR <> 0 goto e_error
deallocate sp_cbcursor
IF @@ERROR <> 0 goto e_error
---- for advance unaccounted 25 nov 2005
set @sql = 'declare sp_cbcursor cursor  for select company,split from advance where gno=' + str(@gno) 
exec (@sql)
IF @@ERROR <> 0 goto e_error
open sp_cbcursor
IF @@ERROR <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
IF @@ERROR <> 0 goto e_error
while @@fetch_status=0 
begin
set @sql = 'if not exists(select * from ' + @temptabletemp + ' where billorder = ' + str(@billorder) + ' and section = ''' + case when @section = 'Y' then 'B' else 'A'  end  +''')'
SET @SQL = @sql + '  INSERT INTO ' +  @TEMPTABLETEMP + ' ( TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10)  VALUES( ''' + CONVERT(VARCHAR(16),'31 jan 9999',106)+''',0' 
SET @SQL = @SQL + ' ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,' + STR(@BILLORDER) + ',''' + case when @SECTION='Y' then 'B' else 'A' end  + ''',0,0,0,0,0,0,0,0,0,0)'
EXEC(@SQL)
IF @@ERROR <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
IF @@ERROR <> 0 goto e_error
end
close sp_cbcursor
IF @@ERROR <> 0 goto e_error
deallocate sp_cbcursor
IF @@ERROR <> 0 goto e_error
-- new try ends
COMMIT TRANSACTION
RETURN
E_ERROR:
ROLLBACK TRANSACTION
RAISERROR('BILL CREATION ERROR',16,-1)
RETURN




GO
/****** Object:  StoredProcedure [dbo].[sp_cbdummy]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sp_cb    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE procedure [dbo].[sp_cbdummy]  @temptable varchar(50), @gno int,@mark int=0
/* with encryption */
as
declare @sql varchar(5000)
declare @company int
declare @channel int
declare @bp1 int
declare @txndt datetime
declare @amt money
declare @rent money
declare @discount money
declare @item1 money
declare @item2 money
declare @item3 money
declare @item4 money
declare @item5 money
declare @item6 money
declare @item7 money
declare @item8 money
declare @item9 money
declare @item10 money
declare @lt money
declare @sc money
declare @total money
declare @sdt datetime
declare @edt datetime
declare @lrate money
declare @samt money
declare @eamt money
declare @famt money
declare @ltamt money
declare @amt2 money
declare @tno int
declare @gtotal money
declare @ctotal money
declare @billorder int
declare @section char(1)
declare @temptabletemp varchar(50)
declare @hostid char(18)
declare @outlet1 int
declare @outlet2 int
declare @outlet3 int
declare @outlet4 int
declare @outlet5 int
declare @outlet6 int
declare @outlet7 int
declare @outlet8 int
declare @outlet9 int
declare @outlet10 int
declare @yesitis int
declare @totrooms int
declare @temp varchar(20)
declare @ltcal char(1)
declare @ltontariff char(1)
declare @head int
declare @temptot money
declare @temptot1 money
declare @temptot2 money
declare @g_disc_ltrules tinyint
declare @taxsystem int
--declare @stamt decimal(12,2)
declare @st money
declare @stontariff char(3)
declare @strate money
declare @stdiscount money
declare @stlessamt money
declare @cessrate money
declare @cess money
declare @allinclusive char(1)
declare @lttype int
declare @nettrent money
declare @renttype int
set @taxsystem=0
select @taxsystem=g_taxsystem from setting
set nocount on
begin transaction
set @hostid = @temptable
set @temp=@temptable
select @ltcal=lt,@lttype=lttype,@nettrent=nettrent,@renttype=renttype1 from guest where gno = @gno
set @billorder = 1
set @yesitis = 1
declare cur_restco cursor for select company,channel,billposition1 from restco order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
set @cess=0
set @st = 0
	if @company=1
	begin
		declare cur_kitroom cursor for 
		select txndt,amt from (
		select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
		kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
		from kithist1 
		join co_rest_ol1 kit on kithist1.outlet=kit.ocode
		where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and lux='Y'  and split='N'
	end
	else
	begin
		declare cur_kitroom cursor for 
		select txndt,amt from (
		select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt,
		kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
		from kithist1 
		join co_rest_ol1 kit on kithist1.outlet=kit.ocode
		where gno = @gno and pmode=2)  a where company=@company   and channel=@channel and lux='Y' and selfdeal='N' and split='N'
	end
	open cur_kitroom
	fetch next from cur_kitroom into @txndt,@amt
	while @@fetch_status=0
	begin
		set @yesitis = 1
		set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
		exec(@sql)
		if @bp1<>0
		begin
			set @sql = 'update  ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
			+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
			exec(@sql)
			if @@error<>0
			begin
				close cur_kitroom
				deallocate cur_kitroom
				close cur_restco
				deallocate cur_restco
				goto e_error
			end
		end
		fetch next from cur_kitroom into @txndt,@amt
	end
	close cur_kitroom
	deallocate cur_kitroom
	fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
-- why here the diff in company 1 and other is not specified is due  to the billpostion1 field
--1
----------------------------------------------------------
----------------------------------------------------------


declare cur_restco cursor for select company,outlet,billposition1 from phoutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and split = 'N' and lux = 'Y' group by txndt
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)
from phone where gno=@gno and pmode=2 and outlet=@channel and split='N' and lux='Y'
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
exec(@sql) 
if @bp1<>0
begin
set @sql = 'update  ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  + ' where datediff(d,''1/1/80'',txndt)='+str(datediff(d,'1/1/80',@txndt))
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
-------- first  try for travel desk 
--2

declare cur_restco cursor for select company,outlet,billposition1 from toutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and split = 'N' and lux = 'Y' group by finaldate
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1

set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0 , 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
exec(@sql)

if @bp1<>0
begin
set @sql = 'update  ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 300 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  
+ ' where datediff(d,''1/1/90'',txndt)=datediff(d,''1/1/90'',''' + convert(varchar(20),@txndt) + ''')'
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
-----traveldesk ends
--print 'peria'
--if @ltcal='Y'
--begin

set @totrooms = 1 
select @totrooms=count(*) from addroom where gno=@gno
if @totrooms < 1 set @totrooms=1
declare @crno numeric
set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,rno,head,st,cess  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@crno,@head,@st,@cess
set @ctotal = 0
while @@fetch_status=0
begin
	if @ltcal='Y'   -- calculate lt
	begin
	set @sql = 'update ' + @temptable  + ' set taxable=' +convert (varchar(14),@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10) + ' where current of cur_temp'
	exec(@sql)
	set @ltamt = 0
	declare @ltamttemp money
	declare @amttemp money
	declare @amttemp1 money
	declare @g_ltonsc tinyint
	set @ltamttemp=0
	set @amttemp=@rent
	select @ltontariff=ltontariff from ltontariff where datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)
	select  top 1  @g_disc_ltrules=g_disc_ltrules ,@g_ltonsc=g_ltonsc from setting
	--set @amttemp1=@rent
	if @rent<>0
	if @ltontariff='Y'   exec findrent @renttype,@crno,@head,@txndt,0,@amttemp out
	-- set @amttemp=(select case when @head=1 then rent1 else rent2 end  from room where rno=@crno)
	--print @ltontariff + str(@head) + '  ' + convert(varchar(20),@amttemp)
	--goto e_error
	set @amttemp1=case when @rent > @amttemp then @rent else @amttemp end
	set @amttemp = @amttemp1
	if @ltontariff='Y'
		set @amt = @amttemp+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10
	else
		if @g_disc_ltrules=0
		set @amt = @amttemp+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10-@discount
	                else
		set @amt = @amttemp+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10
	set @amt= isnull(@amt,0)
	
	if @g_ltonsc=1 set @amt=@amt+@sc
	if  @ltontariff<>'Y' set @amt = @amt/@totrooms
	select @tno=tno from ltmaster where convert(datetime,@txndt,106)  between sdt and edt
	declare @taxed money
	declare @nontaxable money
	set @taxed=0
	set @nontaxable=0
	set @taxsystem=1
	if @taxsystem=0 
	begin
		set @sql = 'declare cur_lttable cursor for select lrate,samt,eamt,famt from lttable where tno = ' +  ltrim(str(@tno))
		exec(@sql)
		open cur_lttable
		fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
		set @amttemp = @amt
		while @@fetch_status=0 and @amt > 0
			begin
			      if @eamt > @amt  set @amt2 = @amt   else   set @amt2 = @eamt
			--if @amt2<>499 begin  print convert(varchar(20),@amt2) goto e_error end
			      set @ltamt = round(@ltamt + ((@amt2 + @famt) * @lrate / 100),2)
			   if @lrate > 0 set @taxed=@taxed + @amt2  else set @nontaxable=@nontaxable+@amt2

			      set @amt = @amt - @amt2
			      if @eamt > @amttemp set @amt2=@amttemp else set @amt2 = @eamt
			if @ltontariff='Y'
			      set @ltamttemp =  round(@ltamttemp+((@amt2+@famt)*@lrate/100),2)
			else
			      set @ltamttemp =  @ltamttemp+round(((@amt2+@famt)*@lrate/100),2)
			      set @amttemp=@amttemp-@amt2
			      fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
			end
			close cur_lttable
			deallocate cur_lttable
	end
	else
	begin
		if isnull(@amt,0) <> 0
		begin
			set @sql = 'declare ct1 cursor for select lrate from lttable where tno=' + str(@tno) + ' and ' + convert(varchar(20),isnull(@amt,0)) + ' between samt and eamt'
			exec(@sql)
			open ct1
			fetch next from ct1 into @lrate
			if @@fetch_status=0
			begin
			            set @ltamttemp =  round(@amt*@lrate/100,2)
			end
			close ct1
			deallocate ct1
		end
	end
	/*
	set @amt2 = (@ltamttemp + @amttemp1)*100
	set @amttemp = floor(        ((@ltamttemp+@amttemp1)*100) /10  )*10
	if @amt2 > @amttemp set @ltamt = @ltamt  -( ( @amt2-@amttemp)/100)
	*/
	--raiserror(@sql ,16,-1) return
	set @ltamt = @ltamttemp
	if @ltamt < 0 set @ltamt=0
	--set @sql = 'update ' + @temptable + ' set lt =' + convert(varchar(14), @ltamt) + ',taxed=' + convert(varchar(20),@taxed) + ',nontaxable=' + convert(varchar(20),@nontaxable) +    '  where current of cur_temp'
	--set @sql = 'update ' + @temptable + ' set lt =' + convert(varchar(14), @ltamt) + ',taxed=' + convert(varchar(20),@taxed) + ',nontaxable=' + convert(varchar(20),@nontaxable) +    '  where current of cur_temp'
	set @sql= 'update ' + @temptable + ' set lt = ' + convert(varchar(20),@ltamt)  + '  where current of cur_temp'
	exec(@sql)
	end --- end of @ltcal='Y'
	else
	begin ----beginning of @ltcal<>'Y'
	set @sql = 'update ' + @temptable + ' set lt = 0  where current of cur_temp'
	exec(@sql)
	end ---- end of @ltcal <>'Y'
	if @ltamt<>0
		begin
			set @temptot =  @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@ltamt-@discount
			set @temptot1 = @temptot*100
			set @temptot2 = floor(@temptot1/10)*10
			if @temptot1-@temptot2 = 1 set @ltamt=@ltamt-.01
			set @sql= 'update ' + @temptable + ' set lt = ' + convert(varchar(20),@ltamt)  +'  where current of cur_temp'
			exec(@sql)
		end
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---- 27 apr 2011 (calculation of service tax)
	if @rent<>0
	begin
		select @allinclusive=allinclusive from guest where gno=@gno
		select top 1  @stontariff=ontariff from sttable where (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		
		if @stontariff='YES'
			begin
				declare @tariff money
				--set @tariff=    (select case when @head=1 then rent1 else rent2 end  from room where rno=@crno)
				exec findrent @renttype,@crno,@head,@txndt,0,@tariff out
				set @amttemp=case when @rent > @tariff then @rent else @tariff end
			end
		else
			set @amttemp=@rent-@discount



		select @strate=rate,@stdiscount=discount,@stlessamt=lamt,@cessrate=cess  from sttable where (@amttemp between samt and eamt) and  (datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt))
		
		set @amttemp = @amttemp - @stlessamt
		set @st=0
		set @cess=0
		if @amttemp>0 
			begin
			if @strate<>0
				set @st = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@strate/100)) as decimal(12,2))
			else
				set @st=0
			if @cessrate > 0 
				set @cess = cast( ((@amttemp - (@amttemp * @stdiscount/100) ) * (@cessrate/100)) as decimal(12,2))
			end
/*		
		if @st > 0 
			if @cessrate > 0 
				set @cess = @st * @cessrate / 100
*/	
		set @sql= 'update ' + @temptable + ' set st = ' + convert(varchar(20),@st)  +'  where current of cur_temp'
		exec(@sql)
		
		set @sql= 'update ' + @temptable + ' set cess = ' + convert(varchar(20),@cess)  +'  where current of cur_temp'
		exec(@sql)
		/*
		if @allinclusive = 'Y'
		begin
			set @sql= 'update ' + @temptable + ' set RENT =RENT- ' + convert(varchar(20),@cess+@ST)  +'  where current of cur_temp'
			exec(@sql)
		end
		*/
	end
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
---------@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
	set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st where current of cur_temp'
	exec (@sql)
	set @ctotal = @ctotal + @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@ltamt-@discount+@st
	fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@crno,@head,@st,@cess
end
close cur_temp
deallocate cur_temp
--end  -- end of @ltcal='Y'
-------------------------------------------------------------------
-------------------------------------------------------------------
-------------------------------------------------------------------
--3

-- here starts the no lt calculation 
declare cur_restco cursor for select company,channel,billposition1 from restco order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--if @company=1
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'N' and lux <> 'Y' group by txndt
--else
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'N' and lux <> 'Y' and selfdeal = 'N' group by txndt
if @company=1
begin
declare cur_kitroom cursor for 
select txndt,amt from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt +servicetax+cess,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno and pmode=2)  a where company=@company and  channel=@channel and  lux<>'Y'  and split='N'
end
else
begin
declare cur_kitroom cursor for 
select txndt,amt from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno and pmode=2)   a where company=@company and  channel=@channel and  lux<>'Y'  and  selfdeal='N' and split='N'
end
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
	--set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'

exec(@sql)
if @bp1<>0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco

declare cur_restco cursor for select company,outlet,billposition1 from phoutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
---declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and split = 'N' and lux <> 'Y' group by txndt
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)
from phone where gno=@gno  and pmode=2 and outlet=@channel and split='N' and lux<>'Y'
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,0, 0,0)'
exec(@sql)
if @bp1 <> 0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco

-------- first  try for travel desk 
declare cur_restco cursor for select company,outlet,billposition1 from toutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and split = 'N' and lux <> 'Y' group by finaldate
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
exec(@sql)
if @bp1<>0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 300 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
-----traveldesk ends

set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,st,cess  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
set @ctotal = 0
while @@fetch_status=0
begin
set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st+cess where current of cur_temp'
set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0) where current of cur_temp'
exec (@sql)
set @ctotal = @ctotal + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)+isnull(@sc,0)-isnull(@discount,0)+isnull(@st,0) + isnull(@cess,0)
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
end
close cur_temp
deallocate cur_temp
if @yesitis=1
 begin
--if @gno = 27986 begin print 'periya solkettan'  goto e_error end
     set @sql = 'insert into ' +  @temptable  + ' ( total,txndt)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31 jan 9999'' )'
     exec (@sql)
     set @sql = 'update ' + @temptable + ' set billorder=1,section=''A'''
      exec (@sql)
 end
--here ends no lt calculation

--------------------------------------------------------------------
set @temptabletemp = @temptable
set @temptable = @temptable + 't'
set @sql = 'if exists (select name,xtype from sysobjects where name = ''' + @temptable +''' and xtype = ''u'') drop table ' + @temptable
exec (@sql)
set @sql = 'select * into ' + @temptable + '  from  dummybill'
exec (@sql)
---------------------------------------------------here starts the sepaerate bill calculation
set @ltamt=0
set @yesitis = 0
declare cur_restco cursor for select company,channel,billposition1 from restco order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--if @company=1
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'Y'  group by txndt
--else
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'Y'  and selfdeal = 'N'  group by txndt
if @company=1
begin
declare cur_kitroom cursor for 
select txndt,amt from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and split='Y'
end
else
begin
declare cur_kitroom cursor for 
select txndt,amt from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and selfdeal='N' and split='Y'
end
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1

set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
exec(@sql)
if @bp1 <> 0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
declare cur_restco cursor for select company,outlet,billposition1 from phoutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and split= 'Y' group by txndt
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)
from phone where gno=@gno and pmode=2  and outlet=@channel and split='Y'
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
exec(@sql)
if @bp1 <> 0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end

close cur_restco
deallocate cur_restco
-------- first  try for travel desk 

declare cur_restco cursor for select company,outlet,billposition1 from toutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and split = 'Y'   group by finaldate
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
exec(@sql)

if @bp1<>0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 300 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco

-----traveldesk ends
--print 'peria'
set @sql = 'declare cur_temp cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),st,cess  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
set @ctotal = 0
while @@fetch_status=0
begin
set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0) where current of cur_temp'
exec (@sql)
set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
end
close cur_temp
deallocate cur_temp
--******************************************************
--******************************************************
--******************************************************
--******************************************************
--******************************************************
       if @yesitis=1 
		 begin
		  set @sql = 'insert into ' +  @temptable  + ' ( total,txndt)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31 jan 9999'' )'
		  exec (@sql)
		  set @sql = 'update ' + @temptable + ' set billorder=1,section=''B'''
		  exec (@sql)
		 end
--******************************************************
--******************************************************
--******************************************************
--******************************************************
--******************************************************
--******************************************************
--print 'periyakaruppa 3445'


set @sql = 'declare t_cur cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),billorder,section,isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0),isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0),st,cess  from ' + @temptable
exec (@sql)
open t_cur
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
while @@fetch_status=0
begin
set @st = 0
set @cess = 0
set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' +  convert(varchar(20),@st)  + ',' +  convert(varchar(20),@cess) + ')'
--if datediff(d,'1 jan 2100',@txndt) > 0 
--begin
--print  convert(varchar(20),@st)  + ',' +  convert(varchar(20),@cess) 
--goto e_error
--end
exec(@sql)

if @@error <> 0
	begin
	close t_cur
	deallocate t_cur
	goto e_error
end
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
end
close t_cur
deallocate t_cur

--here ends seperate bills calculations
-- no mistake upto here
--print 'ramanathan' + @temptable
--------------------------------------------------------------------
set @sql ='truncate table ' + @temptable ------------------------------------------------------- other company bills starts here
exec(@sql)
declare cur_co cursor for select company from company where company > 1 order by company
open cur_co
fetch next from cur_co into @company
while @@fetch_status=0
begin
   set @yesitis = 0
    declare cur_restco cursor for select company,channel,billposition2 from restco where company = @company  and billposition2<>0 order by billposition2
    open cur_restco
    fetch next from cur_restco into @company,@channel,@bp1 
   while @@fetch_status=0
      begin
--      declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel  and selfdeal='Y' and split='N'  group by txndt
declare cur_kitroom cursor for 
select txndt,amt from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and split='N' and selfdeal='Y'
     open cur_kitroom
      fetch next from cur_kitroom into @txndt,@amt
       while @@fetch_status=0
            begin
                 set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
                --print @sql
                exec(@sql)
                       if @bp1<>0 
                       begin
                              set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
                             exec(@sql)
                                 if @@error<>0
                                 begin 
                                     close cur_kitroom 
                                    deallocate cur_kitroom
                                  close cur_restco 
                                  deallocate cur_restco
                                  goto e_error
                                 end
                       end
                     fetch next from cur_kitroom into @txndt,@amt
            end
       close cur_kitroom 
      deallocate cur_kitroom
      fetch next from cur_restco into @company,@channel,@bp1
   end
   close cur_restco
   deallocate cur_restco
   declare cur_restco cursor for select company,outlet,billposition2 from phoutlet where company=@company and billposition2<>0 order by billposition2
   open cur_restco
   fetch next from cur_restco into @company,@channel,@bp1 
   while @@fetch_status=0
   begin
--     declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and selfdeal= 'Y'  and split='N'  group by txndt
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)
from phone where gno=@gno  and pmode=2 and outlet=@channel and split='N' and selfdeal='Y'
     open cur_kitroom
    fetch next from cur_kitroom into @txndt,@amt  
   while @@fetch_status=0
      begin
         set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
          exec(@sql)
          if @bp1<>0
          begin
                set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
               exec(@sql)
              if @@error<>0
                   begin
                       close cur_kitroom
                       deallocate cur_kitroom
                        close cur_restco
                      deallocate cur_restco
                     goto e_error
                   end
          end
         fetch next from cur_kitroom into @txndt,@amt
      end
     close cur_kitroom 
      deallocate cur_kitroom
     fetch next from cur_restco into @company,@channel,@bp1
   end
close cur_restco
deallocate cur_restco
/* here starts travel desk */
declare cur_restco cursor for select company,outlet,billposition2 from toutlet where company=@company and billposition2<>0 order by billposition2
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
declare cur_kitroom cursor for select finaldate,'amt' = sum(amt) from tt_chalan_ol  where gno = @gno and  outlet = @channel and selfdeal= 'Y'  and split='N'  group by finaldate
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
exec(@sql)
if @bp1<>0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 300 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
/*here ends travel desk*/


set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,st,cess  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
set @ctotal = 0
while @@fetch_status=0
begin
set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st+cess where current of cur_temp'
exec (@sql)
set @ctotal = @ctotal + @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@lt+@sc-@discount+@st+@cess
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
end
close cur_temp
deallocate cur_temp
      if @yesitis=1
 begin
 set @sql = 'insert into ' +  @temptable  + ' ( total,txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31jan 9999'',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'' '' ,0,0,0,0,0,0,0,0,0,0,0,0)'
 exec (@sql)
 if @@error <> 0 goto e_error
 ---- updating original tempfile
 set @sql = 'update ' + @temptable + ' set billorder=' + str(@company) + ',section=''' + 'A' + ''''
 exec (@sql)
 end
set @sql = 'declare t_cur cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess  from ' + @temptable
exec (@sql)
open t_cur
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
if @@error <> 0 goto e_error
while @@fetch_status=0
begin
--print 'periya karuppa 111'
set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' + str(@st) + ',' + str(@cess) + ')'
exec(@sql)
if @@error <> 0
begin
close t_cur
deallocate t_cur
goto e_error
end
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
end
close t_cur
deallocate t_cur
set @sql ='truncate table ' + @temptable 
exec(@sql)
fetch next from cur_co into @company
end
close cur_co
deallocate cur_co
----------------------------------------------------------other company bills end here
----------------------complementary bills starts here (first vouching items next no voucher items
---- here starts the complementary bills processing
set @ltamt=0
set @yesitis = 0
declare cur_restco cursor for select company,channel,billposition1 from restco order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--if @company=1
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'C'  group by txndt
--else
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'C'  and selfdeal = 'N'  group by txndt
if @company=1
begin
	declare cur_kitroom cursor for 
	select txndt,amt from (
	select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
	kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
	from kithist1 
	join co_rest_ol1 kit on kithist1.outlet=kit.ocode
	where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and split='C'
end
else
begin
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'N' and lux = 'Y'  and selfdeal='N'  group by txndt
	declare cur_kitroom cursor for 
	select txndt,amt from (
	select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
	kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
	from kithist1 
	join co_rest_ol1 kit on kithist1.outlet=kit.ocode
	where gno = @gno and pmode=2)  a where company=@company and channel=@channel and selfdeal='N' and split='C'
end
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
exec(@sql)
if @bp1 <> 0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
declare cur_restco cursor for select company,outlet,billposition1 from phoutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and split= 'C' group by txndt
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)
from phone where gno=@gno  and pmode=2 and outlet=@channel and split='C'
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0,  0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
exec(@sql)
if @bp1 <> 0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom

deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end

close cur_restco
deallocate cur_restco
--print 'peria'
set @sql = 'declare cur_temp cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),st,cess  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
set @ctotal = 0
while @@fetch_status=0
begin
set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st+cess where current of cur_temp'
set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0) where current of cur_temp'
exec (@sql)
--set @ctotal = @ctotal + @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@lt+@sc-@discount+@st+@cess
set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
end
close cur_temp
deallocate cur_temp
if @yesitis=1 
 begin
  set @sql = 'insert into ' +  @temptable  + '  ( total,txndt)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31 jan 9999'' )'
  exec (@sql)
  set @sql = 'update ' + @temptable + ' set billorder=1,section=''C'''
  exec (@sql)
 end
--print 'periyakaruppa 3445'
set @sql = 'declare t_cur cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),billorder,section,isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0),isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0) ,isnull(st,0),isnull(cess,0) from ' + @temptable 
exec (@sql)
open t_cur
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
while @@fetch_status=0
begin
set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' + str(@st) + ',' + str(@cess)  + ')'
exec(@sql)
if @@error <> 0
begin
close t_cur
deallocate t_cur
goto e_error
end
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
end
close t_cur
deallocate t_cur
---- here ends complementary bills processing for company 1  - to be vouched item

set @sql ='truncate table ' + @temptable ------------------------------------------------------- other company  complementary bills starts here
exec(@sql)
declare cur_co cursor for select company from company where company > 1 order by company
open cur_co
fetch next from cur_co into @company
while @@fetch_status=0
begin
set @yesitis = 0
    declare cur_restco cursor for select company,channel,billposition2 from restco where company = @company  and billposition2<>0 order by billposition2
    open cur_restco
    fetch next from cur_restco into @company,@channel,@bp1 
   while @@fetch_status=0
   begin
--   declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel  and selfdeal='Y' and split='C'  group by txndt
declare cur_kitroom cursor for 
select txndt,amt from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno and pmode=2)  a where company=@company and channel=@channel and selfdeal='Y' and split='C'
   open cur_kitroom
   fetch next from cur_kitroom into @txndt,@amt
   while @@fetch_status=0
   begin
set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
   exec(@sql)
   if @bp1<>0 
   begin
   set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  + 'where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))
   exec(@sql)
   if @@error<>0
   begin 
   close cur_kitroom 
   deallocate cur_kitroom
   close cur_restco 
   deallocate cur_restco
   goto e_error
   end
   end
   fetch next from cur_kitroom into @txndt,@amt
   end
   close cur_kitroom 
   deallocate cur_kitroom
   fetch next from cur_restco into @company,@channel,@bp1

   end
   close cur_restco
   deallocate cur_restco
declare cur_restco cursor for select company,outlet,billposition2 from phoutlet where company=@company and billposition2<>0 order by billposition2
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and selfdeal= 'Y'  and split='C'  group by txndt
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)
from phone where gno=@gno  and pmode=2 and outlet=@channel and split='C' and selfdeal='Y'
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
exec(@sql)
if @bp1<>0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  + 'where datediff(d,''1/1/80'',txndt) = ' + str(datediff(d,'1/1/80',@txndt))
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total ,st,cess from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
set @ctotal = 0
while @@fetch_status=0
begin

set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st+cess where current of cur_temp'
--set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0) where current of cur_temp'
--set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)
set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0) where current of cur_temp'

exec (@sql)
set @ctotal = @ctotal + @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@lt+@sc-@discount+@st+@cess
set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
end
close cur_temp
deallocate cur_temp
if @yesitis=1
 begin
 set @sql = 'insert into ' +  @temptable  + ' ( total,txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31jan 9999'',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,'' '' ,0,0,0,0,0,0,0,0,0,0,0,0)'
 exec (@sql)
 if @@error <> 0 goto e_error
 ---- updating original tempfile
 set @sql = 'update ' + @temptable + ' set billorder=' + str(@company) + ',section=''' + 'C' + ''''
 exec (@sql)
 end
set @sql = 'declare t_cur cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),billorder,section,isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0),isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0),isnull(st,0),isnull(cess,0)  from ' + @temptable 
exec (@sql)
open t_cur
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
while @@fetch_status=0
begin
set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' + str(@st)  + ',' + str(@cess) + ')'
exec(@sql)
if @@error <> 0
begin
close t_cur
deallocate t_cur
goto e_error
end
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
end
close t_cur
deallocate t_cur
set @sql ='truncate table ' + @temptable 
exec(@sql)
fetch next from cur_co into @company
end
close cur_co
deallocate cur_co
----------------------------------------------------------other company complementary  bills end here (with vouching)
set @ltamt=0
set @yesitis = 0
declare cur_restco cursor for select company,channel,billposition1 from restco order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--if @company=1
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'F'  group by txndt
--else
--declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel and split = 'F'  and selfdeal = 'N'  group by txndt
if @company=1
begin
declare cur_kitroom cursor for 
select txndt,amt from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno and pmode=2)  a where company=@company  and channel=@channel and split='F'
end
else
begin
declare cur_kitroom cursor for 
select txndt,amt from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno and pmode=2)  a where company=@company and channel=@channel and selfdeal='N' and split='F'
end
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0)'
exec(@sql)
if @bp1 <> 0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
declare cur_restco cursor for select company,outlet,billposition1 from phoutlet where billposition1 <> 0 order by billposition1
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and split= 'F' group by txndt
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)
from phone where gno=@gno  and pmode=2 and outlet=@channel and split='F'
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0 ,0,0)'
exec(@sql)
if @bp1 <> 0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt) 
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
--print 'peria'
set @sql = 'declare cur_temp cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),st,cess  from ' + @temptable
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
set @ctotal = 0
while @@fetch_status=0
begin
set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st+cess where current of cur_temp'
set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0) where current of cur_temp'
exec (@sql)
--set @ctotal = @ctotal + @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@lt+@sc-@discount+@st+@cess
set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
end
close cur_temp
deallocate cur_temp
       if @yesitis=1 
 begin
  set @sql = 'insert into ' +  @temptable  + ' ( total,txndt)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31 jan 9999'' )'
  exec (@sql)
  set @sql = 'update ' + @temptable + ' set billorder=1,section=''F'''
  exec (@sql)
 end
--print 'periyakaruppa 3445'
set @sql = 'declare t_cur cursor for select txndt,isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),billorder,section,isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0),isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0) ,st,cess from ' + @temptable 
exec (@sql)
open t_cur
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
while @@fetch_status=0
begin
set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ''' + convert(varchar(16),@txndt)+''',' + convert(varchar(14),@rent)
set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) +  ',' + str(@st) + ',' + str(@cess) + ')'
exec(@sql)
if @@error <> 0
begin
close t_cur
deallocate t_cur
goto e_error
end
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
end
close t_cur
deallocate t_cur
---- here ends complementary bills processing for company 1  - no  voucher item
set @sql ='truncate table ' + @temptable ------------------------------------------------------- other company  complementary bills starts here
exec(@sql)
declare cur_co cursor for select company from company where company > 1 order by company
open cur_co
fetch next from cur_co into @company
while @@fetch_status=0
begin
set @yesitis = 0
    declare cur_restco cursor for select company,channel,billposition2 from restco where company = @company  and billposition2<>0 order by billposition2
    open cur_restco
    fetch next from cur_restco into @company,@channel,@bp1 
   while @@fetch_status=0
   begin
--   declare cur_kitroom cursor  for select txndt,'amt' = sum(amt) from kit_room where gno = @gno and company = @company   and channel = @channel  and selfdeal='Y' and split='F'  group by txndt
declare cur_kitroom cursor for 
select txndt,amt from (
select txndt,amt=(select sum((qty*rate)+tax-discount) from kithist where bno=kithist1.bno)+roamt+scamt+servicetax+cess,
kit.cocode company ,kit.rcode channel,kit.selfdeal selfdeal,kit.lux lux,split
from kithist1 
join co_rest_ol1 kit on kithist1.outlet=kit.ocode
where gno = @gno and pmode=2)  a where company=@company and channel=@channel and selfdeal='Y' and split='F'
   open cur_kitroom
   fetch next from cur_kitroom into @txndt,@amt
   while @@fetch_status=0
   begin
set @yesitis = 1
	set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0 ,0,0,0,0,0 ,0,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0, 0,0)'
   exec(@sql)
   if @bp1<>0 
   begin
   set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 100 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
   exec(@sql)
   if @@error<>0
   begin 
   close cur_kitroom 
   deallocate cur_kitroom
   close cur_restco 
   deallocate cur_restco
   goto e_error
   end
   end
   fetch next from cur_kitroom into @txndt,@amt
   end
   close cur_kitroom 
   deallocate cur_kitroom
   fetch next from cur_restco into @company,@channel,@bp1
   end
   close cur_restco
   deallocate cur_restco
declare cur_restco cursor for select company,outlet,billposition2 from phoutlet where company=@company and billposition2<>0 order by billposition2
open cur_restco
fetch next from cur_restco into @company,@channel,@bp1
while @@fetch_status=0
begin
--declare cur_kitroom cursor for select txndt,'amt' = sum(amt) from ph_room where gno = @gno and  outlet = @channel and selfdeal= 'Y'  and split='F'  group by txndt
declare cur_kitroom cursor for 
select txndt,amt=(select sum(amount) from phhist where cno=phone.cno)
from phone where gno=@gno and pmode=2  and outlet=@channel and split='F' and selfdeal='Y'
open cur_kitroom
fetch next from cur_kitroom into @txndt,@amt
while @@fetch_status=0
begin
set @yesitis = 1
set @sql = 'if not exists (select * from ' +  @temptable + ' where datediff(d,''1/1/80'',txndt)=' + str(datediff(d,'1/1/80',@txndt))  + ')   insert into ' + @temptable  + '(txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess) values (''' + convert(varchar(20), @txndt,106) +  ''',0,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0 ,0,0)'
exec(@sql)
if @bp1<>0
begin
set @sql = 'update ' + @temptable  + ' set outlet' + ltrim(str(@bp1)) + ' = 200 + ' + str(@channel) + ',item' + ltrim(str(@bp1)) + ' = item' + ltrim(str(@bp1)) + ' + '  + convert(varchar(14),@amt)  
+ ' where datediff(d,''1/1/90'',txndt ) = datediff(d,''1/1/90'','''+ convert(varchar(20),@txndt) + ''')' 
exec(@sql)
if @@error<>0
begin
close cur_kitroom
deallocate cur_kitroom
close cur_restco
deallocate cur_restco
goto e_error
end
end
fetch next from cur_kitroom into @txndt,@amt
end
close cur_kitroom
deallocate cur_kitroom
fetch next from cur_restco into @company,@channel,@bp1
end
close cur_restco
deallocate cur_restco
set @sql = 'declare cur_temp cursor for select txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,st,cess  from ' + @temptable
--print @sql
exec (@sql)
open cur_temp
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
set @ctotal = 0
while @@fetch_status=0
begin
set @sql = 'update ' + @temptable + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+sc-discount+st+cess where current of cur_temp'
set @sql = 'update ' + @temptable + ' set total=isnull(rent,0)+isnull(item1,0)+isnull(item2,0) +isnull(item3,0)+isnull(item4,0) +isnull(item5,0)+isnull(item6,0)+isnull(item7,0)+isnull(item8,0)+isnull(item9,0)+isnull(item10,0)+isnull(lt,0)+isnull(sc,0)-isnull(discount,0)+isnull(st,0)+isnull(cess,0) where current of cur_temp'
exec (@sql)
set @ctotal = @ctotal + @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@lt+@sc-@discount+@st+@cess
set @ctotal = isnull(@ctotal,0) + isnull(@rent,0)+isnull(@item1,0)+isnull(@item2,0)+isnull(@item3,0)+isnull(@item4,0)+isnull(@item5,0)+isnull(@item6,0)+isnull(@item7,0)+isnull(@item8,0)+isnull(@item9,0)+isnull(@item10,0)+isnull(@lt,0)-isnull(@discount,0)+isnull(@st,0)+isnull(@cess,0)
fetch next from cur_temp into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@st,@cess
end
close cur_temp
deallocate cur_temp

 if @yesitis=1
 begin
 set @sql = 'insert into ' +  @temptable  + ' ( total,txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ' + convert(varchar(14),@ctotal ) + ', '' 31 jan 9999'',0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, '' '' , 0,0,0,0,0, 0,0,0,0,0, 0,0)'
 exec (@sql)
 if @@error <> 0 goto e_error
 ---- updating original tempfile
 set @sql = 'update ' + @temptable + ' set billorder=' + str(@company) + ',section=''' + 'F' + ''''
 exec (@sql)
 end

set @sql = 'declare t_cur cursor for select txndt,
isnull(rent,0),isnull(discount,0),isnull(item1,0),isnull(item2,0),isnull(item3,0),isnull(item4,0),isnull(item5,0),isnull(item6,0),isnull(item7,0),isnull(item8,0),isnull(item9,0),isnull(item10,0),isnull(lt,0),isnull(sc,0),isnull(total,0),
billorder,section,
isnull(outlet1,0),isnull(outlet2,0),isnull(outlet3,0), isnull(outlet4,0),isnull(outlet5,0),isnull(outlet6,0),isnull(outlet7,0),isnull(outlet8,0),isnull(outlet9,0),isnull(outlet10,0),st,cess  from ' + @temptable
exec (@sql)
open t_cur
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
if @@error <> 0 goto e_error
while @@fetch_status=0
begin
--print 'periya karuppa 111'
set @sql = 'insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ''' + convert(varchar(16),@txndt,106)+''',' + convert(varchar(14),@rent)
set @sql = @sql + ' ,' + convert(varchar(15),@discount) + ',' + convert(varchar(15),@item1)+ ',' + convert(varchar(15),@item2)+ ',' + convert(varchar(15),@item3)+ ',' + convert(varchar(15),@item4)+ ',' + convert(varchar(15),@item5)
set @sql = @sql +  ',' + convert(varchar(15),@item6)+ ',' + convert(varchar(15),@item7)+ ',' + convert(varchar(15),@item8)+ ',' + convert(varchar(15),@item9)+ ','+convert(varchar(15),@item10)+ ',' + convert(varchar(15),@lt)+ ',' + convert(varchar(15),@sc)+ ',' + convert(varchar(15),@total) + ',' + str(@billorder) + ',''' + @section + ''','
set @sql = @sql + str(@outlet1) + ','+ str(@outlet2)+','+ str(@outlet3)+','+ str(@outlet4)+','+ str(@outlet5)+','+ str(@outlet6)+','+ str(@outlet7)+','+ str(@outlet8)+','+ str(@outlet9)+','+ str(@outlet10) + ',' + str(@st) + ',' + str(@cess) + ')'
exec(@sql)
if @@error <> 0
begin
close t_cur
deallocate t_cur
goto e_error
end
fetch next from t_cur into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@outlet1,@outlet2,@outlet3,@outlet4,@outlet5,@outlet6,@outlet7,@outlet8,@outlet9,@outlet10,@st,@cess
end
close t_cur
deallocate t_cur
set @sql ='truncate table ' + @temptable 
exec(@sql)
fetch next from cur_co into @company
end
close cur_co
deallocate cur_co
----------------------------------------------------------other company complementary  bills end here   no voucher items
top1:
set @sql = 'drop table ' + @temptable
exec(@sql)
--- new try nov 24 2005
set @sql = 'declare sp_cbcursor cursor  for select company,split from roomdues where gno=' + str(@gno) 
exec (@sql)
if @@error <> 0 goto e_error
open sp_cbcursor
if @@error <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
if @@error <> 0 goto e_error
while @@fetch_status=0 
begin
set @sql = 'if not exists(select * from ' + @temptabletemp + ' where billorder = ' + str(@billorder) + ' and section = ''' + case when @section = 'Y' then 'B' else 'A'  end  +''')'
set @sql = @sql + '  insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ''' + convert(varchar(16),'31 jan 9999',106)+''',0' 
set @sql = @sql + ' ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,' + str(@billorder) + ',''' + case when @section='Y' then 'B' else 'A' end  + ''',0,0,0,0,0,0,0,0,0,0,0,0)'
exec(@sql)
if @@error <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
if @@error <> 0 goto e_error
end

close sp_cbcursor
if @@error <> 0 goto e_error
deallocate sp_cbcursor
if @@error <> 0 goto e_error
---- for advance unaccounted 25 nov 2005

set @sql = 'declare sp_cbcursor cursor  for select company,split from advance where gno=' + str(@gno) 
exec (@sql)
if @@error <> 0 goto e_error
open sp_cbcursor
if @@error <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
if @@error <> 0 goto e_error
while @@fetch_status=0 
begin
set @sql = 'if not exists(select * from ' + @temptabletemp + ' where billorder = ' + str(@billorder) + ' and section = ''' + case when @section = 'Y' then 'B' else 'A'  end  +''')'
set @sql = @sql + '  insert into ' +  @temptabletemp + ' ( txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,outlet1,outlet2,outlet3,outlet4,outlet5,outlet6,outlet7,outlet8,outlet9,outlet10,st,cess)  values( ''' + convert(varchar(16),'31 jan 9999',106)+''',0' 
set @sql = @sql + ' ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,' + str(@billorder) + ',''' + case when @section='Y' then 'B' else 'A' end  + ''',0,0,0,0,0,0,0,0,0,0,0,0)'
exec(@sql)
if @@error <> 0 goto e_error
fetch next from sp_cbcursor into @billorder,@section
if @@error <> 0 goto e_error
end

close sp_cbcursor
if @@error <> 0 goto e_error
deallocate sp_cbcursor
if @@error <> 0 goto e_error
-- this is included for classic  (to calculate the service charges on all the services along wiht the rent 23-nov-2007 11:46 am

if @lttype=2  -- new insertion on 25-11-2011 for Almondz
begin
set @ctotal=0
declare @otherchgs money
set @sql = 'declare cnr cursor for select  txndt,rent,discount,item1,item2,item3,item4,item5,item6,item7,item8,item9,item10,lt,sc,total,billorder,section,st,cess   from ' + @temptabletemp + ' where rent <>0'
exec(@sql)
open cnr
fetch next from cnr  into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@st,@cess 
while @@fetch_status=0
begin
	set @sql = 'update ' + @temptabletemp + ' set otherchgs=' + convert(varchar(20),@nettrent) + '- (rent+lt+st+cess)   where txndt=''' + convert(varchar(20),@txndt) + ''' and billorder=' +  str(@billorder) + ' and section=''' + @section + ''''
	exec(@sql)
	set @sql = 'update ' + @temptabletemp + ' set total =total+otherchgs  where txndt=''' + convert(varchar(20),@txndt) + ''' and billorder=' +  str(@billorder) + ' and section=''' + @section + ''''
	exec(@sql)

	set @sql='declare cnr1 cursor for select  otherchgs from ' + @temptabletemp +  '  where  billorder=' +  str(@billorder) + ' and section=''' + @section + ''''
	exec(@sql)
	open cnr1
	fetch next from cnr1 into @otherchgs	
	close cnr1
	deallocate cnr1

	set @ctotal = @ctotal + @rent+@item1+@item2+@item3+@item4+@item5+@item6+@item7+@item8+@item9+@item10+@lt+@sc-@discount+@st+@cess+ @otherchgs
	fetch next from cnr into @txndt,@rent,@discount,@item1,@item2,@item3,@item4,@item5,@item6,@item7,@item8,@item9,@item10,@lt,@sc,@total,@billorder,@section,@st,@cess
end
close cnr
deallocate cnr

set @sql = 'update ' +  @temptabletemp  + ' set  total=' + convert(varchar(20),@ctotal) + '  where  datediff(d,''31 jan 9999'',txndt) = 0 and  billorder=' +  str(@billorder) + ' and section=''' + @section + ''''
exec(@sql)

end  -- end of new insertion

declare @scforall tinyint
declare @scrate money
declare @scratestr varchar(50)
set @scrate=0
declare @g_sc_afterdisc int
declare @g_sc_ltrules int
select @scforall=scforall,@g_sc_afterdisc=g_sc_afterdisc,@G_sc_Ltrules=g_sc_ltrules from setting
select @scrate=sc from guest where gno=@gno
set @scratestr = convert(varchar(20),@scrate)
if @scforall=1   -- these codes will work only when the scforall is set to 1 in the setting table   (date of insertion 07 december 2007) for hotel classic
	if @scrate<>0
		begin
			if @g_sc_ltrules=0
				if @g_sc_afterdisc=0
					set @sql = 'update ' + @temptabletemp + ' set sc=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10) * ' +  @scratestr   + '/100 '
				else
					set @sql = 'update ' + @temptabletemp + ' set sc=(rent+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-discount) * ' +  @scratestr  + '/100 '
			else
				begin
					set @amttemp=(select case when @head=1 then rent1 else rent2 end  from room where rno=@crno)
					if @g_sc_afterdisc=0
						set @sql = 'update ' + @temptabletemp + ' set sc=(' + str(@amttemp) + '+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10) * ' + @scratestr   + '/100 '
					else
						set @sql = 'update ' + @temptabletemp + ' set sc=(' + str(@amttemp) + '+item1+item2+item3+item4+item5+item6+item7+item8+item9+item10-discount) * ' +  @scratestr  + '/100 '
	
				end

			exec(@sql)

			set @sql = 'update ' + @temptabletemp + ' set total=rent+item1+item2 +item3+item4 +item5+item6+item7+item8+item9+item10+lt+isnull(sc,0)-discount+st+cess where sc <>  0 '
			exec(@sql)

			set @sql = 'update ' + @temptabletemp + '   set total=(select sum(total) from ' + @temptabletemp + ' b  where datediff(d,b.txndt,''31 jan  9999'') <> 0 and b.billorder=' + @temptabletemp + '.billorder and b.section=' + @temptabletemp + '.section ) where datediff(d,txndt,''31 jan 9999'') = 0'
			exec (@sql)
		end
set @sql = 'update ' + @temptabletemp + ' set st=isnull(st,0),cess=isnull(cess,0) '
exec(@sql)


----------------------------------------------------------
----------------------------------------------------------








commit transaction
return
e_error:
rollback transaction
raiserror('bill creation error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sp_gcb]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sp_gcb    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[sp_gcb] @temptabletemp varchar(50) , @temptable varchar(50)
/* with encryption */
 AS
DECLARE @SQL VARCHAR(5000)
DECLARE @COMPANY INT
DECLARE @CHANNEL INT
DECLARE @BP1 INT
DECLARE @TXNDT DATETIME
DECLARE @AMT MONEY
DECLARE @RENT MONEY
DECLARE @DISCOUNT MONEY
DECLARE @ITEM1 MONEY
DECLARE @ITEM2 MONEY
DECLARE @ITEM3 MONEY
DECLARE @ITEM4 MONEY
DECLARE @ITEM5 MONEY
DECLARE @ITEM6 MONEY
DECLARE @ITEM7 MONEY
DECLARE @ITEM8 MONEY
DECLARE @ITEM9 MONEY
DECLARE @ITEM10 MONEY
DECLARE @LT MONEY
DECLARE @SC MONEY
DECLARE @TOTAL MONEY
DECLARE @SDT DATETIME
DECLARE @EDT DATETIME
DECLARE @LRATE MONEY
DECLARE @SAMT MONEY
DECLARE @EAMT MONEY
DECLARE @FAMT MONEY
DECLARE @LTAMT MONEY
DECLARE @AMT2 MONEY
DECLARE @TNO INT
DECLARE @GTOTAL MONEY
DECLARE @CTOTAL MONEY
DECLARE @BILLORDER INT
DECLARE @SECTION CHAR(1)
DECLARE @HOSTID char(18)
DECLARE @OUTLET1 INT
DECLARE @OUTLET2 INT
DECLARE @OUTLET3 INT
DECLARE @OUTLET4 INT
DECLARE @OUTLET5 INT
DECLARE @OUTLET6 INT
DECLARE @OUTLET7 INT
DECLARE @OUTLET8 INT
DECLARE @OUTLET9 INT
DECLARE @OUTLET10 INT
DECLARE @YESITIS INT
DECLARE @TOTROOMS INT
declare @st money
declare @cess money
declare @cess2 money 
declare @otherchgs money
declare @extrabed money
declare @exbedgst money
declare @food money
declare @foodgst money
set nocount on
--DECLARE @SC MONEY
BEGIN TRANSACTION
SET @SQL = 'DECLARE T_CUR CURSOR FOR SELECT TXNDT,RENT,otherchgs,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,BILLORDER,SECTION,OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10,st,cess,cess2,lrate,extrabed,exbedgst,food,foodgst  FROM ' + @TEMPTABLE
--PRINT @SQL
EXEC (@SQL)
OPEN T_CUR
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@otherchgs,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,
@BILLORDER,@SECTION,
@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10,@st,@cess,@cess2,@lrate,@extrabed,@exbedgst,@food,@foodgst
IF @@ERROR <> 0 GOTO E_ERROR
WHILE @@FETCH_STATUS=0
BEGIN
--PRINT 'PERIYA KARUPPA 111'
SET @SQL = 'IF NOT EXISTS (SELECT * FROM ' +  @TEMPTABLEtemp + ' WHERE 
datediff(d,''1/1/80'',txndt) = datediff(d,''1/1/80'', ''' + convert(varchar(20),@txndt)  + ''') and section = '''  + @section + ''' and  billorder = ' + ltrim(str(@billorder)) + ' )  INSERT INTO ' + @TEMPTABLEtemp  +
'  (TXNDT,RENT,DISCOUNT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,LT,SC,TOTAL,billorder,section,st,cess,cess2,extrabed,exbedgst,food,foodgst) VALUES (''' + CONVERT(VARCHAR(20), @TXNDT,106) +  ''',0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,' +str(@billorder) + ',''' + @section + ''',0,0,0 ,0,0,0,0 )'
EXEC(@SQL)
SET @SQL = 'UPDATE ' + @TEMPTABLEtemp + ' SET
rent = rent + ' +  convert(varchar(20) , isnull(@rent,0)) + ',
extrabed = extrabed + ' +  convert(varchar(20) , isnull(@extrabed,0)) + ',
exbedgst = exbedgst + ' +  convert(varchar(20) , isnull(@exbedgst,0)) + ',
food = food + ' +  convert(varchar(20) , isnull(@food,0)) + ',
foodgst = foodgst + ' +  convert(varchar(20) , isnull(@foodgst,0)) + ',
otherchgs = isnull(otherchgs,0) + ' +  convert(varchar(20) , isnull(@otherchgs,0)) + ',
total = total + ' +  convert(varchar(20) , isnull(@total,0)) + ',
ITEM1 = item1 + ' + convert(varchar(20),isnull(@item1,0)) + ',
ITEM2 = item2 + ' + convert(varchar(20),isnull(@item2,0)) + ',
ITEM3 = item3 + ' + convert(varchar(20),isnull(@item3,0)) + ',
ITEM4 = item4 + ' + convert(varchar(20),isnull(@item4,0)) + ',
ITEM5 = item5 + ' + convert(varchar(20),isnull(@item5,0)) + ',
ITEM6 = item6 + ' + convert(varchar(20),isnull(@item6,0)) + ',
ITEM7 = item7 + ' + convert(varchar(20),isnull(@item7,0)) + ',
ITEM8 = item8 + ' + convert(varchar(20),isnull(@item8,0)) + ',
ITEM9 = item9 + ' + convert(varchar(20),isnull(@item9,0)) + ',
ITEM10 = item10 + ' + convert(varchar(20),isnull(@item10,0)) + ',
sc = sc + ' + convert(varchar(20),isnull(@sc,0)) + ',
lt = lt + ' + convert(varchar(20),isnull(@lt,0)) + ',
st = st + ' + convert(varchar(20),isnull(@st,0)) + ',
cess = cess + ' + convert(varchar(20),isnull(@cess,0)) + ',
cess2 = cess2 + ' + convert(varchar(20),isnull(@cess2,0)) + ',
discount = discount + ' + convert(varchar(20),isnull(@discount,0)) + ',
lrate = case when isnull(lrate,0) > ' +  convert(varchar(20),isnull(@lrate,0)) + ' then lrate else '  +  convert(varchar(20),isnull(@lrate,0)) + ' end,
outlet1 = ' + str(isnull(@outlet1,0)) + ',
outlet2 = ' + str(isnull(@outlet2,0)) + ',
outlet3 = ' + str(isnull(@outlet3,0)) + ',
outlet4 = ' + str(isnull(@outlet4,0)) + ',
outlet5 = ' + str(isnull(@outlet5,0)) + ',
outlet6 = ' + str(isnull(@outlet6,0)) + ',
outlet7 = ' + str(isnull(@outlet7,0)) + ',
outlet8 = ' + str(isnull(@outlet8,0)) + ',
outlet9 = ' + str(isnull(@outlet9,0)) + ',
outlet10 = ' + str(isnull(@outlet10,0)) + ' where datediff(d,''1/1/80'',txndt) = datediff(d,''1/1/80'',''' + convert(varchar(20),@txndt) + ''')  and section = ''' + @section + ''' and 
billorder = ' + str(@billorder) 
--print @sql
exec (@sql)
IF @@ERROR <> 0
BEGIN
CLOSE T_CUR
DEALLOCATE T_CUR
GOTO E_ERROR
END
FETCH NEXT FROM T_CUR INTO @TXNDT,@RENT,@otherchgs,@DISCOUNT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,@LT,@SC,@TOTAL,
@BILLORDER,@SECTION,
@OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10,@st,@cess,@cess2,@lrate,@extrabed,@exbedgst,@food,@foodgst
END
CLOSE T_CUR
DEALLOCATE T_CUR
commit transaction 
return
E_ERROR:
ROLLBACK TRANSACTION
RAISERROR('BILL CREATION ERROR',16,-1)
RETURN




GO
/****** Object:  StoredProcedure [dbo].[sp_gco]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sp_gco    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[sp_gco] @tempco varchar(50), @temptable2 varchar(50),@hostid varchar(50), @codate datetime, @grpname varchar(50)
/* with encryption */
AS
declare @sql  varchar(5000)
declare @gno int
declare @temptable varchar(50)
declare @grpcounter int
declare @con_Id char(20)
declare @pcon varchar(100)
declare @cnt numeric
declare @cnt1 numeric
declare @grpstart int
declare @grpend  int
declare @grpgno numeric
set nocount on
begin transaction
select @grpcounter=max(ISNULL(grpcounter,0)) from guest where grpname = @grpname group by grpname,grpcounter
--PRINT @grpname
--goto e_error
set @grpcounter = isnull(@grpcounter,0) + 1

set @sql = 'declare cnt1 cursor for select cnt=count(*) from ' + @tempco
exec(@sql)
open cnt1
fetch next from cnt1 into @cnt
close cnt1
deallocate cnt1
set @sql = 'declare tempcur cursor for select gno from ' + @tempco 
exec (@sql)
open tempcur
fetch next from tempcur into @gno
set @con_id = REPLACE(@grpname,'''','''''') + ltrim(str(@grpcounter))
set @pcon =  'Group-' + ltrim(rtrim(REPLACE(@grpname,'''',''''''))) + ' ' + ltrim(str(@grpcounter))  + ' Checked Out'
set @cnt1=1
set @grpgno=0
while @@fetch_status = 0 
begin
	update guest set grpcounter=@grpcounter where gno=@gno
	set @temptable = 'XXXR' + ltrim(str(@gno)) + ltrim(@hostid)
	if @cnt1=1 set @grpstart=0 else set @grpstart=1
	if @cnt1=@cnt set  @grpend=0 else set @grpend=1
	exec pbpc1o @temptable, @temptable2,@gno,@codate,0,1,0,0,@con_id, @pcon,@grpstart,@grpend,@grpgno
	if @@error <> 0 goto e_error
	if @cnt1=1 select @grpgno=@gno
	set @cnt1=@cnt1+1
	fetch next from tempcur into @gno
end
close tempcur
deallocate tempcur
commit transaction
set @sql = 'drop table ' + @temptable2
exec(@sql)
set @sql = 'drop table ' + @tempco
exec(@sql)
return
e_error:
rollback transaction
raiserror('error in group checkout',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sp_grd]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sp_grd    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[sp_grd] @mgno int  
/* with encryption */
AS
declare @gno int
declare @grpcounter int
declare @grpname varchar(50)
declare @sql varchar(5000)
declare @brno numeric
set nocount on
begin transaction
select @grpcounter=grpcounter,@grpname=grpname,@brno=brno  from guest where gno = @mgno
set @sql = 'declare tempcur cursor for select gno  from guest where grpname = ''' + REPLACE(@grpname,'''','''''') + ''' and grpcounter = ' + str(@grpcounter)
--print @sql
exec(@sql)
open tempcur
fetch next from tempcur into @gno
while @@fetch_status=0
begin
  exec pbpc4dco @gno
  if @@error <> 0 goto e_error
  fetch next from tempcur into @gno
end
close tempcur
deallocate tempcur
commit transaction
return
e_error:
rollback transaction
return




GO
/****** Object:  StoredProcedure [dbo].[spautobill]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spautobill] @gno numeric, @txndt datetime  
/* with encryption */
AS
declare @amt money
declare @outlet int
declare @qty numeric
declare @rate money
declare @staxincl char(1)
declare @code numeric
declare @strate money
declare @tax money
declare @sale money
declare @tot money
declare @tot1 money 
declare @diff money
declare @roamt money
declare @stset varchar(20)
declare @scset varchar(20)
declare @rno numeric
declare @cid datetime
declare @scamt  money
declare @scadd money
declare @servicetaxeligible char(1)
declare @stonsc int
declare @disc money
declare @servicetax1 money
declare @cess1 money
declare @cess2 money
declare @servicetax money
declare @cess money
declare @vatonst int 
declare @stadd money
declare @gstin varchar(15)
declare @gstinsplit varchar(15)
--print 'Periya'
if exists(select * from guest where autobill='N' and gno=@gno) return
if exists(select * from guest_autobill where gno=@gno and datediff(d,'1/1/90',txndt) = datediff(d,'1/1/90',@txndt) ) return
select @outlet=g_autobilloutlet from setting
select @cid=ci_date,@rate=autoamt,@code=autoitem,@qty=autoqty,@rno=rno,@gstin=gstin,@gstinsplit=gstinsplit  from guest where gno=@gno
select @staxincl=g_staxincl,@stset=stset,@scset=scset,@servicetaxeligible=servicetax,@vatonst=vatonst from outlet where code=@outlet

select @strate=tax from kitmast where code=@code

if  not exists(select * from vattabler where outlet=@outlet)
    insert into vattabler (outlet,sdt,edt,vat) values (@OUTLET,'1 jan 2010','31 dec 3999',0)

declare @vat int
 if exists(select * from vattabler where outlet=@OUTLET and  datediff(d,'1/1/2000',getdate()) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)) 
    select @vat=vat from vattabler where outlet=@OUTLET and  datediff(d,'1/1/2000',getdate()) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
        If isnull(@vat,0) = 0 set @strate=0

If @sTaxIncl = 'Y' set @rate = round(( @rate * 100) / (100 + @strate),2)
set @sale = @rate*@qty
set @tax = @rate*@qty * @strate/100
set @tax=round(@tax,2)
set @amt = @sale
				set @disc = 0
				set @scamt=0
				set @scadd = 0
				set @servicetax1 = 0
				set @cess1 = 0
				set @cess2 = 0
				if @servicetaxeligible = 'Y'
					begin
						declare @stable money
						select @stonsc=g_stonsc from setting
						if @stonsc=1 
								select @stable = @amt + @scamt - @disc
						else
								select @stable = @amt  - @disc 
declare @sttableno int
select @sttableno=sttableno from outlet where code=@outlet
print @sttableno
						exec getservicetaxr @stable ,@txndt ,@servicetax1 out ,@cess1 out,@outlet ,@sttableno,@cess2 out 
						set @servicetax = @servicetax + @servicetax1
						set @cess = @cess + @cess1
					end
				if @vatonst= 1 
					set @stadd = @servicetax1+@cess1
				else
					set @stadd = 0

					--print convert(varchar(20),@tax) 
					--goto e_error
				if @stset=0
					select @tax=round((@sale + @scadd + @stadd)*@strate/100,2)
				else
					select @tax=round(((@sale + @scadd + @stadd) - @disc)*@strate/100,2)

set @roamt=0
if exists(select * from setting where stop_roundoff_autobilling=0)
	begin
	set @tot = @sale+@tax+@servicetax1+@cess1+@cess2
	set @tot1 = floor(@tot)
	set @diff=@tot-@tot1
	if @diff > .50 
		begin
		set @roamt = 1 - @diff
		end
	else
		begin
		set @roamt = @diff*- 1
		end
	end
begin transaction
select  * into #dummy from dummykit
insert into #dummy values (@code,@qty,@rate,@strate,@sale, @tax,0,1,0,0,'',@servicetax1,@cess1,0,@cess2)
declare @bno1 int
declare @usernm varchar(20)
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()

--if datediff(s,@txndt,@cid) > 0  set @txndt=@cid
exec  pbpkb1u_co  #dummy,@outlet,@txndt,2,0,@gno,'NO','','',@rno,1,@roamt,0,0,0,@stset,@scset,@bno1 out,@usernm,1,'Y',0,0,0,0,0,@gstin,@gstinsplit
if @@error<>0 goto e_error
insert into guest_autobill  (gno,txndt) values(@gno,@txndt)
if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Erro',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[spbls]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spbls    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[spbls] @company int,@fy int, @month int, @fname varchar(50) OUT 
/* with encryption */
AS
declare @rv varchar(50)
declare @t1 varchar(50)
declare @sql varchar(5000)
declare @opgbal money
declare @cgp_actno int
declare @cgp_Desc varchar(50)
declare @balance money
declare @grp int
declare @flag int
declare @grpname varchar(30)
declare @flgname varchar(30)
declare @al char(1)
declare @d1 money
declare @c1 money
declare @c2 money
declare @d2 money
declare @c3 money
declare @d3 money
declare @c4 money
declare @d4 money
declare @c5 money
declare @d5 money
declare @c6 money
declare @d6 money
declare @c7 money
declare @d7 money
declare @c8 money
declare @d8 money
declare @c9 money
declare @d9 money
declare @c10 money
declare @d10 money
declare @c11 money
declare @d11 money
declare @c12 money
declare @d12 money
declare @cbalance money
declare @pbalance money
declare @change money
declare @credits money
declare @debits money
declare @opgbal1 money
declare @g_finstart int
declare @err varchar(200)
select @g_finstart=g_finstart from setting
begin transaction
set @err = 'BS Error:'
--print 'periya'
EXEC RANDFILE @RV OUT
set @t1 = 'bs' + ltrim(@RV)
SET @SQL=  'if exists (select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'')   drop table ' + @T1
EXEC(@sql) 
if @@error<>0 goto e_error
SET @SQL = 'SELECT * INTO ' + @T1 + '  FROM  dummyBS'
EXEC (@SQL)
if @@error<>0 goto e_error
if not exists (select * from temptables where computer=host_name() and tname = @T1) insert into temptables (computer,tname) values (host_name(),@T1)
set @sql = 'declare tempcur cursor for SELECT opgbal,CGP_ACTNO,CGP_DEsC,BALANCE,GRP,FLAG,GRPNAME=(select name from glgroup where code=glprimld.grp),FLGNAME=(select name from glflag where code=glprimld.flag),(select al from  glgroup where code=glprimld.grp) al,
debit1,credit1,debit2,credit2,debit3,credit3,debit4,credit4,debit5,credit5,debit6,credit6,debit7,credit7,debit8,credit8,debit9,credit9,debit10,credit10,debit11,credit11,debit12,credit12
from glprimld where fy =' + ltrim(rtrim(@fy))
exec(@sql)
open tempcur
fetch next from tempcur into @opgbal1,@cgp_Actno,@cgp_desc,@balance,@grp,@flag,@grpname,@flgname,@al,@d1,@c1,@d2,@c2,@d3,@c3,@d4,@c4,@d5,@c5,@d6,@c6,@d7,@c7,@d8,@c8,@d9,@c9,@d10,@c10,@d11,@c11,@d12,@c12
if @@error<>0 goto e_error
while @@fetch_status=0
begin
set @opgbal=@opgbal1
--if @cgp_actno=1
--begin
--set @err =  @err + str(@fy) + ' Month ' + str(@month) + convert(varchar(20),@opgbal1) + @cgp_desc
--goto e_error
--end
if @g_finstart=0
begin
	set @cbalance = case 
	 when @month=3 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+  @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11+ @c12-@d12 
	 when @month=2 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11+ @c12-@d12 
	 when @month=1 then
			   @opgbal + @c1-@d1 + @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11+ @c12-@d12 
	 when @month=12 then
			   @opgbal + @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11+ @c12-@d12 
	 when @month=11 then
			   @opgbal + @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11
	 when @month=10 then
			   @opgbal + @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10
	 when @month=9 then
			   @opgbal + @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9
	 when @month=8 then
			   @opgbal + @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8
	 when @month=7 then
			   @opgbal + @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7
	 when @month=6 then
			   @opgbal + @c4-@d4+ @c5-@d5+ @c6-@d6
	 when @month=5 then
			   @opgbal + @c4-@d4+@c5-@d5
	 when @month=4 then
			   @opgbal + @c4-@d4
	end
	set @opgbal=@opgbal1
	set @pbalance = case 
	 when @month=3 then
			   @opgbal + @c1-@d1 + @c2-@d2+  @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11+ @c12-@d12 
	 when @month=2 then
			   @opgbal + @c1-@d1 + @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11+ @c12-@d12 
	 when @month=1 then
			   @opgbal + @c4-@d4 + @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11+ @c12-@d12 
	 when @month=12 then
			   @opgbal + @c4-@d4 + @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11
	 when @month=11 then
			   @opgbal + @c4-@d4 + @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10
	 when @month=10 then
			   @opgbal + @c4-@d4 + @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9
	 when @month=9 then
			   @opgbal + @c4-@d4 + @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8
	 when @month=8 then
			   @opgbal + @c4-@d4 + @c5-@d5+ @c6-@d6+ @c7-@d7
	 when @month=7 then
			   @opgbal + @c4-@d4 + @c5-@d5+ @c6-@d6
	 when @month=6 then
			   @opgbal + @c4-@d4 + @c5-@d5
	 when @month=5 then
			   @opgbal + @c4-@d4
	 when @month=4 then
			   @opgbal 
	end
end
else   -- if g_finstart <> 0
begin

	set @cbalance = case 
	 when @month=12 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11+ @c12-@d12 
	 when @month=11 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11
	 when @month=10 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10
	 when @month=9 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9
	 when @month=8 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8
	 when @month=7 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7
	 when @month=6 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6
	 when @month=5 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5
	 when @month=4 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4
	 when @month=3 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3
	 when @month=2 then
			   @opgbal + @c1-@d1 + @c2-@d2
	 when @month=1 then
			   @opgbal + @c1-@d1
	end
	set @opgbal=@opgbal1
	set @pbalance = case 
	 when @month=12 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10+ @c11-@d11
	 when @month=11 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9+ @c10-@d10
	 when @month=10 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8+ @c9-@d9
	 when @month=9 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7+ @c8-@d8
	 when @month=8 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6+ @c7-@d7
	 when @month=7 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5+ @c6-@d6
	 when @month=6 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4+ @c5-@d5
	 when @month=5 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3+ @c4-@d4
	 when @month=4 then
			   @opgbal + @c1-@d1 + @c2-@d2+ @c3-@d3
	 when @month=3 then
			   @opgbal + @c1-@d1 + @c2-@d2
	 when @month=2 then
			   @opgbal + @c1-@d1 
	 when @month=1 then
			   @opgbal 
	end


end

set @credits = case 
 when @month = 4 then @c4
 when @month = 5 then @c5
 when @month = 6 then @c6
 when @month = 7 then @c7
 when @month = 8 then @c8
 when @month = 9 then @c9
 when @month = 10 then @c10
 when @month = 11 then @c11
 when @month = 12 then @c12
 when @month = 1 then @c1
 when @month = 2 then @c2
 when @month = 3 then @c3
end
set @debits = case 
 when @month = 4 then @d4
 when @month = 5 then @d5
 when @month = 6 then @d6
 when @month = 7 then @d7
 when @month = 8 then @d8
 when @month = 9 then @d9
 when @month = 10 then @d10
 when @month = 11 then @d11
 when @month = 12 then @d12
 when @month = 1 then @d1
 when @month = 2 then @d2
 when @month = 3 then @d3
end

if @cbalance<>0 or @pbalance <> 0 
begin
set @sql =  'insert into ' + @t1 +' (cgp_actno,cgp_desc,balance,previous,months,credits,debits,grp,flag,al,grpname,flgname) values (' +
str(@cgp_actno) + ',''' + @cgp_desc  + ''','+ convert(varchar(20),@cbalance)+ ','+ convert(varchar(20),@pbalance)+ ','+
convert(varchar(20),@credits-@debits)+ ',' + convert(varchar(20),@credits)+ ','+ convert(varchar(20),@debits)+ ',' + str(@grp) + ',' +
stR(@flag) + ',''' +@al + ''',''' +  @grpname + ''',''' + @flgname + ''')'
exec(@sql)
if @@error<>0 goto e_error
end
fetch next from tempcur into @opgbal1,@cgp_Actno,@cgp_desc,@balance,@grp,@flag,@grpname,@flgname,@al,@d1,@c1,@d2,@c2,@d3,@c3,@d4,@c4,@d5,@c5,@d6,@c6,@d7,@c7,@d8,@c8,@d9,@c9,@d10,@c10,@d11,@c11,@d12,@c12
end
close tempcur
deallocate tempcur
set @fname=@t1
commit transaction
return
e_error:
rollback transaction

raiserror(@err ,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spbls1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[spbls1] @company int,@fy int, @sdt datetime, @edt datetime , @fullfy int, @fname varchar(50) OUT 
/* with encryption */
as
declare @rv varchar(50)
declare @t1 varchar(50)
declare @sql varchar(5000)
declare @opgbal money
declare @cgp_actno int
declare @cgp_Desc varchar(50)
declare @balance money
declare @grp int
declare @flag int
declare @grpname varchar(30)
declare @flgname varchar(30)
declare @al char(1)
declare @cbalance money
declare @pbalance money
declare @change money
declare @credits money
declare @debits money
declare @opgbal1 money
declare @g_finstart int
declare @err varchar(200)
select @g_finstart=g_finstart from setting
begin transaction
set @err = 'BS Error:'
--print 'periya'
EXEC RANDFILE @RV OUT
set @t1 = 'bs' + ltrim(@RV)
SET @SQL=  'if exists (select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'')   drop table ' + @T1
EXEC(@sql) 
if @@error<>0 goto e_error
SET @SQL = 'SELECT * INTO ' + @T1 + '  FROM  dummyBS'
EXEC (@SQL)
if @@error<>0 goto e_error
if not exists (select * from temptables where computer=host_name() and tname = @T1) insert into temptables (computer,tname) values (host_name(),@T1)

IF CURSOR_STATUS('global','tempcur')>=-1
BEGIN
 DEALLOCATE tempcur
END


set @sql = 'declare tempcur cursor for SELECT opgbal=' + case when @fullfy=1 then 'opgbal' else 'isnull(dbo.getbalancefast(cgp_actno,''' + convert(varchar(20),dateadd(d,-1,@sdt)) + ''',' + str(@fy) + '),0)'  end + ' ,CGP_ACTNO,CGP_DEsC,
isnull((select sum(amt) from vouchers where fy='+ str(@fy) + ' and actnum=glprimld.cgp_actno and datediff(d,''1/1/2000'', txndt) between datediff(d,''1/1/2000'',''' + convert(varchar(20),@sdt) + ''') and datediff(d,''1/1/2000'',''' + convert(varchar(20),@edt) + ''') and  amttyp=''C''),0) as credits,
isnull((select sum(amt) from vouchers where fy='+ str(@fy) + ' and actnum=glprimld.cgp_actno and datediff(d,''1/1/2000'', txndt) between datediff(d,''1/1/2000'',''' + convert(varchar(20),@sdt) + ''') and datediff(d,''1/1/2000'',''' + convert(varchar(20),@edt) + ''') and amttyp=''D''),0) as debits,
GRP,FLAG,GRPNAME=GLGROUP.NAME,FLGNAME=GLFLAG.NAME,
glgroup.al from glprimld left outer join glgroup on glprimld.grp=glgroup.code left outer  join glflag on glprimld.flag=glflag.code where fy = ' + ltrim(str(@fy)) 
exec(@sql)
open tempcur
fetch next from tempcur into @opgbal1,@cgp_Actno,@cgp_desc,@credits,@debits,@grp,@flag,@grpname,@flgname,@al
if @@error<>0 goto e_error
while @@fetch_status=0
begin
set @opgbal=@opgbal1
set @pbalance= @opgbal1
set @cbalance=@opgbal1+@credits-@debits
--if @cbalance<>0 or @pbalance <> 0 
if @credits <>0 or @debits<>0 
begin
set @sql =  'insert into ' + @t1 +' (cgp_actno,cgp_desc,balance,previous,months,credits,debits,grp,flag,al,grpname,flgname) values (' +
str(@cgp_actno) + ',''' + @cgp_desc  + ''','+ convert(varchar(20),@cbalance)+ ','+ convert(varchar(20),@pbalance)+ ','+
convert(varchar(20),@credits-@debits)+ ',' + convert(varchar(20),@credits)+ ','+ convert(varchar(20),@debits)+ ',' + str(@grp) + ',' +
stR(@flag) + ',''' +@al + ''',''' +  @grpname + ''',''' + @flgname + ''')'
exec(@sql)
if @@error<>0 goto e_error
end
fetch next from tempcur into @opgbal1,@cgp_Actno,@cgp_desc,@credits,@debits,@grp,@flag,@grpname,@flgname,@al
end
close tempcur
deallocate tempcur



--set @sql = 'insert into ' + @t1 + ' (cgp_actno,cgp_desc,balance,previous,months,credits,debits,grp,flag,al,grpname,flgname) select cgp_actno,cgp_desc,opgbal-credits-debits balance,opgbal as previous,credits-debits months,credits,debits,grp,flag,al,grpname,flgname from  (
--SELECT opgbal=isnull(dbo.getbalancefast(cgp_actno,''' + convert(varchar(20),dateadd(d,-1,@sdt)) + ''',' + str(@fy) + '),0),CGP_ACTNO,CGP_DEsC,
--isnull((select sum(amt) from vouchers where fy='+ str(@fy) + ' and actnum=glprimld.cgp_actno and datediff(d,''1/1/2000'', txndt) between datediff(d,''1/1/2000'',''' + convert(varchar(20),@sdt) + ''') and datediff(d,''1/1/2000'',''' + convert(varchar(20),@edt) + ''') and  amttyp=''C''),0) as credits,
--isnull((select sum(amt) from vouchers where fy='+ str(@fy) + ' and actnum=glprimld.cgp_actno and datediff(d,''1/1/2000'', txndt) between datediff(d,''1/1/2000'',''' + convert(varchar(20),@sdt) + ''') and datediff(d,''1/1/2000'',''' + convert(varchar(20),@edt) + ''') and amttyp=''D''),0) as debits,
--GRP,FLAG,GRPNAME=GLGROUP.NAME,FLGNAME=GLFLAG.NAME,
--glgroup.al from glprimld left outer join glgroup on glprimld.grp=glgroup.code left outer  join glflag on glprimld.flag=glflag.code where fy = ' + ltrim(str(@fy)) +') a' 
--exec(@sql)

set @fname=@t1
commit transaction
return
e_error:
rollback transaction

raiserror(@err ,16,-1)

GO
/****** Object:  StoredProcedure [dbo].[spchnno]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spchnno] @outlet as int,@sdt as datetime,@edt as datetime,@sno as numeric, @daily as numeric=0,@recordnew as int=0
/* with encryption */
AS
declare @bno numeric
declare @cdt datetime
declare @cdt1 datetime
declare @fy1 numeric
begin transaction
exec getfy @sdt,1,@fy1 out
declare t1 cursor for select bno,txndt from kithist1 where outlet=@outlet and datediff(d,'1/1/90',txndt) between datediff(d,'1/1/90',@sdt) and datediff(d,'1/1/90',@edt)  order by datediff(d,'1/1/90',txndt),bno
open t1
fetch next from t1 into @bno,@cdt
set @cdt1=@cdt
while @@fetch_status=0 begin
update kithist1 set sno=@sno where bno=@bno
if @@error <> 0 goto e_error
set @sno=@sno+1
fetch next from t1 into @bno,@cdt
if @daily=1 and datediff(d,'1/1/90',@cdt1) <> datediff(d,'1/1/90',@cdt) begin
   set @sno=1
   set @cdt1=@cdt
end
end
if @recordnew=1 
begin
--update outlet set bno=@sno-1 where code=@outlet
update serviceasno set sno=@sno-1 where outlet=@outlet and fy=@fy1
if @@error <> 0 goto e_error
end
close t1
deallocate t1
commit transaction
return
e_error:
rollback transaction
raiserror('erro',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[spchnnob]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 20 Aug 2017
-- Description:	Renumbering Service B Bills
-- =============================================
CREATE PROCEDURE [dbo].[spchnnob]
@outlet as int,@sdt as datetime,@edt as datetime,@sno as numeric, @daily as numeric=0,@recordnew as int=0
/* with encryption */
AS
declare @bno numeric
declare @cdt datetime
declare @cdt1 datetime
declare @fy1 numeric
begin transaction
exec getfy @sdt,1,@fy1 out
declare t1 cursor for select cno,txndt from phone where outlet=@outlet and datediff(d,'1/1/90',txndt) between datediff(d,'1/1/90',@sdt) and datediff(d,'1/1/90',@edt)  order by datediff(d,'1/1/90',txndt),cno
open t1
fetch next from t1 into @bno,@cdt
set @cdt1=@cdt
while @@fetch_status=0 begin
update phone set sno=@sno where cno=@bno
if @@error <> 0 goto e_error
set @sno=@sno+1
fetch next from t1 into @bno,@cdt
if @daily=1 and datediff(d,'1/1/90',@cdt1) <> datediff(d,'1/1/90',@cdt) begin
   set @sno=1
   set @cdt1=@cdt
end
end
if @recordnew=1 
begin
update phoutlet set sno=@sno-1 where outlet=@outlet
if @@error <> 0 goto e_error
end
close t1
deallocate t1
commit transaction
return
e_error:
rollback transaction
raiserror('erro',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[spcu]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spcu    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[spcu] @cgpno integer=0 
/* with encryption */
AS
declare @company numeric
declare @fy numeric
declare @actno numeric
declare @flag int
declare @grp int
declare @desc varchar(50)
declare @onac char(1)
declare @asset char(1)
declare @tdno numeric
declare @acno numeric
declare @coname varchar(100)
DECLARE @PERMIT CHAR(1)
begin transaction
declare tempcurco cursor for select company,name from company
open tempcurco
fetch next from tempcurco into @company,@coname
while @@fetch_status=0
begin
	declare tempcurfy cursor for select fy from finyear where company=@company order by fy
	open tempcurfy
	fetch next from tempcurfy into @fy
	while @@fetch_status=0
	begin
		if @cgpno=0
			declare tempcur1 cursor for select distinct cgp_desc,cgp_actno,flag,grp,asset,onac,tdno,PERMIT from chart order by cgp_actno
		else
			declare tempcur1 cursor for select distinct cgp_desc,cgp_actno,flag,grp,asset,onac,tdno,PERMIT from chart where cgp_actno=@cgpno
		open tempcur1
		fetch next from tempcur1 into @desc,@actno,@flag,@grp,@asset,@onac,@tdno,@PERMIT
		while @@fetch_status=0
		begin
			if not exists(select cgp_actno from glprimld where fy=@fy and cgp_actno=@actno)
			begin
				if not exists(select cgp_actno from glprimld where fy=@fy and cgp_desc=rtrim(ltrim(@desc)))
				begin
					insert into glprimld (company,fy,cgp_Actno,cgp_desc,flag,grp,asset,onac,PERMIT,tdno) values (@company,@fy,@actno,@desc,@flag,@grp,@asset,@onac,@PERMIT,@tdno)
				end
			end
			else
			begin
			    if not exists(select cgp_actno from glprimld where cgp_desc=@desc and cgp_actno=@actno and fy=@fy)
				    begin
			  		print @desc
					update glprimld set cgp_desc=@desc,flag=@flag,grp=@grp where fy=@fy and cgp_actno=@actno
					end
			end
			fetch next from tempcur1 into @desc,@actno,@flag,@grp,@asset,@onac,@tdno,@PERMIT
		end
		close tempcur1
		deallocate tempcur1
		fetch next from tempcurfy into @fy
	end
	close tempcurfy
	deallocate tempcurfy
	fetch next from tempcurco into @company,@coname
end
close tempcurco
deallocate tempcurco

declare @bal money
declare @opgdate datetime
commit transaction
return
e_error:
rollback transaction
raiserror('up-chart failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spde]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spde    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE  [dbo].[spde] @gno numeric,@usernm varchar(20)='SYS',@sessionid varchar(200)=''
/* with encryption */
AS
declare @reserve_ref numeric
declare @reserve_adv money
declare @reserve_adv_cno numeric
declare @ctrlno numeric
declare @guestname varchar(50)
declare @eno numeric
declare @rno int
declare @cch_amount money
declare @err varchar(2000)
declare @channelexbed money
declare @dt datetime
declare @expecteddep datetime
declare @extrabedcharges money
declare @qcheck char(1)
declare @qmsg as varchar(100)
if @gno=0 begin SET @ERR =  'Invalid System number' goto e_error end
if not exists(select * from room where gno=@gno) begin print 'Missing System Number' goto e_error end
select @guestname=name,@eno=eno,@reserve_ref=reserve_ref,@reserve_adv=reserve_adv,@reserve_adv_cno=reserve_adv_cno,@channelexbed=channelexbed,@dt=ci_date,@expecteddep=EXPECTEDDEP,@extrabedcharges=ExtraBedCharges   from guest where gno=@gno
select @rno=rno,@qcheck=qcheck  from room_guest where gno=@gno
if @qcheck='Y' set @qmsg='Under Quick Check in process' else set @qmsg=''
if exists (select * from kithist1 where gno=@gno and pmode=2)  or
exists (select * from phone where gno=@gno and pmode = 2) or
exists (select * from chalan where ogno=@gno AND STATUS<>'C') 
begin
raiserror('Some Service Bills are outstanding agains this entry...Deletion not allowed....' ,16,-1)
return
end
if exists (select * from advance where gno=@gno and cno <> @reserve_adv_cno)
begin
SET @ERR =  'Advance received against some rooms are outstanding..Deletion not allowed'
goto e_error
end
if exists(select * from roomdues where gno=@gno) 
begin
SET @ERR =  'Dues from other room check outs are panding for this customer' + nchar(13) + 'Deletion of Entry not Allowed'
goto e_error
end
--SET @ERR = '  Level 1 ' + CONVERT(VARCHAR(20),@RESERVE_REF) + '  '+ + CONVERT(VARCHAR(20),@ctrlno)  GOTO E_ERROR 
if exists(select * from currentkot where pend_type=2 and pend_code=@gno)
begin
set @err = 'Some running order biills are outstanding for this entry' + nchar(13) + 'Deletion not allowed'
goto e_error
end

begin transaction ram1
if @reserve_adv_cno<>0 
	begin
	exec pbpa1508 @reserve_adv_cno, @usernm,0,1
	if @@error <> 0 goto e_error
	end
if @reserve_ref<>0
begin
	select @ctrlno=ctrlno from sptbrs1 where myctrlno=@reserve_ref
	--  print str(@ctrlno)
	update sptbrs1 set arrivedrooms=arrivedrooms-1 where myctrlno=@reserve_ref
	if @channelexbed <> 0 
		begin
		update sptbrs1 set channelexbedok=0 where myctrlno=@reserve_ref
		if @@error <> 0 goto e_error
		end
	update sptbrooms set status1='N' where rno=@rno and myctrlno=@reserve_ref
	if @@error <> 0 goto e_error
	update sptbrs set advanceadjd=advanceadjd-isnull(@reserve_adv,0) where ctrlno=@ctrlno
	if @@error <> 0 goto e_error
if @extrabedcharges<>0
begin
	update sptbrs set extrabedalloted=extrabedalloted-1 where ctrlno=@ctrlno
	if @@error <> 0 goto e_error
end 

	if exists(select * from crcardhold where holdtype=1 and gno=@gno and status=' ') 
		begin
		delete from crcardhold where holdtype=1 and gno=@gno and status=' '
		update crcardhold set status=' ' where holdtype=2 and gno=@ctrlno and status='M'
		end
--	SET @ERR = CONVERT(VARCHAR(20),@RESERVE_REF)  GOTO E_ERROR 
end

update  room set gno=0,grpname='',qcheck=''  where gno=@gno
if @@error <> 0 goto e_error
delete from groom where gno=@gno
   if @@error <> 0 goto e_error
delete from gsplit where gno=@gno
   if @@error <> 0 goto e_error
delete from guest where gno=@gno
   if @@error <> 0 goto e_error
delete from gffl where gno=@gno
   if @@error <> 0 goto e_error
delete from gpassport where gno=@gno
   if @@error <> 0 goto e_error
delete from guest_memo where ctrlno_ref in (select ctrlno from gm_master where gno=@gno)
   if @@error <> 0 goto e_error
delete from gm_master where gno=@gno
   if @@error <> 0 goto e_error
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm,'GR Entry Deleted - Guest Name ' + rtrim(isnull(@guestname,'Not avaialbel')) + ' Entry Number ' + ltrim(str(@eno)) + ' System No ' + ltrim(str(@gno)) + ' '+  @Qmsg ,15,@gno)
if @@error <> 0 goto e_error
declare @nor numeric
declare @adv money
select @nor=arrivedrooms from sptbrs1 where myctrlno=@reserve_ref
select @adv=advanceadjd from sptbrs where ctrlno=@ctrlno
--SET @ERR = convert(varchar(20),@adv) + ' ' + convert(varchar(20),@nor) + '  Level 2 ' + CONVERT(VARCHAR(20),@RESERVE_REF) + '  '+ + CONVERT(VARCHAR(20),@ctrlno)  GOTO E_ERROR 

--updating staah  and CRS
	While DateDiff(d,@DT, @expecteddep) >= 0
	begin
		if not exists (select * from staahinv where status='' and datediff(d,txndt,@dt) = 0) insert into staahinv (txndt) values (@DT)
		if not exists (select * from crsinv where status='' and datediff(d,txndt,@dt) = 0) insert into crsinv (txndt,status,message) values (@DT,'','')
		set @dt = dateadd(d,1,@dt)
	end


commit 
return
e_error:
declare @tc numeric
set @tc = @@trancount
--set @err = 'Error in entry deletion ' + convert(varchar(20),@tc) 
if @tc=1 rollback transaction
raiserror(@err,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spdeg]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spdeg    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[spdeg] @gno numeric 
/* with encryption */
AS
declare @ngno numeric
declare @grpname varchar(50)
declare @err varchar(2000)
declare @tc numeric
begin transaction ram2
select @grpname=grpname from room where gno=@gno
UPDATE ROOM SET GRPNAME='' WHERE GRPNAME<>'' AND GNO=0
declare tempcur cursor for select gno from room where grpname=@grpname AND GNO<>0
open tempcur
fetch next from tempcur into @ngno
while @@fetch_status=0
begin
	exec spde @ngno
	if @@error <> 0 goto e_error1
	fetch next from tempcur into @ngno
end
close tempcur
deallocate tempcur
commit transaction  ram2
return
e_error1:
set @tc = @@trancount
--set @err = 'Error in Group Entry Deletion  Refer to the above Error Message '
if @tc > 0 rollback transaction
raiserror(@err,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spdlpur]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spdlpur    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[spdlpur] @bctrlno int,@type varchar(20)
/* with encryption */
AS
declare @company int
declare @fy int
declare @txndt datetime
declare @grno numeric
declare @storeno numeric
declare @ret_val int
declare @ctrlno numeric
declare @hctrlno numeric
declare @freeze datetime
declare @storename varchar(100)
declare @invno varchar(50)
declare @sid numeric
declare @sname varchar(100)
declare @chlno numeric
declare @errmsg varchar(1000)
set @errmsg = 'Store DELETION Failed'
begin transaction
--print 'periya'
if not exists(select * from storebills where bctrlno=@bctrlno) 
	begin
	set @errmsg = 'Bill NOT Found'
	goto e_error
	end
select @sid=supplier,@invno=invoiceno,@chlno=chlno, @txndt=txndt,@grno=grno,@storeno=storeno from storebills where bctrlno=@bctrlno
select @freeze=freeze,@company=company from store where storeno=@storeno
select @storename=storename from store where storeno=@storeno
--print convert(varchar(20),@txndt) goto e_error
--print str(@bctrlno) goto e_error

if @type='Purchase'
begin
-- Newly added 04 Nov 2017 to check that the stock does go minus in future date due to deleting this purchase entry
declare @cnt int
declare @luperhu int 
declare @iname varchar(200)
declare @qty numeric
declare @qtyhu numeric
declare c1 cursor for select hctrlno,ctrlno,Qty from storehist where bctrlno=@bctrlno
open c1
fetch next from c1 into @hctrlno,@ctrlno,@qty
while @@fetch_status=0 
	begin
	select @luperhu=luperhu,@iname=iname from storeitems where ctrlno=@ctrlno
	set @qtyhu = @qty / @luperhu
	exec geteditbalance1 @ctrlno,@qtyhu,0,@txndt,@cnt out,1      
	if @cnt=1
		begin
		set @errmsg = '###' + @iname + ' - The stock goes minus on future DATE(s) ' + convert(varchar(20),@txndt) + ' on deletion of this purchase..Deletion aborted'
		goto e_error
		end
	fetch next from c1 into @hctrlno,@ctrlno,@qty
	end
close c1
deallocate c1        

----- Newly added 04 Nov 2017 ENDS



	if @sid<>0 
	select @sname=name from suppliers where sid=@sid
	else
	select @sname='Cash Purchase'
end
else
begin
           select @sname= 'Issue : ' 
	select @invno=ltrim(str(@chlno))
end

if datediff(d,@freeze,@txndt) <= 0 begin print 'Freezed Transaction...Not Allowed' goto e_error end
select @fy = 0
exec getfy @txndt,@company,@fy out
if @@error<>0 goto e_error
if @type='Purchase'
begin
select @ret_val =0
exec delgrno @company,@fy,@grno,@txndt,@ret_val out
if @ret_val=1 goto e_error
if @@error<>0 goto e_error
end

declare @bofy datetime
declare @eofy datetime
select @bofy = dbo.getbofy(@txndt)
select @eofy = dbo.geteofy(@txndt)

declare c1 cursor for select hctrlno,ctrlno from storehist where bctrlno=@bctrlno
open c1
fetch next from c1 into @hctrlno,@ctrlno
while @@fetch_status=0 begin
    --update storehist set forupdt='Y' where hctrlno=@hctrlno 
    delete from storehist where hctrlno=@hctrlno
    if @@error<>0 goto e_error
	--exec spsthrewrite @ctrlno,@fy, @bofy,@eofy 
	--if @@error<>0 goto e_error
    fetch next from c1 into @hctrlno,@ctrlno
end
close c1
deallocate c1        
delete from storebills where bctrlno=@bctrlno
if @@error<>0 goto e_error
--print @sname
insert into acclog (narration,txntype) values ('Store-' +@storename + ' ' + @sname + ' '+  @type + ' ' + case when @type='Purchase' then 'Invoice' else 'Chalan' end + ' No:' + ltrim(@invno) + ' Dated ' +  convert(varchar(20),@txndt)  +  ' Deleted',case when @type='Purchase' then 150 else 152 end )
if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror(@errmsg,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spdrs]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spdrs    Script Date: 15/02/2004 6:23:00 PM ******/
CREATE PROCEDURE [dbo].[spdrs] @ctrlno numeric,@sessionid varchar(200)='' 
/* with encryption */
AS
declare @sql  varchar(5000)
declare @txndt datetime
declare @grno numeric
declare @ret_val numeric
declare @fy numeric
declare @userno numeric
if @ctrlno=0 return
select @userno=userno from comuser where [host_id]=host_id() 
select @userno=userno from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
if exists (select * from sptbrs where ctrlno=@ctrlno and net='Y')
if not exists(select * from servicepermission where userno=@userno  and serviceno=845 and permission='Y')
begin
print '.........................................This is an internet booking.........................................' + nchar(13)+ '.............................Y o u   c a n   n o t    d e l e t e   t h i s   e n t r y............................'
raiserror('Error',16,-1)
return
end
if exists (select * from sptbrs1 where ctrlno=@ctrlno and 
(arrivedrooms+cancelledrooms) <> 0)
begin
print 'This entry is either arrived or cancelled..can not be deleted'
raiserror('Error',16,-1)
return
end

begin transaction
set @sql ='declare tcur cursor for select grno,txndt from sptbrs_ad where ctrlno=' + str(@ctrlno)
exec (@sql)
open tcur
fetch next from tcur into @grno,@txndt
while @@fetch_status=0
begin
set @ret_val = 0
set @fy = 0
exec getfy @txndt,1,@fy out
if @@error<>0 goto e_error
exec delgrno 1,@fy,@grno,@txndt,@ret_val out
if @@error<>0 goto e_error
delete from sptbrs_ad where current of tcur
fetch next from tcur into @grno,@txndt
end
close tcur
deallocate tcur
delete from sptbrs1 where ctrlno=@ctrlno
if @@error<>0 goto e_error
delete from sptbrs where ctrlno=@ctrlno
if @@error<>0 goto e_error
declare @usernm varchar(50)
if @sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id()  and inet=0 and [host_id]=host_id()
else
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=@sessionid and [host_id]=host_id()

insert into acclog (txndt,usernm,narration,txntype,gno,sessionid) values (getdate(),@usernm, 'Reservation Deleted..Reservation No.' +STR(@ctrlno) ,21,0,@sessionid)
if @@error <> 0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Error in spdrs',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spedaac]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spedaac    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spedaac]  @cno numeric, @namt money , @post_to numeric=0  
/* with encryption */
AS
declare @advrno int
declare @split char(1)
declare @grno numeric
declare @company int
declare @txndt datetime
declare @fy int
declare @gno numeric
declare @amount money
declare @ret_val int
declare @rno int
declare @partic varchar(100)
declare @advdate datetime
declare @bno numeric
declare  @oad money
--print 'periya'
begin transaction
select @gno=gno,@advrno=ano,@split=split,@grno=grno,@company=company,@txndt=txndt,@amount=amount,@oad=amount from advance where cno=@cno
exec @fy=getfy @txndt,@company,@fy out
exec delgrno @company,@fy,@grno,@txndt,@ret_val out
if @@error <> 0 goto e_error
set @advdate=@txndt
update advance set amount=@namt where cno=@cno
update billhist1 set advance=advance+@namt-@amount where  company=1 and split<>'Y' and brno=(select brno from guest where gno=@gno)
select @bno=bno from billhist1 where  company=1 and split<>'Y' and brno=(select brno from guest where gno=@gno)
select @rno=rno from guest where gno=@gno
set @partic = 'To adv. recd. Room No. ' + str(@rno) + ' Main bill'
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
 values (@company, @fy,  1 ,@Namt , @partic  , 'D', @txndt , @grno ,'Y', @advrno)
if @@error <> 0 goto e_error
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
 values (@company, @fy,  50 ,@Namt ,'By advance recd. Room No. ' + str(@rno) , 'C', @txndt , @grno ,'Y', @advrno)
if @@error <> 0 goto e_error
declare @eno int
declare @guestname varchar(50)
select @eno=eno,@guestname=name from guest_view1 where gno =@gno
declare @usernm varchar(50)
declare @cno1 numeric
select @cno1=cno from billhist1 where company=@company and split<>'Y'  and brno=(select brno from guest where gno=@gno)
exec pbvc @cno1,@cno1
--print 'periya'
declare @t1 varchar(50)
declare @rv varchar(50)
declare @sql varchar(5000)
exec randfile @rv out
if @post_to <> 0
begin
set @txndt=@advdate
 set @t1 = 'vpp' + ltrim(@rv)
 set @sql= 'select * into ' + @t1  + ' from dummyvch'
 exec(@sql)
 if @amount-@namt > 0 
  begin
  set @sql = 'insert into ' + @t1 + ' (company,actnum,amt,partic,amttyp,txndt,txntyp,fdrsno) values (1,' + str(@post_to) +','+ str(@amount-@namt) + ',''' + 'Cash received-adv.Receipt no ' + ltrim(str(@advrno)) + ''',''' + 'C' + ''',''' +  convert(varchar(20),@txndt) + ''',''C'',0)'
  exec(@sql)
  exec pbpv1u @t1
  end
 else
 if @namt-@amount > 0 
  begin
  set @sql = 'insert into ' + @t1 + ' (company,actnum,amt,partic,amttyp,txndt,txntyp,fdrsno) values (1,' + str(@post_to) +','+ str(@namt-@amount) + ',' + '''Cash received-adv.Receipt no ' + ltrim(str(@advrno)) + ''',' + '''D''' + ',''' +  convert(varchar(20),@txndt) + ''',''C'',0)'
  exec(@sql)
  exec pbpv1u @t1
  end
end
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()

set @sql =  'Final Bill Advance  Modified Bill No.' + ltrim(str(isnull(@bno,0))) + ' GRNO ' +ltrim(str(isnull(@eno,0))) + ' Guest Name-' +ltrim(rtrim( isnull(@guestname,' NIL '))) 
+ ' Old Advance  ' + convert(varchar(20),isnull(@oad,0)) + ' New ' + convert(varchar(20),isnull(@namt,0))  +  ' Advance Receipt No. ' + ltrim(str(@advrno))+ ' dt ' + convert(varchar(20),@txndt,6) + ' after checkout'
insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm,@sql ,0,@gno)
if @@error<>0 
begin
print 'Error in updating accesslog'
goto e_error
end
commit transaction
return
e_error:
rollback transaction
raiserror('Deletion of advance entry failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spedmask]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spedmask    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spedmask] @cno numeric,@gno numeric ,@t1 varchar(50) out,@t2 varchar(50) out  
/* with encryption */
AS
begin transaction
--print 'periya'
declare @split char(1)
declare @mainbilldate datetime
declare @sql1 varchar(5000)
declare @grno int
declare @fy int
declare @txndt datetime
declare @outlet as int
declare @ret_val int
declare @sql varchar(5000)
declare @rent money
declare @discount money
declare @item1 money
declare @item2 money
declare @item3 money
declare @item4 money
declare @item5 money
declare @item6 money
declare @item7 money
declare @item8 money
declare @item9 money
declare @item10 money
declare @outlet1 int
declare @outlet2 int
declare @outlet3 int
declare @outlet4 int
declare @outlet5 int
declare @outlet6 int
declare @outlet7 int
declare @outlet8 int
declare @outlet9 int
declare @outlet10 int
declare @sc money
declare @lt money
declare @total money
declare @bp  int
declare @bp1 int
declare @bp2 int
declare @selfdeal char(1)
declare @bcompany int
declare @section char(1)
DECLARE @MOP INT
DECLARE @CRNAME VARCHAR(50)
DECLARE @CRNUM VARCHAR(20)
DECLARE @CRTYPE VARCHAR(50)
DECLARE @GLACTNO INT
declare @channel int
DECLARE @RV AS VARCHAR(10)
declare @dtfound int
DECLARE @OLNM1 CHAR(10)
DECLARE @OLNM2 CHAR(10)
DECLARE @OLNM3 CHAR(10)
DECLARE @OLNM4 CHAR(10)
DECLARE @OLNM5 CHAR(10)
DECLARE @OLNM6 CHAR(10)
DECLARE @OLNM7 CHAR(10)
DECLARE @OLNM8 CHAR(10)
DECLARE @OLNM9 CHAR(10)
DECLARE @OLNM10 CHAR(10)
declare @m_txndt datetime
declare @m_fy int
declare @bno int
declare @m_grno int
declare @m_year int
declare @m_bofy datetime
DECLARE @MBDATE DATETIME 
declare @ctrlno int
declare @spldisc money
declare @foodbill money
declare @taxable money
declare @lrate money
declare @cess money
declare @cess2 money 
declare @extrabed money
declare @exbedgst money
declare @food money
declare @foodgst money 
EXEC RANDFILE @RV OUT
--raiserror('rRARARasr',16,-1)
select @t1 = 'XXXBL' + ltrim(@RV)
SELECT @SQL1 =  'if exists (select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'' ) drop table ' + @T1
EXEC(@sql1) ---
IF @@ERROR <> 0 GOTO E_ERROR
SET @SQL = 'SELECT * INTO ' + @T1 + '  FROM  DUMMYBILL'
EXEC (@SQL)
IF @@ERROR <> 0 GOTO E_ERROR
set @t2 = 'XXXfbpm' + ltrim(@RV)
SET @SQL1 = 'if exists (select name,xtype from sysobjects where name = ''' + @T2 +''' and xtype = ''U'') drop table ' + @T2
EXEC (@SQL1) ----
IF @@ERROR <> 0 GOTO E_ERROR
SET @SQL = 'SELECT * INTO ' + @T2 + '  FROM  DUMMYPYMT'
EXEC (@SQL)
IF @@ERROR <> 0 GOTO E_ERROR
select @foodbill=foodbill,@spldisc=spldiscount,@crname=crname,@crnum=crnum,@crtype=crtype,@glactno=glactno,@mop=mop,@split=split,@bcompany=company from billhist1 where cno=@cno
set @sql = 'insert into ' + @t2 + '  (COMPANY,CRNAME,CRNUM,CRTYPE,GLACTNO,MOP,SECTION,spldiscount,foodbill) VALUES (' + str(@bcompany)+  ',''' + @CRNAME + ''',''' + @CRNUM + ''',''' + @CRTYPE + ''',' + STR(@GLACTNO) + ',' + STR(@MOP) + ', '''+ @split + ''',' + convert(varchar(20),@spldisc)+ ','+ convert(varchar(20),@foodbill)+ ' )'
exec (@sql)
if @@error <> 0  goto e_error
  declare tempc cursor for select TXNDT,DISCOUNT,RENT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,
  OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10,SC,LT,TOTAL,ctrlno,taxable,lrate,cess,cess2,extrabed,exbedgst,food,foodgst
  from billhist where  CNO=@CNO and gno=@gno
  open tempc
  fetch next from tempc into @TXNDT,@DISCOUNT,@RENT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,
  @OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10,@SC , @LT,@TOTAL ,@ctrlno,@taxable,@lrate,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
  while @@fetch_status=0
  begin
  set @sql = 'insert into ' +@t1 + ' (TXNDT,DISCOUNT,RENT,ITEM1,ITEM2,ITEM3,ITEM4,ITEM5,ITEM6,ITEM7,ITEM8,ITEM9,ITEM10,
  OUTLET1,OUTLET2,OUTLET3,OUTLET4,OUTLET5,OUTLET6,OUTLET7,OUTLET8,OUTLET9,OUTLET10,SC,LT,TOTAL,billorder,section ,ctrlno,taxable,lrate,cess,cess2) values (''' + 
  convert(varchar(20),@TXNDT) + ''',' + convert(varchar(20),@DISCOUNT)+ ',' + convert(varchar(20),@RENT)+ ',' +
  convert(varchar(20),@ITEM1) + ',' + convert(varchar(20),@ITEM2) + ',' + 
  convert(varchar(20),@ITEM3) + ',' + convert(varchar(20),@ITEM4) + ',' + 
  convert(varchar(20),@ITEM5) + ',' + convert(varchar(20),@ITEM6) + ',' + 
  convert(varchar(20),@ITEM7) + ',' + convert(varchar(20),@ITEM8) + ',' + 
  convert(varchar(20),@ITEM9) + ',' + convert(varchar(20),@ITEM10) + ',' + 
  str(@OUTLET1) + ',' + str(@OUTLET2) + ',' + str(@OUTLET3) + ',' + str(@OUTLET4) + ',' + str(@OUTLET5) + ',' + 
  str(@OUTLET6) + ',' + str(@OUTLET7) + ',' + str(@OUTLET8) + ',' + str(@OUTLET9) + ',' + str(@OUTLET10) + ',' + 
  convert(varchar(20),@SC) +' ,' + convert( varchar(20),@LT) +',' + convert(varchar(20),@TOTAL) + ' ,' + stR( @bcompany) +',''' + @split + ''',' + str(@ctrlno)  + ','+convert(varchar(20),isnull(@taxable,0))+ ','+convert(varchar(20),isnull(@lrate,0)) + ','+convert(varchar(20),isnull(@cess,0)) + ','+convert(varchar(20),isnull(@cess2,0))  +     ')'
--print @t1 + ' other file ' + @t2  + str(@cno)
  exec (@sql)
if @@error <> 0  begin
    --print 'periya'
    goto e_error
end
--print @t1
--print convert(varchar(20),@item2)
--print substring(@sql,200,1000)
  fetch next from tempc into @TXNDT,@DISCOUNT,@RENT,@ITEM1,@ITEM2,@ITEM3,@ITEM4,@ITEM5,@ITEM6,@ITEM7,@ITEM8,@ITEM9,@ITEM10,
  @OUTLET1,@OUTLET2,@OUTLET3,@OUTLET4,@OUTLET5,@OUTLET6,@OUTLET7,@OUTLET8,@OUTLET9,@OUTLET10,@SC , @LT,@TOTAL ,@ctrlno,@taxable,@lrate,@cess,@cess2,@extrabed,@exbedgst,@food,@foodgst
  end
--print 'periya'
close tempc
deallocate tempc
commit transaction
return
e_error:
rollback transaction
SELECT @SQL1 =  'if exists (select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'' ) drop table ' + @T1
--EXEC(@sql1) ---
SET @SQL1 = 'if exists (select name,xtype from sysobjects where name = ''' + @T2 +''' and xtype = ''U'') drop table ' + @T2
--EXEC (@SQL1) ----
raiserror('falied',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[speptr]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.speptr    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[speptr] @ctrlno int,@gno int 
/* with encryption */
AS
--print 'periya'
declare @number varchar(50)
declare @amt money
declare @t1 varchar(50)
declare @sql varchar(500)
declare @rv varchar(50)
declare @extn char(4)
declare @outlet int
declare @ocno int
declare @nextn char(4)
begin transaction
EXEC RANDFILE @RV OUT
select @t1 = 'tl' + ltrim(@RV)
SELECT @SQL=  'if exists(select name,xtype from sysobjects where name = ''' + @T1 +''' and xtype = ''U'') drop table ' + @T1
EXEC(@sql) 
if @@error<>0 goto e_error
SET @SQL = 'SELECT * INTO ' + @T1 + '  FROM  DUMMYph'
EXEC (@SQL)
if @@error<>0 goto e_error
if @gno<>0
begin
 if not exists(select * from room where gno=@gno)
 begin
 print 'Room seems to be vaccant'
 goto e_error
 end
end
 select @extn=e_extn,@number=phone_num,@amt=amount from epbx where ctrlno = @ctrlno
declare @status char(1)
if @gno=0 set @status='A' else set @status=' '
 update epbx set status=@status ,gno=@gno where ctrlno=@ctrlno
 if @@error <> 0 goto e_error
if @gno<>0 begin
 select @nextn=extn from room where gno=@gno
 update epbx set e_extn=@nextn where ctrlno=@ctrlno
 if @@error <> 0 goto e_error
end
 set @sql = 'insert into ' + @t1 + ' (detail,amount) values (''' + ltrim(@number) + ' transfered a/c-' + ltrim(str(@ctrlno)) + ' '' , ' + convert(varchar(20),@amt) + ')'
 exec(@sql)
 if @@error <> 0 goto e_error
 if substring(@number,1,1) = '0' 
    select @outlet=outlet from sptbph where calltype=2
 else
    select @outlet=outlet from sptbph where calltype=1
declare @mop int
if  @gno=0 set @mop=1 else set @mop=2
 set @sql =  'declare @ocno int exec pbpp1u ''' + @t1 + ''' ,' + str(@outlet) + ',''' + convert(varchar(20),getdate()) + ''',' + str(@mop)   +',0, ' + str(@gno) + ','''','''','''',0, @ocno out'
 exec(@sql)
 if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('eav failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spexro]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spexro    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spexro]  @gno numeric ,@txndt datetime , @head numeric ,@rno  numeric 
/* with encryption */
as
--print 'periya karuppa'
declare @exbed money
declare @capacity numeric
declare @everyex numeric
declare @exchgs money
declare @amt money
declare @oldamt money
declare @rv varchar(20)
declare @sql varchar(500)
declare @t1 varchar(50)
declare @sttableno int
declare @strate money
select @exbed=exbed,@capacity=capacity,@everyex=everyex from room where rno=@rno
if @head <= @capacity  return
if @exbed=0 return
if @everyex=0 return
set @head=round((@head-@capacity)/@everyex,0)
set @exchgs=@head*@exbed
if exists (select * from exbed where gno=@gno and datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@txndt) and amount=@exchgs)
return
if exists (select * from exbed where gno=@gno and datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@txndt))
 begin
 select @oldamt=amount from exbed where gno=@gno and datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@txndt)
 set @amt=@exchgs-@oldamt
 end
else
 set @amt=@exchgs
if @amt <=0 return

begin transaction

exec randfile @rv out
select @t1 = 'tl' + ltrim(@RV)
declare @exbedsplit int
set @sql = 'if exists (select name,xtype from sysobjects where name = ''' + @t1 + ''' and xtype = ''U'') drop table ' + @t1
exec(@sql)
set @sql = 'select * into ' + @t1 + ' from dummyph'
exec(@sql)
if @@error<>0 goto e_error
declare @outlet int
declare @narration varchar(100)
select top 1 @outlet=outlet,@narration=narration from exbedco
--print 'Error in inserting ' + @t1 + isnull(@narration,'testing')
set @sql = 'insert into ' + @t1 + ' (detail,amount) values (''' + @narration + ''' ,'  + convert(varchar(20),@amt) + ')'
exec(@sql)
if @@error <> 0 goto e_error

-- 23 July 2017 Begin  
select @sttableno=sttableno from PHoutlet where OUTLET=@outlet
select  @strate=rate from sttabler where sttableno=@STTableNo and getdate() between sdt and edt and @amt between samt and eamt
-- 23 July 2017 End

select top 1 @exbedsplit=exbedsplit from setting
set @sql =  'declare @ocno int exec pbpp1u  ''' + @t1 + ''' ,' + str(@outlet) + ',''' + convert(varchar(20),@txndt ) + ''',2,0, ' + str(@gno) + ','''','''','''',0, @ocno out,0,0,' + str(@strate) +',' + convert(varchar(20),(@amt*@strate/100)) + ',' + str(@exbedSPLIT )
exec(@sql)
if @@error <> 0 goto e_error
if exists (select * from exbed where gno=@gno and datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@txndt))
update exbed set amount=amount+@amt where gno=@gno and  datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@txndt)
else
insert into exbed (gno,txndt,amount) values (@gno,@txndt,@amt)

set @sql = 'if exists (select name,xtype from sysobjects where name = ''' + @t1 + ''' and xtype = ''U'') drop table ' + @t1
exec(@sql)

commit transaction
return
e_error:
rollback
raiserror(@sql,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spexroRes]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE .[dbo].[spexroRes]  @gno numeric ,@txndt datetime
/* with encryption */
AS
begin
--print 'periya karuppa'
declare @exchgs money
declare @amt money
declare @rv varchar(1000)
declare @t1 varchar(1200)
declare @sql varchar(8000)
select @exchgs=extrabedcharges from guest where gno=@gno

if exists (select * from exbed where gno=@gno and datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@txndt) and amount=@exchgs)
return
set @amt=@exchgs
if @amt <=0 return

begin transaction

exec randfile @rv out
select @t1 = 'tl' + ltrim(@RV)
set @sql = 'if exists (select name,xtype from sysobjects where name = ''' + @t1 + ''' and xtype = ''U'') drop table ' + @t1
exec(@sql)
set @sql = 'select * into ' + @t1 + ' from dummyph'
exec(@sql)
if @@error<>0 goto e_error
declare @outlet int
declare @narration varchar(100)
select top 1 @outlet=outlet,@narration=narration from exbedco
print 'Error in inserting ' + @t1 + isnull(@narration,'testing')
set @sql = 'insert into ' + @t1 + ' (detail,amount) values (''' + @narration + ''' ,'  + convert(varchar(20),@amt) + ')'
exec(@sql)
if @@error <> 0 goto e_error
--set @sql = 'select * from ' +  @t1
--exec (@sql)
--commit transaction
--return
--goto e_error
set @sql =  'declare @ocno int exec pbpp1u  ''' + @t1 + ''' ,' + str(@outlet) + ',''' + convert(varchar(20),@txndt ) + ''',2,0, ' + str(@gno) + ','''','''','''',0, @ocno out'
exec(@sql)
if @@error <> 0 goto e_error
if exists (select * from exbed where gno=@gno and datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@txndt))
update exbed set amount=amount+@amt where gno=@gno and  datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@txndt)
else
insert into exbed (gno,txndt,amount) values (@gno,@txndt,@amt)

set @sql = 'if exists (select name,xtype from sysobjects where name = ''' + @t1 + ''' and xtype = ''U'') drop table ' + @t1
exec(@sql)

commit transaction
return
e_error:
rollback
raiserror('error in extra bed',16,-1)
return

END

GO
/****** Object:  StoredProcedure [dbo].[spglno]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spglno    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spglno] @company int ,@fy int ,@name varchar(50) , @asset char(1), @onac char(1),@flag int, @grp int ,@add1 varchar(50),@add2 varchar(50), @city varchar(50), @phone varchar(50),@email varchar(50),@glactno numeric out,@deletable char(1)='N',@gstin nvarchar(50)='', @pan nvarchar(50)= ''
/* with encryption */
AS
begin transaction 
declare @actno numeric
declare @tdno numeric
declare @opgdate datetime
DECLARE @MSG VARCHAR(6000)
--SET @MSG = @GSTIN GOTO E_ERROR
select @opgdate=(date) from finyear where fy=@fy
set @actno=0
select @actno =max(cgp_actno) from glprimld where company = @company
if @actno <1000
   set @actno = 1001
else
  set @actno=@actno+1
insert glprimld (company,fy,cgp_desc,asset,onac,flag,cgp_actno,grp,add1,add2,city,phone,deletable,email,gstin,pan) values (@company,@fy,@name,@asset,@onac,@flag,@actno,@grp,@add1,@add2,@city,@phone,@deletable,@email,@gstin,@pan)
if @@error<>0 goto e_error
set @glactno = @actno
commit transaction
return
e_error:
rollback transaction
SET @MSG = @MSG + ' error in opening of account'
raiserror(@MSG,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[SPHSNOCODE]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[SPHSNOCODE] 
@SDT DATETIME,@EDT DATETIME,@GSTIN INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE  @SQL VArchar(2000)

IF @GSTIN=1
	BEGIN
	select lrate,hsncode='996311', name='Room Rent',count(*) COUNT1,	sum(servc) servc , sum(rent) rent,sum(lt) lt  INTO #DUMMYHSN 	from (
	select max(lrate) lrate,bno,min(txndt) txndt,sum(-spldiscount+rent+otherchgs) total,sum(servc) servc, 
	sum(rent) rent ,sum(lt) lt from
	(select cno,bno,txndt,spldiscount, (select sum(item1+item2+item3+item4+item5
	+item6+item7+item8+item9+item10+lt+sc+otherchgs
	) from billhist where billhist.cno=billhist1.cno) servc ,
	(select sum(rent+EXTRABED+FOOD) from billhist where billhist.cno=billhist1.cno) rent ,
	(select sum(lt+exbedgst+foodgst) from billhist where billhist.cno=billhist1.cno) lt ,
    isnull((select sum(amount) from roomdues where gno in 
	(select gno from billhist  where cno=billhist1.cno)),0) otherchgs,
	(select max(lrate) from billhist where billhist.cno=billhist1.cno) lrate
	from billhist1 
	where company=1 and split='N' and gstin <> ''
	and datediff(d,'1/1/2000',billhist1.txndt)
	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT)
	) a group by bno   
	) b group by lrate


UNION 

	SELECT lrate,min(sac) sac,min(name) name, COUNT(*) COUNT1,0, SUM(AMOUNT),sum(gst) FROM (
	select '996331' sac,'No Show Charges' name,NOSHOW AMOUNT,GST,dbo.getnoshowgstrate(txndt) lrate 
	from SPTBRS_NOSHOW join sptbrs on sptbrs.ctrlno=sptbrs_noshow.ctrlno
	WHERE GSTIN <> '' and 	  DATEDIFF(D,'1/1/2000',TXNDT) 	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT)
	) A GROUP BY lrate,SAC,NAME

union

	SELECT lrate,SAC,name, COUNT(*) COUNT1,0, SUM(AMOUNT),sum(stamt) FROM 
	(select sac,phoutlet.name, 
	finalamt-STAMT AMOUNT,stamt,dbo.getsttabler(sttableno,finalamt,txndt) lrate from phone join phoutlet on phOUTLET.outlet=phone.outlet 
	WHERE GSTIN <> '' AND SAC <> ' ' and split in ('N','Y')
	and company=1 AND DATEDIFF(D,'1/1/2000',TXNDT)
	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT)
	) A GROUP BY lrate,SAC,NAME
	

	UNION 

SELECT lrate,SAC,name ,COUNT(*) COUNT1, 0,SUM(AMOUNT),sum(st) FROM 
(select sac,outlet.name,dbo.getsttabler(sttableno,txb,txndt) lrate,finalamt-st amount,txb ,st from kithist1  
join (select sum(qty*rate) txb, sum(st) st,bno from kithist group by bno) kithist on kithist.bno=kithist1.bno
join (select code,name,sac,sttableno from outlet) outlet on kithist1.outlet=outlet.code 
WHERE pmode<>6 and GSTIN <> '' and sac <> '' aND split in ('N','Y') and DATEDIFF(D,'1/1/2000',TXNDT)
between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT)
and outlet in (select code from outlet where channel in (select channel from 
restco where company=1))  and  dbo.vateligible(outlet,txndt)=0 
) a group by lrate,sac,name

	UNION 
	--996334
	SELECT lrate, sac ,'BANQUET' ,COUNT(*),0, SUM(BNQ1+BNQ2) TOTAL,sum(st) st  FROM (
	SELECT SAC='998553',CTRLNO,BILLDATE,BILLNO,BNQ1,BNQ2,STFOOD,STBNQ,
	BNQ1+STBNQ+0  TOTAL,stfood+stbnq st 
	,dbo.getbnqgstrateforbanquetcharges(bnqno,billdate) lrate
	FROM BANQUET WHERE GSTIN <> '' AND STATUS='B' and bnq1<>0
	AND DATEDIFF(D,'1/1/2000',BILLDATE)
	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT) 
	) A group by lrate,sac

	UNION 
	--996334
	SELECT lrate, sac ,'BANQUET' ,COUNT(*),0, SUM(BNQ1+BNQ2) TOTAL,sum(st) st  FROM (
	SELECT SAC='996334',CTRLNO,BILLDATE,BILLNO,BNQ1,BNQ2,STFOOD,STBNQ,
	BNQ2+STfood+0  TOTAL,stfood+stbnq st 
	,dbo.getbnqgstrate(bnqno,billdate) lrate
	FROM BANQUET WHERE GSTIN <> '' AND STATUS='B' and bnq2<>0
	AND DATEDIFF(D,'1/1/2000',BILLDATE)
	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT) 
	) A group by lrate,sac


	SELECT lrate,HSNCODE,UPPER(name) name,SUM(COUNT1) COUNT1, SUM(servc) servc ,sum(rent) rent ,
	sum(lt)  lt
	FROM #DUMMYHSN 
	GROUP BY lrate,HSNCODE,NAME
	ORDER BY HSNCODE
END
ELSE
BEGIN
	select lrate,hsncode='996311', name='Room Rent',count(*) COUNT1,	sum(servc) servc , sum(rent) rent,sum(lt) lt into #dummyhsn1 from (
	select max(lrate) lrate,bno,min(txndt) txndt,sum(-spldiscount+rent+otherchgs) total,sum(servc) servc, 
	sum(rent) rent ,sum(lt) lt from
	(select cno,bno,txndt,spldiscount, (select sum(item1+item2+item3+item4+item5
	+item6+item7+item8+item9+item10+lt+sc+otherchgs
	) from billhist where billhist.cno=billhist1.cno) servc ,
	(select sum(rent+EXTRABED+FOOD) from billhist where billhist.cno=billhist1.cno) rent ,
	(select sum(lt+exbedgst+foodgst) from billhist where billhist.cno=billhist1.cno) lt ,
    isnull((select sum(amount) from roomdues where gno in 
	(select gno from billhist  where cno=billhist1.cno)),0) otherchgs,
	(select max(lrate) from billhist where billhist.cno=billhist1.cno) lrate
	from billhist1 
	where company=1 and split = 'N' and gstin = ''
	and datediff(d,'1/1/2000',billhist1.txndt)
	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT)
	) a group by bno   
	) b group by lrate


UNION 


	SELECT lrate,min(sac) sac,min(name) name, COUNT(*) COUNT1,0, SUM(AMOUNT),sum(gst) FROM (
	select '996331' sac,'No Show Charges' name,NOSHOW AMOUNT,GST,dbo.getnoshowgstrate(txndt) lrate 
	from SPTBRS_NOSHOW join sptbrs on sptbrs.ctrlno=sptbrs_noshow.ctrlno
	WHERE GSTIN = '' and 	  DATEDIFF(D,'1/1/2000',TXNDT) 	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT)
	) A GROUP BY lrate,SAC,NAME

union
	SELECT lrate,SAC,name, COUNT(*) COUNT1,0, SUM(AMOUNT),sum(stamt) FROM 
	(select sac,phoutlet.name, 
	finalamt-STAMT AMOUNT,stamt,dbo.getsttabler(sttableno,finalamt,txndt) lrate from phone join phoutlet on phOUTLET.outlet=phone.outlet 
	WHERE GSTIN = '' AND SAC <> ' ' and split in ('N','Y')
	and company=1 AND DATEDIFF(D,'1/1/2000',TXNDT)
	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT)
	) A GROUP BY lrate,SAC,NAME
	

	UNION 

SELECT lrate,SAC,name ,COUNT(*) COUNT1, 0,SUM(AMOUNT),sum(st) FROM 
(select sac,outlet.name,dbo.getsttabler(sttableno,txb,txndt) lrate,finalamt-st amount,txb ,st from kithist1  
join (select sum(qty*rate) txb, sum(st) st,bno from kithist group by bno) kithist on kithist.bno=kithist1.bno
join (select code,name,sac,sttableno from outlet) outlet on kithist1.outlet=outlet.code 
WHERE  pmode<>6 and GSTIN = '' and sac <> '' aND split in ('N','Y') and  DATEDIFF(D,'1/1/2000',TXNDT)
between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT)
and outlet in (select code from outlet where channel in (select channel from 
restco where company=1))   and  dbo.vateligible(outlet,txndt)=0 
) a group by lrate,sac,name

	--UNION 

	--SELECT lrate, sac ,'BANQUET' ,COUNT(*),0, SUM(BNQ1+BNQ2) TOTAL,sum(st) st  FROM (
	--SELECT SAC='996334',CTRLNO,BILLDATE,BILLNO,BNQ1,BNQ2,STFOOD,STBNQ,
	--BNQ1+BNQ2+STFOOD+STBNQ+0  TOTAL,stfood+stbnq st 
	--,dbo.getbnqgstrate(bnqno,billdate) lrate
	--FROM BANQUET WHERE GSTIN = '' AND STATUS='B'
	--AND DATEDIFF(D,'1/1/2000',BILLDATE)
	--between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT) 
	--) A group by lrate,sac



		UNION 
	--996334
	SELECT lrate, sac ,'BANQUET' ,COUNT(*),0, SUM(BNQ1+BNQ2) TOTAL,sum(st) st  FROM (
	SELECT SAC='998553',CTRLNO,BILLDATE,BILLNO,BNQ1,BNQ2,STFOOD,STBNQ,
	BNQ1+STBNQ+0  TOTAL,stfood+stbnq st 
	,dbo.getbnqgstrateforbanquetcharges(bnqno,billdate) lrate
	FROM BANQUET WHERE GSTIN = '' AND STATUS='B' and bnq1<>0
	AND DATEDIFF(D,'1/1/2000',BILLDATE)
	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT) 
	) A group by lrate,sac

	UNION 
	--996334
	SELECT lrate, sac ,'BANQUET' ,COUNT(*),0, SUM(BNQ1+BNQ2) TOTAL,sum(st) st  FROM (
	SELECT SAC='996334',CTRLNO,BILLDATE,BILLNO,BNQ1,BNQ2,STFOOD,STBNQ,
	BNQ2+STfood+0  TOTAL,stfood+stbnq st 
	,dbo.getbnqgstrate(bnqno,billdate) lrate
	FROM BANQUET WHERE GSTIN = '' AND STATUS='B' and bnq2<>0
	AND DATEDIFF(D,'1/1/2000',BILLDATE)
	between datediff(d,'1/1/2000',@SDT) and datediff(d,'1/1/2000',@EDT) 
	) A group by lrate,sac



	SELECT lrate,HSNCODE,UPPER(name) name,SUM(COUNT1) COUNT1, SUM(servc) servc ,sum(rent) rent ,
	sum(lt)  lt
	FROM #DUMMYHSN1 
	GROUP BY lrate,HSNCODE,NAME
	ORDER BY HSNCODE
END

return

END

GO
/****** Object:  StoredProcedure [dbo].[spkbl]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spkbl    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spkbl] @outlet int,@type int,@code int,@drate money,@scrate money , @bno int out,@USERNM VARCHAR(20)='SYS' , @sessionid varchar(200) = ''
/* with encryption */
AS
declare @txndt datetime
declare @year int
declare @bofy datetime
declare @qty int
declare @rate money
declare @total money
declare @tax money
declare @disc money
declare @roamt money
declare @scset int
declare @stset int
--declare @split char(1)
declare @sno int
declare @amt money
declare @strate money
declare @scamt money
declare @total1 money
declare @wno int
declare @ratetype int
declare @selfdeal char(1)
declare @g_staxincl char(1)
select @txndt=curdate,@g_staxincl=g_staxincl from outlet where code=@outlet
set @bofy = dbo.getbofy(@txndt)
set @year = datepart(yy,@bofy)


--print 'periya'
begin transaction
exec  GetServiceASno @outlet,@txndt,@sno out

SELECT @scset=scset,@stset=stset,@selfdeal=selfdeal
FROM CO_REST_OL  WHERE OCODE = @OUTLET
select @total=0
declare tempcur cursor for select qty,rate,strate from currentkot where outlet=@outlet and pend_type=@type and pend_code=@code
open tempcur
fetch next from tempcur into @qty,@rate,@strate
while @@fetch_status=0
begin
if @g_staxincl='Y'
   begin   
        update currentkot set rate=  (@rate*100) / (100 +@strate ) where current of tempcur
        select @rate= (@rate*100)/(100+@strate)
   end
select @amt = @qty*@rate
select @disc= @amt*@drate/100
if @stset=0
  select @tax=round(@amt*@strate/100,2)
else
  select @tax=round((@amt-@disc)*@strate/100,2)
if @scset=0
  select @scamt=round(@amt*@scrate/100,2)
else
  select @scamt=round((@amt-@disc)*@scrate/100,2)
update currentkot set damt=@disc,drate=@drate,scrate=@scrate,scamt=@scamt,tax=@tax where current of tempcur
select @total =( @total + @amt + @tax + @scamt) - @disc
fetch next from tempcur into @qty,@rate,@strate
end
close tempcur
deallocate tempcur
if exists(select * from outlet where code=@outlet and roundoff='Y')
begin
select @roamt = @total  - floor(@total)
if @roamt <  .5 set @roamt = @roamt*-1 else set @roamt= round(@total,0) - @total
end
else
select @roamt=0
update currentkot set roamt=@roamt,nett=floor(@total) where outlet=@outlet and pend_type=@type and pend_code=@code
select top 1 @ratetype=ratetype,@wno=waiter from currentkot where outlet=@outlet and pend_type=@type and pend_code=@code
INSERT KITHIST1 (DRATE,TXNDT,OUTLET,SNO,ROAMT,RATETYPE,SCAMT,SCRATE,STSET,SCSET,wno,txntime,selfdeal,USERNM,finalamt,TNO)  VALUES
(@DRATE,@TXNDT,@OUTLET,@SNO,@ROAMT,@RATETYPE,@SCAMT,@SCRATE,@STSET,@SCSET,
@wno,convert(varchar(5),getdate(),8),@selfdeal,@USERNM,floor(@total)+@roamt,@CODE)
IF @@ERROR<>0 GOTO E_ERROR
select @bno = @@identity
update currentkot set sno=@sno,bno=@bno where outlet=@outlet and pend_type=@type and pend_code=@code
--SELECT @BNO=MAX(BNO) FROM KITHIST1 WHERE OUTLET= @OUTLET 
commit transaction
return @bno
e_error:
rollback transaction
raiserror('error spkbl',16,-1)
return 0




GO
/****** Object:  StoredProcedure [dbo].[spkblnew]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spkbl    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spkblnew] @type int,@code varchar(50),@temp varchar(100),@temp1 varchar(100),@pax int=1,@tempcoupon varchar(100)='' ,@usernm varchar(50)='SYS',@packingchgs money = 0 
, @gstin nvarchar(50) = '' ,@guestname nvarchar(50)= ''
/* with encryption */
AS
declare @bno numeric
declare @scrate money
declare @drate money
declare @outlet numeric
declare @txndt datetime
declare @year int
declare @bofy datetime
declare @qty int
declare @rate money
declare @total money
declare @tax money
declare @disc money
declare @roamt money
declare @scset int
declare @stset int
--declare @split char(1)
declare @sno int
declare @amt money

declare @saleamt money
declare @sctotal money
declare @disctotal money

declare @strate money
declare @scamt money
declare @total1 money
declare @wno int
declare @ratetype int
declare @selfdeal char(1)
declare @g_staxincl char(1)
declare @sql varchar(8000)
declare @sc_table tinyint
declare @sc_room tinyint
declare @sc_door tinyint
declare @commissionrate numeric -- given to agents
declare @guestdiscount numeric -- given to agents
declare @duedt datetime --- coupon duedt
declare @agentcode numeric
declare @couponcode numeric
declare @scadd money
declare @g_scvat tinyint 
declare @servicetax money
declare @cess money
declare @cess2 money
declare @servicetaxeligible char(1)
declare @stonsc int
declare @couponamt decimal(18,2)
declare @servicetax1 money
declare @cess1 money
declare @cess21 money
declare @stadd money
declare @vatonst tinyint
declare @g_kotfinaltime int
declare @cscrate money
declare @showsc int
declare @taxorgst int
declare @itemcode numeric
select @g_kotfinaltime=g_kotfinaltime from setting
select @g_scvat=g_scvat from (select top 1 * from setting) a
set @scrate = 0
set @cscrate = 0
set @saleamt = 0
set @sctotal = 0
set @disctotal = 0

set @sql  = 'truncate table ' + @temp1
exec(@sql)
----select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
begin transaction
set @servicetax=0
set @cess = 0
set @servicetax1=0
set @cess1 = 0
set @cess2 = 0
set @cess21 = 0
	if cursor_status('global','tempcur1')  >= 0 	begin  	close tempcur1 	deallocate tempcur1 	end
	declare tempcur1 cursor for select distinct outlet from currentkot where pend_type=@type and pend_code=@code
	open tempcur1
	fetch next from tempcur1 into @outlet
	while @@fetch_status=0
		begin
			set @servicetax=0
			set @cess = 0
			set @servicetax1=0
			set @cess1 = 0
			set @cess2 = 0
			set @cess21 = 0

			if cursor_status('global','dr') >=0  	begin 	close dr deallocate dr 	end
			set @sql = 'declare dr cursor for select discount,couponamt  from ' + @temp + ' where outlet = ' + str(@outlet) 
			exec(@sql)
			open dr
			fetch next from dr into @drate,@couponamt
			if @@fetch_status=0
			begin
				set @couponamt = isnull(@couponamt,0)
				select @txndt=curdate,@g_staxincl=g_staxincl,@vatonst=vatonst from outlet where code=@outlet
				set @bofy = dbo.getbofy(@txndt)
				set @year = datepart(yy,@bofy)
				exec  GetServiceASno @outlet,@txndt,@sno out
				select @scset=scset,@stset=stset,@selfdeal=selfdeal,@scrate=scrate,
				@sc_table=sc_table,@sc_room=sc_room,@sc_door=sc_door,@servicetaxeligible=servicetax,@vatonst=vatonst,
				@showsc=showsc
				from outlet  where code = @outlet

				select @total= 0
				set @saleamt = 0
				set @sctotal = 0
				set @disctotal = 0
				declare tempcur cursor for select code,qty,rate,strate,scrate from currentkot where outlet=@outlet and pend_type=@type and pend_code=@code
				open tempcur
				fetch next from tempcur into @itemcode,@qty,@rate,@strate,@Cscrate
				while @@fetch_status=0
				begin
				-----
				select @taxorgst=taxorgst from kitprime where icode=(select icode from kitmast where code=@itemcode)
				select @tax=0
				select @amt = @qty*@rate
				select @disc= @amt*@drate/100
				select @saleamt = @saleamt + @amt
				select @disctotal = @disctotal + @disc

				set @scamt=0
				--if (@type=1 and @sc_table=1) or (@type=2 and @sc_room=1) or (@type=3 and @sc_door=1)
				if @showsc=1 set @scrate=@cscrate
				if @scset=0
					select @scamt=round(@amt*@scrate/100,2)
				else
					select @scamt=round((@amt-@disc)*@scrate/100,2)


				set @sctotal = @sctotal + @scamt

				if @g_scvat=1 
					set @scadd = @scamt
				else

				set @scadd = 0
				set @cess21 = 0
				set @servicetax1 = 0
				set @cess1 = 0
				set @stadd=0
				declare @stable money
				if @taxorgst=0
					begin
						if @servicetaxeligible = 'Y'
							begin
								select @stonsc=g_stonsc from setting
								if @stonsc=1 
										select @stable = @amt + @scamt - @disc
								else
										select @stable = @amt  - @disc 
								exec getservicetaxr @stable ,@txndt ,@servicetax1 out ,@cess1 out,@outlet ,1000,@cess21 out
								set @servicetax = @servicetax + @servicetax1
								set @cess = @cess + @cess1
								set @cess2 = @cess2 + @cess21
							end
						if @vatonst= 1 
							set @stadd = @servicetax1+@cess1
						else
							set @stadd = 0
					end
				else
					begin
								select @stable = @scamt
								exec getservicetaxr @stable ,@txndt ,@servicetax1 out ,@cess1 out,@outlet ,1000,@cess21 out
								set @servicetax = @servicetax + @servicetax1
								set @cess = @cess + @cess1
								set @cess2 = @cess2 + @cess21
					end

				if @stset=0
					select @tax=round((@amt + @scadd + @stadd)*@strate/100,2)
				else
					select @tax=round(((@amt + @scadd + @stadd) - @disc)*@strate/100,2)
				
				/* Service Charges Calculation according to the Mode of the bill modified on 27/07/2009   further modified on 13 Marc 2018***/
				update currentkot set damt=@disc,drate=@drate,scrate=@scrate,scamt=0,tax=@tax,st1=@servicetax1,cess1=@cess1,sc1=@scamt,cess21=@cess21  where current of tempcur
				if @@error<>0 begin print 'Error in Updating CurrentKOT2' goto e_error end
				select @total =( @total + @amt + @tax + @scamt + @servicetax1 + @cess1 + @cess21) - @disc
				fetch next from tempcur into @itemcode,@qty,@rate,@strate,@showsc
				end
				close tempcur
				deallocate tempcur
-----------------------------------------------------

				if exists(select * from outlet where code=@outlet and roundoff='Y')
					begin
					select @roamt = @total  - floor(@total)
					if @roamt <  .5 set @roamt = @roamt*-1 else set @roamt= round(@total,0) - @total
					end
				else
					select @roamt=0

					--print 'roamt is ' + convert(varchar(20),@roamt) + ' ' + convert(varchar(20),@total) + ' ' + convert(varchar(20),floor(@total))
					--goto e_error


				update currentkot set servicetax=@servicetax,cess=@cess,cess2=@cess2,roamt=@roamt,nett=@total,ppcouponamt=@couponamt,packingchgs=@packingchgs,GSTIN=@GSTIN  where outlet=@outlet and pend_type=@type and pend_code=@code
				if @@error<>0 begin print 'Error in Updating CurrentKOT1' goto e_error end

				select top 1 @ratetype=ratetype,@wno=waiter from currentkot where outlet=@outlet and pend_type=@type and pend_code=@code
				declare @smsddr char(1)
				set @smsddr = case when @type=3 then 'T' else ' ' end
				INSERT KITHIST1 (DRATE,TXNDT,OUTLET,SNO,ROAMT,RATETYPE,SCAMT,SCRATE,STSET,SCSET,wno,txntime,selfdeal,USERNM,finalamt,tno,ptype,pax,servicetax,cess,smsddr,ppcouponamt,ndiscount,vatonst,packingchgs,gstin,guestname)  VALUES
				(@DRATE,@TXNDT,@OUTLET,@SNO,@ROAMT,@RATETYPE,0,@SCRATE,@STSET,@SCSET,
				@wno,convert(varchar(5),getdate(),8),@selfdeal,@USERNM,0,@code,@type,@pax, 0,0,@smsddr,@couponamt,0,@vatonst,@packingchgs,@gstin,@guestname)
				if @@error<>0 begin print 'Error in Updating KITHIST1' goto e_error end
				select @bno = @@identity
				set @packingchgs = 0  --// not to repeat in additional bills// --
--@total+@roamt-@couponamt
				update currentkot set sno=@sno,bno=@bno where outlet=@outlet and pend_type=@type and pend_code=@code
				if @@error<>0 begin print 'Error in Updating CurrentKOT' goto e_error end
				set @sql = 'insert into ' + @temp1 + ' (bno) values (' + str(@bno) + ')'
				exec(@sql)
				if @@error<>0 begin print 'Error in Updating Bill Numbers' goto e_error end
--- Coupon New 15 July 2013
if @tempcoupon <> ''
begin
	 			if cursor_status('global','ccoupon')  >= 0 	begin  	close ccoupon deallocate ccoupon 	end
				if @@error<>0 goto e_error
				declare @couponno varchar(50)
				declare @camt decimal(18,2)
				set @sql='declare ccoupon cursor for select couponno,amt from ' + @tempcoupon  + ' where outlet=' + str(@outlet)
				exec(@sql)
				if @@error<>0 goto e_error
				open ccoupon
				if @@error<>0 goto e_error
				fetch next from ccoupon into @couponno,@camt
				while @@fetch_status=0
				begin

				if @camt <> 0 and @couponno <> ''

					insert into prepaidhist (bno,couponno,amt) values (@bno,@couponno,@camt)
					if @@error<>0 goto e_error
					fetch next from ccoupon into @couponno,@camt
				end
				if @@error<>0 goto e_error
				close ccoupon
				if @@error<>0 goto e_error
				deallocate ccoupon
				if @@error<>0 goto e_error
end
--------coupon new ends here
				close dr
				deallocate dr
			end   -- if drate is not null ends here
			fetch next from tempcur1 into @outlet
	end
	close tempcur1
	deallocate tempcur1

declare @vatonsc tinyint
select @vatonsc=g_scvat from setting 
update kithist1 set vatonsc=@vatonsc  where bno=@bno


commit transaction
return 
e_error:
rollback transaction
raiserror('Error in Stored Procedure ''spkblnew''',16,-1)
return 


GO
/****** Object:  StoredProcedure [dbo].[spkblnewsingle]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Ramanathan>
-- Create date: <13 Jan 2017>
-- =============================================
CREATE PROCEDURE [dbo].[spkblnewsingle]
@kotctrlno numeric
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
declare @qty int
declare @rate money
declare @strate money
declare @drate money
declare @disc money
declare @saleamt money
declare @scamt money
declare @scrare money
declare @sctotal money
declare @g_scvat int
declare @scadd money
declare @cess money
declare @cess21 money
declare @servicetaxeligible as char(1)
declare @stonsc int
declare @amt money
declare @disctotal money
declare @scset int
declare @scrate money
declare @servicetax money
declare @servicetax1 money
declare @cess1 money
declare @cess2 money
declare @vatonst int
declare @stadd money
declare @stset int
declare @outlet int 
declare @selfdeal char(1)
declare @sc_table int
declare @sc_room int
declare @sc_door int
declare @type int
declare @tax money
declare @txndt datetime
declare @pend_code VARCHAR(50)
begin transaction 
select @type=pend_type,@pend_code=pend_code,@txndt=txndt,@outlet=outlet,@drate=drate,@qty=qty,@rate=rate,@strate=strate,@scrate=scrate from currentkot where kotctrlno=@kotctrlno

select @scset=scset,@stset=stset,@selfdeal=selfdeal,@scrate=scrate,
@sc_table=sc_table,@sc_room=sc_room,@sc_door=sc_door,@servicetaxeligible=servicetax,@vatonst=vatonst
from outlet  where code = @outlet
--print convert(varchar(20),@strate) goto e_error
				select @amt = @qty*@rate
				select @disc= @amt*@drate/100
				select @saleamt = @amt
				select @disctotal =  @disc

				set @scamt=0
				if (@type=1 and @sc_table=1) or (@type=2 and @sc_room=1) or (@type=3 and @sc_door=1)
				if @scset=0
					select @scamt=round(@amt*@scrate/100,2)
				else
					select @scamt=round((@amt-@disc)*@scrate/100,2)

				set @sctotal = @scamt

				if @g_scvat=1 
				set @scadd = @scamt
				else
				set @scadd = 0

				set @cess21 = 0
				set @servicetax1 = 0
				set @cess1=0
				if @servicetaxeligible = 'Y'
					begin
						declare @stable money
						select @stonsc=g_stonsc from setting
						if @stonsc=1 
								select @stable = @amt + @scamt - @disc
						else
								select @stable = @amt  - @disc 
						exec getservicetaxr @stable ,@txndt ,@servicetax1 out ,@cess1 out,@outlet ,1000,@cess21 out
						set @servicetax =  @servicetax1
						set @cess =  @cess1
						set @cess2 = @cess21
					end
				if @vatonst= 1 
					set @stadd = @servicetax1+@cess1
				else
					set @stadd = 0


				if @stset=0
					select @tax=round((@amt + @scadd + @stadd)*@strate/100,2)
				else
					select @tax=round(((@amt + @scadd + @stadd) - @disc)*@strate/100,2)
				--print convert(varchar(20),@strate) goto e_error
				/* Service Charges Calculation according to the Mode of the bill modified on 27/07/2009***/
				update currentkot set damt=@disc,drate=@drate,scrate=@scrate,scamt=0,tax=@tax,st1=@servicetax1,cess1=@cess1,sc1=@scamt,cess21=@cess21  where kotctrlno=@kotctrlno


declare @roamt money
declare @total money
declare @couponamt money
declare @packingchgs money
select @total = sum((qty*rate)+tax+sc1+st1+cess1+cess21) from currentkot where pend_type=@type and pend_code=@pend_code
select @couponamt=max(ppcouponamt) ,@packingchgs=max(packingchgs) from currentkot where pend_type=@type and pend_code=@pend_code
select @total=@total+@couponamt+@packingchgs 
--print convert(varchar(20),@total) goto e_error
				if exists(select * from outlet where code=@outlet and roundoff='Y')
					begin
					select @roamt = @total  - floor(@total)
					if @roamt <  .5 set @roamt = @roamt*-1 else set @roamt= round(@total,0) - @total
					set @roamt = round(@roamt,2,1)
					end
				else
					select @roamt=0

update currentkot set roamt=@roamt where pend_type=@type and pend_code=@pend_code
commit transaction
return

e_Error:
rollback transaction 
raiserror('Error in single Item calculation',16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[spkblp]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spkblp    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spkblp] @table varchar(50),@m_mop int ,@gno int ,@crname varchar(50),@crnum varchar(50),@crbank varchar(20),@pend_type int=1,@sessionid varchar(200)='',@bno int=0,@partpayment money=0,@crname1 varchar(50) = '',@crnum1 varchar(20) = ''  ,@crbank1 varchar(20) = '' ,@gno2 numeric=0 ,@mop1 int=0,@gstin nvarchar(50)='',@gstinsplit nvarchar(50) = ''
/* with encryption */
 AS
declare @m_outlet int
declare @m_txndt datetime
declare @m_year int
declare @m_bofy datetime
declare @m_grno int
declare @ff_vou char(1)
declare @m_split char(1)
declare @m_amt money
declare @m_sale money
declare @m_stamt money
declare @m_damt money
declare @m_channel int
declare @m_company int
declare @m_fy int
declare @gno1 int
declare @rno int
declare @ratetype int
declare @m_olname varchar(50)
declare @m_actnum int
declare @m_st_actnum int
declare @m_dc_actnum int
declare @m_ro_actnum int
declare @m_sc_actnum int
declare @m_selfdeal char(1)
declare @m_co1grno int
declare @m_hno_mybook int
declare @m_hno_htlbook int
declare @lux char(1)
declare @htl_receivable int
declare @self_receivable int
declare @m_txndt1 datetime
declare @drate money
declare @m_code int
declare @m_qty int
declare @m_rate money
declare @m_tax money
declare @m_disc money
declare @m_bno int
declare @m_sno int
declare @m_scamt money
declare @scamt money  ------- both m_scamt and scamt are required
declare @m_roamt money
declare @m_strate money
declare @m_kot int
declare @m_dactno int
declare @m_co1fy int
declare @m_partic varchar(100)
declare @scrate money
declare @scset int
declare @stset int
declare @m_roff1 money
declare @ctrlno numeric
declare @servicetax decimal(14,2)
declare @cess decimal(14,2)
declare @st1 decimal(14,2)
declare @cess1 decimal(14,2)
declare @sc1 decimal(14,2)
declare @mcess21 decimal(14,2)
declare @mst1 decimal(14,2)
declare @mcess1 decimal(14,2)
declare @msc1 decimal(14,2)
declare @partpartpayment money
declare @finalfinalamt money
declare @partpaymentoriginal money
declare @comno numeric
set @partpaymentoriginal=@partpayment
set @finalfinalamt =0
--print 'periya'
--set @partpartpayment = @partpayment
begin transaction
set @rno=0
IF @M_MOP = 2 
	begin
		IF NOT EXISTS (SELECT * FROM ROOM WHERE GNO = @GNO)
			BEGIN
				RAISERROR('ROOM IS VACCATED DURING YOUR BILLING PROCESS',16,-1)
				RETURN
			END    
		select @rno=rno from room where gno=@gno
		select @comno=isnull(company,0) from guest where gno=@Gno 
		if @comno<>0
			begin
			exec spkblp_rsdiscount @table,@pend_type,@comno
			end
	end
--print '[periya karuppu]'
--goto e_error
if @bno=0 
declare curkot2 cursor for select max(kotctrlno) ctrlno from currentkot where pend_code=@table and pend_type=@pend_type group by outlet
else
declare curkot2 cursor for select max(kotctrlno) ctrlno from currentkot where pend_code=@table and pend_type=@pend_type and bno=@bno group by outlet
open curkot2
fetch next from curkot2 into @ctrlno
--print @ctrlno
--goto e_error
while @@fetch_status=0
begin
SELECT @FF_VOU='Y'
SELECT @M_SPLIT = 'N'
select  @m_outlet=outlet , @m_bno=bno,@m_sno=sno ,@drate=drate,@scrate=scrate,@ratetype=ratetype,
@m_roamt=roamt,@m_scamt=scamt,@scrate=scrate,@pend_type=pend_type,@servicetax=servicetax,@cess=cess
from currentkot where kotctrlno = @ctrlno

select @m_txndt=curdate,@m_company=cocode from co_rest_ol1 where ocode=@m_outlet
SELECT @M_TXNDT1 = CONVERT(VARCHAR(20),@M_TXNDT)

set @m_bofy = dbo.getbofy(@m_txndt)
set @m_year = datepart(yy,@m_bofy)

SELECT @M_CHANNEL=CHANNEL FROM OUTLET WHERE CODE = @M_OUTLET
SELECT @M_COMPANY=COMPANY FROM RESTCO WHERE CHANNEL=@M_CHANNEL
EXEC @M_FY= GETFY  @M_TXNDT,@M_COMPANY,@M_FY OUT
set @gno1=0
set @m_split = 'N'
if @m_mop = 2 
begin
	select @gno1= @gno
	if @crNAME='YES'
		set @m_split='C'
	else 	--if @m_company = 1
		begin
		if exists(select * from gsplit where gno=@GNO and outlet = @m_outlet and chtype=1)
		set @m_split = 'Y'
		end 
end

SELECT @M_OLNAME=[OLNAME],@M_ACTNUM=ACTNUM,@M_ST_ACTNUM=ST_ACTNUM,
@M_DC_ACTNUM=DC_ACTNUM,@M_RO_ACTNUM = RO_ACTNUM,@M_SC_ACTNUM=SC_ACTNUM,@M_GRNO=CASE WHEN @M_MOP=1 THEN GRNO ELSE GRNO_TRF END ,
@M_SELFDEAL=SELFDEAL,@M_CO1GRNO=CO1GRNO,@M_HNO_MYBOOK=HNO_MYBOOK,@M_HNO_HTLBOOK=HNO_HTLBOOK ,
@LUX=LUX,@FF_VOU=FREEFOODVOUCHING,@HTL_RECEIVABLE=HTL_RECEIVABLE,@SELF_RECEIVABLE=SELF_RECEIVABLE,@stset=stset,@scset=scset
FROM CO_REST_OL1  WHERE OCODE = @M_OUTLET
IF @M_SPLIT='C' AND @FF_VOU='N'  SELECT @M_SPLIT='F'
DECLARE UPDTKIT CURSOR FOR SELECT CODE,QTY,RATE, STRATE , TAX  ,Damt,KOT,scamt,st1,cess1,sc1,cess21   FROM currentkot where bno=@m_bno
OPEN UPDTKIT
FETCH NEXT FROM UPDTKIT INTO @M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_TAX, @M_DISC,@M_KOT,@scamt,@mst1,@mcess1,@msc1,@mcess21
SELECT @M_SALE  = 0
SELECT @M_STAMT = 0
SELECT @M_DAMT = 0
select @m_scamt=0
WHILE  @@FETCH_STATUS=0
	BEGIN
	select @m_amt = @m_qty*@m_rate
	SELECT @M_SALE = @M_SALE + @M_AMT
	SELECT @M_STAMT = @M_STAMT + @M_TAX
	SELECT  @M_DAMT = @M_DAMT +  @M_DISC
	set @m_scamt = @m_scamt + @scamt
	INSERT KITHIST (BNO,KOT,CODE,QTY,RATE,STRATE,TAX,DISCOUNT,st,cess,sc,cess2) VALUES
	(@M_BNO,@M_KOT,@M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_TAX,@M_DISC,@mst1,@mcess1,@msc1,@mcess21)
	IF @@ERROR<>0 GOTO E_ERROR 
	FETCH NEXT FROM UPDTKIT INTO @M_CODE,@M_QTY,@M_RATE,@M_STRATE,@M_TAX, @M_DISC,@M_KOT,@scamt,@mst1,@mcess1,@msc1,@mcess21
	END
close updtkit
DEALLOCATE UPDTKIT

select top 1 @m_roamt=roamt from currentkot where bno=@m_bno 

update KITHIST1 set
DRATE=@drate,
TXNDT=@m_txndt,
OUTLET=@m_outlet,
GNO=@gno,
RNO=@rno,
PMODE=@m_mop,
SNO=@m_sno,
SPLIT=@m_split,
SELFDEAL=@m_selfdeal,
ROAMT=@m_roamt,
RATETYPE=@ratetype,
SCAMT=@m_scamt,
SCRATE=@scrate,
STSET=@stset,
SCSET=@scset,
CRNAME=@crname,
CRNUM=@crnum,
CRTYPE=@crbank,
LUX=@lux,
smsddr=case when @pend_type=3 then 'T' else ' ' end,
pmode1=@mop1,
gno1=@gno2,
crname1=@crname1,
crnum1=@crnum1,
crbank1=@crbank1,
partpayment=0,
gstin=@gstin
where bno=@m_bno
IF @@ERROR<>0 GOTO E_ERROR

declare @stamt money
declare @dcamt money
declare @sale money
declare @roamt money
declare @couponamt money 
declare @cess2 money
declare @packingchgs money
declare @ndiscount money 
declare @finalamt money 
set @roamt=0
set @couponamt = 0
set @packingchgs =0
set @ndiscount=0

select @roamt=roamt,@couponamt=isnull(ppcouponamt,0),@ndiscount=ndiscount,@packingchgs=packingchgs
from kithist1 where bno=@m_bno

select @stamt=sum(tax),@dcamt=sum(discount),@sale=sum(isnull(qty,0)*isnull(rate,0)),@st1=sum(isnull(st,0)), @cess1=sum(isnull(cess,0)), @sc1= sum(isnull(sc,0)),@cess2=sum(isnull(cess2,0))    from kithist where bno=@m_bno

update kithist1 set finalamt=@sale+@stamt+@scamt-@dcamt+@roamt -@couponamt+@st1+@cess1+@sc1+@cess2+@packingchgs-@ndiscount  where bno=@m_bno

select @finalamt=finalamt from kithist1 where bno=@m_bno
--select @finalamt=@sale+@stamt+@scamt-@dcamt+@roamt +@servicetax+@cess-@couponamt+@st1+@cess1+@sc1+@cess2+@packingchgs-@ndiscount 
--print convert(varchar(20),@finalamt) + ' ' + convert(varchar(20),@sale+@stamt+@scamt-@dcamt+@roamt +@servicetax+@cess-@couponamt+@st1+@cess1+@sc1+@cess2+@packingchgs-@ndiscount)  goto e_error

set @finalfinalamt=@finalfinalamt + @finalamt

if @partpaymentoriginal > 0
	begin
		if @partpayment > @finalamt
			begin
			set @partpayment= @partpayment - @finalamt 
			set @partpartpayment =  @finalamt
			end
		else
			begin
			set @partpartpayment=@partpayment
			set @partpayment= 0
			end
--print 'Now ' + convert(varchar(20),@partpartpayment)  + ' Original ' + convert(varchar(20),@partpaymentoriginal) + ' Balance ' + convert(varchar(20),@partpayment) + ' Final Amt ' + convert(varchar(20),@finalamt) + convert(varchar(20),@servicetax)
--goto e_error
		update kithist1 set partpayment = @partpartpayment  where bno=@m_bno
		if @@error<>0 goto e_error
	end

exec serviceavouching @m_bno,@sessionid
if @@error<>0 goto e_error

final:
--delete from currentkot where bno=@m_bno
update currentkot set servicetax=1000 where bno=@m_bno
fetch next from curkot2 into @ctrlno
if @@error<>0 goto e_error
end
close curkot2
deallocate curkot2
if @partpaymentoriginal > @finalfinalamt 
	begin
	print 'Part Payment ' + convert(varchar(20),@partpaymentoriginal) + ' is greater than the Total of Bill(s) ' + convert(varchar(20),@finalfinalamt) 
	goto e_error
	end
delete from currentkot where servicetax=1000
COMMIT TRANSACTION
RETURN 0
E_ERROR:
ROLLBACK TRANSACTION
raiserror ('updation failed in updtkit ',16,-1)
RETURN 1



GO
/****** Object:  StoredProcedure [dbo].[spkblp_rsdiscount]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[spkblp_rsdiscount] @type int,@code int,@comno int
/* with encryption */
AS
BEGIN
declare @bno numeric
declare @scrate money
declare @drate money
declare @outlet numeric
declare @txndt datetime
declare @year int
declare @bofy datetime
declare @qty int
declare @rate money
declare @total money
declare @tax money
declare @disc money
declare @roamt money
declare @scset int
declare @stset int
--declare @split char(1)
declare @sno int
declare @amt money

declare @saleamt money
declare @sctotal money
declare @disctotal money

declare @strate money
declare @scamt money
declare @total1 money
declare @wno int
declare @ratetype int
declare @selfdeal char(1)
declare @g_staxincl char(1)
declare @sql varchar(8000)
declare @sc_table tinyint
declare @sc_room tinyint
declare @sc_door tinyint
declare @commissionrate numeric -- given to agents
declare @guestdiscount numeric -- given to agents
declare @duedt datetime --- coupon duedt
declare @agentcode numeric
declare @couponcode numeric
declare @scadd money
declare @g_scvat tinyint 
declare @servicetax money
declare @cess money
declare @cess2 money
declare @servicetaxeligible char(1)
declare @stonsc int
declare @couponamt decimal(18,2)
declare @servicetax1 money
declare @cess1 money
declare @cess21 money
declare @stadd money
declare @vatonst tinyint
declare @g_kotfinaltime int
declare @cscrate money
declare @showsc int
declare @taxorgst int
declare @itemcode numeric
declare @temp varchar(20) 
set @temp = 'kot' + convert(varchar(20),getdate(),12) + rtrim(ltrim(str(host_id())))
select @g_kotfinaltime=g_kotfinaltime from setting
select @g_scvat=g_scvat from (select top 1 * from setting) a
set @scrate = 0
set @cscrate = 0
set @saleamt = 0
set @sctotal = 0
set @disctotal = 0

--set @sql  = 'truncate table ' + @temp1
--exec(@sql)
----select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
begin transaction
set @servicetax=0
set @cess = 0
set @servicetax1=0
set @cess1 = 0
set @cess2 = 0
set @cess21 = 0
	if cursor_status('global','tempcur1')  >= 0 	begin  	close tempcur1 	deallocate tempcur1 	end
	declare tempcur1 cursor for select distinct outlet from currentkot where pend_type=@type and pend_code=@code
	open tempcur1
	fetch next from tempcur1 into @outlet
	while @@fetch_status=0
		begin
			set @servicetax=0
			set @cess = 0
			set @servicetax1=0
			set @cess1 = 0
			set @cess2 = 0
			set @cess21 = 0

			select @drate=max(drate),@couponamt=max(ppcouponamt) from currentkot where pend_type=@type and pend_code=@code and outlet=@outlet
			if isnull(@drate,0)=0
			begin 
			if exists(select * from gcompanyfooddiscount where comno=@comno and outlet=@outlet)
	    	begin
				select @drate=fooddiscount from gcompanyfooddiscount where comno=@comno and outlet=@outlet
				update currentkot set drate=@drate where  pend_type=@type and pend_code=@code and outlet=@outlet
		    end
			select @drate=max(drate),@couponamt=max(ppcouponamt) from currentkot where pend_type=@type and pend_code=@code and outlet=@outlet
			IF @drate<>0 
			begin
				set @couponamt = isnull(@couponamt,0)
				select @txndt=curdate,@g_staxincl=g_staxincl,@vatonst=vatonst from outlet where code=@outlet
				set @bofy = dbo.getbofy(@txndt)
				set @year = datepart(yy,@bofy)
				exec  GetServiceASno @outlet,@txndt,@sno out
				select @scset=scset,@stset=stset,@selfdeal=selfdeal,@scrate=scrate,
				@sc_table=sc_table,@sc_room=sc_room,@sc_door=sc_door,@servicetaxeligible=servicetax,@vatonst=vatonst,
				@showsc=showsc
				from outlet  where code = @outlet

				select @total= 0
				set @saleamt = 0
				set @sctotal = 0
				set @disctotal = 0
				declare tempcur cursor for select code,qty,rate,strate,scrate from currentkot where outlet=@outlet and pend_type=@type and pend_code=@code
				open tempcur
				fetch next from tempcur into @itemcode,@qty,@rate,@strate,@Cscrate
				while @@fetch_status=0
				begin
				-----
				select @taxorgst=taxorgst from kitprime where icode=(select icode from kitmast where code=@itemcode)
				select @tax=0
				select @amt = @qty*@rate
				select @disc= @amt*@drate/100
				select @saleamt = @saleamt + @amt
				select @disctotal = @disctotal + @disc

				set @scamt=0
				--if (@type=1 and @sc_table=1) or (@type=2 and @sc_room=1) or (@type=3 and @sc_door=1)
				if @showsc=1 set @scrate=@cscrate
				if @scset=0
					select @scamt=round(@amt*@scrate/100,2)
				else
					select @scamt=round((@amt-@disc)*@scrate/100,2)


				set @sctotal = @sctotal + @scamt

				if @g_scvat=1 
				set @scadd = @scamt
				else

				set @scadd = 0
				set @cess21 = 0
				set @servicetax1 = 0
				set @cess1 = 0
				set @stadd=0
				if @taxorgst=0
				begin
					if @servicetaxeligible = 'Y'
						begin
							declare @stable money
							select @stonsc=g_stonsc from setting
							if @stonsc=1 
									select @stable = @amt + @scamt - @disc
							else
									select @stable = @amt  - @disc 
							exec getservicetaxr @stable ,@txndt ,@servicetax1 out ,@cess1 out,@outlet ,1000,@cess21 out
							set @servicetax = @servicetax + @servicetax1
							set @cess = @cess + @cess1
							set @cess2 = @cess2 + @cess21
						end
					if @vatonst= 1 
						set @stadd = @servicetax1+@cess1
					else
						set @stadd = 0
                end

				if @stset=0
					select @tax=round((@amt + @scadd + @stadd)*@strate/100,2)
				else
					select @tax=round(((@amt + @scadd + @stadd) - @disc)*@strate/100,2)
				
				/* Service Charges Calculation according to the Mode of the bill modified on 27/07/2009   further modified on 13 Marc 2018***/
				update currentkot set damt=@disc,drate=@drate,scrate=@scrate,scamt=0,tax=@tax,st1=@servicetax1,cess1=@cess1,sc1=@scamt,cess21=@cess21  where current of tempcur
				if @@error<>0 begin print 'Error in Updating CurrentKOT2' goto e_error end
				select @total =( @total + @amt + @tax + @scamt + @servicetax1 + @cess1 + @cess21) - @disc
				fetch next from tempcur into @itemcode,@qty,@rate,@strate,@showsc
				end
				close tempcur
				deallocate tempcur
-----------------------------------------------------

				if exists(select * from outlet where code=@outlet and roundoff='Y')
					begin
					select @roamt = @total  - floor(@total)
					if @roamt <  .5 set @roamt = @roamt*-1 else set @roamt= round(@total,0) - @total
					end
				else
					select @roamt=0

					--print 'roamt is ' + convert(varchar(20),@roamt) + ' ' + convert(varchar(20),@total) + ' ' + convert(varchar(20),floor(@total))
					--goto e_error


				update currentkot set servicetax=@servicetax,cess=@cess,cess2=@cess2,roamt=@roamt,nett=@total  where outlet=@outlet and pend_type=@type and pend_code=@code
				if @@error<>0 begin print 'Error in Updating CurrentKOT1' goto e_error end

				select top 1 @ratetype=ratetype,@wno=waiter from currentkot where outlet=@outlet and pend_type=@type and pend_code=@code
				declare @smsddr char(1)
				set @smsddr = case when @type=3 then 'T' else ' ' end
--- Coupon New 15 July 2013
--------coupon new ends here
			end   -- if drate is not null ends here
		end
		fetch next from tempcur1 into @outlet
	end
	close tempcur1
	deallocate tempcur1

commit transaction
return 
e_error:
rollback transaction
raiserror('Error in Stored Procedure ''spkblp_rsdiscount''',16,-1)
return 



END

GO
/****** Object:  StoredProcedure [dbo].[spltp]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spltp    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spltp] @amt money,@txndt datetime,@ltamt money out , @adroom int=1,@rno varchar(20)='',@rent money =0 ,@head int = 1 ,@discount money = 0 , @dup int = 0,@rtype numeric = 1 ,@lrate money=0 out 
/* with encryption */
AS
--print 'periya'
declare @tno int
declare @sql varchar(5000)
--declare @lrate money
declare @samt money
declare @eamt money
declare @famt money
declare @amt2 money
declare @ltontariff char(1)
declare @taxsystem int
select @taxsystem=g_taxsystem from setting
SET @LTAMT = 0
declare @ltamttemp money
declare @amttemp money
declare @amttemp1 money
declare @tariff money
declare  @ltrateontariff char(1)
declare @err varchar(4000)
set @err = 'Error'
set @ltamttemp=0
select  @ltrateontariff=ltrateontariff,@ltontariff=ltontariff from ltontariff where datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt) 
if @ltontariff='N'
   begin
	set @amttemp=@amt
	set @amttemp1=@amt
   end
else
   begin
       if @rent<>0 begin
   				--select @tariff=case when @head=1 then case when @dup=0 then rent1 else rentrate21 end  else case when @dup=0 then rent2 else rentrate22 end end  from room where rno=@rno
				exec  FindRent @rtype,@rno , @head , @txndt ,0,@tariff out
				--if @rent-@discount < @tariff set @amttemp=@amt-@rent+@discount + @tariff else set @amttemp=@amt
				if @rent > @tariff set @amttemp=@rent else set @amttemp=@tariff
            	end
            else
             set @amttemp=@amt

	set @amttemp1=@amttemp
	set @amt=@amttemp1
	set @adroom=1 --since there is no use in additional rooms
   end

print convert(varchar(20),@amt) 
if @adroom=0 set @adroom=1

select @tno=tno from ltmaster where datediff(d,'1/1/90',@txndt)  between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt)
declare @ltrate decimal
if @taxsystem=1
begin
--goto e_error
	--set @sql = 'declare cur_lttable cursor for select lrate,samt,eamt,famt from lttable where tno = ' +  ltrim(str(@tno))
	--			--set @sql = 'declare ct1 cursor for select lrate from lttable where tno=' + str(@tno) + ' and ' + 
	--			--convert(varchar(20),case when (@amt * 100) > (floor(@amt) * 100) then floor(@amt)+1 else @amt end)
	--			--+ ' between samt and eamt'
	--exec( @sql)
	--open cur_lttable
	--fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
	--while @@fetch_status=0 and @amt > 0
	--begin
	--set @ltrate=@lrate
	--	if @eamt > (@amt/@adroom)  set @amt2 = @amt   else   set @amt2 = @eamt
	--	if @lrate<>0 and (@amt2+@famt) <> 0 
	--		set @ltamt = round(@ltamt + ((@amt2 + @famt) * @lrate / 100),2)
		
	--	set @amt = @amt - @amt2
	--	if @eamt > @amttemp set @amt2=@amttemp else set @amt2 = @eamt
	--	if @lrate<>0 and (@amt2+@famt) <> 0 
	--		set @ltamttemp = round(@ltamttemp+((@amt2+@famt)*@lrate/100),2)
	--	set @amttemp=@amttemp-@amt2
	--	fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
	--end
	--close cur_lttable
	--deallocate cur_lttable
	select @lrate= lrate from lttable where tno = ltrim(str(@tno)) and @amt between samt and eamt
			set  @ltamt = round(@amt * isnull(@lrate,0) /100,2)

--set @err='Tariff' + convert(varchar(20),@lrate) goto e_error
end
else
begin

exec  FindRent @rtype,@rno , @head , @txndt ,0,@tariff out
--set @err='Tariff' + convert(varchar(20),@tariff) goto e_error
if @ltrateontariff='Y' 
begin
	if (@amt/@adroom) > @tariff 
	begin

	select @lrate=lrate from lttable where tno=@tno and 	(convert(varchar(20),case when (@amt * 100) > (floor(@amt) * 100) then floor(@amt)+1 else @amt end/@adroom)) between samt and eamt

	end
	else
	begin
	select @lrate=lrate from lttable where tno=@tno and @tariff between samt and eamt
	end
end
else
	select @lrate=lrate from lttable where tno=@tno and 	(convert(varchar(20),case when (@amt * 100) > (floor(@amt) * 100) then floor(@amt)+1 else @amt end/@adroom)) between samt and eamt

--set @err = convert(varchar(20),@amt) + ' ' + convert(varchar(20),@lrate)  goto e_Error

	set  @ltamt = round(@amt * isnull(@lrate,0) /100,2)
end
print @lrate
return @ltamt

e_error:
raiserror(@err,16,-1) 
return




GO
/****** Object:  StoredProcedure [dbo].[spltp2]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spltp    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spltp2] @amt money,@txndt datetime,@ltamt money out , @adroom int=1,@rno varchar(20)='',@rent money =0 ,@head int = 1 ,@discount money = 0 , @dup int = 0,@rtype numeric = 0,@lrate money = 0 out 
/* with encryption */
AS
--print 'periya'
declare @tno int
declare @sql varchar(5000)
--declare @lrate money
declare @samt money
declare @eamt money
declare @famt money
declare @amt2 money
declare @ltontariff char(1)
declare @taxsystem int
select top 1 @taxsystem=g_taxsystem from setting
SET @LTAMT = 0
SELECT @TNO=TNO FROM LTMASTER WHERE CONVERT(DATETIME,@TXNDT,106)  BETWEEN SDT AND EDT
declare @ltamttemp money
declare @amttemp money
declare @amttemp1 money
declare @tariff money
declare @ltrateontariff char(1)
set @ltamttemp=0
select @ltrateontariff=ltrateontariff,@ltontariff=ltontariff from ltontariff where datediff(d,'1/1/90',@txndt) between datediff(d,'1/1/90',sdt) and datediff(d,'1/1/90',edt) 
if @ltontariff='N'
   begin
		set @amttemp=@amt
		set @amttemp1=@amt
   end
else
   begin
            if @rent<>0 begin
   		--select @tariff=case when @head=1 then case when @dup=0 then rent1 else rentrate21 end  else case when @dup=0 then rent2 else rentrate22 end end  from room where rno=@rno
		exec  FindRent @rtype,@rno , @head , @txndt ,0,@tariff out
		if @rent-@discount < @tariff set @amttemp=@amt-@rent+@discount + @tariff else set @amttemp=@amt
            	end
            else
             set @amttemp=@amt
	set @amttemp1=@amttemp
	set @amt=@amttemp1
--	set @adroom=1 --since there is no use in additional rooms
   end
if @adroom=0 set @adroom=1
--set @amt = @amt/@adroom
if @taxsystem=1
begin
	--set @sql = 'declare cur_lttable cursor for select lrate,samt,eamt,famt from lttable where tno = ' +  ltrim(str(@tno))  
	--exec( @sql)
	--open cur_lttable
	--fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
	--while @@fetch_status=0 and @amt > 0
	--begin
	--      if @eamt > (@amt/@adroom)  set @amt2 = @amt   else   set @amt2 = @eamt
	--      set @ltamt = round(@ltamt + ((@amt2 + @famt) * @lrate / 100),2)
	--      set @amt = @amt - @amt2
	--      if @eamt > @amttemp set @amt2=@amttemp else set @amt2 = @eamt
	--      set @ltamttemp = round(@ltamttemp+((@amt2+@famt)*@lrate/100),2)
	--      set @amttemp=@amttemp-@amt2
	--      fetch next from cur_lttable into @lrate,@samt,@eamt,@famt
	--end
	--close cur_lttable
	--deallocate cur_lttable
	select @lrate= lrate from lttable where tno = ltrim(str(@tno)) and @amt between samt and eamt
		set  @ltamt = round(@amt * isnull(@lrate,0) /100,2)
end
else
begin

exec  FindRent @rtype,@rno , @head , @txndt ,0,@tariff out
if @ltrateontariff='Y' 
begin
	if (@amt/@adroom) > @tariff 
	select @lrate=lrate from lttable where tno=@tno and (@amt/@adroom) between samt and eamt
	else
	select @lrate=lrate from lttable where tno=@tno and @tariff between samt and eamt
end
else
	select @lrate=lrate from lttable where tno=@tno and (@amt/@adroom) between samt and eamt

	set  @ltamt = round(@amt * isnull(@lrate,0) /100,2)
end
return @ltamt
e_error:
raiserror('Error',16,-1)
return



GO
/****** Object:  StoredProcedure [dbo].[spmiscolln]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spmiscolln] @dt datetime 
/* with encryption */
AS
declare @company numeric
declare @compname varchar(100)

select * into #dummymis from dummymis
declare c1 cursor for select company,name from company order by company
open c1
fetch next  from c1 into @company,@compname
while @@fetch_status=0
begin
declare @sno numeric
set @sno=1
insert into #dummymis (ctrlno,company,companyname,billtype,refno,cash,crcard,ddch,glac,roomservice,otherroomacbr,complementary,details)
select @sno,
@company,@compname,billtype,billno,cash,creditcard,ddch,glaccts,0,otherroom,complementary,details from
(select *,[billtype]=case when split='N' then 'CO Main Bill' when split='Y' then 'CO Split Bill' else 'Complementary Bill' end, 
[BillDate]=txndt,[BillNo]=ltrim(str(bno)) + '/' + ltrim(str(rno)),cash=case when split='N' or split='Y' then case when mop=0
 then total+foodbill+roomdues-spldiscount-advance else 0 end else 0 end,creditcard=case when mop=1 then 
total+foodbill+roomdues-spldiscount-advance else 0 end,ddch=case when mop=2 then 
total+foodbill+roomdues-spldiscount-advance else 0 end,glaccts=case when mop=3 then 
total+foodbill+roomdues-spldiscount-advance else 0 end,otherroom=case when mop=4 then 
total+foodbill+roomdues-spldiscount-advance else 0 end,complementary=case when split='F' then 
total+foodbill+roomdues-spldiscount-advance else 0 end from  (select  rno=(select top 1 rno from guest where gno in 
(select gno from guest where brno=billhist1.brno)),split,mop,txndt,company,bno,brno,advance,spldiscount,foodbill,
total=isnull((select sum(total) from billhist where billhist.cno=billhist1.cno),0),roomdues=isnull((select sum(amount) 
from roomdues where split=billhist1.split and company=billhist1.company and gno in (select gno from guest where 
brno=billhist1.brno)),0),details=crnum+' '+crname +' '+crtype from billhist1 where datediff(d,'1/1/80',txndt)=
datediff(d,'1/1/80',@dt)  and company = @company) a ) aa


--values (" & sno & "," & rs1!company & ",'" & m_companyname & "','" & rs1!billtype & "','" & rs1!billno & "'," & rs1!Cash & "," &
 --rs1!CreditCard & "," & rs1!DDCh & "," & rs1!GlAccts & ",0," & rs1!otherroom & "," & rs1!Complementary & ",'" & rs1!details & "')")


fetch next  from c1 into @company,@compname
end
close c1
deallocate c1
select * from #dummymis
return




GO
/****** Object:  StoredProcedure [dbo].[spnsdel]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[spnsdel]
@myctrlno numeric(18)
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;

declare @txndt datetime
declare @fy numeric
declare @grno numeric
declare @ret_val int
declare @ctrlno numeric
declare @amt decimal(18,2)

select @amt=amount,@ctrlno=ctrlno,@grno=grno,@txndt=txndt from sptbrs_noshow where myctrlno=@myctrlno 
select @fy=dbo.findfy(1,@txndt) 

exec DELGRNO   1,@fy,@grno,@txndt, @ret_val out
delete from sptbrs_noshow where myctrlno=@myctrlno
delete from sptbrs_rn where pctrlno=@myctrlno
update sptbrs set noshowcharges=noshowcharges-@amt  where ctrlno=@ctrlno 
insert into acclog (narration) values ('No show charges No.' + LTRIM(str(@myctrlno)) + ' for Amount ' + LTRIM(convert(varchar(20),@amt)) + ' Deleted = Res.Ref.No' + LTRIM(str(@ctrlno)) )  
END




GO
/****** Object:  StoredProcedure [dbo].[SPNSED]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



CREATE PROCEDURE [dbo].[SPNSED]
@myctrlno numeric, @noshowtotal decimal(18,2),@mop tinyint, @crnum varchar(50),@crname varchar(50) ,
@crtype varchar(50),@nsglactno numeric,@nsmode int=1,@txndt datetime = getdate ,
@sessionid varchar(200)='',@noshow money=0,@gst money=0
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
    declare @grno numeric
	declare @dtxnno numeric
	declare @ctxnno numeric
    declare @fy numeric
    declare @ret_val tinyint
	declare @actnum numeric
	declare @oamt decimal(18,2)
	declare @omop tinyint
	declare @part varchar(200)
	declare @partic varchar(200)
	declare @company numeric
    declare @ctrlno numeric
    declare @gname varchar(50)
	declare @nsctrlno numeric
    declare @usernm varchar(50)
    declare @rsmyctrlno numeric
	declare @txndto datetime
	DECLARE @SNO NUMERIC

	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=@sessionid and [host_id]=host_id()
	select @company=1

if @nsmode=1
	begin
		select @ctrlno=ctrlno,@omop=mop,@oamt=amount,@grno=grno,@txndto=txndt,@sno=sno from sptbrs_noshow where myctrlno=@myctrlno
		select @fy = dbo.findfy(1,@txndto)
		select @dtxnno=txnno from vouchers where datediff(d,txndt,@txndto)=0 and company=1 and grno=@grno and amttyp='D' 
		select @ctxnno=txnno from vouchers where datediff(d,txndt,@txndto)=0 and company=1 and grno=@grno and amttyp='C' 
		exec delgrno  1,@fy,@grno,@txndto,@ret_val out
	end
else
	begin
		select @fy = dbo.findfy(1,@txndt)
		update finyear set ns_sno=isnull(ns_sno,0)+1 where fy=@fy
		select @sno=ns_sno from finyear where fy=@fy 
	end

	select @ctrlno=ctrlno from sptbrs1 where myctrlno=@myctrlno
	exec getgrp @txndt,1,@grno out

	select @ctxnno=isnull(@ctxnno,0)
	select @dtxnno=isnull(@dtxnno,0)
	select @gname=name from sptbrs where ctrlno=@ctrlno  

    select @fy = dbo.findfy(1,@txndt)
	select @actnum= case 
	when @mop=1 then 1
	when @mop=2 then 22
	when @mop=3 then 46
	when @mop=4 then @nsglactno  end
	set @part = 'NoShow Charges Ref.No. ' + ltrim(str(@ctrlno))
	select @partic = case
	when @mop=1 then @part
	when @mop=2 then @part + ltrim(@crnum) + '|' + ltrim(@crname) + '|' + ltrim(@crtype)
	when @mop=3 then @part + ltrim(@crnum) + '|' + ltrim(@crname) + '|' + ltrim(@crtype)
	when @mop=4 then @part + ltrim(@crname) 
	end
	set @partic = @partic + space(100)
	set @partic = ltrim(substring(@partic,1,97))

begin transaction
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txnno2) 
	values (@company, @fy,  @actnum ,@noshowtotal , 'To ' +@partic  , 'D', @txndt , @grno ,'Y', @ctrlno,@dtxnno)
	if @@error <> 0 goto e_error
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txnno2) 
	values (@company, @fy,  57 ,@noshow ,'By' + @partic  ,'C', @txndt , @grno ,'Y', @ctrlno,@ctxnno)
	if @@error <> 0 goto e_error
	declare @gstacno numeric
	declare @sttableno int
	select @sttableno=sttableno_noshow from setting
	select @gstacno=taxacno from sttabler where sttableno=@sttableno and 
	@noshow between samt and eamt and datediff(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt) 
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txnno2) 
	values (@company, @fy,  @gstacno ,@gst ,'By' + @partic  ,'C', @txndt , @grno ,'Y', @ctrlno,@ctxnno)
	if @@error <> 0 goto e_error

	declare @mopdetails varchar(100)
	set @mopdetails=case
		when @mop=1 then 'Cash Payment'
		when @mop=2 then ltrim(@crnum) + '|' + ltrim(@crname) + '|' + ltrim(@crtype)
		when @mop=3 then ltrim(@crnum) + '|' + ltrim(@crname) + '|' + ltrim(@crtype)
		when @mop=4 then ltrim(@crname) 
		end

if @nsmode=1
	begin
		update sptbrs_noshow set txndt=@txndt,amount=@noshowtotal,mop=@mop,mopdetails = @mopdetails,grno=@grno,noshow=@noshow,gst=@gst where myctrlno=@myctrlno
		update sptbrs set noshowcharges=noshowcharges+@noshowtotal-@oamt where ctrlno=@ctrlno
		update sptbrs_rn set txndt=@txndt,amount=@noshowtotal,grno=@grno where pctrlno=@myctrlno
		insert into acclog (narration) values ('Noshow charges Edited for Res.Ref.No' + ltrim(rtrim(str(@ctrlno))) + ' Guest Name : ' + @gname + ' Pre.Amt' + ltrim(rtrim(convert(varchar(20),@oamt))) + 'New Amt: ' + ltrim(rtrim(convert(varchar(20),@noshowtotal)) ) )
	end
else
	begin
	insert into sptbrs_noshow (ctrlno,rsmyctrlno,txndt,amount,reason,usernm,grno,mop,mopdetails,noshow,gst,sno) values
	(@ctrlno,@myctrlno,@txndt,@noshowtotal,'No Show Additional Carges',@usernm,isnull(@grno,0),@mop,@mopdetails,@noshow,@gst,@sno)
	if @@error <> 0 goto e_error
	select @nsctrlno=@@identity
	insert into sptbrs_rn  (mctrlno,type,amount,crnum,crname,crbank,usernm,glactno,mop,txndt,grno,ctrlno1,myctrlno,pctrlno) values
	(0,1,@noshowtotal,@crnum,@crname,@crtype,@usernm,@nsglactno,@mop,@txndt,isnull(@grno,0),@ctrlno ,@myctrlno,@nsctrlno)
	if @@error <> 0 goto e_error
	update sptbrs set noshowcharges=isnull(noshowcharges,0)+@noshowtotal where ctrlno=@ctrlno
	--print 'periya ' + str(@ctrlno)+ ' ' + convert(varchar(20),@amt) goto e_error
	end

	commit transaction 
	return

E_Error:
rollback transaction
raiserror('Cancellation of reservation failed',16,-1)


END




GO
/****** Object:  StoredProcedure [dbo].[spocg]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spocg    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spocg]  @gno numeric,@codate datetime,@24hrs int,@t1 varchar(50), @new int=0 
/* with encryption */
AS
declare @sql varchar(5000)
declare @cidate datetime
declare @rent money
declare @discount money
declare @sc money
declare @sameday int
declare @codate1 datetime
declare @tdate datetime
declare @diff numeric
declare @append int
declare @cidate1 datetime
declare @damt money
declare @scamt money
declare @orent money
declare @txndt datetime
set @sql = 'truncate table ' + @t1
exec(@sql)
select @cidate=ci_date,@rent=rent,@discount=discount,@sc=sc from guest where gno=@gno
---- 1 false 0 true
set @sameday = 1
If @24hrs=0 
  begin
    set @codate1 = convert(varchar(20), DateAdd(d, -1, @codate),106) + ' 12:00:00'
    set @tdate = convert(varchar(20),@cidate,106) + ' 12:00:00'
    set @diff = DateDiff(Mi, @cidate,@tdate)
    set @append = 1
    set @cidate1=@cidate
    If @diff > 0 
      If @diff >= 300 
         begin
         set @sql = 'insert into ' + @t1 + '(txndt,rent) values (''' + convert(varchar(20),@cidate-1,106) + ''',' + str(@rent)+ ')'
         exec(@sql)
         set @append = 0
        end
      else
        If datediff(d,'1/1/80',@cidate)=datediff(d,'1/1/80',@codate)
          begin
         set @sql = 'insert into ' + @t1 + '(txndt,rent) values (''' + convert(varchar(20),@cidate,106) + ''',' + str(@rent)+ ')'
         exec(@sql)
         set @append=0
         set @sameday = 0
         set @append = 0
         end
    set @cidate = @tdate
    While datediff(d,'1/1/80',@cidate) <= datediff(d,'1/1/80',@codate1)
         begin
         set @sql = 'insert into ' + @t1 + '(txndt,rent) values (''' + convert(varchar(20),@cidate,106) + ''',' + str(@rent)+ ')'
         exec(@sql)
         set @append = 0
         set @CIDATE = @cidate + 1                --DateAdd("h", 24, M_CIDATE)
         end
    set @diff = DateDiff(Mi, @cidate,@codate)
    If @diff > 0 
       If @diff > 240
         begin
         set @append = 0
           If  datediff(d,'1/1/80',@cidate1) <> datediff(d,'1/1/80',@codate)  -- Not DateSerial(Year(M_CIDATE1), Month(M_CIDATE1), Day(M_CIDATE1)) = DateSerial(Year(m_codate), Month(m_codate), Day(m_codate)) Then
               begin
                  set @append = 0
                  set @sql = 'insert into ' + @t1 + '(txndt,rent) values (''' + convert(varchar(20),@cidate,106) + ''',' + str(@rent)+ ')'
                  exec(@sql)
               end
         End
end
/*
Else '' 24 hTRs checkout
    label2(3).Caption = m_codate
    PForm.Grid1.Rows = 2
    PForm.Grid1.Clear
    s$ = "<DATE|<RENT"
    PForm.Grid1.FormatString = s$
    ROW = 1
    PForm.Grid1.FixedCols = 0
    PForm.Grid1.ColWidth(0) = 1000
    PForm.Grid1.ColWidth(1) = 700
    PForm.Grid1.ColAlignment(1) = 6
    x = 0
    append = False
    M_CIDATE = T_DATE
    m_codate1 = m_codate
    Do While M_CIDATE <= m_codate1
         If x = 1 Then
            ROW = ROW + 1
            PForm.Grid1.Rows = PForm.Grid1.Rows + 1
         Else
            x = 1
         End If
         PForm.Grid1.TextMatrix(ROW, 0) = Format(M_CIDATE, "dd MMM yyyy")
         PForm.Grid1.TextMatrix(ROW, 1) = Format(M_RENT, "#0.00")
         M_CIDATE = DateAdd("h", 24, M_CIDATE)
        append = True
    Loop
    M_DIFF = DateDiff("n", M_CIDATE, m_codate)
    If M_DIFF > 0 Then
       If M_DIFF > 240 Then
         append = True
         If x = 1 Then
            ROW = ROW + 1
            PForm.Grid1.Rows = PForm.Grid1.Rows + 1
         Else
            x = 1
         End If
         PForm.Grid1.TextMatrix(ROW, 0) = Format(M_CIDATE, "dd MMM yyyy")
         PForm.Grid1.TextMatrix(ROW, 1) = Format(M_RENT, "#0.00")
       End If
    End If
    If Not append Then
         PForm.Grid1.TextMatrix(ROW, 0) = Format(M_CIDATE, "dd MMM yyyy")
         PForm.Grid1.TextMatrix(ROW, 1) = Format(M_RENT, "#0.00")
         x = 1
    End If
    PForm.Grid1.Cols = 16
    For x = 2 To 15
          PForm.Grid1.ColWidth(x) = 600
    Next
End If
*/
set @sql = 'declare tempcur cursor for select txndt,rent from ' + @t1
exec(@sql)
open tempcur
fetch next from tempcur into @txndt,@orent
declare @totalrent money
set @totalrent = 0
while @@fetch_status=0
begin
set @totalrent=@totalrent+@orent
if @orent<>0
  begin
   set @damt =0
   set @scamt=0
        if @discount<>0 
        set @damt =  @orent*@discount/100
        if @sc<>0 
        set @scamt = @orent*@sc/100
        set @sql = 'update ' + @t1 + ' set discount=' + str(@damt) + ',sc =' + str(@scamt) + ' where current of tempcur'
        exec(@sql)
 end
fetch next from tempcur into @txndt,@orent
end
close tempcur
deallocate tempcur
if @totalrent=0 
begin
set @sql = 'insert into  ' + @t1 + '  (txndt,rent,discount,sc) values ('''  + convert(varchar(20),getdate(),106) + ''' , ' +  str(@rent)  + ',0,0)'
--set @sql = 'update ' + @t1 + ' set rent = ' + convert(varchar(20),@rent)  + ' where datediff(d,''1/1/80'',txndt) = datediff(d,''1/1/80'',''' + convert(varchar(20),getdate()) + ''')'
exec (@sql)
if @@error <> 0  goto e_error
end
if exists(SELECT * FROM GRENT WHERE GNO =@gno)
begin
set @sql = 'declare tempcur cursor for select txndt,rent from ' + @t1
exec(@sql)
open tempcur
fetch next from tempcur into @txndt,@orent
while @@fetch_status=0
begin
select top 1 @rent=rent,@discount=discount from grent where gno=@gno and datediff(d,'1/1/80',@txndt) between datediff(d,'1/1/80',ci_date) and datediff(d,'1/1/80',co_date)  order by ci_date desc --<= datediff(d,'1/1/80',@txndt))
if @orent<>0
  begin
   set @damt =0
   set @scamt=0
        if @discount<>0 
        set @damt =  @rent*@discount/100
        if @sc<>0 
        set @scamt = @rent*@sc/100
        set @sql = 'update ' + @t1 + ' set rent=' + str(@rent) + ',discount=' + str(@damt) + ',sc =' + str(@scamt) + ' where current of tempcur'
        exec(@sql)
 end
fetch next from tempcur into @txndt,@orent
end
close tempcur
deallocate tempcur
end
set @sql = 'update ' + @t1 + ' set item1=0,item2=0,item3=0,item4=0,item5=0,item6=0,item7=0,item8=0,item9=0,item10=0,total=0,billorder=0,section=0'
exec(@sql)
if @new=0 
exec sp_cb @t1,@gno,1
else
exec sp_cb_fast @t1,@gno
return
e_error:
raiserror('error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[sponac]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sponac    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sponac] @company int,@txndt datetime,@mop int,@crname varchar(50), @crnum varchar(20),@crbank varchar(20),
@amt money,@username varchar(20),@glactno int,@txnglno int, @details varchar(400),@ctrlno int out,@sessionid varchar(200)= ''
/* with encryption */
AS
declare @fy int
declare @grno int
declare @actnum int
declare @mopname varchar(100)
declare @partic varchar(500)
declare @orno int
declare @txnguestname varchar(200)
begin transaction
--print 'periya'
EXEC  GETFY @txndt,@company,@fy OUT
IF @fy=0 GOTO E_ERROR
EXEC  GETGRP @txndt,@company,@grno OUT
IF @grno=0 GOTO E_ERROR
select @txnguestname=cgp_Desc from glprimld where cgp_actno=@txnglno and fy=@fy
select @username=usernm from comuser where [host_id]=host_id() and sessionid=@sessionid and [host_id]=host_id()
UPDATE finyear SET onacrcno=onacrcno+1 where fy=@fy
select @orno=onacrcno from finyear where fy=@fy
if @mop=4  set @crnum=@glactno
insert into onacrc (onacrcno,company,fy,txndt,mop,crname,crnum,crbank,grno,amount,username,TxnGlNo,details) values
(@orno,@company,@fy,@txndt,@mop,@crname,@CRnum,@crbank,@grno,@amt,@username,@txnglno,@details)
select @ctrlno=@@identity
select @actnum = case when @mop = 1 then 1
		when @mop=2 then 22
		when @mop=3 then 46
		when @mop=4 then @glactno end
select @mopname = case when @mop=1 then 'Cash Payment' 
		when @mop=2 then 'Credit Card'  + @crnum + '-' + @crbank + '-'+@crname
		when @mop=3 then 'DD/Cheque' + @crnum + '-' + @crbank + '-'+@crname
		when @mop=4 then 'to the Debit if GL a/c ' + @crname end
if @mop=4
select @partic = substring(' On A/c Rec.No.' + ltrim(str(@orno)) + ' ' +  @txnguestname ,1,195)
else
select @partic = substring(' On A/c Rec.No.' + ltrim(str(@orno)) + ' ' + @mopname + ' ' +  @details,1,195)

insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode)
values (@company,@fy,@actnum,@amt,'To '+@partic , 'D',@txndt,@grno,0,'Y')
if @@error <>0 goto e_error

select @partic = substring(' On A/c Rec.No.' + ltrim(str(@orno)) + ' ' + @mopname + ' ' +  @details,1,195)
insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode)
values (@company,@fy,@txnglno,@amt,'By '+@partic , 'C',@txndt,@grno,0,'Y')
if @@error <>0 goto e_error
commit transaction
return @ctrlno
e_error:
rollback transaction
raiserror('Error in sponac',16,-1)
return 0




GO
/****** Object:  StoredProcedure [dbo].[sponacdel]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sponacdel    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sponacdel] @ctrlno int ,@usernm varchar(50) = '',@sessionid varchar(200)=''
/* with encryption */
AS
begin transaction
declare @retval int
declare @txndt datetime
declare @grno int
declare @company int
declare @fy int
declare @rcno numeric
declare @amt money
declare @txnglno numeric
declare @name varchar(100)
select @fy =fy, @company=company,@txndt=txndt,@grno=grno,@rcno=onacrcno,@txnglno=txnglno,@amt=amount from onacrc where ctrlno =@ctrlno
select @name=cgp_desc from glprimld where fy=@fy and cgp_actno=@txnglno
delete from onacrc where ctrlno = @ctrlno
if @@error<>0 goto e_error
exec @retval=delgrno @company,@fy,@grno,@txndt, @retval out
if @retval<>0 goto e_error
if @@error<>0 goto e_error
insert into acclog (txndt,usernm,narration,txntype) values (getdate(),@usernm, 
'On Account Receipt No ' + ltrim(str(isnull(@rcno,0))) + ' for Rs. ' + ltrim(convert(varchar(20),isnull(@amt,0)))  + ' from ' + ltrim(rtrim(isnull(@name,''))) + ' Deleted',
18)
if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('error in deletion',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sponacm]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/   /*06 01 2006*/
/****** Object:  Stored Procedure dbo.sponac    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sponacm] @ctrlno int,@txndt datetime,@mop int,@crname varchar(50), @crnum varchar(20),@crbank varchar(20),
@amt money,@username varchar(20),@glactno int,@txnglno int, @details varchar(100)
/* with encryption */
AS
declare @company int
declare @fy int
declare @grno int
declare @actnum int
declare @mopname varchar(100)
declare @partic varchar(200)
declare @orno int
declare @ret_val numeric
declare @txndt1 datetime
declare @oldamt money
begin transaction
--print 'periya'
select @company=company,@fy=fy,@grno=grno,@txndt1=txndt,@orno=onacrcno,@oldamt=amount  from onacrc where ctrlno=@ctrlno
select @username=usernm from comuser where [host_id]=host_id()

exec delgrno @company,@fy,@grno,@txndt1,@ret_val out
if @@error<>0 goto e_error
--insert into onacrc (onacrcno,company,fy,txndt,mop,crname,crnum,crbank,grno,amount,username,TxnGlNo,details) values
--(@orno,@company,@fy,@txndt,@mop,@crname,@CRnum,@crbank,@grno,@amt,@username,@txnglno,@details)
if datediff(d,@txndt1,@txndt) <> 0 
	exec getgrp @txndt,@company,@grno out

if @mop=4  set @crnum=@glactno

update onacrc set 
txndt=@txndt,
mop=@mop,
crname=@crname,
crnum=@crnum,
crbank=@crbank,
amount=@amt,
txnglno=@txnglno,
details=@details,
grno=@grno
where ctrlno=@ctrlno
if @@error<>0 goto e_error
select @actnum = case when @mop = 1 then 1
    when @mop=2 then 22
    when @mop=3 then 46
    when @mop=4 then @glactno end
select @mopname = case when @mop=1 then 'Cash Payment' 
    when @mop=2 then 'Credit Card'  + @crnum + '-' + @crbank + '-'+@crname
   when @mop=3 then 'DD/Cheque' + @crnum + '-' + @crbank + '-'+@crname
   when @mop=4 then 'to the Debit if GL a/c ' + @crname end
select @partic = substring(' On A/c Rec.No.' + ltrim(str(@orno)) + ' ' + @mopname + ' ' + @details,1,195)
insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode)
values (@company,@fy,@actnum,@amt,'To '+@partic , 'D',@txndt,@grno,0,'Y')
if @@error <>0 goto e_error
insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode)
values (@company,@fy,@txnglno,@amt,'By '+@partic , 'C',@txndt,@grno,0,'Y')
if @@error <>0 goto e_error
insert into acclog (txndt,usernm,narration,txntype,gno) values 
(getdate(),@username,   'On A/c Receipt No ' + ltrim(str(@orno)) + ' Dated ' + convert(varchar(20),@txndt1,6) + ' Edited - Old amt = '+ convert(varchar(20), @oldamt) + ' ,  New Amount = '+ convert(varchar(20),@amt)  + ' New Date : ' + convert(varchar(20),@txndt,6)  ,25 , 0)
commit transaction
return
e_error:
rollback transaction
raiserror('Error in sponac',16,-1)
return 0




GO
/****** Object:  StoredProcedure [dbo].[spppc]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spppc    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spppc] @t1 varchar(50) , @txndt datetime,@company int  
/* with encryption */
AS
declare @sql varchar(1000)
declare @fy int
declare @grno int
declare @actnum int
declare @amount money
declare @partic varchar(100)
declare @actname varchar(50)
--print 'periya'
begin transaction
EXEC  GETFY @txndt,@company,@fy OUT
IF @fy=0 GOTO E_ERROR
select @sql = 'declare tempcur cursor for select actnum,amount,partic from ' + @t1
exec (@sql)
open tempcur
fetch next from tempcur into @actnum,@amount,@partic
if @@fetch_status<>0 begin print 'No vouchers Posted'  goto e_error end
while @@fetch_status=0
begin
EXEC  GETGRP @txndt,@company,@grno OUT
IF @grno=0 GOTO E_ERROR
--set @grno =  @grno
if exists (select * from vouchers where company=@company and fy=@fy and datediff(d,'1/1/90',txndt)=datediff(d,'1/1/90',@txndt) and grno=@grno)
begin
print 'The journal number duplicates.....Please repeat the process' 
goto e_error
end
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno)
values(@company,@fy,@actnum,@amount,'To '+ @partic,'D',@txndt,@grno,0)
IF @@ERROR<>0 GOTO E_ERROR
select @actname=cgp_desc from glprimld where company=@company and fy=@fy and cgp_actno=@actnum
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno)
values(@company,@fy,51,@amount,'By '+ rtrim(@actname)+'-'+@partic,'C',@txndt,@grno,0)
IF @@ERROR<>0 GOTO E_ERROR
fetch next from tempcur into @actnum,@amount,@partic
end
close tempcur
deallocate tempcur
set @sql = 'drop table ' + @t1
exec(@sql)
commit transaction
return
e_error:
rollback transaction
set @sql = 'drop table ' + @t1
exec(@sql)
raiserror('spppc failed',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[spputstock]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spputstock] 
/* with encryption */
AS
begin transaction
declare @ctrlno numeric
update storeitems set forupdt='N'
declare stcur cursor for select ctrlno from storeitems
open stcur
fetch next from stcur into @ctrlno
while @@fetch_status=0
begin
exec fillstock @ctrlno,'1 apr 2004', 1
if @@error<> 0 goto e_error
fetch next from stcur into @ctrlno
end
close stcur
deallocate stcur
commit transaction
return
e_error:
rollback transaction
raiserror('Error in stock updateion',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spradv]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spradv] @ctrlno numeric,@txndt datetime, @advamount money, @mop  int, 
@crnum varchar(50), @crname varchar(50) , @crtype varchar(50),
@glactno numeric ,@arno numeric out,@sessionid varchar(200)=''
/* with encryption */
AS
declare @fy numeric
declare @grno numeric
declare @year numeric
declare @bofy datetime
declare @usernm varchar(50)
declare @actnum numeric
declare @partic varchar(100)
declare @advctrlno numeric
declare @err varchar(2000)
--print 'Periya'
begin transaction
if @advamount <> 0
begin
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=@sessionid
if @ctrlno=0 begin set @err =   'Entry not selected' goto e_error end
if not exists(select * from sptbrs where ctrlno=@ctrlno) begin set @err =  'Entry not found' goto e_error end
if exists(select * from sptbrs_v1 where ctrlno=@ctrlno and (status='C'   or 
datediff(d,'1/1/90',arrivaldate) <  datediff(d,'1/1/90',@txndt)  or noofrooms=(arrivedrooms+cancelledrooms)))
begin
set @err =  'There is some conflict in the Reservation Entry....Check for Cancellation/Arrival Date/Arrived Status etc'
goto e_error
end
exec  getfy @txndt , 1,@fy out
if @fy=0 goto e_error
if @@error <> 0 goto e_error
exec getgrp @txndt,1,@grno out
if @grno=0 goto e_error
if @@error <> 0 goto e_error
--if datepart(mm,@txndt) >- 1 and datepart(mm,@txndt) <= 3
--   set @year = datepart(yy,@txndt)-1
--else
--  set @year = datepart(yy,@txndt)
--select @bofy = '1 Apr ' + str(@year)

set @bofy = dbo.getbofy(@txndt)
set @year = datepart(yy,@bofy)

update finyear set advbfano=case when datediff(d,'1/1/80',advbfadate)= datediff(d,'1/1/80',@bofy) then advbfano+1 else 1 end , advbfadate=@bofy  where fy=@fy
if @@error<>0 begin set @err =  'Error in updating Getting Advance Receipt Number' goto e_error end
select @arno=advbfano from finyear where fy=@fy
update sptbrs set advancerecd=advancerecd+@advamount where ctrlno=@ctrlno
insert into sptbrs_ad (ctrlno,txndt,amount,grno,mop,crnum,crname,crtype,glactno,usernm,amttyp,receiptno) values
(@ctrlno,@txndt,@advamount,@grno,@mop,@crnum,@crname,@crtype,@glactno,@usernm,'C',@arno)
if @@error<>0 begin set @err =  'Error in updateing Advance Aegister' goto e_error end
set @advctrlno=@@identity
if exists(select * from setting where g_finaccount=0) goto final
select @actnum= case 
  when @mop=1 then 1
  when @mop=2 then 22
  when @mop=3 then 46
  when @mop=4 then @glactno
  end
select @partic = case
  when @mop=1 then 'To adv. ag. Res. No. ' + ltrim(str(@ctrlno))
  when @mop=2 then 'To adv. ag. Res. No. ' + ltrim(str(@ctrlno)) + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype)
  when @mop=3 then 'To adv. ag. Res. No. ' + ltrim(str(@ctrlno)) + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype)
  when @mop=4 then 'To adv. ag. Res. No. ' + ltrim(str(@ctrlno)) + ' ' + ltrim(@crname) + ' '
  end
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
values (1, @fy,  @actnum ,@advamount , @partic  , 'D', @txndt , @grno ,'Y', @arno,@sessionid)
if @@error <> 0 begin set @err =  'Error in Updating Voucher File with GL Account Number' goto e_error end
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
values (1, @fy,  56 ,@advamount ,'By adv.ag. Res. No. '+ltrim( str(@ctrlno)) , 'C', @txndt , @grno ,'Y', @arno,@sessionid)
if @@error <> 0 begin set @err =  'Error in Updating Voucher File with Account Number 56'  goto e_error end
end
else
begin
set @err =  'Amount can not be Zero' goto e_error
end
final:
set @arno=@advctrlno
commit transaction 
return
e_error:
rollback transaction 
raiserror(@err,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spradve]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spradve] @ctrlno numeric,@txndt datetime, @advamount money, @mop  int, 
@crnum varchar(50), @crname varchar(50) , @crtype varchar(50),
@glactno numeric,@myctrlno numeric,@sessionid varchar(200)=''
/* with encryption */
AS
declare @arno numeric
declare @fy numeric
declare @grno numeric
declare @year numeric
declare @bofy datetime
declare @usernm varchar(50)
declare @actnum numeric
declare @partic varchar(100)
declare @advctrlno numeric
declare @oldctrlno numeric
declare @oldamount money
declare @oldtxndt datetime
declare @oldgrno numeric
--print 'Periya'
begin transaction
if @advamount <> 0
begin
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=@sessionid and [host_id]=host_id()

if @ctrlno=0 begin print  'Entry not selected' goto e_error end
if @myctrlno=0 begin print 'Error in the old ref.no' goto e_error end
if not exists(select * from sptbrs where ctrlno=@ctrlno) begin print 'Entry not found' goto e_error end
if exists(select * from sptbrs_v1 where ctrlno=@ctrlno and (status='C'   or 
datediff(d,'1/1/90',arrivaldate) <  datediff(d,'1/1/90',@txndt)  or noofrooms=arrivedrooms))
begin
print 'There is some conflict in the Reservation Entry....Check for Cancellation/Arrival Date/Arrived Status etc'
goto e_error
end
select @oldtxndt=txndt,@arno=receiptno,@oldgrno=grno,@oldctrlno=ctrlno,@oldamount=amount from sptbrs_ad where myctrlno=@myctrlno
exec sprda @myctrlno,1
if @@error<>0 goto e_error
exec  getfy @txndt , 1,@fy out
if @fy=0 goto e_error
if @@error <> 0 goto e_error
exec getgrp @txndt,1,@grno out
if @grno=0 goto e_error
if @@error <> 0 goto e_error
--if datepart(mm,@txndt) >- 1 and datepart(mm,@txndt) <= 3
--   set @year = datepart(yy,@txndt)-1
--else
--  set @year = datepart(yy,@txndt)
--select @bofy = '1 Apr ' + str(@year)

set @bofy = dbo.getbofy(@txndt)
set @year = datepart(yy,@bofy)


--update finyear set advbfano=case when datediff(d,'1/1/80',advbfadate)= datediff(d,'1/1/80',@bofy) then advbfano+1 else 1 end , advbfadate=@bofy  where fy=@fy
--if @@error<>0 begin print 'Error in updating Getting Advance Receipt Number' goto e_error end
--select @arno=advbfano from finyear where fy=@fy
update sptbrs set advancerecd=advancerecd+@advamount where ctrlno=@ctrlno
insert into sptbrs_ad (ctrlno,txndt,amount,grno,mop,crnum,crname,crtype,glactno,usernm,amttyp,receiptno) values
(@ctrlno,@txndt,@advamount,@grno,@mop,@crnum,@crname,@crtype,@glactno,@usernm,'C',@arno)
if @@error<>0 begin print 'Error in updateing Advance Aegister' goto e_error end
set @advctrlno=@@identity
if exists(select * from setting where g_finaccount=0) goto final
select @actnum= case 
  when @mop=1 then 1
  when @mop=2 then 22
  when @mop=3 then 46
  when @mop=4 then @glactno
  end
/*select @partic = case
  when @mop=1 then 'To adv. ag. Res. No. ' + ltrim(str(@ctrlno))
  when @mop=2 then 'To ' + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype) + '  adv. ag. Res. No. ' + ltrim(str(@ctrlno))
  when @mop=3 then 'To ' + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype) + '  adv. ag. Res. No. ' + ltrim(str(@ctrlno))
  when @mop=4 then 'To ' + ltrim(@crname) + ' ' + ' adv. ag. Res. No. ' + ltrim(str(@ctrlno))
  end
*/
select @partic = case
  when @mop=1 then 'To adv. ag. Res. No. ' + ltrim(str(@ctrlno))
  when @mop=2 then 'To adv. ag. Res. No. ' + ltrim(str(@ctrlno)) + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype)
  when @mop=3 then 'To adv. ag. Res. No. ' + ltrim(str(@ctrlno)) + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype)
  when @mop=4 then 'To adv. ag. Res. No. ' + ltrim(str(@ctrlno)) + ' ' + ltrim(@crname) + ' '
  end  
  
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
values (1, @fy,  @actnum ,@advamount , @partic  , 'D', @txndt , @grno ,'Y', @arno,@sessionid)
if @@error <> 0 begin Print 'Error in Updating Voucher File with GL Account Number' goto e_error end
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
values (1, @fy,  56 ,@advamount ,'By adv.ag. Res. No. '+ltrim( str(@ctrlno)) , 'C', @txndt , @grno ,'Y', @arno,@sessionid)
if @@error <> 0 begin print 'Error in Updating Voucher File with Account Number 56'  goto e_error end
end
else
begin
print 'Amount can not be Zero' goto e_error
end
final:
insert into acclog (narration,txntype,sessionid,username) values ('Reservation advance edited - Advance Receipt No.' + ltrim(str(@arno)) + ' Old Res.Ref.No.' + ltrim(str(@oldctrlno)) + ' Old Amt ' + convert(varchar(20),@oldamount) +' Old  Dated ' + convert(varchar(20),@oldtxndt,3) + ' New Res.Refno. ' + ltrim(str(@ctrlno)) + ' New Amount-' + convert(varchar(20),@advamount) + ' New Dated ' + convert(varchar(20),@txndt) ,25,@sessionid,@usernm)
commit transaction
return
e_error:
rollback transaction
raiserror('Error in Reservation Advance',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sprda]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sprda] @myctrlno numeric,@upd int=0 ,@sessionid varchar(200)=''
/* with encryption */
AS
--print 'Periya'
begin transaction
declare @ctrlno numeric
declare @txndt datetime
declare @grno numeric
declare @fy numeric
declare @ret_val numeric
declare @amount money
declare @advancerecd money
declare @advanceadjd money
declare @refundamount money
select @ctrlno=ctrlno,@txndt=txndt,@grno=grno,@amount=amount from sptbrs_ad where myctrlno=@myctrlno
if exists(select * from sptbrs_v1 where ctrlno=@ctrlno and (status='C' or noofrooms=arrivedrooms))
begin
print 'This advance entry can not be deleted....Reason can be either it is a cancelled entry or Guest Already Checked-In' 
goto e_error
end
select @advancerecd=advancerecd,@advanceadjd=advanceadjd,@refundamount=refundamount from sptbrs where ctrlno=@ctrlno
if @amount > (@advancerecd-@advanceadjd-@refundamount )
begin
print 'The advance receipt can not be deleted' + nchar(13) + 'reason being' + nchar(13) + 'either there is an adjustment out of it' + nchar(13) + 'or' + nchar(13) + 'some amount is refunded'
goto e_error
end
update sptbrs set advancerecd=advancerecd-@amount where ctrlno=@ctrlno
if @@error<>0 begin print 'Problem in updating Advance amount in Reservation Register...Deletion cancelled' goto e_error end
exec getfy @txndt,1,@fy out
if @@error<>0 begin print 'Error in Getting Financial year' goto e_error end
if @fy=0 goto e_error
exec delgrno 1,@fy,@grno,@txndt,@ret_val out
if @@error<>0 begin print 'Problem in Deleting Related Vouchers..Deletion cancelled' goto e_error end
delete from sptbrs_ad where myctrlno=@myctrlno
if @@error<>0 begin print 'Problem in Deleting Entry from the Advance Register...Deletion cancelled' goto e_error end
if @upd=0
begin
declare @usernm varchar(50)
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=@sessionid
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=@sessionid and [host_id]=host_id()

insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, 'Advance against Reservation Deleted..Reservation No.' +STR(@ctrlno) + ' - Amount ' + convert(varchar(20),@amount,3) + ' Dated ' + convert(varchar(20),@txndt,3) ,24,0)
if @@error <> 0 goto e_error
end
commit transaction
return
e_error:
rollback transaction
raiserror('ERROR',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[sprstent]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sprstent] 
/* with encryption */
AS
declare @ctrlno numeric
declare @myctrlno numeric
begin transaction
declare tentdummy cursor for select ctrlno  from sptbrs where tentative='Y' and datediff(n,getdate(),tent_upto) < 0  and cancelledon=null
open tentdummy
fetch next  from tentdummy into @ctrlno
while @@fetch_status=0
begin
declare tentdummy1 cursor for select myctrlno from sptbrs_v1 where ctrlno=@ctrlno and noofrooms-arrivedrooms-cancelledrooms > 0
open tentdummy1
fetch next  from tentdummy1 into @myctrlno
while @@fetch_status=0
begin
--SPTBNS @myctrlno numeric,@refund money,@rmop int,@rglactno numeric,
---@noshow money,@nsmop int,@crnum char(20), @crname varchar(30),@crtype varchar(20),@nsglactno numeric,@usernm varchar(20),
--@reason varchar(100),@txndt datetime,@roomcancel int=0,@cancelrooms int=0
--as
declare @txndt datetime
set @txndt= getdate()
exec sptbns @myCtrlNo, 0 , 0, 0, 0, 0, '','','', 0 , 'System' ,'Auto Cancellation of Tentative booking',@txndt ,0,0
fetch next  from tentdummy1 into @myctrlno
end
close tentdummy1
deallocate tentdummy1
--print 'periya'
fetch next  from tentdummy into @ctrlno
end
close tentdummy
deallocate tentdummy
commit transaction
return
e_error:
rollback transaction
raiserror('Error in auto cancellation fo reservations',1,-16)
return




GO
/****** Object:  StoredProcedure [dbo].[sprsup]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sprsup    Script Date: 15/02/2004 6:23:01 PM ******/
/****** Credit Card Hold Concept introduced on 25 October 2008  Sunday (27th Diwali)*******/
CREATE PROCEDURE [dbo].[sprsup] @opt int ,@cno numeric ,@arrivaldate datetime , @departuredate datetime ,
@usernm varchar(20),
@instructions varchar(200) , @name varchar(50), @add1 varchar(50) , @add2 varchar(50) , @add3 varchar(50),
@city varchar(50), @state varchar(50), @pin varchar(50), @phone varchar(50),@fax varchar(50), @email varchar(50),
@t1 varchar(50),  @advamount money, @advmop  int, @crnum varchar(50), @crname varchar(50) , @crtype varchar(50),
@glactno numeric ,@address varchar(10),  @company numeric ,
@pickupinstrn varchar(100),@pickup varchar(20), @pickupreqd int,
@pickupstation varchar(50), @pickupfltno varchar(50),
@ctrlno numeric  out,
@bookedby varchar(400),
@net char(1) = 'N',@netid varchar(300)='',@tentative int =0,@dttent datetime='1 jan 2007',
@doc_name varchar(50)='',@doc_no varchar(50) = '',@trackid varchar(100)='',@paymentid varchar(100)='',
@udf1 varchar(20)='',@udf2 varchar(20)='',
@udf3 varchar(20)='',@udf4 varchar(20)='',@memo varchar(200) ='',
@cch_amt money=0,          @cch_crno varchar(20)='',  @cch_crname varchar(50)='',
@cch_crbank varchar(50)='',@cch_refno varchar(50)='',@cch_duedt datetime='',@renttype1 int=1,@mktmgr numeric=1,@sessionid varchar(200)='',
@channel varchar(100)='',@ischannel int=0,@channelamt money=0,@staahid varchar(100)='',@channeladv money=0,
@dobm int=0,@dobd int=0,@domm int=0,@domd int=0,
@extrabeds tinyint=0, @extrabedcharges money=0,
@gstin nvarchar(50)= '',
@commrate numeric(18,2)=0,
@commgstrate  numeric(18,2)=0,
@commtcsrate  numeric(18,2)=0,
@commtdsrate  numeric(18,2)=0,
@commission  numeric(18,2)=0,
@commgst  numeric(18,2)=0,
@commtcs  numeric(18,2)=0,
@commtds  numeric(18,2)=0

/* with encryption */
AS
declare  @roomtype varchar(50)
declare @noofrooms numeric
declare @rentagreed money
declare @instructions1 varchar(50)
declare @advrno numeric
declare @fy int
declare @sql varchar(500)
declare @grno numeric
declare @txndt datetime
declare @bofy datetime
declare @year int
declare @arno numeric
declare @mop int
declare @actnum numeric
declare @partic varchar(200)
declare @ar numeric
declare @renttype int
declare @lttype int
declare @rent1 money
declare @rent2 money
declare @wingno int
declare @discount money
declare @fptype int
declare @sc money
declare @autobillamt money
declare @emsg varchar(2000)
declare @blocked varchar(2000)
declare @myctrlno numeric
declare @rno varchar(10)
declare @channelexbed money
declare @food money
begin transaction
set @mop=@advmop
set @txndt = getdate()
--if datediff(d,@arrivaldate,getdate()) > 0  begin print 'No reservation is allowed for past date' set @emsg = 'No reservation is allowed for past date' goto e_error end
set @emsg='Error in reservation'

declare @chdisc money
declare @upfront int
declare @chglactno numeric
declare @commissionac numeric 
declare @chdiscamt money
declare @chNettRate tinyint
declare @extrabed money 
set @chdiscamt = 0
--set @emsg = 'Cahnnel '+ str(@ischannel) + convert(varchar(20),@advamount)
IF DATEDIFF(d,@arrivaldate,getdate()) >0
begin
set @emsg = 'Past Date Booking not allowed' + convert(varchar(39),@arrivaldate)
goto E_error
end
--goto e_error
if @ischannel=1 
	begin
	select @chdisc=discount,@upfront=upfront,@chglactno=glactno,@commissionac=commissionac,@chnettrate=nettrate  from staahchannel where staahname=@channel
	if @upfront=0
		begin
		--set @advamount = @channeladv
		set @mop = 4
		set @glactno = @chglactno
		end
	end

-- This portion is included on 12-01-2014 for STAAH integration
if @ischannel=1 
begin
	if @channelamt <> 0
		begin
		if @upfront=0
			begin
			set @chdiscamt =0 
			if @chdisc <> 0 and @chnettrate=0
				begin
					set @chdiscamt = @channelamt * @chdisc/100
				end
				--set @advamount = @channeladv --+ @chdiscamt
				set @glactno = @chglactno
				set @mop=4
				set @crname=@channel + ' Booking ID : ' + @staahid
				--declare @staahpartic varchar(100)
				--set @staahpartic = 'Commission to ' + @channel + '-ID : ' + @staahid                
				--exec  getfy @txndt , 1,@fy out
				--if @fy=0 goto e_error
				--if @@error <> 0 goto e_error
				--exec getgrp @txndt,1,@grno out
				--if @grno=0 goto e_error
				--if @@error <> 0 goto e_error
				--	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
				--	values (1, @fy,  @commissionac ,@chdiscamt , @staahpartic  , 'D', @txndt , @grno ,'Y', 0,@sessionid)
				--	if @@error <> 0 goto e_error
				--	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
				--	values (1, @fy,  @chglactno ,@chdiscamt ,@staahpartic , 'C', @txndt , @grno ,'Y',0,@sessionid)
				--	if @@error <> 0 goto e_error
			end
		end
end


if @cno<>0 select @name=name,@add1=add1,@add2=add2,@add3=add3,@city=city,@state=state,@pin=pin,@phone=phone,@email=email from guestad where cno=@cno
insert into sptbrs( cno,bookingdate,arrivaldate,departuredate,usernm,instructions,advancerecd,bookedby,pickup,pickupinstrn,pickupreqd,pickupstation,pickupfltno,net , company,tentative,tent_upto,
address,name,add1,add2,add3,city,state,pin,phone,fax,email,memorandum,renttype1,mktmgr,
channel,ischannel,channelamt,staahid,dobm,dobd,domm,domd,extrabeds,ExtraBedCharges,gstin,
commrate,commgstrate ,commtcsrate,commtdsrate,commission,commgst,commtcs,commtds,staahstatus
) values
(@cno,getdate(),@arrivaldate,@departuredate,@usernm,@instructions,@advamount + @chdiscamt,@bookedby,@pickup,@pickupinstrn,@pickupreqd,@pickupstation,@pickupfltno,@net,@company,case when @tentative=0 then 'N' else 'Y' end , @dttent ,
@address,@name,@add1,@add2,@add3,@city,@state,@pin,@phone,@fax,@email,@memo,@renttype1,@mktmgr,
@channel,@ischannel,@channelamt,@staahid,@dobm,@dobd,@domm,@domd,@extrabeds,@extrabedcharges,@gstin,
@commrate,@commgstrate,@commtcsrate,@commtdsrate,@commission,@commgst,@commtcs,@commtds,'Commit'
)
if @@error <> 0 goto e_error

select @ctrlno=@@identity
if @net='Y'
begin
insert  into sptbrs_inet (ctrlno,doc_name,doc_no,paymentId,trackid,udf1,udf2,udf3,udf4) values (@ctrlno,@doc_name,@doc_no,@paymentid,@trackid,@udf1,@udf2,@udf3,@udf4)
if @@error<>0 goto e_error
end

set @sql = 'declare tempcur cursor for select roomtype,noofrooms,rentagreed,instructions,renttype,lttype,wingno,discount,sc,rent1,rent2,fptype,autobillamt,blocked,channelexbed,isnull(extrabed,0) ,food from ' + @t1
exec(@sql)
open tempcur
fetch next from tempcur into @roomtype,@noofrooms,@rentagreed,@instructions,@renttype,@lttype,@wingno,@discount,@sc,@rent1,@rent2,@fptype,@autobillamt,@blocked,@channelexbed,@extrabed,@food
while @@fetch_status=0
begin
/*
if @net='Y'
begin
exec pbpga6 @arrivaldate,@departuredate,1,@roomtype,@ar out
if @@error <> 0 goto e_error
if @ar < @noofrooms begin print 'Sorry....the requested rooms sold out during your process'  set @emsg='Sorry....the requested rooms sold out during your process' goto e_error  end
end
*/

set @autobillamt=isnull(@autobillamt,0)
--set @emsg=str(@wingno) + 'ERRRROr' goto e_error
insert into sptbrs1 (ctrlno,roomtype,noofrooms,rentagreed,instructions,renttype,lttype,wingno,discount,sc,rent1,rent2,fptype,autobillamt,channelexbed,channelexbedok,extrabed,food)  values
(@ctrlno,@roomtype,@noofrooms,@rent1,@instructions,@renttype,@lttype,@wingno,@discount,@sc,@rent1,@rent2,@fptype,isnull(@autobillamt,0),isnull(@channelexbed,0),0,ISNULL(@extrabed,0),ISNULL(@food,0))
if @@error <> 0 goto e_error
set @myctrlno = ident_current('sptbrs1')
--print @blocked goto e_error
if ltrim(rtrim(@blocked)) <> ''
begin
declare tempc1 cursor for select value from fn_split(@blocked,',')
open tempc1
fetch next from tempc1 into @rno
while @@fetch_status=0
begin
insert into sptbrooms (rno,myctrlno) values (@rno,@myctrlno)
fetch next from tempc1 into @rno
end
close tempc1
deallocate tempc1
end
fetch next from tempcur into @roomtype,@noofrooms,@rentagreed,@instructions,@renttype,@lttype,@wingno,@discount,@sc,@rent1,@rent2,@fptype,@autobillamt,@blocked,@channelexbed,@extrabed,@food
end
close tempcur
deallocate tempcur
-- new concept from October 2008
if @cch_amt<>0 
begin
	insert into crcardhold (holdtype,gno,txndt,crnum,crname,crbank,amount,duedt,status,usernm,refno)
	values
	(2,@ctrlno,getdate(),@cch_crno,@cch_crname,@cch_crbank,@cch_amt,@cch_duedt,' ',@usernm,@cch_refno)
end
if @advamount <> 0
	begin
	exec  getfy @txndt , 1,@fy out
	if @fy=0 goto e_error
	if @@error <> 0 goto e_error
	exec getgrp @txndt,1,@grno out
	if @grno=0 goto e_error
	if @@error <> 0 goto e_error
	if datepart(mm,@txndt) >- 1 and datepart(mm,@txndt) <= 3
	   set @year = datepart(yy,@txndt)-1
	else
	  set @year = datepart(yy,@txndt)
	select @bofy = '1 apr ' + str(@year)
	update finyear set advbfano=case when datediff(d,'1/1/80',advbfadate)= datediff(d,'1/1/80',@bofy) then advbfano+1 else 1 end , advbfadate=@bofy  where fy=@fy
	if @@error<>0 goto e_error
	select @arno=advbfano from finyear where fy=@fy
	insert into sptbrs_ad (ctrlno,txndt,amount,grno,mop,crnum,crname,crtype,glactno,usernm,amttyp,receiptno) values
	(@ctrlno,@txndt,@advamount,@grno,@advmop,@crnum,@crname,@crtype,@glactno,@usernm,'C',@arno)
	if @@error<>0 goto e_error
if exists(select * from setting where g_finaccount=1) 
begin
	select @actnum= case 
	  when @mop=1 then 1
	  when @mop=2 then 22
	  when @mop=3 then 46
	  when @mop=4 then @glactno
	                                end
	select @partic = case
	  when @mop=1 then 'To  adv.ag. Res. No. ' + ltrim(str(@arno))
	  when @mop=2 then 'To  adv.ag. Res. No. ' + ltrim(str(@arno))+ ' ' + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype) 
	  when @mop=3 then 'To  adv.ag. Res. No. ' + ltrim(str(@arno))+ ' ' + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype) 
	  when @mop=4 then 'To  adv.ag. Res. No. ' + ltrim(str(@arno))+ ' ' + ltrim(@crname) + ' ' + 'adv.ag. Res. No. ' + ltrim(str(@ctrlno))
	  end
	--set @emsg=str(@actnum)+'error' goto e_Error
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
	values (1, @fy,  @actnum ,@advamount , @partic  , 'D', @txndt , @grno ,'Y', @arno,@sessionid)
	if @@error <> 0 goto e_error
	  	
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
	values (1, @fy,  56 ,@advamount ,@partic + @crname , 'C', @txndt , @grno ,'Y', @arno,@sessionid)
	if @@error <> 0 goto e_error
end
if @ischannel=1 and @upfront=0  and @chnettrate=0
	if @chdiscamt <> 0
	begin
	exec getgrp @txndt,1,@grno out
	if @grno=0 goto e_error
	if @@error <> 0 goto e_error
		set @advamount = @chdiscamt
		set @mop=4
		select @actnum=@commissionac
		select @glactno=@commissionac
		select @partic = 'To ' + ltrim(@crname) + ' ' + 'adv.ag. Res. No. ' + ltrim(str(@ctrlno))

		update finyear set advbfano=case when datediff(d,'1/1/80',advbfadate)= datediff(d,'1/1/80',@bofy) then advbfano+1 else 1 end , advbfadate=@bofy  where fy=@fy
		if @@error<>0 goto e_error
		select @arno=advbfano from finyear where fy=@fy
			

		insert into sptbrs_ad (ctrlno,txndt,amount,grno,mop,crnum,crname,crtype,glactno,usernm,amttyp,receiptno) values
		(@ctrlno,@txndt,@advamount,@grno,@advmop,@crnum,'COMMISSION PAID ACCOUNT INTERNET BOOKINGS',@crtype,@glactno,@usernm,'C',@arno)
		if @@error<>0 goto e_error
		if exists(select * from setting where g_finaccount=1) 
		begin
			insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
			values (1, @fy,  @actnum ,@advamount , @partic  , 'D', @txndt , @grno ,'Y', @arno,@sessionid)
			if @@error <> 0 goto e_error
			insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,sessionid) 
			values (1, @fy,  56 ,@advamount ,'By adv.ag. Res. No. ' +ltrim( str(@ctrlno)) , 'C', @txndt , @grno ,'Y', @arno,@sessionid)
			if @@error <> 0 goto e_error
		end
	end

	 
final:
end
	insert into staahinv (txndt,edt) values (@arrivaldate,@departuredate)

set @sql = 'drop table ' + @t1
exec(@sql)
commit transaction
return
e_error:
rollback transaction
set @sql  = 'drop table ' + @t1
exec(@sql)
raiserror(@emsg,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[sprsup1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sprsup1    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sprsup1] @ctrlno numeric ,@cno numeric ,@arrivaldate datetime , @departuredate datetime , @usernm varchar(20),
@instructions varchar(200) , @name varchar(50), @add1 varchar(50) , @add2 varchar(50) , @add3 varchar(50),
@city varchar(50), @state varchar(50), @pin varchar(50), @phone varchar(50),@fax varchar(50), @email varchar(50),
@t1 varchar(50),@address varchar(10),@bookedby varchar(400),
@pickupinstrn varchar(100),@pickup datetime, @pickupreqd int,
@pickupstation varchar(50), @pickupfltno varchar(50),@tentative int=0,@tentdate datetime= '1 jan 2007',@memo varchar(200)='',@renttype1 int=1,
@extrabeds int=0,@extrabedcharges money = 0,@voucherno varchar (100)='',@gstin nvarchar(50)='',
@commrate numeric(18,2)=0,
@commgstrate  numeric(18,2)=0,
@commtcsrate  numeric(18,2)=0,
@commtdsrate  numeric(18,2)=0,
@commission  numeric(18,2)=0,
@commgst  numeric(18,2)=0,
@commtcs  numeric(18,2)=0,
@commtds  numeric(18,2)=0
/* with encryption */
AS
declare  @roomtype varchar(50)
declare @noofrooms numeric
declare @rentagreed money
declare @instructions1 varchar(50)
declare @advrno numeric
declare @fy int
declare @sql varchar(500)
declare @grno numeric
declare @txndt datetime
declare @bofy datetime
declare @year int
declare @arno numeric
declare @mop int
declare @actnum numeric
declare @partic varchar(200)
declare @renttype int
declare @lttype int
declare @rent1 money
declare @rent2 money
declare @wingno int
declare @discount money
declare @fptype int
declare @sc money
declare @autobillamt money
declare @blocked varchar(8000)
declare @rno numeric
declare @myctrlno numeric
declare @err varchar(5000)
declare @extrabed money
declare @food money
declare @emsg varchar(200)

set @emsg = 'Error in Reservation Editing'

if exists (select * from sptbrs_v1 where ctrlno=@ctrlno and (arrivedrooms+cancelledrooms) <> 0 )
begin
set @err =  'This entry contains some arrived/cancelled rooms.So edititng is not allowed' + char(13) + 'If only address editing is required you may use the archive editing' + char(15) + ' in the Edit Guest Details Option'
raiserror(@err,16,-1)
return
end

begin transaction
set @txndt = getdate()
select @cno=cno from sptbrs where ctrlno=@ctrlno
/*
update guestad set [name]=@name,add1=@add1,add2=@add2,add3=@add3,city=@city,state=@state,pin=@pin,phone=@phone,fax=@fax,email=@email,
address=@address where cno=@cno
if @@error<>0 goto e_error
*/
--update sptbrs set arrivaldate=@arrivaldate,departuredate=@departuredate,usernm=@usernm,
update sptbrs set 
arrivaldate=@arrivaldate,
departuredate=@departuredate,usernm=@usernm,
pickup=@pickup,
pickupinstrn=@pickupinstrn,
pickupreqd=@pickupreqd,
pickupstation=@pickupstation,
pickupfltno=@pickupfltno,
bookedby=@bookedby,
tentative=case when @tentative=1 then 'Y' else 'N' end,
tent_upto=@tentdate,
[address]=@address,
name=@name,
add1=@add1,
add2=@add2,
add3=@add3,
city=@city,
[state]=@state,
phone=@phone,
email=@email,
memorandum=@memo,
renttype1=@renttype1,
extrabeds = @extrabeds,
ExtraBedCharges=@extrabedcharges,
staahid=@voucherno,
gstin=@gstin,
commrate=@commrate  ,
commgstrate=@commgstrate   ,
commtcsrate=@commtcsrate   ,
commtdsrate=@commtdsrate   ,
commission=@commission   ,
commgst=@commgst   ,
commtcs=@commtcs   ,
commtds=@commtds   ,
staahstatus='Modify'

where ctrlno = @ctrlno
if @@error <> 0 goto e_error

declare @updated tinyint
set @updated = 0
--- new snippet 10 aug 2012
declare c11 cursor for select myctrlno from sptbrs1 where ctrlno=@ctrlno
open c11
fetch next from c11 into @myctrlno
while @@fetch_status=0
begin
exec sptbroomsupdate @myctrlno,1
delete from sptbrooms where myctrlno=@myctrlno
fetch next from c11 into @myctrlno
end
close c11
deallocate c11

delete from sptbrs1 where ctrlno = @ctrlno
set @sql = 'declare tempcur cursor for select roomtype,noofrooms,rentagreed,instructions,renttype,lttype,wingno,discount,sc,rent1,rent2,fptype,autobillamt,blocked,extrabed,food from ' + @t1
exec(@sql)
open tempcur
fetch next from tempcur into @roomtype,@noofrooms,@rentagreed,@instructions,@renttype,@lttype,@wingno,@discount,@sc,@rent1,@rent2,@fptype,@autobillamt,@blocked,@extrabed,@food
while @@fetch_status=0
	begin
	insert into sptbrs1 (ctrlno,roomtype,noofrooms,rentagreed,instructions,renttype,lttype,wingno,discount,sc,rent1,rent2,fptype,autobillamt,food,extrabed)  values
	(@ctrlno,@roomtype,@noofrooms,@rent1,@instructions,@renttype,@lttype,@wingno,@discount,@sc,@rent1,@rent2,@fptype,@autobillamt,isnull(@food,0),isnull(@extrabed,0))
	if @@error <> 0 goto e_error
	set @myctrlno = ident_current('sptbrs1')
		if ltrim(rtrim(@blocked)) <> ''
			begin
			declare tempc1 cursor for select value from fn_split(@blocked,',')
			open tempc1
			fetch next from tempc1 into @rno
			while @@fetch_status=0
				begin
				insert into sptbrooms (rno,myctrlno) values (@rno,@myctrlno)
				fetch next from tempc1 into @rno
				end
			close tempc1
			deallocate tempc1
			end
	set @updated=1
	fetch next from tempcur into @roomtype,@noofrooms,@rentagreed,@instructions,@renttype,@lttype,@wingno,@discount,@sc,@rent1,@rent2,@fptype,@autobillamt,@blocked,@extrabed,@food
	end
close tempcur
deallocate tempcur

set @sql = 'drop table ' + @t1
exec(@sql)

--- new snippet 10 aug 2012
declare c11 cursor for select myctrlno from sptbrs1 where ctrlno=@ctrlno
open c11
fetch next from c11 into @myctrlno
while @@fetch_status=0
begin

exec sptbroomsupdate @myctrlno,0
if @@error<>0  goto e_error
--delete from sptbrooms where myctrlno=@myctrlno
fetch next from c11 into @myctrlno
end
close c11
deallocate c11


--select @usernm=usernm from comuser where [host_id]=host_id()
-- this is already received as parameter
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, 'Details Edited.CN.Reservation No.' +STR(@ctrlno) ,25,0)
if @@error <> 0 goto e_error
if @updated=0 
begin
print 'There seems tobe some error in the data.Please verify your changes'
goto e_error
end
commit transaction
return
e_error:
rollback transaction
set @sql  = 'drop table ' + @t1
exec(@sql)
raiserror(@emsg,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[spshrm]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spshrm    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spshrm] @orno varchar(20),@nrno varchar(20) 
/* with encryption */
AS
begin transaction
declare @gno numeric
declare @grpname varchar(50)
declare @rent money
declare @usernm varchar(50)
declare @ci char(1)
declare @std char(1)
declare @qcheck char(1)
declare @ci_date datetime
declare @guestname varchar(200)
declare @oldrent numeric(18,2)
if exists(select * from room where rnoa=@orno and ltrim(checkout)<>'' and rtrim(ltrim(hostid)) <> '')
begin
print 'The Selected Room is under Checkout Process by some other Terminal...Shifting Rejected'
goto e_error
end

if exists(select * from room where rnoa=@orno and gno=0) 
   begin
   print 'The Room seems to be shifted by some other terminal user during your session...Room not shifted'
   goto e_error
   end

if exists(select * from room where rnoa=@nrno and gno<>0)
   begin
   print 'The New room has been occupied by someone else during your session....Romm not shifted'
   goto e_error
   end
select @rent=rent1,@gno=gno,@grpname=grpname,@qcheck=qcheck,@ci=ci,@std=std  from room where rnoa=@orno
update groom set co_date = getdate() where gno = @gno and co_date is null
if @@error<>0 goto e_error
update room set gno=0,grpname='',ci='',std='',qcheck='N',status='A' where rnoa=@orno and gno=@gno
if @@error<>0 goto e_error
update room set gno=@gno,grpname=@grpname,ci=@ci,std=@std,qcheck=@qcheck where rnoa=@nrno and gno=0
if @@error<>0 goto e_error
update guest set rno= (select rno from room where rnoa=@nrno) where gno=@gno
if @@error<>0 goto e_error
insert groom  (GNO,CI_DATE,RNO,rent) values (@gno,getdate(),(select rno from room where rnoa=@nrno),@rent)
if @@error<>0 goto e_error
declare @pax numeric
select @oldrent=rent,@guestname=name,@pax=male+female,@grpname=name,@ci_date=ci_date from guest where gno=@gno
if exists(select * from setting where shiftrent=1)
begin
 --   print datediff(n,@ci_date,getdate())
	--goto e_error
	--if datediff(n,@ci_date,getdate()) < 120
	--begin
		declare @renttype int
		declare @tariff as money
		declare @nettrent money
		select @renttype=renttype1,@rent=rent,@nettrent=nettrent from guest where gno=@gno
		DECLARE @DT DATETIME
		SET @DT = GETDATE()
		EXEC findrent @renttype,@Nrno,@pax,@DT,0,@tariff out
		if @rent=@nettrent
		begin
			UPDATE GUEST SET RENT=@TARIFF,nettrent=@tariff WHERE GNO=@GNO
			IF @@ERROR<> 0 GOTO E_ERROR
			insert into acclog (narration,txntype,gno) values ('Rent changed during shifting-' + ltrim(rtrim(str(@gno))) +' Guest Name :'+ @guestname + '-Old Rent :' + ltrim(rtrim(str(@oldrent))) + ' New Rent :' + ltrim(rtrim(str(@tariff))),70,@gno)
		end
	--end
end
select @usernm=nullif(usernm,'Not found')  from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
--print @usernm
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),host_name()+'-'+@usernm, ' Room No. ' + ltrim(rtrim(@orno))+ ' shifted to ' + ltrim(@nrno) + ' Guest - '+@grpname  ,13,@gno)
if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Room shifting error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[SPSTAdj]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[SPSTAdj]
	@hctrlno numeric,@balance numeric(18,4)
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @ctrlno numeric
	declare @bctrlno numeric
	declare @storeno numeric
	declare @txndt datetime
	declare @usernm varchar(50)
	--declare @balance numeric(18,4)
	declare @temp varchar(20)
	declare @sql varchar(5000)
	declare @luperhu numeric
	exec randfile @temp out
	set @temp = 'STAD' + @temp
	select @usernm=usernm from comuser where computer = host_name() and sessionid=''
	select @usernm=nullif(usernm,'Not found')  from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
	exec ('select * into ' + @temp + ' from dummysp')
	select @ctrlno=ctrlno,@bctrlno=bctrlno,@txndt=txndt from storehist where hctrlno=@hctrlno
	select @storeno=storeno,@luperhu=luperhu from storeitems where ctrlno=@ctrlno
	set @balance = @balance * -1
	set @sql = 'insert into ' + @temp + ' (sno,ctrlno,qty,price,glactno,saletax,sgstamt,cgstamt,igstamt,cessamt,cgstrate,sgstrate,igstrate,cessrate,oldqty,oldctrlno,exciserate,exciseamt,tcsrate,tcsamt) values (1,' + str(@ctrlno) + ',' + convert(varchar(20),@balance) + ' ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)'
	exec(@sql)
	exec spstpuradjnew @storeno,@txndt,@temp,@usernm,@bctrlno out
    if exists (select name,xtype from sysobjects where name = @temp and xtype = 'U') drop table [@temp]
END




GO
/****** Object:  StoredProcedure [dbo].[spsthrewrite]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spsthrewrite] @ctrlno numeric,@fy numeric,@bofy datetime,@eofy datetime,
@update int=1,@forupdt int=1 ,@fromregister int=1
/* with encryption */
AS
begin transaction
declare @balance numeric
declare @hctrlno numeric
declare @qty numeric
declare @amttyp char(1)
declare @nfy numeric
declare @cfy numeric
declare @crs money
declare @drs money
declare @ltxndt datetime
declare @ntxndt datetime
declare @txndt datetime 
declare @opgbal numeric(18,4)
declare @msg varchar(1000)

set @balance=0

if not exists(select * from finyear where fy=@fy and company=
	(select company from storeitems where ctrlno=@ctrlno)) begin print 'Wrong Item Code and Fin.year' return end

select @bofy=date from finyear where fy=@fy
select @eofy=dbo.geteofy(@bofy)

if not exists(select * from storeprimary where fy=@fy and ictrlno=@ctrlno)
	insert into storeprimary (fy,ictrlno) values (@fy,@ctrlno)



select @ltxndt=ltxndt from storeitems where ctrlno=@ctrlno 

select @eofy=dbo.geteofy(@bofy)
if @fromregister<>1
	begin
		select @balance=isnull(opgbal,0) from storeprimary where ictrlno=@ctrlno and fy=@fy
		set @txndt=@bofy
	end
else
	begin
		if datediff(d,'1/1/2000',@ltxndt) between datediff(d,'1/1/2000',@bofy) and datediff(d,'1/1/2000',@eofy)
			begin
			declare @ltxndt1 datetime
			select @ltxndt1=max(txndt) from storehist where ctrlno=@ctrlno and datediff(d,'1/1/2000',txndt) between datediff(d,'1/1/2000',@bofy) and datediff(d,'1/1/2000',@eofy)
			if @ltxndt1 = null 
				begin
					select @balance=isnull(opgbal,0) from storeprimary where ictrlno=@ctrlno and fy=@fy
					set @txndt=@bofy
				end
			else
				begin
					if exists(select * from storehist where ctrlno=@ctrlno and datediff(d,txndt,@ltxndt)>0) 
						begin
						select @ltxndt1=max(txndt) from storehist where ctrlno=@ctrlno
						if @ltxndt1 < @ltxndt set @ltxndt=@ltxndt1
						select top 1 @balance=balance from storehist where ctrlno=@ctrlno and datediff(d,txndt,@ltxndt)>0  order by txndt desc, amttyp desc,hctrlno2 desc
						set @txndt = @ltxndt
						--print convert(varchar(20),@ltxndt) + ' ' + convert(varchar(20),@balance) + ' here'-- goto e_error
						end
						else
						begin
						select @balance=isnull(opgbal,0) from storeprimary where ictrlno=@ctrlno and fy=@fy
						set @txndt=@bofy
						end
				end
			end
		else
			if @ltxndt > @eofy 
				begin
				select @txndt=@bofy
				select @balance=isnull(opgbal,0) from storeprimary where ictrlno=@ctrlno and fy=@fy
				end
			else
				begin
				select @balance=isnull(opgbal,0) from storeprimary where ictrlno=@ctrlno and fy=@fy
				set @txndt=@bofy
				end
	end
set @ntxndt = getdate()
--select top 1 @txndt=txndt from storehist where ctrlno=@ctrlno and DATEDIFF(d,txndt,@txndt)  > 0 order by txndt desc,amttyp desc ,hctrlno2 desc

--print 'periya'  + str(@fy) + ' ctrlno ' +  STR(@ctrlno) + ' Balance '  + convert(varchar(20),@balance) + convert(varchar(20),@txndt) + ' ' + convert(varchar(20),@bofy)


if @txndt < @bofy 
begin 
set @txndt=@bofy  
select @balance=isnull(opgbal,0) from storeprimary where ictrlno=@ctrlno and fy=@fy
end
set @amttyp='x'
set @qty=0
set @opgbal=@balance
--print  convert(varchar(20),@txndt) + ' '+ convert(varchar(20),@opgbal)  + ' '+  @amttyp + ' ' +  convert(varchar(20),@qty) + ' '+ convert(varchar(20),@balance)
--goto e_error

alter table storehist disable trigger [forltxndtdelete] alter table storehist disable trigger [forltxndtupdate] alter table storehist disable trigger [SHMinusBalance] alter table storehist disable trigger [spstdelete] alter table storehist disable trigger [spstupdate] alter table storehist disable trigger [stpurinsert] alter table storehist disable trigger [tg_updatehctrlno2]
declare csi cursor for select hctrlno,qty,amttyp,txndt from storehist where ctrlno=@ctrlno
and datediff(d,'1/1/90',txndt) between datediff(d,'1/1/90',@txndt) and datediff(d,'1/1/90',@eofy)
order by txndt,amttyp,hctrlno2
open csi
fetch next from csi into @hctrlno,@qty,@amttyp,@txndt
while @@fetch_status=0
	begin
		set @opgbal = @balance
		if @amttyp='C' 
		set @balance=@balance+@qty
		else
		set @balance=@balance-@qty
		set @msg= convert(varchar(20),@txndt)
--set nocount on
--print convert(varchar(20),@txndt) + ' '+ convert(varchar(20),@opgbal)  + ' '+  @amttyp + ' ' +  convert(varchar(20),@qty) + ' '+ convert(varchar(20),@balance)

--goto e_error

		--raiserror(@msg,10,-1)
		--update storehist set forupdt=case when @forupdt=0 then 'N' else 'Y' end where hctrlno=@hctrlno
		update storehist set forupdt='Y',balance=@balance where hctrlno=@hctrlno
		fetch next from csi into @hctrlno,@qty,@amttyp,@txndt
	end
close csi
deallocate csi

update storeitems set forupdt='N',ltxndt=getdate() where ctrlno=@ctrlno
update storehist set forupdt='N' where hctrlno=@hctrlno
alter table storehist enable trigger [forltxndtdelete] alter table storehist enable trigger [forltxndtupdate] alter table storehist enable trigger [SHMinusBalance] alter table storehist enable trigger [spstdelete] alter table storehist enable trigger [spstupdate] alter table storehist enable trigger [stpurinsert] alter table storehist enable trigger [tg_updatehctrlno2] 

if @update=1 exec SpStRewriteSTOpgbal @ctrlno
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return


GO
/****** Object:  StoredProcedure [dbo].[spstiss]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spstiss    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spstiss] @storeno int,@txndt datetime,@t1 varchar(50),@olcode numeric,@oltype int,
@usernm varchar(50),@bctrlno numeric output
/* with encryption */
AS
declare @sql varchar(5000)
declare @ctrlno int
declare @qty float
declare @price money
declare @chlno int
declare @rate money
--print 'periya'
declare @company int
declare @m_year int
declare @m_bofy datetime
declare @fy int
declare @luperhu numeric
declare @fa_posting char(1)
declare @eofy datetime
begin transaction
select @company=company,@fa_posting=fa_posting from store where storeno = @storeno
set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)

EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
if @@error<>0 goto e_error
if not exists(select * from stchlno where storeno=@storeno and chlndt =@m_bofy)
  begin
   insert into stchlno (storeno,chlndt,pchlno,dchlno,adjcr,adjdr) values (@storeno,@m_bofy,0,0,0,0)
   if @@error<>0 goto e_error
   end
UPDATE stchlno  set dchlno= dchlno+1  WHERE storeno= @storeno and chlndt=@m_bofy
if @@error<>0 goto e_error
select @chlno=dchlno from stchlno  WHERE storeno= @storeno and chlndt=@m_bofy
insert into storebills (storeno,amttyp,chlno,txndt,usernm,invoiceno,supplier,grno,issuedtocode,issuedtotype) values
 (@storeno,'D',@chlno,@txndt,@usernm,'',0,0,@olcode,@oltype)
if @@error<>0 begin print 'storebills insert failed' goto e_error end
select @bctrlno=@@identity
set @sql = 'declare tempcur1 cursor for select ctrlno,qty,price,strate  from ' + @t1 + ' order by sno'
exec(@sql)
if @@error<>0 goto e_error
open tempcur1
fetch next from tempcur1 into @ctrlno,@qty,@price,@rate
while @@fetch_status=0
begin
	select @luperhu=luperhu from storeitems where ctrlno=@ctrlno
	set @price=round((@qty/@luperhu)*@rate,2)
	insert into storehist (bctrlno,ctrlno,amttyp,qty,rate,chlno,price,txndt) values (@bctrlno,@ctrlno,'D',@qty,@rate,@chlno,@price,@txndt)
	if @@error<>0 goto e_error
	set @eofy = '31 mar ' + ltrim(str(year(@m_bofy)+1))
	--exec spsthrewrite @ctrlno,@fy,@m_bofy,@eofy,1
	--exec spsthrewrite @ctrlno,@fy,@txndt,@eofy,0
	--exec fillstock1 @ctrlno,@txndt
	fetch next from tempcur1 into @ctrlno,@qty,@price,@rate
end
close tempcur1
deallocate tempcur1
--print 'okya periya'
commit transaction
set @sql = 'drop table '+ @t1
exec( @sql)
return
e_error:
rollback transaction
set @sql = 'drop table '+ @t1
exec @sql
raiserror('error in purchase',16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[spstissdir]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spstiss    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spstissdir]  @pctrlno numeric ,@olcode numeric,@oltype int,@usernm varchar(50),@bctrlno numeric output
/* with encryption */
AS
declare @storeno numeric
declare @txndt datetime
declare @sql varchar(5000)
declare @ctrlno int
declare @qty float
declare @price money
declare @chlno int
declare @rate money
--print 'periya'
declare @company int
declare @m_year int
declare @m_bofy datetime
declare @fy int
declare @luperhu numeric
begin transaction
set nocount on
select @storeno=storeno,@txndt=txndt from storebills where bctrlno=@pctrlno
select @company=company from store where storeno = @storeno
--IF DATEPART(mm,@TXNDT) >=  1 AND DATEPART(mm,@TXNDT) <= 3
--     SET @M_YEAR = DATEPART(yy,@TXNDT)-1
--ELSE
--     SET @M_YEAR = DATEPART(yy,@TXNDT)
--SELECT @M_BOFY = '1 APR ' + STR(@M_YEAR)

set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)
--print 'periya'

EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
if @@error<>0 goto e_error


if not exists(select * from stchlno where storeno=@storeno and chlndt =@m_bofy)
  begin
   insert into stchlno (storeno,chlndt,pchlno,dchlno,adjcr,adjdr) values (@storeno,@m_bofy,0,0,0,0)
   if @@error<>0 goto e_error
   end
UPDATE stchlno  set dchlno= dchlno+1  WHERE storeno= @storeno and chlndt=@m_bofy
if @@error<>0 goto e_error
select @chlno=dchlno from stchlno  WHERE storeno= @storeno and chlndt=@m_bofy
insert into storebills (storeno,amttyp,chlno,txndt,usernm,invoiceno,supplier,grno,issuedtocode,issuedtotype) values
 (@storeno,'D',@chlno,@txndt,@usernm,'',0,0,@olcode,@oltype)
if @@error<>0 begin print 'storebills insert failed' goto e_error end
set @bctrlno=@@identity
--print 'periya1'

select ctrlno,qty,price into #dummy from storehist where bctrlno=@pctrlno
declare tempcur1 cursor for select ctrlno,qty,price from #dummy
open tempcur1
fetch next from tempcur1 into @ctrlno,@qty,@price
while @@fetch_status=0
begin
   select @rate=rate,@luperhu=luperhu from storeitems where ctrlno=@ctrlno
   set @price=round((@qty/@luperhu)*@rate,2)
   insert into storehist (bctrlno,ctrlno,amttyp,qty,rate,chlno,price,txndt) values (@bctrlno,@ctrlno,'D',@qty,@rate,@chlno,@price,@txndt)
   if @@error<>0 goto e_error
--update storeitems set stock=stock-@qty where ctrlno=@ctrlno
   --exec fillstock1 @ctrlno,@txndt
   fetch next from tempcur1 into @ctrlno,@qty,@price
end
close tempcur1
deallocate tempcur1
update storebills set reissue='Y' where bctrlno=@pctrlno
commit transaction
return
e_error:
rollback transaction
raiserror('error in purchase',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spstisse]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spstiss    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spstisse] @storeno int,@txndt datetime,@t1 varchar(50),@olcode numeric,@oltype int,
@usernm varchar(50),@bctrlno numeric 
/* with encryption */
AS
--print str(@bctrlno) goto e_error
declare @sql varchar(5000)
declare @ctrlno int
declare @qty float
declare @price money
declare @chlno int
declare @rate money
--print 'periya'
declare @company int
declare @m_year int
declare @m_bofy datetime
declare @fy int
declare @luperhu numeric
declare @hctrlno numeric
declare @hctrlno2  numeric
begin transaction
select @company=company from store where storeno = @storeno
set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)

EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
if @@error<>0 goto e_error
update storebills set
storeno=@storeno,
txndt=@txndt,
usernm=@usernm,
issuedtocode=@olcode,
issuedtotype=@oltype
where bctrlno = @bctrlno
if @@error<>0 begin print 'Error in updating storebills' goto e_error end

select @chlno=chlno from storebills where bctrlno=@bctrlno
set @sql = 'declare cc cursor for select hctrlno2 from storehist where bctrlno=' + STR(@bctrlno) + ' and hctrlno2 not in (select  hctrlno2 from ' + @t1 + ')'
exec(@sql)
open cc
fetch next from cc into @hctrlno2
while @@fetch_status=0
begin
update storehist set forupdt = 'Y' where hctrlno2=@hctrlno2
delete from storehist where hctrlno2=@hctrlno2
fetch next from cc into @hctrlno2
end
close cc
deallocate cc
declare @octrlno numeric
declare @oqty float
declare @balance float
set @sql = 'declare tempcur1 cursor for select ctrlno,qty,price,strate,hctrlno2,oldqty,oldctrlno from ' + @t1 + ' order by sno'
exec(@sql)
if @@error<>0 goto e_error
open tempcur1

fetch next from tempcur1 into @ctrlno,@qty,@price,@rate,@hctrlno2,@oqty,@octrlno
while @@fetch_status=0
begin
   select @luperhu=luperhu from storeitems where ctrlno=@ctrlno
   set @price=round((@qty/@luperhu)*@rate,2)

if @hctrlno2 <> 0 and ((@ctrlno=@octrlno and @qty <> @oqty) or (@ctrlno<>@octrlno))
	BEGIN
	update storeitems set stock=stock+@Oqty where ctrlno=@octrlno
	if @@error<>0 rollback transaction
	--update storeprimary set balance=balance+@oqty where fy=@fy and ictrlno=@octrlno
	--if @@error<>0 rollback transaction

	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

	update storehist set bctrlno=@bctrlno,ctrlno=@ctrlno,qty=@qty,rate=@rate,price=@price,txndt=@txndt where hctrlno2=@hctrlno2
	END
else
	if @hctrlno2=0
	    begin
		insert into storehist (bctrlno,ctrlno,amttyp,qty,rate,chlno,price,txndt) values (@bctrlno,@ctrlno,'D',@qty,@rate,@chlno,@price,@txndt)
        if @@error<>0 goto e_error
		end
	fetch next from tempcur1 into @ctrlno,@qty,@price,@rate,@hctrlno2,@oqty,@octrlno
end
close tempcur1
deallocate tempcur1
--print 'periya'

--------------------Checking of Minus Balance Starts   30 May 2016 Kottaiyur
--select * from #dumstore
declare @bofy datetime
declare @eofy datetime

select @bofy = dbo.getbofy(@txndt)
select @eofy = dbo.geteofy(@txndt)

declare @qty1 numeric(18,4)
declare @newqty numeric(18,4) 
declare @iname varchar(200) 
-------------------- posting starts
declare @stname varchar(100)
select @stname=storename from store where storeno=@storeno
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@stname))+ ' Issue Chalan No ' + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,6) + ' Edited ',153,0)

UPDATE STOREHIST SET TXNDT=@TXNDT WHERE BCTRLNO=@BCTRLNO

commit transaction
set @sql = 'drop table '+ @t1
exec( @sql)
return
e_error:
close tempcur1
deallocate tempcur1
rollback transaction
raiserror('error in purchase',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spstporC]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spstporC] @storeno int,@txndt datetime,@t1 varchar(50),@saletax  money,@txntyp int,
@glactno integer ,@sid  numeric,@invno varchar(50),@usernm varchar(50),@otherchgs money, @discount money,@stmode int, @bctrlno numeric output,
@cashtype int,@reissue char(1),@registered int = 0,@sellername varchar(50)='',@gstin varchar(15)=''
/* with encryption */
AS
declare @company int
declare @fy int
declare @sql varchar(5000)
declare @ctrlno int
declare @qty float
declare @price money
declare @pono int
declare @rate money
--print 'periya'
declare @m_bofy datetime
declare @m_year int
declare @totalamt money
declare @iglactno int
declare @itemamt money
declare @supplier varchar(50)
declare @grno int
declare @stax money
declare @luperhu money
declare @fa_posting char(1)
declare @freeze datetime
declare @strate money
declare @drate money
declare @damt money
declare @sno numeric
declare @suppglactno numeric
declare @eofy datetime
declare @damttot money
declare @cgstrate money
declare @sgstrate money
declare @igstrate money
declare @cgstamt money
declare @sgstamt money
declare @igstamt money
declare @totcgst money
declare @totsgst money
declare @totigst money
declare @totcess money
declare @totexcise money
declare @tottcs money
declare @cgstacno numeric
declare @sgstacno numeric
declare @igstacno numeric
declare @cessacno numeric
declare @exciseacno numeric
declare @tcsacno numeric
declare @cessrate money
declare @cessamt money
declare @exciserate money
declare @exciseamt money
declare @tcsrate money
declare @tcsamt money

declare @totcgst1 money    -- ITC not eligible
declare @totsgst1 money
declare @totigst1 money
declare @cgstacno1 numeric
declare @sgstacno1 numeric
declare @igstacno1 numeric
declare @registeredsupplier char(1) 
declare @itceligible as char(1) 
declare @cashpurchase int 
declare @msg varchar(5000) 

set @msg = 'Error in purchase'
set @damttot = 0
--declare @bctrlno numeric
begin transaction
select @freeze=freeze, @company=company,@fa_posting=fa_posting,
@cgstacno1=cgstacno,@sgstacno1=sgstacno,@igstacno1=igstacno,@cessacno=cessacno
,@exciseacno=exciseacno,@tcsacno=tcsacno      
from store where storeno = @storeno
-- the following is incrporated on 7 April 2020  as per the program for  GSTR2
set @cgstacno = 105
set @sgstacno = 106
set @igstacno = 107
set @cessacno = 108
set @exciseacno = 109 
set @tcsacno = 110

if datediff(d,@freeze,@txndt) <= 0 begin set @msg = 'Freezed Transaction...Not Allowed' goto e_error end
set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)


EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
if @@error<>0 goto e_error
----------

if not exists(select * from stchlno where storeno=@storeno and chlndt =@m_bofy)
	begin
	insert into stchlno (storeno,chlndt,pchlno,dchlno,adjdr,adjcr,pono) values (@storeno,@m_bofy,0,0,0,0,0)
	if @@error<>0 goto e_error
	end

update stchlno  set pono= pono+1  WHERE storeno= @storeno and chlndt=@m_bofy
if @@error<>0 goto e_error
select @pono=pono from stchlno  WHERE storeno= @storeno and chlndt=@m_bofy

if @fa_posting='Y'
if @txntyp=1 
begin
	declare @sfy numeric
	declare @found int
	set @found=0
	select @sfy=fy,@glactno=glactno from suppglac where supplier=@sid and fy=(select min(fy) from suppglac where supplier=@sid and fy in (select fy from finyear where company=@company) )
	while @sfy<>@fy begin
	   set @found=1
	   if exists(select  *  from glprimld where fy=@sfy and cgp_actno=@glactno)
			begin
				select  @sfy=nextyearfy,@glactno=nextyearac from glprimld where fy=@sfy and cgp_actno=@glactno
			end
				else
			break
	   if @sfy=@fy set @found=0
	end


	if @found<>0
	begin
	   set @msg = 'There is problem is finding the suppliers account for the selected financial year-S' + str(@sfy) + ' G'+ Str(@fy)
	  goto e_error
	end
end   -- type=1

--set @msg='Periya' goto e_error

insert into storepurchaseorder (storeno,pono,txndt,usernm,seller,saletax,otherchgs,discount) 
values (@storeno,@pono,@txndt,@usernm,@sid,@saletax,@otherchgs,@discount)
if @@error<>0 goto e_error
select @bctrlno=@@identity

set @totcgst = 0
set @totsgst = 0
set @totigst = 0
set @totcgst1 = 0
set @totsgst1 = 0
set @totigst1 = 0
set @totcess = 0
set @totexcise = 0
set @tottcs = 0
set @sql = 'declare tempcur1 cursor for select ctrlno,qty,price,saletax,strate,drate,damt,sno,cgstrate,cgstamt,sgstrate,sgstamt,igstrate,igstamt,cessrate,cessamt,exciserate,exciseamt,tcsrate,tcsamt from ' + @t1 + ' order by sno'
exec(@sql)
if @@error<>0 goto e_error
open tempcur1
fetch next from tempcur1 into @ctrlno,@qty,@price,@stax,@strate,@drate,@damt,@sno,
@cgstrate,@cgstamt,@sgstrate,@sgstamt,@igstrate,@igstamt,@cessrate,@cessamt
,@exciserate,@exciseamt,@tcsrate,@tcsamt

while @@fetch_status=0
begin
   select @luperhu=luperhu,@itceligible=itceligible from storeitems where ctrlno=@ctrlno
   set @rate      = round(@price / (@qty/@luperhu),2)
   set @damttot   = @damttot + @damt

	if @registeredsupplier = 'Y'
		begin
		if @itceligible='Y'
			begin
				set @totcgst = @totcgst + isnull(@cgstamt ,0)
				set @totsgst = @totsgst + isnull(@sgstamt ,0)
				set @totigst = @totigst + isnull(@igstamt ,0)
			end
			else
			begin
				set @totcgst1 = @totcgst1 + isnull(@cgstamt ,0)
				set @totsgst1 = @totsgst1 + isnull(@sgstamt ,0)
				set @totigst1 = @totigst1 + isnull(@igstamt ,0)
			end
		end
		else
		begin
			set @totcgst1 = @totcgst1 + isnull(@cgstamt ,0)
			set @totsgst1 = @totsgst1 + isnull(@sgstamt ,0)
			set @totigst1 = @totigst1 + isnull(@igstamt ,0)
		end

   set @totcess   = @totcess + isnull(@cessamt ,0) 
   set @totexcise = @totexcise + isnull(@exciseamt,0)
   set @tottcs    = @tottcs + isnull(@tcsamt,0)

   insert into storepohist (bctrlno,ctrlno,qty,rate,pono,price,saletax,strate,drate,
   damt,txndt,cgstrate,cgstamt,sgstrate,sgstamt,igstrate,igstamt,cessrate,cessamt,exciserate,exciseamt,tcsrate,tcsamt,HSN) 
   values 
   (@bctrlno,@ctrlno,@qty,@rate,@pono,@price,isnull(@stax,0),isnull(@strate,0),@drate,@damt,@txndt,
	isnull(@cgstrate,0),isnull(@cgstamt,0),isnull(@sgstrate,0),isnull(@sgstamt,0),isnull(@igstrate,0),isnull(@igstamt,0),isnull(@cessrate,0),isnull(@cessamt,0)
	,isnull(@exciserate,0) ,isnull(@exciseamt,0) ,isnull(@tcsrate,0),isnull(@tcsamt,0) ,'  '
   )
if @@error<>0 goto e_error
--update storeitems set stock=stock+@qty where ctrlno=@ctrlno

fetch next from tempcur1 into @ctrlno,@qty,@price,@stax,@strate,@drate,@damt,@sno,
@cgstrate,@cgstamt,@sgstrate,@sgstamt,@igstrate,@igstamt,@cessrate,@cessamt
,@exciserate,@exciseamt,@tcsrate,@tcsamt
end
close tempcur1
deallocate tempcur1
set @bctrlno =@pono
exec dropfile @t1
commit transaction
return
e_error:
rollback transaction
exec dropfile @t1
raiserror(@msg,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[spstporE]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE  PROCEDURE [dbo].[spstporE] @pono integer,@storeno int,@txndt datetime,@t1 varchar(50),@saletax  money,@txntyp int,
@glactno integer ,@sid  numeric,@invno varchar(50),@usernm varchar(50),@otherchgs money, @discount money,@stmode int, @bctrlno numeric output,
@cashtype int,@reissue char(1),@registered int = 0,@sellername varchar(50)='',@gstin varchar(15)=''
/* with encryption */
AS
declare @company int
declare @fy int
declare @sql varchar(5000)
declare @ctrlno int
declare @qty float
declare @price money
declare @rate money
--print 'periya'
declare @m_bofy datetime
declare @m_year int
declare @totalamt money
declare @iglactno int
declare @itemamt money
declare @supplier varchar(50)
declare @grno int
declare @stax money
declare @luperhu money
declare @fa_posting char(1)
declare @freeze datetime
declare @strate money
declare @drate money
declare @damt money
declare @sno numeric
declare @suppglactno numeric
declare @eofy datetime
declare @damttot money
declare @cgstrate money
declare @sgstrate money
declare @igstrate money
declare @cgstamt money
declare @sgstamt money
declare @igstamt money
declare @totcgst money
declare @totsgst money
declare @totigst money
declare @totcess money
declare @totexcise money
declare @tottcs money
declare @cgstacno numeric
declare @sgstacno numeric
declare @igstacno numeric
declare @cessacno numeric
declare @exciseacno numeric
declare @tcsacno numeric
declare @cessrate money
declare @cessamt money
declare @exciserate money
declare @exciseamt money
declare @tcsrate money
declare @tcsamt money

declare @totcgst1 money    -- ITC not eligible
declare @totsgst1 money
declare @totigst1 money
declare @cgstacno1 numeric
declare @sgstacno1 numeric
declare @igstacno1 numeric
declare @registeredsupplier char(1) 
declare @itceligible as char(1) 
declare @cashpurchase int 
declare @msg varchar(5000) 

set @msg = 'Error in purchase'
set @damttot = 0
--declare @bctrlno numeric
begin transaction
select @freeze=freeze, @company=company,@fa_posting=fa_posting,
@cgstacno1=cgstacno,@sgstacno1=sgstacno,@igstacno1=igstacno,@cessacno=cessacno
,@exciseacno=exciseacno,@tcsacno=tcsacno      
from store where storeno = @storeno

if datediff(d,@freeze,@txndt) <= 0 begin set @msg = 'Freezed Transaction...Not Allowed' goto e_error end
set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)


EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
if @@error<>0 goto e_error
----------


--insert into storepurchaseorder (storeno,txndt,usernm,seller,saletax,otherchgs,discount) 
--values (@storeno,@txndt,@usernm,@sid,@saletax,@otherchgs,@discount)
update storepurchaseorder 
set storeno = @storeno,
pono = @pono,
txndt = @txndt,
usernm = @usernm,
seller=@sid ,
saletax=@saletax,
otherchgs=@otherchgs,
discount=@discount where pono = @pono

select @bctrlno=ctrlno from storepurchaseorder where pono=@pono

set @totcgst = 0
set @totsgst = 0
set @totigst = 0
set @totcgst1 = 0
set @totsgst1 = 0
set @totigst1 = 0
set @totcess = 0
set @totexcise = 0
set @tottcs = 0
set @sql = 'declare tempcur1 cursor for select ctrlno,qty,price,saletax,strate,drate,damt,sno,cgstrate,cgstamt,sgstrate,sgstamt,igstrate,igstamt,cessrate,cessamt,exciserate,exciseamt,tcsrate,tcsamt from ' + @t1 + ' order by sno'
exec(@sql)
if @@error<>0 goto e_error
open tempcur1
fetch next from tempcur1 into @ctrlno,@qty,@price,@stax,@strate,@drate,@damt,@sno,
@cgstrate,@cgstamt,@sgstrate,@sgstamt,@igstrate,@igstamt,@cessrate,@cessamt
,@exciserate,@exciseamt,@tcsrate,@tcsamt

delete from storepohist where pono=@pono

while @@fetch_status=0
begin
   select @luperhu=luperhu,@itceligible=itceligible from storeitems where ctrlno=@ctrlno
   set @rate      = round(@price / (@qty/@luperhu),2)
   set @damttot   = @damttot + @damt

	if @registeredsupplier = 'Y'
		begin
		if @itceligible='Y'
			begin
				set @totcgst = @totcgst + isnull(@cgstamt ,0)
				set @totsgst = @totsgst + isnull(@sgstamt ,0)
				set @totigst = @totigst + isnull(@igstamt ,0)
			end
			else
			begin
				set @totcgst1 = @totcgst1 + isnull(@cgstamt ,0)
				set @totsgst1 = @totsgst1 + isnull(@sgstamt ,0)
				set @totigst1 = @totigst1 + isnull(@igstamt ,0)
			end
		end
		else
		begin
			set @totcgst1 = @totcgst1 + isnull(@cgstamt ,0)
			set @totsgst1 = @totsgst1 + isnull(@sgstamt ,0)
			set @totigst1 = @totigst1 + isnull(@igstamt ,0)
		end

   set @totcess   = @totcess + isnull(@cessamt ,0) 
   set @totexcise = @totexcise + isnull(@exciseamt,0)
   set @tottcs    = @tottcs + isnull(@tcsamt,0)

   insert into storepohist (bctrlno,ctrlno,qty,rate,pono,price,saletax,strate,drate,
   damt,txndt,cgstrate,cgstamt,sgstrate,sgstamt,igstrate,igstamt,cessrate,cessamt,exciserate,exciseamt,tcsrate,tcsamt,HSN) 
   values 
   (@bctrlno,@ctrlno,@qty,@rate,@pono,@price,isnull(@stax,0),isnull(@strate,0),@drate,@damt,@txndt,
	isnull(@cgstrate,0),isnull(@cgstamt,0),isnull(@sgstrate,0),isnull(@sgstamt,0),isnull(@igstrate,0),isnull(@igstamt,0),isnull(@cessrate,0),isnull(@cessamt,0)
	,isnull(@exciserate,0) ,isnull(@exciseamt,0) ,isnull(@tcsrate,0),isnull(@tcsamt,0) ,'  '
   )
if @@error<>0 goto e_error

fetch next from tempcur1 into @ctrlno,@qty,@price,@stax,@strate,@drate,@damt,@sno,
@cgstrate,@cgstamt,@sgstrate,@sgstamt,@igstrate,@igstamt,@cessrate,@cessamt
,@exciserate,@exciseamt,@tcsrate,@tcsamt
end
close tempcur1
deallocate tempcur1
set @bctrlno =@pono
exec dropfile @t1
commit transaction
return
e_error:
rollback transaction
exec dropfile @t1
raiserror(@msg,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[spstpur]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spstpur    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spstpur] @storeno int,@txndt datetime,@t1 varchar(50),@saletax  money,@txntyp int,
@glactno integer ,@sid  numeric,@invno varchar(50),@usernm varchar(50),@otherchgs money, @discount money,@stmode int, @bctrlno numeric output,
@cashtype int,@reissue char(1),@registered int = 0,@sellername varchar(50)='',@gstin varchar(15)=''
/* with encryption */
AS
declare @company int
declare @fy int
declare @sql varchar(5000)
declare @ctrlno int
declare @qty float
declare @price money
declare @chlno int
declare @rate money
--print 'periya'
declare @m_bofy datetime
declare @m_year int
declare @totalamt money
declare @iglactno int
declare @itemamt money
declare @supplier varchar(50)
declare @grno int
declare @stax money
declare @luperhu money
declare @fa_posting char(1)
declare @freeze datetime
declare @strate money
declare @drate money
declare @damt money
declare @sno numeric
declare @suppglactno numeric
declare @eofy datetime
declare @damttot money
declare @cgstrate money
declare @sgstrate money
declare @igstrate money
declare @cgstamt money
declare @sgstamt money
declare @igstamt money
declare @totcgst money
declare @totsgst money
declare @totigst money
declare @totcess money
declare @totexcise money
declare @tottcs money
declare @cgstacno numeric
declare @sgstacno numeric
declare @igstacno numeric
declare @cessacno numeric
declare @exciseacno numeric
declare @tcsacno numeric
declare @cessrate money
declare @cessamt money
declare @exciserate money
declare @exciseamt money
declare @tcsrate money
declare @tcsamt money

declare @totcgst1 money    -- ITC not eligible
declare @totsgst1 money
declare @totigst1 money
declare @cgstacno1 numeric
declare @sgstacno1 numeric
declare @igstacno1 numeric
declare @registeredsupplier char(1) 
declare @itceligible as char(1) 
declare @cashpurchase int 
declare @msg varchar(5000) 

set @msg = 'Error in purchase'
set @damttot = 0
--declare @bctrlno numeric
begin transaction
select @freeze=freeze, @company=company,@fa_posting=fa_posting,
@cgstacno1=cgstacno,@sgstacno1=sgstacno,@igstacno1=igstacno,@cessacno=cessacno
,@exciseacno=exciseacno,@tcsacno=tcsacno      
from store where storeno = @storeno
-- the following is incrporated on 7 April 2020  as per the program for  GSTR2
set @cgstacno = 105
set @sgstacno = 106
set @igstacno = 107
set @cessacno = 108
set @exciseacno = 109 
set @tcsacno = 110

if datediff(d,@freeze,@txndt) <= 0 begin set @msg = 'Freezed Transaction...Not Allowed' goto e_error end
set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)


EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
if @@error<>0 goto e_error
----------


if @fa_posting='Y' 
	begin
	EXEC  GETGRP @TXNDT,@COMPANY,@GRNO OUT
	IF @GRNO=0 GOTO E_ERROR
	end
else
	set @grno=0
-----------

if not exists(select * from stchlno where storeno=@storeno and chlndt =@m_bofy)
	begin
	insert into stchlno (storeno,chlndt,pchlno,dchlno,adjdr,adjcr) values (@storeno,@m_bofy,0,0,0,0)
	if @@error<>0 goto e_error
	end

update stchlno  set pchlno= pchlno+1  WHERE storeno= @storeno and chlndt=@m_bofy
if @@error<>0 goto e_error
select @chlno=pchlno from stchlno  WHERE storeno= @storeno and chlndt=@m_bofy
set @glactno = case  when @txntyp=0 then 0 else @glactno end


if @fa_posting='Y'
if @txntyp=1 
begin
	declare @sfy numeric
	declare @found int
	set @found=0
	select @sfy=fy,@glactno=glactno from suppglac where supplier=@sid and fy=(select min(fy) from suppglac where supplier=@sid and fy in (select fy from finyear where company=@company) )
	while @sfy<>@fy begin
	   set @found=1
	   if exists(select  *  from glprimld where fy=@sfy and cgp_actno=@glactno)
			begin
				select  @sfy=nextyearfy,@glactno=nextyearac from glprimld where fy=@sfy and cgp_actno=@glactno
			end
				else
			break
	   if @sfy=@fy set @found=0
	end


	if @found<>0
	begin
	   set @msg = 'There is problem is finding the suppliers account for the selected financial year-S' + str(@sfy) + ' G'+ Str(@fy)
	  goto e_error
	end
end   -- type=1

--set @msg='Periya' goto e_error

set @suppglactno=@glactno
if @txntyp=0 
begin 
if @registered = 1 begin set @registeredsupplier = 'Y' set @cashpurchase=1 end else  begin set @registeredsupplier='N' set @cashpurchase=0 end
end
else
begin
select @supplier=name,@registeredsupplier=registered,@sellername=name from suppliers where sid=@sid
select @gstin=gstin from glprimld where cgp_actno=@suppglactno and fy=@fy 
set @cashpurchase=0
end

insert into storebills (storeno,amttyp,chlno,txndt,usernm,invoiceno,supplier,grno,saletax,stmode,discount,cashtype,txntyp,otherchgs,reissue,supplier_actno,registered,sellername,gstin) 
values (@storeno,'C',@chlno,@txndt,@usernm,@invno,@sid,@grno,@saletax,@stmode,@discount,@cashtype, @txntyp,@otherchgs,@reissue,@glactno,@registered ,@sellername ,@gstin)
if @@error<>0 goto e_error
select @bctrlno=@@identity

set @totcgst = 0
set @totsgst = 0
set @totigst = 0
set @totcgst1 = 0
set @totsgst1 = 0
set @totigst1 = 0
set @totcess = 0
set @totexcise = 0
set @tottcs = 0
set @sql = 'declare tempcur1 cursor for select ctrlno,qty,price,saletax,strate,drate,damt,sno,cgstrate,cgstamt,sgstrate,sgstamt,igstrate,igstamt,cessrate,cessamt,exciserate,exciseamt,tcsrate,tcsamt from ' + @t1 + ' order by sno'
exec(@sql)
if @@error<>0 goto e_error
open tempcur1
fetch next from tempcur1 into @ctrlno,@qty,@price,@stax,@strate,@drate,@damt,@sno,
@cgstrate,@cgstamt,@sgstrate,@sgstamt,@igstrate,@igstamt,@cessrate,@cessamt
,@exciserate,@exciseamt,@tcsrate,@tcsamt

while @@fetch_status=0
begin
   select @luperhu=luperhu,@itceligible=itceligible from storeitems where ctrlno=@ctrlno
   set @rate      = round(@price / (@qty/@luperhu),2)
   set @damttot   = @damttot + @damt

	if @registeredsupplier = 'Y'
		begin
		if @itceligible='Y'
			begin
				set @totcgst = @totcgst + isnull(@cgstamt ,0)
				set @totsgst = @totsgst + isnull(@sgstamt ,0)
				set @totigst = @totigst + isnull(@igstamt ,0)
			end
			else
			begin
				set @totcgst1 = @totcgst1 + isnull(@cgstamt ,0)
				set @totsgst1 = @totsgst1 + isnull(@sgstamt ,0)
				set @totigst1 = @totigst1 + isnull(@igstamt ,0)
			end
		end
		else
		begin
			set @totcgst1 = @totcgst1 + isnull(@cgstamt ,0)
			set @totsgst1 = @totsgst1 + isnull(@sgstamt ,0)
			set @totigst1 = @totigst1 + isnull(@igstamt ,0)
		end

   set @totcess   = @totcess + isnull(@cessamt ,0) 
   set @totexcise = @totexcise + isnull(@exciseamt,0)
   set @tottcs    = @tottcs + isnull(@tcsamt,0)

   insert into storehist (bctrlno,ctrlno,amttyp,qty,rate,chlno,price,saletax,strate,drate,
   damt,txndt,cgstrate,cgstamt,sgstrate,sgstamt,igstrate,igstamt,cessrate,cessamt,exciserate,exciseamt,tcsrate,tcsamt) 
   values 
   (@bctrlno,@ctrlno,'C',@qty,@rate,@chlno,@price,isnull(@stax,0),isnull(@strate,0),@drate,@damt,@txndt,
	isnull(@cgstrate,0),isnull(@cgstamt,0),isnull(@sgstrate,0),isnull(@sgstamt,0),isnull(@igstrate,0),isnull(@igstamt,0),isnull(@cessrate,0),isnull(@cessamt,0)
	,isnull(@exciserate,0) ,isnull(@exciseamt,0) ,isnull(@tcsrate,0),isnull(@tcsamt,0) 
   )
if @@error<>0 goto e_error
--update storeitems set stock=stock+@qty where ctrlno=@ctrlno

		set @eofy = '31 mar ' + ltrim(str(year(@m_bofy)+1))
		--exec spsthrewrite @ctrlno,@fy,@txndt,@eofy,0
		--exec fillstock1 @ctrlno,@txndt

   select @iglactno=glactno  from storeitems where ctrlno=@ctrlno
   set @sql = 'update  ' + @t1 + ' set glactno = ' + str(@iglactno) + ' where sno=' + str(@sno)
  exec( @sql)
if @@error<>0 goto e_error
fetch next from tempcur1 into @ctrlno,@qty,@price,@stax,@strate,@drate,@damt,@sno,
@cgstrate,@cgstamt,@sgstrate,@sgstamt,@igstrate,@igstamt,@cessrate,@cessamt
,@exciserate,@exciseamt,@tcsrate,@tcsamt

end
close tempcur1
deallocate tempcur1
----------- posting begins
if exists(select * from setting where g_finaccount=0) 
begin 
	exec dropfile @t1
	commit transaction 
	return 
end  -- Stops for PowerHms-Lite

if @fa_posting='Y' begin
select @supplier=name from suppliers where sid=@sid
if @txntyp=0 set @supplier='Cash Purchase'
set @sql = 'declare tempcur2 cursor for  select [totalamt]=sum(price),glactno from ' + @t1 + ' group by glactno'
exec( @sql)
open tempcur2
fetch next from tempcur2 into @itemamt,@iglactno
set @totalamt=0
while @@fetch_status=0
begin
--print 'solkkettan'
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
values (@company,@fy,@iglactno,@itemamt,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y')
if @@error<>0 goto e_error
set @totalamt=@totalamt+@itemamt
fetch next from tempcur2 into @itemamt,@iglactno
end
close tempcur2
deallocate tempcur2
if @txntyp=0
   begin
		set @iglactno=case when @cashtype=0 then 1 else 51 end
		set @suppglactno = @iglactno	   
   end
else
    begin
    declare @already numeric
    --exec pbsgetsac @sid,@fy,0,@iglactno out,@already out
     set @iglactno=@suppglactno
    end
if  @saletax<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
values (@company,@fy,54,@saletax,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y')
if @@error<>0 goto e_error
end

if  @totcgst<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,sellername,gstin,cashpurchase)
values (@company,@fy,@cgstacno,@totcgst,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@sellername,@gstin,@cashpurchase)
if @@error<>0 goto e_error
end
if  @totsgst<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,sellername,gstin,cashpurchase)
values (@company,@fy,@sgstacno,@totsgst,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@sellername,@gstin,@cashpurchase)
if @@error<>0 goto e_error
end

if  @totigst<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,sellername,gstin,cashpurchase)
values (@company,@fy,@igstacno,@totigst,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@sellername,@gstin,@cashpurchase)
if @@error<>0 goto e_error
end

		if  @totcgst1<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,sellername,gstin,cashpurchase)
			values (@company,@fy,@cgstacno1,@totcgst1,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@sellername,@gstin,@cashpurchase)
			if @@error<>0 goto e_error
			end
		if  @totsgst1<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,sellername,gstin,cashpurchase)
			values (@company,@fy,@sgstacno1,@totsgst1,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@sellername,@gstin,@cashpurchase)
			if @@error<>0 goto e_error
			end

		if  @totigst1<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,sellername,gstin,cashpurchase)
			values (@company,@fy,@igstacno1,@totigst1,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@sellername,@gstin,@cashpurchase)
			if @@error<>0 goto e_error
			end



if  @totcess<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno)
values (@company,@fy,@cessacno,@totcess,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@chlno,@txndt,1,@bctrlno)
if @@error<>0 goto e_error
end

if  @totexcise<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno)
values (@company,@fy,@exciseacno,@totexcise ,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@chlno,@txndt,1,@bctrlno)
if @@error<>0 goto e_error
end

if  @tottcs<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno)
values (@company,@fy,@tcsacno,@tottcs,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@chlno,@txndt,1,@bctrlno)
if @@error<>0 goto e_error
end

set @totalamt=@totalamt
if @otherchgs<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno)
values (@company,@fy,60,@otherchgs,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@chlno,@txndt,1,@bctrlno)
if @@error<>0 goto e_error
end

if @damttot<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
values (@company,@fy,59,@damttot,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' % Discount - ' + @supplier,'C',@txndt,@grno,'Y')
if @@error<>0 goto e_error
end


if @discount<>0
begin
insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
values (@company,@fy,59,@discount,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'C',@txndt,@grno,'Y')
if @@error<>0 goto e_error
end

insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
values (@company,@fy,@iglactno,@totalamt+@otherchgs-@discount-@damttot+@SALETAX+@totcgst+@totsgst+@totigst+@totcgst1+@totsgst1+@totigst1+@totcess+@totexcise+@tottcs,'By Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'C',@txndt,@grno,'Y')
if @@error<>0 goto e_error
end
------------------------------- posting ends
exec dropfile @t1
commit transaction
return
e_error:
rollback transaction
exec dropfile @t1
raiserror(@msg,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[spstpuradj]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spstpur    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spstpuradj] @storeno int,@txndt datetime,@t1 varchar(50),@usernm varchar(50), @bctrlno numeric output
/* with encryption */
AS
declare @company int
declare @fy int
declare @sql varchar(5000)
declare @ctrlno int
declare @qty float
declare @price money
declare @chlno int
declare @rate money
declare @m_bofy datetime
declare @m_year int
declare @totalamt money
declare @iglactno int
declare @itemamt money
declare @supplier varchar(50)
declare @grno int
declare @stax money
declare @luperhu money
declare @freeze datetime
declare @storename varchar(100)
begin transaction
select @storename=storename, @company=company,@freeze=freeze from store where storeno = @storeno
if datediff(d,@freeze,@txndt) <= 0 begin print 'Freezed Transaction...Not Allowed' goto e_error end
--IF DATEPART(mm,@TXNDT) >=  1 AND DATEPART(mm,@TXNDT) <= 3
--     SET @M_YEAR = DATEPART(yy,@TXNDT)-1
--ELSE
--     SET @M_YEAR = DATEPART(yy,@TXNDT)
--SELECT @M_BOFY = '1 APR ' + STR(@M_YEAR)

set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)


EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
if @@error<>0 goto e_error

if not exists(select * from stchlno where storeno=@storeno and chlndt =@m_bofy)
  begin
   insert into stchlno (storeno,chlndt,pchlno,dchlno,adjcr,adjdr) values (@storeno,@m_bofy,0,0,0,0)
   if @@error<>0 goto e_error
   end
/*
if @adjust=0 
*/
begin
update stchlno  set adjcr= adjcr+1  WHERE storeno= @storeno and chlndt=@m_bofy
if @@error<>0 goto e_error
select @chlno=adjcr from stchlno  WHERE storeno= @storeno and chlndt=@m_bofy
end
declare @amttyp char
declare @stock float
declare @qty1 float
insert into storebills (storeno,amttyp,chlno,txndt,usernm,adjust,invoiceno,supplier,grno,saletax,stmode,discount,cashtype,txntyp,otherchgs)
values
(@storeno,'A',@chlno,@txndt,@usernm,'Y',1,0,0,0,0,0,0,2,0)
if @@error<>0 goto e_error
select @bctrlno=@@identity
set @sql = 'declare tempcur1 cursor for select ctrlno,qty,price,saletax from ' + @t1 + ' order by sno'
exec(@sql)
if @@error<>0 goto e_error
open tempcur1
fetch next from tempcur1 into @ctrlno,@qty,@price,@stax
while @@fetch_status=0
begin
   select @luperhu=luperhu,@rate=rate from storeitems where ctrlno=@ctrlno
   select @stock=balance from storeprimary where @fy=fy and ictrlno=@ctrlno
--   update storeprimary set balance=balance - @qty where @fy=fy and ictrlno=@ctrlno
--   update storeitems set stock=@qty where ctrlno=@ctrlno (becuase trigger will update the stock properly)
if @stock < 0 
  set @amttyp='C'
else
  set @amttyp=case when @stock > @qty then 'D' else 'C' end
--print str(@fy) + '  '  + str(@ctrlno) + '  ' + convert(varchar(20),@stock) + '   '  + convert(varchar(20),@qty)  goto e_error

  if @stock<>@qty 
  begin
    --if @stock < 0    set @qty1=@qty+ abs(@stock)
    set @qty1=abs(@stock-@qty)
    set @price=(@qty1/@luperhu)*@rate

    insert into storehist (bctrlno,ctrlno,amttyp,qty,rate,chlno,price,saletax,txndt,adjust) values (@bctrlno,@ctrlno,@amttyp,@qty1,@rate,@chlno,@price,@stax,@txndt,'Y')
    if @@error<>0 goto e_error
    --exec fillstock1 @ctrlno,@txndt
  end
   fetch next from tempcur1 into @ctrlno,@qty,@price,@stax
end
close tempcur1
deallocate tempcur1
insert into acclog (narration,txntype) values ('Store : ' + @storename   + '-Adjustment Chalan : ' + ltrim(str(@chlno)) +  ' Posted',154)
commit transaction
set @sql = 'drop table '+ @t1
exec( @sql)
return
e_error:
rollback transaction
set @sql = 'drop table '+ @t1
exec @sql
raiserror('error in purchase',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spstpuradjnew]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spstpur    Script Date: 11/03/2010 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spstpuradjnew] @storeno int,@txndt datetime,@t1 varchar(50),@usernm varchar(50), @bctrlno numeric output
/* with encryption */
AS
declare @company int
declare @fy int
declare @sql varchar(5000)
declare @ctrlno int
declare @qty money
declare @price money
declare @chlno int
declare @rate money
declare @m_bofy datetime
declare @m_year int
declare @totalamt money
declare @iglactno int
declare @itemamt money
declare @supplier varchar(50)
declare @grno int
declare @stax money
declare @luperhu money
declare @freeze datetime
declare @storename varchar(100)
declare @fy1 numeric
declare @bofy datetime
declare @eofy datetime
declare @err varchar(600)
set @err = 'Error'
begin transaction
select @storename=storename, @company=company,@freeze=freeze from store where storeno = @storeno
if datediff(d,@freeze,@txndt) <= 0 begin print 'freezed transaction...not allowed' goto e_error end
--if datepart(mm,@txndt) >=  1 and datepart(mm,@txndt) <= 3
--     set @m_year = datepart(yy,@txndt)-1
--else
--     set @m_year = datepart(yy,@txndt)
--select @m_bofy = '1 apr ' + str(@m_year)

set @bofy = dbo.getbofy(@txndt)
set @eofy = dbo.geteofy(@txndt) 
set @m_year = datepart(yy,@bofy)
set @m_bofy = @bofy
exec @fy= getfy  @txndt,@company,@fy out
if @@error<>0 goto e_error

if not exists(select * from stchlno where storeno=@storeno and chlndt =@bofy)
	begin
		insert into stchlno (storeno,chlndt,pchlno,dchlno,adjcr,adjdr) values (@storeno,@bofy,0,0,0,0)
		if @@error<>0 goto e_error
	end

update stchlno  set adjcr= adjcr+1  where storeno= @storeno and chlndt=@m_bofy
if @@error<>0 goto e_error

select @chlno=adjcr from stchlno  where storeno= @storeno and chlndt=@m_bofy
declare @amttyp char
declare @stock float
declare @qty1 float
insert into storebills (storeno,amttyp,chlno,txndt,usernm,adjust,invoiceno,supplier,grno,saletax,stmode,discount,cashtype,txntyp,otherchgs)
values
(@storeno,'A',@chlno,@txndt,@usernm,'Y',1,0,0,0,0,0,0,2,0)
if @@error<>0 goto e_error
select @bctrlno=@@identity
set @sql = 'declare tempcur1 cursor for select ctrlno,qty,price,saletax from ' + @t1 + ' order by sno'
exec(@sql)
if @@error<>0 goto e_error
open tempcur1
fetch next from tempcur1 into @ctrlno,@qty,@price,@stax
while @@fetch_status=0
begin
	if @qty < 0   set @amttyp = 'D'  else set @amttyp='C'
	set @qty = abs(@qty)

	select @luperhu=luperhu,@rate=rate from storeitems where ctrlno=@ctrlno
	set @qty = @qty * @luperhu
	set @price=(@qty/@luperhu)*@rate
	insert into storehist (bctrlno,ctrlno,amttyp,qty,rate,chlno,price,saletax,txndt,adjust) values (@bctrlno,@ctrlno,@amttyp,@qty,@rate,@chlno,@price,@stax,@txndt,'Y')
	if @@error<>0 goto e_error
	declare cfy cursor for select (date),fy from finyear where company=@company and fy >= @fy order by fy
	open cfy
	fetch next from cfy into @bofy,@fy1
	while @@fetch_status=0
	begin
		set @eofy = '31 mar ' + ltrim(str(year(@bofy)+1))
		--exec spsthrewrite @ctrlno,@fy1,@bofy,@eofy,1,1
		fetch next from cfy into @bofy,@fy1
	end
	close cfy
	deallocate cfy
	fetch next from tempcur1 into @ctrlno,@qty,@price,@stax
end
close tempcur1
deallocate tempcur1
insert into acclog (narration,txntype) values ('Store : ' + @storename   + '-Adjustment Chalan : ' + ltrim(str(@chlno)) +  ' Posted',154)
commit transaction
set @sql = 'drop table '+ @t1
exec( @sql)
return
e_error:
rollback transaction
--set @sql = 'drop table '+ @t1
--exec @sql
set @err = 'Error in Adjustment Posting-'  + @err 
raiserror(@err,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spstpure]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spstpur    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spstpure] @storeno int,@txndt datetime,@t1 varchar(50),@saletax  money,@txntyp int,
@glactno integer ,@sid  integer,@invno varchar(50),@usernm varchar(50),@otherchgs money, @discount money,@stmode int,
@cashtype int,@bctrlno numeric,@registered int = 0,@sellername varchar(50)='',@gstin varchar(15)=''
/* with encryption */
AS
declare @company int
declare @fy int
declare @sql varchar(5000)
declare @ctrlno int
declare @qty float
declare @price money
declare @chlno int
declare @rate money
--print 'periya'
declare @m_bofy datetime
declare @m_year int
declare @totalamt money
declare @iglactno int
declare @itemamt money
declare @supplier varchar(50)
declare @grno int
declare @stax money
declare @luperhu money
declare @otxndt datetime
declare @ostoreno int
declare @ocompany int
declare @ofy int
declare @rv int
declare @hctrlno numeric
declare @fa_posting char(1)
declare @freeze datetime
declare @stk float
declare @strate money
declare @drate money
declare @damt money
declare @sno numeric
declare @suppglactno numeric
declare @hctrlno2 numeric
declare @damttot money
declare @cgstrate money
declare @sgstrate money
declare @igstrate money
declare @cgstamt money
declare @sgstamt money
declare @igstamt money
declare @totcgst money
declare @totsgst money
declare @totigst money
declare @totCESS money
declare @totexcise money
declare @tottcs money

declare @cgstacno numeric
declare @sgstacno numeric
declare @igstacno numeric

declare @cessacno numeric
declare @exciseacno numeric
declare @tcsacno numeric

declare @cessrate money
declare @cessamt money
declare @exciserate money
declare @exciseamt money
declare @tcsrate money
declare @tcsamt money

declare @msg varchar(5000) 
declare @oqty float
declare @octrlno numeric
declare @orate money

declare @chkamt money

declare @totcgst1 money    -- ITC not eligible
declare @totsgst1 money
declare @totigst1 money
declare @cgstacno1 numeric
declare @sgstacno1 numeric
declare @igstacno1 numeric
declare @registeredsupplier char(1) 
declare @itceligible as char(1) 
declare @cashpurchase int 

set @msg = 'Error in purchase'

set @damttot =0 

begin transaction

if not exists (select * from storebills where amttyp= 'C'  and adjust='N' and bctrlno=@bctrlno) begin print 'This is not a purchase chalan' goto e_error end


--select @freeze=freeze,@company=company,@fa_posting=fa_posting from store where storeno = @storeno
select @freeze=freeze, @company=company,@fa_posting=fa_posting,
@cgstacno1=cgstacno,@sgstacno1=sgstacno,@igstacno1=igstacno,@cessacno=cessacno      
,@exciseacno=exciseacno,@tcsacno=tcsacno      
from store where storeno = @storeno

set @cgstacno = 105
set @sgstacno = 106
set @igstacno = 107
set @cessacno = 108
set @exciseacno = 109 
set @tcsacno = 110

if datediff(d,@freeze,@txndt) <= 0 begin print 'Freezed Transaction...Not Allowed' goto e_error end
set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)


EXEC @FY= GETFY  @TXNDT,@COMPANY,@FY OUT
if @@error<>0 goto e_error
------------------
select @grno=grno,@otxndt=txndt,@ostoreno=storeno,@chlno=chlno from storebills where bctrlno=@bctrlno
select @ocompany=company,@fa_posting=fa_posting from store where storeno = @ostoreno
exec @ofy=getfy @otxndt,@ocompany,@ofy out
if @@error<>0 goto e_error

if @fa_posting='Y' and @grno<> 0 exec delgrno @ocompany,@ofy,@grno,@otxndt,@rv out

if @fa_posting='Y'
if @txntyp=1 begin
declare @sfy numeric
declare @found int
set @found=0
select @sfy=fy,@glactno=glactno from suppglac where supplier=@sid and fy=(select min(fy) from suppglac where supplier=@sid and fy in (select fy from finyear where company=@company) )
while @sfy<>@fy begin
           set @found=1
           if exists(select  *  from glprimld where fy=@sfy and cgp_actno=@glactno)
              select  @sfy=nextyearfy,@glactno=nextyearac from glprimld where fy=@sfy and cgp_actno=@glactno
           else
	break
           if @sfy=@fy set @found=0
end
if @found<>0
begin
   print 'There is problem is finding the suppliers account for the selected financial year'
  goto e_error
end
end
set @suppglactno=@glactno

---------- for new store
select @company=company,@fa_posting=fa_posting from store where storeno=@storeno
exec @fy=getfy @txndt,@company,@fy out
declare @bofy datetime
declare @eofy datetime

select @bofy = dbo.getbofy(@txndt)
select @eofy = dbo.geteofy(@txndt)
--
--if @company<>@ocompany 
--if @fa_posting='Y' begin
---end
--else
--set @grno=0
--


--select ctrlno,qty1=qty,newqty=0 into #dumstore from storehist where bctrlno=@bctrlno

--declare tc cursor for select hctrlno,ctrlno from storehist where bctrlno=@bctrlno
--open tc
--fetch next from tc into @hctrlno,@ctrlno
--while @@fetch_status=0 begin
--	select @stk=stock from storeitems where ctrlno=@ctrlno
--	update storehist set forupdt='N' where current of tc
--	delete from storehist where hctrlno=@hctrlno
--	--exec spsthrewrite @ctrlno,@fy, @bofy,@eofy 
--	fetch next from tc into @hctrlno,@ctrlno
--end
--close tc
--deallocate tc

select @supplier=name,@registeredsupplier=registered from suppliers where sid=@sid

set @registeredsupplier = isnull(@registeredsupplier,'N')

if @txntyp=0 
begin 
if @registered = 1 begin set @registeredsupplier = 'Y' set @cashpurchase=1 end else  begin set @registeredsupplier='N' set @cashpurchase=0 end
--set @msg =  @registeredsupplier  + ' ' + @sellername + ' ' + @gstin  goto e_error
end
else
begin
select @supplier=name,@registeredsupplier=registered,@sellername=name from suppliers where sid=@sid
select @gstin=gstin from glprimld where cgp_actno=@suppglactno and fy=@fy 
set @cashpurchase=0
end



exec getgrp @txndt,@company,@grno out
IF @grno=0 goto e_error

set @grno=@grno 
update storebills set 
grno=@grno,
storeno=@storeno,
txndt=@txndt,
invoiceno=@invno,
supplier=@sid,
saletax=@saletax,
stmode=@stmode,
discount=@discount,
cashtype=@cashtype,
txntyp=@txntyp,
otherchgs=@otherchgs,
supplier_actno=@glactno,
registered=@registered,
sellername=@sellername ,
gstin=@gstin 
where bctrlno=@bctrlno

set @sql = 'IF EXISTS (SELECT * from storehist where bctrlno=' + STR(@bctrlno) + ' and hctrlno2 not in (select  hctrlno2 from ' + @t1 + ')) delete from storehist where bctrlno=' + STR(@bctrlno) + ' and hctrlno2 not in (select  hctrlno2 from ' + @t1 + ')'
exec(@sql)
set @sql = 'declare tempcur1 cursor for select ctrlno,qty,price,saletax,strate,drate,damt,sno,hctrlno2,cgstrate,cgstamt,sgstrate,sgstamt,igstrate,igstamt,cessrate,cessamt,oldqty,oldctrlno,exciserate,exciseamt,tcsrate,tcsamt from ' + @t1 + ' order by sno'
exec(@sql)
if @@error<>0 goto e_error


set @totcgst = 0
set @totsgst = 0
set @totigst = 0
set @totcgst1 = 0
set @totsgst1 = 0
set @totigst1 = 0

set @totcess = 0
set @totexcise = 0
set @tottcs = 0
open tempcur1
fetch next from tempcur1 into @ctrlno,@qty,@price,@stax,@strate,@drate,@damt,@sno,@hctrlno2,
@cgstrate,@cgstamt,@sgstrate,@sgstamt,@igstrate,@igstamt,@cessrate,@cessamt,@oqty,@octrlno
,@exciserate,@exciseamt,@tcsrate,@tcsamt

while @@fetch_status=0
begin
	select @luperhu=luperhu,@itceligible=itceligible from storeitems where ctrlno=@ctrlno
	set @rate= round(@price / (@qty/@luperhu),2)
	set @damttot= @damttot + @damt
	if @registeredsupplier = 'Y'
		begin
		if @itceligible='Y'
			begin
				set @totcgst = @totcgst + isnull(@cgstamt ,0)
				set @totsgst = @totsgst + isnull(@sgstamt ,0)
				set @totigst = @totigst + isnull(@igstamt ,0)
			end
			else
			begin
				set @totcgst1 = @totcgst1 + isnull(@cgstamt ,0)
				set @totsgst1 = @totsgst1 + isnull(@sgstamt ,0)
				set @totigst1 = @totigst1 + isnull(@igstamt ,0)
			end
		end
		else
		begin
			set @totcgst1 = @totcgst1 + isnull(@cgstamt ,0)
			set @totsgst1 = @totsgst1 + isnull(@sgstamt ,0)
			set @totigst1 = @totigst1 + isnull(@igstamt ,0)
		end


	set @totcess = @totcess + isnull(@cessamt ,0) 
	set @totexcise = @totexcise + isnull(@exciseamt,0)
	set @tottcs    = @tottcs + isnull(@tcsamt,0)


	if @hctrlno2 <> 0 
	    begin

		select @orate=rate,@chkamt=damt+cgstamt+sgstamt+igstamt+cessamt+exciseamt+tcsamt from storehist where hctrlno2=@hctrlno2
 			IF ((@ctrlno=@octrlno and @qty <> @oqty) or (@ctrlno<>@octrlno)  or (@rate<>@orate) or (@chkamt<>@damt+@cgstamt+@sgstamt+@igstamt+@cessamt+@exciseamt+@tcsamt)) 
				BEGIN
				update storehist set bctrlno=@bctrlno,ctrlno=@ctrlno,qty=@qty,rate=@rate,price=@price,txndt=@txndt, 
				drate=@drate,damt=@damt,cgstrate=@cgstrate,cgstamt=@cgstamt,sgstrate=@sgstrate,sgstamt=@sgstamt,igstrate=@igstrate,igstamt=@igstamt,cessrate=@cessrate,cessamt=@cessamt,
				exciserate=@exciserate ,exciseamt=@exciseamt ,tcsrate=@tcsrate,tcsamt=@tcsamt
				where hctrlno2=@hctrlno2
				if @@error<>0 rollback transaction
				END
		end
    IF @HCTRLNO2=0
		begin
		insert into storehist (bctrlno,ctrlno,amttyp,qty,rate,chlno,price,saletax,strate,
		forupdt,drate,damt,txndt,cgstrate,cgstamt,sgstrate,sgstamt,igstrate,igstamt,cessrate,cessamt,exciserate,exciseamt ,tcsrate,tcsamt) 
		values 
		(@bctrlno,@ctrlno,'C',@qty,@rate,@chlno,@price,isnull(@stax,0),isnull(@strate,0),
		'Y',@drate,@damt,@txndt,
		isnull(@cgstrate,0),isnull(@cgstamt,0),isnull(@sgstrate,0),isnull(@sgstamt,0),isnull(@igstrate,0),isnull(@igstamt,0),isnull(@cessrate,0),isnull(@cessamt,0),
		isnull(@exciserate,0),isnull(@exciseamt,0),isnull(@tcsrate,0),isnull(@tcsamt,0)
		)
		end
	if @@error<>0 goto e_error
	update storeprimary set balance=dbo.GetStockFast(@ctrlno,getdate(),@fy)  where ictrlno=@ctrlno and fy=@fy   



	--if not exists(select * from #dumstore where ctrlno=@ctrlno)
	--insert into #dumstore (ctrlno,qty1,newqty) values (@ctrlno,0,0)
	--if @@error<>0 goto e_error
	--update #dumstore set newqty=@qty where ctrlno=@ctrlno
	select @iglactno=glactno  from storeitems where ctrlno=@ctrlno
	set @sql = 'update  ' + @t1 + ' set glactno = ' + str(@iglactno) + ' where sno=' + str(@sno)
	exec( @sql)
	if @@error<>0 goto e_error
	fetch next from tempcur1 into @ctrlno,@qty,@price,@stax,@strate,@drate,@damt,@sno,@hctrlno2,
	@cgstrate,@cgstamt,@sgstrate,@sgstamt,@igstrate,@igstamt,@cessrate,@cessamt,@oqty,@octrlno
	,@exciserate,@exciseamt,@tcsrate,@tcsamt
end
close tempcur1
deallocate tempcur1


--------------------Checkin of Minus Balance Starts
--select * from #dumstore
--declare @qty1 numeric(18,4)
--declare @newqty numeric(18,4) 
--declare tempstore1 cursor for select ctrlno,qty1,newqty from #dumstore
--open tempstore1
--fetch next from tempstore1 into @ctrlno,@qty1,@newqty
--while @@fetch_status=0
--begin
--    if @newqty > @qty1
--	begin
--	--exec spsthrewrite @ctrlno,@fy, @bofy,@eofy 
--	if @@error<>0 goto e_error
--	end
--	fetch next from tempstore1 into @ctrlno,@qty1,@newqty
--end
--close tempstore1
--deallocate tempstore1
-------------------- posting starts
if @fa_posting='Y' 
	begin
		select @supplier=name,@registeredsupplier=registered from suppliers where sid=@sid
		if @txntyp=0 set @supplier='Cash Purchase'
		set @sql = 'declare tempcur2 cursor for  select [totalamt]=sum(price),glactno from ' + @t1 + ' group by glactno'
		exec( @sql)
		open tempcur2
		fetch next from tempcur2 into @itemamt,@iglactno
		set @totalamt=0
		while @@fetch_status=0
			begin
			--print 'solkkettan'
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
			values (@company,@fy,@iglactno,@itemamt,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y')
			if @@error<>0 goto e_error
			set @totalamt=@totalamt+@itemamt
			fetch next from tempcur2 into @itemamt,@iglactno
			end
		close tempcur2
		deallocate tempcur2


		if @txntyp=0
			begin
				set @iglactno=case when @cashtype=0 then 1 else 51 end
				set @suppglactno = @iglactno	   
			end
		else
		   begin
			   declare @already numeric
			   --exec pbsgetsac @sid,@fy,0,@iglactno out,@already out
			   set @iglactno=@suppglactno
		   end


		if @saletax<>0 
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
			values (@company,@fy,54,@saletax,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y')
			if @@error<>0 goto e_error
			end

		if  @totcgst<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,gstin,sellername,cashpurchase)
			values (@company,@fy,@cgstacno,@totcgst,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@gstin,@sellername,@cashpurchase)
			if @@error<>0 goto e_error
			end
		if  @totsgst<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,gstin,sellername,cashpurchase)
			values (@company,@fy,@sgstacno,@totsgst,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@gstin,@sellername,@cashpurchase)
			if @@error<>0 goto e_error
			end

		if  @totigst<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,gstin,sellername,cashpurchase)
			values (@company,@fy,@igstacno,@totigst,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@gstin,@sellername,@cashpurchase)
			if @@error<>0 goto e_error
			end
		   			--print convert(varchar(20),@totcgst) + ' '  + convert(varchar(20),@totcgst1) + ' ' + str(@cgstacno1) goto e_error

		if  @totcgst1<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,gstin,sellername,cashpurchase)
			values (@company,@fy,@cgstacno1,@totcgst1,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@gstin,@sellername,@cashpurchase)
			if @@error<>0 goto e_error
			end
		if  @totsgst1<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,gstin,sellername,cashpurchase)
			values (@company,@fy,@sgstacno1,@totsgst1,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@gstin,@sellername,@cashpurchase)
			if @@error<>0 goto e_error
			end

		if  @totigst1<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno,gstin,sellername,cashpurchase)
			values (@company,@fy,@igstacno1,@totigst1,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno,@gstin,@sellername,@cashpurchase)
			if @@error<>0 goto e_error
			end


		if  @totcess<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno)
			values (@company,@fy,@cessacno,@totcess,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno)
			if @@error<>0 goto e_error
			end

		if  @totexcise<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno)
			values (@company,@fy,@exciseacno,@totexcise ,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno)
			if @@error<>0 goto e_error
			end

		if  @tottcs<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode,seller,billno,billdate_closedate,taxtype,chlno_rtxnno)
			values (@company,@fy,@tcsacno,@tottcs,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y',@suppglactno,@INVno,@txndt,1,@bctrlno)
			if @@error<>0 goto e_error
			end

		set @totalamt=@totalamt
			if @otherchgs<>0 
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
			values (@company,@fy,60,@otherchgs,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'D',@txndt,@grno,'Y')
			if @@error<>0 goto e_error
			end

		if @damttot<>0
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
			values (@company,@fy,59,@damttot,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' % Discount - ' + @supplier,'C',@txndt,@grno,'Y')
			if @@error<>0 goto e_error
			end

		if @discount<>0 
			begin
			insert vouchers(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
			values (@company,@fy,59,@discount,'To Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'C',@txndt,@grno,'Y')
			if @@error<>0 goto e_error
			end
		insert vouchers
		(company,fy,actnum,amt,partic,amttyp,txndt,grno,channelcode)
		values 
		(@company,@fy,@iglactno,@totalamt+@otherchgs-@discount-@DAMTTOT+@SALETAX+@totcgst+@totsgst+@totigst+@totcgst1+@totsgst1+@totigst1+@totcess+@totexcise+@tottcs,
		'By Pur.Chln.No ' + ltrim(str(@chlno))+ ' - ' + @supplier,'C',@txndt,@grno,'Y')
		if @@error<>0 goto e_error
	end
------------ posting ends

declare @stname varchar(100)
select @stname=storename from store where storeno=@storeno
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@stname))+ ' Purchase Chalan No ' + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,6) + ' Edited ',151,0)
commit transaction
set @sql = 'drop table '+ @t1
exec( @sql)
return
e_error:
rollback transaction
set @sql = 'drop table '+ @t1
exec @sql
raiserror(@msg,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[SpStRewriteSTOpgbal]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SpStRewriteSTOpgbal] (@ctrlno numeric ) 
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
declare @bal numeric(18,4) 
declare @tctrlno numeric
declare @balance numeric(18,4)
declare @cr numeric(18,4)
declare @dr numeric(18,4)
declare @eofy datetime
declare @bofy datetime
declare @fy numeric
declare @msg varchar(1000)
declare @company int 
declare @iname varchar(200) 
declare @cnt numeric
DECLARE @ERR VARCHAR(100)
set @cnt=0
update storehist set qty1=case when amttyp='C' then qty else -qty end where ctrlno=@ctrlno
select @company=company from store where storeno=(select storeno from storeitems where ctrlno=@ctrlno)
select @iname=iname from storeitems where ctrlno=@ctrlno
--set @msg= CONVERT(varchar(20),@company)
---raiserror(@msg,10,-1)
delete from storeprimary where fy not in(select fy from finyear)

declare c1 cursor for select fy from finyear where company=@company order by fy
open c1
fetch next from c1 into @fy
while @@FETCH_STATUS=0
	begin
--set @msg= CONVERT(varchar(20),@fy)
--raiserror(@msg,10,-1)

	if not exists(select * from StorePrimary where fy=@fy and ictrlno=@ctrlno)
		begin
			insert into StorePrimary(fy,ictrlno) values (@fy,@ctrlno)
		end
		fetch next from c1 into @fy
	end
close c1
deallocate c1
set @bal=0
select @bal=isnull(opgstock,0) from storeitems where ctrlno=@ctrlno  -- this line is added on 26 May 2018 

declare c2 cursor for select ctrlno,balance,fy from storeprimary where ictrlno=@ctrlno order by fy
open c2
fetch next from c2 into @tctrlno,@balance,@fy
while @@fetch_status=0
	begin
	--SET @CNT=@CNT+1
	update storeprimary set opgbal=@bal where ictrlno=@ctrlno and fy=@fy
	select @bofy=date from finyear where fy=@fy
	set @eofy = dbo.geteofy(@bofy)
	--select @cr=sum(case when amttyp='C' then isnull(qty,0) else 0 end),@dr=sum(case when amttyp='D' then isnull(qty,0) else 0 end) from storehist where ctrlno=@ctrlno and datediff(d,'1/1/2000',txndt) between datediff(d,'1/1/2000',@bofy) and datediff(d,'1/1/2000',@eofy)
	update storeprimary set opgbal=@bal,balance=dbo.getstockfast(@ctrlno,@eofy,@fy) where ctrlno=@tctrlno
	select @bal=balance from storeprimary where ctrlno=@tctrlno
	--print str(@ctrlno) + ' ' + @iname + ' '+  convert(varchar(20),@bal) + ' '+ str(@fy) + ' ' + convert(varchar(20),@eofy)
SET @ERR = LTRIM(STR(@CNT))
	--RAISERROR(@ERR,0,1) WITH NOWAIT
	fetch next from c2 into @tctrlno,@balance,@fy
	end
close c2
deallocate c2
END




GO
/****** Object:  StoredProcedure [dbo].[sptbadv]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbadv] @bnqctrlno numeric,@txndt datetime,@amount money,@mop int, @crnum varchar(50),@crname varchar(50) ,@crbank varchar(50),@ctrlno numeric out 
/* with encryption */
AS
--print 'periya karuppa'
declare @company numeric
declare @fy numeric
declare @banquet numeric
declare @grno numeric
declare @ret_val numeric
declare @usernm varchar(50)
declare @advrno numeric
declare @bofy datetime
declare @naration varchar(100)
declare @status char(1)
declare @actnum numeric
begin transaction
if @amount <= 0 begin print 'The amount can not be zero' goto e_error end
if not exists(select * from banquet where ctrlno=@bnqctrlno) begin print 'The entry with the given reference number is not available' end
select @banquet=bnqno,@status=status from banquet where ctrlno=@bnqctrlno
if @status='C' begin print 'This Booking Entry is cancelled' goto e_error end
if @status='B' begin print 'This Booking Entry is already billed and closed' goto e_error end
select @company=company from banqmaster where bnqno=@banquet
select @usernm=nullif(usernm,'Not found')  from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
if isnull(@usernm,'')='' begin print 'U s e r   n o t   L o g g e d   i n' goto e_error end
exec getfy @txndt,@company,@fy out
if @fy=0 goto e_error
exec getgrp @txndt,@company,@grno out
if @grno=0 goto e_error
select @bofy=[date] from finyear where fy=@fy
if not exists(select * from banqmaster where datediff(d,advdate,@bofy)=0 and bnqno=@banquet)
   update banqmaster set advrno=0,advdate=@bofy where bnqno=@banquet
   update banqmaster set advrno=advrno+1 where bnqno=@banquet
   select @advrno=advrno from banqmaster where bnqno=@banquet
insert into banqadv (bnqctrlno,txndt,advrno,amount,mop,crnum,crname,crbank,usernm,grno) values (@bnqctrlno,@txndt,@advrno,@amount,@mop,@crnum,@crname,@crbank,@usernm,@grno)
set @ctrlno=@@identity
set @naration='To  Bnq.Adv.Rec.No' + ltrim(str(@advrno)) + '-sys.no' + ltrim(str(@ctrlno))
set @actnum= case when @mop=0 then 1
                                when @mop=1 then 22
                                when @mop=2 then 46
                                when @mop=3 then @crnum end
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
 values (@company, @fy,  @actnum ,@amount , @naration  , 'D', @txndt , @grno ,'Y', @advrno,'RADV')
if @@error <> 0 goto e_error
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
 values (@company, @fy,  69 ,@amount ,'By ' + rtrim(substring(@naration,4,200))  , 'C', @txndt , @grno ,'Y', @advrno,'RADV')
if @@error <> 0 goto e_error
commit transaction
return
e_error:
rollback transaction 
raiserror('Error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[sptbadvd]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbadvd] @ctrlno numeric 
/* with encryption */
AS
declare @company int
declare @fy int
declare  @grno numeric
declare @bnqctrlno numeric
declare @bnqno numeric
declare @txndt datetime
declare @ret_val numeric
declare @status  char(1)
declare @amount money
declare @sql varchar(5000)
begin transaction
select @bnqctrlno=bnqctrlno,@grno=grno,@txndt=txndt,@amount=amount  from banqadv where ctrlno=@ctrlno
select @bnqno=bnqno,@status=status from banquet where ctrlno=@bnqctrlno
select @company=company from banqmaster where bnqno=@bnqno
if @status='B' begin print 'This is a closed Entry...No deletion alloed' goto e_error end
if @status='C' begin print 'This is a cancelled Entry...No deletion alloed' goto e_error end
exec @fy=getfy @txndt,@company,@fy out
if @@error<>0 goto e_error
exec delgrno @company,@fy,@grno,@txndt,@ret_val out
if @@error<>0 goto e_error
delete from banqadv where ctrlno=@ctrlno
if @@error<>0 goto e_error
declare @usernm varchar(50)
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

set @sql = 'Banquet Booking No ' + ltrim(str(@ctrlno)) + '-Advance deleted - Amount ' + convert(varchar(20),@amount)
insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, @sql ,80,0)
if @@error <> 0  goto e_error


commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[sptbbb]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbbb] @ctrlno numeric,@mop tinyint, @crnum varchar(50),@crname varchar(50),@crbank varchar(50),
@temp varchar(50),@pax numeric,@txndt datetime,@stbnq money,@stfood money,@vatfood money,@pax1 numeric,@ltbnq money,@cessbnq money,@cessfood money,@kkcbnq money,@kkcfood money
/* with encryption */
AS
declare @usernm varchar(50)
declare @bnqno numeric
declare @company int
declare @fy int
declare @grno numeric
declare @partic varchar(100)
declare @sql varchar(8000)
declare @status char(1)
declare @bno numeric
declare @bofy datetime
declare @billamount money
declare @min numeric
declare @rate money
declare @advance money
declare @amt money
declare @desc varchar(100)
declare @amttyp char(1)
declare @actno numeric
declare @cno numeric
declare @name varchar(100)
declare @crdr char(4)
declare @chargedpax numeric
declare @chargedpax1 numeric

declare @bnqsale money
declare @bnqsale2 money
declare @servicetype tinyint 
declare @bnq1 money
declare @bnq2 money
declare @rate1 money
declare @sttableno int 
declare @bnqsttableno int
declare @taxacno int

begin transaction
if not exists(select * from banquet where ctrlno=@ctrlno) begin print 'The entry with the given reference number is not available' end
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

if isnull(@usernm,'')='' begin print 'U s e r   n o t   L o g g e d   i n' goto e_error end
select @bno=isnull(billno,0),@cno=cno,@min=minpax,@rate=rate,@rate1=rate1,@bnqno=bnqno,@status=status,@name=name from banquet where ctrlno=@ctrlno
select @company=company,@sttableno=sttableno,@bnqsttableno=bnqsttableno    from banqmaster where bnqno=@bnqno
select @advance=sum(amount) from banqadv where bnqctrlno=@ctrlno
--select @name=name from guestad where cno=@cno
set @advance=isnull(@advance,0)
if @pax < @min 
	begin
	set @chargedpax = @min
	set @bnq1=@rate*@min
	end
else 
	begin
	set @chargedpax = @pax
	set @bnq1=@rate*@pax
	end


if @pax1 < @min 
	begin
	set @chargedpax1 = @min
	set @bnq2=@rate1*@min
	end
else 
	begin
	set @chargedpax1 = @pax1
	set @bnq2=@rate1*@pax1
	end


set @billamount = @bnq1+@bnq2  
set @bnqsale = @bnq1
set @bnqsale2 = @bnq2

if @status='C' begin print 'This Booking Entry is cancelled' goto e_error end
if @status='B' begin print 'This Booking Entry is already billed and closed' goto e_error end
exec getfy @txndt,@company,@fy out
if @fy=0 goto e_error
exec getgrp @txndt,@company,@grno out
if @grno=0 goto e_error
if @bno=0
begin
	if not exists(select * from banqbillno where bnqno=@bnqno and fy=@fy) 
	insert into banqbillno (bnqno,fy,billno) values (@bnqno,@fy,0)

	update banqbillno set billno = billno + 1 where bnqno=@bnqno and fy=@fy
	select @bno=billno from banqbillno where bnqno=@bnqno and fy=@fy

end
set @partic = 'Bnq.Bill No.' + ltrim(str(@bno)) +'-'+ @name
declare @oldactno numeric
declare @totamt money
declare @oamttyp char(1)
set @sql = 'declare c1 cursor for select description,amount,actno,amttyp,servicetype from ' + @temp + ' order by actno'
exec(@sql)
open c1
declare @fs int
declare @oservicetype int
fetch next from c1 into @desc,@amt,@actno,@amttyp,@servicetype
set @fs = @@fetch_status
while @fs=0 
begin
	set @oldactno=@actno
	set @totamt = 0
	set @oservicetype=@servicetype
	set @oamttyp = @amttyp
	while @fs=0 and @actno=@oldactno and @servicetype=@oservicetype and @amttyp=@oamttyp
		begin
			set @totamt=@totamt+@amt
			insert into banqbill (bnqctrlno,description,amount,amttyp,actno,servicetype) values (@ctrlno,@desc,@amt,@amttyp,@actno,@servicetype)
			if @@error <> 0 goto e_error
			fetch next from c1 into @desc,@amt,@actno,@amttyp,@servicetype
			set @fs = @@fetch_status
		end    
		if @oamttyp='D'
			begin
			set @crdr = 'To '
			set @billamount = @billamount-@totamt
			if @oservicetype=0
				set @bnq1=@bnq1-@totamt
			else
				set @bnq2=@bnq2-@totamt
			end
		else
			begin
			set @crdr = 'By '
			set @billamount = @billamount+@totamt
			if @oservicetype=0
				set @bnq1=@bnq1+@totamt
			else
				set @bnq2=@bnq2+@totamt
			end
       
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
	values (@company, @fy,  @oldactno ,@totamt , @crdr+@partic  ,@oamttyp, @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error
end

close c1
deallocate c1

set @billamount=@billamount+@stbnq+@stfood+@vatfood+@LTBNQ+@cessbnq+@cessfood+@kkcbnq+@kkcfood

update banquet set status='B',billno=@bno,billdate=@txndt,billamount=@billamount,mop=@mop,crnum=@crnum,crname=@crname,crbank=@crbank,
chargedpax=@chargedpax,usernm2=@usernm,grno=@grno,stbnq=@stbnq,stfood=@stfood,vatfood=@vatfood,bnq1=@bnq1,bnq2=@bnq2 ,
chargedpax1=@chargedpax1,ltbnq=@ltbnq,cessbnq=@cessbnq,cessfood=@cessfood,kkcbnq=@kkcbnq,kkcfood=@kkcfood
where ctrlno=@ctrlno

insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  9  ,@bnqsale , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy, 84  ,@bnqsale2 , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error


if exists(select * from sttabler where sttableno=@bnqsttableno and @txndt between sdt and edt )
select @taxacno=taxacno from sttabler where sttableno=@bnqsttableno and @txndt between sdt and edt
if isnull(@taxacno,0)=0 set @taxacno=72
if @stbnq > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  @taxacno  ,@stbnq , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error

if @cessbnq > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  86  ,@cessbnq , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error

if exists(select * from sttabler where sttableno=@sttableno and @txndt between sdt and edt )
	select @taxacno=taxacno from sttabler where sttableno=@sttableno and @txndt between sdt and edt
if isnull(@taxacno,0)=0 set @taxacno=82

if @stfood > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  @taxacno  ,@stfood , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error

if @cessfood > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  87  ,@cessfood , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error

if @kkcbnq > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  94  ,@kkcbnq , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error

if @kkcfood > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  96  ,@kkcfood , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error


if @vatfood > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  83  ,@vatfood , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error

if @Ltbnq > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  85  ,@Ltbnq , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error


if @advance > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
values (@company, @fy,  69 ,@advance , 'To ' +@partic  ,'D', @txndt , @grno ,'Y', @bno,'BNQ')
	if @@error <> 0 goto e_error

set @actno = case when @mop=0 then 1
                               when @mop=1 then 22
                               when @mop=2 then 46
                               when @mop=3 then @crnum  end
if @billamount-@advance > 0
   insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  @actno  ,@billamount-@advance , 'To ' +@partic  ,'D', @txndt , @grno ,'Y', @bno,'BNQ')
else
   insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  @actno  ,@advance-@billamount , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')

	if @@error <> 0 goto e_error

--print 'periya'
--goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[sptbbbd]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbbbd] @ctrlno numeric  
/* with encryption */
AS
declare @bnqno numeric
declare @company int
declare @fy int
declare @grno numeric
declare @sql varchar(1000)
declare @txndt datetime
declare @Ret_Val int
declare @status char(1)
declare @billamount money
declare @usernm varchar(50)
begin transaction
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
if isnull(@usernm,'')='' begin print 'U s e r   n o t   L o g g e d   i n' goto e_error end
select @billamount=billamount,@status=status,@txndt=billdate,@bnqno=bnqno,@grno=grno  from banquet where ctrlno=@ctrlno
select @company=company from banqmaster where bnqno=@bnqno
if @status='C' begin print 'This Booking Entry is cancelled' goto e_error end
if @status=' ' begin print 'This Booking Entry is not yet billed and closed' goto e_error end
exec @fy=getfy @txndt,@company,@fy out
if @@error<>0 goto e_error
exec delgrno @company,@fy,@grno,@txndt,@ret_val out
if @@error<>0 goto e_error

update banquet set crnum='',crname='',mop=0,crbank='',billdate=null,usernm2='',grno=0,billamount=0,chargedpax=0,status=' ' where ctrlno=@ctrlno
if @@error<>0 goto e_error
delete from banqbill where bnqctrlno=@ctrlno
--print 'periya'
set @sql = 'Banquet Booking No ' + ltrim(str(@ctrlno)) + '-Bill deleted - Amount ' + convert(varchar(20),@billamount)
 insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, @sql ,80,0)
if @@error <> 0  goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,1)

GO
/****** Object:  StoredProcedure [dbo].[sptbbbe]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbbbe] @ctrlno numeric,@mop tinyint, @crnum varchar(50),@crname varchar(50),@crbank varchar(50),
@temp varchar(50),@pax numeric,@txndt datetime,@stbnq money,@stfood money,@vatfood money,@pax1 numeric,@ltbnq money,@cessbnq money,@cessfood money,@kkcbnq money,@kkcfood money
/* with encryption */
AS
declare @usernm varchar(50)
declare @bnqno numeric
declare @company int
declare @fy int
declare @grno numeric
declare @partic varchar(100)
declare @sql varchar(8000)
declare @status char(1)
declare @bno numeric
declare @bofy datetime
declare @billamount money
declare @min numeric
declare @rate money
declare @advance money
declare @amt money
declare @desc varchar(100)
declare @amttyp char(1)
declare @actno numeric
declare @cno numeric
declare @name varchar(100)
declare @crdr char(4)
declare @chargedpax numeric
declare @chargedpax1 numeric
declare @ret_val int
declare @oldbillamount money
declare @otxndt datetime

declare @bnqsale money
declare @bnqsale2 money
declare @servicetype tinyint 
declare @bnq1 money
declare @bnq2 money
declare @rate1 money
declare @sttableno int
declare @bnqsttableno int
declare @taxacno int

declare @errmsg varchar(2000)
set @errmsg = 'Error'
begin transaction
if not exists(select * from banquet where ctrlno=@ctrlno) begin print 'The entry with the given reference number is not available' end
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
if isnull(@usernm,'')='' begin print 'U s e r   n o t   L o g g e d   i n' goto e_error end
select @bno=isnull(billno,0),@cno=cno,@min=minpax,@rate=rate,@rate1=rate1,@bnqno=bnqno,@status=status,@name=name from banquet where ctrlno=@ctrlno
select @oldbillamount=billamount,@status=status,@otxndt=billdate,@bnqno=bnqno,@grno=grno  from banquet where ctrlno=@ctrlno
select @company=company,@sttableno=sttableno,@bnqsttableno=bnqsttableno from banqmaster where bnqno=@bnqno
if @status='C' begin print 'This Booking Entry is cancelled' goto e_error end
if @status=' ' begin print 'This Booking Entry is not yet billed and closed' goto e_error end
exec @fy=getfy @otxndt,@company,@fy out
if @@error<>0 goto e_error
--set @errmsg = 'GRNO : ' + str(@grno) + ' ' +  convert(varchar(20),@otxndt) + ' '  + str(@company) +  ' '+ str(@fy)
--goto e_error
exec delgrno @company,@fy,@grno,@otxndt,@ret_val out
if @@error<>0 goto e_error
update banquet set crnum='',crname='',mop=0,crbank='',billdate=null,usernm2='',grno=0,billamount=0,chargedpax=0,status=' ',chargedpax1=0 where ctrlno=@ctrlno
if @@error<>0 goto e_error
delete from banqbill where bnqctrlno=@ctrlno
if @@error<>0 goto e_error
select @company=company from banqmaster where bnqno=@bnqno
select @advance=sum(amount) from banqadv where bnqctrlno=@ctrlno
--select @name=name from guestad where cno=@cno
set @advance=isnull(@advance,0)
if @pax < @min 
	begin
	set @chargedpax = @min
	set @bnq1=@rate*@min
	end
else 
	begin
	set @chargedpax = @pax
	set @bnq1=@rate*@pax
	end

if @pax1 < @min 
	begin
	set @chargedpax1 = @min
	set @bnq2=@rate1*@min
	end
else 
	begin
	set @chargedpax1 = @pax1
	set @bnq2=@rate1*@pax1
	end

	set @bnq1=isnull(@bnq1,0)
	set @bnq2=isnull(@bnq2,0)

set @billamount = @bnq1+@bnq2  
set @bnqsale = @bnq1
set @bnqsale2 = @bnq2
if @status='C' begin print 'This Booking Entry is cancelled' goto e_error end
if @status<>'B' begin print 'This Booking Entry is not yet billed' goto e_error end
exec getfy @txndt,@company,@fy out
if @fy=0 goto e_error
exec getgrp @txndt,@company,@grno out
if @grno=0 goto e_error
update banquet set grno=@grno where ctrlno=@ctrlno
if @bno=0
begin
select @bofy=[date] from finyear where fy=@fy
if not exists(select * from banqmaster where datediff(d,billdate,@bofy)=0 and bnqno=@bnqno)
   update banqmaster set billno=0,billdate=@bofy where bnqno=@bnqno
   update banqmaster set billno=billno+1 where bnqno=@bnqno
   select @bno=billno from banqmaster where bnqno=@bnqno
end
set @partic = 'Bnq.Bill No.' + ltrim(str(@bno)) +'-'+ @name
declare @oldactno numeric
declare @totamt money
declare @oamttyp char(1)
set @sql = 'declare c1 cursor for select description,amount,actno,amttyp,servicetype from ' + @temp + ' order by actno'
exec(@sql)
open c1
declare @fs as int 
declare @oservicetype int
fetch next from c1 into @desc,@amt,@actno,@amttyp,@servicetype
set @fs = @@fetch_status
while @fs=0 
begin
	set @oldactno=@actno
	set @totamt = 0
	set @oservicetype=@servicetype
		set @oamttyp = @amttyp
	while @fs=0 and @actno=@oldactno and @servicetype=@oservicetype and @amttyp=@oamttyp 
		begin
		set @totamt=@totamt+@amt
		insert into banqbill (bnqctrlno,description,amount,amttyp,actno,servicetype) values (@ctrlno,@desc,@amt,@amttyp,@actno,@servicetype)
		if @@error <> 0 goto e_error
		fetch next from c1 into @desc,@amt,@actno,@amttyp,@servicetype
		set @fs = @@fetch_status
		end    
	if @oamttyp='D'
		begin
		set @crdr = 'To '
		set @billamount = @billamount-@totamt
		if @oservicetype=0
			set @bnq1=@bnq1-@totamt
		else
			set @bnq2=@bnq2-@totamt

		end
	else
		begin
		set @crdr = 'By '
		set @billamount = @billamount+@totamt
		if @oservicetype=0
			set @bnq1=@bnq1+@totamt
		else
			set @bnq2=@bnq2+@totamt

		end
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	 values (@company, @fy,  @oldactno ,@totamt , @crdr+@partic  ,@oamttyp, @txndt , @grno ,'Y', @bno)
	if @@error <> 0 goto e_error
	--fetch next from c1 into @desc,@amt,@actno,@amttyp
end
close c1
deallocate c1

set @billamount=@billamount+@stbnq+@stfood+@vatfood+@ltbnq+@cessbnq+@cessfood+@kkcbnq+@kkcfood

update banquet set status='B',billno=@bno,billdate=@txndt,billamount=@billamount,mop=@mop,crnum=@crnum,crname=@crname,crbank=@crbank,
chargedpax=@chargedpax,usernm2=@usernm,grno=@grno,stbnq=@stbnq,stfood=@stfood,vatfood=@vatfood,bnq1=@bnq1,bnq2=@bnq2 ,
chargedpax1=@chargedpax1,ltbnq=@ltbnq,cessbnq=@cessbnq,cessfood=@cessfood,kkcbnq=@kkcbnq,kkcfood=@kkcfood
where ctrlno=@ctrlno

insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  9  ,@bnqsale , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy, 84  ,@bnqsale2 , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')

if exists(select * from sttabler where sttableno=@bnqsttableno and @txndt between sdt and edt )
select @taxacno=taxacno from sttabler where sttableno=@bnqsttableno and @txndt between sdt and edt
if isnull(@taxacno,0)=0 set @taxacno=72

if @stbnq > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  @taxacno   ,@stbnq , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
if @cessbnq > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  86  ,@cessbnq , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')


if exists(select * from sttabler where sttableno=@sttableno and @txndt between sdt and edt )
	select @taxacno=taxacno from sttabler where sttableno=@sttableno and @txndt between sdt and edt
if isnull(@taxacno,0)=0 set @taxacno=82

if @stfood > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  @taxacno  ,@stfood , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
if @cessfood > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  87  ,@cessfood , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')

if @vatfood > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  83  ,@vatfood , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
if @ltbnq > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  85  ,@ltbnq , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')

if @kkcbnq > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  94  ,@kkcbnq , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')

if @kkcfood > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  96  ,@kkcfood , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')




if @advance > 0 
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
 values (@company, @fy,  69 ,@advance , 'To ' +@partic  ,'D', @txndt , @grno ,'Y', @bno,'BNQ')
set @actno = case when @mop=0 then 1
                               when @mop=1 then 22
                               when @mop=2 then 46
                               when @mop=3 then @crnum  end
if @billamount-@advance > 0
   insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  @actno  ,@billamount-@advance , 'To ' +@partic  ,'D', @txndt , @grno ,'Y', @bno,'BNQ')
else
   insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
   values (@company, @fy,  @actno  ,@advance-@billamount , 'By ' +@partic  ,'C', @txndt , @grno ,'Y', @bno,'BNQ')
--print 'periya'
--goto e_error
set @sql = 'Banquet Booking No ' + ltrim(str(@ctrlno)) + '-Bill Edited - Old Amount ' + convert(varchar(20),@oldbillamount) + ' ,' +convert(varchar(20),@otxndt) +'  to ' + convert(varchar(20),@billamount) + ',' + convert(varchar(20),@txndt)
insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, @sql ,80,0)
if @@error <> 0  goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror(@errmsg,16,-1)
return

GO
/****** Object:  StoredProcedure [dbo].[sptbbq]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbbq] 
@bnqno numeric,@cno numeric,@sdof datetime,@edof datetime,@ftype tinyint,@min numeric,@max numeric,@rate money,@hall int,@contactname varchar(50),@contactno varchar(50),
@temp1 varchar(20),@temp2 varchar(20),@advance money,@mop tinyint,@crnum varchar(20),@crname varchar(50),@crbank varchar(50),
@address varchar(50),@name varchar(50),@add1 varchar(50),@add2 varchar(50),@add3 varchar(50),@city varchar(50),@state varchar(50),
@pin varchar(50),@phone varchar(50),@fax varchar(50),@email varchar(50),@rate1 money,@GSTIN VARCHAR(50),@ctrlno numeric out,@cnt numeric out,@dob datetime = ''
/* with encryption */
AS
declare @sql varchar(8000)
declare @mode numeric
declare @details varchar(150)
declare @txndt datetime
declare @usernm varchar(60)
begin transaction
if @dob='' set @dob=getdate()
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
if isnull(@usernm,'')='' begin print 'U s e r   n o t   L o g g e d   i n' goto e_error end
if @cno<>0 select @name=name,@add1=add1,@add2=add2,@add3=add3,@city=city,@state=state,@pin=pin,@phone=phone,@email=email from guestad where cno=@cno
insert into banquet (bnqno,cno,dob,sdof,edof,ftype,minpax,maxpax,rate,hall,contactperson,contactno,usernm1,name,add1,add2,add3,city,state,pin,phone,email,rate1,GSTIN) values (
@bnqno,@cno,@dob,@sdof,@edof,@ftype,@min,@max,@rate,@hall,@contactname,@contactno,@usernm,@name,@add1,
@add2,@add3,@city,@state,@pin,@phone,@email,@rate1,@GSTIN)
if @@error<>0 goto e_error
set @ctrlno=@@identity
set @sql = 'declare c1 cursor for select mode,details from ' + @temp1  + ' order by mode,gridno'
exec(@sql)
open c1
fetch next from c1 into @mode,@details
while @@fetch_status=0
begin
insert into banquet1 (ctrlno,mode,detail) values (@ctrlno,@mode,@details)
if @@error<>0 goto e_error
fetch next from c1 into @mode,@details
end
close c1
deallocate c1
set @sql = 'declare c1 cursor for select mode from ' + @temp2  + ' order by gridno'
exec(@sql)
open c1
fetch next from c1 into @mode
while @@fetch_status=0
begin
insert into banquet2 (ctrlno,foodcode) values (@ctrlno,@mode)
if @@error<>0 goto e_error
fetch next from c1 into @mode
end
close c1
deallocate c1
if @advance<>0
  begin
  set @txndt=@dob
  exec sptbadv  @Ctrlno,@txndt,@advance,@mop,@crnum,@crname,@crbank,@cnt out
  if @@error<>0 goto e_error
  end
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sptbbqc]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbbqc]  @ctrlno numeric,@mop int,@crnum varchar(50),@crname varchar(50),@crbank varchar(50) ,@reason varchar(100) 
/* with encryption */
AS
declare @bnqno numeric 
declare @company int
declare @fy int
declare @grno numeric
declare @sql varchar(1000)
declare @txndt datetime
declare @Ret_Val int
declare @status char(1)
declare @advance money
declare @actno numeric
declare @partic varchar(100)
declare @usernm varchar(50)
declare @cancelledon datetime
--print 'periya'
begin transaction
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
if isnull(@usernm,'')='' begin print 'U s e r   n o t   L o g g e d   i n' goto e_error end
set @txndt=getdate()
select @status=status,@bnqno=bnqno,@grno=grno,@cancelledon=cancelledon  from banquet where ctrlno=@ctrlno
select @company=company from banqmaster where bnqno=@bnqno
select @advance=sum(amount) from banqadv where bnqctrlno=@ctrlno
if @status='C' begin print 'This Booking Entry is already cancelled on ' + convert(varchar(20),@cancelledon,3) goto e_error end
if @status='B' begin print 'This Booking Entry is already billed and closed' goto e_error end
exec @fy=getfy @txndt,@company,@fy out
if @@error<>0 goto e_error
exec getgrp @txndt,@company,@grno out
if @grno=0 goto e_error
update banquet set status='C',cancelledby=@usernm,cancellreason=@reason,cancelledon=getdate() where ctrlno=@ctrlno
if isnull(@advance,0)<>0
begin
set @actno=case when @mop=0 then 1
                             when @mop=1 then  @crnum end
set @partic = 'Bnq.Regn.No.' + ltrim(str(@ctrlno)) + ' - cancelled advance refunded'
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
 values (@company, @fy,  69 ,@advance ,'To ' + @partic  , 'D', @txndt , @grno ,'Y', @ctrlno,'BNQ')
if @@error <> 0 goto e_error
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
 values (@company, @fy, @actno ,@advance ,'By ' + @partic  , 'C', @txndt , @grno ,'Y', @ctrlno,'BNQ')
if @@error <> 0 goto e_error
update banquet set cancellgrno=@grno,cancellglac=@actno,cancellmop=@mop where ctrlno=@ctrlno
end
set @sql = 'Banquet Booking No ' + ltrim(str(@ctrlno)) + '-Cancelled and advance refunded ' + convert(varchar(20),isnull(@advance,0))
 insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, @sql ,80,0)
if @@error <> 0  goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[sptbbqE]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbbqE] @ctrlno numeric,
@bnqno numeric,@cno numeric,@sdof datetime,@edof datetime,@ftype tinyint,@min numeric,@max numeric,@rate money,@hall int,@contactname varchar(50),@contactno varchar(50),
@temp1 varchar(20),@temp2 varchar(20),@address varchar(50),@name varchar(50),@add1 varchar(50),@add2 varchar(50),@add3 varchar(50),@city varchar(50),@state varchar(50),
@pin varchar(50),@phone varchar(50),@fax varchar(50), @email varchar(50),@rate1 money 
/* with encryption */
AS
declare @sql varchar(8000)
declare @mode numeric
declare @details varchar(150)
declare @txndt datetime
declare @usernm varchar(60)
begin transaction
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
if isnull(@usernm,'')='' begin print 'U s e r   n o t   L o g g e d   i n' goto e_error end
/*
if @cno=0
  begin
    insert into guestad (address,name,add1,add2,add3,city,state,pin,phone,fax,email) values 
    (@address,@name,@add1,@add2,@add3,@city,@state,@pin,@phone,@fax,@email)
    if @@error<>0 goto e_error
    set @cno=@@identity
  end
*/
--insert into banquet (bnqno,cno,dob,sdof,edof,ftype,minpax,maxpax,rate,hall,contactperson,contactno,usernm1) values (
--@bnqno,@cno,getdate(),@sdof,@edof,@ftype,@min,@max,@rate,@hall,@contactname,@contactno,@usernm)
update banquet set
cno=@cno,sdof=@sdof,edof=@edof,ftype=@ftype,minpax=@min,maxpax=@max,rate=@rate,hall=@hall,contactperson=@contactname,contactno=@contactno,
name=@name,
add1=@add1,
add2=@add2,
add3=@add3,
city=@city,
state=@state,
pin=@pin,
phone=@phone,
email=@email,
rate1=@rate1
where ctrlno=@ctrlno
if @@error<>0 goto e_error
delete from banquet1 where ctrlno=@ctrlno
if @@error<>0 goto e_error
delete from banquet2 where ctrlno=@ctrlno
if @@error<>0 goto e_error
set @sql = 'declare c1 cursor for select mode,details from ' + @temp1  + ' order by mode,gridno'
exec(@sql)
open c1
fetch next from c1 into @mode,@details
while @@fetch_status=0
begin
insert into banquet1 (ctrlno,mode,detail) values (@ctrlno,@mode,@details)
if @@error<>0 goto e_error
fetch next from c1 into @mode,@details
end
close c1
deallocate c1
set @sql = 'declare c1 cursor for select mode from ' + @temp2  + ' order by gridno'
exec(@sql)
open c1
fetch next from c1 into @mode
while @@fetch_status=0
begin
insert into banquet2 (ctrlno,foodcode) values (@ctrlno,@mode)
if @@error<>0 goto e_error
fetch next from c1 into @mode
end
close c1
deallocate c1
set @sql = 'Banquet Booking No ' + ltrim(str(@ctrlno)) + '-Modified'
 insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, @sql ,80,0)
if @@error <> 0  goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[SPTBCC]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.SPTBCC    Script Date: 15/02/2004 6:23:01 PM ******/
/**** Updated on 04/July/2009 10.15 am (moochu kuthu pidichiuku :( *****/
CREATE PROCEDURE [dbo].[SPTBCC] @canctrlno numeric,@usernm varchar(30),@sessionid varchar(200)=''
/* with encryption */
as
declare @company int
declare @fy int
declare @txndt datetime
declare @ret_val numeric
declare @rgrno numeric
declare @nsgrno numeric
declare @ctrlno numeric
declare @myctrlno numeric
declare @noshowchgs money
declare @refundamt money
declare @rooms numeric
declare @sdt datetime
declare @edt datetime
declare @dt datetime

begin transaction
--type(s) 
--refund=2
--noshow=1
select @txndt=txndt,@myctrlno=mctrlno,@rooms=cancelrooms from sptbrs_can where ctrlno=@canctrlno

if exists(select * from sptbrs1 where myctrlno=@myctrlno and cancelledrooms < @rooms)
begin
print 'The revival might have already done..No. of cancelled rooms is less than the revival rooms'
goto e_error
end

update sptbrs1 set cancelledrooms=cancelledrooms-@rooms  where myctrlno=@myctrlno
if @@error<>0 goto e_error
select @ctrlno=ctrlno from sptbrs1 where myctrlno=@myctrlno
select @sdt=arrivaldate,@edt=departuredate  from sptbrs where ctrlno=@ctrlno

set @rgrno=0
set @nsgrno=0
if exists(select * from sptbrs_rn where mctrlno=@canctrlno and type=2) select @rgrno=grno,@refundamt=amount from sptbrs_rn where mctrlno=@canctrlno and type=2
if exists(select * from sptbrs_rn where mctrlno=@canctrlno and type=1) select @nsgrno=grno,@noshowchgs=amount  from sptbrs_rn where mctrlno=@canctrlno and type=1
set @company=1
exec getfy @txndt , @company , @fy out
if @@error<>0 goto e_error
if @fy=0 goto e_error
if isnull(@rgrno,0)<>0   EXEC DELGRNO @COMPANY,@FY,@rGRNO,@TXNDT,@RET_VAL OUT
if @@error<>0 goto e_error
if isnull(@nsgrno,0)<>0 EXEC DELGRNO @COMPANY,@FY,@nsGRNO,@TXNDT,@RET_VAL OUT
if @@error<>0 goto e_error
update sptbrs set refundamount=isnull(refundamount,0) - isnull(@refundamt,0) ,refundmop=null,refundglactno=null,
noshowcharges=isnull(noshowcharges,0)-@noshowchgs,noshowmop=null,noshowcrnum=null,noshowcrtype=null,noshowglactno=null,
noshowusernm=null,cancellreason=null ,refundgrno=null,noshowgrno=null,cancelledon=null,status=' '
where ctrlno=@ctrlno
if @@error<>0 goto e_error
delete from sptbrs_rn where mctrlno=@canctrlno
if @@error<>0 goto e_error
delete from sptbrs_can where ctrlno=@canctrlno
if @@error<>0 goto e_error
delete from sptbrs_noshow where rsmyctrlno=@myctrlno
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=@sessionid
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=@sessionid and [host_id]=host_id()

set @dt=@sdt
while datediff(d,@dt,@edt) >=0
begin
		if not exists (select * from staahinv where status='' and datediff(d,txndt,@dt) = 0) insert into staahinv (txndt) values (@DT)
		if not exists (select * from crsinv where status='' and datediff(d,txndt,@dt) = 0) insert into crsinv (txndt,status,message) values (@DT,'','')
set @dt=dateadd(d,1,@dt)
end


insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, 'Cancellation Revived..Reservation No.' +STR(@ctrlno)+'/'+str(@myctrlno) ,23,0)
if @@error <> 0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Cancellation of reservation failed',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[sptbcc_editns]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbcc_editns] @myctrlno numeric,@txndt datetime,
@noshow money,@nsmop int,@crnum char(20), @crname varchar(30), 
@crtype varchar(20),@nsglactno numeric,@reason varchar(500) = '',@sessionid varchar(200)=''
/* with encryption */
AS
declare @oldgrno numeric
declare @rctrlno numeric
declare @usernm numeric
declare @partic varchar(200)
declare @ret_val numeric 
declare @company numeric
declare @fy numeric
declare @otxndt datetime
declare @ngrno numeric
declare @noshowchgs money
declare @rsmyctrlno numeric
declare @actnum numeric
declare @part varchar(500)
declare @grno numeric
declare @ctrlno numeric
/*
@ctrlno = sptbrs_rn ctrlno
@myctrlno = sptbrs1 ctrlno
@rctrlno = sptbrs ctrlno
*/
begin transaction
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
select @rsmyctrlno=rsmyctrlno from sptbrs_noshow where myctrlno=@myctrlno
select @otxndt=txndt,@ngrno=grno,@noshowchgs=amount,@myctrlno=myctrlno,@rctrlno=mctrlno  from sptbrs_rn where myctrlno=@rsmyctrlno
set @company=1

exec getfy @otxndt , @company , @fy out
if @@error<>0 goto e_error
if @fy=0 goto e_error

if isnull(@ngrno,0)<>0 
begin
	exec delgrno @company,@fy,@ngrno,@otxndt,@ret_val out
	if @@error<>0 goto e_error
end

--to get the new fy for the new date
exec getfy @otxndt , @company , @fy out
if @@error<>0 goto e_error
if @fy=0 goto e_error

update sptbrs set 
noshowcharges=isnull(noshowcharges,0)-@noshowchgs + @noshow,noshowmop=@nsmop,
noshowcrnum=@crnum,noshowcrtype=@crtype,noshowglactno=@nsglactno,
noshowusernm=@usernm,noshowgrno=0
where ctrlno=@rctrlno
if @@error<>0 goto e_error

if @noshow<>0
begin
	exec getgrp @txndt, @company, @ngrno out
	if @ngrno=0 goto e_error

	update sptbrs set noshowgrno=0
	where ctrlno=@rctrlno

	select @actnum= case 
		when @nsmop=1 then 1
		when @nsmop=2 then 22
		when @nsmop=3 then 46
		when @nsmop=4 then @nsglactno  
	end
	set @part = 'NoShow Charges Ref.No. ' + ltrim(str(@ctrlno))
	select @partic = case
		when @nsmop=1 then @part
		when @nsmop=2 then @part + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype)
		when @nsmop=3 then @part + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype)
		when @nsmop=4 then @part + ltrim(@crname) 
	end
	set @partic = @partic + space(100)
	set @partic = ltrim(substring(@partic,1,97))

	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	values (@company, @fy,  @actnum ,@noshow , 'To ' +@partic  , 'D', @txndt , @ngrno ,'Y', @ctrlno)
	if @@error <> 0 goto e_error

	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	values (@company, @fy,  57 ,@noshow ,'By' + @partic  ,'C', @txndt , @ngrno ,'Y', @ctrlno)
	if @@error <> 0 goto e_error

	declare @nsmopdetails varchar(100)
	set @nsmopdetails=case
	when @nsmop=1 then 'Cash Payment'
	when @nsmop=2 then ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype)
	when @nsmop=3 then ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crtype)
	when @nsmop=4 then ltrim(@crname) 
	end
	print 'Error in updating no show register'
	update sptbrs_rn
	set 
	txndt=@txndt,
	amount=@noshow,
	usernm=@usernm,
	grno=@grno,
	mop=@nsmop
--	mopdetails=@nsmopdetails
	where ctrlno=@ctrlno
	if @@error <> 0 goto e_error
end
commit transaction
return
e_error:
rollback transaction
raiserror('Error in editing no show charges',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[SPTBNS]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.SPTBNS    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[SPTBNS] @myctrlno numeric,@refund money,@rmop int,@rglactno numeric,
@noshowtotal money,@nsmop int,@crnum char(20), @crname varchar(30),@crtype varchar(20),@nsglactno numeric,@usernm varchar(20),
@reason varchar(100),@txndt datetime,@roomcancel int=0,@cancelrooms int=0,@noshow money=0,@gst money=0
/* with encryption */
as
declare @rgrno numeric
declare @ngrno numeric
declare @ractno numeric
declare @nactno numeric
declare @company int
declare @fy int
declare @partic varchar(100)
declare @rdate datetime
declare @rname varchar(100)
declare @actnum numeric
declare @part varchar(100)
declare @rg numeric
declare @ng numeric
declare @ctrlno numeric
declare @net char(1)
declare @nsctrlno numeric
declare @sno numeric
declare @arrivaldate datetime
declare @departuredate datetime
set @sno=0
set @nsctrlno = 0
begin transaction
select @ctrlno=ctrlno from sptbrs1 where myctrlno=@myctrlno
select @departuredate=departuredate,@arrivaldate=arrivaldate  from sptbrs where ctrlno=@ctrlno
if @roomcancel=0 and exists(select * from sptbrs1 where myctrlno=@myctrlno and arrivedrooms > 0)
	begin
	print 'Some rooms have arrived out of this reservation' + nchar(13) + 'So full cancellation is not allowed' + nchar(13) + 'Please check the Cancel Some Rooms Check Box and Cancel it'
	goto e_error
	end
if @roomcancel=1 and @cancelrooms=0
	begin
	print 'You have selected for cancelling some rooms out of the reserved rooms' + nchar(13) + 'But not given the number of room to cancel'
	goto e_error
	end
if @roomcancel=1 and @cancelrooms > (select noofrooms-isnull(arrivedrooms,0)-isnull(cancelledrooms,0) from sptbrs1 where myctrlno=@myctrlno)
	begin
	select @ctrlno= noofrooms-isnull(arrivedrooms,0)-isnull(cancelledrooms,0) from sptbrs1 where myctrlno=@myctrlno
	print 'You have selected more than the cancellable rooms' + nchar(13) + 'Cancellation aborted' + nchar(13) + ltrim(str(@cancelrooms))  + '/' + ltrim(str(@ctrlno))
	goto e_error
	end
if   (select noofrooms-isnull(arrivedrooms,0)-isnull(cancelledrooms,0) from sptbrs1 where myctrlno=@myctrlno)= 0 
	begin
	print 'There is no room to cancell in this entry'
	goto e_error
	end
if exists(select * from setting where g_finaccount=1)
begin
if @rmop=2 and @rglactno=0 goto e_error
if @nsmop=4 and @nsglactno=0 goto e_error
end
set @company=1
select @rdate=bookingdate,@rname=[name],@net=net from sptbrs_v1 where ctrlno=@ctrlno
if datediff(d,'1/1/90',@rdate) > datediff(d,'1/1/90',@txndt) 
	begin
	print 'Date of Cancellation is prior to the booking Date...Operation Cancelled'
	goto e_error
	end
if exists(select * from setting where g_finaccount=0) goto final
exec getfy @txndt , @company , @fy out
if @fy=0 goto e_error
if @refund <> 0
begin
	exec getgrp @txndt, @company, @rgrno out
	if @rgrno=0 goto e_error
	select @actnum =  case when @rmop=1 then 1 else @rglactno end
	set @partic= 'adv.ag. Res. No. ' + ltrim(str(@ctrlno)) +' Dt.' + ltrim(rtrim(convert(varchar(20),@rdate,106)))  + '-Refund' --              'Refund-Reservation-Dt. ' + ltrim(rtrim(convert(varchar(20),@rdate,106))) + ' ' + rtrim(substring(@rname,1,30)) + ' Ref.No '+ltrim(str(@ctrlno))
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	values (@company, @fy,  56 ,@refund , 'To ' + @partic  ,  'D', @txndt , @rgrno ,'Y', @ctrlno)
	if @@error <> 0 goto e_error
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	values (@company, @fy,  @actnum ,@refund ,'By ' +  @partic  , 'C', @txndt , @rgrno ,'Y', @ctrlno)
	if @@error <> 0 goto e_error
end

if @noshow<>0
begin
	exec getgrp @txndt, @company, @ngrno out
	if @ngrno=0 goto e_error
	select @actnum= case 
	when @nsmop=1 then 1
	when @nsmop=2 then 22
	when @nsmop=3 then 46
	when @nsmop=4 then @nsglactno  end
	set @part = 'NoShow Charges Ref.No. ' + ltrim(str(@ctrlno))
	select @partic = case
	when @nsmop=1 then @part
	when @nsmop=2 then @part + ltrim(@crnum) + '|' + ltrim(@crname) + '|' + ltrim(@crtype)
	when @nsmop=3 then @part + ltrim(@crnum) + '|' + ltrim(@crname) + '|' + ltrim(@crtype)
	when @nsmop=4 then @part + ltrim(@crname) 
end
	set @partic = @partic + space(100)
	set @partic = ltrim(substring(@partic,1,97))
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	values (@company, @fy,  @actnum ,@noshowtotal , 'To ' +@partic  , 'D', @txndt , @ngrno ,'Y', @ctrlno)
	if @@error <> 0 goto e_error
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	values (@company, @fy,  57 ,@noshow ,'By' + @partic  ,'C', @txndt , @ngrno ,'Y', @ctrlno)
	if @@error <> 0 goto e_error
	declare @gstacno numeric
	declare @sttableno int
	select @sttableno=sttableno_noshow from setting
	select @gstacno=taxacno from sttabler where sttableno=@sttableno and 
	@noshow between samt and eamt and datediff(d,'1/1/2000',@txndt) between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt) 

	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	values (@company, @fy,  @gstacno  ,@gst ,'By' + @partic  ,'C', @txndt , @ngrno ,'Y', @ctrlno)
	if @@error <> 0 goto e_error

final:
declare @nsmopdetails varchar(100)
set @nsmopdetails=case
	when @nsmop=1 then 'Cash Payment'
	when @nsmop=2 then ltrim(@crnum) + '|' + ltrim(@crname) + '|' + ltrim(@crtype)
	when @nsmop=3 then ltrim(@crnum) + '|' + ltrim(@crname) + '|' + ltrim(@crtype)
	when @nsmop=4 then ltrim(@crname) 
	end
	update finyear set ns_sno=isnull(ns_sno,0)+1 where fy=@fy 
	select @sno=ns_sno from finyear where fy=@fy
insert into sptbrs_noshow (ctrlno,rsmyctrlno,txndt,amount,reason,usernm,grno,mop,mopdetails,noshow,gst,sno) values
(@ctrlno,@myctrlno,@txndt,@noshowtotal,@reason,@usernm,isnull(@ngrno,0),@nsmop,@nsmopdetails,@noshow,@gst,@sno)
if @@error <> 0 goto e_error
select @nsctrlno=@@identity
end

if @roomcancel=0 
	begin
		update sptbrs set refundamount=isnull(refundamount,0)+@refund,refundmop=@rmop,refundglactno=@rglactno,
		noshowcharges=@noshowtotal,noshowmop=@nsmop,noshowcrnum=@crnum,noshowcrtype=@crtype,noshowglactno=@nsglactno,
		noshowusernm=@usernm,cancellreason=@reason ,refundgrno=isnull(@rgrno,0),noshowgrno=isnull(@ngrno,0),cancelledon=@txndt
		where ctrlno=@ctrlno
		if @@error <> 0 goto e_error
		select @cancelrooms=noofrooms-cancelledrooms from sptbrs1 where myctrlno=@myctrlno
		update sptbrs1 set cancelledrooms=noofrooms where myctrlno=@myctrlno
		if @@error <> 0 goto e_error
		if not exists(select * from sptbrs_v1 where ctrlno=@ctrlno and noofrooms > (arrivedrooms+cancelledrooms)) 
		update sptbrs set status='C',STAAHSTATUS='Cancel' where ctrlno=@ctrlno
		if @@error<>0 goto e_error
if @net='Y' begin
update sptbrs_inet set cancelled='Y',cancelledon=getdate() where ctrlno=@ctrlno
if @@error<>0 goto e_error
end
	end
	else
	begin
		update sptbrs1 set cancelledrooms=cancelledrooms+@cancelrooms where myctrlno=@myctrlno
		if @@error <> 0 goto e_error
		declare @status char(1)
		declare @a1 money
		declare @ar numeric
		declare @tr numeric
		select @ar=SUM(arrivedrooms),@tr=sum(noofrooms-cancelledrooms-arrivedrooms) from sptbrs_v1 where ctrlno=@ctrlno
		if @ar=0 and @tr=0 set @status='C' else set @status=' '	
		update sptbrs set refundamount=isnull(refundamount,0)+@refund,refundmop=@rmop,refundglactno=@rglactno,
		noshowcharges=@noshowtotal,noshowmop=@nsmop,noshowcrnum=@crnum,noshowcrtype=@crtype,noshowglactno=@nsglactno,
		noshowusernm=@usernm,cancellreason=@reason ,refundgrno=isnull(@rgrno,0),noshowgrno=isnull(@ngrno,0),status=@status
		where ctrlno=@ctrlno
		if @@error <> 0 goto e_error
		if @status='C' 
			begin
			update sptbrs set cancelledon=@txndt WHERE CTRLNO=@CTRLNO
			if @@error<>0 goto e_error
			if @net='Y' begin
				update sptbrs_inet set cancelled='Y',cancelledon=getdate() where ctrlno=@ctrlno
				if @@error<>0 goto e_error
			end
			end
	end
declare @canctrlno numeric
insert into sptbrs_can (mctrlno,txndt,cancelrooms,reason,usernm) values (@myctrlno,@txndt,@cancelrooms,@reason,@usernm)
if @@error <> 0 goto e_error
select @canctrlno=@@identity

if @noshowtotal<>0
begin
insert into sptbrs_rn  (mctrlno,type,amount,crnum,crname,crbank,usernm,glactno,mop,txndt,grno,ctrlno1,myctrlno,pctrlno) values
(@canctrlno,1,@noshowtotal,@crnum,@crname,@crtype,@usernm,@nsglactno,@nsmop,@txndt,isnull(@ngrno,0),@ctrlno ,@myctrlno,@nsctrlno)
if @@error <> 0 goto e_error
end

if @refund<>0
begin
insert into sptbrs_rn  (mctrlno,type,amount,crnum,crname,crbank,usernm,glactno,mop,txndt,grno,ctrlno1,myctrlno) values
(@canctrlno,2,@refund,' ',' ',' ',@usernm,@rglactno,@rmop,@txndt,isnull(@rgrno,0),@ctrlno ,@myctrlno)
if @@error<>0 goto e_error
end

select @usernm=usernm from comuser where [host_id]=host_id()
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()
insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, 'Reservation Cancelled...Reservation No.' +ltrim(STR(@ctrlno))+ '/' +ltrim(str(@myctrlno))+ ' Date of Cancellation = ' + convert(varchar(20),@txndt) ,23,0)
if @@error <> 0 goto e_error
delete from sptbrooms where myctrlno=@myctrlno

insert into staahinv (txndt,edt) values (@arrivaldate,@departuredate)

commit transaction
return
e_error:
rollback transaction
raiserror('Cancellation of reservation failed',16,-1)




GO
/****** Object:  StoredProcedure [dbo].[sptbos]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbos] @name varchar(50),@add1  varchar(50),@add2  varchar(50),@city varchar(50),@phone varchar(50), @fax varchar(50),@saletaxno varchar(50) ,@stmode int,@cos varchar(50),
@gstin nvarchar(50)=' ',@subglstyle int=0,@registered char(1)='Y'
/* with encryption */
as
declare @sid numeric
declare @sql varchar(4000)
declare @company numeric
declare @fy numeric 
declare @glactno numeric
declare @msg varchar(200)
declare @coname varchar(100)
declare @subgl char(1)
if @subglstyle=0 set @subgl='O' else set @subgl='S'

begin transaction
set @msg = '0 - Duplicate Supplier Name'
print @msg
if exists(select * from suppliers where name=@name)  goto e_error

set @msg = '1 - Error  in opening of Suppliers Account'
print @msg
insert into suppliers (name,add1,add2,city,phone,fax,saltaxno,stmode,gstin,registered) values 
(@name,@add1,@add2,@city,@phone,@fax,@saletaxno,@stmode,@gstin,@registered)
if @@error <> 0 goto e_error
select @sid=IDENT_CURRENT('suppliers')
set @sql = 'declare c1 cursor for select company,name from company where company in  (' + @cos + ')'
exec(@sql)
open c1
fetch next from c1 into @company,@coname
while @@fetch_status=0
begin
	select @fy=min(fy) from finyear where company=@company
	set @msg = '2 - Error  in opening of GL Account for Suppliers Account - Company - ' + rtrim(ltrim(@coname))
	print @msg
	exec spglno  @company,@fy,@name,'C',@subgl,'13','3',@add1,@add2,@city,@phone,'', @glactno OUT 
	if @@error <> 0 goto e_error

	set @msg = '3 - Error  in Applying GL Account to Suppliers Account -  Company ' + ltrim(rtrim(@coname))
	print @msg
	insert into suppglac (supplier,fy,glactno) values (@sid,@fy,@glactno)
	if @@error <> 0 goto e_error

	fetch next from c1 into @company,@coname
end
close c1
deallocate c1

commit transaction
return
e_error:
rollback transaction
raiserror('Error in Supplier Account Opening',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[SPTBRF]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[SPTBRF] @ctrlno numeric,@refund money,@txndt datetime,@rmop int,@rglactno numeric,@glname varchar(50) = 'Nothing',@refundtype int=1,@refundctrlno numeric=0
/* with encryption */
AS
declare @company numeric
declare @fy numeric
declare @rgrno numeric
declare @rdate datetime
declare @partic varchar(100)
declare @actnum numeric
declare @usernm varchar(50)
if @refund=0 return

if @refundtype<>2
if exists (select * from sptbrs where ctrlno=@ctrlno and isnull(refundamount,0)+@refund > (isnull(advancerecd,0)-isnull(advanceadjd,0)) )
begin
print 'Difference in the availability of advance to refund.....The refund process can not be continued'
goto e_error
end

select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid='' and [host_id]=host_id()

select @rdate=bookingdate from sptbrs where ctrlno=@ctrlno
begin transaction
	set @company=1
	exec getfy  @txndt , @company , @fy out
	if @fy=0 goto e_error
-- added on 20 June 2013 Bala Got Baby on 14 June 2013. Yesterday discharged from hospital
if @refundtype<>1
	begin
	    declare @ofy numeric
	    declare @otxndt datetime
	    declare @out int 
		declare @oamt numeric(18,2)
		select @oamt=amount,@rgrno=grno,@otxndt=txndt from sptbrs_rn where ctrlno=@refundctrlno
	print convert(varchar(20),@otxndt)

		exec getfy @otxndt , @company , @ofy out
		if @ofy=0 goto e_error
	    
		exec delgrno @company,@ofy,@rgrno,@otxndt,@out out
		update sptbrs set refundamount=isnull(refundamount,0)-@oamt where ctrlno=@ctrlno
		delete from sptbrs_rn where ctrlno=@refundctrlno
		
		if exists (select * from sptbrs where ctrlno=@ctrlno and isnull(refundamount,0)+@refund > (isnull(advancerecd,0)-isnull(advanceadjd,0)) )
			begin
			print 'Difference in the availability of advance to refund.....The refund process can not be continued'
			goto e_error
			end

	end

exec getgrp @txndt, @company, @rgrno out
if @rgrno=0 goto e_error

	select @actnum =  case when @rmop=1 then 1 else @rglactno end
	set @partic= 'Adv.ag. Res. No. ' + ltrim(str(@ctrlno)) +' Dt.' + ltrim(rtrim(convert(varchar(20),@rdate,106)))  + '-Refund' --              'Refund-Reservation-Dt. ' + ltrim(rtrim(convert(varchar(20),@rdate,106))) + ' ' + rtrim(substring(@rname,1,30)) + ' Ref.No '+ltrim(str(@ctrlno))
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	values (@company, @fy,  56 ,@refund , 'To ' + @partic  ,  'D', @txndt , @rgrno ,'Y', @ctrlno)
	if @@error <> 0 goto e_error
	insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
	values (@company, @fy,  @actnum ,@refund ,'By ' +  @partic  , 'C', @txndt , @rgrno ,'Y', @ctrlno)
	if @@error <> 0 goto e_error
	update sptbrs set refundamount=isnull(refundamount,0)+@refund,refundmop=@rmop,refundglactno=@rglactno,refundgrno=@rgrno where ctrlno=@ctrlno
	if @@error<>0 goto e_error
	insert into sptbrs_rn  (mctrlno,type,amount,crnum,crname,crbank,usernm,glactno,mop,txndt,grno,ctrlno1) values
	(0,2,@refund,' ',@glname,' ',@usernm,@rglactno,@rmop,@txndt,@rgrno,@ctrlno)
	if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('SQL Error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[SPTBROOMSUpdate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SPTBROOMSUpdate]
	-- Add the parameters for the stored procedure here
	@myctrlno numeric(18,0),@type int  -- @type 0 for insert 1 for delete
/* with encryption */
AS
begin transaction
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET NOCOUNT ON;
declare @rno varchar(10)
declare @str varchar(5000)
declare @cnt int
declare @emsg varchar(200)
set @cnt=0
set @str = ''
declare rc1 cursor for select rno from sptbrooms where myctrlno=@myctrlno
open rc1
fetch next from rc1 into @rno
while @@fetch_status=0
begin
	if @cnt=1 
	set @str = @str + ',' + ltrim(rtrim(@rno))
	else
	set @str = ltrim(rtrim(@rno))
	set @cnt=1
	fetch next from rc1 into @rno
end
close rc1
deallocate rc1

if @str <> ''
if @type=0
	insert into acclog (narration) values ('Reservations-(MyCTrlno)Ref.No ' + str(@myctrlno) + ' Room(s) ' + @str + ' Blocked')
	else
	insert into acclog (narration) values ('Reservations-(MyCTrlno)Ref.No ' + str(@myctrlno) + ' Room(s) ' + @str + ' Block Released')
END
commit transaction
return
e_error:
rollback transaction
raiserror(@emsg,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sptbrsblock]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sptbrsblock] ( @ctrlno as numeric ) 
/* with encryption */
AS
declare @rt varchar(100)
declare @nor int 
declare @sql varchar(8000)
declare @doa varchar(20)
declare @dod varchar(20)
declare @rno numeric
declare @blocked  varchar(300)
declare @norb int
declare @first int
declare @myctrlno numeric
select @doa=doa,@dod=dod from sessions where ctrlno=@ctrlno

declare c1 cursor for select roomtype,noofrooms,ctrlno from inetreserve where inetrefno=@ctrlno
open c1
fetch next from c1 into @rt,@nor,@myctrlno

create table #dummyrno (rno numeric) 

while @@fetch_status=0
begin

set @blocked=''
set @norb = 0
set @first =0
declare c2 cursor for 
select rno from room join wings on wings.wingno=room.roomwing where description= @rt and rno not in (select rno from sptbrooms_v where sstatus <> 'C' and noofrooms-(arrivedrooms+cancelledrooms) > 0 and ((roomtype= @rt and datediff(s,'1/1/90',arrivaldate) between datediff(s,'1/1/90',@doa) and datediff(s,'1/1/90',@dod)-1 or datediff(s,'1/1/90',departuredate)-1 between datediff(s,'1/1/90',@doa) and datediff(s,'1/1/90',@dod)-1 
or (roomtype= @rt and datediff(s,'1/1/90',@doa) between datediff(s,'1/1/90',arrivaldate) and datediff(s,'1/1/90',departuredate)-1 ) )
or (roomtype= @rt and datediff(s,'1/1/90',@dod)-1 between datediff(s,'1/1/90',arrivaldate) and datediff(s,'1/1/90',departuredate)-1)) 
union select room.rno from room join guest on guest.gno=room.gno where roomtype=@rt and room.gno<>0 and datediff(s,'1/1/90',expecteddep) > datediff(s,'1/1/90',@doa)  
union select rno from #dummyrno) order by rno

open c2
fetch next from c2 into @rno
while @@fetch_status = 0
begin
set @norb=@norb+1
if @norb > @nor break 

if @first=0
set @blocked = @blocked + str(@rno) 
else
set @blocked = @blocked + ',' +  str(@rno) 

insert into #dummyrno (rno) values (@rno)

set @first=1
fetch next from c2 into @rno
end
close c2
deallocate c2

update inetreserve set blocked=@blocked where ctrlno=@myctrlno

fetch next from c1 into @rt,@nor,@myctrlno
end
close c1
deallocate c1

GO
/****** Object:  StoredProcedure [dbo].[sptbru]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptbru]  @extn char(8) ,@station char(2), @stime char(5) , @etime char(5),@duration numeric , @phonenum varchar(20),@direction char(1)='O'
/* with encryption */
AS
declare @gno numeric
declare @amount money
declare @pulse numeric
declare @g_callrate money
declare @callrate money
declare @g_pulserate money
declare @g_phonetax money
declare @g_hotelphonehandlingchgs money
declare @search varchar(7)
declare @x int
declare @isd char(1)
declare @city varchar(50)
declare @rate money
declare @country varchar(50)
declare @g_stdrate money
declare @g_isdrate money
declare @g_fixstd char(1)
declare @g_stdpulse numeric
declare @g_isdpulse numeric

declare @testextn varchar(20)  -- Introduced on 26 September 2018 Last month at 168 (4 days left)
set nocount on                 -- For Panasonic Integration 
if exists (select * from epbxbarred where phone=@phonenum) 
	begin
	select top 1 rno, city='Internal call',amount=0  from room
	return 
	end
--print 'periya'
select top 1 @testextn=extn from  room
if SUBSTRING(@testextn,1,2)='00'  set @extn = right('000000' + replace(convert(numeric,@extn),' ',''),6)
set @country=''
select @g_stdpulse=g_stdpulse,@g_isdpulse=g_isdpulse,@g_fixstd=g_fixstd,@g_stdrate=g_stdrate,@g_isdrate=g_isdrate,@g_callrate=g_callrate,@g_pulserate=g_pulserate,@g_phonetax=g_phonetax,@g_hotelphonehandlingchgs=g_hotelphonehandlingchgs from setting   --callrate from setting
set @callrate=0
set @gno=0
if @duration=0 set @duration=1
if exists(select * from room where extn=@extn) 
	begin
		select @gno=isnull(gno,0),@callrate=isnull(callrate,0) from room where extn=@extn
		if @callrate=0   set @callrate = @g_callrate
	end
else
	begin
	if not exists (select * from extension where extn=convert(numeric,@extn))
	insert into extension (extn,[name]) values (convert(numeric,@extn) ,'Other Extension')
	end
if substring(@phonenum,1,2)='00'  set @isd='Y' else set @isd= 'N'
if substring(@phonenum,1,1)='0'
begin
   if @g_fixstd='N'
	begin
		set @x=7
		while @x >= 1
		begin
			set @search = substring(@phonenum,1,@x)
			if exists(select top 1 * from stdisd3 where code like @search + '%')
				begin
				select top 1 @city=isnull(station,''),@rate=cast( isnull(rate1,'0') as money) from stdisd3 where code like @search+'%'
				if @rate <> 0 set @amount = ceiling(@duration/@rate) * @g_pulserate
				if @g_phonetax <> 0 set @amount = @amount  + (@amount* (@g_phonetax/100)  ) + @g_hotelphonehandlingchgs
				break
				end
			set @x=@x-1
		end
	end
   else
	begin
	--Print 'Periya'
	--for classic
	if @g_isdpulse <> 0 and @duration <> 0 
	if @isd='Y'  set @amount=ceiling(@duration/@g_isdpulse) *  @g_isdrate 
                 else set @amount=ceiling(@duration/@g_stdpulse) *  @g_stdrate
	end

end

else

begin
	set @city='Local'
	if @callrate = 0 set @callrate=@g_callrate
	if substring(@phonenum,1,1)='N'   --  earier this  was '2' for landline to landline
		set @amount = ceiling(@duration/120) * @callrate
	else
		set @amount = ceiling(@duration/60)*@callrate
end
if not exists(select * from comuser where [host_id]=host_id()) 
insert into comuser (computer,usernm,userno,control,[host_id]) values(host_name(),'SYS',1,'Y',host_id())


update comuser set control='Y' where [host_id]=host_id()
exec pbpe1u @extn,@extn,@phonenum,@amount,@station,@stime,@etime,@duration,@isd,@country,@city,@rate,@gno
update comuser set control='N' where [host_id]=host_id()
delete from comuser where usernm='SYS' and userno=1
set nocount off
select top 1 rno, city=isnull(@city,''),amount=@amount  from room
return
e_error:
rollback transaction
raiserror('Error',16,-1)
return







GO
/****** Object:  StoredProcedure [dbo].[sptcac]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptcac]  @actnum numeric,@actnum1 numeric ,@company int,@fy int
/* with encryption */
as
declare @txnno numeric
declare @partic varchar(100)
declare @tdno numeric
declare @amt money
declare @seller numeric
declare @billdate_closedate datetime
declare @taxtype int
declare @txndt datetime
begin transaction
declare c1 cursor for select txnno from vouchers where actnum=@actnum and company=@company and fy=@fy
open c1
fetch next from c1 into @txnno
while @@fetch_status=0
begin
select @txndt=txndt,@amt=amt,@actnum=actnum,@company=company,@fy=fy, @partic=partic,@seller=seller ,@billdate_closedate=billdate_closedate,@taxtype=taxtype from vouchers where txnno=@txnno
if @actnum <=104 and @actnum <= 110 
begin
	set @seller=isnull(@seller,@actnum)
	set @billdate_closedate=isnull(@billdate_closedate,@txndt)
	set @taxtype=isnull(@taxtype,0)
end
update vouchers set actnum=@actnum1,seller=@seller,billdate_closedate=@billdate_closedate,taxtype=@taxtype where txnno=@txnno
if @@error<>0 goto eerror


fetch next from c1 into @txnno
end
close c1
deallocate c1
commit transaction
return
eerror:
rollback transaction
raiserror('error in changing a/c number',16,-1)
close c1
deallocate c1




GO
/****** Object:  StoredProcedure [dbo].[sptcac_single]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptcac_single] @txnno numeric,@actnum1 numeric
/* with encryption */
as
declare @partic varchar(100)
declare @tdno numeric
declare @amt money
declare @actnum numeric
declare @company int
declare @fy int
declare @seller numeric
declare @billdate_closedate datetime
declare @taxtype int
declare @txndt datetime
declare @billno varchar(100)
declare @err varchar(2000)
declare @asset char(1)
declare @amttyp char(1)
begin transaction
set @err='error in changing a/c number'

select @amt=amt,@actnum=actnum,@company=company,@fy=fy, @partic=partic from vouchers where txnno=@txnno
select @billno=billno,@txndt=txndt,@amt=amt,@actnum=actnum,@company=company,@fy=fy, @partic=partic,@seller=seller ,@billdate_closedate=billdate_closedate,@taxtype=taxtype,@amttyp=amttyp from vouchers where txnno=@txnno

select @asset=asset from glprimld where company=@company and fy=@fy and cgp_actno=@actnum

if @actnum1 >=104 and @actnum1 <= 110 
begin

	set @seller=isnull(@seller,@actnum1)
	set @billdate_closedate=isnull(@billdate_closedate,@txndt)
	set @taxtype=isnull(@taxtype,0)
	set @billno=isnull(@billno,0)
end

update vouchers set actnum=@actnum1,seller=@seller,billdate_closedate=@billdate_closedate,taxtype=@taxtype,billno=@billno where txnno=@txnno
if @@error<>0 goto eerror

if (@asset='D' and @amttyp='D')  or (@asset='C' and @amttyp='C') 
update vouchers set totamt_balance=amt where txnno=@txnno
update glprimld set balance=dbo.getbalancefast(@actnum1,@txndt,@fy) where cgp_Actno=@actnum1 and fy=@FY
update glprimld set balance=dbo.getbalancefast(@actnum,@txndt,@fy) where cgp_Actno=@actnum and fy=@FY

commit transaction
return
eerror:
rollback transaction
raiserror(@err,16,-1)
--close c1
--deallocate c1




GO
/****** Object:  StoredProcedure [dbo].[sptt1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sptt1    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sptt1] @outlet int ,
@txndt datetime,
@name varchar(50),
@add1 varchar(50),
@add2 varchar(50),
@add3 varchar(50),
@city varchar(50),
@state varchar(50),
@driver varchar(50),
@destination varchar(100),
@sreading int ,
@vtype varchar(20),
@vmake varchar(20),
@vnumber varchar(20),
@notravelagent int,
@travelagent int,
@basicamt money,
@perkm money,
@perhour money,
@bata money,
@basicamt_t money,
@perkm_t money,
@perhour_t money,
@bata_t money,
@maxkm int,
@maxhours int,
@maxkm_t int,
@maxhours_t int,
@tttype int,
@chlno int,
@ogno int,
@minkm int,
@minkm_t int,
@ino numeric=0,
@phoneno varchar(200)='',
@driverno varchar(50)=''

/* with encryption */
AS
declare @autochln int
declare @company int
declare @year int
declare @bofy datetime
declare @fy int
declare @err varchar(500)
set @err = 'chalan updation failed'
if @outlet=0 
begin 
print 'The Outlet is not selected'
goto e_error
end
begin transaction
if @ogno<>0
  if exists (select * from room where gno=@ogno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
select @company=company from toutlet where outlet=@outlet
exec getfy @txndt, @company, @fy out
select @autochln=autono1 from toutlet where outlet=@outlet
set @err = 'Err-1: Chalan updation failed'
if @autochln=0
   begin   
if not exists(select * from toutlet1 where outlet=@outlet and fy=@fy and ctype=1) insert into toutlet1  (outlet,ctype,fy,chlno) values (@outlet,1,@fy,0) 
update toutlet1 set chlno=isnull(chlno,0)+1 where outlet=@outlet and fy=@fy and ctype=1
select @chlno=chlno from toutlet1 where outlet=@outlet and fy=@fy and ctype=1
--   print 'solkkettan'
   end
set @err = 'Err-2: Chalan updation failed'
insert chalan (chlno,chldate,stype,[name],add1,add2,add3,city,state,phoneno,driver,destination,sreading,v_type,v_make,v_number,travelagent,basicamt,
perkm,perhour,bata,tttype,basicamt_t,perkm_t,perhour_t,bata_t,notravelagent,maxkm,maxhours,maxkm_t,maxhours_t,outlet,ogno,minkm,minkm_t,ino,driverno) values
(@chlno,@txndt,1,@name,@add1,@add2,@add3,@city,@state,@phoneno,@driver,@destination,@sreading,@vtype,@vmake,@vnumber,@travelagent,@basicamt,
@perkm,@perhour,@bata,@tttype,@basicamt_t,@perkm_t,@perhour_t,@bata_t,@notravelagent,@maxkm,@maxhours,@maxkm_t,@maxhours_t,@outlet,@ogno,@minkm,@minkm_t,@ino,@driverno)
if @@error <> 0 goto e_error
commit transaction
return @@identity
--print 'periya karuppa'
e_error:
rollback transaction
raiserror(@err,16,-1)
return 0




GO
/****** Object:  StoredProcedure [dbo].[sptt11]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/*May 2004*/
CREATE PROCEDURE [dbo].[sptt11]  @chctrlno numeric,@purchasetype int, @ta numeric
/* with encryption */
AS
declare @company numeric
declare @fy numeric
declare @partic varchar(50)
declare @olname varchar(50)
declare @glactno numeric
declare @purchase numeric
declare @grno numeric
declare @finalamt_t money
declare @txndt datetime
declare @chlno numeric
declare @outlet numeric
declare @ret_val numeric
declare @finalbata_t money 
declare @amt money
declare @advance_t money
declare @advpaid money
begin transaction
select @txndt=isnull(finaldate,getdate()) , @chlno=chlno,@outlet=outlet,@grno=grno_t, @finalamt_t = finalamt_t,@finalbata_t=finalbata_t,@advance_t=advance_t from chalan where chctrlno=@chctrlno
select @purchase=purchase1,@company=company,@olname=[name],@advpaid=advancepaid  from toutlet where outlet=@outlet
exec getfy @txndt, @company,   @fy out
exec delgrno @company,@fy,@grno,@txndt ,@ret_val out
if @purchasetype=0 set @ta=0
update chalan set notravelagent=@purchasetype,travelagent=@ta, @advance_t=advance_t  where chctrlno=@chctrlno
set @partic = 'Chln.No ' + ltrim(str(@chlno))+ ' ' + @olname

if @purchasetype = 1 
BEGIN
exec sptt1vpt @chctrlno
end
commit transaction
return
e_error:
rollback transaction
raiserror('error',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sptt1cl]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sptt1cl    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sptt1cl]  @ctrlno int,@ereading int, @basicamt money,@tax money,@bata money,@discount int ,@basicamt_t money,
@bata_t money,@mop int, @crname varchar(50),@crnum varchar(20),@crbank varchar(20),@glactno int,@txndt datetime,@roomcomp char(1),
@otherchgs money,@cess money
/* with encryption */
as
declare @company int
declare @fy int
declare @outlet int
declare @purchase int
declare @sales int
declare @advrecd int
declare @advpaid int
declare @advance money
declare @advance_t money
declare @staxrate money
declare @selfdeal char(1)
declare @hno_mybook int
declare @hno_htlbook int
declare @htl_purchase int
declare @htl_sales int
declare @htl_receivable int
declare @chlno int
declare @tttype int
declare @grno int
declare @grno_t int
declare @myreceivable int
declare @dactno int
declare @amt money
declare @olname varchar(50)
declare @partic varchar(100)
declare @nota int
declare @tacode int
declare @ta int   -- travel agent gl actnum
declare @gno int
declare @finalamt money
declare @finalamt_t money
declare @split char(1)
declare @grno_co1 numeric
declare @otherchgsac numeric
declare @bataac numeric
declare @status char(1)
declare @ino numeric
declare @gstin nvarchar(15)
begin transaction
--set @otherchgs=isnull(@otherchgs,0)
set @split = 'N'
if @roomcomp='Y' set @split = 'C'

select @ino=isnull(ino,0) from chalan where chctrlno=@ctrlno

select @nota=notravelagent,@ta=glactno,@olname=olname,@myreceivable=my_receivable,@company=company,@chlno=chlno,
@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,@htl_purchase=htl_purchase,@htl_sales=htl_sales,
@htl_receivable=htl_receivable,@purchase=purchase1,@sales=sales1,@advrecd=advancerecd,@advpaid=advancepaid,@staxrate=staxrate,
@advance=advance,@advance_t=advance_t,
@tttype=tttype ,@tacode=travelagent,
@otherchgsac=otherchgsac,@bataac=bataac ,  @status=status
from tt_chalan_ol where chctrlno=@ctrlno
if @status <> '' begin print 'This chalan  is closed....'   goto e_error end
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
EXEC  GETGRP @TXNDT,@COMPANY,@GRNO OUT
IF @GRNO=0 GOTO E_ERROR
EXEC  GETGRP @TXNDT,@COMPANY,@GRNO_T OUT
IF @GRNO_T=0 GOTO E_ERROR
set @gno=0
if @mop=2 set @gno=@glactno
if @mop=2 
if exists (select * from room where gno=@gno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
set @finalamt = @basicamt+@tax+@bata+@otherchgs-@discount-@advance+@cess 
set @finalamt_t = @basicamt_t+@bata_t-@advance_t
/*
 now  the @ta  is the gl actno of the travel agent .. But it is the actnum on the date of opeing of that travel agent 
To get the current account number in our glprimld we introduced the new function findactnum - 
Date : 20 March 2008
*/
-- Getting New Actnum
declare @taactnum numeric
declare @tafy numeric
select @taactnum=glactno,@tafy=fy from travelagent where code=@tacode
set @ta= dbo.findactnum(@taactnum,@tafy,@txndt)
----- getting actnum over

if @mop=2 select @gstin=gstin from guest where gno=@gno


update chalan set status='C',finaldate=@txndt,finalBASIC=@basicamt,finaltax=@tax,finalbata=@bata,discount=@discount,
finalBASIC_t=@basicamt_t,finalbata_t=@bata_t,mop=@mop,cc_name=@crname,cc_num=@crnum,cc_bank=@crbank,
ereading=@ereading,glactnum=@glactno,gno=@gno,grno=@grno,grno_t=@grno_t,
finalamt=@finalamt,finalamt_t=@finalamt_t,split=@split,stax=@staxrate,otherchgs=@otherchgs,cess=@cess
where chctrlno=@ctrlno
if @@error <> 0 goto e_error

if exists(select * from setting where g_finaccount=0) goto final

if @ino=0 exec sptt1vpc @ctrlno
exec sptt1vpt @ctrlno
final:
commit transaction
return
E_ERROR:
rollback transaction
raiserror('problem in chalan closing',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sptt1crv]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sptt1crv    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sptt1crv] @ctrlno int,@userno int  
/* with encryption */
AS
declare @company int
declare @txndt datetime
declare @fy int
declare @grno int
declare @grno_t int
declare @grno_co1 int
declare @retval int
declare @gno int
declare @chlno numeric
declare @amt money
declare @outlet int
declare @finalamt1 money
declare @finalamt2 money 
declare @m_txndt datetime
declare @transact int
declare @m_split char(1)
declare @sql varchar(5000)
declare @lux char(1)
begin transaction
set @transact=0
select @lux=lux, @m_split=split,@gno=gno,@company=company,@txndt=finaldate,@grno=grno,@grno_t=grno_t,@grno_co1=grno_co1 from tt_chalan_ol where chctrlno=@ctrlno
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
/*
if @gno<>0
  if not exists (select * from room where gno=@gno)
     begin
     print 'Guest Checked out....Chalan Revival not Possible'
     goto E_error
     end
*/
if @gno<>0
  if exists (select * from room where gno=@gno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
if @gno<>0 if not exists (select * from room where gno=@gno) begin
  if not exists(select * from servicepermission where userno=@userno and serviceno=185 and outlettype=4 and permission='Y') begin
    print 'You are not permitted to Revive Chalans after check-out'
    goto e_error
  end
  set @transact=1
end
select @m_txndt=finaldate,@outlet=outlet,@chlno=chlno,@amt=amt from chalan where chctrlno=@ctrlno
update chalan set finaldate=null,cc_name='',cc_num='',cc_bank='',grno=0,grno_t=0,finalbasic=0,finalbasic_t=0,finalbata=0,finalbata_t=0,
otherchgs=0,finalamt=0,finalamt_t=0,cess=0,
discount=0,glactnum=0,gno=0,mop=0,ereading=0,status=' ',finaltax=0,amt=0 where chctrlno=@ctrlno
if @@error <> 0 goto e_error
exec delgrno @company,@fy,@grno,@txndt,@retval out
if @@error <> 0 goto e_error
exec delgrno @company,@fy,@grno_t,@txndt,@retval out
if @@error <> 0 goto e_error
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
end
declare @m_olname varchar(30)
declare @usernm varchar(30)
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

select @m_olname=[name] from toutlet where outlet=@outlet
insert into acclog (txndt,usernm,narration,txntype) values (getdate(),@usernm, ltrim(rtrim(@M_olname))+ ' TT Chalan  No ' + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@AMT)) +  ' Revived',11)
if @@error <> 0 goto e_error
if @transact=1
   begin
   update chalan set ogno=0 where chctrlno=@ctrlno
   set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@amt) + ',0,' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,0,''X'',' + str(@userno) + ',''' + @lux + ''''
   exec (@sql)
   if @@error <> 0  goto e_error
   end
insert into acclog(txndt,usernm,narration,txntype) values (getdate(),'',
'TT chalan No.' + str(@chlno) +  ' Closed on ' + convert(varchar(20) ,@txndt) +    ' Revived after Closure',
20)
if @@error<>0 goto e_error
commit transaction
return
e_error:
raiserror('deletion of chalan failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[SPTT1VPC]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<PowerBrain Software (India)>
-- Create date: <12 June 2012>
-- Description:	<TT 1 Voucher Posting>
-- =============================================
CREATE PROCEDURE [dbo].[SPTT1VPC] 
	@ctrlno numeric
/* with encryption */
AS
BEGIN
	if exists(select * from setting where g_finaccount=0) return

	SET NOCOUNT ON;
declare @txndt datetime
declare @basciamt money
declare @tax money
declare @bata money
declare @discount money
declare @finalamt money
declare @staxrate money
declare @otherchgs money
declare @mop int 
declare @crname varchar(50)
declare @crnum  varchar(50)
declare @crbank varchar(50)
declare @glactno numeric(18,0)
declare @gno numeric(18,0)
declare @split char(1)
declare @chlno numeric
declare @company numeric
declare @fy numeric
declare @partic varchar(200)
declare @advance money
declare @basicamt money
declare @amt money
declare @myreceivable numeric
declare @hno_mybook numeric
declare @hno_htlbook numeric
declare @htl_sales numeric
declare @htl_receivable numeric
declare @tttype int
declare @tacode numeric
declare @status char(1)
declare @dactno numeric
declare @olname varchar(50)
declare @advrecd money
declare @grno numeric
declare @selfdeal char(1)
declare @sales money
declare @bataac numeric
declare @otherchgsac numeric
declare @err varchar(2000)
declare @nota int
declare @ta numeric
declare @grno_co1 numeric
declare @outlet numeric
declare @lux char(1)
declare @sql varchar(6000)
declare @bamt money
declare @cess money
declare @cessac numeric
declare @sttableno int
declare @taxacno numeric
set @taxacno=61
set @err = 'Voucher Posting'

update chalan set finalamt=(isnull(finalbasic,0)+isnull(finaltax  ,0)+isnull(finalBATA ,0)+isnull(otherchgs,0)- isnull(discount,0)-isnull(advance,0)+isnull(cess,0)) where chctrlno=@ctrlno

select @nota=notravelagent,@olname=olname,@myreceivable=my_receivable,
@company=company,@chlno=chlno,
@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,
@htl_sales=htl_sales,
@htl_receivable=htl_receivable,@sales=sales1,@advrecd=advancerecd,
@staxrate=staxrate,
@advance=advance,
@tttype=tttype ,@tacode=travelagent,
@otherchgsac=otherchgsac,@bataac=bataac ,  @status=status,
@outlet=outlet,@lux=lux
from tt_chalan_ol where chctrlno=@ctrlno

select @cessac=88

select @chlno=chlno,@txndt=finaldate,@basicamt=finalbasic,@tax=finaltax,
@bata=finalbata,@discount=discount,
@mop=mop,@crname=cc_name,@crnum=cc_num,@crbank=cc_bank,@glactno=glactnum,
@gno=gno,@grno=grno,@grno_co1=grno_co1,
@finalamt=finalamt,@split=split,@staxrate=stax,@otherchgs=otherchgs ,@cess=cess
from chalan where chctrlno=@ctrlno

select @sttableno=sttableno  from ta_servicetaxsetting where outlet=@outlet  and servicetype=0

select @taxacno=taxacno from sttabler where sttableno=@sttableno and datediff(d,'1/1/2000',@txndt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
and @basicamt between samt and eamt

exec getfy @txndt,@company,@fy out
if @fy=0 goto e_error

if isnull(@grno,0)=0
begin
exec getgrp @txndt,@company,@grno OUT
if @grno=0 goto e_error
end

--update chalan set status='C',finaldate=@txndt,finalBASIC=@basicamt,finaltax=@tax,
--finalbata=@bata,discount=@discount,
--finalBASIC_t=@basicamt_t,finalbata_t=@bata_t,mop=@mop,cc_name=@crname,cc_num=
--@crnum,cc_bank=@crbank,
--ereading=@ereading,glactnum=@glactno,gno=@gno,grno=@grno,grno_t=@grno_t,
--finalamt=@finalamt,finalamt_t=@finalamt_t,split=@split,stax=@staxrate,
--otherchgs=@otherchgs
--where chctrlno=@ctrlno

if @@error <> 0 goto e_error

if @mop=6 return

if @mop=2 and @split='C'
if @advance<>0 
begin 
set @err = 'This entry is paid with Advance..Complimentary is not possible..Please Delete the Advance entry or Charge this chalan'
goto e_error
end

SELECT @DACTNO = 
                              CASE 
                                          WHEN @MOP = 1 THEN 1           --- CASH SALES
                                          WHEN @MOP = 2 THEN               -- ROOM SERVICE
             case when @split<>'C' then
				   CASE WHEN @COMPANY = 1 THEN @MYRECEIVABLE 
					ELSE  
								  CASE
								  WHEN @SELFDEAL='Y' THEN  @myRECEIVABLE   ELSE @HNO_MYBOOK
								  END
					 END
				   else 26 end

                                          WHEN @MOP = 3 THEN 23  -- BILL RECEIVABLE
                                          WHEN @MOP = 4 THEN @glactno -- GL A/C
                                          WHEN @MOP = 5 THEN 22   -- CREDIT CARD 
                                          WHEN @MOP = 6 THEN 0   -- COMPLEMENTARTY No vouching 
                               END
set @partic = 'Chln.No ' + ltrim(str(@chlno))+ ' ' + @olname
if @mop=5
set @partic = @crnum + ' ' + @crname + ' ' + @partic

begin transaction
if (@basicamt+@tax+@bata+@otherchgs-@discount ) > @advance
	begin
	set @amt = @basicamt+@tax+@cess+@bata+@otherchgs-@advance-@discount 
	update chalan set amt=@amt where chctrlno=@ctrlno
	if @@error <> 0 goto e_error
	if  @amt <> 0
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,@dactno,@amt , 'To '+@partic ,'D',@txndt,@grno,@chlno,'Y','TT')
		if @@error <> 0 goto e_error
		end
	end
else
	begin
	set @amt = @advance - (@basicamt+@tax+@cess+@otherchgs+@bata-@discount )
	if  @amt <> 0
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,@dactno,@amt , 'By '+@partic ,'C',@txndt,@grno,@chlno,'Y','TT')
		if @@error <> 0 goto e_error
		end
	end

	set @bamt = @amt


if @advance  > 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,@advrecd,@advance , 'To '+@partic  ,'D',@txndt,@grno,@chlno,'Y','TT')
	if @@error <> 0 goto e_error
	end

if @discount  > 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy,62,@discount , 'To ' + @partic  ,'D',@txndt,@grno,@chlno,'Y','TT')
	if @@error <> 0 goto e_error
	end

if  @tax  > 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy, @taxacno  ,@tax , 'By '+@partic   ,'C',@txndt,@grno,@chlno,'Y','TT')
	if @@error <> 0 goto e_error
	end

if  @cess  > 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy, @cessac ,@cess , 'By '+@partic   ,'C',@txndt,@grno,@chlno,'Y','TT')
	if @@error <> 0 goto e_error
	end

/*
if @basicamt+@bata+@otherchgs  > 0
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode)
	values (@company,@fy, @sales  ,@basicamt+@bata+@otherchgs , 'By '+@partic   ,'C',@txndt,@grno,@chlno,'Y')
	if @@error <> 0 goto e_error
	end
*/
if @basicamt > 0 
	begin
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy, @sales  ,@basicamt, 'By '+@partic,'C',@txndt,@grno,@chlno,'Y','TT')
	if @@error <> 0 goto e_error
	end

if @bata> 0 
	begin
if @bataac = 0 begin print 'The Bata/Other Charges Accounts are not set as per the new concept.. Please call PowerBrain Software (India)' goto e_error end
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy, @bataac  ,@bata, 'By '+@partic,'C',@txndt,@grno,@chlno,'Y','TT')
	if @@error <> 0 goto e_error
	end

if @otherchgs > 0 
	begin
	if @otherchgs = 0 begin print 'The Bata/Other Charges Accounts are not set as per the new concept.. Please call PowerBrain Software (India)' goto e_error end
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @otherchgsac  ,@otherchgs, 'By '+@partic,'C',@txndt,@grno,@chlno,'Y','TT')
		if @@error <> 0 goto e_error
		end

END
if @mop=2 and @selfdeal='N'  and @split <> 'C'
begin
	set @amt = @basicamt+@tax+@bata+@otherchgs-@advance-@discount 
	if @amt < 0 goto final
	set @company = 1 
	exec  getfy @txndt,@company,@fy out

	if isnull(@grno_co1,0)=0 
		exec getgrp @txndt,1,@grno_co1 out
	if @grno_co1=0 goto e_error
	set @grno=@grno_co1
	update chalan set grno_co1=@grno where chctrlno=@ctrlno
	if @fy=0 goto e_error
	
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (1,@fy, @hno_htlbook ,@amt , 'By '+@partic   ,'C',@txndt,@grno,@chlno,'Y','TT')
	if @@error <> 0 goto e_error
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (1,@fy, @htl_receivable ,@amt , 'To '+@partic   ,'D',@txndt,@grno,@chlno,'Y','TT')
	if @@error <> 0 goto e_error
end

final:

commit transaction
return
e_error:
rollback transaction
set @err = 'Error in TT 1 VP: ' +@err
raiserror(@err,16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[SPTT1VPT]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SPTT1VPT] @ctrlno numeric
/* with encryption */
AS
BEGIN

declare @amt money
declare @nota int
declare @company numeric
declare @advance_t money
declare @basicamt_t money
declare @bata_t money
declare @fy numeric
declare @grno_t numeric
declare @purchase numeric
declare @advpaid money
declare @ta numeric
declare @partic varchar(200)
declare @txndt datetime
declare @chlno numeric
declare @err varchar(2000)
declare @olname varchar(50)
declare @tacode numeric
declare @status char(1)
declare @retval int
declare @finalamt_t money
begin transaction
set @err = ''
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
select @nota=notravelagent,@olname=olname,@company=company,@chlno=chlno,
@tacode=travelagent,@purchase=purchase1,
@status=status,@company=company,
@advpaid=advancepaid
from tt_chalan_ol where chctrlno=@ctrlno

select @chlno=chlno,@txndt=finaldate,@basicamt_t=finalbasic_t,@bata_t=finalbata_t,
@advance_t=advance_t,@finalamt_t=finalamt_t,@grno_t=grno_t
from chalan where chctrlno=@ctrlno

exec getfy @txndt,@company,@fy out

if @grno_t<>0 
begin
exec delgrno @company,@fy,@grno_t,@txndt,@retval out
if @@error <> 0 goto e_error
end

if isnull(@grno_t,0)=0 exec getgrp @txndt,@company,@grno_t out


-- Getting New Actnum
declare @taactnum numeric
declare @tafy numeric
select @taactnum=glactno,@tafy=fy from travelagent where code=@tacode
set @ta= dbo.findactnum(@taactnum,@tafy,@txndt)
----- getting actnum over


set @partic = 'Chln.No ' + ltrim(str(@chlno))+ ' ' + @olname
set @finalamt_t = @basicamt_t+@bata_t-@advance_t
if @nota=1
begin
    update chalan set grno_t=@grno_t where chctrlno=@ctrlno
	set @amt = @basicamt_t + @bata_t 
	insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
	values (@company,@fy, @purchase,@amt , 'To '+@partic   ,'D',@txndt,@grno_t,@chlno,'Y','TT')
	if @@error <> 0 goto e_error

	if @advance_t  > 0
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @advpaid ,@advance_t , 'By '+@partic   ,'C',@txndt,@grno_t,@chlno, 'Y','TT')
		if @@error <> 0 goto e_error
		end
	
	if @amt-@advance_t > 0
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @ta ,@amt-@advance_t , 'By '+@partic   ,'C',@txndt,@grno_t,@chlno,'Y','TT')
		if @@error <> 0 goto e_error
		end
	else
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @ta ,@advance_t-@amt , 'To '+@partic   ,'D',@txndt,@grno_t,@chlno,'Y','TT')
		if @@error <> 0 goto e_error
		end
end

commit transaction
return

e_error:
rollback transaction
set @err = 'Error in TT-TA Voucher Posting -' + @err
raiserror(@err,16,-1)

END




GO
/****** Object:  StoredProcedure [dbo].[sptt2]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sptt2    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sptt2]  @outlet int ,
@txndt datetime,
@name varchar(50),
@add1 varchar(50),
@add2 varchar(50),
@add3 varchar(50),
@city varchar(50),
@state varchar(50),
@details varchar(100),
@purchasetype int,
@travelagent int,
@glactno int,
@finalamt money,
@finalamt_t money,
@tax money,
@discount money,
@serchgs money,
@tttype int,
@chlno int,
@ogno int,
@nota int,
@maxkm int,
@ino numeric=0,
@cess money=0 ,
@phoneno varchar(200)=''
/* with encryption */
AS
-- here maxkm is for identifying the tab of the customer selection tabstrip
declare @autochln int
declare @company int
declare @year int
declare @bofy datetime
declare @purchase2 int
declare @olname varchar(50)
declare @partic varchar(100)
declare @grno int
declare @fy int
declare @staxrate money
declare @id numeric

begin transaction

if @outlet=0 
begin 
print 'The Outlet is not selected'
goto e_error
end

if @ogno<>0 
  if exists (select * from room where gno=@ogno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
IF @TXNDT < '1 APR 1950' SELECT @TXNDT = GETDATE()
--IF DATEPART(mm,@TXNDT) >=  1 AND DATEPART(mm,@TXNDT) <= 3
--     SET @YEAR = DATEPART(yy,@TXNDT)-1
--ELSE
--     SET @YEAR = DATEPART(yy,@TXNDT)
--SELECT @BOFY = '1 APR ' + STR(@YEAR)
set @bofy = dbo.getbofy(@txndt)
set @year = datepart(yy,@bofy)

select @staxrate=tkttaxrate from tktmaster where tktctrlno=@tttype
declare @taname varchar(100)
set @taname='No Travel Agent'
if @purchasetype = 1
begin
set @glactno=0
declare @glac numeric
declare @tafy numeric
    select @taname=[name], @glac=glactno,@tafy=fy from travelagent where code=@travelagent
    set @glactno=dbo.findactnum(@glac , @tafy,@txndt)
-- new function for finindg current ac num of travel agent - created on 20 mar 2008
end
select @autochln=autono2, @purchase2=purchase2,@company=company,@olname=[name] from toutlet where outlet=@outlet


--print 'periya' + str(@company) + convert(varchar(20),@txndt) goto e_error

EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR



if @autochln=0
   begin   
	if not exists(select * from toutlet1 where outlet=@outlet and fy=@fy and ctype=2) insert into toutlet1  (outlet,ctype,fy) values (@outlet,2,@fy)
	update toutlet1 set chlno=chlno+1 where outlet=@outlet and fy=@fy and ctype=2
	select @chlno=chlno from toutlet1 where outlet=@outlet and fy=@fy and ctype=2
--   print 'solkkettan'
   end
--***** sreading - used for purchase type 0 - cash 1 - travel agent 2 - for gl a/c
---       ereading - used for glactno for gl purchase
----    finalbata -  service charges
--print @glactno 
--goto e_error

insert chalan (chlno,chldate,stype,[name],add1,add2,add3,city,state,phoneno,destination,travelagent,finalamt,
tttype,finalamt_t, sreading,outlet,discount,finaltax,finalbata,ereading,ogno,stax,finalbasic,finalbasic_t,maxkm,notravelagent,ino,cess) values
(@chlno,@txndt,2,@name,@add1,@add2,@add3,@city,@state,@phoneno,@details,@travelagent,@finalamt+@tax+@serchgs-@discount+@cess,
@tttype,@finalamt_t,@purchasetype,@outlet,@discount,@tax,@serchgs,@glactno,@ogno,@staxrate,@finalamt,@finalamt_t,@maxkm,@purchasetype,@ino,@cess )
if @@error <> 0 goto e_error
set @id = @@identity

EXEC SPTT2VPT @ID

commit transaction
return @id
--print 'periya karuppa'
e_error:
rollback transaction
raiserror('chalan updation failed',16,-1)
return 0



GO
/****** Object:  StoredProcedure [dbo].[sptt21]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
CREATE PROCEDURE [dbo].[sptt21] @chctrlno numeric,@purchasetype int, @ta numeric
/* with encryption */
AS
declare @company numeric
declare @fy numeric
declare @partic varchar(50)
declare @olname varchar(50)
declare @glactno numeric
declare @purchase2 numeric
declare @grno numeric
declare @finalamt_t money
declare @txndt datetime
declare @chlno numeric
declare @outlet numeric
declare @ret_val numeric
declare @ota numeric
declare @onota numeric
declare @otname varchar(50)
declare @newtname varchar(50)
begin transaction
select @onota=notravelagent,@ota=travelagent,@txndt=chldate ,@chlno=chlno,@outlet=outlet,
@grno=grno_t, @finalamt_t = finalamt_t from chalan where chctrlno=@chctrlno
select @purchase2=purchase2,@company=company,@olname=[name] 
from toutlet where outlet=@outlet
exec getfy @txndt, @company,   @fy out
-----print str(@grno) + ' ' + str(@company) +' ' +  convert(varchar(20),@txndt) goto e_error
---exec delgrno @company,@fy,@grno,@txndt ,@ret_val out
if @ret_val=1 goto e_error
if @purchasetype = 0 
	select @glactno = 1
else 
	begin
		--select @glactno=glactno from travelagent where code=@ta
		declare @glac numeric
		declare @tafy numeric
		select @glac=glactno,@tafy=fy from travelagent where code=@ta
		set @glactno=dbo.findactnum(@glac , @tafy,@txndt)
		--New Function for finding out th A/C Num of travel agent - created on 20 mar 2008
	end

select @otname=name from travelagent where code=@ota
select @newtname=name from travelagent where code=@ta
set @partic = ' TKT Chln.No.' + ltrim(str(@chlno)) + ' ' + rtrim(@olname) + ' T.A.' + rtrim(@newtname)

update chalan set notravelagent=@purchasetype, travelagent=case when @purchasetype=0 then 0 else @ta end where chctrlno=@chctrlno
/*
INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
VALUES (@COMPANY,@FY,@purchase2,@finalamt_t , 'To '+@partic ,'D',@TXNDT,@GRNO,@chlno,'Y','TKT')
IF @@ERROR <> 0 GOTO E_ERROR

INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
VALUES (@COMPANY,@FY,@glactno,@finalamt_t , 'By '+@partic ,'C',@TXNDT,@GRNO,@chlno,'Y','TKT')
IF @@ERROR <> 0 GOTO E_ERROR
*/

EXEC SPTT2VPT @chctrlno

commit transaction
return
e_error:
rollback transaction
raiserror('eeorr',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sptt2can]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Ramanathan, PowerBrain Software (India) 
-- Create date: 13 July 2015
-- Description:	Cancellation
-- =============================================
CREATE PROCEDURE [dbo].[sptt2can] 
@chctrlno numeric,@txndt datetime,
@mopp tinyint, @crnamep varchar(50),@crnump varchar(60),@crbankp varchar(50),
@mops tinyint, @crnames varchar(50),@crnums varchar(60),@crbanks varchar(50),
@user varchar(20),@tttype tinyint
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @status char(1)
	declare @cancelled char(1)
	declare @err varchar(500) 
	declare @outlet int
	--declare @salesreturn2 numeric
	--declare @salesreturn numeric
	--declare @servicetaxreturnac numeric
if @tttype=2 
	begin
	select @outlet=outlet,@status=status,@cancelled=cancelled from chalan where chctrlno=@chctrlno
	--select @salesreturn2=salesreturn2,@salesreturn=salesreturn2,@servicetaxreturnac=servicetaxreturnac 	from toutlet where outlet=@outlet
	end
else
	begin
	select @outlet=outlet,@status=status,@cancelled=cancelled from ta_chalan where ctrlno=@chctrlno
	end

set @err= ''
begin transaction
	if @cancelled='Y' begin  set @err = 'This is already cancelled Entry...Process aborted'  goto e_error end
	if @tttype=2 
	begin
    update chalan set cancelled='Y' where chctrlno=@chctrlno
	if @@error<>0 goto e_error
	end
	else
	begin
    update ta_chalan set cancelled='Y' where ctrlno=@chctrlno
	if @@error<>0 goto e_error
	end

insert into ta_cancel 
	(cancellationpurchasereturnmop,cancellationpurchasereturncrname,cancellationpurchasereturncrnum,cancellationpurchasereturncrbank,
	cancellationsalesreturnmop,cancellationsalesreturncrname,cancellationsalesreturncrnum,cancellationsalesreturncrbank,
	cancelledon,cancelledby,tttype,chctrlno)
	values 
    (@mopp,@crnamep,@crnump,@crbankp, @mops,@crnames,@crnums,@crbanks,@txndt,@user,@tttype,@chctrlno)

 	exec sptt2canpv @chctrlno , @tttype
	if @@error<>0 goto e_error


if @tttype=2 
	begin
		if @status='C' 
			begin
				exec sptt2canrv @chctrlno, @tttype
				if @@error<>0 goto e_error
			end
	end
	else
	begin
		exec sptt2canrv @chctrlno,@tttype
		if @@error<>0 goto e_error
	end




commit transaction
return
e_error:
rollback transaction
set @err = 'Error on Cancellation - ' + @err
raiserror(@err,16,-1) return

END

GO
/****** Object:  StoredProcedure [dbo].[sptt2canchgsp]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sptt2canchgsp]
@chctrlno numeric,@txndt datetime,
@amtpaid money,@mopp tinyint, @crnamep varchar(50),@crnump varchar(60),@crbankp varchar(50),
@user varchar(20),@tttype tinyint
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @status char(1)
	declare @cancelled char(1)
	declare @err varchar(500) 
	declare @outlet int
	declare @salesreturn2 numeric
	declare @purchasereturn numeric
	declare @servicetaxreturnac numeric
	set @err= 'Error : ' 
begin transaction
if @tttype=2
	select @outlet=outlet,@status=status,@cancelled=cancelled from chalan where chctrlno=@chctrlno
else
	select @outlet=outlet,@status=status,@cancelled=cancelled from ta_chalan where ctrlno=@chctrlno
	---the following line is not necessory
	select @salesreturn2=salesreturn2,@purchasereturn=purchasereturn2,@servicetaxreturnac=servicetaxreturnac 
	from toutlet where outlet=@outlet
	set @err='Error in Updating'
	if @cancelled <> 'Y' begin set @err = 'This is not a cancelled Entry...Process aborted'  goto e_error end
	set @err='Error in Updating1'

    update ta_cancel set cancellationchgspaidamount=@amtpaid,
	cancellationchgspaidmop=@mopp,cancellationchgspaidcrname=@crnamep,cancellationchgspaidcrnum=@crnump,cancellationchgspaidcrbank=@crbankp,
	cancellationchgspaiddate=@txndt,cancellationchgspaidby=@user,cancellationchgspaid='Y'
	where chctrlno=@chctrlno and tttype=@tttype
	if @@error<>0 goto e_error

	set @err='Error in Vouching'
	exec sptt2canchgspv @chctrlno,@tttype
	if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
set @err = 'Error on Cancellation Charges - ' + @err
raiserror(@err,16,-1)

END

GO
/****** Object:  StoredProcedure [dbo].[sptt2canchgspv]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sptt2canchgspv]
@chctrlno numeric,@tttype tinyint
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @status char(1)
	declare @err varchar(500) 
	declare @outlet int
	declare @salesreturn2 numeric
	declare @purchasereturn numeric
	declare @servicetaxreturnac numeric
	declare @cancellationchgspaidac numeric
	declare @company int
	declare @fy numeric
	declare @txndt datetime

	declare @mopr int
	declare @ccramt money
	declare @ccrcrname varchar(50)
	declare @ccrcrnum varchar(50)
	declare @ccrcrbank varchar(50)
	declare @ccrgrno numeric

	declare @mopp int
	declare @ccpamt money
	declare @ccpcrname varchar(50)
	declare @ccpcrnum varchar(50)
	declare @ccpcrbank varchar(50)
	declare @ccpgrno numeric
	declare @retval int
	declare @cancelled char(1)
	declare @partic varchar(200)
	declare @chlno numeric
	declare @olname varchar(50)
	declare @actno numeric

	begin transaction
	set @err='Error in Select 1'
	if @tttype=2
		begin
			select @outlet=outlet,@status=status,@chlno=chlno from chalan where chctrlno=@chctrlno
			if @@error<>0 goto e_error
		end
		else
		begin
			select @outlet=outlet,@status=status,@chlno=chlno from ta_chalan where ctrlno=@chctrlno
			if @@error<>0 goto e_error
		end


	select @txndt=cancellationchgspaiddate,
	@ccpamt=cancellationchgspaidamount,@mopp=cancellationchgspaidmop,@ccpcrname=cancellationchgspaidcrname,@ccpcrnum=cancellationchgspaidcrnum,@ccpcrbank=cancellationchgspaidcrbank,@ccpgrno=cancellationchgspaidgrno,
	@ccramt=cancellationchgsrecdamount,@mopr=cancellationchgsrecdmop,@ccrcrname=cancellationchgsrecdcrname,@ccrcrnum=cancellationchgsrecdcrnum,@ccrcrbank=cancellationchgsrecdcrbank,@ccrgrno=cancellationchgsrecdgrno,
	@mopp=cancellationchgspaidmop,@ccpcrnum=cancellationchgspaidcrnum 
	from ta_cancel where chctrlno=@chctrlno
	if @@error<>0 goto e_error

	set @err='Error in Select 2'
	select @company=company,@salesreturn2=salesreturn2,@purchasereturn=purchasereturn2,@servicetaxreturnac=servicetaxreturnac,
	@olname=name, @cancellationchgspaidac=cancellationchgspaidac  
	from toutlet where outlet=@outlet
	if @@error<>0 goto e_error

	if @cancelled<>'Y' begin  set @err = 'This is not a cancelled Entry...Process aborted'  goto e_error end
	exec getfy @txndt,@company,@fy out
	if @@error<>0 goto e_error

	if isnull(@ccpamt,0)<>0
		if isnull(@ccpgrno,0)=0
		begin
			exec getgrp @txndt,@company,@ccpgrno out
			if @@error<>0 goto e_error
		end
		if @tttype=2
			select @partic = rtrim(@olname) + ' Ticket Ch.No.' + ltrim(str(@chlno)) +  '-Cancellation Charges Paid for Ticket cancellation'
		else
			select @partic = rtrim(@olname) + ' Hotel  Ch.No.' + ltrim(str(@chlno)) +  '-Cancellation Charges Paid for Ticket cancellation'

	if isnull(@ccpamt,0)<>0
	begin
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @cancellationchgspaidac,@ccpamt , 'To '+@partic   ,'D',@txndt,@ccpgrno,@chlno,'Y','TKTC')
		if @@error <> 0 goto e_error
		end
		begin
		select @actno = case when @mopp=0 then 1 else @ccpcrnum end 
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @actno,@ccpamt , 'By '+@partic   ,'C',@txndt,@ccpgrno,@chlno,'Y','TKTC')
		if @@error <> 0 goto e_error
		end
	end
	update ta_cancel set cancellationchgspaid='Y',cancellationchgspaidgrno=@ccpgrno where chctrlno=@chctrlno and tttype=@tttype
commit transaction
return
	E_error:
	rollback transaction
	set @err = 'Error on Cancellation - ' + @err
	raiserror(@err,16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[sptt2canchgsr]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sptt2canchgsr]
@chctrlno numeric,@txndt datetime,
@amtrecd money,@mopp tinyint, @crnamep varchar(50),@crnump varchar(60),@crbankp varchar(50),
@user varchar(20),@tttype tinyint
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @status char(1)
	declare @cancelled char(1)
	declare @err varchar(500) 
	declare @outlet int
	declare @salesreturn2 numeric
	declare @purchasereturn numeric
	declare @servicetaxreturnac numeric
	set @err= 'Error : ' 
begin transaction
if @tttype=2
	select @outlet=outlet,@status=status,@cancelled=cancelled from chalan where chctrlno=@chctrlno
else
	select @outlet=outlet,@status=status,@cancelled=cancelled from ta_chalan where ctrlno=@chctrlno

	select @salesreturn2=salesreturn2,@purchasereturn=purchasereturn2,@servicetaxreturnac=servicetaxreturnac 
	from toutlet where outlet=@outlet
	set @err='Error in Updating'
	if @cancelled <> 'Y' begin set @err = 'This is not a cancelled Entry...Process aborted'  goto e_error end
	set @err='Error in Updating1'
    update ta_cancel set cancellationchgsrecdamount=@amtrecd,
	cancellationchgsrecdmop=@mopp,cancellationchgsrecdcrname=@crnamep,cancellationchgsrecdcrnum=@crnump,cancellationchgsrecdcrbank=@crbankp,
	cancellationchgsrecddate=@txndt,cancellationchgsrecdby=@user,cancellationchgsrecd='Y'
	where tttype=@tttype and chctrlno=@chctrlno
	if @@error<>0 goto e_error
	set @err='Error in Vouching'
	exec sptt2canchgsrv @chctrlno,@tttype
	if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
set @err = 'Error on Cancellation Charges - ' + @err
raiserror(@err,16,-1)

END


GO
/****** Object:  StoredProcedure [dbo].[sptt2canchgsrv]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sptt2canchgsrv]
@chctrlno numeric,@tttype tinyint 
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @status char(1)
	declare @err varchar(500) 
	declare @outlet int
	declare @salesreturn2 numeric
	declare @purchasereturn numeric
	declare @servicetaxreturnac numeric
	declare @cancellationchgsrecdac numeric
	declare @company int
	declare @fy numeric
	declare @txndt datetime

	declare @mopr int
	declare @ccramt money
	declare @ccrcrname varchar(50)
	declare @ccrcrnum varchar(50)
	declare @ccrcrbank varchar(50)
	declare @ccrgrno numeric

	declare @mopp int
	declare @ccpamt money
	declare @ccpcrname varchar(50)
	declare @ccpcrnum varchar(50)
	declare @ccpcrbank varchar(50)
	declare @ccpgrno numeric
	declare @retval int
	declare @cancelled char(1)
	declare @partic varchar(200)
	declare @chlno numeric
	declare @olname varchar(50)
	declare @actno numeric

	begin transaction
	set @err='Error in Select 1'
	if @tttype=2
		begin
			select @outlet=outlet,@status=status,@chlno=chlno from chalan where chctrlno=@chctrlno
			if @@error<>0 goto e_error
		end
		else
		begin
			select @outlet=outlet,@status=status,@chlno=chlno from ta_chalan where ctrlno=@chctrlno
			if @@error<>0 goto e_error
		end

	select @txndt=cancellationchgsrecddate,
	@ccpamt=cancellationchgsrecdamount,@mopp=cancellationchgsrecdmop,@ccpcrname=cancellationchgsrecdcrname,@ccpcrnum=cancellationchgsrecdcrnum,@ccpcrbank=cancellationchgsrecdcrbank,@ccpgrno=cancellationchgsrecdgrno,
	@ccramt=cancellationchgsrecdamount,@mopr=cancellationchgsrecdmop,@ccrcrname=cancellationchgsrecdcrname,@ccrcrnum=cancellationchgsrecdcrnum,@ccrcrbank=cancellationchgsrecdcrbank,@ccrgrno=cancellationchgsrecdgrno,
	@mopp=cancellationchgsrecdmop,@ccpcrnum=cancellationchgsrecdcrnum 
	from ta_cancel where tttype=@tttype and chctrlno=@chctrlno
	if @@error<>0 goto e_error

	set @err='Error in Select 2'
	select @company=company,@salesreturn2=salesreturn2,@purchasereturn=purchasereturn2,@servicetaxreturnac=servicetaxreturnac,
	@olname=name, @cancellationchgsrecdac=cancellationchgsrecdac  
	from toutlet where outlet=@outlet
	if @@error<>0 goto e_error

	if @cancelled<>'Y' begin  set @err = 'This is not a cancelled Entry...Process aborted'  goto e_error end
	exec getfy @txndt,@company,@fy out
	if @@error<>0 goto e_error

	if isnull(@ccpamt,0)<>0
		if isnull(@ccpgrno,0)=0
		begin
			exec getgrp @txndt,@company,@ccpgrno out
			if @@error<>0 goto e_error
		end
	if @tttype=2
		select @partic = rtrim(@olname) + ' Ticket Ch.No.' + ltrim(str(@chlno)) +  '-Cancellation Charges recd for Ticket cancellation'
	else
		select @partic = rtrim(@olname) + ' Hotel  Ch.No.' + ltrim(str(@chlno)) +  '-Cancellation Charges recd for Ticket cancellation'

	if isnull(@ccpamt,0)<>0
	begin
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @cancellationchgsrecdac,@ccpamt , 'By '+@partic   ,'C',@txndt,@ccpgrno,@chlno,'Y','TKTC')
		if @@error <> 0 goto e_error
		end
		begin
		select @actno = case when @mopp=0 then 1 when @mopp=1 then @ccpcrnum else 22 end 
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @actno,@ccpamt , 'To '+@partic   ,'D',@txndt,@ccpgrno,@chlno,'Y','TKTC')
		if @@error <> 0 goto e_error
		end
	end
	update ta_cancel set cancellationchgsrecd='Y',cancellationchgsrecdgrno=@ccpgrno where tttype=@tttype and chctrlno=@chctrlno
commit transaction
return
	E_error:
	rollback transaction
	set @err = 'Error on Cancellation - ' + @err
	raiserror(@err,16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[sptt2candelete]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<PowerBrain Software (India)>
-- Create date: <17 Ju; 2015>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sptt2candelete]
@chctrlno numeric , @tttype tinyint
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @pgrno numeric
	declare @sgrno numeric
	declare @outlet int
	declare @company numeric
	declare @fy numeric
	declare @txndt datetime
	declare @err varchar(2000)
	declare @ret_val int
	if @tttype=2
	select @outlet=outlet from chalan where chctrlno=@chctrlno
	else
	select @outlet=outlet from ta_chalan where ctrlno=@chctrlno

	select @txndt=cancelledon,@pgrno=cancellationpurchasereturngrno,@sgrno=cancellationsalesreturngrno from ta_cancel where tttype=@tttype and chctrlno=@chctrlno
	select @company=company from toutlet where outlet=@outlet
	begin transaction 
	exec getfy @txndt,@company,@fy out
	if @@error<>0 goto e_error
	exec delgrno @company,@fy,@pgrno,@txndt,@ret_val out
	if @@error<>0 goto e_error
	exec delgrno @company,@fy,@sgrno,@txndt,@ret_val out
	if @@error<>0 goto e_error
	if @tttype=2
    update chalan set cancelled=' ' where chctrlno=@chctrlno    
	else
    update ta_chalan set cancelled=' ' where ctrlno=@chctrlno    
	if @@error<>0 goto e_error
	delete from ta_cancel where tttype=@tttype and chctrlno=@chctrlno
	if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror(@err,16,-1)

END

GO
/****** Object:  StoredProcedure [dbo].[sptt2canpv]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Ramanathan PowerBrain
-- Create date: 13 July 2015
-- Description  
-- 2 purchase return  (PR cr to Party Dr)   
-- 3 advance recovery (advpaid Cr to Party Dr)
-- =============================================
CREATE PROCEDURE [dbo].[sptt2canpv]
@chctrlno numeric,@tttype tinyint
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @status char(1)
	declare @err varchar(500) 
	declare @outlet int
	--declare @salesreturn2 numeric
	declare @purchasereturn numeric
	declare @servicetaxreturnac numeric
	declare @company int
	declare @fy numeric
	declare @txndt datetime

	declare @mopr int
	declare @ccramt money
	declare @ccrcrname varchar(50)
	declare @ccrcrnum varchar(50)
	declare @ccrcrbank varchar(50)
	declare @ccrgrno numeric

	declare @mopp int
	declare @ccpamt money
	declare @ccpcrname varchar(50)
	declare @ccpcrnum varchar(50)
	declare @ccpcrbank varchar(50)
	declare @ccpgrno numeric
	declare @retval int
	declare @cancelled char(1)
	declare @actno numeric
	declare @partic varchar(200)
	declare @partic1 varchar(50)
	declare @chlno numeric
	if @tttype=2 
		begin
			select @chlno=chlno,@outlet=outlet,@status=status,@cancelled=cancelled,@ccpamt=finalamt_t from chalan where chctrlno=@chctrlno
			select @txndt=cancelledon, 	@mopp=cancellationpurchasereturnmop,@ccpcrname=cancellationpurchasereturncrname,@ccpcrnum=cancellationpurchasereturncrnum,@ccpcrbank=cancellationpurchasereturncrbank,@ccpgrno=cancellationpurchasereturngrno from ta_cancel where tttype=@tttype and chctrlno=@chctrlno
			select @company=company,@purchasereturn=purchasereturn2,@servicetaxreturnac=servicetaxreturnac from toutlet where outlet=@outlet
		end
		else
		begin
			select @chlno=chlno,@outlet=outlet,@status=status,@cancelled=cancelled,@ccpamt=advance_t from ta_chalan where ctrlno=@chctrlno
			select @txndt=cancelledon, 	@mopp=cancellationpurchasereturnmop,@ccpcrname=cancellationpurchasereturncrname,@ccpcrnum=cancellationpurchasereturncrnum,@ccpcrbank=cancellationpurchasereturncrbank,@ccpgrno=cancellationpurchasereturngrno from ta_cancel where tttype=@tttype and chctrlno=@chctrlno
			select @company=company,@purchasereturn=advancepaid,@servicetaxreturnac=servicetaxreturnac from toutlet where outlet=@outlet
		end


	begin transaction
	if @cancelled <>'Y' begin set @err = 'This is not a cancelled Entry...Process aborted'  goto e_error end

	exec getfy @txndt,@company,@fy out
	if @@error<>0 goto e_error
	if isnull(@ccpamt,0)<>0
		begin
			if isnull(@ccpgrno,0)=0
				begin
					exec getgrp @txndt,@company,@ccpgrno out
					if @@error<>0 goto e_error
				end
			select @actno= case when @mopp=0  then 1 else @ccpcrnum end
			if @tttype=2 
				begin
					select @partic='Ticket Ch.No' + ltrim(str(@chctrlno)) + ' Dated ' + convert(varchar(20),@txndt,3) +' Cancelled - ' 
					select @partic1 = 'Dr Account Purchase Return A./C'
				end
				else
				begin
					select @partic='Hotel Ch.No' + ltrim(str(@chctrlno)) + ' Dated ' + convert(varchar(20),@txndt,3) +' Cancelled - ' 
					select @partic1 = 'Dr Account Advance Received A./C'
				end
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
			values (@company,@fy, @actno,@ccpamt , 'To '+ @partic + @partic1 ,'D',@txndt,@ccpgrno,@chlno,'Y','TKTC')
			if @@error <> 0 goto e_error

			select @partic1 = 'Cr Account ' + @ccpcrname 
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
			values (@company,@fy, @purchasereturn,@ccpamt , 'By '+ @partic + @partic1 ,'C',@txndt,@ccpgrno,@chlno,'Y','TKTC')
			if @@error <> 0 goto e_error
		end
		update ta_cancel set cancellationpurchasereturngrno=@ccpgrno where tttype=@tttype and chctrlno=@chctrlno
commit transaction
return
	E_error:
	rollback transaction
	set @err = 'Error on Cancellation - ' + @err
	raiserror(@err,16,-1)

END

GO
/****** Object:  StoredProcedure [dbo].[sptt2canrv]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Ramanathan PowerBrain
-- Create date: 13 July 2015
-- Description  
-- 2 purchase return  (PR cr to Party Dr)   
-- 3 advance recovery (advpaid Cr to Party Dr)
-- =============================================
CREATE PROCEDURE [dbo].[sptt2canrv]
@chctrlno numeric,@tttype tinyint
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @status char(1)
	declare @err varchar(500) 
	declare @outlet int
	declare @salesreturn2 numeric
	declare @purchasereturn numeric
	declare @servicetaxreturnac numeric
	declare @company int
	declare @fy numeric
	declare @txndt datetime

	declare @mopr int
	declare @ccramt money
	declare @ccrcrname varchar(50)
	declare @ccrcrnum varchar(50)
	declare @ccrcrbank varchar(50)
	declare @ccrgrno numeric

	declare @mopp int
	declare @ccpamt money
	declare @ccpcrname varchar(50)
	declare @ccpcrnum varchar(50)
	declare @ccpcrbank varchar(50)
	declare @ccpgrno numeric
	declare @retval int
	declare @cancelled char(1)
	declare @actno numeric
	declare @partic varchar(200)
	declare @partic1 varchar(50)
	declare @chlno numeric

	declare @salesreturn money  -- finalbasic
	declare @servicetax money  --  finaltax
	declare @sc money  --  finalbata
	declare @discount money -- discount
	declare @partyamt money -- finalamt
  
set @salesreturn = 0
set @servicetax =0
set @sc = 0 
set @discount=0
set @partyamt=0

	if @tttype=2
		begin
			select @chlno=chlno,@outlet=outlet,@status=status,@cancelled=cancelled,
			@salesreturn=isnull(finalbasic,0),@discount=isnull(discount,0),@servicetax=isnull(finaltax,0),@sc=isnull(finalbata,0),@partyamt=isnull(finalamt,0),@ccpamt=finalamt_t 
			from chalan where chctrlno=@chctrlno

			select @txndt=cancelledon,@mopp=cancellationsalesreturnmop,@ccpcrname=cancellationsalesreturncrname,@ccpcrnum=cancellationsalesreturncrnum,@ccpcrbank=cancellationsalesreturncrbank,@ccpgrno=cancellationsalesreturngrno
			from ta_cancel  where tttype=@tttype and chctrlno=@chctrlno

			select @company=company,@salesreturn2=salesreturn2,@purchasereturn=purchasereturn2,@servicetaxreturnac=servicetaxreturnac 
			from toutlet where outlet=@outlet
		end
		else
		begin
			select @chlno=chlno,@outlet=outlet,@status=status,@cancelled=cancelled,
			@salesreturn=isnull(advance,0),@ccpamt=isnull(advance,0)
			from ta_chalan where ctrlno=@chctrlno

			select @txndt=cancelledon,@mopp=cancellationsalesreturnmop,@ccpcrname=cancellationsalesreturncrname,@ccpcrnum=cancellationsalesreturncrnum,@ccpcrbank=cancellationsalesreturncrbank,@ccpgrno=cancellationsalesreturngrno
			from ta_cancel  where tttype=@tttype and chctrlno=@chctrlno

			select @company=company,@salesreturn2=advancerecd from toutlet where outlet=@outlet

			set @partyamt=@salesreturn 

		end


	begin transaction
	if @cancelled <>'Y' begin set @err = 'This is not a cancelled Entry...Process aborted'  goto e_error end

	exec getfy @txndt,@company,@fy out
	if @@error<>0 goto e_error
	if isnull(@ccpamt,0)<>0
		begin
			if isnull(@ccpgrno,0)=0
			begin
				exec getgrp @txndt,@company,@ccpgrno out
				if @@error<>0 goto e_error
			end

			select @partic='Ticket Ch.No' + ltrim(str(@chctrlno)) + ' Dated ' + convert(varchar(20),@txndt,3) +' Cancelled - ' 
			select @partic1 = 'Dr Account ' + @ccpcrname 
			select @actno= case when @mopp=0  then 1 else @ccpcrnum end

			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
			values (@company,@fy, @salesreturn2,@salesreturn,'To '+ @partic + @partic1 ,'D',@txndt,@ccpgrno,@chlno,'Y','TKTC')
			if @@error <> 0 goto e_error

			--Discount
			if @discount<>0
				begin
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (@company,@fy, 62,@discount , 'By '+ @partic ,'C',@txndt,@ccpgrno,@chlno,'Y','TKTC')
				if @@error <> 0 goto e_error
				end
			--service charges
			if @sc <> 0 
				begin
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (@company,@fy, 49,@sc , 'To '+ @partic ,'D',@txndt,@ccpgrno,@chlno,'Y','TKTC')
				if @@error <> 0 goto e_error
				end
		
			--service tax
			if @servicetax <> 0
				begin
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (@company,@fy, 61,@servicetax , 'To '+ @partic ,'D',@txndt,@ccpgrno,@chlno,'Y','TKTC')
				if @@error <> 0 goto e_error
				end
			if @partyamt<>0
				begin
				select @partic1 = 'Cr Account Sales Return A/C'
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (@company,@fy, @actno,@partyamt , 'By '+ @partic + @partic1 ,'C',@txndt,@ccpgrno,@chlno,'Y','TKTC')
				if @@error <> 0 goto e_error
				end
		end
		update ta_cancel set cancellationsalesreturngrno=@ccpgrno where chctrlno=@chctrlno and tttype=@tttype
commit transaction
return
	E_error:
	rollback transaction
	set @err = 'Error on Cancellation - ' + @err
	raiserror(@err,16,-1)

END

GO
/****** Object:  StoredProcedure [dbo].[sptt2cl]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sptt2cl    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sptt2cl]
@ctrlno int,@mop int, @crname varchar(50),@crnum varchar(20),@crbank varchar(20),@glactno int,@txndt datetime,@roomcomp char(1)
/* with encryption */
as
declare @company int
declare @fy int
declare @grno int
declare @olname varchar(50)
declare @gno int
declare @sales int
declare @chlno int
declare @outlet int
declare @myreceivable int
declare @hno_mybook int
declare @dactno int
declare @hno_htlbook int
declare @htl_purchase int
declare @htl_sales int
declare @htl_receivable int
declare @selfdeal char(1)
declare @split char(1)
declare @partic varchar(100)
declare @status char(1)
declare @finalamt money
declare @finaltax money
declare @serchgs money
declare @discount money
declare @advance money
declare @amt money
declare @advrecd money
declare @ino numeric
declare @gstin nvarchar(15)
begin transaction
select @ino=ino from chalan where chctrlno=@ctrlno

select @chlno=chlno,@company=company,@outlet=outlet,@olname=olname,@sales=sales2,@finalamt=finalbasic,@finaltax=finaltax,@serchgs=finalbata,@discount=discount,
@advance=advance,@selfdeal=selfdeal,@myreceivable=my_receivable,@htl_purchase=htl_purchase,@htl_sales=htl_sales,
@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,@htl_receivable=htl_receivable,@advrecd=advancerecd,@status=status
from tt_chalan_ol where chctrlno=@ctrlno
if @status <> '' begin print 'This chalan  is closed....'   goto e_error end
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
EXEC  GETGRP @TXNDT,@COMPANY,@GRNO OUT
IF @GRNO=0 GOTO E_ERROR
set @gno=0
if @mop=2 set @gno=@glactno
if @mop=2 
  if exists (select * from room where gno=@gno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
set @split='N'
if @roomcomp='Y' set @split='C'
if @mop=2 
select @gstin=case when @split<>'Y' then gstin else gstinsplit end from guest where gno=@gno 
update chalan set status='C',finaldate=@txndt,mop=@mop,cc_name=@crname,cc_num=@crnum,cc_bank=@crbank,
glactnum=@glactno,gno=@gno,grno=@grno,split=@split,gstin=@gstin 
where chctrlno=@ctrlno
if @@error <> 0 goto e_error

if @ino=0 EXEC SPTT2VPC @CTRLNO

print 'periya'
final:
commit transaction
return
e_error:
rollback transaction
raiserror('Porblematic closure..',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sptt2crv]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sptt2crv    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sptt2crv] @ctrlno numeric,@userno numeric 
/* with encryption */
AS
declare @company int
declare @txndt datetime
declare @fy int
declare @grno int
declare @grno_t int
declare @grno_co1 int
declare @retval int
declare @gno int
declare @outlet int
declare @amt money
declare @chlno numeric
declare @transact int
declare @lux char(1)
declare @m_split char(1)
declare @m_txndt datetime
declare @sql varchar(5000)
begin transaction
set @transact = 0
select @m_txndt=finaldate,@outlet=outlet,@chlno=chlno,@amt=amt from chalan where chctrlno = @ctrlno
select  @lux=lux, @m_split=split,@gno=gno,@company=company,@txndt=finaldate,@grno=grno,@grno_t=grno_t,@grno_co1=grno_co1 from tt_chalan_ol where chctrlno=@ctrlno
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
if @gno<>0 
  if exists (select * from room where gno=@gno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
if @gno<>0 if not exists (select * from room where gno=@gno) begin
  if not exists(select * from servicepermission where userno=@userno and serviceno=185 and outlettype=4 and permission='Y') begin
    print 'You are not permitted to Revive Chalans after check-out'
    goto e_error
  end
  set @transact=1
end
update chalan set finaldate=null,cc_name='',cc_num='',cc_bank='',grno=0,status=' ',split=' ',amt=0,gno=0,glactnum=0,mop=0 where chctrlno=@ctrlno
if @@error <> 0 goto e_error
exec delgrno @company,@fy,@grno,@txndt,@retval out
if @@error <> 0 goto e_error
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
end
declare @m_olname varchar(30)
declare @usernm varchar(30)
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @m_olname=[name] from toutlet where outlet=@outlet
insert into acclog (txndt,usernm,narration,txntype) values (getdate(),@usernm, ltrim(rtrim(@M_olname))+ ' TKT Chalan  No ' + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@AMT)) +  ' revived ',11)
if @@error<>0 goto e_error
if @transact=1
   begin
   update chalan set ogno=0 where chctrlno=@ctrlno
   set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@amt) + ',0,' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,0,''X'',' + str(@userno) + ',''' + @lux + ''''
   exec (@sql)
   if @@error <> 0  goto e_error
  --update chalan set ogno=@gno  where chctrlno=@ctrlno
   end
insert into acclog(txndt,usernm,narration,txntype) values (getdate(),'',
'TKT chalan No.' + str(@chlno) +  ' Closed on ' + convert(varchar(20) ,@txndt) +    ' Revived after Closure',
19)
if @@error<>0 goto e_error
commit transaction
return
e_error:
raiserror('deletion of chalan failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sptt2E]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sptt2    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sptt2E]  @outlet int ,
@txndt datetime,
@name varchar(50),
@add1 varchar(50),
@add2 varchar(50),
@add3 varchar(50),
@city varchar(50),
@state varchar(50),
@details varchar(100),
@purchasetype int,
@travelagent int,
@glactno int,
@finalamt money,
@finalamt_t money,
@tax money,
@discount money,
@serchgs money,
@tttype int,
@chlno int,
@ogno int,
@nota int,
@maxkm int,
@chctrlno numeric,
@cess money
/* with encryption */
AS
declare @SQLMSG  varchar(500)
declare @autochln int
declare @company int
declare @year int
declare @bofy datetime
declare @purchase2 int
declare @olname varchar(50)
declare @partic varchar(100)
declare @grno int
declare @fy int
declare @staxrate money
declare @id numeric
declare @temp numeric
declare @ocompany int
declare @ofy int
declare @otxndtTA datetime
declare @otxndtFinal  datetime

declare @ogrno numeric
declare @ret_val int
declare @ooutlet int
declare @ogrno_t numeric
declare @taname varchar(100)

declare @sales int
declare @hno_mybook int
declare @dactno int
declare @hno_htlbook int
declare @htl_purchase int
declare @htl_sales int
declare @htl_receivable int
declare @selfdeal char(1)
declare @split char(1)

declare @finaltax money
declare @advance money
declare @amt money
declare @advrecd money
declare @ctrlno numeric
declare @mop integer
declare @status char(1)
declare @ofinaldate datetime
declare @gno numeric
declare @oldamt money
declare @lux char(1)
set @sqlmsg = ''
begin transaction

declare @ofinalamt money
declare @ofinaltax money
declare @oserchgs money
declare @odiscount money
select @outlet=outlet,@ofinalamt=finalbasic,@ofinaltax=finaltax,@oserchgs=finalbata,@odiscount=discount,
@status=status,@mop=mop,@gno=gno,@split=split,@lux=lux,@gno=gno,
@ofinaldate=finaldate
from tt_chalan_ol where chctrlno=@chctrlno

set @oldamt=@ofinalamt+@ofinaltax+@oserchgs-@odiscount

if @ogno<>0 
  if exists (select * from room where gno=@ogno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
IF @TXNDT < '1 APR 1950'  SELECT @TXNDT = GETDATE()
--IF DATEPART(mm,@TXNDT) >=  1 AND DATEPART(mm,@TXNDT) <= 3
--     SET @YEAR = DATEPART(yy,@TXNDT)-1
--ELSE
--     SET @YEAR = DATEPART(yy,@TXNDT)
--SELECT @BOFY = '1 APR ' + STR(@YEAR)

set @bofy = dbo.getbofy(@txndt)
set @year = datepart(yy,@bofy)


select @staxrate=tkttaxrate from tktmaster where tktctrlno=@tttype
set @taname = 'No Travel Agent'
if @purchasetype = 1
begin
	set @glactno=0
	--   select @glactno=glactno from travelagent where code=@travelagent
	declare @glac numeric
	declare @tafy numeric
	select @taname=[name],@glac=glactno,@tafy=fy from travelagent where code=@travelagent
	set @glactno=dbo.findactnum(@glac , @tafy,@txndt)
end
select @autochln=autono2, @purchase2=purchase2,@company=company,@olname=[name] from toutlet where outlet=@outlet
--print 'periya' + str(@company) + convert(varchar(20),@txndt)  goto e_error

EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR

select @chlno=chlno,@ogrno=grno, @ogrno_t=grno_t,@otxndtTA=chldate,@otxndtfinal=finaldate,@ooutlet=outlet , 
@mop=mop,@status=status,@ofinaldate=finaldate
from chalan where chctrlno=@chctrlno


select @ocompany=company from toutlet where outlet=@ooutlet

set @grno=@ogrno_t
-- Deleting Old Transaction entries
exec getfy @otxndtTA,@ocompany,@ofy out
exec delgrno @ocompany,@ofy,@ogrno,@otxndtfinal,@ret_val out
exec delgrno @ocompany,@ofy,@ogrno_t,@otxndtTA,@ret_val out
---- upto this deletion
--insert chalan (chlno,chldate,stype,[name],add1,add2,add3,city,state,destination,travelagent,finalamt,
--tttype,finalamt_t, sreading,outlet,discount,finaltax,finalbata,ereading,grno_t,ogno,stax,finalbasic,finalbasic_t) values
--(@chlno,@txndt,2,@name,@add1,@add2,@add3,@city,@state,@details,@travelagent,@finalamt+@tax+@serchgs-@discount,
--@tttype,@finalamt_t,@purchasetype,@outlet,@discount,@tax,@serchgs,@glactno,@grno,@ogno,@staxrate,@finalamt,@finalamt_t)
update chalan set notravelagent=@nota,
finaldate=@otxndtfinal,[name]=@name,add1=@add1,add2=@add2,add3=@add3,city=@city,
state=@state,destination=@details,travelagent=@travelagent,finalamt=@finalamt+@tax+@serchgs-@discount+@cess,
tttype=@tttype,finalamt_t=@finalamt_t,sreading=@purchasetype,outlet=@outlet,discount=@discount,finaltax=@tax,finalbata=@serchgs,
ereading=@glactno,grno_t=@grno,ogno=@ogno,stax=@staxrate,finalbasic=@finalamt,finalbasic_t=@finalamt_t ,maxkm=@maxkm,cess=@cess
where chctrlno = @chctrlno
if @@error <> 0 goto e_error
--set @id = @@identity

select @outlet=outlet,@finalamt=finalbasic,@finaltax=finaltax,@serchgs=finalbata,@discount=discount,
@status=status,@txndt=finaldate,@mop=mop,@gno=gno,@split=split,@lux=lux
from tt_chalan_ol where chctrlno=@chctrlno

set @finalamt=@finalamt+@finaltax+@cess+ @serchgs-@discount
if @mop=2
begin
declare @userno numeric
declare @sql varchar(5000)
select @userno=userno from comuser where [host_id]=host_id()
select @userno=userno from comuser where [host_id]=host_id() and sessionid=''
set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@oldamt) + ','  + convert(varchar(20),@finalamt) +  ',' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@txndt) + ''',''' + @split + ''',0,' + convert(varchar(20),@finalamt) + ',''X'',' + str(@userno) + ',''' + @lux + ''''
--print @sql  goto e_error

exec(@sql)
end



EXEC SPTT2VPT @CHCTRLNO
declare @usernm varchar(50)
select @usernm=usernm from comuser where [host_id]=host_id()  and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
insert into acclog (txndt,usernm,narration,txntype) values (getdate(),@usernm, 
'Ticketing Chalan no ' + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@otxndtta) + ' Edited ' ,17)
--- if chalan is closed 
if @status='C' 
EXEC SPTT2VPC @chCTRLNO
--print 'periya'
final:
commit transaction
return @id
--print 'periya karuppa'
e_error:
rollback transaction
set @sqlmsg = 'chalan updation failed'+@sqlmsg
raiserror(@sqlmsg,16,-1)
return 0




GO
/****** Object:  StoredProcedure [dbo].[SPTT2VPC]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SPTT2VPC] @ctrlno numeric
	-- Add the parameters for the stored procedure here
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
declare @company int
declare @fy int
declare @grno int
declare @olname varchar(50)
declare @gno int
declare @salesac numeric
declare @sales2 numeric
declare @sales5 numeric
declare @sales6 numeric

declare @chlno numeric
declare @outlet numeric
declare @myreceivable numeric
declare @hno_mybook numeric
declare @dactno numeric
declare @hno_htlbook numeric
declare @htl_purchase numeric
declare @htl_sales numeric 
declare @htl_receivable numeric
declare @selfdeal char(1)
declare @split char(1)
declare @partic varchar(100)
declare @status char(1)
declare @finalamt money
declare @finaltax money
declare @serchgs money
declare @discount money
declare @advance money
declare @amt money
declare @advrecd money
declare @err varchar(200)
declare @retval int
declare @txndt datetime
declare @mop int
declare @glactno numeric
declare @crname varchar(50)
declare @crnum varchar(50)
declare @crbank varchar(50)
declare @grno_co1 numeric
declare @lux char(1)
declare @sql varchar(6000)
declare @bamt money
declare @tttype int
declare @cess money
declare @cessac numeric
declare @sttableno int
declare @taxacno numeric
set @taxacno=61

set @err = ''
begin transaction
select @chlno=chlno,@company=company,@outlet=outlet,@olname=olname,@tttype=tttype,
@finalamt=finalbasic,@finaltax=finaltax,@serchgs=finalbata,@discount=discount,
@advance=advance,@selfdeal=selfdeal,@myreceivable=my_receivable,@htl_purchase=htl_purchase,@htl_sales=htl_sales,
@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,@htl_receivable=htl_receivable,@advrecd=advancerecd,@status=status,
@txndt=finaldate,@mop=mop,@gno=gno,
@crname=cc_name,@crnum=cc_num,@crbank=cc_bank,
@glactno=glactnum,@split=split,
@grno=grno,@grno_co1=grno_co1,
@lux=lux
from tt_chalan_ol where chctrlno=@ctrlno

select @cess=cess from chalan where chctrlno=@ctrlno

select @sales2=sales2,@sales5=sales5,@sales6=sales6 from toutlet where outlet=@outlet
select @cessac=89

if @status<>'C' 
begin
set @err = 'This chalan is not closed'
goto e_error
end

select @sttableno=sttableno  from ta_servicetaxsetting where outlet=@outlet  and servicetype=0

select @taxacno=taxacno from sttabler where sttableno=@sttableno and datediff(d,'1/1/2000',@txndt)
between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
and @FINALamt between samt and eamt

exec getfy @txndt,@company,@fy out

if isnull(@grno,0) <> 0
exec delgrno @Company,@fy,@grno,@txndt,@retval out

if isnull(@grno_co1,0) <> 0
exec delgrno 1,@fy,@grno_co1,@txndt,@retval out

if isnull(@grno,0)=0  exec getgrp @txndt,@company,@grno out

update chalan set grno=@grno where chctrlno=@ctrlno

if @mop=2 if exists (select * from room where gno=@gno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end

if exists(select * from setting where g_finaccount=0) goto final

select @salesac = 
	case
		when @tttype=1 then @sales2
		when @tttype=2 then @sales5
		when @tttype=3 then @sales6
	end

SELECT @DACTNO = 
	CASE 
	WHEN @MOP = 1 THEN 1           --- CASH SALES
	WHEN @MOP = 2 THEN               -- ROOM SERVICE
		case when @split<>'C' then
			CASE WHEN @COMPANY = 1 THEN @MYRECEIVABLE 
			ELSE  
				CASE
				WHEN @SELFDEAL='Y' THEN  @myRECEIVABLE   ELSE @HNO_MYBOOK
				END
			END 
		else 26 
		end
	WHEN @MOP = 3 THEN 23  -- BILL RECEIVABLE
	WHEN @MOP = 4 THEN @glactno -- GL A/C
	WHEN @MOP = 5 THEN 22   -- CREDIT CARD 
	WHEN @MOP = 6 THEN 0   -- COMPLEMENTARTY No vouching 
	END
set @partic = 'Chln.No ' + ltrim(str(@chlno))+ ' ' + @olname
if @mop=5
set @partic = @crnum + ' ' + @crname + ' ' + @partic
--print @partic
set @amt=@finalamt+@finaltax+@serchgs-@discount+@cess 
set @bamt = @amt
--print 'ramamant'
--print convert(varchar(20),@amt)
if @mop=6 goto Final

	if @amt > 0
		BEGIN
		update chalan set amt=@amt where chctrlno=@ctrlno
		if @@error <> 0 goto e_error
		INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		VALUES (@COMPANY,@FY, @salesac,@finalamt , 'By '+@partic   ,'C',@TXNDT,@GRNO,@chlno,'Y','TKT')
		IF @@ERROR <> 0 GOTO E_ERROR
		END
	if @finaltax<>0 
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,@TAXACNO,@finaltax,'By ' + @partic ,'C',@txndt,@grno,@chlno,'Y','TKT')
		if @@error<>0 goto e_error
		end
	if @cess<>0 
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,@cessac,@cess,'By ' + @partic ,'C',@txndt,@grno,@chlno,'Y','TKT')
		if @@error<>0 goto e_error
		end

	if @serchgs<>0 
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,49,@serchgs,'By ' + @partic ,'C',@txndt,@grno,@chlno,'Y','TKT')
		if @@error<>0 goto e_error
		end
	if @discount <>0 
		begin
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy,62,@discount ,'To ' + @partic ,'D',@txndt,@grno,@chlno,'Y','TKT')
		if @@error<>0 goto e_error
		end
	if @advance <> 0
		BEGIN
		INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		VALUES (@COMPANY,@FY, @advrecd  ,@advance , 'To '+@partic   ,'D',@TXNDT,@GRNO,@chlno,'Y','TKT')
		IF @@ERROR <> 0 GOTO E_ERROR
		END
	if @amt-@advance > 0
		BEGIN
		INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		VALUES (@COMPANY,@FY, @dactno  ,@amt-@advance , 'To '+@partic   ,'D',@TXNDT,@GRNO,@chlno,'Y','TKT')
		IF @@ERROR <> 0 GOTO E_ERROR
		END
	else if @advance-@amt > 0
		BEGIN
		INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		VALUES (@COMPANY,@FY, @dactno  ,@advance-@amt , 'By '+@partic   ,'C',@TXNDT,@GRNO,@chlno,'Y','TKT')
		IF @@ERROR <> 0 GOTO E_ERROR
		END
		

	if @selfdeal='N' and @company > 1 and @mop=2  and @split <> 'C'
		begin
		set @company = 1
		EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
		IF @FY=0 GOTO E_ERROR
		if isnull(@grno_co1,0)=0
		begin
		EXEC  GETGRP @TXNDT,@COMPANY,@GRNO OUT
		IF @GRNO=0 GOTO E_ERROR
		end
		else
		set @grno=@grno_co1
		
		

		update chalan set grno_co1=@grno where chctrlno=@ctrlno

		--INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		--VALUES (@COMPANY,@FY, @htl_purchase ,@amt , 'TO '+@partic   ,'D',@TXNDT,@GRNO,@chlno,'Y','TKT')
		--IF @@ERROR <> 0 GOTO E_ERROR

		INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		VALUES (@COMPANY,@FY, @hno_htlbook ,@amt , 'By '+@partic   ,'C',@TXNDT,@GRNO,@chlno,'Y','TKT')
		IF @@ERROR <> 0 GOTO E_ERROR

		--INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		--VALUES (@COMPANY,@FY, @htl_sales ,@amt , 'By '+@partic   ,'C',@TXNDT,@GRNO,@chlno,'Y','TKT')
		--IF @@ERROR <> 0 GOTO E_ERROR

		INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		VALUES (@COMPANY,@FY, @htl_receivable ,@amt , 'TO '+@partic   ,'D',@TXNDT,@GRNO,@chlno,'Y','TKT')
		IF @@ERROR <> 0 GOTO E_ERROR
		end
final:

commit transaction
return
e_error:
rollback transaction
set @err = 'Error in Voucher Posting:' + @err
raiserror(@err,16,-1)
return
END




GO
/****** Object:  StoredProcedure [dbo].[SPTT2VPT]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SPTT2VPT] @CTRLNO NUMERIC
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
declare @purchase2 numeric
declare @purchase5 numeric
declare @purchase6 numeric

declare @chlno numeric
declare @glactno numeric
declare @company numeric
declare @fy numeric
declare @grno numeric

declare @olname varchar(50)
declare @txndt datetime
declare @outlet numeric
declare @finalamt_t money
declare @partic varchar(200)
declare @nota int
declare @name varchar(50)
declare @taname varchar(50)
declare @travelagent numeric
declare @retval int
declare @tttype int

declare @purchaseac numeric 

begin transaction

select @tttype=tttype, @outlet=outlet,@txndt=chldate from chalan where chctrlno=@ctrlno

select @purchase2=purchase2,@purchase5=purchase5,@purchase6=purchase6,@company=company,
@olname=[name] from toutlet where outlet=@outlet

exec getfy @txndt,@company,@fy out

select
@chlno=chlno,@txndt=chldate,
@name=name,
@travelagent=travelagent,
@finalamt_t=finalamt_t,
@grno=grno_t,
@nota=notravelagent,
@glactno=ereading
from chalan where chctrlno=@ctrlno
if @@error <> 0 goto e_error

    -- Insert statements for procedure here
if @nota = 0 select @glactno = 1 


set @purchaseac = 
		case	
			when @tttype= 1 then  @purchase2  
			when @tttype= 2 then  @purchase5 
			when @tttype= 3 then  @purchase6 
		end

if @nota = 1
begin
	set @glactno=0
	declare @glac numeric
	declare @tafy numeric
    select @taname=[name], @glac=glactno,@tafy=fy from travelagent where code=@travelagent
    set @glactno=dbo.findactnum(@glac , @tafy,@txndt)
end
else
set @taname = 'No Travel Agent'

if @grno<>0 
begin
exec delgrno @company,@fy,@grno,@txndt,@retval out
if @@error <> 0 goto e_error
end

if isnull(@grno,0)=0 
begin
exec getgrp @txndt,@company,@grno out
end

update chalan set grno_t=@grno where chctrlno=@ctrlno
set @partic = ' TKT Chln.No.' + ltrim(str(@chlno)) + ' ' + rtrim(@olname) + '-' +ltrim(@name) + ' T.A.' + @taname
INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE)
VALUES (@COMPANY,@FY,@purchaseac,@finalamt_t , 'To '+@partic ,'D',@TXNDT,@GRNO,@chlno,'Y')
IF @@ERROR <> 0 GOTO E_ERROR

INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE)
VALUES (@COMPANY,@FY,@glactno,@finalamt_t , 'By '+@partic ,'C',@TXNDT,@GRNO,@chlno,'Y')
IF @@ERROR <> 0 GOTO E_ERROR
commit transaction
return
e_error:
rollback transaction
raiserror('Error in TA voucher posting-2',16,-1)
return
END



GO
/****** Object:  StoredProcedure [dbo].[sptt3]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		RM.Ramanathan
-- Create date: 06 January 2015
-- Description:	Hotel Booking Form
-- =============================================
CREATE PROCEDURE [dbo].[sptt3]
@outlet int ,
@sdt datetime,
@name varchar(50),
@add1 varchar(50),
@add2 varchar(50),
@add3 varchar(50),
@city varchar(50),
@email varchar(50),
@phone varchar(50),
@hotelno  numeric,
@roomtype numeric,
@foodplan numeric,
@guestrate money, 
@hotelrate money,
@ratemode int,
@male int,
@female int,
@child int,
@nor int,
@nod int,
@Terms varchar(2000),
@ChalanNo numeric,
@ctype int,
@ogno numeric,
@ino numeric,
@mailsent char(1),
@nota int=0,
@tacode numeric=0
/* with encryption */
AS
BEGIN
declare @autochln int;
declare @chlno numeric
declare @company int;
declare @fy int;
declare @ctrlno numeric
SET NOCOUNT ON;
if @outlet=0 
	begin 
	print 'The Outlet is not selected'
	goto e_error
	end
begin transaction
if @ogno<>0
  if exists (select * from room where gno=@ogno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end

select @company=company from toutlet where outlet=@outlet
declare @txndt datetime
set @txndt = getdate();
exec getfy @txndt, @company, @fy out
select @autochln=autono3 from toutlet where outlet=@outlet

if @autochln=0
	begin   
		if not exists(select * from toutlet1 where outlet=@outlet and fy=@fy and ctype=3) insert into toutlet1  (outlet,ctype,fy,chlno) values (@outlet,3,@fy,0) 
		update toutlet1 set chlno=isnull(chlno,0)+1 where outlet=@outlet and fy=@fy and ctype=3
		select @chlno=chlno from toutlet1 where outlet=@outlet and fy=@fy and ctype=3
	end
else
	select @chlno=@chalanno 
insert into ta_chalan (chlno,chldate,stype,[name],add1,add2,add3,city,email,phone,outlet,ogno,ino,htl_ctype,htl_hotelno,htl_roomtype,htl_foodplan,htl_sdt,htl_nights,htl_nor,htl_male,htl_female,htl_child,htl_guestrate,htl_hotelrate,htl_terms,mailsent,notravelagent,travelagent) values
(@chlno,getdate(),3,@name,@add1,@add2,@add3,@city,@EMAIL,@phone,@outlet,@ogno,@ino,@ctype,@hotelno,@roomtype,@foodplan,@sdt,@nod,@nor,@male,@female,@child,@guestrate,@hotelrate,@terms,@mailsent,@nota,@tacode)
if @@error <> 0 goto e_error
set @ctrlno=@@identity
select cnt=@ctrlno 

commit transaction
return @ctrlno
--print 'periya karuppa'
e_error:
rollback transaction
raiserror('chalan updation failed',16,-1)
return 0

END

GO
/****** Object:  StoredProcedure [dbo].[sptt3cl]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Rm.Ramanathan, PowerBrainSoftware.com
-- Create date: 14 Jan 2015
-- Description:	Hotel Chalan Closing
-- =============================================
CREATE PROCEDURE [dbo].[sptt3cl]
@ctrlno numeric,@mop int, @crname varchar(50),@crnum varchar(20),@crbank varchar(20),@glactno int,@txndt datetime,@roomcomp char(1),
@basic money,@tax money,@otherchgs money,@Otherdesc varchar(2000), @finalamt money,@finalamt1 money,
@remark varchar(2000),@discount money,@cess money
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @err varchar(1000)
    declare @status char(1)
	declare @rno numeric
	declare @gno numeric
	declare @grno1 numeric
	declare @grno2 numeric
	declare @fy numeric
	declare @company numeric
	declare @outlet numeric
	declare @split char(1)
	declare @ino numeric
	select @status=status,@grno1=isnull(grno1,0),@grno2=isnull(grno2,0),@outlet=outlet,@INO=INO from ta_chalan where ctrlno=@ctrlno
	if @status<>'' begin raiserror('This Chalan is already closed',16,-1) return end

	select @company=company from toutlet where outlet=@outlet
	begin transaction 
	set @err='Error in SPTT3CL' 

	EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
	IF @FY=0 GOTO E_ERROR

	if @grno1=0
		EXEC  GETGRP @TXNDT,@COMPANY,@GRNO1 OUT
		IF @GRNO1=0 GOTO E_ERROR

	if @grno2=0
		EXEC  GETGRP @TXNDT,@COMPANY,@GRNO2 OUT
		IF @GRNO2=0 GOTO E_ERROR


	set @gno=0
	if @mop=2 begin set @gno=@glactno set @glactno=0 end
	if @mop=2 
		if exists (select * from room where gno=@gno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
	set @split='N'
	if @roomcomp='Y' set @split='C'
	update ta_chalan set [status]='C',finaldate=@txndt,mop=@mop,crname=@crname,crnum=@crnum,crbank=@crbank,gno=@gno,grno1=@grno1,grno2=@grno2,split=@split,
    finalamt=@finalamt,finalamt_t=@finalamt1,finalbasic=@basic,finaltax=@tax,otherchgs=@otherchgs,otherchgsdesc=@Otherdesc,remark=@remark,discount=@discount,glac=@glactno,cess=@cess 
	where ctrlno=@ctrlno
	if @@error <> 0 goto e_error
	if @ino=0 EXEC SPTT3VPC @ctrlno
	if @@error <> 0 goto e_error
	exec sptt3vpt @ctrlno
	select ctrlno=@ctrlno
	commit transaction
	return 
	e_error:
	rollback transaction
	raiserror(@err,16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[sptt3crv]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO



/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.sptt1crv    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[sptt3crv] @ctrlno int,@userno int  
/* with encryption */
AS
declare @company int
declare @txndt datetime
declare @fy int
declare @grno int
declare @grno_t int
declare @grno_co1 int
declare @retval int
declare @gno int
declare @chlno numeric
declare @amt money
declare @outlet int
declare @finalamt1 money
declare @finalamt2 money 
declare @m_txndt datetime
declare @transact int
declare @m_split char(1)
declare @sql varchar(5000)
declare @lux char(1)
begin transaction
set @transact=0
--select @lux=lux, @m_split=split,@gno=gno,@company=company,@txndt=finaldate,@grno=grno,@grno_t=grno_t,@grno_co1=grno_co1 from tt_chalan_ol where chctrlno=@ctrlno
select @outlet=outlet,@m_split=split,@txndt=finaldate,@grno=grno1,@grno_t=grno2,@grno_co1=grno3 from ta_chalan where ctrlno=@ctrlno
select @lux=lux,@company=company from toutlet where outlet=@outlet
EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
if @gno<>0
  if exists (select * from room where gno=@gno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end
if @gno<>0 if not exists (select * from room where gno=@gno) begin
  if not exists(select * from servicepermission where userno=@userno and serviceno=185 and outlettype=4 and permission='Y') begin
    print 'You are not permitted to Revive Chalans after check-out'
    goto e_error
  end
  set @transact=1
end
select @m_txndt=finaldate,@outlet=outlet,@chlno=chlno,@amt=finalamt from ta_chalan where ctrlno=@ctrlno
update ta_chalan set finaldate=null,crname='',crnum='',crbank='',grno1=0,grno2=0,finalbasic=0,
otherchgs=0,finalamt=0,finalamt_t=0,
discount=0,glac=0,gno=0,mop=0,status=' ',finaltax=0 where ctrlno=@ctrlno
if @@error <> 0 goto e_error
exec delgrno @company,@fy,@grno,@txndt,@retval out
if @@error <> 0 goto e_error
exec delgrno @company,@fy,@grno_t,@txndt,@retval out
if @@error <> 0 goto e_error
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
end
declare @m_olname varchar(30)
declare @usernm varchar(30)
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @m_olname=[name] from toutlet where outlet=@outlet
insert into acclog (txndt,usernm,narration,txntype) values (getdate(),@usernm, ltrim(rtrim(@M_olname))+ ' Hotel Chalan  No ' + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@AMT)) +  ' Revived',11)
if @@error <> 0 goto e_error
if @transact=1
   begin
   update chalan set ogno=0 where chctrlno=@ctrlno
   set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@amt) + ',0,' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@m_txndt) + ''',''' + @m_split + ''',0,0,''X'',' + str(@userno) + ',''' + @lux + ''''
   exec (@sql)
   if @@error <> 0  goto e_error
   end
insert into acclog(txndt,usernm,narration,txntype) values (getdate(),'',
'Hotel chalan No.' + str(@chlno) +  ' Closed on ' + convert(varchar(20) ,@txndt) +    ' Revived after Closure',
20)
if @@error<>0 goto e_error
commit transaction
return
e_error:
raiserror('deletion of chalan failed',16,-1)
return





GO
/****** Object:  StoredProcedure [dbo].[SPTT3VPC]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[SPTT3VPC]
@ctrlno numeric
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	declare @err varchar(2000)
	declare @chlno numeric
	declare @finalamt money
	declare @finalamt1 money
	declare @ctype money
	declare @finaltax money
	declare @purchase numeric
	declare @sales numeric
	declare @taxac numeric
	declare @cessac numeric
	declare @otherac numeric
	declare @finaldate datetime
	declare @outlet numeric
	declare @company numeric
	declare @txndt datetime
	declare @fy int
	declare @grno1 numeric
	declare @grno2 numeric
	declare @retval numeric
	declare @mop int
	declare @gno int
	declare @dactno numeric
	declare @split char(1)
	declare @myreceivable numeric
	declare @hno_mybook numeric
	declare @selfdeal char(1)
	declare @partic varchar(200)
	declare @crnum varchar(50)
	declare @crname varchar(50)
	declare @crbank varchar(50)
	declare @olname varchar(50)
	declare @amt money
	declare @discount money
	declare @ac numeric
	declare @amttyp char(1)
	declare @otherchgs money
	declare @finalbasic money
	declare @hotelno numeric
	declare @crac numeric
	declare @ino numeric
	declare @glacno numeric
	declare @advance money
	declare @ar_glac numeric 
	declare @cess money
	begin transaction
	set @err= 'Error in SPTT3VPC'
	if exists(select * from setting where g_finaccount=0) goto final

	update ta_chalan set finalamt=isnull(finalbasic,0)+isnull(finaltax,0)+isnull(otherchgs,0)+isnull(cess,0)  where ctrlno=@ctrlno
	select @advance=isnull(advance,0),@finalbasic=finalbasic,@otherchgs=otherchgs, @discount=discount,@chlno=chlno,@split=split,@gno=gno,@mop=mop,@grno1=isnull(grno1,0),@grno2=isnull(grno2,0),@outlet=outlet,@finaldate=finaldate,@finalamt=finalamt,@finalamt1=finalamt_t,@finaltax=finaltax,@cess=cess,@ctype=htl_ctype,@hotelno=htl_hotelno,@ino=ino,@glacno=isnull(glac,0) from ta_chalan where ctrlno=@ctrlno
	if @@error<>0 goto e_error

	select @company=company,@selfdeal=selfdeal,@olname=name,@hno_mybook=hno_mybook,@purchase=purchase3,@sales=sales3,@taxac=ServiceTaxAC,@otherac=otherchgsac,@myreceivable=my_receivable,@ar_glac=advancerecd    from toutlet where outlet = @outlet 
	select @crac=glac from ta_hotel where ctrlno=@hotelno
	set @txndt=@finaldate

	if @ino<>0 return
	exec getfy @txndt,@company,@fy out

	if @ctype<>3
		if isnull(@grno1	,0) <> 0
			exec delgrno @Company,@fy,@grno1,@txndt,@retval out

	if isnull(@grno2,0) <> 0
		exec delgrno 1,@fy,@grno2,@txndt,@retval out

	update ta_chalan set grno1=@grno1,grno2=@grno2 where ctrlno=@ctrlno

	if @mop=2 if exists (select * from room where gno=@gno and hostid <> '' ) begin print 'This room is under checkout operations..service bill entry/closing/modifications not allowed'   goto e_error end


	SELECT @DACTNO = 
		CASE 
		WHEN @MOP = 1 THEN 1           --- CASH SALES
		WHEN @MOP = 2 THEN               -- ROOM SERVICE
			case when @split<>'C' then
				CASE WHEN @COMPANY = 1 THEN @MYRECEIVABLE 
				ELSE  
					CASE
					WHEN @SELFDEAL='Y' THEN  @myRECEIVABLE   ELSE @HNO_MYBOOK
					END
				END 
			else 26 
			end
		WHEN @MOP = 3 THEN 23  -- BILL RECEIVABLE
		WHEN @MOP = 4 THEN @glacno  -- GL A/C
		WHEN @MOP = 5 THEN 22   -- CREDIT CARD 
		WHEN @MOP = 6 THEN 0   -- COMPLEMENTARTY No vouching 
		END

	set @partic = 'Chln.No ' + ltrim(str(@chlno))+ ' ' + @olname
	if @mop=5
		set @partic = @crnum + ' ' + @crname + ' ' + @partic
	if @mop=6 goto Final

	if @ctype<>3
		begin
		if @finalamt-@discount-@advance <> 0
		if @finalamt-@discount-@advance > 0
				BEGIN
				set @amt= @finalamt - @discount - @advance
				set @ac = @dactno
				set @amttyp = 'D'
				INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
				VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO1,@ctrlno,'Y','HTL')
				IF @@ERROR <> 0 GOTO E_ERROR
				END
			else
				BEGIN
				set @amt= abs(@finalamt - @discount - @advance)
				set @ac = @dactno
				set @amttyp = 'C'
				INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
				VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO1,@ctrlno,'Y','HTL')
				IF @@ERROR <> 0 GOTO E_ERROR
				END
		if @advance<>0
			BEGIN
			set @amt= @advance
			set @ac = @ar_glac
			set @amttyp = 'D'
			INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
			VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO1,@ctrlno,'Y','HTL')
			IF @@ERROR <> 0 GOTO E_ERROR
			END
			

		if @discount > 0
			BEGIN
			set @amt= @finalamt
			set @ac = 62
			set @amttyp = 'D'
			INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
			VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO1,@ctrlno,'Y','HTL')
			IF @@ERROR <> 0 GOTO E_ERROR
			END
		if @finalbasic > 0
			BEGIN
			set @amt= @finalbasic
			set @ac = @sales
			set @amttyp = 'C'
			INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
			VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO1,@ctrlno,'Y','HTL')
			IF @@ERROR <> 0 GOTO E_ERROR
			END

		if @otherchgs > 0
			BEGIN
			set @amt= @otherchgs
			set @ac = @otherac 
			set @amttyp = 'C'
			INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
			VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO1,@ctrlno,'Y','HTL')
			IF @@ERROR <> 0 GOTO E_ERROR
			END
		if @finaltax > 0
			BEGIN
			set @amt= @finaltax
			set @ac = @taxac 
			set @amttyp = 'C'
			INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
			VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO1,@ctrlno,'Y','HTL')
			IF @@ERROR <> 0 GOTO E_ERROR
			END
		if @cess > 0
			BEGIN
			set @amt= @cess
			set @ac = 91
			set @amttyp = 'C'
			INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
			VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO1,@ctrlno,'Y','HTL')
			IF @@ERROR <> 0 GOTO E_ERROR
			END

	end

	--if @finalamt1 > 0
	--	BEGIN
	--	set @amt= @finalamt1
	--	set @ac = @purchase
	--	set @amttyp = 'D'
	--	INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
	--	VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO2,@ctrlno,'Y','HTL')
	--	IF @@ERROR <> 0 GOTO E_ERROR
	--	END
	--if @finalamt1 > 0
	--	BEGIN
	--	set @amt= @finalamt1
	--	set @ac = @crac 
	--	set @amttyp = 'C'
	--	INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
	--	VALUES (@COMPANY,@FY, @ac  ,@amt , case when @amttyp='C' then 'By ' else 'To ' end+@partic   ,@amttyp,@TXNDT,@GRNO2,@ctrlno,'Y','HTL')
	--	IF @@ERROR <> 0 GOTO E_ERROR
	--	END
Final:
commit transaction
return
E_ERROR:
rollback transaction
raiserror(@err,16,-1)
return
END

GO
/****** Object:  StoredProcedure [dbo].[sptt3vpt]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sptt3vpt]
@ctrlno numeric
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @outlet int
	declare @txndt datetime 
	declare @purchase3 numeric
	declare @company numeric
	declare @olname varchar(50)
	declare @fy numeric
	declare @chlno numeric
	declare @name varchar(50)
	declare @grno numeric
	declare @finalamt_t money
	declare @hotelno numeric
	declare @glactno numeric
	declare @retval numeric
	declare @partic varchar(200)
	declare @hotelname varchar(50)
	declare @err varchar(500)
	declare @ar_glac numeric
	declare @advancepaid money
	declare @amt money 
	declare @nota tinyint
	declare @tacode numeric
	declare @hotelGLAC numeric
	declare @taglac numeric

	begin transaction
	set @err = 'Error in posting Hotel Voucher'
	select @nota=notravelagent,@tacode=travelagent,@outlet=outlet,@txndt=finaldate,@hotelno=htl_hotelno,@name=name from ta_chalan where ctrlno=@ctrlno

	select @purchase3=purchase3,@company=company,@olname=[name],@ar_GLAC=advancepaid from toutlet where outlet=@outlet
	
	exec getfy @txndt,@company,@fy out
select
@chlno=chlno,
@name=name,
@finalamt_t=finalamt_t,
@grno=grno2,
@hotelno=htl_hotelno,
@advancepaid=isnull(advance_t,0)
from ta_chalan where ctrlno=@ctrlno
if @@error <> 0 goto e_error

select @hotelglac=glac,@hotelname=hotelname from ta_hotel where ctrlno=@hotelno  


if @nota=0
	set @glactno=@hotelglac
else
	begin
		select @taglac=glactno from travelagent where code=@tacode
		set @glactno=@taglac
	end

--	set @err = 'periya' + convert(varchar(20),@glactno) goto e_error

set @amt = @finalamt_t - @advancepaid 

if @grno<>0 
begin
exec delgrno @company,@fy,@grno,@txndt,@retval out
if @@error <> 0 goto e_error
end

if isnull(@grno,0)=0 
begin
exec getgrp @txndt,@company,@grno out
end

update ta_chalan set grno2=@grno where ctrlno=@ctrlno

set @partic = ' Hotel Chln.No.' + ltrim(str(@chlno)) + ' ' + rtrim(@olname) + '-' +ltrim(@name) + ' T.A.' + @hotelname
INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
VALUES (@COMPANY,@FY,@purchase3,@finalamt_t , 'To '+@partic ,'D',@TXNDT,@GRNO,@chlno,'Y','HTL')
IF @@ERROR <> 0 GOTO E_ERROR

if @amt > 0 
	begin
		INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		VALUES (@COMPANY,@FY,@glactno,@amt , 'By '+@partic ,'C',@TXNDT,@GRNO,@chlno,'Y','HTL')
		IF @@ERROR <> 0 GOTO E_ERROR
	end
else
	if @amt < 0 
	begin
		INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE,txntype)
		VALUES (@COMPANY,@FY,@glactno,@amt , 'To '+@partic ,'D',@TXNDT,@GRNO,@chlno,'Y','HTL')
		IF @@ERROR <> 0 GOTO E_ERROR
	end

if @advancepaid<>0 
	begin
		INSERT VOUCHERS (COMPANY,FY,ACTNUM,AMT,PARTIC,AMTTYP,TXNDT,GRNO,FDRSNO,CHANNELCODE)
		VALUES (@COMPANY,@FY,@ar_GLAC,@advancepaid , 'By '+@partic ,'C',@TXNDT,@GRNO,@chlno,'Y')
		IF @@ERROR <> 0 GOTO E_ERROR
	end
	

commit transaction
return
e_error:
rollback transaction
raiserror(@err,16,-1)
return
END

GO
/****** Object:  StoredProcedure [dbo].[spttap]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spttap    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spttap] @txndt datetime,
@ctrlno int,@amt money,@mop int,
@crname varchar(50),@CRNUM VARCHAR(20),@glactnum int,@usernm varchar(30),@tttype int=1 
/* with encryption */
as
declare @company int
declare @outlet int
declare @ano int
declare @m_year int
declare @m_bofy  datetime
declare @advrno int
declare @fy int
declare @grno int
declare @sql varchar(500)
declare @rno int
declare @actnum int
declare @partic varchar(200)
declare @chlno int
declare @ar_glac int
declare @status char(1)
declare @ta int
declare @nta int

declare @myreceivable numeric
declare @hno_mybook numeric
declare @selfdeal char(1)
declare @olname varchar(50)
declare @purchase numeric
declare @sales numeric
declare @tttypenarration varchar(200)
begin transaction

set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)


if @tttype<>3 
	begin
		select @nta=notravelagent,@ta=travelagent,@status=status,@chlno=chlno,@outlet=outlet,@ar_glac=advancepaid,@company=company  from tt_chalan_ol where chctrlno=@ctrlno
		if @status<> ' ' goto e_error
		if @ta=0 goto e_error
		if @nta=0 goto e_error
	end
else
	begin
		select @nta=notravelagent,@ta=travelagent,@status=status,@chlno=chlno,@outlet=outlet  from ta_chalan where ctrlno=@ctrlno
		select @company=company,@selfdeal=selfdeal,@olname=name,@hno_mybook=hno_mybook,@purchase=purchase3,@sales=sales3,
		@ar_glac=advancepaid,
		--@taxac=ServiceTaxAC,@otherac=otherchgsac,
		@myreceivable=my_receivable  from toutlet where outlet = @outlet 
		if @status<> ' ' goto e_error
	end


EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
EXEC  GETGRP @TXNDT,@COMPANY,@GRNO OUT
IF @GRNO=0 GOTO E_ERROR
update toutlet  set ano_t = case when adate_t <> @m_bofy then 1 else ano_t + 1 end ,adate_t = case  when adate_t <> @m_bofy then @m_bofy else adate_t end where outlet = @outlet
if @@error <> 0 goto e_error
select @ano=ano_t from toutlet where outlet=@outlet
insert into ttadvance (chctrlno,txndt,ano,mop,crcardname,grno,amount,glactnum,atype,usernm,tmstmp,tttype) values (@ctrlno,@txndt,@ano,@mop,@crname,@grno,@amt,@glactnum,'P',@usernm,substring(convert(char(5),getdate(),8),1,5),@tttype)
if @@error <> 0 goto e_error

if @tttype=3 
	update ta_chalan set advance_t=isnull(advance_t,0)+@amt where ctrlno=@ctrlno
else
	update chalan set advance_t=isnull(advance_t,0)+@amt where chctrlno=@ctrlno

set @tttypenarration = 'Adv.Paid-' + case when @tttype=3 then 'Hotel Booking' else 'CAB/Ticket Chalan' end
select @actnum= case 
  when @mop=1 then 1
  when @mop=2 then @glactnum
  when @mop=3 then @glactnum
                           end
--- 1 CASH 2  CHEQUE 3 GL A/C
select @partic = case
  when @mop=1 then 'To ' + @tttypenarration + ' Chl.No. ' + ltrim(str(@chlno)) + ' R.No.' + ltrim(str(@ano))
  when @mop=2 then 'To ' + ltrim(@crnUM) + ' ' + @tttypenarration +  ' Chl.No '+ ltrim(str(@chlno))+  '-Adv-R.No.' + ltrim(str(@ano))
  when @mop=3 then 'To ' + ltrim(@crname) + ' ' + @tttypenarration + ' Chl.No '+ ltrim(str(@chlno))+  '-Adv-R.No.' + ltrim(str(@ano))
  end
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
values (@company, @fy,  @actnum ,@amt , 'By ' + @partic  , 'C', @txndt , @grno ,'Y', @ano,'HTL')
if @@error <> 0 goto e_error
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno,txntype) 
values (@company, @fy,  @ar_glac ,@amt ,'To ' + @partic + ltrim(str(@ano)) , 'D', @txndt , @grno ,'Y', @ano,'HTL')
if @@error <> 0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('chalan  Advane failed - Possibly the Chalan would have been alreay closed/No travel agent',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spttar]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spttar    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spttar]  @txndt datetime,
@ctrlno int,@amt money,@mop int,
@crname varchar(50),@crnum varchar(20),@crbank varchar(20),@glactnum int,@usernm varchar(30) ,@tttype int=1
/* with encryption */
as
declare @company int
declare @outlet int
declare @ano int
declare @m_year int
declare @m_bofy  datetime
declare @advrno int
declare @fy int
declare @grno int
declare @sql varchar(500)
declare @rno int
declare @actnum int
declare @partic varchar(200)
declare @chlno int
declare @ar_glac int
declare @olname varchar(50)
declare @narad varchar(200)

begin transaction

set @m_bofy = dbo.getbofy(@txndt)
set @m_year = datepart(yy,@m_bofy)

if @tttype=3 
begin
select @chlno=chlno,@outlet=outlet  from ta_chalan where ctrlno=@ctrlno
select @olname=name,@ar_glac=advancerecd,@company=company from toutlet where outlet=@outlet
end
else
begin
select @olname=olname,@ar_glac=advancerecd,@chlno=chlno,@company=company,@outlet=outlet  from tt_chalan_ol where chctrlno=@ctrlno
end


EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
IF @FY=0 GOTO E_ERROR
EXEC  GETGRP @TXNDT,@COMPANY,@GRNO OUT
IF @GRNO=0 GOTO E_ERROR
update toutlet  set ano = case when adate <> @m_bofy then 1 else ano + 1 end ,adate = case  when adate <> @m_bofy then @m_bofy else adate end where outlet = @outlet
if @@error <> 0 goto e_error
select @ano=ano from toutlet where outlet=@outlet
insert into ttadvance (chctrlno,txndt,ano,mop,crcardnum,crcardname,crcardbank,grno,amount,glactnum,atype,usernm,tmstmp,tttype) values (@ctrlno,@txndt,@ano,@mop,@crnum,@crname,@crbank,@grno,@amt,@glactnum,'R',@usernm,substring(convert(char(5),getdate(),8),1,5),@tttype )
if @@error <> 0 goto e_error
--update chalan set advance=advance+@amt where chctrlno=@ctrlno
--if @@error <> 0 goto e_error
select @actnum= case 
  when @mop=1 then 1
  when @mop=2 then 22
  when @mop=3 then 46
  when @mop=4 then @glactnum
                                     end
if @tttype=3 
set @narad = 'Adv.Recd-Hotel Booking-'
else
set @narad = 'Adv.Recd-Cab/Ticket Booking-'
select @partic = case
  when @mop=1 then  @narad + ' Chl.No. ' + ltrim(str(@chlno)) + ' Adv.R.No.' + ltrim(str(@ano)) + ' ' + @olname
  when @mop=2 then  @narad + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crbank) + 'Chl.No '+ ltrim(str(@chlno))+  '-Adv-R.No.' + ltrim(str(@ano))+ ' ' + @olname
  when @mop=3 then  @narad + ltrim(@crnum) + ' ' + ltrim(@crname) + ' ' + ltrim(@crbank) + 'Chl.No '+ ltrim(str(@chlno))+  '-Adv-R.No.' + ltrim(str(@ano))+ ' ' + @olname
  when @mop=4 then  @narad + ltrim(@crname) + ' ' + 'Chl.No '+ ltrim(str(@chlno))+  '-Adv-R.No.' + ltrim(str(@ano))+ ' ' + @olname
  end
  --print @partic goto e_error
if @tttype=3 
update ta_chalan set advance=isnull(advance,0)+@amt where ctrlno=@ctrlno
else
update chalan set advance=isnull(advance,0)+@amt where chctrlno=@ctrlno

insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
values (@company, @fy,  @actnum ,@amt , 'To ' +  @partic  , 'D', @txndt , @grno ,'Y', @ano)
if @@error <> 0 goto e_error
insert  vouchers (company,fy,actnum,amt,partic ,amttyp,txndt,grno,channelcode,refno) 
values (@company, @fy,  @ar_glac ,@amt ,'By ' + @partic , 'C', @txndt , @grno ,'Y', @ano)
if @@error <> 0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('chalan  Advane failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spttdc]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spttdc    Script Date: 15/02/2004 6:23:01 PM ******/
/* Programed during 2003 feb to mar*/
CREATE PROCEDURE [dbo].[spttdc] @ctrlno int  , @tttype int=1
/* with encryption */
AS
declare @grno int
declare @company int
declare @fy int
declare @txndt datetime
declare @retval int
declare @olname varchar(30)
declare @usernm varchar(30)
declare @amt money
declare @chlno numeric
declare @gno numeric
declare @status char
declare @stype int
begin transaction
-----------this was in amt column earlier ---finalamt+finaltax+finalbata-discount
if @tttype<>3
	begin
	select @stype=stype,@status=status, @gno=gno,@chlno=chlno, @amt= finalamt+finaltax+finalbata-discount+otherchgs,@olname=olname,@grno=grno_t,@company=company,@txndt=chldate  from tt_chalan_ol where chctrlno=@ctrlno
	--select @gno=gno,@company=company,@txndt=finaldate,@grno=grno,@grno_t=grno_t,@grno_co1=grno_co1 from tt_chalan_ol where chctrlno=@ctrlno
	if @status<>' ' or @gno<> 0 begin print 'The chaln is reversed already...No deletion allowed' goto e_error end
	if @grno<>0 
	begin
	EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
	IF @FY=0 GOTO E_ERROR
	if @grno<>0 exec delgrno @company,@fy,@grno,@txndt,@retval out
	if @retval<>0  goto e_error
	end
	delete from chalan where chctrlno = @ctrlno
	if @@error <> 0 goto e_error
	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
	insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@olname))+ case when @stype=1 then ' TT Chalan  No ' else 'TKT Chalan No' end + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@AMT)) +  ' Deleted ', case when @stype=1 then 8 else 16 end,@gno)
	if @@error<>0 goto e_error
	end
else
	begin
	declare @outlet int 
	select @stype=stype,@status=status, @gno=isnull(gno,0),@chlno=chlno, @amt= isnull(finalamt,0) + isnull(finaltax,0) - isnull(discount,0)+isnull(otherchgs,0),@grno=grno1,@txndt=chldate,@outlet=outlet   from ta_chalan where ctrlno=@ctrlno
	select @company=company,@olname=[name] from toutlet where outlet=@outlet
	if @status<>' ' or @gno<> 0 begin print 'The chaln is reversed already...No deletion allowed' goto e_error end
	if @grno<>0 
	begin
	EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
	IF @FY=0 GOTO E_ERROR
	if @grno<>0 exec delgrno @company,@fy,@grno,@txndt,@retval out
	if @retval<>0  goto e_error
	end

	delete from TA_chalan where ctrlno = @ctrlno
	if @@error <> 0 goto e_error
	--print 'Periya***** ' + convert(varchar(20),@amt) goto e_error
	set @usernm = dbo.getusername()
	insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@olname))+ case when @stype=1 then ' TT Chalan  No ' else 'TKT Chalan No' end + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,6) + ' Amount ' + ltrim(convert(varchar(20),@AMT)) +  ' Deleted ', case when @stype=1 then 8 else 16 end,@gno)
	if @@error<>0 goto e_error
		
	end
commit transaction
return
e_error:
rollback transaction
raiserror('chalan deletion failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[sptted1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[sptted1] @chctrlno numeric,@basicamt money, @tax money,@bata money,@discount money,@otherchgs money,
@basicamt_t money,@bata_t money,@ereading varchar(20),@finaldate datetime,@cess money
/* with encryption */
AS
declare @company int
declare @fy int
declare @grno numeric
declare @grno_t numeric
declare @grno_co1 numeric
declare @retval int
declare @txndt datetime
declare @mop int
declare @split char(1)
declare @olname varchar(50)
declare @gno numeric
declare @stype int
declare @o_finalbasic_t money
declare @o_finalbata_t money
declare @nota int
declare @ta numeric
declare @myreceivable numeric
declare @chlno numeric
declare @selfdeal char(1)
declare @hno_mybook numeric
declare @hno_htlbook numeric
declare @htl_purchase numeric
declare @htl_sales numeric
declare @htl_receivable numeric
declare @purchase money
declare @sales money
declare @advrecd money
declare @advpaid money
declare @staxrate money
declare @advance money
declare @advance_t money
declare @tttype int
declare @dactno numeric
declare @amt money
declare @partic varchar(200)
declare @glactno numeric
declare @crnum varchar(20)
declare @crname varchar(40)
declare @crbank varchar(20)
declare @change1 int
declare @change2 int
declare @finalamt money
declare @finalamt_t money
declare @lux char(1)
declare @newamt money
declare @oldamt money
declare @otherchgsac numeric
declare @bataac numeric
declare @outlet numeric
declare @guestname varchar(200) 
--print 'periya'
set @change1 = 0
set @change2 = 0
begin transaction
select @lux=lux,@o_finalbasic_t=finalbasic_t,@o_finalbata_t=finalbata_t,@mop=mop, @split=split,@gno=gno,@company=company,@txndt=finaldate,@grno=grno,@grno_t=grno_t,@grno_co1=grno_co1,@stype=stype, @olname=olname,
@nota=notravelagent,@ta=glactno,@olname=olname,@myreceivable=my_receivable,@company=company,@chlno=chlno,@selfdeal=selfdeal,@hno_mybook=hno_mybook,@hno_htlbook=hno_htlbook,@htl_purchase=htl_purchase,@htl_sales=htl_sales,
@htl_receivable=htl_receivable,@purchase=purchase1,@sales=sales1,@advrecd=advancerecd,@advpaid=advancepaid,@staxrate=staxrate,
@advance=advance,@advance_t=advance_t,@crnum=cc_num,@crname=cc_name,@crbank=cc_bank,@outlet=outlet,@lux=lux,
@tttype=tttype,@glactno=glactnum,@otherchgsac=otherchgsac,@bataac=bataac,@guestname=name  from tt_chalan_ol where chctrlno=@chctrlno
set @oldamt=0
--if @mop = 2
select @oldamt=finalbasic+finaltax+finalbata+otherchgs-discount-advance from chalan where chctrlno=@chctrlno
set @newamt = 0

exec getfy @txndt,@company,@fy out
if @fy=0 goto e_error

if not exists(select * from chalan where chctrlno=@chctrlno and datediff(s,'1/1/90',finaldate)=datediff(d,'1/1/90',@txndt) and
finalBASIC=@basicamt and finaltax=@tax and finalbata=@bata and discount=@discount and otherchgs=@otherchgs) begin
	exec delgrno @company,@fy,@grno,@txndt,@retval out
	if @@error <> 0 goto e_error
	set @change1=1
end
if @o_finalbasic_t <> @basicamt_t or @o_finalbata_t <> @bata_t  begin
   exec delgrno @company,@fy,@grno_t,@txndt,@retval out
   if @@error <> 0 goto e_error
   set @change2 = 1
end
if @grno_co1 <> 0 and @mop=2 begin
    exec getfy @txndt,1,@fy out
    if @fy=0 goto e_error
    exec delgrno 1,@fy,@grno_co1,@txndt,@retval out
end
--EXEC  GETFY @TXNDT,@COMPANY,@FY OUT
exec getfy @txndt,@company,@fy out
if @fy=0 goto e_error

set @finalamt = @basicamt+@tax+@bata+@otherchgs-@discount 
set @finalamt_t = @basicamt_t+@bata_t
update chalan set finaldate=@txndt,finalBASIC=@basicamt,finaltax=@tax,finalbata=@bata,discount=@discount,
finalBASIC_t=@basicamt_t,finalbata_t=@bata_t,ereading=@ereading,finalamt=@finalamt,finalamt_t=@finalamt_t,
otherchgs=@otherchgs ,cess = @cess
where chctrlno=@chctrlno
if @@error <> 0 goto e_error

select @newamt=finalbasic+finaltax+finalbata+otherchgs-discount-advance from chalan where chctrlno=@chctrlno


if @mop=2
begin
declare @userno numeric
declare @sql varchar(5000)
select @userno=userno from comuser where [host_id]=host_id()
set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@oldamt) + ','  + convert(varchar(20),@finalamt) +  ',' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@txndt) + ''',''' + @split + ''',0,' + convert(varchar(20),@finalamt) + ',''X'',' + str(@userno) + ',''' + @lux + ''''
exec(@sql)
end 

if @newamt<>@oldamt 
	begin
	declare @usernm varchar(100)
	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
	insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm ,'TT Chalan Edited - Guest Name ' + rtrim(@guestname)  +  ' Old  Amount - ' + convert(varchar(20),@oldamt)+ ' New  Amount - ' + convert(varchar(20),@newamt)+ ' Chalan No. ' + ltrim(str(@chctrlno))+ ' dt ' + convert(varchar(20),@txndt,6)  ,51,@gno)
	end
exec sptt1vpc @chctrlno
exec sptt1vpt @chctrlno


final:
commit transaction
return
e_error:
rollback transaction
raiserror('Error in sptted1',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spttSplit]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spttSplit] @ctrlno numeric,@split char(1)  
/* with encryption */
AS
begin transaction
declare @osplit char(1)
declare @mop int
declare @gno numeric
declare @amt money
declare @basicamt money
declare @tax money
declare @bata money
declare @otherchgs money
declare @advance money
declare @discount money
declare @stype int
declare @chlno numeric
declare @txndt datetime
declare @finalamt money
declare @finaltax money
declare @serchgs money
declare @company numeric
declare @outlet numeric
declare @userno numeric
declare @lux char(1)
declare @sql varchar(6000)

select @stype=stype,@osplit=split,@mop=mop,@gno=gno from chalan where chctrlno=@ctrlno

select @chlno=chlno,@txndt=finaldate,@basicamt=finalbasic,@tax=finaltax,
@bata=finalbata,@discount=discount,
@mop=mop,@gno=gno,
@finalamt=finalamt,@osplit=split,@otherchgs=otherchgs ,@advance=advance,
@finaltax=finaltax,@serchgs=finalbata,
@outlet = outlet
from chalan where chctrlno=@ctrlno

select @lux=lux,@company=company from toutlet where outlet=@outlet

update chalan set split=@split where chctrlno=@ctrlno


if @stype=1
	begin
	exec sptt1vpc @ctrlno
	set @amt = @basicamt+@finaltax+@bata+@otherchgs-@advance-@discount 
	end
else
	begin
	exec sptt2vpc @ctrlno
	set @amt=@basicamt+@finaltax+@serchgs-@discount
	end
select @userno=userno from comuser where [host_id]=host_id()
if @mop=2
	begin
	if @split='N'
		set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@amt) + ',0,' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@txndt) + ''',''' + @osplit + ''',0,' + convert(varchar(20),@amt) + ',''' + @split + ''',' + str(@userno) + ',''' + @lux + ''''
	else
		set @sql = 'exec delcout ' + str(@gno) + ',' + convert(varchar(20),@amt) + ',0,' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@txndt) + ''',''' + @osplit + ''',0,' + convert(varchar(20),@amt) + ',''' + @split + ''','+str(@userno)+','''+@lux  +''''
--print @sql goto e_error
	exec(@sql)
	end
insert into  acclog (narration,txntype,gno) values ('TT Chalan Split changed to ' + case when @split='N' then 'Main' else 'Complementary' end + ' from ' + case when @osplit='N' then 'Main' else 'Complementary' end,20,@gno)

commit transaction
return
e_error:
rollback transaction
raiserror('Error in split setting for TT chalan',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spudob]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spudob    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spudob] @t1 as varchar(50),@company int, @fy int
/* with encryption */
as
declare @tdno int
declare @asset char(1)
declare @onac char(1)
declare @cgp_actno int
declare @balance money
declare @amttype char(1)
declare @sql varchar(1000)
declare @opgbal money
declare @balance1 money
declare @cgp_desc varchar(100)
declare @txndt datetime
declare @txndt1 datetime
begin transaction
select @txndt = dateadd(dd,-1,[date]) from finyear where fy= @fy
set @txndt1 = dateadd(dd,1,@txndt)
set @sql = 'declare tempcur cursor for select cgp_actno,balance,amttyp from ' + @t1
exec (@sql)
open tempcur
fetch next from tempcur into @cgp_actno,@balance,@amttype
while  @@fetch_status=0 
begin
select @cgp_desc=cgp_desc,@opgbal=opgbal,@tdno=tdno,@asset=asset,@onac=onac from glprimld where company = @company and @fy = fy and cgp_actno = @cgp_actno
if @@error<>0 goto e_error
if @amttype='D'
   set @balance1= @balance*-1
else
   set @balance1 = @balance
if @opgbal<>@balance1 begin
   update glprimld set opgbal=@balance1 where company = @company and @fy = fy and cgp_actno = @cgp_actno
   if @@error<>0 goto e_error
   exec fillbal   @txndt1,@company,@cgp_actno,1   
   if @@error<>0 goto e_error
    surpass:
end
fetch next from tempcur into @cgp_actno,@balance,@amttype
end
close tempcur
deallocate tempcur
set @sql = 'drop table ' + @t1
exec(@sql)
commit transaction
return
e_error:
--close tempcur
--deallocate tempcur
rollback transaction
raiserror('ob failed',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[spupdd]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spupdd    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spupdd] @t1 varchar(50),@t2 varchar(50),@t3 varchar(50),@t4 varchar(50),@t5 varchar(50),
@t6 varchar(50),@t7 varchar(50),@t8 varchar(50),@t9 varchar(50),@t10 varchar(50) ,@t11 money,@dom datetime,
@dob datetime ,@GSTIN CHAR(15) 
/* with encryption */
as
declare @code int
declare @err varchar(2000)
declare @dobm int
declare @dobd int
declare @domm int
declare @domd int
begin transaction

set @dobm = 0
set @dobd = 0
set @domm = 0
set @domd = 0

if year(@dob) <> 1900
begin
set @dobm = month(@dob)
set @dobd = day(@dob)
end

if year(@dom) <> 1900
begin
set @domm = month(@dom)
set @domd = day(@dom)
end



insert into doorcust (name,add1,add2,area,phone,discountrate,ltxndt,marriageday,birthday,dobm,dobd,domm,domd,GSTIN) values (@t1,@t2,@t3,@t4,@t5,@t11,getdate(),@dom,@dob,@dobm,@dobd,@domm,@domd,@GSTIN)
if @@error <> 0 goto e_error
select @code= @@identity
set @err = 'Error'
--if @t1<>'NIL'
--begin
--insert into doorcust_id values(@t1,@code)
--if @@error <> 0 goto e_error
--end
if @t1<>'NIL'
begin
set @err =@t1
insert into doorcust_id values(@t1,@code)
if @@error <> 0 goto e_error
end
if @t6<>'NIL'
begin
set @err =@t6
insert into doorcust_id values(@t6,@code)
if @@error <> 0 goto e_error
end
if @t7<>'NIL'
begin
set @err =@t7
insert into doorcust_id values( @t7,@code)
if @@error <> 0 goto e_error
end
if @t8<>'NIL'
begin
set @err =@t8
insert into doorcust_id values (@t8,@code)
if @@error <> 0 goto e_error
end
if @t9<>'NIL'
begin
set @err =@t9
insert into doorcust_id values (@t9,@code)
if @@error <> 0 goto e_error
end
if @t10<>'NIL'
begin
set @err =@t10
insert into doorcust_id values (@t10,@code)
if @@error <> 0 goto e_error
end
commit transaction
return
e_error:
set @err = 'Error : ' + @err
rollback transaction
raiserror(@err,16,-1)




GO
/****** Object:  StoredProcedure [dbo].[spupdde]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spupdd    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[spupdde] @code numeric,@t1 varchar(50),@t2 varchar(50),@t3 varchar(50),@t4 varchar(50),@t5 varchar(50),@t6 varchar(50),@t7 varchar(50),@t8 varchar(50),@t9 varchar(50),@t10 varchar(50) ,@t11 money,@dom datetime, @dob datetime,@GSTIN CHAR(15)  
/* with encryption */
as
begin transaction
declare @err varchar(200)
set @err = 'Error : '
if not exists (select * from doorcust where ctrlno=@code) 
begin
print 'Customer not found in database'
goto e_error
end

update doorcust set 
name=@t1,
add1=@t2,
add2=@t3,
area=@t4,
phone=@t5,
discountrate=@t11,
marriageday=@dom,
birthday=@dob,
GSTIN=@GSTIN
where ctrlno=@code
if @@error <> 0 goto e_error

delete from doorcust_id where ctrlno=@code

if @t1<>'NIL'
begin
set @err =@t1
insert into doorcust_id values(@t1,@code)
if @@error <> 0 goto e_error
end

if @t6<>'NIL'
begin
set @err =@t6
insert into doorcust_id values(@t6,@code)
if @@error <> 0 goto e_error
end

if @t7<>'NIL'
begin
set @err =@t7
insert into doorcust_id values( @t7,@code)
if @@error <> 0 goto e_error
end

if @t8<>'NIL'
begin
set @err =@t8
insert into doorcust_id values (@t8,@code)
if @@error <> 0 goto e_error
end

if @t9<>'NIL'
begin
set @err =@t9
insert into doorcust_id values (@t9,@code)
if @@error <> 0 goto e_error
end

if @t10<>'NIL'
begin
set @err =@t10
insert into doorcust_id values (@t10,@code)
if @@error <> 0 goto e_error
end

commit transaction
return
e_error:
rollback transaction
raiserror(@err,16,-1)




GO
/****** Object:  StoredProcedure [dbo].[spupdusr]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.spupdusr    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE   PROCEDURE [dbo].[spupdusr] @user varchar(50),@version char(20),@ud int=0,@userno numeric=0 ,@inet int = 0 ,@sessionid varchar(200) = '',@hostname varchar(200)
/* with encryption */
AS
	declare @err varchar(5000)
	declare @g_UC tinyint
	select top 1 @g_uc=g_uc from setting 
if @hostname='RCPLTERMINAL' begin raiserror('RCPLTERMINAL',16,-1) return end
if @hostname='' begin raiserror('blank system name',16,-1) return end
if  @ud=0
	begin
		if exists(select top 1 * from comuser where computer=@hostname and inet=@inet and sessionid=@sessionid and [host_id]=host_id())
				update comuser set [host_id]=host_id(),usernm=@user,version_no=@version,logintime=getdate(),userno=@userno,control='Y'  where computer=@hostname and sessionid=@sessionid and [host_id]=host_id()
			else
				insert into comuser (computer,usernm,version_no,userno,inet,sessionid,[host_id],control) values ( @hostname,@user,@version,@userno,@inet,@sessionid,host_id(),'Y')
		declare @x int 
	end
else
	begin
		if exists(select top 1 * from comuser where computer=@hostname and sessionid=@sessionid and [host_id]=host_id() and inet=@inet)
			delete from comuser where computer=@hostname and inet=@inet and sessionid=@sessionid  and [host_id]=host_id()
	end



GO
/****** Object:  StoredProcedure [dbo].[spupsgla]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[spupsgla] @supp numeric,@actno numeric,@fy int 
/* with encryption */
AS
set nocount on 
declare @cnt  int
declare @err varchar(6000) 
if exists (select * from suppglac where supplier=@supp and fy=@fy)
begin
--set @err = 'select count(*) cnt  from suppglac where supplier=' + str(@supp) + '  and fy= ' + str(@fy)
--RAISERROR(@ERR,16,-1) RETURN 
  if (select count(*) cnt  from suppglac where supplier=@supp and fy=@fy) > 1 
    begin
	    delete from suppglac where fy=@fy and supplier=@supp
		insert into suppglac (supplier,fy,glactno)  values (@supp,@fy,@actno)
		set @cnt=1
	end
	else
	begin
		--update suppglac set glactno=@actno where fy=@fy and supplier=@supp
		update suppglac set glactno=@actno where supplier=@supp
		set @cnt=0
	end
end
else
begin
insert into suppglac (supplier,fy,glactno)  values (@supp,@fy,@actno)
set @cnt=1
end
select cnt=@cnt
return




GO
/****** Object:  StoredProcedure [dbo].[StaahRateUpdate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		PowerBrain Software (India)
-- Create date: 10-01-2014
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[StaahRateUpdate]
@staahrenttype varchar(100),
@sdt datetime,@edt datetime
/* with encryption */
AS
BEGIN
SET NOCOUNT ON;
declare @renttype int
declare @rentTable int
declare @tedt datetime
declare @doit as int
declare @csdt as datetime
declare @staahroomtype as varchar(50)
declare @roomtype int
declare @rno int
declare @rate decimal
declare @ssdt varchar(20)
declare @sedt varchar(20)

set @csdt = @sdt

select @renttype=ctrlno from renttype where staahcode=@staahrenttype

set @doit = 0
while @doit = 0
	begin
	select @renttable=ctrlno,@tedt=edt from rentperiod where rtype=@renttype and 
	datediff(d,'1/1/2000',@csdt)
	between datediff(d,'1/1/2000',sdt) and datediff(d,'1/1/2000',edt)
    ---print convert(varchar(20),@edt) + ' ' + convert(varchar(20),@tedt)
	if datediff(d,@edt,@tedt) > 0 
	begin
	set @tedt = @edt
	set @doit= 1 
    end

	declare c1 cursor for 
	select a.roomwing roomtype,(select top 1 rno from room b where b.roomwing=a.roomwing) rno from 
	(select distinct roomwing  from room ) a 
	open c1 
	fetch next from c1 into @roomtype,@rno
	while @@fetch_status=0

	begin
		select @rate=rent1 from rentmaster where rtypectrlno=@renttable and rno=@Rno
		select @staahroomtype=staahcode from wings where wingno=@roomtype
        if @staahroomtype <> '' 
        begin
			if exists (select * from staahrates where 
			datediff(d,@csdt,sdt)=0 and datediff(d,@tedt,edt)=0 
			and roomtype=@staahroomtype and renttype=@staahrenttype)
			begin
				delete from staahrates where 
				datediff(d,@csdt,sdt)=0 and datediff(d,@tedt,edt)=0 
				and roomtype=@staahroomtype and renttype=@staahrenttype
			end

			--select @staahroomtype=staahcode from wings where wingno=@roomtype
			--print convert(varchar(20),@csdt) + ' ' +  str(year(@csdt)) + ' ' + convert(varchar(20),@tedt)
			set @ssdt = ltrim(rtrim(str(year(@csdt))))+ '-'+ right('00' + ltrim(rtrim(str(month(@csdt)))),2)+'-'+right('00'+ltrim(rtrim(str(day(@csdt)))),2)
			set @sedt = ltrim(rtrim(str(year(@tedt))))+ '-'+ right('00' + ltrim(rtrim(str(month(@tedt)))),2)+'-'+right('00'+ltrim(rtrim(str(day(@tedt)))),2)
			
			insert into staahrates 
			(sdt,edt,renttype,roomtype,rent)	values
			(@ssdt,@sedt,@staahrenttype,@staahroomtype,@rate)
		end	
		fetch next from c1 into @roomtype,@rno
	end	
	close c1
	deallocate c1
    set @csdt = dateadd(day,1,@tedt)
 
	end
return
e_error:
rollback transaction
raiserror('error in rate updation',16,-1)
return
END


GO
/****** Object:  StoredProcedure [dbo].[storecreateac]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[storecreateac] @ctrlno numeric 
/* with encryption */
AS
declare @storeno numeric
declare @company numeric
declare @fy numeric
select @company=company from store where storeno=(select storeno from storeitems where ctrlno=@ctrlno)
declare cfy cursor for select fy from finyear where company=@company order by fy
open cfy
fetch next from cfy into @fy
while @@fetch_status=0
begin
if not exists(select * from storeprimary where ictrlno=@ctrlno and fy=@fy)
insert into storeprimary (fy,ictrlno) values (@fy,@ctrlno)
fetch next from cfy into @fy
end
close cfy
deallocate cfy




GO
/****** Object:  StoredProcedure [dbo].[storeinitfy]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[storeinitfy] @fy numeric,@ctrlno numeric,@oldfy numeric
/* with encryption */
AS
BEGIN
declare @storeno numeric
declare @bofy datetime
declare @eofy datetime
declare @chlno numeric
declare @bctrlno numeric
declare @company numeric
declare @qty numeric(18,4)
declare @hctrlno numeric
select @bofy=date from finyear where fy=@fy 
select @storeno=storeno from storeitems where ctrlno=@ctrlno
SET NOCOUNT ON;
begin transaction
select @qty=0
if exists(select * from storeprimary where ictrlno=@ctrlno and fy=@fy)
	select @qty=opgbal from storeprimary where ictrlno=@ctrlno and fy=@fy
if isnull(@qty,0) > 0 
	begin
	if not exists(select * from stchlno where storeno=@storeno and chlndt =@bofy)
		begin
			insert into stchlno (storeno,chlndt,pchlno,dchlno,adjcr,adjdr) values (@storeno,@bofy,0,0,0,0)
			if @@error<>0 goto e_error
		end

	update stchlno  set adjcr= adjcr+1  where storeno= @storeno and chlndt=@bofy
	if @@error<>0 goto e_error
	select @chlno=adjcr from stchlno  where storeno= @storeno and chlndt=@bofy
	insert into storebills (storeno,amttyp,chlno,txndt,usernm,adjust,invoiceno,supplier,grno,saletax,stmode,discount,cashtype,txntyp,otherchgs)
	values
	(@storeno,'A',@chlno,@bofy,'SYS','Y',1,0,0,0,0,0,0,2,0)
	if @@error<>0 goto e_error
	select @bctrlno=@@identity

	insert into storehist (bctrlno,ctrlno,amttyp,qty,rate,chlno,price,saletax,txndt,adjust) values 
	(@bctrlno,@ctrlno,'C',@qty,0,@chlno,0,0,@bofy,'Y')
	if @@error<>0 goto e_error
	set @hctrlno=@@IDENTITY
	update storehist set hctrlno2=@hctrlno where hctrlno=@hctrlno  

	update storeprimary set opgbal=0 where ictrlno=@ctrlno and fy=@fy
	end
delete from storeprimary where ictrlno=@ctrlno and fy=@oldfy

select @bofy=date,@company=company from finyear where fy=@oldfy 
select @eofy = dbo.geteofy(@bofy)

update storehist set forupdt='Y' where ctrlno=@ctrlno  
and datediff(d,'1/1/2000',txndt) between 
datediff(d,'1/1/2000',@bofy) and 
datediff(d,'1/1/2000',@eofy)

--triggers disabled

delete from storehist where ctrlno=@ctrlno
and datediff(d,'1/1/2000',txndt) between 
datediff(d,'1/1/2000',@bofy) and 
datediff(d,'1/1/2000',@eofy)

--triggers enabled

commit transaction
return
e_error:
rollback transaction
raiserror('Error In initialising Store Primary',16,-1)
return
END

GO
/****** Object:  StoredProcedure [dbo].[storenewyear]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[storenewyear]  @fy numeric  
/* with encryption */
AS
declare @storeno numeric
declare @company numeric
declare @ctrlno numeric
select @company=company from finyear where fy=@fy
declare cstore cursor for select storeno from store  where company=@company
open cstore
fetch next from cstore into @storeno
while @@fetch_status=0
begin
	declare citems cursor for select ctrlno from storeitems where storeno=@storeno
	open citems
	fetch next from citems into @ctrlno
	while @@fetch_status=0
	begin
		if not exists(select * from storeprimary where fy=@fy and ictrlno=@ctrlno)
		insert into storeprimary (fy,ictrlno) values (@fy,@ctrlno)
		fetch next from citems into @ctrlno
	end
	close citems
	deallocate citems
	fetch next from cstore into @storeno
end
close cstore
deallocate cstore
declare @ofy numeric
select @ofy=max(fy) from finyear where fy < @fy and company=@company
--print str(@ofy)
select * into #dummystp from storeprimary where fy=@ofy 
update storeprimary set opgbal=isnull((select balance from #dummystp  where ictrlno=storeprimary.ictrlno and fy=@ofy),0) where fy=@fy
declare @bofy datetime
declare @eofy datetime
select @bofy=date from finyear where fy=@fy
--set @eofy = '31 mar' + str(datepart(year,@bofy)+1)

set @eofy = dbo.geteofy(@bofy)

--declare csii cursor for select ictrlno from storeprimary where fy=@fy
--open csii
--fetch next from csii into @ctrlno

--while @@fetch_status=0
--begin
--exec spsthrewrite @ctrlno,@fy,@bofy,@eofy,1
--fetch next from csii into @ctrlno
--end

--close csii
--deallocate csii




GO
/****** Object:  StoredProcedure [dbo].[storeupdate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[storeupdate]  
/* with encryption */
AS
declare @ctrlno numeric
begin transaction
declare tc cursor for select ctrlno from storeitems order by ctrlno
open tc
fetch next from tc into @ctrlno
while @@fetch_status=0 begin
   exec fillstock @ctrlno,'1 apr 2004' ,1
   fetch next from tc into @ctrlno
end
close tc
deallocate tc
commit transaction
return




GO
/****** Object:  StoredProcedure [dbo].[ststipdate]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[ststipdate](@ctrlno numeric,@txndt datetime) 
/* with encryption */
AS
declare @company numeric
declare @fy numeric
select @company=company from store where storeno=(select storeno from storeitems where ctrlno=@ctrlno)
exec getfy @txndt,@company,@fy out




GO
/****** Object:  StoredProcedure [dbo].[stttda]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.stttda    Script Date: 15/02/2004 6:23:01 PM ******/
CREATE PROCEDURE [dbo].[stttda] @ctrlno int ,@ttype int=1
/* with encryption */
as
declare @outlet int
declare @chctrlno numeric
declare @amt money
declare @company int
declare @fy int
declare @txndt datetime
declare @grno numeric
declare @retval int
declare @status char(1)
declare @atype char(1)
begin transaction
	select @atype=atype,@chctrlno=chctrlno,@amt=amount,@txndt=txndt,@grno=grno  from ttadvance where ctrlno=@ctrlno 
	if @ttype=3
		begin 
			select @status=status,@outlet=outlet from ta_chalan where ctrlno=@chctrlno
			select @company=company from toutlet where outlet=@outlet
			if @status<>' ' begin print 'This chalan is closed'  goto e_error end
			if @atype='P'
				begin
				update ta_chalan set advance_t=advance_t-@amt where ctrlno=@chctrlno
				if @@error<>0 goto e_error
				end
				else
				begin
				update ta_chalan set advance=advance-@amt where ctrlno=@chctrlno
				if @@error<>0 goto e_error
				end
		end
	else
		begin
		select @status=status,@outlet=outlet,@company=company from tt_chalan_ol where chctrlno=@chctrlno
		if @status<>' ' begin print 'This chalan is closed'  goto e_error end
		if @atype='P'
			begin
				update chalan set advance_t=advance_t-@amt where chctrlno=@chctrlno
				if @@error<>0 goto e_error
			end
			else
			begin
				update chalan set advance=advance-@amt where chctrlno=@chctrlno
				if @@error<>0 goto e_error
			end

		end

	exec getfy @txndt,@company,@fy out
	if @grno<>0 
	begin
	exec delgrno @company,@fy,@grno,@txndt,@retval out
	if @@error<>0 goto e_error
	end
	delete from ttadvance where ctrlno=@ctrlno 
	if @@error <> 0 goto E_error
commit transaction
return
E_error:
rollback transaction
raiserror('error in deletion',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[super]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


/* PowerBrain Software (India) New Delhi*/
/****** Object:  Stored Procedure dbo.super    Script Date: 15/02/2004 6:23:01 PM ******/
/*** Modified on 26/06/2009 ***/
CREATE PROCEDURE [dbo].[super] @userno int,@t1 varchar(100),@ename varchar(50),@userno1 numeric,@opt tinyint=0 ,@acc tinyint=0
/* with encryption */
AS
declare @serviceno numeric
declare @permission char(1)
declare @outlettype int
declare @outlet numeric
declare @sql varchar(1000)
declare @preper char(1)
declare @usernm varchar(50)
declare @servicename varchar(200)
declare @olname varchar(50)
begin transaction
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''

if @opt=0
delete from servicepermission where userno = @userno
else
delete from servicepermissiond where userno = @userno

if @@error<>0 goto e_error
set @sql = 'declare tempcur cursor for select serviceno,permission,outlettype,outlet,preper from ' + @t1
exec(@sql)
if @@error<>0 goto e_error
open tempcur
fetch next from tempcur into @serviceno,@permission,@outlettype,@outlet ,@preper
while @@fetch_status=0
begin
if @opt=0
insert into servicepermission (serviceno,userno,permission,outlettype,outlet,preper,userno1) values(@serviceno,@userno,@permission,@outlettype,@outlet,@preper,@userno1)
else
insert into servicepermissiond (serviceno,userno,permission,outlettype,outlet,preper,userno1) values(@serviceno,@userno,@permission,@outlettype,@outlet,@preper,@userno1)
if @acc=0
if @permission <> @preper 
	begin
	set @olname = ''
	if @outlettype=1 select @olname=name from outlet where code=@outlet
	if @outlettype=2 select @olname=name from phoutlet where outlet=@outlet
	select @servicename=servicename from servicenumbers where serviceno=@serviceno
	set @servicename = ltrim(rtrim(@servicename)) 
	insert into acclog(usernm,narration,txntype,gno,txntrace) values (@usernm,'Access Permissions Modified/Created for '+ ltrim(rtrim(@ename))  + ' ' +  ltrim(rtrim(str(@serviceno))) + ' ' +  ltrim(rtrim(@servicename)) + ' ' +  ltrim(rtrim(@olname)) +'- Previos Value ' + @preper + ' New Value ' + @permission ,101,0,@ename)
	end
fetch next from tempcur into @serviceno,@permission,@outlettype,@outlet ,@preper
end
close tempcur
deallocate tempcur


commit transaction

return
e_error:
rollback transaction
raiserror('error in updpermis',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[super1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[super1] @userno numeric,@desgnno numeric 
/* with encryption */
AS
begin transaction
delete from servicepermission where userno=@userno
if @@error<>0 goto e_error
select * into #tempsp from servicepermissiond where userno=@desgnno
update #tempsp set userno=@userno
if @@error<>0 goto e_error
insert into servicepermission select * from #tempsp
if @@error<>0 goto e_error
commit transaction
return
e_error:
rollback transaction
raiserror('Error in Servicepermission by designation',16,-1)
return




GO
/****** Object:  StoredProcedure [dbo].[TA_AdvanceReceipt]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[TA_AdvanceReceipt]
@ctrlno numeric,
@txndt datetime,
@amt money,
@mop int,
@crname varchar(50),
@crnum varchar(50),
@crbank varchar(50),
@gno numeric,
@glac numeric,
@usernm varchar(50)
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	begin transaction
	declare @outlet int
	declare @company numeric
	declare @fy numeric
	declare @grno numeric
	declare @ano numeric		
	declare @err varchar(2000)
	set @err = 'Error in Travel Package Voucher Posting'
	select @outlet=outlet from ta_itinerary where ctrlno=@ctrlno
	select @company=company from toutlet where outlet=@outlet
	exec getfy @txndt,@company,@fy out
	if @@error <> 0 goto e_error
	
	update finyear set ta_ano=ta_ano +1 where fy=@fy
	select @ano=ta_ano from finyear where fy=@fy

	insert into ta_advance (ictrlno,txndt,ano,amt,crname,crnum,crbank,mop,glac,gno,usernm) values 
	(@ctrlno,@txndt,@ano,@amt,@crname,@crnum,@crbank,@mop,@glac,@gno,@usernm)
    if @@error <> 0 goto e_error
	select @ano=@@IDENTITY 
	update ta_itinerary set advance=isnull(advance,0)+@amt where ctrlno=@ctrlno
    if @@error <> 0 goto e_error

	exec ta_advvp @ano 
    if @@error <> 0 goto e_error

	commit transaction
	return @ano
	e_error:
	rollback transaction
	raiserror(@err,16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[ta_advvp]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ta_advvp]
@ctrlno numeric
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	begin transaction	
	declare @grno numeric 
	declare @mop int
	declare @crname varchar(50)
	declare @crnum varchar(50)
	declare @crbank varchar(50)
	declare @gno numeric
	declare @glac numeric
	declare @usernm varchar(50)
	declare @txndt datetime
	declare @ictrlno numeric
	declare @company numeric
	declare @fy numeric
	declare @err varchar(1000)
	declare @outlet int 
	declare @amt money
	declare @paymentac numeric
	declare @partic varchar(200)
	declare @txntype char(1)
	declare @acno numeric
	declare @prefix char(3)
	declare @advac numeric
	declare @retval numeric
	declare @ano numeric
	declare @name varchar(50)
	set @err = 'Error TA Advance Vouchjer Posting'
	select @amt=amt,@ictrlno=ictrlno,@txndt=txndt,@grno=isnull(grno,0),@mop=mop,
	@crname=crname,@crnum=crnum,@crbank=crbank,
	@gno=gno,@glac=glac,@usernm=usernm,@ano=ano 
	from ta_advance where ctrlno=@ctrlno
	if @@error <> 0 goto e_error

	select @outlet=outlet,@name=[name] from ta_itinerary where ctrlno=@ictrlno
	if @@error <> 0 goto e_error
	select @company=company,@advac=advancerecd from toutlet where outlet=@outlet
	if @@error <> 0 goto e_error
	exec getfy @txndt,@company,@fy out
	if @@error <> 0 goto e_error
	if isnull(@grno,0) <> 0
		begin
		exec delgrno @Company,@fy,@grno,@txndt,@retval out
		if @@error <> 0 goto e_error
		end


	if isnull(@grno,0)=0  
		begin 
		exec getgrp @txndt,@company,@grno out
		if @@error <> 0 goto e_error
		update ta_itinerary set grno=@grno where ctrlno=@ctrlno
		end

	select @paymentac = case	
							when @mop=1 then 1
							when @mop=4 then @glac
							when @mop=5 then 22
						end
	set @partic = 'Tour Advance Receipt : ' + ltrim(rtrim(str(@ano)))   + ' A/C ' + @name
	
	if @amt > 0 
		begin
		set @acno=@advac
		set @txntype = 'C'
		set @prefix = 'By '
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno,@ctrlno,'Y','TUR')
		if @@error <> 0 goto e_error

		set @acno=@paymentac 
		set @txntype = 'D'
		set @prefix = 'To '
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno,@ctrlno,'Y','TUR')
		if @@error <> 0 goto e_error
		end
	commit transaction
	return
	e_error:
	rollback transaction
	raiserror(@err,16,-1)
	return
END

GO
/****** Object:  StoredProcedure [dbo].[TA_billing]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- ============================================
CREATE PROCEDURE [dbo].[TA_billing]
@ctrlno numeric,@txndt datetime,@amt money,@servicetax money,@otherchgs money,@spldiscount money,
@mop int,@crname varchar(50),@crnum varchar(50),@crbank varchar(50),@glac numeric,@gno numeric,@cess money
/* with encryption */
AS
BEGIN
	declare @outlet int
	declare @sale3 numeric
	declare @otherchg3 numeric
	declare @servicetaxac numeric
	declare @discountac numeric
	declare @paymetnac numeric
	declare @company numeric
	declare @fy numeric
	declare @err varchar(600)
	declare @bno numeric

	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	if exists(select * from ta_itinerary where ctrlno=@ctrlno and billed='Y') 
		begin 
		raiserror('This Package is already Billed',16,-1)
		return
		end
	begin transaction
		set @err='Error in Itinerary Billing'
		select @outlet=outlet,@bno=isnull(bno,0) from ta_itinerary where ctrlno=@ctrlno
		select @company=company from toutlet where outlet=@outlet
		exec getfy @txndt,@company,@fy out
		if @@error <> 0 goto e_error
		if @bno=0
			begin
			update finyear set packagebno=packagebno+1 where fy=@fy
			select @bno=packagebno from finyear where fy=@fy  		
			update ta_itinerary set bno=@bno where ctrlno=@ctrlno
			end

		update ta_itinerary set billed='Y',packageamount=@amt,servicetax=@servicetax,otherchgs=@otherchgs,spldiscount=@spldiscount,billdate=@txndt ,
		mop=@mop,crname=@crname,crnum=@crnum,crbank=@crbank,glac=@glac,gno=@gno,cess=@cess
		where ctrlno=@ctrlno
        if @@error<>0 goto e_error

		exec ta_packagev @ctrlno
		if @@ERROR<>0 goto e_error

	commit transaction
	return
e_error:
rollback transaction
raiserror(@err,16,-1)
return
END

GO
/****** Object:  StoredProcedure [dbo].[TA_DelAdvance]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[TA_DelAdvance] @ctrlno numeric, @usernm varchar(50) 
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @grno numeric
	declare @company numeric
	declare @fy numeric
	declare @outlet numeric
	declare @ictrlno numeric
	declare @txndt datetime
	declare @amt decimal
	declare @retval numeric
	Declare @name varchar(50)
	declare @olname varchar(50)
	declare @ano numeric
	select @ano=ano,@txndt=txndt,@amt=amt,@ictrlno=ictrlno,@grno=isnull(grno,0) from ta_advance where ctrlno=@ctrlno
	select @name=name,@outlet=outlet from ta_itinerary where ctrlno=@ictrlno
	select @company=company,@olname=rtrim(name)  from toutlet where outlet=@outlet
	begin transaction

	exec getfy @txndt,@company,@fy out
	if @@error <> 0 goto e_error
	if isnull(@grno,0) <> 0
		begin
		exec delgrno @Company,@fy,@grno,@txndt,@retval out
		if @@error <> 0 goto e_error
		end
    update ta_itinerary set advance=advance-@amt where ctrlno=@ictrlno
	if @@error <> 0 goto e_error
	delete from ta_advance where ctrlno=@ctrlno
	if @@error <> 0 goto e_error
	insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm,'Tour Package ID ' + ltrim(rtrim(str(@ictrlno))) + ' Name ' + @name + ' Outlet ' + @olname + ' Advance Receipt No.' +  ltrim(rtrim(str(@ano))) + ' Amount ' + ltrim(rtrim(str(@amt))) +  'Dated ' + convert(varchar(20),@txndt) + ' Deleted ',906,0)
	commit transaction
	return
	e_error:
	raiserror('Error in deleting Tour Advance',16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[TA_DELENTRY]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<PowerBrain Software (India),,RM.Ramanathan>
-- Create date: <19-Mar-2015,,>
-- Description:	<Deleting Itinerary Entry,,>
-- =============================================
CREATE PROCEDURE [dbo].[TA_DELENTRY] @CTRLNO NUMERIC , @usernm varchar(50)
/* with encryption */
AS
BEGIN
	declare @name varchar(50)
	declare @olname varchar(50)
	declare @outlet numeric
	declare @advance money
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	IF EXISTS (SELECT * FROM TA_CHALAN where ino in (select ctrlno from ta_itlist where ictrlno=@ctrlno)) 
		begin
			raiserror('There are some itinerary entries outstanding for this Tour Id',16,-1)
			return
		end
	select @name=name,@outlet=outlet,@advance=advance from ta_itinerary where ctrlno=@ctrlno
	select @olname=name from toutlet where outlet=@outlet
	if @advance<>0 
		begin
			 raiserror('There are advance amount outstanding in this Tour Package',16,-1)
			 return
		end
	begin transaction
	delete from ta_itlist where ictrlno=@ctrlno
	delete from ta_itinerary where ctrlno=@ctrlno

	insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm,'Tour Package ID ' + ltrim(rtrim(str(@ctrlno))) + ' Name ' + @name + ' Outlet ' + @olname + ' Deleted ',902,0)
	commit transaction
	return
	e_error:
	raiserror('Error in deletion',16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[ta_deletebill]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ta_deletebill]
	-- Add the parameters for the stored procedure here
@ctrlno numeric
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    declare @company int
	declare @fy int
	declare @fy1 int
	declare @grno numeric
	declare @grno1 numeric
	declare @selfdeal char(1)
	declare @mop int
	declare @outlet numeric
	declare @billed char(1)
	declare @txndt datetime 
	declare @retval int 
	begin transaction
	select @txndt=billdate,@billed=billed,@mop=mop,@outlet=outlet,@grno=grno,@grno1=grno1 from ta_itinerary where ctrlno=@ctrlno
	select @company=company from toutlet where outlet=@outlet
	if @billed<> 'Y'
		begin
		raiserror('This is not a billed Itinerary',16,-1) 
		return
		end
	
		exec getfy @txndt,@company,@fy out
		if @@error <> 0 goto e_error
		exec getfy @txndt,1,@fy1 out
		if @@error <> 0 goto e_error

		if isnull(@grno,0) <> 0
			begin
			exec delgrno @Company,@fy,@grno,@txndt,@retval out
			if @@error <> 0 goto e_error
			end
		if isnull(@grno1,0) <> 0
			begin
			exec delgrno 1,@fy1,@grno1,@txndt,@retval out
			if @@error <> 0 goto e_error
			end
		update ta_itinerary set billed='' where ctrlno=@ctrlno

		commit transaction
		return
e_error:
rollback transaction
raiserror('Error in deleting Tour Package Bill',16,-1)
return
END

GO
/****** Object:  StoredProcedure [dbo].[TA_EditAdvance]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[TA_EditAdvance] 
@ctrlno numeric,
@txndt1 datetime,
@amt1 money,
@mop1 int,
@crname1 varchar(50),
@crnum1 varchar(50),
@crbank1 varchar(50),
@glac1 numeric,
@gno1 numeric,
@usernm1 varchar(50)
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @outlet numeric
	declare @company numeric
	declare @fy numeric
	declare @mop numeric
	declare @amt money
	declare @txndt datetime
	declare @grno numeric
	declare @ictrlno numeric
	declare @retval numeric
	declare @usernm varchar(50)
	declare @name varchar(50)
	declare @olname varchar(50)
	declare @ano numeric
	begin transaction
		select @ano=ano,@ictrlno=ictrlno,@grno=grno,@amt=amt,@mop=mop,@txndt=txndt from ta_advance where ctrlno=@ctrlno
		select @outlet=outlet,@name=name from ta_itinerary where ctrlno=@ictrlno
		select @company=company,@olname=rtrim(name) from toutlet where outlet=@outlet

		exec getfy @txndt,@company,@fy out
		if @@error <> 0 goto e_error

		if isnull(@grno,0) <> 0
			begin
			exec delgrno @Company,@fy,@grno,@txndt,@retval out
			if @@error <> 0 goto e_error
			end

		update ta_itinerary set advance=advance-@amt+@amt1 where ctrlno=@ictrlno
		if @@error<>0 goto e_error

		update ta_advance set txndt=@txndt1,mop=@mop1,amt=@amt1 ,crname=@crname1,crnum=@crnum1,crbank=@crbank1,gno=@gno1,glac=@glac1,usernm=@usernm1  where ctrlno=@ctrlno
		if @@error<>0 goto e_error

		exec ta_advvp @ctrlno
		if @@error<>0 goto e_error

		insert into acclog(txndt,usernm,narration,txntype,gno) values (getdate(),@usernm1,'Tour Package ID ' + ltrim(rtrim(str(@ictrlno))) + ' Name ' + @name + ' Outlet ' + @olname + ' Advance Receipt No.' +  ltrim(rtrim(str(@ano))) + ' Amount ' + ltrim(rtrim(str(@amt))) +  'Dated ' + convert(varchar(20),@txndt) + ' MOP : ' +  case when @mop=1 then 'Cash Receipt' when @mop=4 then 'GL Account' else 'Credit Card' end  + ' Edited and changed to ' + case when @mop=1 then 'Cash Payment' when @mop1=4 then 'GL Account' else 'CRedit Card' end + ' Amount ' + convert(varchar(20),@amt1) + ' Dated ' + convert(varchar(20),@txndt1)   ,906,0)

commit transaction
return
e_error:
raiserror('Error in Editing TA Advance',16,-1)


END

GO
/****** Object:  StoredProcedure [dbo].[TA_ItineraryInsert]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<PowerBrain Software (India),,Ramanathan>
-- Create date: <19-Mar-2015,,>
-- Description:	<Itinerary Insert (new records),,>
-- =============================================
CREATE PROCEDURE [dbo].[TA_ItineraryInsert]
@outlet int,
@txndt datetime,
@address varchar(10),
@name varchar(50),
@add1 varchar(50),
@add2 varchar(50),
@add3 varchar(50),
@city varchar(50),
@state varchar(50),
@pin varchar(50),
@phone varchar(50),
@email varchar(50),
@amount money,
@sdt datetime,
@edt datetime,
@contactperson varchar(50),
@temptable varchar(20),
@taxinclusive int,
@male int,
@female int,
@child int,
@tc varchar(2000),
@usernm varchar(50)

/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @ctrlno numeric
	declare @typeid int
	declare @details varchar(500)
	declare @err varchar(500)
	declare @sql varchar(5000)
	begin transaction	
		insert into TA_Itinerary (outlet,txndt,address,name,add1,add2,add3,city,state,pin,phone,email,amount,sdt,edt,contactperson,advance,taxinclusive,male,female,child,tc,usernm) values 
		(@outlet,@txndt,@address,@name,@add1,@add2,@add3,@city,@state,@pin,@phone,@email,@amount,@sdt,@edt,@contactperson,0,@taxinclusive,@male,@female,@child,@tc,@usernm)
		if @@error <> 0 goto e_error
		set @ctrlno=@@IDENTITY

		set @sql = 'declare c1 cursor for select txndt,typeid,city,details from ' +  @temptable 
		exec (@sql)
		open c1
		fetch next from c1 into @txndt,@typeid,@city,@details
		while @@fetch_status=0
		begin
			insert into ta_itlist (ictrlno,txndt,typeid,city,details,usernm,status) values
			(@ctrlno,@txndt,@typeid,@city,@details,@usernm,'')
			if @@error <> 0 goto e_error
			fetch next from c1 into @txndt,@typeid,@city,@details
		end
		close c1
		deallocate c1
	commit transaction
return @ctrlno
e_error:
rollback transaction
raiserror(@err,16,-1)
END

GO
/****** Object:  StoredProcedure [dbo].[ta_packagev]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[ta_packagev]
@ctrlno numeric
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @err varchar(1000)
	declare @outlet numeric
	declare @txndt datetime
	declare @company numeric
	declare @fy numeric
	declare @grno numeric
	declare @salesamt money
	declare @otherchgs money
	declare @spldiscount money
	declare @servicetax money
	declare @billed char(1)
	declare @sales4 numeric
	declare @otherchgs4 numeric
	declare @servicetax4 numeric
	declare @discount4 numeric
	declare @paymentac numeric
	declare @mop int
	declare @crname varchar(50)
	declare @crnum varchar(50)
	declare @crbank varchar(50)
	declare @glac numeric
	declare @gno numeric
	declare @selfdeal char(1)
	declare @hno_mybook numeric
	declare @self_receivable numeric
	declare @retval int

	declare @acno numeric
	declare @amt money
	declare @prefix char(3)
	declare @txntype char(1)
	declare @partic varchar(200)
	declare @bookingdate datetime
	declare @name varchar(100)
	declare @advance money
	declare @advance4 numeric
	declare @grno1 numeric
	declare @fy1 int
	declare @paymentamt money
	declare @htl_mybook numeric
	declare @htl_receivable numeric
	declare @hno_htlbook numeric
	declare @cess money
begin transaction
	set @err='Error in Vocuher Posting for Tour Package Billing'
	select @grno1=isnull(grno1,0),@advance=isnull(advance,0),@name=name,@bookingdate=txndt,@billed=billed,@outlet=outlet,@txndt=billdate,@salesamt=isnull(packageamount,0),@otherchgs=isnull(otherchgs,0),@spldiscount=isnull(spldiscount,0),@servicetax=isnull(servicetax,0),@mop=mop,@crname=crname,@crnum=crnum,@crbank=crbank,@cess=cess from ta_itinerary where ctrlno=@ctrlno
    select @selfdeal=selfdeal, @advance4=advancerecd,@company=company,@sales4=sales4,@otherchgs4=otherchgs4,@discount4=discount4, 
	@servicetax4=servicetaxac,
	@self_receivable=my_receivable,
	@hno_htlbook=@hno_htlbook,
	@htl_receivable=htl_receivable,
	@hno_mybook=hno_mybook
	from toutlet where outlet=@outlet

	if @billed<>'Y' return

	exec getfy @txndt,@company,@fy out
	if @@error <> 0 goto e_error

	exec getfy @txndt,1,@fy1 out
	if @@error <> 0 goto e_error


	if isnull(@grno,0) <> 0
		begin
		exec delgrno @Company,@fy,@grno,@txndt,@retval out
		if @@error <> 0 goto e_error
		end
	if isnull(@grno1,0) <> 0
		begin
		exec delgrno 1,@fy1,@grno1,@txndt,@retval out
		if @@error <> 0 goto e_error
		end

	if isnull(@grno,0)=0  
		begin 
		exec getgrp @txndt,@company,@grno out
		if @@error <> 0 goto e_error
		update ta_itinerary set grno=@grno where ctrlno=@ctrlno
		end

	select @paymentac = case
		when @mop = 1 then 1  				--cash sales
		when @mop=2 then 				--room service
			case 
			when @company=1 then @self_receivable
			else
				case when @selfdeal='Y' then @self_receivable 
				else @hno_mybook 
				end
			end
		when @mop=3 then 23				--bills receivable
		when @mop=4 then @glac		 	-- GL A/C
		when @mop=5 then 22 			-- credir card
		when @mop=6 then 0 				-- complementary - no vouching
	end


	set @partic = 'Tour Package ID No. ' + ltrim(rtrim(str(@ctrlno))) + ' Dated ' + convert(varchar(20),@bookingdate) + ' A/C ' +  @name

	if @salesamt<>0 
		begin
		set @acno=@sales4
		set @amt = @salesamt
		set @txntype = 'C'
		set @prefix = 'By '
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno,@ctrlno,'Y','TUR')
		if @@error <> 0 goto e_error
		end

	if @servicetax <> 0
		begin
		set @acno=@servicetax4 
		set @amt = @servicetax 
		set @txntype = 'C'
		set @prefix = 'By '
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno,@ctrlno,'Y','TUR')
		if @@error <> 0 goto e_error
		end
	if @cess <> 0
		begin
		set @acno=90
		set @amt = @cess 
		set @txntype = 'C'
		set @prefix = 'By '
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno,@ctrlno,'Y','TUR')
		if @@error <> 0 goto e_error
		end
	if @otherchgs <> 0
		begin
		set @acno=@otherchgs4
		set @amt = @otherchgs
		set @txntype = 'C'
		set @prefix = 'By '
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno,@ctrlno,'Y','TUR')
		if @@error <> 0 goto e_error
		end
	if @spldiscount <> 0
		begin
		set @acno=@discount4 
		set @amt = @spldiscount 
		set @txntype = 'D'
		set @prefix = 'To '
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno,@ctrlno,'Y','TUR')
		if @@error <> 0 goto e_error
		end
	if @advance <> 0
		begin
		set @acno=@advance4
		set @amt = @advance
		set @txntype = 'D'
		set @prefix = 'To '
		insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
		values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno,@ctrlno,'Y','TUR')
		if @@error <> 0 goto e_error
		end
	set @paymentamt = @salesamt+@servicetax+@otherchgs-@spldiscount-@advance
	if  @paymentamt  <> 0
		begin
			set @acno=@paymentac 
			set @amt = @paymentamt
			if @amt > 0 
				begin
				set @txntype = 'D'
				set @prefix = 'To '
				end
			else
				begin
				set @txntype = 'C'
				set @prefix = 'By '
				end
			insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
			values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno,@ctrlno,'Y','TUR')
			if @@error <> 0 goto e_error
		end
	   
	if @mop=2 and @selfdeal='N'
		if @paymentamt <> 0
			begin
				if isnull(@grno1,0)=0  
					begin 
					exec getgrp @txndt,1,@grno1 out
					if @@error <> 0 goto e_error
					update ta_itinerary set grno1=@grno1 where ctrlno=@ctrlno
					end
				set @acno=@htl_receivable
				set @amt = @paymentamt
				set @txntype = 'D'
				set @prefix = 'To '
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno1,@ctrlno,'Y','TUR')
				if @@error <> 0 goto e_error

				set @acno=@hno_htlbook 
				set @amt = @paymentamt
				set @txntype = 'C'
				set @prefix = 'By '
				insert vouchers (company,fy,actnum,amt,partic,amttyp,txndt,grno,fdrsno,channelcode,txntype)
				values (@company,@fy, @acno  ,@amt , @prefix+ @partic   ,@txntype,@txndt,@grno1,@ctrlno,'Y','TUR')
				if @@error <> 0 goto e_error
			end
commit transaction
return
e_error:
rollback transaction
raiserror(@err,16,-1)
return
END

GO
/****** Object:  StoredProcedure [dbo].[ta_updtperiodtable]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE procedure [dbo].[ta_updtperiodtable]
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
declare @sdt datetime
declare @edt datetime
declare @ctrlno integer
declare @m_sdt datetime
declare @cnt integer
set @cnt=0
set @m_sdt = '1 jan 2000'
if cursor_status('global','c1') > 0
begin
close c1
deallocate c1
end
declare c1 cursor for select edt,ctrlno from ta_rateperiod order by ctrlno
open c1
fetch next from c1 into @edt,@ctrlno
while @@fetch_status = 0
begin
if @cnt=0 
update sttable set sdt = @m_sdt where ctrlno=@ctrlno
else 
update sttable set sdt = @m_sdt where ctrlno=@ctrlno

set @m_sdt = dateadd(d,1,@edt)
fetch next from c1 into @edt,@ctrlno
end
close c1
deallocate c1
end

GO
/****** Object:  StoredProcedure [dbo].[TallyGLPRIMLD]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Rm.Ramanathan
-- Create date: 29 Oct 2017
-- Description:	For Tallying GLPRIMLD
-- =============================================
CREATE PROCEDURE [dbo].[TallyGLPRIMLD]   @fy int
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET NOCOUNT ON;
/*
select cgp_actno,cgp_desc,opgbal,dr,cr,balance,opgbal+isnull(cr,0)-isnull(dr,0) newbal,balance - (opgbal+isnull(cr,0)-isnull(dr,0)) diff from 
(
select  cgp_actno,cgp_desc,opgbal,dr,cr,balance,opgbal+isnull(cr,0)-isnull(dr,0) newbal,balance - (opgbal+isnull(cr,0)-isnull(dr,0)) diff 
from (
select cgp_actno,cgp_desc, balance,opgbal,
(select sum(amt) from vouchers where fy=@fy and amttyp='D' and actnum=glprimld.cgp_actno) dr,
(select sum(amt) from vouchers where fy=@fy and amttyp='C' and actnum=glprimld.cgp_actno) cr
from glprimld where fy=@fy) a  
) b where diff<>0 order by cgp_desc
*/
--SELECT * FROM GLPRIMLD WHERE FY=@FY AND (OPGBAL<>0 or BALANCE <> 0 or EXISTS(SELECT ACTNUM FROM VOUCHERS WHERE FY=@FY)) order by cgp_desc
SELECT * FROM GLPRIMLD WHERE FY=@FY AND (OPGBAL<>0 or BALANCE <> 0 or 
EXISTS(SELECT ACTNUM FROM VOUCHERS WHERE FY=@FY and actnum=glprimlD.CGP_ACTNO)) order by cgp_desc
END

GO
/****** Object:  StoredProcedure [dbo].[TallyGLPRIMLD1]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		Rm.Ramanathan
-- Create date: 29 Oct 2017
-- Description:	For Tallying GLPRIMLD
-- =============================================
CREATE PROCEDURE [dbo].[TallyGLPRIMLD1]   @fy1 int
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @fY VARCHAR(20)
SET @FY = LTRIM(RTRIM(STR(@FY1)))

declare @temp varchar(20)
declare  @sql varchar(6000)
declare  @sql1 varchar(6000)
declare @cgp_actno int
exec randfile @temp out

if cursor_status('global','c1') > 0
begin
close c1
deallocate c1
end

set @sql = 'update glprimld set debit1=0,debit2=0,debit3=0,debit4=0,debit5=0,debit6=0,debit7=0,debit8=0,debit9=0,debit10=0,debit11=0,debit12=0,
credit1=0,credit2=0,credit3=0,credit4=0,credit5=0,credit6=0,credit7=0,credit8=0,credit9=0,credit10=0,credit11=0,credit12=0 WHERE FY=' + @FY
        
exec(@sql)

SET @SQL= 'SELECT CGP_ACTNO,CGP_DESC FROM GLPRIMLD WHERE FY=' + @FY + ' AND (BALANCE<>0 OR OPGBAL <> 0  
OR EXISTS (SELECT ACTNUM FROM VOUCHERS WHERE FY=' + @FY + ' AND ACTNUM=CGP_ACTNO)
)'

--EXEC(@SQL + ' order by cgp_actno')
--set @sql1 =' select cgp_actno from (' + @sql + ') a ORDER BY CGP_ACTNO'

--print @sql1
--exec(@sql1)


set @sql1 = 'declare c1 cursor for select cgp_actno from (' + @sql + ') a ORDER BY CGP_ACTNO'
exec(@sql1)
open c1
fetch next from c1 into @cgp_actno
while @@fetch_status=0
		begin
PRINT @CGP_ACTNO
		exec rewrite_new  @CGP_actno , @FY1 ,0
		fetch next from c1 into @cgp_actno
		END
close c1
deallocate c1

Exec(@SQL + ' order by cgp_actno')

RETURN
END
GO
/****** Object:  StoredProcedure [dbo].[TTSplit]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		RM.Ramanathan
-- Create date: 3 Sep 2015
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[TTSplit] @chctrlno numeric,@split char(1),@userno int=0,@checkpw1 int
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
	declare @billgno numeric
	declare @oldsplit char(1)
	declare @finalamt money
	declare @finalamt1 money
	declare @mop int
	declare @err varchar(500)
	declare @outlet int
	declare @sql varchar(8000)
	declare @company int
	declare @txndt datetime
	declare @lux char(1)
	declare @aftercheckout int
	declare @olname varchar(50)
	declare @chlno numeric
	declare @stype int
	declare @stypename varchar(20)
	declare @splitnarration varchar(100)
	declare @gname varchar(100)
	declare @gstin nvarchar(15)
	set @aftercheckout=0

	begin transaction
		select @gname=name,@stype=stype,@chlno=chlno,@txndt=finaldate,@billgno=gno,@outlet=outlet,@mop=mop,@finalamt=finalamt,@finalamt1=finalamt,@oldsplit=split from chalan where chctrlno=@chctrlno
		select @company=company,@lux=lux,@olname=name from toutlet where outlet=@outlet
		if not exists(select * from room where gno=@billgno) set @aftercheckout=1
		set @stypename = case when @stype=1 then 'CAB Chalan' when @stype=2 then 'Ticketing Chalan' else 'Other Chalan' end
		if @mop<>2 
			begin
			set @err='Either this chalan is already changed..or It is not closed with MOP Room Account '
			goto e_error
			end
		if not exists(select * from servicepermission where userno=@userno and serviceno=@checkpw1 and outlettype=4 and permission='Y')
			begin
			set @err='You are not permitted to use this option'  + str(@userno)  + ' ' + str(@checkpw1)
			goto e_error
			end
		if @mop=2 select @gstin=case when @split<>'Y' then gstin else gstinsplit end from guest where gno=@billgno 

		update chalan set split=@split,gstin=@gstin where chctrlno=@chctrlno
		if @aftercheckout=1 
			begin
				set @sql = 'exec delcout ' + str(@billgno) + ',' + convert(varchar(20),@finalamt1) + ',0' 
				
				+ ',' + str(@company) + ',' + str(300+@outlet) + ',''' + convert(varchar(20),@txndt)
				 + 
				''',''' + @oldsplit + ''',0,' + convert(varchar(20),@finalamt) + ',''' + @split + ''','+
				str(@userno)+','''+@lux+''',1'
				exec (@sql)
				if @@error <> 0  goto e_error
			end
	declare @usernm varchar(50)
	set @splitnarration = case when @oldsplit='N' then 'Main' when @oldsplit='Y' then 'Split' else 'Compliment' end + ' to ' + case when @split='Y' then 'Split' when @split='N' then 'Main' else 'Compliment' end  + case when @aftercheckout=1 then ' After Checkout' else '' end
	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
	select @usernm=usernm from comuser where [host_id]=host_id() and sessionid=''
	insert into acclog (txndt,usernm,narration,txntype,gno) values (getdate(),@usernm, ltrim(rtrim(@olname))+ ' Guest : ' + @gname + ' TT Chalan No ' + ltrim(str(@chlno)) + ' Dated ' + convert(varchar(20),@txndt,3) + ' Amt ' + ltrim(convert(varchar(20), @finalamt)) +  ' split status changed from "' + @splitnarration +'"' ,7,@billgno)
	commit transaction
	return
e_error:
rollback transaction
raiserror(@err,16,-1)
return
END


GO
/****** Object:  StoredProcedure [dbo].[UpdateGLCRDR]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE   PROCEDURE [dbo].[UpdateGLCRDR] @fy int 
AS
begin

declare @cgp_actno int 
declare @credits decimal(18,2)
declare @debits decimal(18,2)
declare @month int 
declare @sql varchar(1000)
set @month = 1
set nocount on 

declare @t1 varchar(100)


EXEC dbo.CloseAndDeallocateCursor 'c1'
EXEC DELCURSOR 'C1'

set 
@sql = 'declare c1 cursor for 
select cgp_actno from glprimld where fy='+ str(@fy) + ' and (opgbal+credit1-debit1
+credit2-debit2+credit3-debit3+credit4-debit4+credit5-debit5+credit6-debit6+credit7-debit7+credit8-debit8
+credit9-debit9+credit10-debit10+credit11-debit11+credit12-debit12<>balance)'
exec (@SQl)
open c1
fetch next from c1 into @cgp_actno
while @@fetch_status=0
begin
while @month <= 12
	begin
	set @credits = 0 
	set @debits  = 0

if exists(select * from vouchers where fy=@fy and actnum=@cgp_actno)
    begin
	select @credits=isnull(sum(case when amttyp='C' then amt else 0 end),0) ,@debits=isnull(sum(case when amttyp ='D' then amt else 0 end),0) 
	from vouchers where fy=@fy and actnum=@cgp_actno and month(txndt) = @month
	end
	set @sql =  'update glprimld set credit' + ltrim(str(@month)) + '=' + convert(varchar(20),@credits) + ',debit'+  ltrim(str(@month)) + '=' + convert(varchar(20),@debits)
	+ ' where fy=' + ltrim(str(@fy)) + ' and cgp_actno=' + ltrim(str(@cgp_actno))
	exec(@sql)
	set @month= @month+1
	end
set @month=1
update comuser set chktime=getdate() where [host_id]=host_id() AND 
computer=host_name() and sessionid='' 
fetch next from c1 into @cgp_actno
end
close c1
deallocate c1 

set @sql='if exists (select * from dbo.sysobjects where name = ''' + @t1 + ''' ) 
		DROP TABLE ' + @T1  
exec(@sql)	

end

GO
/****** Object:  StoredProcedure [dbo].[UPDATEINPUTAC]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[UPDATEINPUTAC]
AS
BEGIN
	SET NOCOUNT ON;

IF NOT EXISTS (SELECT * FROM GLPRIMLD WHERE CGP_ACTNO=104 AND ASSET='C' AND ONAC='S')
BEGIN
UPDATE GLPRIMLD SET ASSET='C' ,ONAC='S' WHERE CGP_ACTNO=104
UPDATE GLPRIMLD SET ASSET='D' ,ONAC='S' WHERE CGP_ACTNO=105
UPDATE GLPRIMLD SET ASSET='D' ,ONAC='S' WHERE CGP_ACTNO=106
UPDATE GLPRIMLD SET ASSET='D' ,ONAC='S' WHERE CGP_ACTNO=107
UPDATE GLPRIMLD SET ASSET='D' ,ONAC='S' WHERE CGP_ACTNO=111

UPDATE VOUCHERS SET TOTAMT_BALANCE=AMT WHERE ACTNUM IN (104,105,106,107,111)


END


END
GO
/****** Object:  StoredProcedure [dbo].[updateledgerforVB6]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Rm.Ramanathan
-- Create date: 03 Nov 2017
-- Description:	VB6 Ledger Update
-- =============================================
CREATE PROCEDURE [dbo].[updateledgerforVB6] @filename as varchar(50),@actnum int,@dt datetime,@fy int
/* with encryption */
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
declare @sql varchar(5000)
declare @balance money 
declare @cr money
declare @dr money
DECLARE @TXNNO2 NUMERIC
set @balance = dbo.getbalancefast(@actnum,@dt,@fy) 
set @sql = 'update ' + @filename + ' set balance=''' + convert(varchar(20),@balance) + case when @balance < 0 then ' Dr' else ' Cr' end  + ''' where [TXN.DATE] IS NULL'
--PRINT @SQL
EXEC(@SQL) 
set @sql = 'declare c1 cursor for select credits,debits,TXNNO2 from ' + @filename + ' WHERE [TXN.DATE] IS NOT NULL order by T1,txnno2 '
exec (@sql)
open c1
fetch next from c1 into @cr,@dR,@TXNNO2
while @@fetch_status=0
begin
set @balance = ISNULL(@balance,0) + isnull(@cr,0) - isnull(@dr,0) 
set @sql = 'update ' + @filename + ' set balance=''' + convert(varchar(20),ABS(@balance)) + CASE WHEN @BALANCE > 0  THEN ' Cr' ELSE ' Dr' END  + ''' where TXNNO2=' + STR(@TXNNO2)
--PRINT @SQL
exec(@sql )
fetch next from c1 into @cr,@dR,@TXNNO2
end
CLOSE C1
DEALLOCATE C1
END



GO
/****** Object:  StoredProcedure [dbo].[updatelic]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[updatelic] @company numeric
/* with encryption */
AS
declare @sdt datetime
declare @edt datetime
declare @ctrlno integer
declare @m_sdt datetime
declare @cnt integer
declare @sql varchar(8000)
set @cnt=0
set @m_sdt = '1 jan 2000'
if cursor_status('global','c1') > 0
begin
close c1
deallocate c1
end

declare c1 cursor for select edt,ctrlno from LIC where company=@company order by ctrlno
open c1
fetch next from c1 into @edt,@ctrlno
while @@fetch_status = 0
begin
if @cnt=0 
set @sql = 'update lic set sdt = ''' + convert(varchar(20),@m_sdt) + ''' where ctrlno=' + str(@ctrlno)
else 
set @sql = 'update lic set sdt = ''' + convert(varchar(20),@m_sdt) + ''' where ctrlno=' + str(@ctrlno)
execute(@sql)
set @m_sdt = dateadd(d,1,@edt)
fetch next from c1 into @edt,@ctrlno
end
close c1
deallocate c1




GO
/****** Object:  StoredProcedure [dbo].[UPDATELTONTARIFF]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[UPDATELTONTARIFF] 
/* with encryption */
AS
declare @sdt datetime
declare @edt datetime
declare @ctrlno integer
declare @m_sdt datetime
declare @cnt integer
declare @sql varchar(8000)
set @cnt=0
set @m_sdt = '1 jan 2000'
if cursor_status('global','c1') > 0
begin
close c1
deallocate c1
end

set @sql = 'declare c1 cursor for select edt,ctrlno from LTONTARIFF order by ctrlno'
execute(@sql)
open c1
fetch next from c1 into @edt,@ctrlno
while @@fetch_status = 0
begin
if @cnt=0 
set @sql = 'update LTONTARIFF set sdt = ''' + convert(varchar(20),@m_sdt) + ''' where ctrlno=' + str(@ctrlno)
else 
set @sql = 'update LTONTARIFF set sdt = ''' + convert(varchar(20),@m_sdt) + ''' where ctrlno=' + str(@ctrlno)
execute(@sql)
set @m_sdt = dateadd(d,1,@edt)
fetch next from c1 into @edt,@ctrlno
end
close c1
deallocate c1





GO
/****** Object:  StoredProcedure [dbo].[updaterentaccount]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


/*
Created on 12 August 2012
for updating the new rentaccount table for hotels installed previous to this date
*/

CREATE PROCEDURE [dbo].[updaterentaccount] 
/* with encryption */
AS
if not exists(select * from rentaccount where lrate=0)
insert into rentaccount(lrate,actnum) values (0,80)

if not exists(select * from rentaccount where lrate=10)
insert into rentaccount(lrate,actnum) values (10,15)

if not exists(select * from rentaccount where lrate=3)
insert into rentaccount(lrate,actnum) values (3,81)




GO
/****** Object:  StoredProcedure [dbo].[UPDATEstaahINV]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[UPDATEstaahINV]
@sdt datetime,@edt datetime
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
while datediff(d,@sdt,@edt) >=0
begin
if not exists(select * from staahinv where datediff(d,txndt,@sdt)=0)
insert into staahINV (txndt,status) values (@sdt,'')
set @sdt = dateadd(d,1,@sdt)
end

END


GO
/****** Object:  StoredProcedure [dbo].[updatevattabler]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Ramanathan>
-- Create date: <13 July 2017>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[updatevattabler] @outlet int
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;

declare @sdt datetime
declare @edt datetime
declare @ctrlno integer
declare @m_sdt datetime
declare @cnt integer
declare @sql varchar(8000)
set @cnt=0
set @m_sdt = '1 jan 2000'
if cursor_status('global','c1') > 0
begin
close c1
deallocate c1
end
set @sql = 'declare c1 cursor for select edt,ctrlno from vattabler where outlet=' + str(@outlet) + ' order by ctrlno'
execute(@sql)
open c1
fetch next from c1 into @edt,@ctrlno
while @@fetch_status = 0
begin
if @cnt=0 
set @sql = 'update vattabler set sdt = ''' + convert(varchar(20),@m_sdt) + ''' where ctrlno=' + str(@ctrlno)
else 
set @sql = 'update vattabler set sdt = ''' + convert(varchar(20),@m_sdt) + ''' where ctrlno=' + str(@ctrlno)

execute(@sql)
set @m_sdt = dateadd(d,1,@edt)
fetch next from c1 into @edt,@ctrlno
end
close c1
deallocate c1

END

GO
/****** Object:  StoredProcedure [dbo].[updatexxx123]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[updatexxx123] @asset char(1)
AS
BEGIN

declare @fy int
declare @actnum numeric
declare @tdno numeric
declare @balance numeric(18,2)
declare @asset1 char(1)

declare c1 cursor for select fy,cgp_Actno,balance,asset,tdno  from glprimld where asset=@asset and isnull(tdno,0)<>0 and  fy in (select fy from finyear where date=dbo.getbofy(getdate()))
open c1
fetch next from c1 into @fy,@actnum,@balance,@asset1,@tdno
while @@fetch_status=0
begin
if not exists(select * from xxx123 where fy=@fy and actnum=@actnum) 
insert into xxx123 (fy,actnum,tdno,balance,asset,total,balonac,d30,d60,d90,a90,cgp_desc,onac,tot) values (@fy,@actnum,@tdno,@balance,@asset1,0,0,0,0,0,0,'','',0)
update xxx123 set balance=@balance where fy=@fy and actnum=@actnum
fetch next from c1 into @fy,@actnum,@balance,@asset,@tdno
end
close c1
deallocate c1

update xxx123 set tdno=(select tdno from glprimld where glprimld.fy=xxx123.fy and glprimld.cgp_actno=xxx123.actnum)

update xxx123 set 
cgp_desc = isnull((select cgp_desc from glprimld where fy=xxx123.fy and cgp_actno=xxx123.actnum),'not known'),
onac=isnull((select onac from glprimld  where glprimld.fy=xxx123.fy and glprimld.cgp_actno=xxx123.actnum),''),
balance=isnull((select balance from glprimld where fy=xxx123.fy and cgp_actno=xxx123.actnum),0),
balonac=isnull((select onacbal from tdmaster where tdno=xxx123.tdno),0),
d30=isnull((SELECT SUM(BALANCE) FROM FDR WHERE TDNO=xxx123.TDNO and DATEDIFF(d,txndt,getdate()) between 0 and 30 ),0),
d60=isnull((SELECT SUM(BALANCE) FROM FDR WHERE TDNO=xxx123.TDNO and DATEDIFF(d,txndt,getdate()) between 31 and 60 ),0),
d90=isnull((SELECT SUM(BALANCE) FROM FDR WHERE TDNO=xxx123.TDNO and DATEDIFF(d,txndt,getdate()) between 61 and 90 ),0),
a90=isnull((SELECT SUM(BALANCE) FROM FDR WHERE TDNO=xxx123.TDNO and DATEDIFF(d,txndt,getdate()) > 90),0) ,
total=isnull((SELECT SUM(BALANCE) FROM FDR WHERE TDNO=xxx123.TDNO),0 )

declare @linkid numeric
select @linkid=max(linkid) from xxx123

update xxx123 set linkid=@linkid+ctrlno where linkid is null
delete from xxx123 where tdno is null
END
GO
/****** Object:  StoredProcedure [dbo].[updtfy]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[updtfy] 
/* with encryption */
AS
BEGIN
	SET NOCOUNT ON;
declare @fy int
declare @co int
declare @dt datetime
set @dt= getdate()
declare c1 cursor for select company from company
open c1
fetch next from c1 into @co
while @@fetch_status=0
begin
exec getfy @dt,@co,@fy out
fetch next from c1 into @co
end
close c1
deallocate c1

END





GO
/****** Object:  StoredProcedure [dbo].[updtrentmaster]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[updtrentmaster]  
/* with encryption */
AS
declare @rc  numeric
declare c1 cursor for select ctrlno from rentperiod
open c1
fetch next from c1 into @rc
while @@fetch_status=0
begin
insert into rentmaster (rtypectrlno,rno,rent1,rent2,renti1,renti2) 
select @rc,rno,rent1,rent2,rent1,rent2 from room where rno not in (select rno from rentmaster where rtypectrlno=@rc )
fetch next from c1 into @rc
end
close c1
deallocate c1




GO
/****** Object:  StoredProcedure [dbo].[updtrentperiod]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[updtrentperiod] @rtype integer  
/* with encryption */
AS

declare @sdt datetime
declare @edt datetime
declare @ctrlno integer
declare @m_sdt datetime
declare @cnt integer
set @cnt=0
set @m_sdt = '1 jan 2000'
if cursor_status('global','c1') > 0
begin
	close c1
	deallocate c1
end
declare c1 cursor for select edt,ctrlno from rentperiod where rtype=@rtype order by ctrlno
open c1
fetch next from c1 into @edt,@ctrlno
while @@fetch_status = 0
begin
if @cnt=0 
update rentperiod set sdt = @m_sdt where ctrlno=@ctrlno
else 
update rentperiod set sdt = @m_sdt where ctrlno=@ctrlno

set @m_sdt = dateadd(d,1,@edt)
fetch next from c1 into @edt,@ctrlno
end
close c1
deallocate c1




GO
/****** Object:  StoredProcedure [dbo].[updtSttable]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[updtSttable] 
/* with encryption */
AS
declare @sdt datetime
declare @edt datetime
declare @ctrlno integer
declare @m_sdt datetime
declare @cnt integer
set @cnt=0
set @m_sdt = '1 jan 2000'
if cursor_status('global','c1') > 0
begin
close c1
deallocate c1
end
declare c1 cursor for select edt,ctrlno from sttable order by ctrlno
open c1
fetch next from c1 into @edt,@ctrlno
while @@fetch_status = 0
begin
if @cnt=0 
update sttable set sdt = @m_sdt where ctrlno=@ctrlno
else 
update sttable set sdt = @m_sdt where ctrlno=@ctrlno

set @m_sdt = dateadd(d,1,@edt)
fetch next from c1 into @edt,@ctrlno
end
close c1
deallocate c1




GO
/****** Object:  StoredProcedure [dbo].[updtSttableR]    Script Date: 08/04/2026 16:48:12 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE PROCEDURE [dbo].[updtSttableR] @TNAME varchar(50),@sttableno varchar(10)
/* with encryption */
AS
declare @sdt datetime
declare @edt datetime
declare @ctrlno integer
declare @m_sdt datetime
declare @cnt integer
declare @sql varchar(8000)
set @cnt=0
set @m_sdt = '1 jan 2000'
if cursor_status('global','c1') > 0
begin
close c1
deallocate c1
end
set @sql = 'declare c1 cursor for select edt,ctrlno from ' + @tname + ' where sttableno=' + @sttableno + ' order by ctrlno'
execute(@sql)
open c1
fetch next from c1 into @edt,@ctrlno
while @@fetch_status = 0
begin
if @cnt=0 
set @sql = 'update ' + @tname + ' set sdt = ''' + convert(varchar(20),@m_sdt) + ''' where ctrlno=' + str(@ctrlno)
else 
set @sql = 'update ' + @tname + ' set sdt = ''' + convert(varchar(20),@m_sdt) + ''' where ctrlno=' + str(@ctrlno)

execute(@sql)
set @m_sdt = dateadd(d,1,@edt)
fetch next from c1 into @edt,@ctrlno
end
close c1
deallocate c1




GO
